loadSync method
Loads the newest valid manifest, or null when the database is empty.
A single torn slot is tolerated (the other slot is authoritative). If
allowTornSingleSlot is true and exactly one slot file exists but does
not decode, null is returned so the caller can recover from the WAL:
that is the signature of a crash during the very first manifest write,
before the WAL was truncated. With two bad slot files the database is
corrupt, not empty; returning null there would silently drop every
document.
Implementation
Manifest? loadSync({bool allowTornSingleSlot = false}) {
Manifest? best;
var sawFile = 0;
for (var slot = 0; slot < slotNames.length; slot++) {
if (!directory.existsSync(slotNames[slot])) continue;
sawFile++;
SyncStorageFile? file;
try {
file = directory.openSync(slotNames[slot]);
final bytes = file.readAllSync();
if (bytes.isEmpty) continue;
final manifest = Manifest.decode(bytes);
if (best == null || manifest.generation > best.generation) {
best = manifest;
}
} on VxsFormatException {
// A torn slot is expected after a crash; the other slot is authoritative.
continue;
} finally {
file?.closeSync();
}
}
if (best == null && sawFile > 0) {
if (sawFile == 1 && allowTornSingleSlot) return null;
throw const VelxoCorruptException(
'manifest files exist but none decodes; refusing to open',
);
}
return best;
}