open static method

Future<KalanDB> open(
  1. String path, {
  2. KalanDBConfig? config,
})

Opens a pre-built .vdb file at path. Validates header + checksum. Throws KalanDBException on failure.

Implementation

static Future<KalanDB> open(String path, {KalanDBConfig? config}) async {
  final cfg = config ?? const KalanDBConfig();
  final pathPtr = path.toNativeUtf8(allocator: malloc);
  final cfgPtr = malloc<VDBConfig>();
  cfgPtr.ref
    ..efSearch = cfg.efSearch
    ..verifyChecksum = cfg.verifyChecksum ? 1 : 0;
  try {
    final db = bindings.open(pathPtr, cfgPtr);
    if (db == nullptr) {
      throw const KalanDBException('failed to allocate database handle');
    }
    if (bindings.isOpen(db) == 0) {
      final msg = bindings.lastError(db).toDartString();
      bindings.close(db);
      throw KalanDBException(msg.isEmpty ? 'failed to open database' : msg);
    }
    return KalanDB._(db);
  } finally {
    malloc.free(pathPtr);
    malloc.free(cfgPtr);
  }
}