Constructor
new CHash(rfactor, pfactor, treeopt, optsopt)
Consistent hash implementation. Maintains a red-black tree containing the hash ring (for ordering), with keys as hashes of node names and values as the nodes themselves. This implementation also contains an rfactor
(replication factor) and pfactor
(persistence factor). rfactor
dictates how many times an element is inserted into the ring, while pfactor
dictates how many nodes will be used for redundancy.
Parameters:
Name | Type | Attributes | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
rfactor |
Number | Replication factor for node insertion. |
|||||||||
pfactor |
Number | Persistence factor for redundancy in node-neighbor calculations. |
|||||||||
tree |
RBTree |
<optional> |
Existing red-black tree to instantiate local tree from. |
||||||||
opts |
Object |
<optional> |
Options object for hash ring. Properties
|
- Source:
Methods
begin() → {Iterator}
Generates an iterator on this hash ring, starting at the first (smallest hash) node.
- Source:
Returns:
Iterator over this instance.
- Type
- Iterator
end() → {Iterator}
Generates an iterator on this hash ring, starting at the last (largest hash) node.
- Source:
Returns:
Iterator over this instance.
- Type
- Iterator
equals(chash) → {Boolean}
Returns whether two consistent hash rings are equal. This involves checking the following:
- The hash rings have the same size
- For every entry in this instance's RBT, the corresponding entry exists in the RBT of
chash
Parameters:
Name | Type | Description |
---|---|---|
chash |
Clusterluck.CHash | CHash to derive equality against. |
- Source:
Returns:
Whether this chash instance equals chash
.
- Type
- Boolean
find(data) → {Node}
Finds the next node in the hash ring based on the bucket placement of data
. If no succeeding node exists, loops around to the beginning of the hash ring and picks the first node.
Parameters:
Name | Type | Description |
---|---|---|
data |
String | Data to find the next node off of. |
- Source:
Returns:
Next node in the hash ring.
- Type
- Node
fromJSON(ent) → {Clusterluck.CHash}
Instantiates state from a JSON object.
Parameters:
Name | Type | Description |
---|---|---|
ent |
Object | JSON object to instantiate state from. |
- Source:
Returns:
This instance.
- Type
- Clusterluck.CHash
get(node, def) → {Node}
Gets node
from the hash ring by class. Useful if the id is known but host and port need to be queried.
Parameters:
Name | Type | Description |
---|---|---|
node |
Node | Node to access. |
def |
Node | Default value to return if |
- Source:
Returns:
Node with matching id.
- Type
- Node
insert(node, weightopt) → {Clusterluck.CHash}
Inserts node
into this ring, repeating this process rfactor
number of times. Ignores insertions if node
is already present in the hash ring.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
node |
Node | Node to insert into this instance. |
|
weight |
Number |
<optional> |
Number of times to insert this node. Defaults to |
- Source:
Returns:
This instance.
- Type
- Clusterluck.CHash
intersect(chash) → {Clusterluck.CHash}
Computes a set-intersection on two hash rings, removing any nodes not found in both rings.
Parameters:
Name | Type | Description |
---|---|---|
chash |
Clusterluck.CHash | Hash ring to intersect with. |
- Source:
Returns:
This instance.
- Type
- Clusterluck.CHash
isDefined() → {Boolean}
Checks node
is defined in this hash ring.
- Source:
Returns:
Whether node
is defined.
- Type
- Boolean
iterator(node) → {Iterator}
Generates an iterator on this hash ring, starting at node
.
Parameters:
Name | Type | Description |
---|---|---|
node |
Node | Node to start iteration on. |
- Source:
Returns:
Iterator over this instance.
- Type
- Iterator
merge(chash) → {Clusterluck.CHash}
Computes a set-union on two hash rings, inserting any nodes present in chash
that aren't in this hash ring.
Parameters:
Name | Type | Description |
---|---|---|
chash |
Clusterluck.CHash | Hash ring to merge with. |
- Source:
Returns:
This instance.
- Type
- Clusterluck.CHash
next(node) → {Array}
Returns a list of neighbors of node
in the hash ring. This is bounded by pfactor
, which dictates the maximum number of neighbors a node can have (barring size limitations).
Parameters:
Name | Type | Description |
---|---|---|
node |
Node | Node to find neighbors of. |
- Source:
Returns:
Neighbors of node
in the hash ring.
- Type
- Array
nodes() → {Array}
Acts as a getter for the nodes in this hash ring.
- Source:
Returns:
List of nodes in this hash ring.
- Type
- Array
numberNodes() → {Number}
Returns the number of nodes in the ring.
- Source:
Returns:
Number of nodes in this hash ring.
- Type
- Number
pfactor(popt) → {Number}
Acts as a getter/setter for the pfactor of this hash ring.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
p |
Number |
<optional> |
pfactor to set on this ring. |
- Source:
Returns:
pfactor of this instance.
- Type
- Number
prev(node) → {Array}
Returns a list of preceding neighbors of node
in the hash ring. This is bounded by pfactor
, which dictates the maximum number of neighbors a node can have (barring size limitations).
Parameters:
Name | Type | Description |
---|---|---|
node |
Node | Node to find neighbors of. |
- Source:
Returns:
Neighbors of node
in the hash ring.
- Type
- Array
rangeNext(data, rangeopt) → {Array}
Returns a bucket of nodes including and following the hash slot corresponding to data
. This is bounded by range
, which dictates the size of the bucket returned).
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
data |
String | Data to calculate hash of and find following nodes. |
|
range |
Number |
<optional> |
Number of nodes to return. Defaults to |
- Source:
Returns:
Bucket of nodes with size range
associated with key data
.
- Type
- Array
rangePrev(data, rangeopt) → {Array}
Returns a bucket of nodes preceding the hash slot corresponding to data
. This is bounded by range
, which dictates the size of the bucket returned).
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
data |
String | Data to calculate hash of and find following nodes. |
|
range |
Number |
<optional> |
Number of nodes to return. Defaults to |
- Source:
Returns:
Bucket of nodes with size range
preceding key data
.
- Type
- Array
remove(node) → {Clusterluck.CHash}
Removes node
from this ring, repeating this process rfactor
number of times. Ignores removals node
doesn't exist in the ring.
Parameters:
Name | Type | Description |
---|---|---|
node |
Node | Node to remove from this instance. |
- Source:
Returns:
This instance.
- Type
- Clusterluck.CHash
rfactor(ropt) → {Number}
Acts as a getter/setter for the rfactor of this hash ring.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
r |
Number |
<optional> |
rfactor to set on this ring. |
- Source:
Returns:
rfactor of this instance.
- Type
- Number
size() → {Number}
Acts as a getter for the number of elements in this hash ring.
- Source:
Returns:
Number of elements in this hash ring.
- Type
- Number
toJSON(fastopt) → {Object}
Computes the JSON serialization of this instance. Useful when periodically flushing this ring to disk.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fast |
Boolean |
<optional> |
Whether to compute the JSON serialization of this instance using a copy of internal state. |
- Source:
Returns:
JSON serialization of this instance.
- Type
- Object
tree(treeopt) → {RBTree}
Acts as a getter/setter for the red-black tree of this hash ring.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
tree |
RBTree |
<optional> |
Tree to set on this instance. |
- Source:
Returns:
Red-black tree of this instance.
- Type
- RBTree
update(node, weight) → {Clusterluck.CHash}
Update the weight of node
in the hash ring.
Parameters:
Name | Type | Description |
---|---|---|
node |
Node | Node to add state to. |
weight |
Number | Weight to change |
- Source:
Returns:
This instance.
- Type
- Clusterluck.CHash
weights() → {Map}
Returns the weight map from node IDs to weights in the hash ring.
- Source:
Returns:
Weight map from node IDs to weights
- Type
- Map