maglev 0.2.0

Maglev - Google's consistent hashing algorithm
Documentation

rust-maglev travis build crate docs

Google's consistent hashing algorithm

Usage

To use maglev, first add this to your Cargo.toml:

[dependencies]
maglev = "0.1"

Then, add this to your crate root:

extern crate maglev;

use maglev::*;

And then, use Maglev with ConsistentHasher trait

let m = Maglev::new(&["Monday",
                      "Tuesday",
                      "Wednesday",
                      "Thursday",
                      "Friday",
                      "Saturday",
                      "Sunday"][..]);

assert_eq!(*m.get(&"alice"), "Friday");
assert_eq!(*m.get(&"bob"), "Wednesday");

When the node list changed, ensure to use same capacity to rebuild

let m = Maglev::with_capacity(&["Monday",
                                // "Tuesday",
                                "Wednesday",
                                // "Thursday",
                                "Friday",
                                "Saturday",
                                "Sunday"][..],
                              m.capacity());

assert_eq!(*m.get(&"alice"), "Friday");
assert_eq!(*m.get(&"bob"), "Wednesday");

Maglev use std::collections::hash_map::DefaultHasher by default, we could use the given hash builder to hash keys.

use fasthash::spooky::SpookyHash128;

let m = Maglev::with_hasher(&["Monday",
                              "Tuesday",
                              "Wednesday",
                              "Thursday",
                              "Friday",
                              "Saturday",
                              "Sunday"][..],
                            SpookyHash128 {});

assert_eq!(*m.get(&"alice"), "Monday");
assert_eq!(*m.get(&"bob"), "Wednesday");