pub struct Ring<'a, T, H = RandomState> { /* private fields */ }Expand description
A hashing ring implemented using multi-probe consistent hashing.
Multi-probe consistent hashing is a variation on consistent hashing where instead of the nodes
being hashed multiple times to reduce variance, the keys are hashed multiple times. Each key is
hashed hash_count times and the closest node over all hashes is returned.
§Examples
use hash_rings::mpc::Ring;
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;
type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;
let mut ring = Ring::with_hasher(DefaultBuildHasher::default(), 2);
ring.insert_node(&"node-1");
ring.insert_node(&"node-2");
ring.remove_node(&"node-1");
assert_eq!(ring.get_node(&"point-1"), &"node-2");
assert_eq!(ring.len(), 1);
let mut iterator = ring.iter();
assert_eq!(iterator.next(), Some(&"node-2"));
assert_eq!(iterator.next(), None);Implementations§
Source§impl<'a, T> Ring<'a, T, RandomState>
impl<'a, T> Ring<'a, T, RandomState>
Source§impl<'a, T, H> Ring<'a, T, H>
impl<'a, T, H> Ring<'a, T, H>
Sourcepub fn with_hasher(hash_builder: H, hash_count: u64) -> Self
pub fn with_hasher(hash_builder: H, hash_count: u64) -> Self
Constructs a new, empty Ring<T> that hashes hash_count times when a key is inserted
with a specified hash builder.
§Examples
use hash_rings::mpc::Ring;
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;
type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;
let mut ring: Ring<&str, _> = Ring::with_hasher(DefaultBuildHasher::default(), 2);Sourcepub fn insert_node(&mut self, id: &'a T)where
T: Hash,
H: BuildHasher,
pub fn insert_node(&mut self, id: &'a T)where
T: Hash,
H: BuildHasher,
Inserts a node into the ring with a number of replicas.
Increasing the number of replicas will increase the number of expected points mapped to the node. For example, a node with three replicas will receive approximately three times more points than a node with one replica.
§Examples
use hash_rings::mpc::Ring;
let mut ring: Ring<&str> = Ring::new(2);
ring.insert_node(&"node-1");Sourcepub fn remove_node(&mut self, id: &T)where
T: Hash,
H: BuildHasher,
pub fn remove_node(&mut self, id: &T)where
T: Hash,
H: BuildHasher,
Removes a node.
§Examples
use hash_rings::mpc::Ring;
let mut ring: Ring<&str> = Ring::new(2);
ring.insert_node(&"node-1");
ring.remove_node(&"node-1");Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of nodes in the ring.
§Examples
use hash_rings::mpc::Ring;
let mut ring: Ring<&str> = Ring::new(2);
ring.insert_node(&"node-1");
assert_eq!(ring.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the ring is empty.
§Examples
use hash_rings::mpc::Ring;
let mut ring: Ring<&str> = Ring::new(2);
assert!(ring.is_empty());
ring.insert_node(&"node-1");
assert!(!ring.is_empty());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 in no particular order.
§Examples
use hash_rings::mpc::Ring;
let mut ring = Ring::new(2);
ring.insert_node(&"node-1");
let mut iterator = ring.iter();
assert_eq!(iterator.next(), Some(&"node-1"));
assert_eq!(iterator.next(), None);