close method

Future<void> close()

Release the cross-process file lock. Idempotent. Always call this when you're done with a path-backed database — otherwise the <path>.lock sidecar will keep readers/writers blocked until the process exits.

Implementation

Future<void> close() async {
  // Flush built vector-index state so the next open can skip the
  // (potentially expensive) rebuild of HNSW graphs / IVF centroids.
  if (path != null) {
    var anyBuilt = false;
    for (final b in _vectorIndexes.values) {
      if (b.index != null) {
        anyBuilt = true;
        break;
      }
    }
    if (anyBuilt) {
      try {
        await _persist();
      } catch (_) {
        // Best-effort; a failed flush must not block close.
      }
    }
  }
  // V37: drain any queued unawaited persist calls (e.g. from
  // vec_batch_insert / vec_import_csv TVFs) before releasing the
  // file lock so a reopen observes the latest committed state.
  try {
    await _persistChain;
  } catch (_) {}
  // Flush + close every paged table first; their journals must be
  // gone before we drop the file lock so a subsequent open sees a
  // clean state.
  for (final pt in _pagedTables.values) {
    try {
      await pt.commit();
    } catch (_) {}
    try {
      await pt.close();
    } catch (_) {}
  }
  _pagedTables.clear();
  final fl = _fileLock;
  _fileLock = null;
  if (fl != null) await fl.release();
}