Class PartitionAwareKey<K,P>

java.lang.Object
com.hazelcast.partition.PartitionAwareKey<K,P>
Type Parameters:
K - the key type
P - the partitionKey type
All Implemented Interfaces:
DataSerializable, PartitionAware<Object>

public final class PartitionAwareKey<K,P> extends Object implements PartitionAware<Object>, DataSerializable
A PartitionAware key. This is useful in combination with a Map where you want to control the partition of a key.
  • Constructor Details

    • PartitionAwareKey

      public PartitionAwareKey(K key, P partitionKey)
      Creates a new PartitionAwareKey.
      Parameters:
      key - the key
      partitionKey - the partitionKey
      Throws:
      IllegalArgumentException - if key or partitionKey is null.
  • Method Details

    • getKey

      public K getKey()
      Gets the key (not the partitionKey).
      Returns:
      the key (not the partitionKey)
    • getPartitionKey

      public P getPartitionKey()
      Description copied from interface: PartitionAware
      The key that will be used by Hazelcast to specify the partition. You should give the same key for objects that you want to be in the same partition.

      The contract of PartitionAware.getPartitionKey() method is as follows:

      Let us define toData(o) as serialized form of given object obtained by using Hazelcast Serialization configured for the cluster (the exact method used is SerializationService.toData from an internal SPI).

      Assume PartitionAware a, b are objects (e.g. IMap keys) and T pk1 = a.getPartitionKey(), pk2 = b.getPartitionKey() are partition key values.

      Then PartitionAware.getPartitionKey() implementation must obey the following contract:

      1. (mandatory) Deterministic partitioning: if a.equals(b) then either toData(a.getPartitionKey()).equals(toData(b.getPartitionKey())) or a.getPartitionKey() == null && b.getPartitionKey() == null
      2. (recommended) Reasonable partitioning: if a.equals(b) then a.getPartitionKey().equals(b.getPartitionKey())
      3. (recommended) Reasonable partitioning key serialization (if custom serialization is used): if pk1.equals(pk2) then toData(pk1).equals(toData(pk2))
      4. The above stated conditions must hold when the PartitionAware.getPartitionKey() is invoked any number of times on any member or client, regardless of time (note that some partitioned data structures support persistence) and if the JVM is restarted, regardless of client/member version and JVM version (across all JVMs ever used in given deployment), timezone, locale and similar differences in the environment.
      Adhering to the contract guarantees stable partitioning of the data and ability to find appropriate member and partition who owns given object during querying and modifications.

      Notes:

      Specified by:
      getPartitionKey in interface PartitionAware<K>
      Returns:
      the key that specifies the partition. Returning null or this will cause the result as if the object did not implement PartitionAware. If the returned key itself implements PartitionAware, this fact will be ignored and the key will be treated as a plain object.
    • writeData

      public void writeData(ObjectDataOutput out) throws IOException
      Description copied from interface: DataSerializable
      Writes object fields to output stream
      Specified by:
      writeData in interface DataSerializable
      Parameters:
      out - output
      Throws:
      IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
    • readData

      public void readData(ObjectDataInput in) throws IOException
      Description copied from interface: DataSerializable
      Reads fields from the input stream
      Specified by:
      readData in interface DataSerializable
      Parameters:
      in - input
      Throws:
      IOException - if an I/O error occurs. In particular, an IOException may be thrown if the input stream has been closed.
    • equals

      public boolean equals(Object thatObject)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object