pub struct HashRing<T, S = BuildHasherDefault<XxHash64>> { /* private fields */ }Expand description
HashRing
Implementations§
Source§impl<T: ToString + Clone> HashRing<T, BuildHasherDefault<XxHash64>>
impl<T: ToString + Clone> HashRing<T, BuildHasherDefault<XxHash64>>
Sourcepub fn new(
nodes: Vec<T>,
replicas: isize,
) -> HashRing<T, BuildHasherDefault<XxHash64>>
pub fn new( nodes: Vec<T>, replicas: isize, ) -> HashRing<T, BuildHasherDefault<XxHash64>>
Creates a new hash ring with the specified nodes. Replicas is the number of virtual nodes each node has to make a better distribution.
Examples found in repository?
examples/readme.rs (line 33)
6fn main() {
7 let mut nodes: Vec<NodeInfo> = Vec::new();
8 nodes.push(NodeInfo {
9 host: "localhost",
10 port: 15324,
11 });
12 nodes.push(NodeInfo {
13 host: "localhost",
14 port: 15325,
15 });
16 nodes.push(NodeInfo {
17 host: "localhost",
18 port: 15326,
19 });
20 nodes.push(NodeInfo {
21 host: "localhost",
22 port: 15327,
23 });
24 nodes.push(NodeInfo {
25 host: "localhost",
26 port: 15328,
27 });
28 nodes.push(NodeInfo {
29 host: "localhost",
30 port: 15329,
31 });
32
33 let mut hash_ring: HashRing<NodeInfo> = HashRing::new(nodes, 10);
34
35 println!(
36 "Key: '{}', Node: {}",
37 "hello",
38 hash_ring.get_node(("hello").to_string()).unwrap()
39 );
40
41 println!(
42 "Key: '{}', Node: {}",
43 "dude",
44 hash_ring.get_node(("dude").to_string()).unwrap()
45 );
46
47 println!(
48 "Key: '{}', Node: {}",
49 "martian",
50 hash_ring.get_node(("martian").to_string()).unwrap()
51 );
52
53 println!(
54 "Key: '{}', Node: {}",
55 "tardis",
56 hash_ring.get_node(("tardis").to_string()).unwrap()
57 );
58
59 hash_ring.remove_node(&NodeInfo {
60 host: "localhost",
61 port: 15329,
62 });
63
64 println!(
65 "Key: '{}', Node: {}",
66 "hello",
67 hash_ring.get_node(("hello").to_string()).unwrap()
68 );
69
70 hash_ring.add_node(&NodeInfo {
71 host: "localhost",
72 port: 15329,
73 });
74
75 println!(
76 "Key: '{}', Node: {}",
77 "hello",
78 hash_ring.get_node(("hello").to_string()).unwrap()
79 );
80}Source§impl<T, S> HashRing<T, S>
impl<T, S> HashRing<T, S>
Sourcepub fn with_hasher(
nodes: Vec<T>,
replicas: isize,
hash_builder: S,
) -> HashRing<T, S>
pub fn with_hasher( nodes: Vec<T>, replicas: isize, hash_builder: S, ) -> HashRing<T, S>
Examples found in repository?
examples/custom_hasher.rs (line 55)
27fn main() {
28 let mut nodes: Vec<NodeInfo> = Vec::new();
29 nodes.push(NodeInfo {
30 host: "localhost",
31 port: 15324,
32 });
33 nodes.push(NodeInfo {
34 host: "localhost",
35 port: 15325,
36 });
37 nodes.push(NodeInfo {
38 host: "localhost",
39 port: 15326,
40 });
41 nodes.push(NodeInfo {
42 host: "localhost",
43 port: 15327,
44 });
45 nodes.push(NodeInfo {
46 host: "localhost",
47 port: 15328,
48 });
49 nodes.push(NodeInfo {
50 host: "localhost",
51 port: 15329,
52 });
53
54 let hash_ring: HashRing<NodeInfo, ConstantBuildHasher> =
55 HashRing::with_hasher(nodes, 10, ConstantBuildHasher::default());
56
57 println!(
58 "Key: '{}', Node: {}",
59 "hello",
60 hash_ring.get_node(("hello").to_string()).unwrap()
61 );
62 println!(
63 "Key: '{}', Node: {}",
64 "dude",
65 hash_ring.get_node(("dude").to_string()).unwrap()
66 );
67 println!(
68 "Key: '{}', Node: {}",
69 "martian",
70 hash_ring.get_node(("martian").to_string()).unwrap()
71 );
72 println!(
73 "Key: '{}', Node: {}",
74 "tardis",
75 hash_ring.get_node(("tardis").to_string()).unwrap()
76 );
77}Sourcepub fn add_node(&mut self, node: &T)
pub fn add_node(&mut self, node: &T)
Adds a node to the hash ring
Examples found in repository?
examples/readme.rs (lines 70-73)
6fn main() {
7 let mut nodes: Vec<NodeInfo> = Vec::new();
8 nodes.push(NodeInfo {
9 host: "localhost",
10 port: 15324,
11 });
12 nodes.push(NodeInfo {
13 host: "localhost",
14 port: 15325,
15 });
16 nodes.push(NodeInfo {
17 host: "localhost",
18 port: 15326,
19 });
20 nodes.push(NodeInfo {
21 host: "localhost",
22 port: 15327,
23 });
24 nodes.push(NodeInfo {
25 host: "localhost",
26 port: 15328,
27 });
28 nodes.push(NodeInfo {
29 host: "localhost",
30 port: 15329,
31 });
32
33 let mut hash_ring: HashRing<NodeInfo> = HashRing::new(nodes, 10);
34
35 println!(
36 "Key: '{}', Node: {}",
37 "hello",
38 hash_ring.get_node(("hello").to_string()).unwrap()
39 );
40
41 println!(
42 "Key: '{}', Node: {}",
43 "dude",
44 hash_ring.get_node(("dude").to_string()).unwrap()
45 );
46
47 println!(
48 "Key: '{}', Node: {}",
49 "martian",
50 hash_ring.get_node(("martian").to_string()).unwrap()
51 );
52
53 println!(
54 "Key: '{}', Node: {}",
55 "tardis",
56 hash_ring.get_node(("tardis").to_string()).unwrap()
57 );
58
59 hash_ring.remove_node(&NodeInfo {
60 host: "localhost",
61 port: 15329,
62 });
63
64 println!(
65 "Key: '{}', Node: {}",
66 "hello",
67 hash_ring.get_node(("hello").to_string()).unwrap()
68 );
69
70 hash_ring.add_node(&NodeInfo {
71 host: "localhost",
72 port: 15329,
73 });
74
75 println!(
76 "Key: '{}', Node: {}",
77 "hello",
78 hash_ring.get_node(("hello").to_string()).unwrap()
79 );
80}Sourcepub fn remove_node(&mut self, node: &T)
pub fn remove_node(&mut self, node: &T)
Deletes a node from the hash ring
Examples found in repository?
examples/readme.rs (lines 59-62)
6fn main() {
7 let mut nodes: Vec<NodeInfo> = Vec::new();
8 nodes.push(NodeInfo {
9 host: "localhost",
10 port: 15324,
11 });
12 nodes.push(NodeInfo {
13 host: "localhost",
14 port: 15325,
15 });
16 nodes.push(NodeInfo {
17 host: "localhost",
18 port: 15326,
19 });
20 nodes.push(NodeInfo {
21 host: "localhost",
22 port: 15327,
23 });
24 nodes.push(NodeInfo {
25 host: "localhost",
26 port: 15328,
27 });
28 nodes.push(NodeInfo {
29 host: "localhost",
30 port: 15329,
31 });
32
33 let mut hash_ring: HashRing<NodeInfo> = HashRing::new(nodes, 10);
34
35 println!(
36 "Key: '{}', Node: {}",
37 "hello",
38 hash_ring.get_node(("hello").to_string()).unwrap()
39 );
40
41 println!(
42 "Key: '{}', Node: {}",
43 "dude",
44 hash_ring.get_node(("dude").to_string()).unwrap()
45 );
46
47 println!(
48 "Key: '{}', Node: {}",
49 "martian",
50 hash_ring.get_node(("martian").to_string()).unwrap()
51 );
52
53 println!(
54 "Key: '{}', Node: {}",
55 "tardis",
56 hash_ring.get_node(("tardis").to_string()).unwrap()
57 );
58
59 hash_ring.remove_node(&NodeInfo {
60 host: "localhost",
61 port: 15329,
62 });
63
64 println!(
65 "Key: '{}', Node: {}",
66 "hello",
67 hash_ring.get_node(("hello").to_string()).unwrap()
68 );
69
70 hash_ring.add_node(&NodeInfo {
71 host: "localhost",
72 port: 15329,
73 });
74
75 println!(
76 "Key: '{}', Node: {}",
77 "hello",
78 hash_ring.get_node(("hello").to_string()).unwrap()
79 );
80}Sourcepub fn get_node(&self, key: String) -> Option<&T>
pub fn get_node(&self, key: String) -> Option<&T>
Gets the node a specific key belongs to
Examples found in repository?
examples/custom_hasher.rs (line 60)
27fn main() {
28 let mut nodes: Vec<NodeInfo> = Vec::new();
29 nodes.push(NodeInfo {
30 host: "localhost",
31 port: 15324,
32 });
33 nodes.push(NodeInfo {
34 host: "localhost",
35 port: 15325,
36 });
37 nodes.push(NodeInfo {
38 host: "localhost",
39 port: 15326,
40 });
41 nodes.push(NodeInfo {
42 host: "localhost",
43 port: 15327,
44 });
45 nodes.push(NodeInfo {
46 host: "localhost",
47 port: 15328,
48 });
49 nodes.push(NodeInfo {
50 host: "localhost",
51 port: 15329,
52 });
53
54 let hash_ring: HashRing<NodeInfo, ConstantBuildHasher> =
55 HashRing::with_hasher(nodes, 10, ConstantBuildHasher::default());
56
57 println!(
58 "Key: '{}', Node: {}",
59 "hello",
60 hash_ring.get_node(("hello").to_string()).unwrap()
61 );
62 println!(
63 "Key: '{}', Node: {}",
64 "dude",
65 hash_ring.get_node(("dude").to_string()).unwrap()
66 );
67 println!(
68 "Key: '{}', Node: {}",
69 "martian",
70 hash_ring.get_node(("martian").to_string()).unwrap()
71 );
72 println!(
73 "Key: '{}', Node: {}",
74 "tardis",
75 hash_ring.get_node(("tardis").to_string()).unwrap()
76 );
77}More examples
examples/readme.rs (line 38)
6fn main() {
7 let mut nodes: Vec<NodeInfo> = Vec::new();
8 nodes.push(NodeInfo {
9 host: "localhost",
10 port: 15324,
11 });
12 nodes.push(NodeInfo {
13 host: "localhost",
14 port: 15325,
15 });
16 nodes.push(NodeInfo {
17 host: "localhost",
18 port: 15326,
19 });
20 nodes.push(NodeInfo {
21 host: "localhost",
22 port: 15327,
23 });
24 nodes.push(NodeInfo {
25 host: "localhost",
26 port: 15328,
27 });
28 nodes.push(NodeInfo {
29 host: "localhost",
30 port: 15329,
31 });
32
33 let mut hash_ring: HashRing<NodeInfo> = HashRing::new(nodes, 10);
34
35 println!(
36 "Key: '{}', Node: {}",
37 "hello",
38 hash_ring.get_node(("hello").to_string()).unwrap()
39 );
40
41 println!(
42 "Key: '{}', Node: {}",
43 "dude",
44 hash_ring.get_node(("dude").to_string()).unwrap()
45 );
46
47 println!(
48 "Key: '{}', Node: {}",
49 "martian",
50 hash_ring.get_node(("martian").to_string()).unwrap()
51 );
52
53 println!(
54 "Key: '{}', Node: {}",
55 "tardis",
56 hash_ring.get_node(("tardis").to_string()).unwrap()
57 );
58
59 hash_ring.remove_node(&NodeInfo {
60 host: "localhost",
61 port: 15329,
62 });
63
64 println!(
65 "Key: '{}', Node: {}",
66 "hello",
67 hash_ring.get_node(("hello").to_string()).unwrap()
68 );
69
70 hash_ring.add_node(&NodeInfo {
71 host: "localhost",
72 port: 15329,
73 });
74
75 println!(
76 "Key: '{}', Node: {}",
77 "hello",
78 hash_ring.get_node(("hello").to_string()).unwrap()
79 );
80}Auto Trait Implementations§
impl<T, S> Freeze for HashRing<T, S>where
S: Freeze,
impl<T, S> RefUnwindSafe for HashRing<T, S>where
S: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, S> Send for HashRing<T, S>
impl<T, S> Sync for HashRing<T, S>
impl<T, S> Unpin for HashRing<T, S>
impl<T, S> UnwindSafe for HashRing<T, S>where
S: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more