truncate method

Future<void> truncate()

Implementation

Future<void> truncate() async {
  if (_closed) return;
  await _flush();
  await _raf.close();

  _lsn++;
  final checkpointRecord = WalBinRecord(
    lsn: _lsn, txnId: 0, prevLsn: 0,
    op: WalBinOp.checkpoint, flags: 0, tableId: 0, rowId: 0,
    ddlPayload: {'lsn': _lsn, 'ts': DateTime.now().millisecondsSinceEpoch},
  );
  final encoded = checkpointRecord.encode();
  final lenBuf  = Uint8List(4);
  ByteData.sublistView(lenBuf).setUint32(0, encoded.length, Endian.little);

  final file = File(path);
  final headerBytes = const Utf8Encoder().convert(_fileVersion);
  final content = <int>[
    ...headerBytes,
    ...lenBuf, ...encoded, ...lenBuf,
  ];
  await file.writeAsBytes(content);

  _raf = await file.open(mode: FileMode.append);
  await _flush();
  _flushedLsn = _lsn;
  _txnLastLsn.clear();
}