Class 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 by affinityKey(Object) method will be used for key-to-node affinity.

    Note that the equals(Object) and hashCode() methods delegate directly to the wrapped cache key provided by key() 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 with Company 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.

    See Also:
    AffinityKeyMapped, AffinityKeyMapper, AffinityFunction, Serialized Form
    • 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.
      • AffinityKey

        public AffinityKey​(K key,
                           Object affKey)
        Initializes key together with its affinity key counter-part.
        Parameters:
        key - Key.
        affKey - Affinity 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, then key value will be returned.

        This method is annotated with AffinityKeyMapped and will be picked up by GridCacheDefaultAffinityKeyMapper 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, then key value will be returned.
        Parameters:
        affKey - Affinity key to use for affinity mapping.
      • hashCode

        public int hashCode()
        Hash code implementation which delegates to the underlying key(). Note, however, that different subclasses of AffinityKey will produce different hash codes.

        Users should override this method if different behavior is desired.

        Overrides:
        hashCode in class Object
        Returns:
        Hash code.
      • equals

        public boolean equals​(Object obj)
        Equality check which delegates to the underlying key equality. Note, however, that different subclasses of AffinityKey will never be equal.

        Users should override this method if different behavior is desired.

        Overrides:
        equals in class Object
        Parameters:
        obj - Object to check for equality.
        Returns:
        True if objects are equal.