Oracle Coherence Filter Condition Example

Oracle Coherence Filter Condition Example

To retrieve the stored data in Oracle coherence cache it provides in build API which can be use as filtering criteria to in data retrieval process and its same as when you execute SQL query put filer condition in where clause. There are many filters API are available which can be use but here I will show you only which people use frequently:

In this example class I will show you:

  • BetweenFilter
  • OrFilter
  • EqualsFilter
  • AllFilter
  • InFilter
  • AndFilter
  • GetDataByCacheKey

CoherenceFilterExample.java:

public class CoherenceFilterExample {

	public static void main(String[] args) {
		
		NamedCache cache = (NamedCache) CacheRegistry.getNamedCache("CacheName");
		
		betweenDateFilter(cache);
		
		OrFilterTest(cache);
		
		equalsFilterTest(cache);
		
		allFilterTest(cache);
		
		inFilterTest(cache);
		
		andFilterTest(cache);
		
		getDataBasedOnKey(cache);
		
	}
	
	private static void getDataBasedOnKey(NamedCache cache) {
		
		String key = "123456";
		
		Object object = cache.get(key);
		System.out.println(object);
	}	

	@SuppressWarnings("unchecked")
	private static void andFilterTest(NamedCache cache) {
		
		//Pass index value to attribute extractor
		AttributeItemExtractor eventType = new AttributeItemExtractor(1);
		Filter eventTypeFilter = new EqualsFilter(eventType, "eventType");
		
		AttributeItemExtractor processStatus = new AttributeItemExtractor(2);		
		Filter processStatusFilter = new EqualsFilter(processStatus, "NEW");
		
		Filter andFilter = new AndFilter(eventTypeFilter, processStatusFilter);
		
		Set<Entry<String, Object>> resutls = cache.entrySet(andFilter);
		
		System.out.println("Got data with size: "+resutls.size());
		
		for (Entry<String, Object> entry : resutls) {
			System.out.println(entry);
		}
	}

	@SuppressWarnings("unchecked")
	private static void inFilterTest(NamedCache cache) {
		
		//Pass index value to attribute extractor
		AttributeItemExtractor processStatus = new AttributeItemExtractor(1);
		
		Set<String> value = new HashSet<String>();
		value.add("NEW");
		
		Filter inFilter = new InFilter(processStatus, value);
		
		Set<Entry<String, Object>> resutls = cache.entrySet(inFilter);
		
		System.out.println("Got data with size: "+resutls.size());
		
		for (Entry<String, Object> entry : resutls) {
			System.out.println(entry);
		}
	}

	@SuppressWarnings("unchecked")
	private static void allFilterTest(NamedCache cache) {
		
		//Pass index value to attribute extractor
		AttributeItemExtractor processStatus = new AttributeItemExtractor(1);
		
		Filter processStatusFilter = new EqualsFilter(processStatus, "NEW");
		Filter processStatusFilter2 = new EqualsFilter(processStatus, "Processed");
		Filter processStatusFilterOR = new OrFilter(processStatusFilter, processStatusFilter2);
		
		AttributeItemExtractor eventType = new AttributeItemExtractor(2);
		Filter equalsFilter = new EqualsFilter(eventType, "eventType");
		
		Filter filterArray [] = {processStatusFilterOR,equalsFilter};
		Filter filter = new AllFilter(filterArray);
		
		Set<Entry<String, Object>> resutls = cache.entrySet(filter);
		
		System.out.println("Got data with size: "+resutls.size());
		
		for (Entry<String, Object> entry : resutls) {
			System.out.println(entry);
		}
	}

	@SuppressWarnings("unchecked")
	private static void equalsFilterTest(NamedCache cache) {
		
		//Pass index value to attribute extractor
		AttributeItemExtractor eventType = new AttributeItemExtractor(2);
		Filter equalsFilter = new EqualsFilter(eventType, "eventType");
		Set<Entry<String, Object>> resutls = cache.entrySet(equalsFilter);
		
		System.out.println("Got data with size: "+resutls.size());
		
		for (Entry<String, Object> entry : resutls) {
			System.out.println(entry);
		}
	}

	@SuppressWarnings("unchecked")
	private static void OrFilterTest(NamedCache cache) {
		
		//Pass index value to attribute extractor
		AttributeItemExtractor processStatus = new AttributeItemExtractor(1);
		
		Filter processStatusFilter = new EqualsFilter(processStatus, "NEW");
		Filter processStatusFilter2 = new EqualsFilter(processStatus, "Processed");
		Filter processStatusFilterOR = new OrFilter(processStatusFilter, processStatusFilter2);
		
		Set<Entry<String, Object>> resutls = cache.entrySet(processStatusFilterOR);
		
		System.out.println("Got data with size: "+resutls.size());
		
		for (Entry<String, Object> entry : resutls) {
			System.out.println(entry);
		}
	}

	@SuppressWarnings({ "unchecked" })
	private static void betweenDateFilter(NamedCache cache) {
		
		//Pass index value to attribute extractor
		LocalDateTime localDateToStart = LocalDate.now().atStartOfDay();				
		LocalDateTime localDateToEnd	= localDateToStart.plusDays(1).minus(1, ChronoUnit.SECONDS);
		
		Date dateToStart = Date.from(localDateToStart.atZone(ZoneId.systemDefault()).toInstant());
		Date dateToEnd = Date.from(localDateToEnd.atZone(ZoneId.systemDefault()).toInstant());
		
		AttributeItemExtractor eventDate = new AttributeItemExtractor(3);
		
		Filter betweenDateFilter = new BetweenFilter(eventDate, dateToStart, dateToEnd);
		
		Set<Entry<String, Object>> resutls = cache.entrySet(betweenDateFilter);
		
		System.out.println("Got data with size: "+resutls.size());
		
		for (Entry<String, Object> entry : resutls) {
			System.out.println(entry);
		}
	}

}

 

  • For more information to use coherence filter please visit Oracle coherence API here

Leave a Reply

Your email address will not be published. Required fields are marked *