QueryExample runs sample queries for contacts.

The purpose of this example is to show how to create Extractors on cache data and how to create a KeyExtractor for the cache keys. It also illustrates how to use the indexes to filter the dataset in order to efficiently create a matching set. Finally, the example demonstrates how to use some of the built-in cache aggregators to do simple computational tasks on the cache data. A subset of the code is shown below.

Java Query Example

Implementation Class: com.tangosol.examples.contacts.QueryExample in java/src

Add an index to make queries more efficient.

cache.addIndex(new ChainedExtractor("getHomeAddress.getState"), /*fOrdered*/ false, /*comparator*/ null);

Find all contacts who live in Massachusetts.

Set setResults = cache.entrySet(new EqualsFilter("getHomeAddress.getState", "MA"));

Count contacts who are older than nAge for the entire cache dataset.

System.out.println("count > " + nAge + ": " + cache.aggregate(new GreaterFilter("getAge", nAge), new Count()));

.NET Query Example

Implementation Class: QueryExample in namespace Tangosol.Examples.Contacts in dotnet/src/contacts

Add an index to make queries more efficient.

cache.AddIndex(new ChainedExtractor("getHomeAddress.getState"),/*fOrdered*/ false, /*comparator*/ null);

Find all contacts who live in Massachusetts.

ICacheEntry[] aCacheEntry = cache.GetEntries(new EqualsFilter("getHomeAddress.getState", "MA"));

Count contacts who are older than nAge for the entire cache dataset.

Console.WriteLine("count > " + nAge + ": "+ cache.Aggregate(new GreaterFilter("getAge", nAge), new Count()));

C++ Query Example

Implementation Class: QueryExample in namespace coherence::examples in cpp/contacts

Add an index to make queries more efficient.

ValueExtractor::View vHomeStateExtractor = ChainedExtractor::create(
   ChainedExtractor::createExtractors("getHomeAddress.getState"));

Find all contacts who live in Massachusetts.

Object::View voStateName = String::create("MA");
Set::View setResults = hCache->entrySet(
      EqualsFilter::create(vHomeStateExtractor, voStateName));

Count contacts who are older than nAge for the entire cache dataset.

Integer32::View nAge = Integer32::valueOf(58);
Object::View vResult = hCache->aggregate(
      (Filter::View) GreaterFilter::create(vAgeExtractor, nAge),
         Count::create());
std::cout << "count > " << nAge->getValue() << ": " << vResult << std::endl;

Example Output

The example output is large due to 10,000 contacts and several queries. A sample of the query for Massachusetts residents:


MA Residents
ConverterEntry{Key="John Scqngqda", Value="John Scqngqda
Addresses
Home: 265 Beacon St.

Oaskxm, MA 88259
US
Work: Yoyodyne Propulsion Systems
330 Lectroid Rd.
Grover's Mill, OK 95744
US
Phone Numbers
work: +11 88 903 8991283
home: +11 98 553 5878221
Birth Date: 1960-01-03"}