flushSync method
Flushes the memtable into a new segment and resets the WAL.
Does nothing when the memtable is empty. Returns the new segment info.
Implementation
SegmentInfo? flushSync() {
_checkWritable();
_checkOpen();
if (_memtable.length == 0) return null;
final builder = SegmentBuilder(blockTargetSize: blockTargetSize);
_memtable.forEachSorted((key, chunk, offset, length) {
if (length < 0) {
builder.addTombstone(key);
} else {
builder.add(key, _arena.view(chunk, offset, length));
}
});
final info = _writeSegment(
builder.finish().asUint8List(),
builder.docCount,
);
// Commit point passed: the WAL is no longer needed.
_walFile.truncateSync(0);
_walFile.flushSync();
_memtable.clear();
_arena = DocArena(chunkSize: _arenaChunkSize);
_memtableEpoch++;
_lastBackgroundWalEnd = 0;
// Bounded segment count: compaction runs on the write that crosses the
// threshold. This costs latency on that one write and keeps reads from
// degrading silently.
if (_maybeAutoCompact()) {
return _segmentInfos.isEmpty ? info : _segmentInfos.last;
}
return info;
}