Package org.apache.ignite.cache.affinity
Class AffinityKey<K>
- java.lang.Object
-
- org.apache.ignite.cache.affinity.AffinityKey<K>
-
- All Implemented Interfaces:
Externalizable,Serializable
- Direct Known Subclasses:
AffinityUuid
public class AffinityKey<K> extends Object implements Externalizable
Optional wrapper for cache keys to provide support for custom affinity mapping. The value returned byaffinityKey(Object)method will be used for key-to-node affinity.Note that the
equals(Object)andhashCode()methods delegate directly to the wrapped cache key provided bykey()method.This class is optional and does not have to be used. It only provides extra convenience whenever custom affinity mapping is required. Here is an example of how
Personobjects can be collocated withCompanyobjects they belong to:Object personKey = new AffinityKey(myPersonId, myCompanyId); // Both, the company and the person objects will be cached on the same node. cache.put(myCompanyId, new Company(..)); cache.put(personKey, new Person(..));
For more information and examples of cache affinity refer to
AffinityKeyMapperand@AffinityKeyMappeddocumentation.
-
-
Constructor Summary
Constructors Constructor Description AffinityKey()Empty constructor.AffinityKey(K key)Initializes key wrapper for a given key.AffinityKey(K key, Object affKey)Initializes key together with its affinity key counter-part.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> TaffinityKey()Gets affinity key to use for affinity mapping.voidaffinityKey(Object affKey)Sets affinity key to use for affinity mapping.booleanequals(Object obj)Equality check which delegates to the underlying key equality.inthashCode()Hash code implementation which delegates to the underlyingkey().Kkey()Gets wrapped key.voidkey(K key)Sets wrapped key.voidreadExternal(ObjectInput in)StringtoString()voidwriteExternal(ObjectOutput out)
-
-
-
Constructor Detail
-
AffinityKey
public AffinityKey()
Empty constructor.
-
AffinityKey
public AffinityKey(K key)
Initializes key wrapper for a given key. If affinity key is not initialized, then this key will be used for affinity.- Parameters:
key- Key.
-
-
Method Detail
-
key
public K key()
Gets wrapped key.- Returns:
- Wrapped key.
-
key
public void key(K key)
Sets wrapped key.- Parameters:
key- Wrapped key.
-
affinityKey
public <T> T affinityKey()
Gets affinity key to use for affinity mapping. If affinity key is not provided, thenkeyvalue will be returned.This method is annotated with
AffinityKeyMappedand will be picked up byGridCacheDefaultAffinityKeyMapperautomatically.- Type Parameters:
T- Type of affinity key.- Returns:
- Affinity key to use for affinity mapping.
-
affinityKey
public void affinityKey(Object affKey)
Sets affinity key to use for affinity mapping. If affinity key is not provided, thenkeyvalue will be returned.- Parameters:
affKey- Affinity key to use for affinity mapping.
-
writeExternal
public void writeExternal(ObjectOutput out) throws IOException
- Specified by:
writeExternalin interfaceExternalizable- Throws:
IOException
-
readExternal
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
- Specified by:
readExternalin interfaceExternalizable- Throws:
IOExceptionClassNotFoundException
-
hashCode
public int hashCode()
Hash code implementation which delegates to the underlyingkey(). Note, however, that different subclasses ofAffinityKeywill produce different hash codes.Users should override this method if different behavior is desired.
-
equals
public boolean equals(Object obj)
Equality check which delegates to the underlying key equality. Note, however, that different subclasses ofAffinityKeywill never be equal.Users should override this method if different behavior is desired.
-
-