pub struct Ring<'a, T, H = RandomState> { /* private fields */ }Expand description
A hashing ring implemented using consistent hashing.
Consistent hashing is based on mapping each node to a pseudorandom value. In this implementation the pseudorandom is a combination of the hash of the node and the hash of the replica number. A point is also represented as a pseudorandom value and it is mapped to the node with the smallest value that is greater than or equal to the point’s value. If such a node does not exist, then the point maps to the node with the smallest value.
§Examples
use hash_rings::consistent::Ring;
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;
type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;
let mut ring = Ring::with_hasher(DefaultBuildHasher::default());
ring.insert_node(&"node-1", 1);
ring.insert_node(&"node-2", 3);
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", 3)));
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) -> Self
pub fn with_hasher(hash_builder: H) -> Self
Constructs a new, empty Ring<T> with a specified hash builder.
§Examples
use hash_rings::consistent::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());Sourcepub fn insert_node(&mut self, id: &'a T, replicas: usize)
pub fn insert_node(&mut self, id: &'a T, replicas: usize)
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::consistent::Ring;
let mut ring: Ring<&str> = Ring::new();
// "node-2" will receive three times more points than "node-1"
ring.insert_node(&"node-1", 1);
ring.insert_node(&"node-2", 3);Sourcepub fn remove_node(&mut self, id: &T)
pub fn remove_node(&mut self, id: &T)
Removes a node and all its replicas from the ring.
§Examples
use hash_rings::consistent::Ring;
let mut ring: Ring<&str> = Ring::new();
ring.insert_node(&"node-1", 1);
ring.insert_node(&"node-2", 1);
ring.remove_node(&"node-2");Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of nodes in the ring.
§Examples
use hash_rings::consistent::Ring;
let mut ring: Ring<&str> = Ring::new();
ring.insert_node(&"node-1", 3);
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::consistent::Ring;
let mut ring: Ring<&str> = Ring::new();
assert!(ring.is_empty());
ring.insert_node(&"node-1", 3);
assert!(!ring.is_empty());Sourcepub fn iter(&'a self) -> impl Iterator<Item = (&'a T, usize)>
pub fn iter(&'a self) -> impl Iterator<Item = (&'a T, usize)>
Returns an iterator over the ring. The iterator will yield nodes and the replica count in no particular order.
§Examples
use hash_rings::consistent::Ring;
let mut ring = Ring::new();
ring.insert_node(&"node-1", 1);
let mut iterator = ring.iter();
assert_eq!(iterator.next(), Some((&"node-1", 1)));
assert_eq!(iterator.next(), None);