#consistent #hash #cache

conhash

Consistent Hashing library in Rust

7 unstable releases

Uses old Rust 2015

0.4.0 Jan 14, 2018
0.3.3 Jan 14, 2018
0.3.2 Apr 8, 2016
0.3.1 Jan 11, 2016
0.1.0 May 1, 2015

#41 in Caching

Download history 4612/week @ 2021-09-26 3780/week @ 2021-10-03 4441/week @ 2021-10-10 4659/week @ 2021-10-17 4864/week @ 2021-10-24 3969/week @ 2021-10-31 3144/week @ 2021-11-07 3146/week @ 2021-11-14 2258/week @ 2021-11-21 3249/week @ 2021-11-28 2855/week @ 2021-12-05 3033/week @ 2021-12-12 2689/week @ 2021-12-19 2104/week @ 2021-12-26 3319/week @ 2022-01-02 2558/week @ 2022-01-09

10,798 downloads per month
Used in 4 crates (2 directly)

MIT/Apache

11KB
171 lines

Consistent Hashing for Rust

Build Status

Consistent hashing is a special kind of hashing such that when a hash table is resized and consistent hashing is used, only K/n keys need to be remapped on average, where K is the number of keys, and n is the number of slots.

Usage

[dependencies]
conhash = "*"
extern crate conhash;

use conhash::{ConsistentHash, Node};

#[derive(Debug, Clone, Eq, PartialEq)]
struct ServerNode {
    host: String,
    port: u16,
}

impl Node for ServerNode {
    fn name(&self) -> String {
        format!("{}:{}", self.host, self.port)
    }
}

impl ServerNode {
    fn new(host: &str, port: u16) -> ServerNode {
        ServerNode {
            host: host.to_owned(),
            port: port,
        }
    }
}

fn main() {
    let nodes = [
        ServerNode::new("localhost", 12345),
        ServerNode::new("localhost", 12346),
        ServerNode::new("localhost", 12347),
        ServerNode::new("localhost", 12348),
        ServerNode::new("localhost", 12349),
        ServerNode::new("localhost", 12350),
        ServerNode::new("localhost", 12351),
        ServerNode::new("localhost", 12352),
        ServerNode::new("localhost", 12353),
    ];

    const REPLICAS: usize = 20;

    let mut ch = ConsistentHash::new();

    for node in nodes.iter() {
        ch.add(node, REPLICAS);
    }

    assert_eq!(ch.len(), nodes.len() * REPLICAS);

    let node_for_hello = ch.get("hello").unwrap().clone();
    assert_eq!(node_for_hello, ServerNode::new("localhost", 12347));

    ch.remove(&ServerNode::new("localhost", 12350));
    assert_eq!(ch.get("hello").unwrap().clone(), node_for_hello);
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~96KB