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
Person
objects can be collocated withCompany
objects 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
AffinityKeyMapper
and@AffinityKeyMapped
documentation.
-
-
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> T
affinityKey()
Gets affinity key to use for affinity mapping.void
affinityKey(Object affKey)
Sets affinity key to use for affinity mapping.boolean
equals(Object obj)
Equality check which delegates to the underlying key equality.int
hashCode()
Hash code implementation which delegates to the underlyingkey()
.K
key()
Gets wrapped key.void
key(K key)
Sets wrapped key.void
readExternal(ObjectInput in)
String
toString()
void
writeExternal(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, thenkey
value will be returned.This method is annotated with
AffinityKeyMapped
and will be picked up byGridCacheDefaultAffinityKeyMapper
automatically.- 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, thenkey
value will be returned.- Parameters:
affKey
- Affinity key to use for affinity mapping.
-
writeExternal
public void writeExternal(ObjectOutput out) throws IOException
- Specified by:
writeExternal
in interfaceExternalizable
- Throws:
IOException
-
readExternal
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
- Specified by:
readExternal
in interfaceExternalizable
- Throws:
IOException
ClassNotFoundException
-
hashCode
public int hashCode()
Hash code implementation which delegates to the underlyingkey()
. Note, however, that different subclasses ofAffinityKey
will 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 ofAffinityKey
will never be equal.Users should override this method if different behavior is desired.
-
-