Reviews
Oracle CoherenceOracle Coherence: Creating Indexes
Now that we know how Coherence indexes are structured and why we should use them, let's look at how we can create them.
At the beginning of this chapter, we showed an incomplete definition of a QueryMap interface. What we omitted from it are the two methods that allow us to create and remove cache indexes:
- public interface QueryMap extends Map {
- Set keySet(Filter filter)
- Set entrySet(Filter filter)
- Set entrySet(Filter filter, Comparator comparator)
- void addIndex(ValueExtractor extractor,
- boolean isOrdered,
- Comparator comparator)
- void removeIndex(ValueExtractor extractor)
- }
As you can see, in order to create an index you need to specify three things:
- The value extractor that should be used to retrieve attribute value to use as an index key
- The flag specifying whether the index should be ordered or not
- Finally, the comparator to use for ordering
The first argument is by far the most important, as it determines index contents. It is also used as the index identifier, which is why you need to ensure that all value extractors you create implement the equals and hashCode methods properly.
If you decide to create an ordered index, index entries will be stored within a SortedMap instance, which introduces some overhead on index updates. Because of that, you should only order indexes for attributes that are likely to be used for sorting query results or in range queries, such as greater, less, between, and so on.
The last argument allows you to specify a comparator to use for index ordering, but you can specify null if the attribute you are indexing implements the Comparable interface and you want the index to use natural ordering. Of course, if an index is not ordered, you should always specify null for this argument.
Now that we know all of this, let's see what the code to define indexes on firstName and lastName attributes from the previous example should look like:
- NamedCache people = CacheFactory.getCache("people")
- people.addIndex(new PropertyExtractor("firstName"), false, null)
- people.addIndex(new PropertyExtractor("lastName"), true, null)
As you can see, adding indexes to a cache is very simple. In this case we have created an unordered index on the firstName attribute and an ordered index using natural string ordering on the lastName attribute.
The last thing you should know is that the call to the addIndex method is treated by Coherence as a hint that an index should be created. What this means in practice is that you can safely create the same set of indexes on each Coherence node, even if another node has already created those same indexes. If an index for the specified extractor already exists, Coherence will simply ignore all subsequent requests for its creation.
Oracle Coherence
- Oracle Coherence 3.5
- Oracle Coherence: Distributed Caching
- Oracle Coherence: In-place and Parallel Processing
- Oracle Coherence: Coherence within the Oracle Ecosystem
- Oracle Coherence: Querying The Data Grid
- Oracle Coherence: Built-in Filters
- Oracle Coherence: Value and Reflection Extractor
- Oracle Coherence: Other built-in value extractors
- Oracle Coherence: Implementing Custom Value Extractor
- Oracle Coherence: Simplifying Coherence Queries
- Oracle Coherence: Obtaining Query Results
- Oracle Coherence: Controlling Query Scope Using Data Affinity
- Oracle Coherence: Querying Near Cache
- Oracle Coherence: Paging Over Query Results
- Oracle Coherence: Anatomy of an Index
- Oracle Coherence: Creating Indexes
- Oracle Coherence: Query Limitations
- Oracle Coherence: Aggregators
- Oracle Coherence: Built-in Aggregators
- Oracle Coherence: Implementing LookupValuesAggregator







