pub struct BlockStoreSharding {
pub config: ShardingConfig,
pub total_insertions: u64,
pub total_lookups: u64,
pub total_bytes: u64,
/* private fields */
}Expand description
A sharded, content-addressed block store.
Distributes blocks across num_shards logical shards using FNV-1a hashing
of the CID. Provides per-shard metrics, rebalancing, and LRU eviction.
Fields§
§config: ShardingConfigRuntime configuration.
total_insertions: u64Total insertions since creation (new + replaced).
total_lookups: u64Total get calls since creation.
total_bytes: u64Total bytes currently stored.
Implementations§
Source§impl BlockStoreSharding
impl BlockStoreSharding
Sourcepub fn new(config: ShardingConfig) -> Self
pub fn new(config: ShardingConfig) -> Self
Creates a new BlockStoreSharding with the given configuration.
Panics if config.num_shards == 0.
Sourcepub fn shard_for_cid(&self, cid: &str) -> ShardKey
pub fn shard_for_cid(&self, cid: &str) -> ShardKey
Deterministically maps a CID string to a shard using FNV-1a % num_shards.
Sourcepub fn put(
&mut self,
cid: String,
data: Vec<u8>,
now: u64,
) -> Result<bool, ShardingError>
pub fn put( &mut self, cid: String, data: Vec<u8>, now: u64, ) -> Result<bool, ShardingError>
Inserts or replaces a block.
Returns Ok(true) if the block was newly inserted, Ok(false) if an
existing block for the same CID was replaced.
Returns Err(ShardingError::ShardFull) if the shard is at capacity and
there is no existing entry for the CID to replace.
Sourcepub fn get(&mut self, cid: &str) -> Option<&BlockRecord>
pub fn get(&mut self, cid: &str) -> Option<&BlockRecord>
Looks up a block by CID.
Updates the shard’s hit_count / miss_count and, on hit, increments
the record’s access_count. Returns None if not found.
Sourcepub fn get_mut(&mut self, cid: &str) -> Option<&mut BlockRecord>
pub fn get_mut(&mut self, cid: &str) -> Option<&mut BlockRecord>
Returns a mutable reference to a block record (no metrics update).
Sourcepub fn remove(&mut self, cid: &str) -> bool
pub fn remove(&mut self, cid: &str) -> bool
Removes a block by CID.
Returns true if the block existed and was removed, false otherwise.
Sourcepub fn shard_metrics(&self, shard: ShardKey) -> Option<&ShardMetrics>
pub fn shard_metrics(&self, shard: ShardKey) -> Option<&ShardMetrics>
Returns metrics for a specific shard, or None if the shard id is
out of range.
Sourcepub fn all_shard_metrics(&self) -> &[ShardMetrics]
pub fn all_shard_metrics(&self) -> &[ShardMetrics]
Returns a slice of per-shard metrics.
Sourcepub fn needs_rebalance(&self) -> bool
pub fn needs_rebalance(&self) -> bool
Returns true when at least one shard exceeds avg * (1 + threshold).
Returns false when no blocks are stored.
Sourcepub fn rebalance(&mut self, now: u64) -> usize
pub fn rebalance(&mut self, now: u64) -> usize
Rebalances the store by moving blocks from over-loaded shards to under-loaded shards.
For each shard that exceeds the average by more than the threshold,
the function removes the surplus blocks (those with the lowest
access_count) and re-inserts them using their natural shard_for_cid
routing. This is a no-op if the shard they naturally belong to has not
changed (e.g. due to the same num_shards); however the inserted_at
timestamp is refreshed to now.
Returns the total number of blocks moved.
Sourcepub fn evict_lru(&mut self, shard: ShardKey, count: usize) -> usize
pub fn evict_lru(&mut self, shard: ShardKey, count: usize) -> usize
Evicts up to count blocks from the given shard, preferring blocks
with the lowest access_count (LRU approximation).
Returns the number of blocks actually evicted (may be less than count
if the shard has fewer blocks).
Sourcepub fn cids_in_shard(&self, shard: ShardKey) -> Vec<&str>
pub fn cids_in_shard(&self, shard: ShardKey) -> Vec<&str>
Returns all CIDs stored in the given shard.
Sourcepub fn total_block_count(&self) -> usize
pub fn total_block_count(&self) -> usize
Returns the total number of blocks across all shards.
Sourcepub fn total_bytes(&self) -> u64
pub fn total_bytes(&self) -> u64
Returns the total bytes stored across all shards.
Sourcepub fn global_stats(&self) -> BlockStoreGlobalStats
pub fn global_stats(&self) -> BlockStoreGlobalStats
Returns a snapshot of global statistics.
Auto Trait Implementations§
impl Freeze for BlockStoreSharding
impl RefUnwindSafe for BlockStoreSharding
impl Send for BlockStoreSharding
impl Sync for BlockStoreSharding
impl Unpin for BlockStoreSharding
impl UnsafeUnpin for BlockStoreSharding
impl UnwindSafe for BlockStoreSharding
Blanket Implementations§
impl<T> Allocation for T
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