Oracle Fusion Middleware – “How to Query Oracle Coherence” ?

In my previous post “Oracle Fusion Middleware – Concepts of Oracle Coherence” i have explained how to preLoad cache which means get all the required details for the first time and put that into cache. Today i am going to explain how to query the cache once we have the details.

Querying in Oracle Coherence is obtained by set of Filters that the Coherence has provided us. Please be noted that queries apply only to currently cached data thus, the data set should be loaded entirely into cache before queries are performed(go through my preLoading concept in the previous post).

The concept of querying is based on the ValueExtractor interface. A value extractor is used to extract an attribute from a given object for querying Most developers need only the ReflectionExtractor implementation of this interface. The implementation uses reflection to extract an attribute from a value object by referring to a method name which is typically a getter method

Let me show you the example which is connected to my previous post:

/**
 * 
 */
package com.spark.coherence.cache.filters;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.spark.coherence.cache.repository.CacheRepository;
import com.spark.coherence.pof.beans.EmployeeBean;
import com.tangosol.util.Filter;
import com.tangosol.util.ValueExtractor;
import com.tangosol.util.extractor.ReflectionExtractor;
import com.tangosol.util.filter.EqualsFilter;
import com.tangosol.util.filter.GreaterEqualsFilter;
import com.tangosol.util.filter.GreaterFilter;

/**
 * @author Sony
 *
 */
public class EmployeeFilter {

	ValueExtractor valueExtractor;

	@SuppressWarnings("rawtypes")
	public EmployeeBean equalsFilter(String methodName, String methodParam) {
		valueExtractor = new ReflectionExtractor(methodName);
		Filter filter = new EqualsFilter(valueExtractor, methodParam);
		EmployeeBean person = null;
		for (Iterator iter = CacheRepository.getEmployeeCache().entrySet(filter).iterator(); iter.hasNext();) {
			Map.Entry entry = (Map.Entry) iter.next();
			Integer key = (Integer) entry.getKey();
			person = (EmployeeBean) entry.getValue();
		}
		return person;
	}
	
	@SuppressWarnings("rawtypes")
	public List<EmployeeBean> greaterThanFilter(String methodName, int methodParam) {
		valueExtractor = new ReflectionExtractor(methodName);
		Filter filter = new GreaterEqualsFilter(valueExtractor, methodParam);
		List<EmployeeBean> employeeBeans = new ArrayList<>();
		for (Iterator iter = CacheRepository.getEmployeeCache().entrySet(filter).iterator(); iter.hasNext();) {
			Map.Entry entry = (Map.Entry) iter.next();
			Integer key = (Integer) entry.getKey();
			EmployeeBean person = (EmployeeBean) entry.getValue();
			employeeBeans.add(person);
		}
		return employeeBeans;
	}

}

Happy Caching..Happy Coding 🙂

Advertisements