HashRing

Struct HashRing 

Source
pub struct HashRing<N, B> { /* private fields */ }
Expand description

A hash ring for consistent hashing.

§Examples

use hulahoop::HashRing;
let mut ring: HashRing<&str, _> = HashRing::new();

ring.insert("10.0.0.1:1234", 1);
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));

Implementations§

Source§

impl<N> HashRing<N, BuildHasherDefault<DefaultHasher>>

Source

pub fn new() -> Self

Creates a new HashRing with the default hasher.

§Examples
use hulahoop::HashRing;

let mut ring: HashRing<&str, _> = HashRing::new();

ring.insert("10.0.0.1:1234", 1);
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));
Source§

impl<N, B> HashRing<N, B>
where N: Hash, B: BuildHasher,

Source

pub fn with_hasher(hash_builder: B) -> Self

Creates an empty HashRing which will use the given hash_builder to hash nodes and keys.

§Examples
use std::hash::BuildHasherDefault;
use rustc_hash::FxHasher;
use hulahoop::HashRing;

let mut ring: HashRing<&str, BuildHasherDefault<FxHasher>> = HashRing::with_hasher(BuildHasherDefault::<FxHasher>::default());

ring.insert("10.0.0.1:1234", 1);
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));
Source

pub fn hasher(&self) -> &B

Returns a reference to the ring’s BuildHasher.

§Examples
use hulahoop::HashRing;
use std::collections::hash_map::RandomState;

let hasher = RandomState::new();
let ring: HashRing<&str, _> = HashRing::with_hasher(hasher);
let hasher: &RandomState = ring.hasher();
Source

pub fn insert(&mut self, node: N, weight: u64) -> Option<N>

Inserts a node to the HashRing.

A weight, representing the number of virtual nodes for the given node, must be provided.

There can be hash collisions resulting in fewer than weight virtual nodes added. If the ring did not have this node present or weight is 0, None is returned. If the ring did have this node present, the virtual nodes are updated, and the old node is returned.

§Examples
use hulahoop::HashRing;

let mut ring: HashRing<&str, _> = HashRing::default();

assert_eq!(ring.insert("10.0.0.1:1234", 1), None);
assert_eq!(ring.insert("10.0.0.1:1234", 1), Some("10.0.0.1:1234"));
Source

pub fn get<K>(&self, key: K) -> Option<&N>
where K: Hash,

Returns a reference to the node with a hash closest to the hash of the key.

§Examples
use hulahoop::HashRing;

let mut ring: HashRing<&str, _> = HashRing::default();

ring.insert("10.0.0.1:1234", 1);
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));
assert_eq!(ring.get(12345), Some(&"10.0.0.1:1234"));
Source

pub fn len(&self) -> usize

Returns the number of nodes in the Hashring.

It does not return the number of virtual nodes (as specified with weight in the insert method).

§Examples
use hulahoop::HashRing;

let mut ring: HashRing<&str, _> = HashRing::default();

ring.insert("10.0.0.1:1234", 10);
ring.insert("10.0.0.2:1234", 10);
assert_eq!(ring.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the ring contains no elements.

§Examples
use hulahoop::HashRing;

let mut ring: HashRing<&str, _> = HashRing::default();
assert!(ring.is_empty());

ring.insert("10.0.0.1:1234", 10);
assert!(!ring.is_empty());
Source

pub fn contains_node(&self, node: &N) -> bool

Returns true if the ring contains the specified node.

§Examples
use hulahoop::HashRing;

let mut ring: HashRing<&str, _> = HashRing::default();

ring.insert("10.0.0.1:1234", 10);
assert_eq!(ring.contains_node(&"10.0.0.1:1234"), true);
assert_eq!(ring.contains_node(&"10.0.0.2:1234"), false);
Source

pub fn iter(&self) -> Iter<'_, N>

An iterator visiting all node-weight pairs in arbitrary order. The iterator element type is (&'a N, u64).

The weight is the actual number of virtual nodes. It may be lower than the weight provided when inserting a node in case of hash collisions.

§Examples
use hulahoop::HashRing;

let mut ring: HashRing<&str, _> = HashRing::default();

ring.insert("10.0.0.1:1234", 10);
ring.insert("10.0.0.2:1234", 5);

for (node, weight) in ring.iter() {
    println!("node: {node} weight: {weight}");
}
Source

pub fn remove(&mut self, node: &N) -> u64

Removes a node from the HashRing, returning the number of virtual nodes (weight) of the removed node.

The returned number is the actual number of virtual nodes. It may be lower than the weight provided when inserting a node in case of hash collisions.

§Examples
use hulahoop::HashRing;

let mut ring: HashRing<&str, _> = HashRing::default();

ring.insert("10.0.0.1:1234", 10);
assert_eq!(ring.remove(&"10.0.0.1:1234"), 10);
assert_eq!(ring.remove(&"10.0.0.1:1234"), 0);

Trait Implementations§

Source§

impl<N: Debug, B: Debug> Debug for HashRing<N, B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<N> Default for HashRing<N, BuildHasherDefault<DefaultHasher>>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<N, B> Freeze for HashRing<N, B>
where B: Freeze,

§

impl<N, B> RefUnwindSafe for HashRing<N, B>

§

impl<N, B> Send for HashRing<N, B>
where B: Send, N: Sync + Send,

§

impl<N, B> Sync for HashRing<N, B>
where B: Sync, N: Sync + Send,

§

impl<N, B> Unpin for HashRing<N, B>
where B: Unpin,

§

impl<N, B> UnwindSafe for HashRing<N, B>

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.