pub struct ConsistentHashRing { /* private fields */ }Expand description
A consistent hash ring for routing documents to shards.
The ring uses virtual nodes to achieve better load distribution across shards. Each physical shard is represented by multiple virtual nodes on the ring, which helps ensure that documents are distributed evenly.
§Thread Safety
The ring itself is Clone and can be wrapped in Arc<RwLock<_>> for
thread-safe access with dynamic updates, or Arc<_> for read-only
concurrent access.
§Example
use phago_distributed::hashing::ConsistentHashRing;
use phago_core::types::DocumentId;
let ring = ConsistentHashRing::new(3);
let doc_id = DocumentId::from_seed(42);
let shard = ring.get_shard(&doc_id);
println!("Document maps to shard: {}", shard);Implementations§
Source§impl ConsistentHashRing
impl ConsistentHashRing
Sourcepub fn with_virtual_nodes(num_shards: u32, virtual_nodes: u32) -> Self
pub fn with_virtual_nodes(num_shards: u32, virtual_nodes: u32) -> Self
Create a new hash ring with custom virtual nodes per shard.
More virtual nodes generally result in better distribution but increase memory usage and lookup time slightly.
§Arguments
num_shards- The number of physical shardsvirtual_nodes- Number of virtual nodes per shard
Sourcepub fn get_shard(&self, doc_id: &DocumentId) -> ShardId
pub fn get_shard(&self, doc_id: &DocumentId) -> ShardId
Sourcepub fn get_shard_for_key<K: Hash>(&self, key: &K) -> ShardId
pub fn get_shard_for_key<K: Hash>(&self, key: &K) -> ShardId
Get the shard ID for an arbitrary key.
This is useful for routing non-document data to shards.
§Arguments
key- Any hashable key
Sourcepub fn add_shard(&mut self, shard_id: ShardId)
pub fn add_shard(&mut self, shard_id: ShardId)
Add a new shard to the ring.
This will redistribute approximately 1 / (n+1) of the keys from
existing shards to the new shard, where n is the current number of shards.
§Arguments
shard_id- The shard ID to add
Sourcepub fn remove_shard(&mut self, shard_id: ShardId)
pub fn remove_shard(&mut self, shard_id: ShardId)
Remove a shard from the ring.
Documents previously assigned to this shard will be redistributed to the next shard in the ring (clockwise).
§Arguments
shard_id- The shard ID to remove
Sourcepub fn shard_count(&self) -> u32
pub fn shard_count(&self) -> u32
Get the number of shards.
Sourcepub fn all_shards(&self) -> Vec<ShardId>
pub fn all_shards(&self) -> Vec<ShardId>
Get all shard IDs in the ring.
Returns a sorted, deduplicated list of all shard IDs.
Sourcepub fn virtual_nodes_per_shard(&self) -> u32
pub fn virtual_nodes_per_shard(&self) -> u32
Get the number of virtual nodes per shard.
Sourcepub fn total_virtual_nodes(&self) -> usize
pub fn total_virtual_nodes(&self) -> usize
Get the total number of virtual nodes in the ring.
Sourcepub fn get_replica_shards(
&self,
doc_id: &DocumentId,
replica_count: usize,
) -> Vec<ShardId>
pub fn get_replica_shards( &self, doc_id: &DocumentId, replica_count: usize, ) -> Vec<ShardId>
Get replica shards for a document.
Returns the primary shard plus replica_count additional shards
that should store replicas of the document.
§Arguments
doc_id- The document IDreplica_count- Number of additional replicas (excluding primary)
§Returns
A vector of shard IDs, with the primary shard first.
Trait Implementations§
Source§impl Clone for ConsistentHashRing
impl Clone for ConsistentHashRing
Source§fn clone(&self) -> ConsistentHashRing
fn clone(&self) -> ConsistentHashRing
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more