/// <summary>Change the layer id of this directory</summary>
        /// <param name="trans">Transaction to use for the operation</param>
        /// <param name="newLayer">New layer id of this directory</param>
        public async Task <FdbDirectorySubspace> ChangeLayerAsync(IFdbTransaction trans, string newLayer)
        {
            Contract.NotNull(trans, nameof(trans));
            Contract.NotNull(newLayer, nameof(newLayer));

            var descriptor = this.Descriptor;

            if (descriptor.Path.Count == 0)
            {             // cannot change the layer of the root of a directory layer
                throw ThrowHelper.InvalidOperationException("Cannot change the layer id of the root of a directory layer or partition.");
            }

            if (descriptor.Layer == FdbDirectoryPartition.LayerId)
            {             // cannot change a partition back to a regular directory
                throw ThrowHelper.InvalidOperationException("Cannot change the layer id of a directory partition.");
            }
            if (newLayer == FdbDirectoryPartition.LayerId)
            {             // cannot change a regular directory into a new partition
                //REVIEW: or maybe we can? This would only be possible if this directory does not contain any sub-directory
                throw ThrowHelper.InvalidOperationException("Cannot transform a regular directory into a partition.");
            }

            EnsureIsValid();

            // set the layer to the new value
            var metadata = await this.DirectoryLayer.Resolve(trans);

            await metadata.ChangeLayerInternalAsync(trans, descriptor.Path, newLayer).ConfigureAwait(false);

            // and return the new version of the subspace
            var changed = new FdbDirectoryLayer.DirectoryDescriptor(descriptor.DirectoryLayer, descriptor.Path, descriptor.Prefix, newLayer, descriptor.Partition, descriptor.ValidationChain);

            return(new FdbDirectorySubspace(changed, this.KeyEncoder, this.Context));
        }
 internal FdbDirectorySubspace(FdbDirectoryLayer.DirectoryDescriptor descriptor, IDynamicKeyEncoder encoder, ISubspaceContext?context, bool cached)
     : base(descriptor.Prefix, encoder, context ?? SubspaceContext.Default)
 {
     Contract.Debug.Requires(descriptor?.Partition != null);
     this.Descriptor = descriptor;
     this.Cached     = cached;
 }
        internal static FdbDirectoryLayer.DirectoryDescriptor MakePartition(FdbDirectoryLayer.DirectoryDescriptor descriptor)
        {
            var partition = new FdbDirectoryLayer.PartitionDescriptor(descriptor.Path, KeySubspace.CreateDynamic(descriptor.Prefix), descriptor.Partition);

            return(new FdbDirectoryLayer.DirectoryDescriptor(descriptor.DirectoryLayer, descriptor.Path, descriptor.Prefix, descriptor.Layer, partition, descriptor.ValidationChain));
        }
 internal FdbDirectoryPartition(FdbDirectoryLayer.DirectoryDescriptor descriptor, FdbDirectoryLayer.PartitionDescriptor parent, IDynamicKeyEncoder keyEncoder, ISubspaceContext?context)
     : base(descriptor, keyEncoder, context)
 {
     Contract.NotNull(parent, nameof(parent));
     this.Parent = parent;
 }