Skip to main content

Ring

Struct Ring 

Source
pub struct Ring<'a, T, H = RandomState> { /* private fields */ }
Expand description

A hashing ring implemented using multi-probe consistent hashing.

Multi-probe consistent hashing is a variation on consistent hashing where instead of the nodes being hashed multiple times to reduce variance, the keys are hashed multiple times. Each key is hashed hash_count times and the closest node over all hashes is returned.

§Examples

use hash_rings::mpc::Ring;
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;

type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;

let mut ring = Ring::with_hasher(DefaultBuildHasher::default(), 2);

ring.insert_node(&"node-1");
ring.insert_node(&"node-2");

ring.remove_node(&"node-1");

assert_eq!(ring.get_node(&"point-1"), &"node-2");
assert_eq!(ring.len(), 1);

let mut iterator = ring.iter();
assert_eq!(iterator.next(), Some(&"node-2"));
assert_eq!(iterator.next(), None);

Implementations§

Source§

impl<'a, T> Ring<'a, T, RandomState>

Source

pub fn new(hash_count: u64) -> Self

Constructs a new, empty Ring<T> that hashes hash_count times when a key is inserted.

§Examples
use hash_rings::mpc::Ring;

let mut ring: Ring<&str> = Ring::new(2);
Source§

impl<'a, T, H> Ring<'a, T, H>

Source

pub fn with_hasher(hash_builder: H, hash_count: u64) -> Self

Constructs a new, empty Ring<T> that hashes hash_count times when a key is inserted with a specified hash builder.

§Examples
use hash_rings::mpc::Ring;
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;

type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;

let mut ring: Ring<&str, _> = Ring::with_hasher(DefaultBuildHasher::default(), 2);
Source

pub fn insert_node(&mut self, id: &'a T)
where T: Hash, H: BuildHasher,

Inserts a node into the ring with a number of replicas.

Increasing the number of replicas will increase the number of expected points mapped to the node. For example, a node with three replicas will receive approximately three times more points than a node with one replica.

§Examples
use hash_rings::mpc::Ring;

let mut ring: Ring<&str> = Ring::new(2);
ring.insert_node(&"node-1");
Source

pub fn remove_node(&mut self, id: &T)
where T: Hash, H: BuildHasher,

Removes a node.

§Examples
use hash_rings::mpc::Ring;

let mut ring: Ring<&str> = Ring::new(2);

ring.insert_node(&"node-1");
ring.remove_node(&"node-1");
Source

pub fn get_node<U>(&self, point: &U) -> &T
where U: Hash,

Returns the node associated with a point.

§Panics

Panics if the ring is empty.

§Examples
use hash_rings::mpc::Ring;

let mut ring: Ring<&str> = Ring::new(2);

ring.insert_node(&"node-1");
assert_eq!(ring.get_node(&"point-1"), &"node-1");
Source

pub fn len(&self) -> usize

Returns the number of nodes in the ring.

§Examples
use hash_rings::mpc::Ring;

let mut ring: Ring<&str> = Ring::new(2);

ring.insert_node(&"node-1");
assert_eq!(ring.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the ring is empty.

§Examples
use hash_rings::mpc::Ring;

let mut ring: Ring<&str> = Ring::new(2);

assert!(ring.is_empty());
ring.insert_node(&"node-1");
assert!(!ring.is_empty());
Source

pub fn iter(&'a self) -> impl Iterator<Item = &'a T>

Returns an iterator over the ring. The iterator will yield the nodes in the ring in no particular order.

§Examples
use hash_rings::mpc::Ring;

let mut ring = Ring::new(2);
ring.insert_node(&"node-1");

let mut iterator = ring.iter();
assert_eq!(iterator.next(), Some(&"node-1"));
assert_eq!(iterator.next(), None);

Trait Implementations§

Source§

impl<'a, T, H> IntoIterator for &'a Ring<'a, T, H>
where T: Hash + Eq,

Source§

type IntoIter = Box<dyn Iterator<Item = &'a T> + 'a>

Which kind of iterator are we turning this into?
Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<'a, T, H> Freeze for Ring<'a, T, H>
where H: Freeze,

§

impl<'a, T, H> RefUnwindSafe for Ring<'a, T, H>

§

impl<'a, T, H> Send for Ring<'a, T, H>
where H: Send, T: Sync,

§

impl<'a, T, H> Sync for Ring<'a, T, H>
where H: Sync, T: Sync,

§

impl<'a, T, H> Unpin for Ring<'a, T, H>
where H: Unpin,

§

impl<'a, T, H> UnsafeUnpin for Ring<'a, T, H>
where H: UnsafeUnpin,

§

impl<'a, T, H> UnwindSafe for Ring<'a, T, H>

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.