/// <summary>Opens a named partition, and change the root subspace of the database to the corresponding prefix</summary> internal static async Task SwitchToNamedPartitionAsync([NotNull] FdbDatabase db, [NotNull, ItemNotNull] string[] path, bool readOnly, CancellationToken ct) { Contract.Requires(db != null && path != null); ct.ThrowIfCancellationRequested(); if (path.Length == 0) { throw new ArgumentException("The path to the named partition cannot be empty", nameof(path)); } if (Logging.On) { Logging.Verbose(typeof(Fdb.Directory), "OpenNamedPartitionAsync", $"Opened root layer using cluster file '{db.ClusterFile}'"); } // look up in the root layer for the named partition var descriptor = await db.ReadWriteAsync(tr => db.Directory.CreateOrOpenAsync(tr, path, layer: FdbDirectoryPartition.LayerId), 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 '/' var rootSpace = descriptor.Copy(); //note: create a copy of the key //TODO: find a nicer way to do that! db.ChangeRoot(rootSpace, FdbDirectoryLayer.Create(rootSpace, path), 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}"); } }
/// <summary>Opens a named partition, and change the root subspace of the database to the corresponding prefix</summary> internal static async Task SwitchToNamedPartitionAsync(FdbDatabase db, FdbPath root, CancellationToken ct) { Contract.Requires(db != null); ct.ThrowIfCancellationRequested(); if (Logging.On) { Logging.Verbose(typeof(Fdb.Directory), "OpenNamedPartitionAsync", $"Opened root layer using cluster file '{db.ClusterFile}'"); } if (root.Count != 0) { // create the root partition if does not already exist var descriptor = await db.ReadWriteAsync(tr => db.DirectoryLayer.CreateOrOpenAsync(tr, root), ct).ConfigureAwait(false); if (Logging.On) { Logging.Info(typeof(Fdb.Directory), "OpenNamedPartitionAsync", $"Opened partition {descriptor.Path} at {descriptor.GetPrefixUnsafe()}"); } } }