Expand description
An implementation of Consistent hashing algorithm.
Currently this crate only provides StaticHashRing
which
represents statically built, virtual node based hash rings.
§Examples
use consistent_hash::{Node, StaticHashRing, DefaultHash};
let nodes = vec![
Node::new("foo").quantity(5),
Node::new("bar").quantity(5),
Node::new("baz").quantity(1),
Node::new("baz").quantity(2), // ignored (duplicate key)
];
let ring = StaticHashRing::new(DefaultHash, nodes.into_iter());
assert_eq!(ring.len(), 11); // virtual node count
assert_eq!(ring.nodes().len(), 3); // real node count
assert_eq!(ring.calc_candidates(&"aa").map(|n| &n.key).collect::<Vec<_>>(),
[&"bar", &"foo", &"baz"]);
assert_eq!(ring.calc_candidates(&"bb").map(|n| &n.key).collect::<Vec<_>>(),
[&"foo", &"bar", &"baz"]);
Structs§
- Candidates
- An iterator which represents a sequence of the candidate nodes for an item.
- Default
Hash - The default
RingHash
implementation. - Node
- A node in a hash ring.
- Static
Hash Ring - A hash ring which is built statically.
Traits§
- Ring
Hash - This trait allows calculating hash codes for virtual nodes and items.