Struct lfchring::HashRing [−][src]
The consistent hashing ring data structure.
Users will probably interact with this crate mostly through this type, as it is central to its API.
In multi-threaded contexts, it needs to be wrapped in Arc
.
To find out more general information regarding its use, refer to the crate-level documentation.
Implementations
impl<N: ?Sized> HashRing<N, DefaultStdHasher> where
N: Node,
[src]
N: Node,
pub fn with_nodes(
vnodes_per_node: Vnid,
replication_factor: u8,
nodes: &[Arc<N>]
) -> Result<Self>
[src]
vnodes_per_node: Vnid,
replication_factor: u8,
nodes: &[Arc<N>]
) -> Result<Self>
Create a new HashRing<N, H>
configured with the given parameters (i.e., the number of
virtual nodes per ring node and the replication factor) and initialize it with the
provided Node
s (the ring will be populated by their VirtualNode
s automatically).
The new HashRing<N, H>
will employ the built-in Hasher
that is based on standard
library’s DefaultHasher
.
Errors
-
Returns
HashRingError::InvalidConfiguration
if either the number of virtual nodes per distinct ring node or the replication factor is0
. -
Returns
HashRingError::VirtualNodeAlreadyExists
in the case that a hash collision occurs while attempting to insert the givenNode
s in the new consistent hashing ring. The reason for this can be one of the following:- the output of
Node::hashring_node_id
for two (or more) of theNode
s provided for insertion is equal; - the provided
Hasher
produces equal hash digests for different outputs ofNode::hashring_node_id
for two (or more)Node
s among those provided for insertion.
- the output of
pub fn new(vnodes_per_node: Vnid, replication_factor: u8) -> Result<Self>
[src]
Create a new HashRing<N, H>
configured with the given parameters (i.e., the number of
virtual nodes per ring node and the replication factor), which is initially empty of
Node
s (and, of course, empty of VirtualNode
s too).
The new HashRing<N, H>
will employ the built-in Hasher
that is based on standard
library’s DefaultHasher
.
Errors
Returns HashRingError::InvalidConfiguration
if either the number of virtual nodes per
distinct ring node or the replication factor is 0
.
impl<N: ?Sized, H> HashRing<N, H> where
N: Node,
H: Hasher,
[src]
N: Node,
H: Hasher,
pub fn with_hasher_and_nodes(
hasher: H,
vnodes_per_node: Vnid,
replication_factor: u8,
nodes: &[Arc<N>]
) -> Result<Self>
[src]
hasher: H,
vnodes_per_node: Vnid,
replication_factor: u8,
nodes: &[Arc<N>]
) -> Result<Self>
Create a new HashRing<N, H>
configured with the given parameters (i.e., the number of
virtual nodes per ring node and the replication factor) and initialize it with the
provided Node
s (the ring will be populated by their VirtualNode
s automatically).
The new HashRing<N, H>
will employ the provided Hasher
for placing the
VirtualNode
s on the consistent hashing ring.
Errors
-
Returns
HashRingError::InvalidConfiguration
if either the number of virtual nodes per distinct ring node or the replication factor is0
. -
Returns
HashRingError::VirtualNodeAlreadyExists
in the case that a hash collision occurs while attempting to insert the givenNode
s in the new consistent hashing ring. The reason for this can be one of the following:- the output of
Node::hashring_node_id
for two (or more) of theNode
s provided for insertion is equal; - the provided
Hasher
produces equal hash digests for different outputs ofNode::hashring_node_id
for two (or more)Node
s among those provided for insertion.
- the output of
pub fn with_hasher(
hasher: H,
vnodes_per_node: Vnid,
replication_factor: u8
) -> Result<Self>
[src]
hasher: H,
vnodes_per_node: Vnid,
replication_factor: u8
) -> Result<Self>
Create a new HashRing<N, H>
configured with the given parameters (i.e., the number of
virtual nodes per ring node and the replication factor), which is initially empty of
Node
s (and, of course, empty of VirtualNode
s too).
The new HashRing<N, H>
will employ the provided Hasher
for placing the
VirtualNode
s on the consistent hashing ring.
Errors
Returns HashRingError::InvalidConfiguration
if either the number of virtual nodes per
distinct ring node or the replication factor is 0
.
pub fn len_nodes(&self) -> usize
[src]
Returns the number of distinct ring nodes that currently populate the consistent hashing ring.
pub fn len_virtual_nodes(&self) -> usize
[src]
Returns the number of virtual nodes that currently populate the consistent hashing ring.
This should always be equal to the result of HashRing::len_nodes
multiplied by the
vnodes_per_node
for a particular HashRing<N, H>
.
pub fn insert(&self, nodes: &[Arc<N>]) -> Result<()>
[src]
Insert the given Node
s to the consistent hashing ring, thereby expanding it.
Errors
HashRingError::VirtualNodeAlreadyExists
is returned in the case of a hash collision
while attempting to insert the given Node
s in the consistent hashing ring.
This can happen if:
- the output of
Node::hashring_node_id
for one (or more) of the newNode
s is equal to that of one of theNode
s that already exist in the ring; - the output of
Node::hashring_node_id
for two (or more) of the newNode
s is equal to each other; - the provided
Hasher
produces equal hash digests for different outputs ofNode::hashring_node_id
for two (or more)Node
s among the new or the already existing ones.
pub fn remove(&self, nodes: &[Arc<N>]) -> Result<()>
[src]
Remove the given Node
s from the consistent hashing ring, thereby shrinking it.
Errors
HashRingError::VirtualNodeDoesNotExist
is returned in the case that one of the
Node
s provided for removal does not currently exist in the consistent hashing ring.
pub fn has_virtual_node<K>(&self, key: &K) -> bool where
K: Borrow<[u8]>,
[src]
K: Borrow<[u8]>,
Returns true
if the provided key
corresponds to an existing VirtualNode
of some
Node
in the consistent hashing ring, or false
otherwise.
pub fn virtual_node_for_key<K>(&self, key: &K) -> Result<VirtualNode<N>> where
K: Borrow<[u8]>,
[src]
K: Borrow<[u8]>,
Look up in the consistent hashing ring and return a clone of the VirtualNode
that
the given key
should be assigned on.
Errors
Returns HashRingError::EmptyRing
if the consistent hashing ring is currently empty of
Node
s and therefore the given key
cannot be assigned to any VirtualNode
(as none
exists).
pub fn nodes_for_key<K>(&self, key: &K) -> Result<Vec<Arc<N>>> where
K: Borrow<[u8]>,
[src]
K: Borrow<[u8]>,
Look up in the consistent hashing ring and return a Vec
of Node
s, each wrapped in
an Arc
, on which a replica of the given key
should be assigned on.
The Node
s are returned according to their order in the consistent hashing ring.
Therefore, the first Node
is always the one that the particular VirtualNode
originally belongs to.
Errors
Returns HashRingError::EmptyRing
if the consistent hashing ring is currently empty of
Node
s and therefore the given key
cannot be assigned to any VirtualNode
(as none
exists).
pub fn predecessor<K>(&self, key: &K) -> Result<VirtualNode<N>> where
K: Borrow<[u8]>,
[src]
K: Borrow<[u8]>,
Look up in the consistent hashing ring and return the VirtualNode
which is the
predecessor of the one that the given key
should be assigned on.
Errors
Returns HashRingError::EmptyRing
if the consistent hashing ring is currently empty of
Node
s and therefore the given key
cannot be assigned to any VirtualNode
(as none
exists).
pub fn successor<K>(&self, key: &K) -> Result<VirtualNode<N>> where
K: Borrow<[u8]>,
[src]
K: Borrow<[u8]>,
Look up in the consistent hashing ring and return the VirtualNode
which is the
successor of the one that the given key
should be assigned on.
Errors
Returns HashRingError::EmptyRing
if the consistent hashing ring is currently empty of
Node
s and therefore the given key
cannot be assigned to any VirtualNode
(as none
exists).
pub fn predecessor_node<K>(&self, key: &K) -> Result<VirtualNode<N>> where
K: Borrow<[u8]>,
[src]
K: Borrow<[u8]>,
Look up in the consistent hashing ring and return the first predecessor VirtualNode
to
the one that the given key
should be assigned on, but which also belongs to a different
distinct Node
than the latter.
Errors
- Returns
HashRingError::EmptyRing
if the consistent hashing ring is currently empty ofNode
s and therefore the givenkey
cannot be assigned to anyVirtualNode
(as none exists). - Returns
HashRingError::SingleDistinctNodeRing
if the consistent hashing ring currently consists of a single distinct node and therefore allVirtualNode
s in the ring actually belong to the sameNode
.
pub fn successor_node<K>(&self, key: &K) -> Result<VirtualNode<N>> where
K: Borrow<[u8]>,
[src]
K: Borrow<[u8]>,
Look up in the consistent hashing ring and return the first successor VirtualNode
to
the one that the given key
should be assigned on, but which also belongs to a different
distinct Node
than the latter.
Errors
- Returns
HashRingError::EmptyRing
if the consistent hashing ring is currently empty ofNode
s and therefore the givenkey
cannot be assigned to anyVirtualNode
(as none exists). - Returns
HashRingError::SingleDistinctNodeRing
if the consistent hashing ring currently consists of a single distinct node and therefore allVirtualNode
s in the ring actually belong to the sameNode
.
pub fn iter<'guard>(&self, guard: &'guard Guard) -> Iter<'guard, N, H>ⓘ
[src]
Returns an Iter
, i.e., an iterator to loop through all VirtualNode
s that populate
the consistent hashing ring.
See the documentation of Iter
for more information regarding its use.
Trait Implementations
impl<N: ?Sized, H> Clone for HashRing<N, H> where
N: Node,
H: Hasher,
[src]
N: Node,
H: Hasher,
fn clone(&self) -> Self
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<N: Debug + ?Sized, H: Debug> Debug for HashRing<N, H> where
N: Node,
H: Hasher,
[src]
N: Node,
H: Hasher,
impl<N: ?Sized, H> Display for HashRing<N, H> where
N: Node,
H: Hasher,
[src]
N: Node,
H: Hasher,
impl<N: ?Sized, H> Extend<Arc<N>> for HashRing<N, H> where
N: Node,
H: Hasher,
[src]
N: Node,
H: Hasher,
fn extend<I: IntoIterator<Item = Arc<N>>>(&mut self, iter: I)
[src]
Extend the HashRing<N, H>
by the Node
s provided through the given IntoIterator
over Arc<N>>
.
Note that, due to the restriction of Extend::extend
’s signature, a &mut HashRing
is
required to use this method.
The preferred way to extend the ring is via HashRing::insert
anyway; read the section
below for further details.
Panics
Although the Extend
trait is implemented for HashRing<N, H>
, it is not the
preferred way of extending it.
The signature of Extend::extend
does not allow to return a Result::Err
if the
extension attempt fails.
Therefore, in case of hash collision (e.g., when inserting an already existing Node
in
the HashRing<N, H>
) this method fails by panicking (although the ring remains in a
consistent state, since updating the ring is considered an atomic operation).
The preferred way to add Node
s to the HashRing<N, H>
is via HashRing::insert
instead, which returns a Result::Err
that can be handled in case of a failure.
Only use this method if you know for sure that hash collisions are extremely unlikely and
practically impossible (e.g., when employing a cryptographically secure hash algorithm and
no attempts to re-insert existing Node
s occur).
pub fn extend_one(&mut self, item: A)
[src]
pub fn extend_reserve(&mut self, additional: usize)
[src]
Auto Trait Implementations
impl<N: ?Sized, H> RefUnwindSafe for HashRing<N, H> where
H: RefUnwindSafe,
N: RefUnwindSafe,
H: RefUnwindSafe,
N: RefUnwindSafe,
impl<N: ?Sized, H> Send for HashRing<N, H> where
H: Send + Sync,
N: Send + Sync,
H: Send + Sync,
N: Send + Sync,
impl<N: ?Sized, H> Sync for HashRing<N, H> where
H: Send + Sync,
N: Send + Sync,
H: Send + Sync,
N: Send + Sync,
impl<N: ?Sized, H> Unpin for HashRing<N, H>
impl<N: ?Sized, H> UnwindSafe for HashRing<N, H> where
H: RefUnwindSafe,
N: RefUnwindSafe,
H: RefUnwindSafe,
N: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Pointable for T
[src]
pub const ALIGN: usize
[src]
type Init = T
The type for initializers.
pub unsafe fn init(init: <T as Pointable>::Init) -> usize
[src]
pub unsafe fn deref<'a>(ptr: usize) -> &'a T
[src]
pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T
[src]
pub unsafe fn drop(ptr: usize)
[src]
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,