示例#1
0
        /// <summary>Create a range that will return all keys starting with <paramref name="prefix"/>: ('prefix' &lt;= k &lt; strinc('prefix'))</summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static FdbKeyRange StartsWith(Slice prefix)
        {
            if (prefix.IsNull)
            {
                throw Fdb.Errors.KeyCannotBeNull("prefix");
            }

            // prefix => [ prefix, prefix + 1 )
            return(new FdbKeyRange(
                       prefix,
                       FdbKey.Increment(prefix)
                       ));
        }
示例#2
0
        /// <summary>Create a range that selects all keys starting with <paramref name="prefix"/>, but not the prefix itself: ('prefix\x00' &lt;= k &lt; string('prefix')</summary>
        /// <param name="prefix">Key prefix (that will be excluded from the range)</param>
        /// <returns>Range including all keys with the specified prefix.</returns>
        public static FdbKeyRange PrefixedBy(Slice prefix)
        {
            if (prefix.IsNull)
            {
                throw Fdb.Errors.KeyCannotBeNull("prefix");
            }

            // prefix => [ prefix."\0", prefix + 1)
            return(new FdbKeyRange(
                       prefix + FdbKey.MinValue,
                       FdbKey.Increment(prefix)
                       ));
        }
示例#3
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);
        }
示例#4
0
        /// <summary>Create a range that will return all keys starting with <paramref name="prefix"/>: ('prefix' &lt;= k &lt; strinc('prefix'))</summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static KeyRange StartsWith(Slice prefix)
        {
            if (prefix.IsNull)
            {
                throw Fdb.Errors.KeyCannotBeNull("prefix");
            }
            if (prefix.Count == 0)
            {
                return(new KeyRange(Slice.Empty, FdbKey.MaxValue));
            }


            // prefix => [ prefix, prefix + 1 )
            return(new KeyRange(
                       prefix,
                       FdbKey.Increment(prefix)
                       ));
        }