https://github.com/flier/rust-maglev
Google's consistent hashing algorithm
https://github.com/flier/rust-maglev
Last synced: over 1 year ago
JSON representation
Google's consistent hashing algorithm
- Host: GitHub
- URL: https://github.com/flier/rust-maglev
- Owner: flier
- License: apache-2.0
- Created: 2017-01-22T08:51:08.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-10-26T02:52:38.000Z (almost 5 years ago)
- Last Synced: 2025-03-18T05:07:06.349Z (over 1 year ago)
- Language: Rust
- Size: 18.6 KB
- Stars: 25
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rust-maglev [](https://travis-ci.org/flier/rust-maglev) [](https://crates.io/crates/maglev) [](https://docs.rs/maglev/)
Google's consistent hashing algorithm
## Usage
To use `maglev`, first add this to your `Cargo.toml`:
```toml
[dependencies]
maglev = "0.2"
```
And then, use `Maglev` with `ConsistentHasher` trait
```rust
use maglev::{ConsistentHasher, Maglev};
fn main() {
let m = Maglev::new(vec!["Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"]);
assert_eq!(m["alice"], "Friday");
assert_eq!(m["bob"], "Wednesday");
// When the node list changed, ensure to use same `capacity` to rebuild
let m = Maglev::with_capacity(vec!["Monday",
// "Tuesday",
"Wednesday",
// "Thursday",
"Friday",
"Saturday",
"Sunday"],
m.capacity());
assert_eq!(m["alice"], "Friday");
assert_eq!(m["bob"], "Wednesday");
}
```
Maglev use `std::collections::hash_map::DefaultHasher` by default, we could use the given hash builder to hash keys.
```rust
use fasthash::spooky::Hash128;
use maglev::Maglev;
fn main() {
let m = Maglev::with_hasher(vec!["Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"],
Hash128 {});
assert_eq!(m["alice"], "Monday");
assert_eq!(m["bob"], "Wednesday");
}
```