openOn static method

Future<Result<(NativeWorker, int), DbError>> openOn(
  1. NativeSymbols symbols,
  2. String path,
  3. String options, {
  4. bool abiV2 = false,
})

Opens <path>.lmdb with options (JSON) on a running worker of symbols: answers the worker and the handle, or the error the engine answered (such as DbErrorCode.legacyFormat).

With abiV2 (the library has it, see NativeSymbols.abiV2), the handle is one of the ABI v2: use it with executeV2 and closeV2.

Implementation

static Future<Result<(NativeWorker, int), DbError>> openOn(
  NativeSymbols symbols,
  String path,
  String options, {
  bool abiV2 = false,
}) async {
  final worker = await of(symbols);

  // A worker that stopped meanwhile is no longer listed: ask again.
  if (worker._stopped) {
    return openOn(symbols, path, options, abiV2: abiV2);
  }

  final operation = abiV2 ? 'open_v2' : 'open';

  return (await worker._call(operation, [path, options])).flatMap(
    (reply) => switch (reply) {
      [0, final String response] => ProtocolEnvelope.decode(response).flatMap(
        (payload) => Err(
          DbError(
            DbErrorCode.unsupportedProtocol,
            'The engine opened nothing but answered $payload',
          ),
        ),
      ),
      [final int handle, String()] => Ok((worker, handle)),
      _ => Err(_malformed(reply)),
    },
  );
}