pub struct BlockGarbageCollector { /* private fields */ }Expand description
Production-grade block-level garbage collector.
Supports mark-and-sweep, reference counting, tri-color, and generational policies. All operations run in O(V + E) where V = blocks, E = DAG edges.
Implementations§
Source§impl BlockGarbageCollector
impl BlockGarbageCollector
Sourcepub fn new(config: BgcCollectorConfig) -> Self
pub fn new(config: BgcCollectorConfig) -> Self
Create a new collector with the given configuration.
Source§impl BlockGarbageCollector
impl BlockGarbageCollector
Sourcepub fn register_block(
&mut self,
cid: BgcBlockCid,
size_bytes: u64,
refs: Vec<BgcBlockCid>,
) -> Result<(), BgcError>
pub fn register_block( &mut self, cid: BgcBlockCid, size_bytes: u64, refs: Vec<BgcBlockCid>, ) -> Result<(), BgcError>
Register a new block.
refs lists the CIDs this block holds direct references to (children).
Sourcepub fn unregister_block(&mut self, cid: BgcBlockCid) -> Result<(), BgcError>
pub fn unregister_block(&mut self, cid: BgcBlockCid) -> Result<(), BgcError>
Unregister a block, removing it from the registry and edge map.
Returns Err(BlockIsPinned) if the block is explicitly pinned.
Sourcepub fn touch(&mut self, cid: &BgcBlockCid) -> Result<(), BgcError>
pub fn touch(&mut self, cid: &BgcBlockCid) -> Result<(), BgcError>
Touch a block’s last_accessed timestamp.
Sourcepub fn get_block(&self, cid: &BgcBlockCid) -> Option<&BgcBlockRecord>
pub fn get_block(&self, cid: &BgcBlockCid) -> Option<&BgcBlockRecord>
Return an immutable reference to a block record.
Source§impl BlockGarbageCollector
impl BlockGarbageCollector
Sourcepub fn pin(&mut self, cid: BgcBlockCid) -> Result<(), BgcError>
pub fn pin(&mut self, cid: BgcBlockCid) -> Result<(), BgcError>
Explicitly pin a block so it is never collected.
Sourcepub fn unpin(&mut self, cid: BgcBlockCid) -> Result<(), BgcError>
pub fn unpin(&mut self, cid: BgcBlockCid) -> Result<(), BgcError>
Remove the explicit pin from a block.
Sourcepub fn add_root(&mut self, cid: BgcBlockCid)
pub fn add_root(&mut self, cid: BgcBlockCid)
Add a CID to the GC root set (always considered live).
Sourcepub fn remove_root(&mut self, cid: &BgcBlockCid)
pub fn remove_root(&mut self, cid: &BgcBlockCid)
Remove a CID from the GC root set.
Sourcepub fn is_pinned(&self, cid: &BgcBlockCid) -> bool
pub fn is_pinned(&self, cid: &BgcBlockCid) -> bool
Return true if the block is currently pinned.
Sourcepub fn is_root(&self, cid: &BgcBlockCid) -> bool
pub fn is_root(&self, cid: &BgcBlockCid) -> bool
Return true if the block is a GC root.
Source§impl BlockGarbageCollector
impl BlockGarbageCollector
Sourcepub fn increment_ref(&mut self, cid: &BgcBlockCid) -> Result<(), BgcError>
pub fn increment_ref(&mut self, cid: &BgcBlockCid) -> Result<(), BgcError>
Increment the reference count of a block.
Sourcepub fn decrement_ref(&mut self, cid: &BgcBlockCid) -> Result<bool, BgcError>
pub fn decrement_ref(&mut self, cid: &BgcBlockCid) -> Result<bool, BgcError>
Decrement the reference count of a block.
Returns Ok(true) if the block is now at ref_count == 0 (candidate for
collection); Ok(false) otherwise.
Source§impl BlockGarbageCollector
impl BlockGarbageCollector
Sourcepub fn mark_phase(&self) -> BTreeSet<BgcBlockCid>
pub fn mark_phase(&self) -> BTreeSet<BgcBlockCid>
Mark phase: BFS from all roots and pinned blocks.
Returns the set of reachable (live) CIDs.
Source§impl BlockGarbageCollector
impl BlockGarbageCollector
Sourcepub fn sweep_phase(
&mut self,
reachable: &BTreeSet<BgcBlockCid>,
) -> BgcSweepResult
pub fn sweep_phase( &mut self, reachable: &BTreeSet<BgcBlockCid>, ) -> BgcSweepResult
Sweep phase: remove all blocks not in reachable.
Respects min_age_secs and dry_run from the config.
Source§impl BlockGarbageCollector
impl BlockGarbageCollector
Sourcepub fn run_gc(&mut self, policy: BgcGcPolicy) -> Result<BgcGcResult, BgcError>
pub fn run_gc(&mut self, policy: BgcGcPolicy) -> Result<BgcGcResult, BgcError>
Run a complete GC cycle using the given policy.
Source§impl BlockGarbageCollector
impl BlockGarbageCollector
Sourcepub fn collect_orphans(&self, min_age_secs: u64) -> Vec<BgcBlockCid> ⓘ
pub fn collect_orphans(&self, min_age_secs: u64) -> Vec<BgcBlockCid> ⓘ
Return all CIDs that are unreachable, not pinned, and older than
min_age_secs. Does not remove them.
Sourcepub fn collect_orphans_default(&self) -> Vec<BgcBlockCid> ⓘ
pub fn collect_orphans_default(&self) -> Vec<BgcBlockCid> ⓘ
Collect orphans using the internal min_age_secs from config.
Source§impl BlockGarbageCollector
impl BlockGarbageCollector
Sourcepub fn gc_stats(&self) -> BgcCollectorStats
pub fn gc_stats(&self) -> BgcCollectorStats
Compute current collector statistics.
Sourcepub fn block_count(&self) -> usize
pub fn block_count(&self) -> usize
Return the number of registered blocks.
Sourcepub fn log_entries(&self) -> impl Iterator<Item = &BgcGcLogEntry>
pub fn log_entries(&self) -> impl Iterator<Item = &BgcGcLogEntry>
Iterate over log entries (most recent last).
Sourcepub fn gc_log(&self) -> &VecDeque<BgcGcLogEntry>
pub fn gc_log(&self) -> &VecDeque<BgcGcLogEntry>
Return the full GC log as a slice-like iterator.
Source§impl BlockGarbageCollector
impl BlockGarbageCollector
Sourcepub fn random_cid(&mut self) -> BgcBlockCid
pub fn random_cid(&mut self) -> BgcBlockCid
Generate a pseudo-random CID for testing (uses xorshift).
Sourcepub fn cid_from_bytes(data: &[u8]) -> BgcBlockCid
pub fn cid_from_bytes(data: &[u8]) -> BgcBlockCid
Compute a deterministic CID from raw bytes (used in tests).
Sourcepub fn config(&self) -> &BgcCollectorConfig
pub fn config(&self) -> &BgcCollectorConfig
Return the config (read-only).
Sourcepub fn config_mut(&mut self) -> &mut BgcCollectorConfig
pub fn config_mut(&mut self) -> &mut BgcCollectorConfig
Mutate the config (e.g. flip dry_run in tests).
Sourcepub fn update_edges(
&mut self,
cid: BgcBlockCid,
new_refs: Vec<BgcBlockCid>,
) -> Result<(), BgcError>
pub fn update_edges( &mut self, cid: BgcBlockCid, new_refs: Vec<BgcBlockCid>, ) -> Result<(), BgcError>
Update edges for an already-registered block.
Sourcepub fn children_of(&self, cid: &BgcBlockCid) -> &[BgcBlockCid] ⓘ
pub fn children_of(&self, cid: &BgcBlockCid) -> &[BgcBlockCid] ⓘ
Return children of a block (direct DAG edges).
Sourcepub fn reconcile_pin_flags(&mut self)
pub fn reconcile_pin_flags(&mut self)
Set the is_pinned flag to match the pin set (reconcile).
Sourcepub fn promote_generation(&mut self)
pub fn promote_generation(&mut self)
Promote all blocks in generation 0 to generation 1.
Sourcepub fn compact_edges(&mut self)
pub fn compact_edges(&mut self)
Compact: remove all edge entries for blocks that no longer exist.
Sourcepub fn all_cids(&self) -> Vec<BgcBlockCid> ⓘ
pub fn all_cids(&self) -> Vec<BgcBlockCid> ⓘ
Return the set of all registered CIDs.
Sourcepub fn reachable_set(&self) -> BTreeSet<BgcBlockCid>
pub fn reachable_set(&self) -> BTreeSet<BgcBlockCid>
Return the reachable set (live blocks) without modifying state.
Auto Trait Implementations§
impl Freeze for BlockGarbageCollector
impl RefUnwindSafe for BlockGarbageCollector
impl Send for BlockGarbageCollector
impl Sync for BlockGarbageCollector
impl Unpin for BlockGarbageCollector
impl UnsafeUnpin for BlockGarbageCollector
impl UnwindSafe for BlockGarbageCollector
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