Пример #1
0
        /// <summary>Checks that a key is valid, and is inside the global key space of this database</summary>
        /// <param name="database"></param>
        /// <param name="key">Key to verify</param>
        /// <param name="endExclusive">If true, the key is allowed to be one past the maximum key allowed by the global namespace</param>
        /// <param name="ignoreError"></param>
        /// <param name="error"></param>
        /// <returns>An exception if the key is outside of the allowed key space of this database</returns>
        internal static bool ValidateKey(IFdbDatabase database, ref Slice key, bool endExclusive, bool ignoreError, out Exception error)
        {
            error = null;

            // null or empty keys are not allowed
            if (key.IsNull)
            {
                if (!ignoreError)
                {
                    error = Fdb.Errors.KeyCannotBeNull();
                }
                return(false);
            }

            // key cannot be larger than maximum allowed key size
            if (key.Count > Fdb.MaxKeySize)
            {
                if (!ignoreError)
                {
                    error = Fdb.Errors.KeyIsTooBig(key);
                }
                return(false);
            }

            // special case for system keys
            if (IsSystemKey(ref key))
            {
                // note: it will fail later if the transaction does not have access to the system keys!
                return(true);
            }

            // first, it MUST start with the root prefix of this database (if any)
            if (!database.Contains(key))
            {
                // special case: if endExclusive is true (we are validating the end key of a ClearRange),
                // and the key is EXACTLY equal to strinc(globalSpace.Prefix), we let is slide
                if (!endExclusive ||
                    !key.Equals(FdbKey.Increment(database.GlobalSpace.GetPrefix())))                 //TODO: cache this?
                {
                    if (!ignoreError)
                    {
                        error = Fdb.Errors.InvalidKeyOutsideDatabaseNamespace(database, key);
                    }
                    return(false);
                }
            }

            return(true);
        }
 public virtual bool Contains(Slice key)
 {
     return(m_database.Contains(key));
 }