open static method

Future<Wal> open(
  1. String path
)

Implementation

static Future<Wal> open(String path) async {
  final wal  = Wal._(path);
  final file = File(path);
  if (!await file.exists()) await file.create(recursive: true);

  // Resume LSN from the highest valid record in the existing WAL.
  try {
    final bytes   = await file.readAsBytes();
    final content = const Utf8Decoder(allowMalformed: true).convert(bytes);
    for (final line in content.split('\n').reversed) {
      if (line.trim().isEmpty) continue;
      final rec = WalRecord.tryDecode(line);
      if (rec != null) {
        wal._lsn        = rec.lsn;
        wal._flushedLsn = rec.lsn;
        break;
      }
    }
  } catch (_) {}

  // Open RandomAccessFile in append mode (creates if absent, does not truncate).
  wal._raf = await file.open(mode: FileMode.append);

  return wal;
}