getAllBlocks method

  1. @override
Future<List<Block>> getAllBlocks()

Returns a Future that resolves to a List of all Blocks in the store.

Implementation

@override
/// Returns a [Future] that resolves to a [List] of all [Block]s in the store.
Future<List<Block>> getAllBlocks() async {
  try {
    _logger.debug('Getting all blocks');
    final blocks = <Block>[];
    for (final cid in _blockIndex) {
      final cached = _blockCache.get(cid);
      if (cached != null) {
        blocks.add(cached);
        continue;
      }
      final data = await getPlatform().readBytes(p.join(path, cid));
      if (data != null) {
        // Bulk reads bypass the cache deliberately: the returned list
        // defeats the bound anyway, and churning the LRU would evict
        // useful entries. Filenames are CIDs — no rehash needed.
        blocks.add(Block(cid: CID.decode(cid), data: data));
      }
    }
    return blocks;
  } catch (e) {
    _logger.error('Failed to get all blocks', e);
    return [];
  }
}