-
Notifications
You must be signed in to change notification settings - Fork 17
/
custom_hasher.rs
77 lines (69 loc) · 1.76 KB
/
custom_hasher.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
extern crate hash_ring;
use hash_ring::HashRing;
use hash_ring::NodeInfo;
use std::hash::BuildHasherDefault;
use std::hash::Hasher;
// This is a hasher that always returns the same number
// no matter the input. This is meant as an example and
// should never be used in production code as all keys go
// to the same node.
#[derive(Default)]
struct ConstantHasher;
impl Hasher for ConstantHasher {
fn write(&mut self, _bytes: &[u8]) {
// Do nothing
}
fn finish(&self) -> u64 {
return 1;
}
}
type ConstantBuildHasher = BuildHasherDefault<ConstantHasher>;
fn main() {
let mut nodes: Vec<NodeInfo> = Vec::new();
nodes.push(NodeInfo {
host: "localhost",
port: 15324,
});
nodes.push(NodeInfo {
host: "localhost",
port: 15325,
});
nodes.push(NodeInfo {
host: "localhost",
port: 15326,
});
nodes.push(NodeInfo {
host: "localhost",
port: 15327,
});
nodes.push(NodeInfo {
host: "localhost",
port: 15328,
});
nodes.push(NodeInfo {
host: "localhost",
port: 15329,
});
let hash_ring: HashRing<NodeInfo, ConstantBuildHasher> =
HashRing::with_hasher(nodes, 10, ConstantBuildHasher::default());
println!(
"Key: '{}', Node: {}",
"hello",
hash_ring.get_node(("hello").to_string()).unwrap()
);
println!(
"Key: '{}', Node: {}",
"dude",
hash_ring.get_node(("dude").to_string()).unwrap()
);
println!(
"Key: '{}', Node: {}",
"martian",
hash_ring.get_node(("martian").to_string()).unwrap()
);
println!(
"Key: '{}', Node: {}",
"tardis",
hash_ring.get_node(("tardis").to_string()).unwrap()
);
}