compact method

  1. @override
Stream<int> compact(
  1. bool dryRun
)
override

Compact the access log. If dryRun is true, yields each entry id that WOULD be removed without performing the deletion. If false, performs the compaction and yields each id as it is removed.

Implementation

@override
Stream<int> compact(bool dryRun) async* {
  final total = entriesCount();
  final firstN = (total * (compactionPercentage / 100)).toInt();
  if (firstN <= 0) return;
  final ids = _db.raw
      .select('SELECT seq FROM access_log ORDER BY seq LIMIT ?;', [firstN])
      .map((r) => r['seq'] as int)
      .toList();
  if (dryRun) {
    yield* Stream.fromIterable(ids);
    return;
  }
  _db.runInTransaction(() {
    for (final id in ids) {
      _db.raw.execute('DELETE FROM access_log WHERE seq = ?;', [id]);
    }
  });
  yield* Stream.fromIterable(ids);
}