open static method

Future<DatabaseOwner> open(
  1. String path, {
  2. required bool directoryEntriesDurable,
  3. bool wal = false,
})

Implementation

static Future<DatabaseOwner> open(String path,
    {required bool directoryEntriesDurable, bool wal = false}) async {
  if (!directoryEntriesDurable) {
    throw UnsupportedError('Durably provision database and sidecar first');
  }
  final canonical = await File(path).resolveSymbolicLinks();
  final ready = ReceivePort();
  final exited = ReceivePort();
  final admin = Capability();
  Isolate? isolate;
  try {
    isolate = await Isolate.spawn(
        _runOwner, [canonical, wal, admin, ready.sendPort]);
    isolate.addOnExitListener(exited.sendPort);
    final response =
        await ready.first.timeout(const Duration(seconds: 30)) as List;
    if (response[0] != 'ready') {
      // The owner reports the error before its finally closes the files.
      // Killing it there leaves Windows holding the directory.
      await _releaseFailedOwner(isolate, exited);
      isolate = null;
      throw StateError(response[1] as String);
    }
    isolate.removeOnExitListener(exited.sendPort);
    final port = response[1] as SendPort;
    final opened = isolate;
    isolate = null;
    return DatabaseOwner._(opened, port, admin, DatabaseClient.connect(port));
  } catch (_) {
    if (isolate != null) await _releaseFailedOwner(isolate, exited);
    rethrow;
  } finally {
    ready.close();
    exited.close();
  }
}