init method

  1. @override
Future<void> init()
override

Initializes the table from storage (called by Collection during startup when isPersistent is true).

The default implementation is a no-op. Override to load an index, warm a cache, or set up internal state from the backing store. Must be called exactly once before any other method.

Implementation

@override
Future<void> init() async {
  if (_db != null || _closed) return;
  var db = await _openIfHealthy(_path);
  if (db == null) {
    final tag = 'corrupt-${DateTime.now().toUtc().millisecondsSinceEpoch}';
    final quarantine = backend.quarantineDatabase(_path, tag);
    db = await _restoreOrReset(quarantine);
  }
  db.execute('PRAGMA journal_mode = WAL');
  db.execute('PRAGMA synchronous = NORMAL');
  db.execute('PRAGMA foreign_keys = ON');
  _migrate(db);
  _nodeId = _readOrCreateNodeId(db);
  _canonical = _readClock(db, _nodeId);
  _db = db;
  _startSnapshots();

  final recovery = _recovery;
  if (recovery != null) {
    scheduleMicrotask(() {
      if (!_recoveries.isClosed) _recoveries.add(recovery);
    });
  }
}