#consistent-hashing #distributed-systems #hashing #hashring

conshash

A library to do consistent hashing in Rust

5 releases

Uses old Rust 2015

0.1.4 Dec 3, 2019
0.1.3 Nov 23, 2017
0.1.2 Aug 21, 2015
0.1.1 Jun 8, 2015
0.1.0 Jun 8, 2015

#7 in #option-t

Download history 3/week @ 2023-11-02 3/week @ 2023-11-09 3/week @ 2023-11-16 10/week @ 2023-11-23 24/week @ 2023-11-30 2/week @ 2023-12-07 8/week @ 2023-12-14 13/week @ 2023-12-21 7/week @ 2023-12-28 3/week @ 2024-01-04 1/week @ 2024-01-11 13/week @ 2024-01-18 10/week @ 2024-01-25 11/week @ 2024-02-01 13/week @ 2024-02-08 57/week @ 2024-02-15

92 downloads per month

MIT license

9KB
129 lines

conshash

Build Status

A library to do consistent hashing in Rust.

Crate

Get the crate at conshash

Example

extern crate conshash;


#[derive(Clone , Debug)]
struct TestNode {
   host_name: &'static str,
   ip_address: &'static str,
   port: u32,
}

impl ToString for TestNode {
   fn to_string(&self) -> String {
       format!("{}{}", self.ip_address.to_string(), self.port.to_string())
   }
}

let mut hash_ring = Ring::new(5);

let test_node = TestNode{host_name: "Skynet", ip_address: "192.168.1.1", port: 42};
hash_ring.add_node(&test_node);
hash_ring.remove_node(&test_node);
hash_ring.add_node(&test_node);
let x = hash_ring.get_node(hash(&format!("{}{}", test_node.to_string(), 0.to_string())));
// x is the node in the form of an Option<T> where T: Clone + ToString + Debug

License

MIT


lib.rs:

#Example

extern crate conshash;

use std::collections::hash_map::DefaultHasher;

#[derive(Clone, Debug)]
struct TestNode {
    host_name: &'static str,
    ip_address: &'static str,
    port: u32,
}

impl ToString for TestNode {
    fn to_string(&self) -> String {
        format!("{}{}", self.ip_address.to_string(), self.port.to_string())
    }
}

let mut hash_ring = conshash::Ring::new(5);

let test_node = TestNode{host_name: "Skynet", ip_address: "192.168.1.1", port: 42};
hash_ring.add_node(&test_node);
hash_ring.remove_node(&test_node);
hash_ring.add_node(&test_node);
let x = hash_ring.get_node(conshash::hash(&format!("{}{}", test_node.to_string(), 0.to_string())));
// x is the node in the form of an Option<T> where T: Clone + ToString + Debug

No runtime deps