public class GridConsistentHash<N> extends Object
For a good explanation of what consistent hashing is, you can refer to Tom White's Blog.
Constructor and Description |
---|
GridConsistentHash()
Constructs consistent hash using default parameters.
|
GridConsistentHash(Comparator<N> nodesComp,
Object affSeed)
Constructs consistent hash using given affinity seed and hasher function.
|
GridConsistentHash(Object affSeed)
Constructs consistent hash using given affinity seed and
MD5 hasher function. |
Modifier and Type | Method and Description |
---|---|
boolean |
addNode(N node,
int replicas)
Adds a node to consistent hash algorithm.
|
void |
addNodes(Collection<N> nodes,
int replicas)
Adds nodes to consistent hash algorithm (if nodes are
null or empty, then no-op). |
boolean |
belongs(Object key,
Collection<N> nodes)
Checks if given key belongs to any of the given nodes.
|
boolean |
belongs(Object key,
int cnt,
Collection<N> nodes)
Checks if node the key is mapped to or the given count of its adjacent nodes are
contained in given set of nodes.
|
boolean |
belongs(Object key,
int cnt,
N node)
Checks if node the key is mapped to or the given count of its adjacent nodes contain
the node passed in.
|
boolean |
belongs(Object key,
N node)
Checks if key belongs to the given node.
|
void |
clear()
Clears all nodes from consistent hash.
|
int |
count()
Gets number of distinct nodes, excluding replicas, in consistent hash.
|
protected int |
hash(Object o)
Gets hash code for a given object.
|
boolean |
isEmpty()
Checks if consistent hash has nodes added to it.
|
N |
node(Object key)
Gets node for a key.
|
N |
node(Object key,
Collection<N> inc)
Gets node for a given key.
|
N |
node(Object key,
Collection<N> inc,
Collection<N> exc)
Gets node for a given key.
|
N |
node(Object key,
IgnitePredicate<N>... p)
Gets node for a given key.
|
Set<N> |
nodes()
Gets set of all distinct nodes in the consistent hash (in no particular order).
|
List<N> |
nodes(Object key,
int cnt)
Gets specified count of adjacent nodes for a given key.
|
List<N> |
nodes(Object key,
int cnt,
Collection<N> inc)
Gets specified count of adjacent nodes for a given key.
|
List<N> |
nodes(Object key,
int cnt,
Collection<N> inc,
Collection<N> exc)
Gets specified count of adjacent nodes for a given key.
|
List<N> |
nodes(Object key,
int cnt,
IgnitePredicate<N>... p)
Gets specified count of adjacent nodes for a given key.
|
N |
random()
Picks a random node from consistent hash.
|
boolean |
removeNode(N node)
Removes a node and all of its replicas.
|
void |
removeNodes(Collection<N> nodes)
Removes given nodes and all their replicas from consistent hash algorithm
(if nodes are
null or empty, then no-op). |
int |
size()
Gets size of all nodes (including replicas) in consistent hash.
|
String |
toString() |
public GridConsistentHash(@Nullable Object affSeed)
MD5
hasher function.affSeed
- Affinity seed (will be used as key prefix for hashing).public GridConsistentHash()
public GridConsistentHash(@Nullable Comparator<N> nodesComp, @Nullable Object affSeed)
nodesComp
- Nodes comparator to resolve hash codes collisions.affSeed
- Affinity seed (will be used as key prefix for hashing).public void addNodes(@Nullable Collection<N> nodes, int replicas)
null
or empty, then no-op).nodes
- Nodes to add.replicas
- Number of replicas for every node.public boolean addNode(@Nullable N node, int replicas)
node
- New node (if null
then no-op).replicas
- Number of replicas for the node.True
if node was added, false
if it is null
or
is already contained in the hash.public void removeNodes(@Nullable Collection<N> nodes)
null
or empty, then no-op).nodes
- Nodes to remove.public boolean removeNode(@Nullable N node)
node
- Node to remove (if null
, then no-op).True
if node was removed, false
if node is null
or
not present in hash.public void clear()
public int count()
public int size()
public boolean isEmpty()
True
if consistent hash is empty, false
otherwise.public Set<N> nodes()
@Nullable public N random()
null
if there are no nodes.@Nullable public N node(@Nullable Object key)
key
- Key.@Nullable public N node(@Nullable Object key, @Nullable Collection<N> inc)
key
- Key to get node for.inc
- Optional inclusion set. Only nodes contained in this set may be returned.
If null
, then all nodes may be included.null
if node was not found.@Nullable public N node(@Nullable Object key, @Nullable Collection<N> inc, @Nullable Collection<N> exc)
key
- Key to get node for.inc
- Optional inclusion set. Only nodes contained in this set may be returned.
If null
, then all nodes may be included.exc
- Optional exclusion set. Only nodes not contained in this set may be returned.
If null
, then all nodes may be returned.null
if node was not found.@Nullable public N node(@Nullable Object key, @Nullable IgnitePredicate<N>... p)
key
- Key to get node for.p
- Optional predicate for node filtering.null
if node was not found.public List<N> nodes(@Nullable Object key, int cnt)
key
- Key to get adjacent nodes for.cnt
- Number of adjacent nodes to get.public List<N> nodes(@Nullable Object key, int cnt, @Nullable Collection<N> inc)
key
- Key to get adjacent nodes for.cnt
- Number of adjacent nodes to get.inc
- Optional inclusion set. Only nodes contained in this set may be returned.
If null
, then all nodes may be returned.public List<N> nodes(@Nullable Object key, int cnt, @Nullable Collection<N> inc, @Nullable Collection<N> exc)
key
- Key to get adjacent nodes for.cnt
- Number of adjacent nodes to get.inc
- Optional inclusion set. Only nodes contained in this set may be returned.
If null
, then all nodes may be included.exc
- Optional exclusion set. Only nodes not contained in this set may be returned.
If null
, then all nodes may be returned.public List<N> nodes(@Nullable Object key, int cnt, @Nullable IgnitePredicate<N>... p)
key
- Key to get adjacent nodes for.cnt
- Number of adjacent nodes to get.p
- Optional predicate to filter out nodes. Nodes that don't pass the filter
will be skipped.public boolean belongs(@Nullable Object key, N node)
key
- Key to check.node
- Node to check.True
if key belongs to given node.public boolean belongs(@Nullable Object key, @Nullable Collection<N> nodes)
key
- Key to check.nodes
- Nodes to check (if null
then false
is returned).True
if key belongs to any of the given nodes.public boolean belongs(@Nullable Object key, int cnt, N node)
key
- Key to check.cnt
- Number of adjacent nodes to check.node
- Node to check.True
if node the key is mapped to or given count of its adjacent
neighbors contain the node.public boolean belongs(@Nullable Object key, int cnt, @Nullable Collection<N> nodes)
key
- Key to check.cnt
- Number of adjacent nodes to check.nodes
- Nodes to check.True
if node the key is mapped to or given count of its adjacent
neighbors are contained in given set of nodes.protected int hash(Object o)
o
- Object to get hash code for.
Follow @ApacheIgnite
Ignite Fabric : ver. 2.0.0 Release Date : April 30 2017