#consistent-hashing #hashing #consistent #google

maglev

Maglev - Google的一致性哈希算法

6次发布

0.2.1 2021年10月26日
0.2.0 2020年11月14日
0.1.3 2017年1月23日

#696算法

Download history 2095/week @ 2024-03-14 1184/week @ 2024-03-21 1339/week @ 2024-03-28 1143/week @ 2024-04-04 1267/week @ 2024-04-11 2018/week @ 2024-04-18 3623/week @ 2024-04-25 2100/week @ 2024-05-02 2729/week @ 2024-05-09 889/week @ 2024-05-16 1185/week @ 2024-05-23 1648/week @ 2024-05-30 3885/week @ 2024-06-06 3868/week @ 2024-06-13 2362/week @ 2024-06-20 2685/week @ 2024-06-27

13,143 每月下载量
用于 sonya-proxy

Apache-2.0

16KB
281

rust-maglev travis build crate docs

Google的一致性哈希算法

用法

要使用 maglev,首先将以下内容添加到你的 Cargo.toml

[dependencies]
maglev = "0.2"

然后,使用 MaglevConsistentHasher 特性一起使用

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 默认使用 std::collections::hash_map::DefaultHasher,我们可以使用提供的哈希构建器来哈希键。

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

依赖

~0.8–2.1MB
~31K SLoC