open static method
Open (or create) a paged file.
If a <path>.journal exists from a previous crashed write, it is
rolled back before the file is exposed for reads, restoring the
pre-crash image of every page recorded in it.
Implementation
static Future<PagedFile> open(
String path, {
int pageSize = 4096,
int cacheCapacity = 64,
}) async {
if (pageSize < 512 || (pageSize & (pageSize - 1)) != 0) {
throw ArgumentError.value(
pageSize, 'pageSize', 'must be a power of two ≥ 512');
}
if (cacheCapacity < 1) {
throw ArgumentError.value(cacheCapacity, 'cacheCapacity', 'must be ≥ 1');
}
final pf = PagedFile._(path, pageSize, cacheCapacity);
// Recover from a crashed previous transaction BEFORE opening for
// normal use — the rollback writes through directly.
await pf._recoverIfNeeded();
final f = File(path);
if (!await f.exists()) {
await f.create(recursive: true);
}
pf._data = await f.open(mode: FileMode.append);
final len = await pf._data!.length();
if (len % pageSize != 0) {
// Truncate trailing partial page — it can only come from a torn
// append that the journal didn't cover.
await pf._data!.truncate(len - (len % pageSize));
}
pf._pageCount = (await pf._data!.length()) ~/ pageSize;
return pf;
}