Exforsys

Home arrow Reviews arrow Oracle Coherence

Oracle Coherence: Obtaining Query Results

Author: Packt Publishing     Published on: 1st Jul 2010

The easiest way to obtain query results is to invoke one of the QueryMap.entrySet methods:

Ads

Sample Code
  1.     Filter filter = ...
  2.     Set<map.entry></map.entry> results = cache.entrySet(filter)
  3.  
Copyright exforsys.com


This will return a set of Map.Entry instances representing both the key and the value of a cache entry, which is likely not what you want. More often than not you need only values, so you will need to iterate over the results and extract the value from each Map.Entry instance:

Sample Code
  1.     List values = new ArrayList(results.size())
  2.     for (Map.Entry entry : entries) {
  3.         values.add(entry.getValue())
  4.     }
  5.  
Copyright exforsys.com


After doing this a couple times you will probably want to create a utility method for this task. Because all the queries should be encapsulated within various repository implementations, we can simply add the following utility methods to our AbstractCoherenceRepository class:

Sample Code
  1.    public abstract class AbstractCoherenceRepository> {
  2.  
  3.       ...
  4.  
  5.       protected Collection<v></v> queryForValues(Filter filter) {
  6.         Set> entries = getCache().entrySet(filter)
  7.         return extractValues(entries)
  8.       }
  9.  
  10.       protected Collection<v></v> queryForValues(Filter filter,
  11.                                              Comparator comparator) {
  12.         Set> entries =
  13.                 getCache().entrySet(filter, comparator)
  14.         return extractValues(entries)
  15.       }
  16.  
  17.       private Collection<v></v> extractValues(Set> entries) {
  18.         List<v></v> values = new ArrayList<v></v>(entries.size())
  19.         for (Map.Entry entry : entries) {
  20.           values.add(entry.getValue())
  21.         }
  22.         return values
  23.       }
  24.  
Copyright exforsys.com


Ads

What happened to the QueryMap.values() method?
Obviously, things would be a bit simpler if the QueryMap interface also had an overloaded version of the values method that accepts a filter and optionally comparator as arguments. I'm not sure why this functionality is missing from the API, but I hope it will be added in one of the future releases. In the meantime, a simple utility method is all it takes to provide the missing functionality, so I am not going to complain too much.



 
This tutorial is part of a Oracle Coherence tutorial series. Read it from the beginning and learn yourself.

Oracle Coherence

 

Comments