Exforsys

Home arrow Reviews arrow Oracle Coherence

Oracle Coherence: Controlling Query Scope Using Data Affinity

Author: Packt Publishing     Published on: 2nd Jul 2010

As we discussed in the previous chapter, data affinity can provide a significant performance boost because it allows Coherence to optimize the query for related objects. Instead of executing the query in parallel across all the nodes and aggregating the results, Coherence can simply execute it on a single node, because data affinity guarantees that all the results will be on that particular node. This effectively reduces the number of objects searched to approximately C/N, where C is the total number of objects in the cache query is executed against, and N is the number of partitions in the cluster.

Ads

However, this optimization is not automatic--you have to target the partition to search explicitly, using KeyAssociatedFilter:


Sample Code
  1.     Filter query = ...    Filter filter = new KeyAssociatedFilter(query, key)
Copyright exforsys.com



In the previous example, we create a KeyAssociatedFilter that wraps the query we want to execute. The second argument to its constructor is the cache key that determines the partition to search.

To make all of this more concrete, let's look at the final implementation of the code for our sample application that returns account transactions for a specific period. First, we need to add the getTransactions method to our Account class:


Sample Code
  1.     public Collection<transaction> getTransactions(Date from, Date to) {      return getTransactionRepository().findTransactions(m_id, from, to)    }<p> </p><br>
Copyright exforsys.com


Finally, we need to implement the findTransactions method within the CoherenceTransactionRepository:


Sample Code
  1.     public Collection<transaction> findTransactions(                         Long accountId, Date from, Date to) {        Filter filter = new FilterBuilder()                  .equals("id.accountId", accountId)                  .between("time", from, to)                  .build()        return queryForValues(                  new KeyAssociatedFilter(filter, accountId),                  new DefaultTransactionComparator())    }
Copyright exforsys.com


Ads


As you can see, we target the query using the account identifier and ensure that the results are sorted by transaction number by passing DefaultTransactionComparator to the queryForValues helper method we implemented earlier. This ensures that Coherence looks for transactions only within the partition that the account with the specified id belongs to.



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

Oracle Coherence

 

Comments