pub struct Ring<H = RandomState> { /* private fields */ }Expand description
A hashing ring implemented using jump hashing.
Jump hashing is based on using a hash of the key as the seed for a random number generator and using it to jump forward in a list of nodes until it falls off the end. The last node it lands on is the result.
Jump hashing is very fast and executes in O(ln n) time. It also has no memory overhead and has
virtually perfect key distribution. However, the main limitation of jump hashing is that it
returns an integer in the range [0, nodes) and it does not support arbitrary node names.
§Examples
use hash_rings::jump::Ring;
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;
type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;
let ring = Ring::with_hasher(DefaultBuildHasher::default(), 100);
assert_eq!(ring.get_node(&"foo"), 8);
assert_eq!(ring.nodes(), 100);Implementations§
Source§impl Ring<RandomState>
impl Ring<RandomState>
Source§impl<H> Ring<H>
impl<H> Ring<H>
Sourcepub fn with_hasher(hash_builder: H, nodes: u32) -> Self
pub fn with_hasher(hash_builder: H, nodes: u32) -> Self
Constructs a new Ring with a specified number of nodes and hash builder.
§Panics
Panics if the number of nodes is zero.
§Examples
use hash_rings::jump::Ring;
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;
type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;
let ring: Ring<_> = Ring::with_hasher(DefaultBuildHasher::default(), 100);Sourcepub fn get_node<T>(&self, key: &T) -> u32where
T: Hash,
H: BuildHasher,
pub fn get_node<T>(&self, key: &T) -> u32where
T: Hash,
H: BuildHasher,
Returns the node associated with a key.
§Examples
use hash_rings::jump::Ring;
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;
type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;
let ring = Ring::with_hasher(DefaultBuildHasher::default(), 100);
assert_eq!(ring.get_node(&"foo"), 8);Auto Trait Implementations§
impl<H> Freeze for Ring<H>where
H: Freeze,
impl<H> RefUnwindSafe for Ring<H>where
H: RefUnwindSafe,
impl<H> Send for Ring<H>where
H: Send,
impl<H> Sync for Ring<H>where
H: Sync,
impl<H> Unpin for Ring<H>where
H: Unpin,
impl<H> UnsafeUnpin for Ring<H>where
H: UnsafeUnpin,
impl<H> UnwindSafe for Ring<H>where
H: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more