pub struct Ring<'a, T, H = RandomState> { /* private fields */ }Expand description
A hashing ring implemented using the Cache Array Routing Protocol.
The Cache Array Routing Protocol calculates the relative weight for each node in the ring to distribute points according to their weights.
§Examples
use hash_rings::carp::{Node, Ring};
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;
type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;
let mut ring = Ring::with_hasher(
DefaultBuildHasher::default(),
vec![Node::new(&"node-1", 1f64), Node::new(&"node-2", 3f64)],
);
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", 3f64)));
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, nodes: Vec<Node<'a, T>>) -> Self
pub fn with_hasher(hash_builder: H, nodes: Vec<Node<'a, T>>) -> Self
Constructs a new, empty Ring<T> with a specified hash builder.
§Examples
use hash_rings::carp::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(), vec![]);Sourcepub fn insert_node(&mut self, new_node: Node<'a, T>)
pub fn insert_node(&mut self, new_node: Node<'a, T>)
Inserts a node into the ring with a particular weight.
Increasing the weight will increase the number of expected points mapped to the node. For example, a node with a weight of three will receive approximately three times more points than a node with a weight of one.
§Examples
use hash_rings::carp::{Node, Ring};
let mut ring = Ring::new(vec![Node::new(&"node-1", 1f64)]);
ring.insert_node(Node::new(&"node-2", 1f64));Sourcepub fn remove_node(&mut self, id: &T)where
T: Eq,
pub fn remove_node(&mut self, id: &T)where
T: Eq,
Removes a node from the ring.
§Examples
use hash_rings::carp::{Node, Ring};
let mut ring = Ring::new(vec![Node::new(&"node-1", 1f64), Node::new(&"node-2", 3f64)]);
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::carp::{Node, Ring};
let mut ring = Ring::new(vec![Node::new(&"node-1", 1f64), Node::new(&"node-2", 3f64)]);
assert_eq!(ring.len(), 2);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the ring is empty.
§Examples
use hash_rings::carp::{Node, Ring};
let ring: Ring<'_, u32, _> = Ring::new(vec![]);
assert!(ring.is_empty());Sourcepub fn iter(&'a self) -> impl Iterator<Item = (&'a T, f64)>
pub fn iter(&'a self) -> impl Iterator<Item = (&'a T, f64)>
Returns an iterator over the ring. The iterator will yield nodes and their weights in sorted by weight, and then by id. particular order.
§Examples
use hash_rings::carp::{Node, Ring};
let mut ring = Ring::new(vec![Node::new(&"node-1", 1f64), Node::new(&"node-2", 3f64)]);
let mut iterator = ring.iter();
assert_eq!(iterator.next(), Some((&"node-1", 1f64)));
assert_eq!(iterator.next(), Some((&"node-2", 3f64)));
assert_eq!(iterator.next(), None);