Struct lfchring::HashRing[][src]

pub struct HashRing<N: ?Sized, H> where
    N: Node,
    H: Hasher
{ /* fields omitted */ }

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]

pub fn with_nodes(
    vnodes_per_node: Vnid,
    replication_factor: u8,
    nodes: &[Arc<N>]
) -> 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) and initialize it with the provided Nodes (the ring will be populated by their VirtualNodes automatically).

The new HashRing<N, H> will employ the built-in Hasher that is based on standard library’s DefaultHasher.

Errors

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 Nodes (and, of course, empty of VirtualNodes 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]

pub fn with_hasher_and_nodes(
    hasher: H,
    vnodes_per_node: Vnid,
    replication_factor: u8,
    nodes: &[Arc<N>]
) -> 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) and initialize it with the provided Nodes (the ring will be populated by their VirtualNodes automatically).

The new HashRing<N, H> will employ the provided Hasher for placing the VirtualNodes on the consistent hashing ring.

Errors

pub fn with_hasher(
    hasher: H,
    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 Nodes (and, of course, empty of VirtualNodes too).

The new HashRing<N, H> will employ the provided Hasher for placing the VirtualNodes 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 Nodes 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 Nodes in the consistent hashing ring. This can happen if:

pub fn remove(&self, nodes: &[Arc<N>]) -> Result<()>[src]

Remove the given Nodes from the consistent hashing ring, thereby shrinking it.

Errors

HashRingError::VirtualNodeDoesNotExist is returned in the case that one of the Nodes 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]

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]

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 Nodes 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]

Look up in the consistent hashing ring and return a Vec of Nodes, each wrapped in an Arc, on which a replica of the given key should be assigned on.

The Nodes 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 Nodes 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]

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 Nodes 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]

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 Nodes 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]

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

pub fn successor_node<K>(&self, key: &K) -> Result<VirtualNode<N>> where
    K: Borrow<[u8]>, 
[src]

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

pub fn iter<'guard>(&self, guard: &'guard Guard) -> Iter<'guard, N, H>

Notable traits for Iter<'guard, N, H>

impl<'guard, N: ?Sized, H> Iterator for Iter<'guard, N, H> where
    N: Node,
    H: Hasher
type Item = &'guard VirtualNode<N>;
[src]

Returns an Iter, i.e., an iterator to loop through all VirtualNodes 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]

impl<N: Debug + ?Sized, H: Debug> Debug for HashRing<N, H> where
    N: Node,
    H: Hasher
[src]

impl<N: ?Sized, H> Display for HashRing<N, H> where
    N: Node,
    H: Hasher
[src]

impl<N: ?Sized, H> Extend<Arc<N>> for HashRing<N, H> where
    N: Node,
    H: Hasher
[src]

fn extend<I: IntoIterator<Item = Arc<N>>>(&mut self, iter: I)[src]

Extend the HashRing<N, H> by the Nodes 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 Nodes 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 Nodes occur).

Auto Trait Implementations

impl<N: ?Sized, H> RefUnwindSafe for HashRing<N, H> where
    H: RefUnwindSafe,
    N: RefUnwindSafe

impl<N: ?Sized, H> Send for HashRing<N, H> where
    H: Send + Sync,
    N: Send + Sync

impl<N: ?Sized, H> Sync for HashRing<N, H> where
    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

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pointable for T[src]

type Init = T

The type for initializers.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.