Skip to content

Commit

Permalink
Update readme and docs (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajesipow authored Oct 29, 2022
1 parent 9eb5078 commit b7ee205
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@
## Usage

```rust
use std::num::NonZeroU64;
use hulahoop::HashRing;

let mut map: HashRing<&str, _> = HashRing::default();
let mut ring: HashRing<&str, _> = HashRing::default();

// Nodes only need to implement Hash
// Provide a weight to define the number of virtual nodes
map.insert("10.0.0.1:1234", 10);
map.insert("10.0.0.2:1234", 10);
ring.insert("10.0.0.1:1234", 10);
ring.insert("10.0.0.2:1234", 10);

// Keys also only need to implement Hash
assert_eq!(map.get("Some key"), Some(&"10.0.0.1:1234"));
assert_eq!(map.get("Another key"), Some(&"10.0.0.2:1234"));
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));
assert_eq!(ring.get("Another key"), Some(&"10.0.0.2:1234"));

map.remove(&"10.0.0.2:1234");
ring.remove(&"10.0.0.2:1234");

assert_eq!(map.get("Some key"), Some(&"10.0.0.1:1234"));
assert_eq!(map.get("Another key"), Some(&"10.0.0.1:1234"));
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));
assert_eq!(ring.get("Another key"), Some(&"10.0.0.1:1234"));
```

`HashRing` uses `Arc` under the hood to allocate memory only per node and not for every virtual node added via the weight parameter.
Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ where
/// 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, None is returned.
/// 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
Expand Down Expand Up @@ -672,20 +672,20 @@ mod tests {

#[test]
fn read_me_test() {
let mut map: HashRing<&str, _> = HashRing::default();
let mut ring: HashRing<&str, _> = HashRing::default();

// Nodes only need to implement Hash
// Provide a weight to define the number of virtual nodes
map.insert("10.0.0.1:1234", 10);
map.insert("10.0.0.2:1234", 10);
ring.insert("10.0.0.1:1234", 10);
ring.insert("10.0.0.2:1234", 10);

// Keys also only need to implement Hash
assert_eq!(map.get("Some key"), Some(&"10.0.0.1:1234"));
assert_eq!(map.get("Another key"), Some(&"10.0.0.2:1234"));
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));
assert_eq!(ring.get("Another key"), Some(&"10.0.0.2:1234"));

map.remove(&"10.0.0.2:1234");
ring.remove(&"10.0.0.2:1234");

assert_eq!(map.get("Some key"), Some(&"10.0.0.1:1234"));
assert_eq!(map.get("Another key"), Some(&"10.0.0.1:1234"));
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));
assert_eq!(ring.get("Another key"), Some(&"10.0.0.1:1234"));
}
}

0 comments on commit b7ee205

Please sign in to comment.