Пример #1
0
        public FoundPage Find(MemorySlice key)
        {
            for (int i = 0; i < _cache.Length; i++)
            {
                var page = _cache[i];
                if (page == null)
                    continue;

                var first = page.FirstKey;
                var last = page.LastKey;
                
                switch (key.Options)
                {
                    case SliceOptions.BeforeAllKeys:
                        if (first.Options == SliceOptions.BeforeAllKeys)
                            return page;
                        break;
                    case SliceOptions.AfterAllKeys:
                        if (last.Options == SliceOptions.AfterAllKeys)
                            return page;
                        break;
                    case SliceOptions.Key:
                        if ((first.Options != SliceOptions.BeforeAllKeys && key.Compare(first) < 0))
                            continue;
                        if (last.Options != SliceOptions.AfterAllKeys && key.Compare(last) > 0)
                            continue;
                        return page;
                    default:
                        throw new ArgumentException(key.Options.ToString());
                }
            }

            return null;
        }
Пример #2
0
        public FoundPage Find(MemorySlice key)
        {
            int position = current;

            int itemsLeft = _cacheSize;

            while (itemsLeft > 0)
            {
                var page = _cache[position % _cacheSize];
                if (page == null)
                {
                    itemsLeft--;
                    position++;

                    continue;
                }

                var first = page.FirstKey;
                var last  = page.LastKey;

                switch (key.Options)
                {
                case SliceOptions.Key:
                    if ((first.Options != SliceOptions.BeforeAllKeys && key.Compare(first) < 0))
                    {
                        break;
                    }
                    if (last.Options != SliceOptions.AfterAllKeys && key.Compare(last) > 0)
                    {
                        break;
                    }
                    return(page);

                case SliceOptions.BeforeAllKeys:
                    if (first.Options == SliceOptions.BeforeAllKeys)
                    {
                        return(page);
                    }
                    break;

                case SliceOptions.AfterAllKeys:
                    if (last.Options == SliceOptions.AfterAllKeys)
                    {
                        return(page);
                    }
                    break;

                default:
                    throw new ArgumentException(key.Options.ToString());
                }

                itemsLeft--;
                position++;
            }

            return(null);
        }
Пример #3
0
        public FoundPage Find(MemorySlice key)
        {
            for (int i = 0; i < _cache.Length; i++)
            {
                var page = _cache[i];
                if (page == null)
                {
                    continue;
                }

                var first = page.FirstKey;
                var last  = page.LastKey;

                switch (key.Options)
                {
                case SliceOptions.BeforeAllKeys:
                    if (first.Options == SliceOptions.BeforeAllKeys)
                    {
                        return(page);
                    }
                    break;

                case SliceOptions.AfterAllKeys:
                    if (last.Options == SliceOptions.AfterAllKeys)
                    {
                        return(page);
                    }
                    break;

                case SliceOptions.Key:
                    if ((first.Options != SliceOptions.BeforeAllKeys && key.Compare(first) < 0))
                    {
                        continue;
                    }
                    if (last.Options != SliceOptions.AfterAllKeys && key.Compare(last) > 0)
                    {
                        continue;
                    }
                    return(page);

                default:
                    throw new ArgumentException(key.Options.ToString());
                }
            }

            return(null);
        }
Пример #4
0
        public FoundPage Find(MemorySlice key)
        {
            int position = current;

            int itemsLeft = _cacheSize;
            while ( itemsLeft > 0 )
            {
                var page = _cache[position % _cacheSize];
                if (page == null)
                {
                    itemsLeft--;
                    position++;

                    continue;
                }

                var first = page.FirstKey;
                var last = page.LastKey;

                switch (key.Options)
                {
                    case SliceOptions.Key:
                        if ((first.Options != SliceOptions.BeforeAllKeys && key.Compare(first) < 0))
                            break;
                        if (last.Options != SliceOptions.AfterAllKeys && key.Compare(last) > 0)
                            break;
                        return page;
                    case SliceOptions.BeforeAllKeys:
                        if (first.Options == SliceOptions.BeforeAllKeys)
                            return page;
                        break;
                    case SliceOptions.AfterAllKeys:
                        if (last.Options == SliceOptions.AfterAllKeys)
                            return page;
                        break;
                    default:
                        throw new ArgumentException(key.Options.ToString());
                }

                itemsLeft--;
                position++;
            }

            return null;
        }
Пример #5
0
        private NodeHeader* SearchPrefixed( MemorySlice key )
        {
            key.PrepareForSearching();

            int numberOfEntries = NumberOfEntries;
            if (numberOfEntries == 0)
            {
                LastSearchPosition = 0;
                LastMatch = 1;
                return null;
            }

            switch (key.Options)
            {
                case SliceOptions.Key:
                    {
                        var pageKey = CreateNewEmptyKey();

                        if (numberOfEntries == 1)
                        {
                            var node = GetNode(0);

                            SetNodeKey(node, ref pageKey);
                            LastMatch = key.Compare(pageKey);
                            LastSearchPosition = LastMatch > 0 ? 1 : 0;
                            return LastSearchPosition == 0 ? node : null;
                        }

                        int low = IsLeaf ? 0 : 1;
                        int high = numberOfEntries - 1;
                        int position = 0;

                        while (low <= high)
                        {
                            position = (low + high) >> 1;

                            var node = (NodeHeader*)(_base + KeysOffsets[position]);

                            SetNodeKey(node, ref pageKey);

                            LastMatch = key.Compare(pageKey);
                            if (LastMatch == 0)
                                break;

                            if (LastMatch > 0)
                                low = position + 1;
                            else
                                high = position - 1;
                        }

                        if (LastMatch > 0) // found entry less than key
                        {
                            position++; // move to the smallest entry larger than the key
                        }

                        Debug.Assert(position < ushort.MaxValue);
                        LastSearchPosition = position;

                        if (position >= numberOfEntries)
                            return null;
                        return GetNode(position);
                    }
                case SliceOptions.BeforeAllKeys:
                    {
                        LastSearchPosition = 0;
                        LastMatch = 1;
                        return GetNode(0);
                    }
                case SliceOptions.AfterAllKeys:
                    {
                        LastMatch = -1;
                        LastSearchPosition = numberOfEntries - 1;
                        return GetNode(LastSearchPosition);
                    }
                default:
                    throw new NotSupportedException("This SliceOptions is not supported. Make sure you have updated this code when adding a new one.");
            }
        }
Пример #6
0
        private NodeHeader *SearchPrefixed(MemorySlice key)
        {
            key.PrepareForSearching();

            int numberOfEntries = NumberOfEntries;

            if (numberOfEntries == 0)
            {
                LastSearchPosition = 0;
                LastMatch          = 1;
                return(null);
            }

            switch (key.Options)
            {
            case SliceOptions.Key:
            {
                var pageKey = CreateNewEmptyKey();

                if (numberOfEntries == 1)
                {
                    var node = GetNode(0);

                    SetNodeKey(node, ref pageKey);
                    LastMatch          = key.Compare(pageKey);
                    LastSearchPosition = LastMatch > 0 ? 1 : 0;
                    return(LastSearchPosition == 0 ? node : null);
                }

                int low      = IsLeaf ? 0 : 1;
                int high     = numberOfEntries - 1;
                int position = 0;

                while (low <= high)
                {
                    position = (low + high) >> 1;

                    var node = (NodeHeader *)(_base + KeysOffsets[position]);

                    SetNodeKey(node, ref pageKey);

                    LastMatch = key.Compare(pageKey);
                    if (LastMatch == 0)
                    {
                        break;
                    }

                    if (LastMatch > 0)
                    {
                        low = position + 1;
                    }
                    else
                    {
                        high = position - 1;
                    }
                }

                if (LastMatch > 0)      // found entry less than key
                {
                    position++;         // move to the smallest entry larger than the key
                }

                Debug.Assert(position < ushort.MaxValue);
                LastSearchPosition = position;

                if (position >= numberOfEntries)
                {
                    return(null);
                }
                return(GetNode(position));
            }

            case SliceOptions.BeforeAllKeys:
            {
                LastSearchPosition = 0;
                LastMatch          = 1;
                return(GetNode(0));
            }

            case SliceOptions.AfterAllKeys:
            {
                LastMatch          = -1;
                LastSearchPosition = numberOfEntries - 1;
                return(GetNode(LastSearchPosition));
            }

            default:
                throw new NotSupportedException("This SliceOptions is not supported. Make sure you have updated this code when adding a new one.");
            }
        }
Пример #7
0
        public NodeHeader *Search(MemorySlice key)
        {
            if (KeysPrefixed)
            {
                key.PrepareForSearching();
            }

            if (NumberOfEntries == 0)
            {
                LastSearchPosition = 0;
                LastMatch          = 1;
                return(null);
            }

            if (key.Options == SliceOptions.BeforeAllKeys)
            {
                LastSearchPosition = 0;
                LastMatch          = 1;
                return(GetNode(0));
            }

            if (key.Options == SliceOptions.AfterAllKeys)
            {
                LastMatch          = -1;
                LastSearchPosition = NumberOfEntries - 1;
                return(GetNode(LastSearchPosition));
            }

            var pageKey = CreateNewEmptyKey();

            if (NumberOfEntries == 1)
            {
                SetNodeKey(GetNode(0), ref pageKey);
                LastMatch          = key.Compare(pageKey);
                LastSearchPosition = LastMatch > 0 ? 1 : 0;
                return(LastSearchPosition == 0 ? GetNode(0) : null);
            }

            int low      = IsLeaf ? 0 : 1;
            int high     = NumberOfEntries - 1;
            int position = 0;

            while (low <= high)
            {
                position = (low + high) >> 1;

                var node = (NodeHeader *)(_base + KeysOffsets[position]);

                SetNodeKey(node, ref pageKey);

                LastMatch = key.Compare(pageKey);
                if (LastMatch == 0)
                {
                    break;
                }

                if (LastMatch > 0)
                {
                    low = position + 1;
                }
                else
                {
                    high = position - 1;
                }
            }

            if (LastMatch > 0)             // found entry less than key
            {
                position++;                // move to the smallest entry larger than the key
            }

            Debug.Assert(position < ushort.MaxValue);
            LastSearchPosition = position;

            if (position >= NumberOfEntries)
            {
                return(null);
            }
            return(GetNode(position));
        }
Пример #8
0
		public NodeHeader* Search(MemorySlice key)
		{
			if(KeysPrefixed)
				key.PrepareForSearching();

			if (NumberOfEntries == 0)
			{
				LastSearchPosition = 0;
				LastMatch = 1;
				return null;
			}

			if (key.Options == SliceOptions.BeforeAllKeys)
			{
				LastSearchPosition = 0;
				LastMatch = 1;
				return GetNode(0);
			}

			if (key.Options == SliceOptions.AfterAllKeys)
			{
				LastMatch = -1;
				LastSearchPosition = NumberOfEntries - 1;
				return GetNode(LastSearchPosition);
			}

			var pageKey = CreateNewEmptyKey();

			if (NumberOfEntries == 1)
			{
				SetNodeKey(GetNode(0), ref pageKey);
				LastMatch = key.Compare(pageKey);
				LastSearchPosition = LastMatch > 0 ? 1 : 0;
				return LastSearchPosition == 0 ? GetNode(0) : null;
			}

			int low = IsLeaf ? 0 : 1;
			int high = NumberOfEntries - 1;
			int position = 0;

			while (low <= high)
			{
				position = (low + high) >> 1;

				var node = (NodeHeader*)(_base + KeysOffsets[position]);

				SetNodeKey(node, ref pageKey);

				LastMatch = key.Compare(pageKey);
				if (LastMatch == 0)
					break;

				if (LastMatch > 0)
					low = position + 1;
				else
					high = position - 1;
			}

			if (LastMatch > 0) // found entry less than key
			{
				position++; // move to the smallest entry larger than the key
			}

			Debug.Assert(position < ushort.MaxValue);
			LastSearchPosition = position;

			if (position >= NumberOfEntries)
				return null;
			return GetNode(position);
		}