public static Task <IFdbDatabase> OpenAsync([CanBeNull] IKeySubspace globalSpace, CancellationToken ct = default)
        {
            var options = new FdbConnectionOptions
            {
                GlobalSpace = globalSpace,
            };

            return(OpenInternalAsync(options, ct));
        }
        public static Task <IFdbDatabase> OpenAsync([CanBeNull] string clusterFile, [CanBeNull] string dbName, CancellationToken ct = default)
        {
            var options = new FdbConnectionOptions
            {
                ClusterFile = clusterFile,
                DbName      = dbName,
            };

            return(OpenInternalAsync(options, ct));
        }
        public static Task <IFdbDatabase> OpenAsync([CanBeNull] string clusterFile, [CanBeNull] string dbName, [CanBeNull] IKeySubspace globalSpace, bool readOnly = false, CancellationToken ct = default)
        {
            var options = new FdbConnectionOptions
            {
                ClusterFile = clusterFile,
                DbName      = dbName,
                GlobalSpace = globalSpace,
                ReadOnly    = readOnly
            };

            return(OpenInternalAsync(options, ct));
        }
            public static async Task <IFdbDatabase> OpenNamedPartitionAsync(string clusterFile, string dbName, [NotNull] IEnumerable <string> path, bool readOnly, CancellationToken ct)
            {
                Contract.NotNull(path, nameof(path));
                var partitionPath = (path as string[]) ?? path.ToArray();

                if (partitionPath.Length == 0)
                {
                    throw new ArgumentException("The path to the named partition cannot be empty", nameof(path));
                }

                var options = new FdbConnectionOptions
                {
                    ClusterFile   = clusterFile,
                    DbName        = dbName,
                    PartitionPath = partitionPath,
                };
                var db = await Fdb.OpenInternalAsync(options, ct).ConfigureAwait(false);

                return(db);
            }
        internal static async Task <IFdbDatabase> OpenInternalAsync(FdbConnectionOptions options, CancellationToken ct)
        {
            Contract.Requires(options != null);
            ct.ThrowIfCancellationRequested();

            string       clusterFile = options.ClusterFile;
            string       dbName      = options.DbName ?? FdbConnectionOptions.DefaultDbName;  // new FdbConnectionOptions { GlobalSpace =
            bool         readOnly    = options.ReadOnly;
            IKeySubspace globalSpace = options.GlobalSpace ?? KeySubspace.Empty;

            string[] partitionPath = options.PartitionPath?.ToArray();
            bool     hasPartition  = partitionPath != null && partitionPath.Length > 0;

            if (Logging.On)
            {
                Logging.Info(typeof(Fdb), nameof(OpenInternalAsync), $"Connecting to database '{dbName}' using cluster file '{clusterFile}' and subspace '{globalSpace}' ...");
            }

            FdbCluster  cluster = null;
            FdbDatabase db      = null;
            bool        success = false;

            try
            {
                cluster = await CreateClusterInternalAsync(clusterFile, ct).ConfigureAwait(false);

                //note: since the cluster is not provided by the caller, link it with the database's Dispose()
                db = await cluster.OpenDatabaseInternalAsync(dbName, globalSpace, readOnly : !hasPartition && readOnly, ownsCluster : true, ct : ct).ConfigureAwait(false);

                // set the default options
                if (options.DefaultTimeout != TimeSpan.Zero)
                {
                    db.DefaultTimeout = checked ((int)Math.Ceiling(options.DefaultTimeout.TotalMilliseconds));
                }
                if (options.DefaultRetryLimit != 0)
                {
                    db.DefaultRetryLimit = options.DefaultRetryLimit;
                }
                if (options.DefaultMaxRetryDelay != 0)
                {
                    db.DefaultMaxRetryDelay = options.DefaultMaxRetryDelay;
                }

                if (hasPartition)
                {                 // open the partition, and switch the root of the db
                    await Fdb.Directory.SwitchToNamedPartitionAsync(db, partitionPath, readOnly, ct);
                }

                success = true;
                return(db);
            }
            finally
            {
                if (!success)
                {
                    // cleanup the cluter if something went wrong
                    db?.Dispose();
                    cluster?.Dispose();
                }
            }
        }
 public static Task <IFdbDatabase> OpenAsync([NotNull] FdbConnectionOptions options, CancellationToken ct)
 {
     Contract.NotNull(options, nameof(options));
     return(OpenInternalAsync(options, ct));
 }