Exforsys

Home arrow Reviews arrow Oracle Coherence

Oracle Coherence: Anatomy of an Index

Author: Packt Publishing     Published on: 5th Jul 2010

A Coherence index is an instance of a class that implements the com.tangosol.util.MapIndex interface:

Ads

Sample Code
  1.     public interface MapIndex {
  2.       ValueExtractor getValueExtractor()
  3.       boolean isOrdered()
  4.       Map getIndexContents()
  5.       Object get(Object key)
  6.     }
Copyright exforsys.com


The getValueExtractor method returns the value extractor used to extract the attribute that should be indexed from an object, while the isOrdered method returns whether the index is sorted or not.

The get method allows us to obtain a value of the indexed attribute for the specified key directly from an index, which avoids object deserialization and repeat value extraction.

Finally, the getIndexContents method returns the actual index contents. This is a map that uses the value extracted from the indexed attribute as a key, while the value for each index entry is a set of cache keys corresponding to that attribute value.

Looking at an example should make the previous paragraph much easier to understand.

Let's assume that we have the following entries in the cache:

Sample Code
  1.        Key    Value
  2.        1      Person(firstName    =   'Aleksandar',   lastName    =   'Seovic')
  3.        2      Person(firstName    =   'Marija',       lastName    =   'Seovic')
  4.        3      Person(firstName    =   'Ana Maria',    lastName    =   'Seovic')
  5.        4      Person(firstName    =   'Novak',        lastName    =   'Seovic')
  6.        5      Person(firstName    =   'Aleksandar',   lastName    =   'Jevic')
  7.  
Copyright exforsys.com


If we create an index on the lastName attribute, our index contents will look like this:

Sample Code
  1.                            Key           Value
  2.                            Jevic         {5}
  3.                            Seovic        { 1, 2, 3, 4 }      
Copyright exforsys.com


On the other hand, an index on the firstName attribute will look like this:

Sample Code
  1.                                Key                   Value
  2.                                Aleksandar            { 1, 5 }
  3.                                Ana Maria             {3}
  4.                                Marija                {2}
  5.                                Novak                 {4}
Copyright exforsys.com


Index internals
Keep in mind that while I'm showing the actual values in the previous examples, the Coherence index actually stores both keys and values in their internal binary format. For the most part you shouldn't care about this fact, but it is good to know if you end up accessing index contents directly.

The previous example should also make it obvious why indexes have such a profound effect on query performance.

If we wanted to obtain a list of keys for all people in the cache that have last name 'Seovic', without an index Coherence would have to deserialize each cache entry, extract the lastName attribute and perform a comparison, and if the comparison matches then it would add the cache entry key to the resulting list of keys.

Ads

With an index, Coherence doesn't need to do any of this--it will simply look up an index entry with the key Seovic and return the set of keys from that index entry.



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

Oracle Coherence

 

Comments