pub struct Client<'a, T, U, H = RandomState> { /* private fields */ }Expand description
A client that uses Ring<T>.
§Examples
use hash_rings::consistent::Client;
let mut client = Client::new();
client.insert_node(&"node-1", 3);
client.insert_point(&"point-1");
client.insert_point(&"point-2");
assert_eq!(client.len(), 1);
assert_eq!(client.get_node(&"point-1"), &"node-1");
client.remove_point(&"point-2");
assert_eq!(client.get_points(&"node-1"), [&"point-1"]);Implementations§
Source§impl<'a, T, U> Client<'a, T, U, RandomState>
impl<'a, T, U> Client<'a, T, U, RandomState>
Source§impl<'a, T, U, H> Client<'a, T, U, H>
impl<'a, T, U, H> Client<'a, T, U, H>
Sourcepub fn with_hasher(hash_builder: H) -> Self
pub fn with_hasher(hash_builder: H) -> Self
Constructs a new, empty Client<T, U> with a specified hash builder.
§Examples
use hash_rings::consistent::Client;
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;
type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;
let mut client: Client<&str, &str, _> = Client::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::Client;
let mut client: Client<&str, &str> = Client::new();
// "node-2" will receive three times more points than "node-1"
client.insert_node(&"node-1", 1);
client.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.
§Panics
Panics if the ring is empty after removal of a node or if the node does not exist.
§Examples
use hash_rings::consistent::Client;
let mut client: Client<&str, &str> = Client::new();
client.insert_node(&"node-1", 1);
client.insert_node(&"node-2", 1);
client.remove_node(&"node-2");Sourcepub fn get_points(&self, id: &T) -> Vec<&U>
pub fn get_points(&self, id: &T) -> Vec<&U>
Returns the points associated with a node and its replicas.
§Panics
Panics if the node does not exist.
§Examples
use hash_rings::consistent::Client;
let mut client: Client<&str, &str> = Client::new();
client.insert_node(&"node-1", 1);
client.insert_point(&"point-1");
assert_eq!(client.get_points(&"node-1"), [&"point-1"]);Sourcepub fn insert_point(&mut self, point: &'a U) -> &T
pub fn insert_point(&mut self, point: &'a U) -> &T
Sourcepub fn remove_point(&mut self, point: &U)
pub fn remove_point(&mut self, point: &U)
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of nodes in the ring.
§Examples
use hash_rings::consistent::Client;
let mut client: Client<&str, &str> = Client::new();
client.insert_node(&"node-1", 3);
assert_eq!(client.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::Client;
let mut client: Client<&str, &str> = Client::new();
assert!(client.is_empty());
client.insert_node(&"node-1", 3);
assert!(!client.is_empty());Sourcepub fn iter(&'a self) -> impl Iterator<Item = (&'a T, Vec<&'a U>)>
pub fn iter(&'a self) -> impl Iterator<Item = (&'a T, Vec<&'a U>)>
Returns an iterator over the ring. The iterator will yield nodes and points in no particular order.
§Examples
use hash_rings::consistent::Client;
let mut client = Client::new();
client.insert_node(&"node-1", 1);
client.insert_point(&"point-1");
let mut iterator = client.iter();
assert_eq!(iterator.next(), Some((&"node-1", vec![&"point-1"])));
assert_eq!(iterator.next(), None);