org.apache.ignite.internal.client.util

## Class GridClientConsistentHash<N>

- java.lang.Object
- - org.apache.ignite.internal.client.util.GridClientConsistentHash<N>

    public class GridClientConsistentHash<N>
    extends Object

Controls key to node affinity using consistent hash algorithm. This class is thread-safe and does not have to be externally synchronized.

For a good explanation of what consistent hashing is, you can refer to Tom White's Blog.

### Constructor Summary

Constructors
Constructor and Description
`GridClientConsistentHash()` Constructs consistent hash using empty affinity seed and `MD5` hasher function.
`GridClientConsistentHash(Comparator<N> nodesComp, Object affSeed)` Constructs consistent hash using given affinity seed and hasher function.
`GridClientConsistentHash(Object affSeed)` Constructs consistent hash using given affinity seed and `MD5` hasher function.

### Method Summary

Methods
`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).
`int` `count()` Gets number of distinct nodes, excluding replicas, in consistent hash.
`static 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, GridClientPredicate<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).
`N` `random()` Picks a random node from consistent hash.
`boolean` `removeNode(N node)` Removes a node and all of its replicas.
`int` `size()` Gets size of all nodes (including replicas) in consistent hash.
`String` `toString()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Constructor Detail

#### GridClientConsistentHash

    public GridClientConsistentHash()

Constructs consistent hash using empty affinity seed and `MD5` hasher function.

#### GridClientConsistentHash

    public GridClientConsistentHash(Object affSeed)

Constructs consistent hash using given affinity seed and `MD5` hasher function.

Parameters:
  `affSeed` - Affinity seed (will be used as key prefix for hashing).

#### GridClientConsistentHash

    public GridClientConsistentHash(Comparator<N> nodesComp,
                            Object affSeed)

Constructs consistent hash using given affinity seed and hasher function.

Parameters:
  `nodesComp` - Nodes comparator to resolve hash codes collisions. If `null` natural order will be used.
  `affSeed` - Affinity seed (will be used as key prefix for hashing).

### Method Detail

#### addNodes

    public void addNodes(Collection<N> nodes,
                int replicas)

Adds nodes to consistent hash algorithm (if nodes are `null` or empty, then no-op).

Parameters:
  `nodes` - Nodes to add.
  `replicas` - Number of replicas for every node.

#### addNode

    public boolean addNode(N node,
                  int replicas)

Adds a node to consistent hash algorithm.

Parameters:
  `node` - New node (if `null` then no-op).
  `replicas` - Number of replicas for the node.
Returns:
  `True` if node was added, `false` if it is `null` or is already contained in the hash.

#### removeNode

    public boolean removeNode(N node)

Removes a node and all of its replicas.

Parameters:
  `node` - Node to remove (if `null`, then no-op).
Returns:
  `True` if node was removed, `false` if node is `null` or not present in hash.

#### count

    public int count()

Gets number of distinct nodes, excluding replicas, in consistent hash.

Returns:
  Number of distinct nodes, excluding replicas, in consistent hash.

#### size

    public int size()

Gets size of all nodes (including replicas) in consistent hash.

Returns:
  Size of all nodes (including replicas) in consistent hash.

#### isEmpty

    public boolean isEmpty()

Checks if consistent hash has nodes added to it.

Returns:
  `True` if consistent hash is empty, `false` otherwise.

#### nodes

    public Set<N> nodes()

Gets set of all distinct nodes in the consistent hash (in no particular order).

Returns:
  Set of all distinct nodes in the consistent hash.

#### random

    public N random()

Picks a random node from consistent hash.

Returns:
  Random node from consistent hash or `null` if there are no nodes.

#### node

    public N node(Object key)

Gets node for a key.

Parameters:
  `key` - Key.
Returns:
  Node.

#### node

    public N node(Object key,
         Collection<N> inc)

Gets node for a given key.

Parameters:
  `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.
Returns:
  Node for key, or `null` if node was not found.

#### node

    public N node(Object key,
         @Nullable
         Collection<N> inc,
         @Nullable
         Collection<N> exc)

Gets node for a given key.

Parameters:
  `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.
Returns:
  Node for key, or `null` if node was not found.

#### node

    public N node(Object key,
         GridClientPredicate<N>... p)

Gets node for a given key.

Parameters:
  `key` - Key to get node for.
  `p` - Optional predicate for node filtering.
Returns:
  Node for key, or `null` if node was not found.

#### hash

    public static int hash(Object o)

Gets hash code for a given object.

Parameters:
  `o` - Object to get hash code for.
Returns:
  Hash code.

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`

2015 Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Fabric | : ver. 1.5.0.final
Release Date | : December 29 2015