Skip to main content

BlockGarbageCollector

Struct BlockGarbageCollector 

Source
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

Source

pub fn set_clock(&mut self, secs: u64)

Override the internal clock (useful in tests).

Source

pub fn cid_hash(cid: &BgcBlockCid) -> u64

Derive a deterministic u64 from a CID for internal purposes.

Source§

impl BlockGarbageCollector

Source

pub fn new(config: BgcCollectorConfig) -> Self

Create a new collector with the given configuration.

Source§

impl BlockGarbageCollector

Source

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).

Source

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.

Source

pub fn touch(&mut self, cid: &BgcBlockCid) -> Result<(), BgcError>

Touch a block’s last_accessed timestamp.

Source

pub fn get_block(&self, cid: &BgcBlockCid) -> Option<&BgcBlockRecord>

Return an immutable reference to a block record.

Source§

impl BlockGarbageCollector

Source

pub fn pin(&mut self, cid: BgcBlockCid) -> Result<(), BgcError>

Explicitly pin a block so it is never collected.

Source

pub fn unpin(&mut self, cid: BgcBlockCid) -> Result<(), BgcError>

Remove the explicit pin from a block.

Source

pub fn add_root(&mut self, cid: BgcBlockCid)

Add a CID to the GC root set (always considered live).

Source

pub fn remove_root(&mut self, cid: &BgcBlockCid)

Remove a CID from the GC root set.

Source

pub fn is_pinned(&self, cid: &BgcBlockCid) -> bool

Return true if the block is currently pinned.

Source

pub fn is_root(&self, cid: &BgcBlockCid) -> bool

Return true if the block is a GC root.

Source§

impl BlockGarbageCollector

Source

pub fn increment_ref(&mut self, cid: &BgcBlockCid) -> Result<(), BgcError>

Increment the reference count of a block.

Source

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

Source

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

Source

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

Source

pub fn run_gc(&mut self, policy: BgcGcPolicy) -> Result<BgcGcResult, BgcError>

Run a complete GC cycle using the given policy.

Source§

impl BlockGarbageCollector

Source

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.

Source

pub fn collect_orphans_default(&self) -> Vec<BgcBlockCid>

Collect orphans using the internal min_age_secs from config.

Source§

impl BlockGarbageCollector

Source

pub fn gc_stats(&self) -> BgcCollectorStats

Compute current collector statistics.

Source

pub fn block_count(&self) -> usize

Return the number of registered blocks.

Source

pub fn log_len(&self) -> usize

Return the number of GC log entries.

Source

pub fn log_entries(&self) -> impl Iterator<Item = &BgcGcLogEntry>

Iterate over log entries (most recent last).

Source

pub fn gc_log(&self) -> &VecDeque<BgcGcLogEntry>

Return the full GC log as a slice-like iterator.

Source§

impl BlockGarbageCollector

Source

pub fn random_cid(&mut self) -> BgcBlockCid

Generate a pseudo-random CID for testing (uses xorshift).

Source

pub fn cid_from_bytes(data: &[u8]) -> BgcBlockCid

Compute a deterministic CID from raw bytes (used in tests).

Source

pub fn config(&self) -> &BgcCollectorConfig

Return the config (read-only).

Source

pub fn config_mut(&mut self) -> &mut BgcCollectorConfig

Mutate the config (e.g. flip dry_run in tests).

Source

pub fn update_edges( &mut self, cid: BgcBlockCid, new_refs: Vec<BgcBlockCid>, ) -> Result<(), BgcError>

Update edges for an already-registered block.

Source

pub fn children_of(&self, cid: &BgcBlockCid) -> &[BgcBlockCid]

Return children of a block (direct DAG edges).

Source

pub fn reconcile_pin_flags(&mut self)

Set the is_pinned flag to match the pin set (reconcile).

Source

pub fn promote_generation(&mut self)

Promote all blocks in generation 0 to generation 1.

Source

pub fn compact_edges(&mut self)

Compact: remove all edge entries for blocks that no longer exist.

Source

pub fn clear(&mut self)

Clear entire state — useful in tests.

Source

pub fn all_cids(&self) -> Vec<BgcBlockCid>

Return the set of all registered CIDs.

Source

pub fn reachable_set(&self) -> BTreeSet<BgcBlockCid>

Return the reachable set (live blocks) without modifying state.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more