#include <coherence/util/filter/KeyAssociatedFilter.hpp>
Inherits Object, PortableObject, and Filter.
This filter is intended to be used to optimize queries for partitioned caches that utilize any of the key association algorithms (by implementing either coherence::net::partition::KeyAssociator or coherence::net::cache::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.)
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 collected with their Parent objects (in the same storage-enabled cluster node).
virtual Object::View getAssociatedKey() const { return getParentId(); }and the ParentKey class implement coherence::net::cache::KeyAssociation as follows:
public: virtual Object::View getAssociatedKey() { return getId(); }Note: a Java version of these classes must also exist and contain the same logic
Now, to retrieve all the Child objects of a given Parent using an optimized query you would do the following:
ParentKey::Handle parentKey = ParentKey::create(...); Integer64::View 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::View filterEq = EqualsFilter::create(ReflectionExtractor::create( "getParentId"), parentId);
// this Filter will direct the query to the cluster node that currently // owns the Parent object with the given identifier Filter::View filterAsc = KeyAssociatedFilter::create(filterEq, parentId);
// run the optimized query to get the ChildKey objects Set::View setChildKeys = cacheChildren->keySet(filterAsc);
// get all the Child objects at once Set::Handle setChildren = cacheChildren->getAll(setChildKeys);To remove the Child objects you would then do the following:
cacheChildren->keySet()->removeAll(setChildKeys);
Public Types | ||||
typedef spec::Handle | Handle | |||
KeyAssociatedFilter Handle definition. | ||||
typedef spec::View | View | |||
KeyAssociatedFilter View definition. | ||||
typedef spec::Holder | Holder | |||
KeyAssociatedFilter Holder definition. | ||||
Public Member Functions | ||||
virtual bool | evaluate (Object::View v) const | |||
Apply the test to the object.
| ||||
virtual void | readExternal (PofReader::Handle hIn) | |||
virtual void | writeExternal (PofWriter::Handle hOut) const | |||
virtual bool | equals (Object::View v) const | |||
virtual size32_t | hashCode () const | |||
Return a hash code value for the Object. This method is supported for the benefit of hash-based containers.
The general contract of
The default implementation is identity based.
| ||||
virtual TypedHandle < const String > | toString () const | |||
Output a human-readable description of this Object to the given stream. Note that when overriding this method the return type must be TypedHandle<const String> rather then String::View. These two types are assignment compatible but not equivalent and declaring the override with String::View will not be a compatible override. coherence::lang::operator<<(std::ostream, Object::View) is defined and will call into the toString method, to output Objects. If a managed String object is desired, the COH_TO_STRING macro can be used to build up a String from streamable contents and is generally how toString() will be implemented.
Object::View vKey = ... Object::View vValue = ... std::cout << vKey << " = " << vValue << std::endl; String::View vs = COH_TO_STRING(vKey << " = " << vValue); The COH_TO_STRING macro is also the most common way to implement the toString method. For example:
virtual TypedHandle<const String> Person::toString() const { return COH_TO_STRING("Name: " << f_sName << " SSN: " << f_nSSN); }
| ||||
virtual Filter::View | getFilter () const | |||
Obtain the wrapped Filter. | ||||
virtual Object::View | getHostKey () const | |||
Obtain the host key that serves as an associated key for all keys that the wrapped filter will be applied to. | ||||
Protected Member Functions | ||||
KeyAssociatedFilter () | ||||
Default constructor (necessary for the PortableObject interface). | ||||
KeyAssociatedFilter (Filter::View vFilter, Object::View vHostKey) | ||||
Construct a key associated filter. | ||||
Protected Attributes | ||||
FinalView< Filter > | f_vFilter | |||
The underlying filter. | ||||
FinalView< Object > | f_vHostKey | |||
The association host key. |
KeyAssociatedFilter | ( | Filter::View | vFilter, | |
Object::View | vHostKey | |||
) | [protected] |
virtual Filter::View getFilter | ( | ) | const [virtual] |
virtual Object::View getHostKey | ( | ) | const [virtual] |
Obtain the host key that serves as an associated key for all keys that the wrapped filter will be applied to.