Reviews
Oracle CoherenceOracle Coherence: Controlling Query Scope Using Data Affinity
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.
However, this optimization is not automatic--you have to target the partition to search explicitly, using KeyAssociatedFilter:
- Filter query = ...
Filter filter = new KeyAssociatedFilter(query, key)
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:
- public Collection<transaction> getTransactions(Date from, Date to) { return getTransactionRepository().findTransactions(m_id, from, to)
}<p> </p><br>
Finally, we need to implement the findTransactions method within the CoherenceTransactionRepository:
- 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()) }
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.
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







