HashRing

Struct HashRing 

Source
pub struct HashRing<T, S = BuildHasherDefault<XxHash64>> { /* private fields */ }
Expand description

HashRing

Implementations§

Source§

impl<T: ToString + Clone> HashRing<T, BuildHasherDefault<XxHash64>>

Source

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>
where T: ToString + Clone, S: BuildHasher,

Source

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}
Source

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}
Source

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}
Source

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
Hide additional 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>

§

impl<T, S> Send for HashRing<T, S>
where S: Send, T: Send,

§

impl<T, S> Sync for HashRing<T, S>
where S: Sync, T: Sync,

§

impl<T, S> Unpin for HashRing<T, S>
where S: Unpin, T: Unpin,

§

impl<T, S> UnwindSafe for HashRing<T, S>
where S: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V