gc method

  1. @override
Future<int> gc()

Returns a Future that resolves to the number of blocks removed during garbage collection.

Removes all unpinned blocks from the store.

Implementation

@override
/// Returns a [Future] that resolves to the number of blocks removed during garbage collection.
///
/// Removes all unpinned blocks from the store.
Future<int> gc() async {
  _logger.info('Starting Garbage Collection...');
  int removedCount = 0;

  // Iterate the on-disk index so uncached blocks are collected too
  final allCids = _blockIndex.toList();

  for (final cidStr in allCids) {
    try {
      final cid = CID.decode(cidStr);
      if (!_pinManager.isBlockPinned(cid.toProto())) {
        await removeBlock(cidStr);
        removedCount++;
      }
    } catch (e) {
      _logger.warning('Failed to process block $cidStr during GC: $e');
    }
  }

  _logger.info('Garbage Collection finished. Removed $removedCount blocks.');
  return removedCount;
}