Exforsys

Home arrow Reviews arrow Oracle Coherence

Oracle Coherence: Creating Indexes

Author: Packt Publishing     Published on: 6th Jul 2010

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:

Ads

Sample Code
  1.     public interface QueryMap extends Map {
  2.       Set keySet(Filter filter)
  3.       Set entrySet(Filter filter)
  4.       Set entrySet(Filter filter, Comparator comparator)
  5.  
  6.       void addIndex(ValueExtractor extractor,
  7.                          boolean isOrdered,
  8.                          Comparator comparator)
  9.  
  10.         void removeIndex(ValueExtractor extractor)
  11.     }
  12.  
Copyright exforsys.com


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:

Sample Code
  1.     NamedCache people = CacheFactory.getCache("people")
  2.     people.addIndex(new PropertyExtractor("firstName"), false, null)
  3.     people.addIndex(new PropertyExtractor("lastName"), true, null)
  4.  
Copyright exforsys.com


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.

Ads

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.



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

Oracle Coherence

 

Comments