pub struct HashRing<N, B> { /* private fields */ }Expand description
A hash ring for consistent hashing.
§Examples
use hulahoop::HashRing;
let mut ring: HashRing<&str, _> = HashRing::new();
ring.insert("10.0.0.1:1234", 1);
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));Implementations§
Source§impl<N> HashRing<N, BuildHasherDefault<DefaultHasher>>
impl<N> HashRing<N, BuildHasherDefault<DefaultHasher>>
Source§impl<N, B> HashRing<N, B>where
N: Hash,
B: BuildHasher,
impl<N, B> HashRing<N, B>where
N: Hash,
B: BuildHasher,
Sourcepub fn with_hasher(hash_builder: B) -> Self
pub fn with_hasher(hash_builder: B) -> Self
Creates an empty HashRing which will use the given hash_builder to hash nodes and keys.
§Examples
use std::hash::BuildHasherDefault;
use rustc_hash::FxHasher;
use hulahoop::HashRing;
let mut ring: HashRing<&str, BuildHasherDefault<FxHasher>> = HashRing::with_hasher(BuildHasherDefault::<FxHasher>::default());
ring.insert("10.0.0.1:1234", 1);
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));Sourcepub fn hasher(&self) -> &B
pub fn hasher(&self) -> &B
Returns a reference to the ring’s BuildHasher.
§Examples
use hulahoop::HashRing;
use std::collections::hash_map::RandomState;
let hasher = RandomState::new();
let ring: HashRing<&str, _> = HashRing::with_hasher(hasher);
let hasher: &RandomState = ring.hasher();Sourcepub fn insert(&mut self, node: N, weight: u64) -> Option<N>
pub fn insert(&mut self, node: N, weight: u64) -> Option<N>
Inserts a node to the HashRing.
A weight, representing the number of virtual nodes for the given node, must be provided.
There can be hash collisions resulting in fewer than weight virtual nodes added.
If the ring did not have this node present or weight is 0, None is returned.
If the ring did have this node present, the virtual nodes are updated, and the old node is returned.
§Examples
use hulahoop::HashRing;
let mut ring: HashRing<&str, _> = HashRing::default();
assert_eq!(ring.insert("10.0.0.1:1234", 1), None);
assert_eq!(ring.insert("10.0.0.1:1234", 1), Some("10.0.0.1:1234"));Sourcepub fn get<K>(&self, key: K) -> Option<&N>where
K: Hash,
pub fn get<K>(&self, key: K) -> Option<&N>where
K: Hash,
Returns a reference to the node with a hash closest to the hash of the key.
§Examples
use hulahoop::HashRing;
let mut ring: HashRing<&str, _> = HashRing::default();
ring.insert("10.0.0.1:1234", 1);
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));
assert_eq!(ring.get(12345), Some(&"10.0.0.1:1234"));Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of nodes in the Hashring.
It does not return the number of virtual nodes (as specified with weight in the insert method).
§Examples
use hulahoop::HashRing;
let mut ring: HashRing<&str, _> = HashRing::default();
ring.insert("10.0.0.1:1234", 10);
ring.insert("10.0.0.2:1234", 10);
assert_eq!(ring.len(), 2);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the ring contains no elements.
§Examples
use hulahoop::HashRing;
let mut ring: HashRing<&str, _> = HashRing::default();
assert!(ring.is_empty());
ring.insert("10.0.0.1:1234", 10);
assert!(!ring.is_empty());Sourcepub fn contains_node(&self, node: &N) -> bool
pub fn contains_node(&self, node: &N) -> bool
Returns true if the ring contains the specified node.
§Examples
use hulahoop::HashRing;
let mut ring: HashRing<&str, _> = HashRing::default();
ring.insert("10.0.0.1:1234", 10);
assert_eq!(ring.contains_node(&"10.0.0.1:1234"), true);
assert_eq!(ring.contains_node(&"10.0.0.2:1234"), false);Sourcepub fn iter(&self) -> Iter<'_, N> ⓘ
pub fn iter(&self) -> Iter<'_, N> ⓘ
An iterator visiting all node-weight pairs in arbitrary order. The iterator element type is (&'a N, u64).
The weight is the actual number of virtual nodes. It may be lower than the weight provided when inserting
a node in case of hash collisions.
§Examples
use hulahoop::HashRing;
let mut ring: HashRing<&str, _> = HashRing::default();
ring.insert("10.0.0.1:1234", 10);
ring.insert("10.0.0.2:1234", 5);
for (node, weight) in ring.iter() {
println!("node: {node} weight: {weight}");
}Sourcepub fn remove(&mut self, node: &N) -> u64
pub fn remove(&mut self, node: &N) -> u64
Removes a node from the HashRing, returning the number of virtual nodes (weight) of the removed node.
The returned number is the actual number of virtual nodes. It may be lower than the weight provided when inserting
a node in case of hash collisions.
§Examples
use hulahoop::HashRing;
let mut ring: HashRing<&str, _> = HashRing::default();
ring.insert("10.0.0.1:1234", 10);
assert_eq!(ring.remove(&"10.0.0.1:1234"), 10);
assert_eq!(ring.remove(&"10.0.0.1:1234"), 0);