예제 #1
0
            public static async Task <IFdbDatabase> OpenNamedPartitionAsync(string clusterFile, string dbName, [NotNull] IEnumerable <string> path, bool readOnly, CancellationToken ct)
            {
                if (path == null)
                {
                    throw new ArgumentNullException(nameof(path));
                }
                var partitionPath = path.ToList();

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

                // looks at the global partition table for the specified named partition

                // By convention, all named databases will be under the "/Databases" folder
                FdbDatabase db        = null;
                var         rootSpace = KeySubspace.Empty;

                try
                {
                    db = await Fdb.OpenInternalAsync(clusterFile, dbName, rootSpace, readOnly : false, ct : ct).ConfigureAwait(false);

                    var rootLayer = FdbDirectoryLayer.Create(rootSpace);
                    if (Logging.On)
                    {
                        Logging.Verbose(typeof(Fdb.Directory), "OpenNamedPartitionAsync", $"Opened root layer of database {db.Name} using cluster file '{db.Cluster.Path}'");
                    }

                    // look up in the root layer for the named partition
                    var descriptor = await rootLayer.CreateOrOpenAsync(db, partitionPath, layer : FdbDirectoryPartition.LayerId, ct : ct).ConfigureAwait(false);

                    if (Logging.On)
                    {
                        Logging.Verbose(typeof(Fdb.Directory), "OpenNamedPartitionAsync", $"Found named partition '{descriptor.FullName}' at prefix {descriptor}");
                    }

                    // we have to chroot the database to the new prefix, and create a new DirectoryLayer with a new '/'
                    rootSpace = descriptor.Copy();                     //note: create a copy of the key
                    //TODO: find a nicer way to do that!
                    db.ChangeRoot(rootSpace, FdbDirectoryLayer.Create(rootSpace, partitionPath), readOnly);

                    if (Logging.On)
                    {
                        Logging.Info(typeof(Fdb.Directory), "OpenNamedPartitionAsync", $"Opened partition {descriptor.FullName} at {db.GlobalSpace}, using directory layer at {db.Directory.DirectoryLayer.NodeSubspace}");
                    }

                    return(db);
                }
                catch (Exception e)
                {
                    db?.Dispose();
                    if (Logging.On)
                    {
                        Logging.Exception(typeof(Fdb.Directory), "OpenNamedPartitionAsync", e);
                    }
                    throw;
                }
            }
예제 #2
0
        internal static async Task <FdbDatabase> OpenInternalAsync(string clusterFile, string dbName, IFdbSubspace globalSpace, bool readOnly, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            dbName      = dbName ?? "DB";
            globalSpace = globalSpace ?? FdbSubspace.Empty;

            if (Logging.On)
            {
                Logging.Info(typeof(Fdb), "OpenAsync", String.Format("Connecting to database '{0}' using cluster file '{1}' and subspace '{2}' ...", dbName, clusterFile, globalSpace));
            }

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

            try
            {
                cluster = await CreateClusterInternalAsync(clusterFile, cancellationToken).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 : readOnly, ownsCluster : true, cancellationToken : cancellationToken).ConfigureAwait(false);

                success = true;
                return(db);
            }
            finally
            {
                if (!success)
                {
                    // cleanup the cluter if something went wrong
                    if (db != null)
                    {
                        db.Dispose();
                    }
                    if (cluster != null)
                    {
                        cluster.Dispose();
                    }
                }
            }
        }
예제 #3
0
        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();
                }
            }
        }