예제 #1
0
        public unsafe override SkipListNode GetNode(long nodeLocation)
        {
            var ptr    = (byte *)_pointer.ToPointer() + nodeLocation;
            var length = Unsafe.Read <int>(ptr);
            var node   = new SkipListNode(new Span <byte>(ptr, length), nodeLocation);

            return(node);
        }
예제 #2
0
        public SimpleAllocator(long maxSize, byte maxHeight)
            : base(maxHeight)
        {
            _maxSize = maxSize;
            _buffer.Add(null);

            var headNodeSize = SkipListNode.CalculateSizeNeeded(maxHeight, 0);

            _headNodeLocation = AllocateNode(headNodeSize, out var memory);
            _ = new SkipListNode(memory, _headNodeLocation, maxHeight, Array.Empty <byte>());
        }
예제 #3
0
        private SearchResult Search(ReadOnlySpan <byte> key, out SkipListNode node)
        {
            var currentNode = _allocator.HeadNode;
            var level       = (byte)(_allocator.CurrentHeight - 1);

            while (true)
            {
                var nextNodeLocation = currentNode.GetTableLocation(level);
                if (nextNodeLocation == 0)
                {
                    if (level > 0)
                    {
                        level--;
                        continue;
                    }
                    else
                    {
                        node = default;
                        return(SearchResult.NotFound);
                    }
                }
                node = _allocator.GetNode(nextNodeLocation);
                var compare = key.SequenceCompareTo(node.Key);
                if (compare == 0)
                {
                    if (node.IsDeleted)
                    {
                        return(SearchResult.Deleted);
                    }
                    else
                    {
                        return(SearchResult.Found);
                    }
                }
                else if (compare > 0)
                {
                    currentNode = node;
                    continue;
                }

                if (level > 0)
                {
                    level--;
                    continue;
                }

                return(SearchResult.NotFound);
            }
        }
예제 #4
0
        public SkipListNode AllocateNode(ReadOnlySpan <byte> key)
        {
            var height       = _heightGenerator.GetHeight();
            var memoryNeeded = SkipListNode.CalculateSizeNeeded(height, key.Length);
            var nodeLocation = AllocateNode(memoryNeeded, out var memory);

            if (nodeLocation == 0)
            {
                return(new SkipListNode());
            }

            var returnValue = new SkipListNode(memory, nodeLocation, height, key);

            return(returnValue);
        }
예제 #5
0
        public NativeAllocator(long maxSize, byte maxHeight)
            : base(maxHeight)
        {
            _maxSize = maxSize;
            _pointer = VirtualAlloc(IntPtr.Zero, (UIntPtr)maxSize, AllocationType.MEM_COMMIT | AllocationType.MEM_RESERVE, Protection.PAGE_READWRITE);
            if (_pointer == IntPtr.Zero)
            {
                var error = Marshal.GetLastWin32Error();
                throw new OutOfMemoryException($"We could not allocate memory for the skip list error code was {error}");
            }

            _currentPointer = ALIGNMENTSIZE;

            var headNodeSize = SkipListNode.CalculateSizeNeeded(maxHeight, 0);

            AllocateNode(headNodeSize, out var memory);
            _ = new SkipListNode(memory, ALIGNMENTSIZE, maxHeight, Array.Empty <byte>());
        }