use super::*;
impl<N> Default for ConsistentHashRing<N> {
fn default() -> Self {
Self::new(DEFAULT_VIRTUAL_NODES)
}
}
impl<N> ConsistentHashRing<N> {
pub fn new(virtual_nodes: usize) -> Self {
Self {
virtual_nodes: virtual_nodes.max(1),
nodes: Vec::new(),
ring: Vec::new(),
}
}
#[inline(always)]
pub fn len(&self) -> usize {
let Self { nodes, .. } = self;
nodes.len()
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline(always)]
pub fn virtual_len(&self) -> usize {
let Self { ring, .. } = self;
ring.len()
}
pub fn add_node<S>(&mut self, id: S, value: N)
where
S: Into<String>,
{
self.add_weighted_node(id, value, 1);
}
pub fn add_weighted_node<S>(&mut self, id: S, value: N, weight: usize)
where
S: Into<String>,
{
let id_str: String = id.into();
let effective_weight: usize = weight.max(1);
let Self { nodes, .. } = self;
if let Some(existing) = nodes.iter_mut().find(|n: &&mut RingNode<N>| n.id == id_str) {
existing.value = value;
existing.weight = effective_weight;
} else {
nodes.push(RingNode {
id: id_str,
weight: effective_weight,
value,
});
}
self.rebuild_ring();
}
pub fn remove_node(&mut self, id: &str) -> Option<N> {
let Self { nodes, .. } = self;
let pos: usize = nodes.iter().position(|n: &RingNode<N>| n.id == id)?;
let removed: RingNode<N> = nodes.swap_remove(pos);
self.rebuild_ring();
Some(removed.value)
}
#[inline]
pub fn get(&self, key: &[u8]) -> Option<&N> {
let Self { nodes, ring, .. } = self;
if ring.is_empty() {
return None;
}
let target: u64 = hash_bytes(key);
let idx: usize = ring.partition_point(|(h, _): &(u64, usize)| *h < target);
let slot: usize = if idx == ring.len() { 0 } else { idx };
let node_idx: usize = ring[slot].1;
nodes.get(node_idx).map(|n: &RingNode<N>| &n.value)
}
pub fn get_replicas(&self, key: &[u8], count: usize) -> Vec<&N> {
let Self { nodes, ring, .. } = self;
let wanted: usize = count.min(nodes.len());
if wanted == 0 || ring.is_empty() {
return Vec::new();
}
let target: u64 = hash_bytes(key);
let start: usize = ring.partition_point(|(h, _): &(u64, usize)| *h < target);
let mut out: Vec<&N> = Vec::with_capacity(wanted);
let mut seen: HashSet<usize> = HashSet::with_capacity(wanted);
let ring_len: usize = ring.len();
let mut step: usize = 0;
while step < ring_len && out.len() < wanted {
let slot: usize = (start + step) % ring_len;
let node_idx: usize = ring[slot].1;
if seen.insert(node_idx) {
out.push(&nodes[node_idx].value);
}
step += 1;
}
out
}
fn rebuild_ring(&mut self) {
let Self {
virtual_nodes,
nodes,
ring,
} = self;
let total_vnodes: usize = nodes
.iter()
.map(|n: &RingNode<N>| n.weight.saturating_mul(*virtual_nodes))
.sum();
ring.clear();
ring.reserve(total_vnodes);
for (node_idx, node) in nodes.iter().enumerate() {
let vcount: usize = node.weight.saturating_mul(*virtual_nodes);
let id_bytes: &[u8] = node.id.as_bytes();
for v in 0..vcount {
let point: u64 = hash_bytes_seeded(id_bytes, (v as u64).wrapping_add(1));
ring.push((point, node_idx));
}
}
ring.sort_unstable_by_key(|(h, idx): &(u64, usize)| (*h, *idx));
}
}