GitXplorerGitXplorer
f

rust-maglev

public
25 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
e4c507d642e8da152f5e07777070e1390b17a470

refactor API

fflier committed 6 years ago
Unverified
23a0404f5ef5606b2c173f396a140a644c9999bc

Improve document

fflier committed 8 years ago
Unverified
b4c3ee96b9d173c819499a6b4f6b9c8bb9481084

Bump version to 0.1.3

fflier committed 8 years ago
Unverified
e66237d676d1b46e702b2206b09fe6e5b53b44a8

Update links

fflier committed 8 years ago
Unverified
1dd57019f2225b4605da1332be3f89e63da41cb3

Add fasthash for testing and example

fflier committed 8 years ago
Unverified
fd615a039ff26e91bb17b586b68287b5edaeaf00

Bump version to 0.1.2

fflier committed 8 years ago

README

The README file for this repository.

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.2"

And then, use Maglev with ConsistentHasher trait

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.

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");
}