pub struct ConsistentHash { /* private fields */ }Expand description
Virtual-node consistent-hash ring.
Each real node is mapped to virtual_nodes_per_node positions on a
u64 hash ring using FNV-1a hashes of the string "<node_id>_<i>".
Key routing finds the first virtual-node position ≥ hash(key) on the
ring (wrapping around).
Implementations§
Source§impl ConsistentHash
impl ConsistentHash
Sourcepub fn new(virtual_nodes: u32) -> Self
pub fn new(virtual_nodes: u32) -> Self
Create an empty ring with the given number of virtual nodes per real node.
Sourcepub fn add_node(&mut self, node_id: NodeId)
pub fn add_node(&mut self, node_id: NodeId)
Add node_id to the ring by inserting virtual_nodes_per_node hash
positions derived from "<node_id>_<i>" for i in 0..virtual_nodes.
Sourcepub fn remove_node(&mut self, node_id: NodeId)
pub fn remove_node(&mut self, node_id: NodeId)
Remove all virtual nodes associated with node_id.
Sourcepub fn get_node(&self, key: &[u8]) -> Option<NodeId>
pub fn get_node(&self, key: &[u8]) -> Option<NodeId>
Route key to the first node whose ring position is ≥ hash(key),
wrapping around to the lowest position if needed.
Returns None when the ring is empty.
Sourcepub fn get_n_nodes(&self, key: &[u8], n: usize) -> Vec<NodeId>
pub fn get_n_nodes(&self, key: &[u8], n: usize) -> Vec<NodeId>
Return up to n distinct successor nodes for key (for replication).
Starts at the primary successor and walks the ring clockwise, collecting
distinct NodeIds until n unique nodes are found or the ring is
exhausted.
Sourcepub fn virtual_node_count(&self) -> usize
pub fn virtual_node_count(&self) -> usize
Return the number of virtual nodes currently in the ring.
Sourcepub fn real_node_count(&self) -> usize
pub fn real_node_count(&self) -> usize
Return the number of distinct real nodes in the ring.
Trait Implementations§
Source§impl Clone for ConsistentHash
impl Clone for ConsistentHash
Source§fn clone(&self) -> ConsistentHash
fn clone(&self) -> ConsistentHash
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more