pub struct RoutingTableSharding {
pub config: ShardConfig,
pub shards: Vec<HashMap<NodeId, RoutingEntry>>,
pub total_insertions: u64,
pub total_evictions: u64,
pub total_lookups: Cell<u64>,
/* private fields */
}Expand description
Sharded DHT routing table.
Peers are placed into shards by node_id.0[0] % num_shards, giving an
approximately uniform distribution for random node IDs while preserving the
XOR-metric locality needed for closest-peer queries.
Fields§
§config: ShardConfigShard configuration (immutable after construction).
shards: Vec<HashMap<NodeId, RoutingEntry>>One HashMap per shard.
total_insertions: u64Monotonically increasing count of successful insert calls.
total_evictions: u64Monotonically increasing count of evictions caused by capacity enforcement.
total_lookups: Cell<u64>Monotonically increasing count of get / closest_nodes calls.
Uses Cell for interior-mutability so that read-only methods
(get, closest_nodes) can increment the counter without requiring
&mut self.
Implementations§
Source§impl RoutingTableSharding
impl RoutingTableSharding
Sourcepub fn new(config: ShardConfig) -> Self
pub fn new(config: ShardConfig) -> Self
Construct a new routing table with config.num_shards empty shards.
Sourcepub fn insert(&mut self, entry: RoutingEntry, _now: u64) -> bool
pub fn insert(&mut self, entry: RoutingEntry, _now: u64) -> bool
Insert entry into the appropriate shard.
If the shard is already at capacity after the insertion, one entry is
evicted according to config.eviction_policy.
Returns true if an eviction occurred, false otherwise.
Sourcepub fn remove(&mut self, node_id: &NodeId) -> bool
pub fn remove(&mut self, node_id: &NodeId) -> bool
Remove the entry for node_id. Returns true if an entry existed.
Sourcepub fn get(&self, node_id: &NodeId) -> Option<&RoutingEntry>
pub fn get(&self, node_id: &NodeId) -> Option<&RoutingEntry>
Look up a single entry by NodeId.
Sourcepub fn update_rtt(&mut self, node_id: &NodeId, rtt_ms: u32, now: u64) -> bool
pub fn update_rtt(&mut self, node_id: &NodeId, rtt_ms: u32, now: u64) -> bool
Update the RTT and last_seen timestamp for an existing entry.
Returns true if the entry was found and updated.
Sourcepub fn closest_nodes(&self, target: &NodeId, k: usize) -> Vec<&RoutingEntry>
pub fn closest_nodes(&self, target: &NodeId, k: usize) -> Vec<&RoutingEntry>
Return the k entries whose XOR distance to target is smallest,
sorted ascending by distance (closest first).
Scans all shards; O(N log k) where N is the total number of entries.
Sourcepub fn nodes_in_shard(&self, shard: ShardId) -> Vec<&RoutingEntry>
pub fn nodes_in_shard(&self, shard: ShardId) -> Vec<&RoutingEntry>
Return all entries in the specified shard.
Sourcepub fn evict_stale(&mut self, now: u64, max_age_ms: u64) -> usize
pub fn evict_stale(&mut self, now: u64, max_age_ms: u64) -> usize
Remove all entries where now.saturating_sub(last_seen) > max_age_ms.
Returns the number of entries removed.
Sourcepub fn shard_stats(&self, shard: ShardId, now: u64) -> ShardStats
pub fn shard_stats(&self, shard: ShardId, now: u64) -> ShardStats
Statistics for a single shard at time now.
Sourcepub fn all_stats(&self, now: u64) -> Vec<ShardStats>
pub fn all_stats(&self, now: u64) -> Vec<ShardStats>
Statistics for all shards, sorted ascending by shard_id.
Sourcepub fn total_entries(&self) -> usize
pub fn total_entries(&self) -> usize
Total number of entries across all shards.
Sourcepub fn global_stats(&self) -> (u64, u64, u64)
pub fn global_stats(&self) -> (u64, u64, u64)
Global counters: (total_insertions, total_evictions, total_lookups).
Auto Trait Implementations§
impl !Freeze for RoutingTableSharding
impl !RefUnwindSafe for RoutingTableSharding
impl !Sync for RoutingTableSharding
impl Send for RoutingTableSharding
impl Unpin for RoutingTableSharding
impl UnsafeUnpin for RoutingTableSharding
impl UnwindSafe for RoutingTableSharding
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more