pub struct BlockFragmentStore { /* private fields */ }Expand description
Production-grade store for fragmented blocks.
Handles concurrent fragment arrival, checksum verification, automatic assembly on completion, LRU-style eviction of the oldest pending sets when the cap is exceeded, and a bounded assembled-block cache.
Implementations§
Source§impl BlockFragmentStore
impl BlockFragmentStore
Sourcepub fn new(max_pending: usize, max_assembled: usize) -> Self
pub fn new(max_pending: usize, max_assembled: usize) -> Self
Create a new store with the given capacity limits.
max_pending– maximum number of in-progress fragment sets.max_assembled– maximum number of assembled blocks kept in cache.
Sourcepub fn store_fragment(
&mut self,
fragment: BfsFragment,
now: u64,
) -> Result<FragmentSetState, FragmentError>
pub fn store_fragment( &mut self, fragment: BfsFragment, now: u64, ) -> Result<FragmentSetState, FragmentError>
Store a fragment.
If the fragment completes its set, assembly is attempted automatically
and the pending set is removed. If max_pending would be exceeded when
creating a new set, the oldest pending set (by created_at) is dropped.
Returns the current FragmentSetState after the operation.
Sourcepub fn get_state(&self, block_cid: &str) -> Option<FragmentSetState>
pub fn get_state(&self, block_cid: &str) -> Option<FragmentSetState>
Return the current state of a fragment set.
Returns None if neither a pending set nor an assembled block exists for
the given CID.
Sourcepub fn missing_indices(&self, block_cid: &str) -> Vec<u32>
pub fn missing_indices(&self, block_cid: &str) -> Vec<u32>
Return the indices of fragments that have not yet been received, in ascending order. Returns an empty vec if the block is assembled or unknown.
Sourcepub fn assemble(
&mut self,
block_cid: &str,
now: u64,
) -> Result<AssembledBlock, FragmentError>
pub fn assemble( &mut self, block_cid: &str, now: u64, ) -> Result<AssembledBlock, FragmentError>
Assemble a block from its fragments.
All fragments must be present and pass checksum verification. On success, the assembled block is stored in the cache and the pending set is removed.
Sourcepub fn get_assembled(&self, block_cid: &str) -> Option<&[u8]>
pub fn get_assembled(&self, block_cid: &str) -> Option<&[u8]>
Return a reference to the data of an assembled block, or None if not
present in the cache.
Sourcepub fn get_assembled_block(&self, block_cid: &str) -> Option<&AssembledBlock>
pub fn get_assembled_block(&self, block_cid: &str) -> Option<&AssembledBlock>
Return a reference to the full AssembledBlock struct, if cached.
Sourcepub fn verify_fragment(fragment: &BfsFragment) -> bool
pub fn verify_fragment(fragment: &BfsFragment) -> bool
Return true if the fragment’s checksum matches the recomputed value.
Sourcepub fn verify_set(set: &FragmentSet) -> Vec<u32>
pub fn verify_set(set: &FragmentSet) -> Vec<u32>
Return the indices of fragments in a set whose checksums do not match.
Sourcepub fn evict_stale_pending(&mut self, max_age_ms: u64, now: u64) -> usize
pub fn evict_stale_pending(&mut self, max_age_ms: u64, now: u64) -> usize
Remove all pending (incomplete) sets whose created_at is older than
max_age_ms milliseconds before now. Returns the number of sets
removed.
Sourcepub fn evict_assembled(&mut self, cid: &str) -> bool
pub fn evict_assembled(&mut self, cid: &str) -> bool
Remove an assembled block from the cache by CID. Returns true if the
block was present and removed.
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Number of pending (incomplete) fragment sets.
Sourcepub fn assembled_count(&self) -> usize
pub fn assembled_count(&self) -> usize
Number of assembled blocks in the cache.
Sourcepub fn stats(&self) -> FragmentStats
pub fn stats(&self) -> FragmentStats
Aggregate statistics about the store.
Sourcepub fn pending_cids(&self) -> Vec<&str>
pub fn pending_cids(&self) -> Vec<&str>
Return all pending fragment set CIDs.
Sourcepub fn assembled_cids(&self) -> Vec<&str>
pub fn assembled_cids(&self) -> Vec<&str>
Return all assembled block CIDs.
Sourcepub fn clear_pending(&mut self) -> usize
pub fn clear_pending(&mut self) -> usize
Clear all pending sets without assembling them. Returns the number of sets dropped.
Sourcepub fn clear_assembled(&mut self) -> usize
pub fn clear_assembled(&mut self) -> usize
Clear all assembled blocks from the cache. Returns the number of blocks dropped.
Sourcepub fn pending_capacity_remaining(&self) -> usize
pub fn pending_capacity_remaining(&self) -> usize
Return how many more pending sets can be accepted before the cap is hit.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for BlockFragmentStore
impl RefUnwindSafe for BlockFragmentStore
impl Send for BlockFragmentStore
impl Sync for BlockFragmentStore
impl Unpin for BlockFragmentStore
impl UnsafeUnpin for BlockFragmentStore
impl UnwindSafe for BlockFragmentStore
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