Struct hash_rings::carp::Ring [−][src]
pub struct Ring<'a, T> where
T: 'a, { /* fields omitted */ }
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}; let mut ring = Ring::new(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);
Methods
impl<'a, T> Ring<'a, T>
[src]
impl<'a, T> Ring<'a, T>
pub fn new(nodes: Vec<Node<'a, T>>) -> Ring<'a, T> where
T: Ord,
[src]
pub fn new(nodes: Vec<Node<'a, T>>) -> Ring<'a, T> where
T: Ord,
Constructs a new, empty Ring<T>
.
Examples
use hash_rings::carp::Ring; let mut ring: Ring<&str> = Ring::new(vec![]);
pub fn insert_node(&mut self, new_node: Node<'a, T>) where
T: Ord,
[src]
pub fn insert_node(&mut self, new_node: Node<'a, T>) where
T: Ord,
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.remove_node(&"node-1");
pub fn remove_node(&mut self, id: &T) where
T: Eq,
[src]
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");
pub fn get_node<U>(&self, point: &U) -> &'a T where
T: Ord,
U: Hash,
[src]
pub fn get_node<U>(&self, point: &U) -> &'a T where
T: Ord,
U: Hash,
Returns the node associated with a point.
Panics
Panics if the ring is empty.
Examples
use hash_rings::carp::{Node, Ring}; let mut ring = Ring::new(vec![Node::new(&"node-1", 1f64)]); assert_eq!(ring.get_node(&"point-1"), &"node-1");
pub fn len(&self) -> usize
[src]
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);
pub fn is_empty(&self) -> bool
[src]
pub fn is_empty(&self) -> bool
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)]); assert_eq!(ring.len(), 2);
pub fn iter(
&'a self
) -> impl Iterator<Item = (&'a T, f64)>
[src]
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);
Trait Implementations
impl<'a, T> IntoIterator for &'a Ring<'a, T>
[src]
impl<'a, T> IntoIterator for &'a Ring<'a, T>