openWithRecovery<E> method

Future<Box<E>> openWithRecovery<E>(
  1. String name
)

The corruption drill: open name's pre-corrupted fixture through the recovery path (clear + re-fetch) and return the recovered, EMPTY box.

Contract (spec 005 edge cases):

  • the pre-corrupted fixture must exist inside the temp box;
  • opening detects the corruption — Hive's crash recovery drops the unreadable frames (or the open throws outright), and the drill clears the fixture and reopens when the open refuses;
  • the recovered box must be EMPTY — the caller re-fetches from the source through the adapter;
  • nothing outside the temp box is touched.

Violations surface as CorruptionDrillFailure — a deterministic red.

Implementation

Future<Box<E>> openWithRecovery<E>(String name) async {
  final key = name.toLowerCase();
  final file = File(boxPath(key));
  if (!file.existsSync()) {
    throw CorruptionDrillFailure(
      'no pre-corrupted fixture for box "$name" — seed it with '
      'seedCorruptedBox("$name") before drilling',
    );
  }
  Box<E> box;
  try {
    box = await Hive.openBox<E>(key);
  } catch (error) {
    // The open refused the corruption outright — clear + re-fetch.
    await file.delete();
    box = await Hive.openBox<E>(key);
  }
  if (box.length != 0) {
    throw CorruptionDrillFailure(
      'recovery for box "$name" left ${box.length} frame(s) readable — '
      'the corrupted fixture must recover to an empty box (clear + '
      're-fetch), not partially decoded data',
    );
  }
  return box;
}