Skip to main content

MaglevTable

Struct MaglevTable 

Source
pub struct MaglevTable<N> { /* private fields */ }
Expand description

Maglev consistent hashing table.

Generic over the node type N which must be Hash + Clone + Eq. The table provides O(1) primary lookups and preference-list queries for replica-aware routing.

Implementations§

Source§

impl<N: Hash + Clone + Eq> MaglevTable<N>

Source

pub fn new(nodes: Vec<N>) -> Self

Build a new Maglev table with default capacity (nodes.len() * 100, rounded up to the nearest prime).

Source

pub fn with_capacity(nodes: Vec<N>, min_capacity: usize) -> Self

Build a new Maglev table with the given minimum capacity.

The actual table size will be the smallest prime ≥ min_capacity. When comparing tables across node set changes (e.g., measuring disruption), use the same capacity for both to ensure keys hash to the same slots.

Source

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

O(1) lookup of the primary node for a key.

Source

pub fn get_top_k<K: Hash>(&self, key: &K, k: usize) -> Vec<&N>

Return up to k distinct nodes in preference order for the given key.

The first element always matches get(key). Preference order is determined by walking the lookup table from the hashed slot and collecting unique nodes encountered. This ensures consistency with the primary lookup.

Source

pub fn is_match<K: Hash>(&self, key: &K, node: &N, top_k: usize) -> bool

Returns true if node appears in the top-top_k entries of the preference list for key.

Note: when top_k >= self.len() the result is always true (every node appears exactly once in the full preference list). This method is most useful with small top_k values (e.g., checking if a node is one of the top-3 replicas for a key).

Source

pub fn nodes(&self) -> &[N]

Return all nodes in the table.

Source

pub fn capacity(&self) -> usize

Return the table size (the prime M).

Source

pub fn len(&self) -> usize

Return the number of nodes.

Source

pub fn is_empty(&self) -> bool

Return whether the table has no nodes.

Auto Trait Implementations§

§

impl<N> Freeze for MaglevTable<N>

§

impl<N> RefUnwindSafe for MaglevTable<N>
where N: RefUnwindSafe,

§

impl<N> Send for MaglevTable<N>
where N: Send,

§

impl<N> Sync for MaglevTable<N>
where N: Sync,

§

impl<N> Unpin for MaglevTable<N>
where N: Unpin,

§

impl<N> UnsafeUnpin for MaglevTable<N>

§

impl<N> UnwindSafe for MaglevTable<N>
where N: UnwindSafe,

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.