public Task <IFdbDatabaseHandler> OpenDatabaseAsync(string databaseName, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(Task.FromCanceled <IFdbDatabaseHandler>(cancellationToken)); } var future = FdbNative.ClusterCreateDatabase(m_handle, databaseName); return(FdbFuture.CreateTaskFromHandle( future, (h) => { DatabaseHandle database; var err = FdbNative.FutureGetDatabase(h, out database); if (err != FdbError.Success) { database.Dispose(); throw Fdb.MapToException(err); } var handler = new FdbNativeDatabase(database); return (IFdbDatabaseHandler)handler; }, cancellationToken )); }
private static async ValueTask <IFdbDatabaseHandler> CreateDatabaseLegacyAsync(string?clusterFile, CancellationToken ct) { // In legacy API versions, you first had to create "cluster" handle and then obtain a database handle that that cluster. // The API is async, but the future always completed inline... ClusterHandle? cluster = null; DatabaseHandle?database = null; try { cluster = await FdbFuture.CreateTaskFromHandle( FdbNative.CreateCluster(clusterFile), h => { var err = FdbNative.FutureGetCluster(h, out var handle); if (Fdb.Failed(err)) { throw Fdb.MapToException(err) !; } return(handle); }, ct).ConfigureAwait(false); database = await FdbFuture.CreateTaskFromHandle( FdbNative.ClusterCreateDatabase(cluster, "DB"), h => { var err = FdbNative.FutureGetDatabase(h, out var handle); if (Fdb.Failed(err)) { throw Fdb.MapToException(err) !; } return(handle); }, ct).ConfigureAwait(false); return(new FdbNativeDatabase(database, clusterFile, cluster)); } catch (Exception) { database?.Dispose(); cluster?.Dispose(); throw; } }