Skip to content

Commit

Permalink
Merge pull request #21 from dzhao/master
Browse files Browse the repository at this point in the history
add a batch_add feature to avoid sort for each virtual node addition
  • Loading branch information
jeromefroe authored Nov 12, 2023
2 parents 1f2b7ad + 09d0527 commit 6cc61fe
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ impl<T: Hash, S: BuildHasher> HashRing<T, S> {
self.ring.sort();
}

pub fn batch_add(&mut self, nodes: Vec<T>) {
for node in nodes {
let key = get_key(&self.hash_builder, &node);
self.ring.push(Node::new(key, node));
}
self.ring.sort()
}

/// Remove `node` from the hash ring. Returns an `Option` that will contain the `node`
/// if it was in the hash ring or `None` if it was not present.
pub fn remove(&mut self, node: &T) -> Option<T> {
Expand Down Expand Up @@ -338,9 +346,7 @@ mod tests {
let vnode5 = VNode::new("127.0.0.2", 1024, 3);
let vnode6 = VNode::new("127.0.0.3", 1024, 1);

ring.add(vnode4);
ring.add(vnode5);
ring.add(vnode6);
ring.batch_add(vec![vnode4, vnode5, vnode6]);

assert_eq!(ring.remove(&vnode1).unwrap(), vnode1);
assert_eq!(ring.remove(&vnode3).unwrap(), vnode3);
Expand Down

0 comments on commit 6cc61fe

Please sign in to comment.