allocatePage method

Future<int> allocatePage()

Allocate a new page at the end of the file. Returns its page number. The page is zero-filled, cached, and marked dirty.

Implementation

Future<int> allocatePage() async {
  _ensureOpen();
  // Make room BEFORE inserting — otherwise the just-allocated page
  // (clean for an instant before we mark it dirty) can be picked as
  // the LRU clean victim by [_evictIfNeeded] and dropped, leaving
  // the caller's reference detached from the cache so future writes
  // are silently lost.
  await _makeRoomForOneMore();
  final pageNo = _pageCount;
  _pageCount += 1;
  final buf = Uint8List(pageSize);
  _cache[pageNo] = buf;
  _touch(pageNo);
  // New pages have no pre-image to journal — mark them as already
  // captured so [commit] doesn't try to roll them back to "the page
  // didn't exist", which our truncate-on-rollback handles separately.
  _journaled.add(pageNo);
  _dirty.add(pageNo);
  return pageNo;
}