protected void FailKeyOutOfBound(Slice key)
        {
#if DEBUG
            // only in debug mode, because have the key and subspace in the exception message could leak sensitive information
            string msg = $"The key {FdbKey.Dump(key)} does not belong to subspace {this}";
#else
            string msg = "The specifed key does not belong to this subspace";
#endif
            throw new ArgumentException(msg, nameof(key));
        }
        public virtual string DumpKey(Slice key)
        {
            // note: we can't use ExtractAndCheck(...) because it may throw in derived classes
            var prefix = this.InternalKey;

            if (!key.StartsWith(prefix))
            {
                FailKeyOutOfBound(key);
            }

            return(FdbKey.Dump(key.Substring(prefix.Count)));
        }
            internal static Exception InvalidKeyOutsideDatabaseNamespace(IFdbDatabase db, ReadOnlySpan <byte> key)
            {
                Contract.Requires(db != null);
                return(new FdbException(
                           FdbError.KeyOutsideLegalRange,
#if DEBUG
                           $"An attempt was made to use a key '{FdbKey.Dump(key)}' that is outside of the root keyspace {db.Root}."
#else
                           $"An attempt was made to use a key that is outside of the root Root {db.Root}"
#endif
                           ));
            }
            internal static Exception InvalidKeyOutsideDatabaseNamespace(IFdbDatabase db, Slice key)
            {
                Contract.Requires(db != null);
                return(new FdbException(
                           FdbError.KeyOutsideLegalRange,
#if DEBUG
                           $"An attempt was made to use a key '{FdbKey.Dump(key)}' that is outside of the global namespace {db.GlobalSpace}."
#else
                           $"An attempt was made to use a key that is outside of the global namespace {db.GlobalSpace}"
#endif
                           ));
            }
        /// <summary>
        /// Modify the database snapshot represented by transaction to remove the given key from the database. If the key was not previously present in the database, there is no effect.
        /// </summary>
        /// <param name="key">Name of the key to be removed from the database.</param>
        public void Clear(Slice key)
        {
            EnsureCanWrite();

            m_database.EnsureKeyIsValid(ref key);

#if DEBUG
            if (Logging.On && Logging.IsVerbose)
            {
                Logging.Verbose(this, "Clear", String.Format("Clearing '{0}'", FdbKey.Dump(key)));
            }
#endif

            m_handler.Clear(key);
        }
        /// <summary>
        /// Modify the database snapshot represented by transaction to change the given key to have the given value. If the given key was not previously present in the database it is inserted.
        /// The modification affects the actual database only if transaction is later committed with CommitAsync().
        /// </summary>
        /// <param name="key">Name of the key to be inserted into the database.</param>
        /// <param name="value">Value to be inserted into the database.</param>
        public void Set(Slice key, Slice value)
        {
            EnsureCanWrite();

            m_database.EnsureKeyIsValid(ref key);
            m_database.EnsureValueIsValid(ref value);

#if DEBUG
            if (Logging.On && Logging.IsVerbose)
            {
                Logging.Verbose(this, "Set", String.Format("Setting '{0}' = {1}", FdbKey.Dump(key), Slice.Dump(value)));
            }
#endif

            m_handler.Set(key, value);
        }
        /// <summary>
        /// Returns a list of public network addresses as strings, one for each of the storage servers responsible for storing <paramref name="key"/> and its associated value
        /// </summary>
        /// <param name="key">Name of the key whose location is to be queried.</param>
        /// <returns>Task that will return an array of strings, or an exception</returns>
        public Task <string[]> GetAddressesForKeyAsync(Slice key)
        {
            EnsureCanRead();

            m_database.EnsureKeyIsValid(ref key);

#if DEBUG
            if (Logging.On && Logging.IsVerbose)
            {
                Logging.Verbose(this, "GetAddressesForKeyAsync", String.Format("Getting addresses for key '{0}'", FdbKey.Dump(key)));
            }
#endif

            return(m_handler.GetAddressesForKeyAsync(key, cancellationToken: m_cancellation));
        }
        /// <summary>Modify the database snapshot represented by this transaction to perform the operation indicated by <paramref name="mutation"/> with operand <paramref name="param"/> to the value stored by the given key.</summary>
        /// <param name="key">Name of the key whose value is to be mutated.</param>
        /// <param name="param">Parameter with which the atomic operation will mutate the value associated with key_name.</param>
        /// <param name="mutation">Type of mutation that should be performed on the key</param>
        public void Atomic(Slice key, Slice param, FdbMutationType mutation)
        {
            //note: this method as many names in the various bindings:
            // - C API   : fdb_transaction_atomic_op(...)
            // - Java    : tr.Mutate(..)
            // - Node.js : tr.add(..), tr.max(..), ...

            EnsureCanWrite();

            m_database.EnsureKeyIsValid(ref key);
            m_database.EnsureValueIsValid(ref param);

            //The C API does not fail immediately if the mutation type is not valid, and only fails at commit time.
            EnsureMutationTypeIsSupported(mutation, Fdb.ApiVersion);

#if DEBUG
            if (Logging.On && Logging.IsVerbose)
            {
                Logging.Verbose(this, "AtomicCore", String.Format("Atomic {0} on '{1}' = {2}", mutation.ToString(), FdbKey.Dump(key), Slice.Dump(param)));
            }
#endif

            m_handler.Atomic(key, param, mutation);
        }
            internal static Exception InvalidKeyOutsideDatabaseNamespace(IFdbDatabase db, Slice key)
            {
                Contract.Requires(db != null);
                return(new FdbException(
                           FdbError.KeyOutsideLegalRange,
#if DEBUG
                           String.Format("An attempt was made to use a key '{2}' that is outside of the global namespace {0} of database '{1}'", db.GlobalSpace, db.Name, FdbKey.Dump(key))
#else
                           String.Format("An attempt was made to use a key that is outside of the global namespace {0} of database '{1}'", db.GlobalSpace, db.Name)
#endif
                           ));
            }
 /// <summary>Returns a user-friendly description of this directory</summary>
 public override string ToString()
 {
     return($"DirectorySubspace(path={this.Path.ToString()}, prefix={FdbKey.Dump(GetPrefixUnsafe())})");
 }
 public override string ToString()
 {
     return($"DirectoryPartition(path={this.Descriptor.Path.ToString()}, prefix={FdbKey.Dump(GetPrefixUnsafe())})");
 }
Esempio n. 12
0
 public override string ToString()
 {
     return("Watch(" + FdbKey.Dump(this.Key) + ")");
 }