searchRecords function

SessionSearchOutcome searchRecords(
  1. List<SessionRecord> records,
  2. SessionSearchQuery query
)

Searches an in-memory record list — the pure core the file scan feeds (and the unit tests drive directly). Records must be in file order. Map mode and branch scope run a cheap census pass first: map mode seeds the ordinal table the checkpoint-depth fold needs (bounded by checkpoint-referenced ids), branch scope resolves the archive's FINAL active branch so the result cap counts true branch hits at collection time (the E4 retention note covers the census's cost).

Implementation

SessionSearchOutcome searchRecords(
  List<SessionRecord> records,
  SessionSearchQuery query,
) {
  final needsCensus =
      query.mode == SessionSearchMode.map ||
      (query.mode == SessionSearchMode.search &&
          query.scope == SessionSearchScope.branch);
  var ordinalWanted = const <String>{};
  var branchIds = const <String>{};
  if (needsCensus) {
    final census = _ScanAccumulator(query, censusOnly: true);
    for (final record in records) {
      census.seeRecord(record);
    }
    ordinalWanted = census._ordinalWanted;
    branchIds = census._walkChain(census._activeLeaf);
  }
  final accumulator = _ScanAccumulator(
    query,
    ordinalWanted: ordinalWanted,
    branchIds: branchIds,
  );
  for (final record in records) {
    accumulator.seeRecord(record);
    if (accumulator.stopped) break;
  }
  return accumulator.finish();
}