pub struct Ring<'a, T> { /* private fields */ }Expand description
A hashing ring implemented using maglev hashing.
Maglev hashing produces a lookup table that allows finding a node in constant time by generating random permutations.
§Examples
use hash_rings::maglev::Ring;
let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
assert_eq!(ring.get_node(&"point-1"), &"node-3");
assert_eq!(ring.nodes(), 3);
assert_eq!(ring.capacity(), 307);Implementations§
Source§impl<'a, T> Ring<'a, T>
impl<'a, T> Ring<'a, T>
Sourcepub fn new(nodes: Vec<&'a T>) -> Selfwhere
T: Hash,
pub fn new(nodes: Vec<&'a T>) -> Selfwhere
T: Hash,
Constructs a new Ring<T> with a specified list of nodes.
§Examples
use hash_rings::maglev::Ring;
let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);Sourcepub fn with_capacity_hint(nodes: Vec<&'a T>, capacity_hint: usize) -> Selfwhere
T: Hash,
pub fn with_capacity_hint(nodes: Vec<&'a T>, capacity_hint: usize) -> Selfwhere
T: Hash,
Constructs a new Ring<T> with a specified list of nodes and a capacity hint. The actual
capacity of the ring will always be the next prime greater than or equal to
capacity_hint. If nodes are removed and the ring is regenerated, the ring should be
rebuilt with the same capacity.
§Examples
use hash_rings::maglev::Ring;
let ring = Ring::with_capacity_hint(vec![&"node-1", &"node-2", &"node-3"], 100);
assert_eq!(ring.capacity(), 101);Sourcepub fn nodes(&self) -> usize
pub fn nodes(&self) -> usize
Returns the number of nodes in the ring.
§Examples
use hash_rings::maglev::Ring;
let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
assert_eq!(ring.nodes(), 3);Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the capacity of the ring. If nodes are removed and the ring is regenerated, the ring should be rebuilt with the same capacity.
§Examples
use hash_rings::maglev::Ring;
let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
assert_eq!(ring.capacity(), 307);Sourcepub fn get_node<U>(&self, key: &U) -> &Twhere
U: Hash,
pub fn get_node<U>(&self, key: &U) -> &Twhere
U: Hash,
Returns the node associated with a key.
§Examples
use hash_rings::maglev::Ring;
let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
assert_eq!(ring.get_node(&"point-1"), &"node-3");Sourcepub fn iter(&'a self) -> impl Iterator<Item = &'a T>
pub fn iter(&'a self) -> impl Iterator<Item = &'a T>
Returns an iterator over the ring. The iterator will yield the nodes in the ring.
§Examples
use hash_rings::maglev::Ring;
let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
let mut iterator = ring.iter();
assert_eq!(iterator.next(), Some(&"node-1"));
assert_eq!(iterator.next(), Some(&"node-2"));
assert_eq!(iterator.next(), Some(&"node-3"));
assert_eq!(iterator.next(), None);