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>
impl<N: Hash + Clone + Eq> MaglevTable<N>
Sourcepub fn new(nodes: Vec<N>) -> Self
pub fn new(nodes: Vec<N>) -> Self
Build a new Maglev table with default capacity (nodes.len() * 100,
rounded up to the nearest prime).
Sourcepub fn with_capacity(nodes: Vec<N>, min_capacity: usize) -> Self
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.
Sourcepub fn get_top_k<K: Hash>(&self, key: &K, k: usize) -> Vec<&N>
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.
Sourcepub fn is_match<K: Hash>(&self, key: &K, node: &N, top_k: usize) -> bool
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).