Package com.hazelcast.partition
Class PartitionAwareKey<K,P>
java.lang.Object
com.hazelcast.partition.PartitionAwareKey<K,P>
- Type Parameters:
K
- the key typeP
- 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
Creates a new PartitionAwareKey.- Parameters:
key
- the keypartitionKey
- the partitionKey- Throws:
IllegalArgumentException
- if key or partitionKey is null.
-
-
Method Details
-
getKey
Gets the key (not the partitionKey).- Returns:
- the key (not the partitionKey)
-
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 isSerializationService.toData
from an internal SPI).Assume
PartitionAware a, b
are objects (eg. IMap keys) andT pk1 = a.getPartitionKey(), pk2 = b.getPartitionKey()
are partition key values.Then
PartitionAware.getPartitionKey()
implementation must obey the following contract:- (mandatory) Deterministic partitioning: if
a.equals(b)
then eithertoData(a.getPartitionKey()).equals(toData(b.getPartitionKey()))
ora.getPartitionKey() == null && b.getPartitionKey() == null
- (recommended) Reasonable partitioning: if
a.equals(b)
thena.getPartitionKey().equals(b.getPartitionKey())
- (recommended) Reasonable partitioning key serialization (if custom
serialization is used): if
pk1.equals(pk2)
thentoData(pk1).equals(toData(pk2))
- 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.
Notes:
PartitionAware.getPartitionKey()
contract is similar tohashCode()
but with stricter long-term requirements.- Partition key is not compared directly, only serialized form is compared or used to calculate partition id.
- For unequal objects
PartitionAware.getPartitionKey()
may return the same or different values according to specific partitioning use case needs.
- Specified by:
getPartitionKey
in interfacePartitionAware<K>
- Returns:
- the key that specifies the partition. Returning
null
orthis
will cause the result as if the object did not implementPartitionAware
. If the returned key itself implementsPartitionAware
, this fact will be ignored and the key will be treated as a plain object.
- (mandatory) Deterministic partitioning: if
-
writeData
Description copied from interface:DataSerializable
Writes object fields to output stream- Specified by:
writeData
in interfaceDataSerializable
- Parameters:
out
- output- Throws:
IOException
- if an I/O error occurs. In particular, anIOException
may be thrown if the output stream has been closed.
-
readData
Description copied from interface:DataSerializable
Reads fields from the input stream- Specified by:
readData
in interfaceDataSerializable
- Parameters:
in
- input- Throws:
IOException
- if an I/O error occurs. In particular, anIOException
may be thrown if the input stream has been closed.
-
equals
-
hashCode
public int hashCode() -
toString
-