Oracle® Coherence Java API Reference
v3.5.3

E15583-01

com.tangosol.util.filter
Class KeyAssociatedFilter

java.lang.Object
  extended by com.tangosol.util.Base
      extended by com.tangosol.util.BitHelper
          extended by com.tangosol.util.ExternalizableHelper
              extended by com.tangosol.util.filter.KeyAssociatedFilter
All Implemented Interfaces:
ExternalizableLite, PortableObject, Filter, java.io.Serializable

public class KeyAssociatedFilter
extends ExternalizableHelper
implements Filter, ExternalizableLite, PortableObject

Filter which limits the scope of another filter according to the key association information.

This filter is intended to be used to optimize queries for partitioned caches that utilize any of the key association algorithms (by implementing either KeyAssociator or KeyAssociation) to ensure placement of all associated entries in the same distributed cache partition (and therefore in the same storage-enabled cluster node). Using the KeyAssociatedFilter will instruct the distributed cache to apply the wrapped filter only to the entries stored at the cache service node that owns the specified host key.

Note 1: This filter must be the outermost filter and cannot be used as a part of any composite filter (AndFilter, OrFilter, etc.)

Note 2: This filter is intended to be processed only on the client side of the partitioned cache service.

For example, consider two classes called Parent and Child that are stored in separate caches using ParentKey and ChildKey objects respectively. The Parent and Child classes have a getId method that returns a Long value that uniquely identifies the object. Similarly, the ParentKey and ChildKey classes have a getId method that uniquely identifies the corresponding cached object. Futhermore, the Child and ChildKey classes include a getParentId method that returns the Long identifier of the Parent object.

There are two ways to ensure that Child objects are collocated with their Parent objects (in the same storage-enabled cluster node).

  1. Make the ChildKey class implement KeyAssociation as follows:
     public Object getAssociatedKey()
         {
         return getParentId();
         }
    and the ParentKey class implement KeyAssociation as follows:
     public Object getAssociatedKey()
         {
         return getId();
         }
  2. Implement a custom KeyAssociator as follows:
     public Object getAssociatedKey(Object oKey)
         {
         if (oKey instanceof ChildKey)
             {
             return ((ChildKey) oKey).getParentId();
             }
         else if (oKey instanceof ParentKey)
             {
             return ((ParentKey) oKey).getId();
             }
         else
             {
             return null;
             }
         }
The first approach requires a trivial change to the ChildKey and ParentKey classes, whereas the second requires a new class and a configuration change, but no changes to existing classes.

Now, to retrieve all the Child objects of a given Parent using an optimized query you would do the following:

 ParentKey parentKey = new ParentKey(...);
 Long      parentId  = parentKey.getId();

 // this Filter will be applied to all Child objects in order to fetch those
 // for which getParentId() returns the specified Parent identifier
 Filter filterEq = new EqualsFilter("getParentId", parentId);

 // this Filter will direct the query to the cluster node that currently
 // owns the Parent object with the given identifier
 Filter filterAsc = new KeyAssociatedFilter(filterEq, parentId);

 // run the optimized query to get the ChildKey objects
 Set setChildKeys = cacheChildren.keySet(filterAsc);

 // get all the Child objects at once
 Set setChildren = cacheChildren.getAll(setChildKeys);
 
To remove the Child objects you would then do the following:
 cacheChildren.keySet().removeAll(setChildKeys);

Author:
gg 2005.06.09, jh 2005.11.02

Constructor Summary
KeyAssociatedFilter()
          Default constructor (required by ExternalizableLite interface).
KeyAssociatedFilter(Filter filter, java.lang.Object oHostKey)
          Construct a key associated filter.
 
Method Summary
 boolean equals(java.lang.Object o)
          Compare the KeyAssociatedFilter with another object to determine equality.
 boolean evaluate(java.lang.Object o)
          Apply the test to the object.
 Filter getFilter()
          Obtain the wrapped Filter.
 java.lang.Object getHostKey()
          Obtain the host key that serves as an associated key for all keys that the wrapped filter will be applied to.
 int hashCode()
          Determine a hash value for the KeyAssociatedFilter object according to the general Object.hashCode() contract.
 void readExternal(java.io.DataInput in)
          Restore the contents of this object by loading the object's state from the passed DataInput object.
 void readExternal(PofReader in)
          Restore the contents of a user type instance by reading its state using the specified PofReader object.
 java.lang.String toString()
          Return a human-readable description for this Filter.
 void writeExternal(java.io.DataOutput out)
          Save the contents of this object by storing the object's state into the passed DataOutput object.
 void writeExternal(PofWriter out)
          Save the contents of a POF user type instance by writing its state using the specified PofWriter object.
 
Methods inherited from class com.tangosol.util.BitHelper
countBits, countBits, countBits, indexOfLSB, indexOfLSB, indexOfLSB, indexOfMSB, indexOfMSB, indexOfMSB, rotateLeft, rotateLeft, rotateLeft, rotateRight, rotateRight, rotateRight, toBitString, toBitString, toBitString
 

Constructor Detail

KeyAssociatedFilter

public KeyAssociatedFilter()
Default constructor (required by ExternalizableLite interface).


KeyAssociatedFilter

public KeyAssociatedFilter(Filter filter,
                           java.lang.Object oHostKey)
Construct a key associated filter.

Parameters:
filter - the underlying (wrapped) filter
oHostKey - the host key that serves as an associated key for all keys that the wrapped filter will be applied to
Method Detail

evaluate

public boolean evaluate(java.lang.Object o)
Apply the test to the object.

Specified by:
evaluate in interface Filter
Parameters:
o - the object to test
Returns:
true if the test passes, false otherwise

readExternal

public void readExternal(java.io.DataInput in)
                  throws java.io.IOException
Restore the contents of this object by loading the object's state from the passed DataInput object.

Specified by:
readExternal in interface ExternalizableLite
Parameters:
in - the DataInput stream to read data from in order to restore the state of this object
Throws:
java.io.IOException - if an I/O exception occurs
java.io.NotActiveException - if the object is not in its initial state, and therefore cannot be deserialized into

writeExternal

public void writeExternal(java.io.DataOutput out)
                   throws java.io.IOException
Save the contents of this object by storing the object's state into the passed DataOutput object.

Specified by:
writeExternal in interface ExternalizableLite
Parameters:
out - the DataOutput stream to write the state of this object to
Throws:
java.io.IOException - if an I/O exception occurs

readExternal

public void readExternal(PofReader in)
                  throws java.io.IOException
Restore the contents of a user type instance by reading its state using the specified PofReader object.

Specified by:
readExternal in interface PortableObject
Parameters:
in - the PofReader from which to read the object's state
Throws:
java.io.IOException - if an I/O error occurs

writeExternal

public void writeExternal(PofWriter out)
                   throws java.io.IOException
Save the contents of a POF user type instance by writing its state using the specified PofWriter object.

Specified by:
writeExternal in interface PortableObject
Parameters:
out - the PofWriter to which to write the object's state
Throws:
java.io.IOException - if an I/O error occurs

getFilter

public Filter getFilter()
Obtain the wrapped Filter.

Returns:
the wrapped filter object

getHostKey

public java.lang.Object getHostKey()
Obtain the host key that serves as an associated key for all keys that the wrapped filter will be applied to.

Returns:
the host key

equals

public boolean equals(java.lang.Object o)
Compare the KeyAssociatedFilter with another object to determine equality. Two KeyAssociatedFilter objects are considered equal iff the wrapped filters and host keys are equal.

Returns:
true iff this KeyAssociatedFilter and the passed object are equivalent KeyAssociatedFilter objects

hashCode

public int hashCode()
Determine a hash value for the KeyAssociatedFilter object according to the general Object.hashCode() contract.

Returns:
an integer hash value for this KeyAssociatedFilter object

toString

public java.lang.String toString()
Return a human-readable description for this Filter.

Returns:
a String description of the Filter

Oracle® Coherence Java API Reference
v3.5.3

E15583-01

Copyright © 2000, 2010, Oracle. All rights reserved.