Пример #1
0
        private void MoveBranchNode(Page parentPage, Page from, Page to)
        {
            Debug.Assert(from.IsBranch);
            var originalFromKeyStart = GetActualKey(from, from.LastSearchPositionOrLastEntry);

            var  fromNode   = from.GetNode(from.LastSearchPosition);
            long pageNum    = fromNode->PageNumber;
            var  itemsMoved = _tx.Pager.Get(_tx, pageNum).ItemCount;

            from.ItemCount -= itemsMoved;
            to.ItemCount   += itemsMoved;

            if (to.LastSearchPosition == 0)
            {
                // cannot add to left implicit side, adjust by moving the left node
                // to the right by one, then adding the new one as the left

                var implicitLeftKey = GetActualKey(to, 0);
                var leftPageNumber  = to.GetNode(0)->PageNumber;

                to.AddNode(1, implicitLeftKey, -1, leftPageNumber);
                to.AddNode(0, Slice.BeforeAllKeys, -1, pageNum);
                to.RemoveNode(1);
            }
            else
            {
                to.AddNode(to.LastSearchPosition, originalFromKeyStart, -1, pageNum);
            }

            if (from.LastSearchPositionOrLastEntry == 0)
            {
                // cannot just remove the left node, need to adjust those
                var rightPageNumber = from.GetNode(1)->PageNumber;
                from.RemoveNode(0); // remove the original node
                from.RemoveNode(0); // remove the next node
                from.AddNode(0, Slice.BeforeAllKeys, -1, rightPageNumber);
                Debug.Assert(from.NumberOfEntries >= 2);
            }
            else
            {
                from.RemoveNode(from.LastSearchPositionOrLastEntry);
            }

            var pos = parentPage.LastSearchPositionOrLastEntry;

            parentPage.RemoveNode(pos);
            var newKey     = GetActualKey(to, 0); // get the next smallest key it has now
            var pageNumber = to.PageNumber;

            if (parentPage.GetNode(0)->PageNumber == to.PageNumber)
            {
                pageNumber = from.PageNumber;
                newKey     = GetActualKey(from, 0);
            }

            parentPage.AddNode(pos, newKey, -1, pageNumber);
        }
Пример #2
0
        private void MoveBranchNode(Page parentPage, Page from, Page to)
        {
            Debug.Assert(from.IsBranch);
            var originalFromKeyStart = GetActualKey(from, from.LastSearchPositionOrLastEntry);

            to.EnsureHasSpaceFor(_tx, originalFromKeyStart, -1);

            var  fromNode = from.GetNode(from.LastSearchPosition);
            long pageNum  = fromNode->PageNumber;

            if (to.LastSearchPosition == 0)
            {
                // cannot add to left implicit side, adjust by moving the left node
                // to the right by one, then adding the new one as the left

                var implicitLeftKey = GetActualKey(to, 0);
                var leftPageNumber  = to.GetNode(0)->PageNumber;
                to.AddPageRefNode(1, implicitLeftKey, leftPageNumber);

                to.ChangeImplicitRefPageNode(pageNum);                 // setup the new implicit node
            }
            else
            {
                to.AddPageRefNode(to.LastSearchPosition, originalFromKeyStart, pageNum);
            }

            if (from.LastSearchPositionOrLastEntry == 0)
            {
                var rightPageNumber = from.GetNode(1)->PageNumber;
                from.RemoveNode(0);                              // remove the original implicit node
                from.ChangeImplicitRefPageNode(rightPageNumber); // setup the new implicit node
                Debug.Assert(from.NumberOfEntries >= 2);
            }
            else
            {
                from.RemoveNode(from.LastSearchPositionOrLastEntry);
            }

            var pos = parentPage.LastSearchPositionOrLastEntry;

            parentPage.RemoveNode(pos);
            var newKey     = GetActualKey(to, 0); // get the next smallest key it has now
            var pageNumber = to.PageNumber;

            if (parentPage.GetNode(0)->PageNumber == to.PageNumber)
            {
                pageNumber = from.PageNumber;
                newKey     = GetActualKey(from, 0);
            }
            parentPage.EnsureHasSpaceFor(_tx, newKey, -1);
            parentPage.AddPageRefNode(pos, newKey, pageNumber);
        }
Пример #3
0
        private void MoveLeafNode(Page parentPage, Page from, Page to)
        {
            Debug.Assert(from.IsBranch == false);
            var originalFromKeyStart = GetActualKey(from, from.LastSearchPositionOrLastEntry);

            var   fromNode = from.GetNode(from.LastSearchPosition);
            byte *val      = @from.Base + @from.KeysOffsets[@from.LastSearchPosition] + Constants.NodeHeaderSize + originalFromKeyStart.Size;
            var   dataPos  = to.AddNode(to.LastSearchPosition, originalFromKeyStart, fromNode->DataSize, -1);

            NativeMethods.memcpy(dataPos, val, fromNode->DataSize);
            [email protected];
            ++to.ItemCount;

            from.RemoveNode(from.LastSearchPositionOrLastEntry);

            var pos = parentPage.LastSearchPositionOrLastEntry;

            parentPage.RemoveNode(pos);

            var newKey     = GetActualKey(to, 0); // get the next smallest key it has now
            var pageNumber = to.PageNumber;

            if (parentPage.GetNode(0)->PageNumber == to.PageNumber)
            {
                pageNumber = from.PageNumber;
                newKey     = GetActualKey(from, 0);
            }

            parentPage.AddNode(pos, newKey, -1, pageNumber);
        }
Пример #4
0
        private void MoveLeafNode(Page parentPage, Page from, Page to)
        {
            Debug.Assert(from.IsBranch == false);
            var originalFromKeyStart = GetActualKey(from, from.LastSearchPositionOrLastEntry);

            var   fromNode = from.GetNode(from.LastSearchPosition);
            byte *val      = @from.Base + @from.KeysOffsets[@from.LastSearchPosition] + Constants.NodeHeaderSize + originalFromKeyStart.Size;

            var nodeVersion = fromNode->Version;             // every time new node is allocated the version is increased, but in this case we do not want to increase it

            if (nodeVersion > 0)
            {
                nodeVersion -= 1;
            }

            byte *dataPos;

            switch (fromNode->Flags)
            {
            case NodeFlags.PageRef:
                to.EnsureHasSpaceFor(_tx, originalFromKeyStart, -1);
                dataPos = to.AddPageRefNode(to.LastSearchPosition, originalFromKeyStart, fromNode->PageNumber);
                break;

            case NodeFlags.Data:
                to.EnsureHasSpaceFor(_tx, originalFromKeyStart, fromNode->DataSize);
                dataPos = to.AddDataNode(to.LastSearchPosition, originalFromKeyStart, fromNode->DataSize, nodeVersion);
                break;

            case NodeFlags.MultiValuePageRef:
                to.EnsureHasSpaceFor(_tx, originalFromKeyStart, fromNode->DataSize);
                dataPos = to.AddMultiValueNode(to.LastSearchPosition, originalFromKeyStart, fromNode->DataSize, nodeVersion);
                break;

            default:
                throw new NotSupportedException("Invalid node type to move: " + fromNode->Flags);
            }

            if (dataPos != null)
            {
                NativeMethods.memcpy(dataPos, val, fromNode->DataSize);
            }

            from.RemoveNode(from.LastSearchPositionOrLastEntry);

            var pos = parentPage.LastSearchPositionOrLastEntry;

            parentPage.RemoveNode(pos);

            var newKey     = GetActualKey(to, 0); // get the next smallest key it has now
            var pageNumber = to.PageNumber;

            if (parentPage.GetNode(0)->PageNumber == to.PageNumber)
            {
                pageNumber = from.PageNumber;
                newKey     = GetActualKey(from, 0);
            }
            parentPage.EnsureHasSpaceFor(_tx, newKey, -1);
            parentPage.AddPageRefNode(pos, newKey, pageNumber);
        }
Пример #5
0
        public byte *Execute()
        {
            var rightPage = Tree.NewPage(_tx, _page.Flags, 1);

            _txInfo.RecordNewPage(_page, 1);
            rightPage.Flags = _page.Flags;
            if (_cursor.PageCount == 0) // we need to do a root split
            {
                var newRootPage = Tree.NewPage(_tx, PageFlags.Branch, 1);
                _cursor.Push(newRootPage);
                _txInfo.RootPageNumber = newRootPage.PageNumber;
                _txInfo.State.Depth++;
                _txInfo.RecordNewPage(newRootPage, 1);

                // now add implicit left page
                newRootPage.AddNode(0, Slice.BeforeAllKeys, -1, _page.PageNumber);
                _parentPage = newRootPage;
                _parentPage.LastSearchPosition++;
                _parentPage.ItemCount = _page.ItemCount;
            }
            else
            {
                // we already popped the page, so the current one on the stack is what the parent of the page
                _parentPage = _cursor.CurrentPage;
            }

            if (_page.LastSearchPosition >= _page.NumberOfEntries)
            {
                // when we get a split at the end of the page, we take that as a hint that the user is doing
                // sequential inserts, at that point, we are going to keep the current page as is and create a new
                // page, this will allow us to do minimal amount of work to get the best density

                byte *pos;
                if (_page.IsBranch)
                {
                    // here we steal the last entry from the current page so we maintain the implicit null left entry
                    var node = _page.GetNode(_page.NumberOfEntries - 1);
                    Debug.Assert(node->Flags == NodeFlags.PageRef);
                    var itemsMoved = _tx.Pager.Get(_tx, node->PageNumber).ItemCount;
                    rightPage.AddNode(0, Slice.Empty, -1, node->PageNumber);
                    pos = rightPage.AddNode(1, _newKey, _len, _pageNumber);
                    rightPage.ItemCount = itemsMoved;

                    AddSeperatorToParentPage(rightPage, new Slice(node));

                    _page.RemoveNode(_page.NumberOfEntries - 1);
                    _page.ItemCount -= itemsMoved;
                }
                else
                {
                    AddSeperatorToParentPage(rightPage, _newKey);
                    pos = rightPage.AddNode(0, _newKey, _len, _pageNumber);
                }
                _cursor.Push(rightPage);
                IncrementItemCountIfNecessary();
                return(pos);
            }

            return(SplitPageInHalf(rightPage));
        }
Пример #6
0
        private void RemoveLeafNode(Page page, out ushort nodeVersion)
        {
            var node = page.GetNode(page.LastSearchPosition);

            nodeVersion = node->Version;
            if (node->Flags == (NodeFlags.PageRef))             // this is an overflow pointer
            {
                var overflowPage = _tx.GetReadOnlyPage(node->PageNumber);
                FreePage(overflowPage);
            }

            page.RemoveNode(page.LastSearchPosition);
        }
Пример #7
0
 private void MergePages(Page parentPage, Page left, Page right)
 {
     for (int i = 0; i < right.NumberOfEntries; i++)
     {
         right.LastSearchPosition = i;
         var key  = GetActualKey(right, right.LastSearchPositionOrLastEntry);
         var node = right.GetNode(i);
         left.CopyNodeDataToEndOfPage(node, key);
     }
     left.ItemCount += right.ItemCount;
     parentPage.RemoveNode(parentPage.LastSearchPositionOrLastEntry); // unlink the right sibling
     _tx.FreePage(right.PageNumber);
 }
Пример #8
0
        private void RemoveLeafNode(Page page, out ushort nodeVersion)
        {
            var node = page.GetNode(page.LastSearchPosition);

            nodeVersion = node->Version;
            if (node->Flags == (NodeFlags.PageRef)) // this is an overflow pointer
            {
                var overflowPage  = _tx.GetReadOnlyPage(node->PageNumber);
                var numberOfPages = _tx.DataPager.GetNumberOfOverflowPages(overflowPage.OverflowSize);
                for (int i = 0; i < numberOfPages; i++)
                {
                    _tx.FreePage(overflowPage.PageNumber + i);
                }

                State.OverflowPages -= numberOfPages;
                State.PageCount     -= numberOfPages;
            }
            page.RemoveNode(page.LastSearchPosition);
        }
Пример #9
0
        private void RemoveLeafNode(Transaction tx, Cursor cursor, Page page)
        {
            var node = page.GetNode(page.LastSearchPosition);

            if (node->Flags == (NodeFlags.PageRef)) // this is an overflow pointer
            {
                var overflowPage  = tx.GetReadOnlyPage(node->PageNumber);
                var numberOfPages = GetNumberOfOverflowPages(tx, overflowPage.OverflowSize);
                for (int i = 0; i < numberOfPages; i++)
                {
                    tx.FreePage(overflowPage.PageNumber + i);
                }
                var txInfo = tx.GetTreeInformation(this);

                txInfo.State.OverflowPages -= numberOfPages;
                txInfo.State.PageCount     -= numberOfPages;
            }
            page.RemoveNode(page.LastSearchPosition);
            cursor.DecrementItemCount();
        }
Пример #10
0
        private bool TryMergePages(Page parentPage, Page left, Page right)
        {
            TemporaryPage tmp;

            using (_tx.Environment.GetTemporaryPage(_tx, out tmp))
            {
                var mergedPage = tmp.GetTempPage(left.KeysPrefixed);
                Memory.Copy(mergedPage.Base, left.Base, left.PageSize);

                var previousSearchPosition = right.LastSearchPosition;

                for (int i = 0; i < right.NumberOfEntries; i++)
                {
                    right.LastSearchPosition = i;
                    var key  = GetActualKey(right, right.LastSearchPositionOrLastEntry);
                    var node = right.GetNode(i);

                    var prefixedKey = mergedPage.PrepareKeyToInsert(key, mergedPage.NumberOfEntries);

                    if (mergedPage.HasSpaceFor(_tx, SizeOf.NodeEntryWithAnotherKey(node, prefixedKey) + Constants.NodeOffsetSize + SizeOf.NewPrefix(prefixedKey)) == false)
                    {
                        right.LastSearchPosition = previousSearchPosition;                         //previous position --> prevent mutation of parameter
                        return(false);
                    }

                    mergedPage.CopyNodeDataToEndOfPage(node, prefixedKey);
                }

                Memory.Copy(left.Base, mergedPage.Base, left.PageSize);
            }

            parentPage.RemoveNode(parentPage.LastSearchPositionOrLastEntry);             // unlink the right sibling
            _tree.FreePage(right);

            return(true);
        }
Пример #11
0
        public byte *Execute()
        {
            Page rightPage = _tree.NewPage(_page.Flags, 1);

            if (_cursor.PageCount == 0) // we need to do a root split
            {
                Page newRootPage = _tree.NewPage(_tree.KeysPrefixing ? PageFlags.Branch | PageFlags.KeysPrefixed : PageFlags.Branch, 1);
                _cursor.Push(newRootPage);
                _treeState.RootPageNumber = newRootPage.PageNumber;
                _treeState.Depth++;

                // now add implicit left page
                newRootPage.AddPageRefNode(0, _tree.KeysPrefixing ? (MemorySlice)PrefixedSlice.BeforeAllKeys : Slice.BeforeAllKeys, _page.PageNumber);
                _parentPage = newRootPage;
                _parentPage.LastSearchPosition++;
            }
            else
            {
                // we already popped the page, so the current one on the stack is the parent of the page

                if (_tree.Name == Constants.FreeSpaceTreeName)
                {
                    // a special case for FreeSpaceTree because the allocation of a new page called above
                    // can cause a delete of a free space section resulting in a run of the tree rebalancer
                    // and here the parent page that exists in cursor can be outdated

                    _parentPage = _tx.ModifyPage(_cursor.CurrentPage.PageNumber, _tree, null); // pass _null_ to make sure we'll get the most updated parent page
                    _parentPage.LastSearchPosition = _cursor.CurrentPage.LastSearchPosition;
                    _parentPage.LastMatch          = _cursor.CurrentPage.LastMatch;
                }
                else
                {
                    _parentPage = _tx.ModifyPage(_cursor.CurrentPage.PageNumber, _tree, _cursor.CurrentPage);
                }

                _cursor.Update(_cursor.Pages.First, _parentPage);
            }

            if (_page.IsLeaf)
            {
                _tree.ClearRecentFoundPages();
            }

            if (_tree.Name == Constants.FreeSpaceTreeName)
            {
                // we need to refresh the LastSearchPosition of the split page which is used by the free space handling
                // because the allocation of a new page called above could remove some sections
                // from the page that is being split

                _page.NodePositionFor(_newKey);
            }

            if (_page.LastSearchPosition >= _page.NumberOfEntries)
            {
                // when we get a split at the end of the page, we take that as a hint that the user is doing
                // sequential inserts, at that point, we are going to keep the current page as is and create a new
                // page, this will allow us to do minimal amount of work to get the best density

                Page branchOfSeparator;

                byte *pos;
                if (_page.IsBranch)
                {
                    if (_page.NumberOfEntries > 2)
                    {
                        // here we steal the last entry from the current page so we maintain the implicit null left entry

                        NodeHeader *node = _page.GetNode(_page.NumberOfEntries - 1);
                        Debug.Assert(node->Flags == NodeFlags.PageRef);
                        rightPage.AddPageRefNode(0, _tree.KeysPrefixing ? (MemorySlice)PrefixedSlice.BeforeAllKeys : Slice.BeforeAllKeys, node->PageNumber);
                        pos = AddNodeToPage(rightPage, 1);

                        var separatorKey = _page.GetNodeKey(node);

                        AddSeparatorToParentPage(rightPage.PageNumber, separatorKey, out branchOfSeparator);

                        _page.RemoveNode(_page.NumberOfEntries - 1);
                    }
                    else
                    {
                        _tree.FreePage(rightPage); // return the unnecessary right page
                        pos = AddSeparatorToParentPage(_pageNumber, _newKey, out branchOfSeparator);

                        if (_cursor.CurrentPage.PageNumber != branchOfSeparator.PageNumber)
                        {
                            _cursor.Push(branchOfSeparator);
                        }

                        return(pos);
                    }
                }
                else
                {
                    AddSeparatorToParentPage(rightPage.PageNumber, _newKey, out branchOfSeparator);
                    pos = AddNodeToPage(rightPage, 0);
                }
                _cursor.Push(rightPage);
                return(pos);
            }

            return(SplitPageInHalf(rightPage));
        }
Пример #12
0
		public void MultiAdd(Slice key, Slice value, ushort? version = null)
		{
			if (value == null) throw new ArgumentNullException("value");
			int maxNodeSize = _tx.DataPager.MaxNodeSize;
			if (value.Size > maxNodeSize)
				throw new ArgumentException(
					"Cannot add a value to child tree that is over " + maxNodeSize + " bytes in size", "value");
			if (value.Size == 0)
				throw new ArgumentException("Cannot add empty value to child tree");

			State.IsModified = true;

			Lazy<Cursor> lazy;
			var page = FindPageFor(key, out lazy);
			if ((page == null || page.LastMatch != 0))
			{
				MultiAddOnNewValue(_tx, key, value, version, maxNodeSize);
				return;
			}

			page = _tx.ModifyPage(page.PageNumber, page);

			var item = page.GetNode(page.LastSearchPosition);

			// already was turned into a multi tree, not much to do here
			if (item->Flags == NodeFlags.MultiValuePageRef)
			{
				var existingTree = OpenOrCreateMultiValueTree(_tx, key, item);
				existingTree.DirectAdd(value, 0, version: version);
				return;
			}

			byte* nestedPagePtr;
			if (item->Flags == NodeFlags.PageRef)
			{
				var overFlowPage = _tx.ModifyPage(item->PageNumber, null);
				nestedPagePtr = overFlowPage.Base + Constants.PageHeaderSize;
			}
			else
			{
				nestedPagePtr = NodeHeader.DirectAccess(_tx, item);
			}

			var nestedPage = new Page(nestedPagePtr, "multi tree", (ushort)NodeHeader.GetDataSize(_tx, item));

			var existingItem = nestedPage.Search(value);
			if (nestedPage.LastMatch != 0)
				existingItem = null;// not an actual match, just greater than

			ushort previousNodeRevision = existingItem != null ?  existingItem->Version : (ushort)0;
			CheckConcurrency(key, value, version, previousNodeRevision, TreeActionType.Add);
			
			if (existingItem != null)
			{
				// maybe same value added twice?
				var tmpKey = new Slice(item);
				if (tmpKey.Compare(value) == 0)
					return; // already there, turning into a no-op
				nestedPage.RemoveNode(nestedPage.LastSearchPosition);
			}

			if (nestedPage.HasSpaceFor(_tx, value, 0))
			{
				// we are now working on top of the modified root page, we can just modify the memory directly
				nestedPage.AddDataNode(nestedPage.LastSearchPosition, value, 0, previousNodeRevision);
				return;
			}

			int pageSize = nestedPage.CalcSizeUsed() + Constants.PageHeaderSize;
			var newRequiredSize = pageSize + nestedPage.GetRequiredSpace(value, 0);
			if (newRequiredSize <= maxNodeSize)
			{
				// we can just expand the current value... no need to create a nested tree yet
				var actualPageSize = (ushort)Math.Min(Utils.NearestPowerOfTwo(newRequiredSize), maxNodeSize);
				ExpandMultiTreeNestedPageSize(_tx, key, value, nestedPagePtr, actualPageSize, item->DataSize);

				return;
			}
			// we now have to convert this into a tree instance, instead of just a nested page
			var tree = Create(_tx, TreeFlags.MultiValue);
			for (int i = 0; i < nestedPage.NumberOfEntries; i++)
			{
				var existingValue = nestedPage.GetNodeKey(i);
				tree.DirectAdd(existingValue, 0);
			}
			tree.DirectAdd(value, 0, version: version);
			_tx.AddMultiValueTree(this, key, tree);
			// we need to record that we switched to tree mode here, so the next call wouldn't also try to create the tree again
			DirectAdd(key, sizeof (TreeRootHeader), NodeFlags.MultiValuePageRef);
		}
Пример #13
0
		public void MultiDelete(Slice key, Slice value, ushort? version = null)
		{
			State.IsModified = true;
			Lazy<Cursor> lazy;
			var page = FindPageFor(key, out lazy);
			if (page == null || page.LastMatch != 0)
			{
				return; //nothing to delete - key not found
			}

			page = _tx.ModifyPage(page.PageNumber, page);

			var item = page.GetNode(page.LastSearchPosition);

			if (item->Flags == NodeFlags.MultiValuePageRef) //multi-value tree exists
			{
				var tree = OpenOrCreateMultiValueTree(_tx, key, item);

				tree.Delete(value, version);

				// previously, we would convert back to a simple model if we dropped to a single entry
				// however, it doesn't really make sense, once you got enough values to go to an actual nested 
				// tree, you are probably going to remain that way, or be removed completely.
				if (tree.State.EntriesCount != 0) 
					return;
				_tx.TryRemoveMultiValueTree(this, key);
				_tx.FreePage(tree.State.RootPageNumber);
				Delete(key);
			}
			else // we use a nested page here
			{
				var nestedPage = new Page(NodeHeader.DirectAccess(_tx, item), "multi tree", (ushort)NodeHeader.GetDataSize(_tx, item));
				var nestedItem = nestedPage.Search(value);
				if (nestedItem == null) // value not found
					return;

				byte* nestedPagePtr;
				if (item->Flags == NodeFlags.PageRef)
				{
					var overFlowPage = _tx.ModifyPage(item->PageNumber, null);
					nestedPagePtr = overFlowPage.Base + Constants.PageHeaderSize;
				}
				else
				{
					nestedPagePtr = NodeHeader.DirectAccess(_tx, item);
				}

				nestedPage = new Page(nestedPagePtr, "multi tree", (ushort)NodeHeader.GetDataSize(_tx, item))
				{
					LastSearchPosition = nestedPage.LastSearchPosition
				};

				CheckConcurrency(key, value, version, nestedItem->Version, TreeActionType.Delete);
				nestedPage.RemoveNode(nestedPage.LastSearchPosition);
				if (nestedPage.NumberOfEntries == 0)
					Delete(key);
			}
		}
Пример #14
0
        private void MoveBranchNode(Page parentPage, Page from, Page to)
        {
            Debug.Assert(from.IsBranch);

	        var originalFromKey = to.PrepareKeyToInsert(GetActualKey(from, from.LastSearchPositionOrLastEntry), to.LastSearchPosition);

            to.EnsureHasSpaceFor(_tx, originalFromKey, -1);

            var fromNode = from.GetNode(from.LastSearchPosition);
            long pageNum = fromNode->PageNumber;

            if (to.LastSearchPosition == 0)
            {
                // cannot add to left implicit side, adjust by moving the left node
                // to the right by one, then adding the new one as the left

	            NodeHeader* actualKeyNode;
                var implicitLeftKey = GetActualKey(to, 0, out actualKeyNode);
	            var implicitLeftNode = to.GetNode(0);
				var leftPageNumber = implicitLeftNode->PageNumber;

	            MemorySlice implicitLeftKeyToInsert;

	            if (implicitLeftNode == actualKeyNode)
	            {
					// no need to create a prefix, just use the existing prefixed key from the node
					// this also prevents from creating a prefix which is the full key given in 'implicitLeftKey'

		            if (_tree.KeysPrefixing)
			            implicitLeftKeyToInsert = new PrefixedSlice(actualKeyNode);
		            else
			            implicitLeftKeyToInsert = new Slice(actualKeyNode);
	            }
				else
					implicitLeftKeyToInsert = to.PrepareKeyToInsert(implicitLeftKey, 1);
	            
				to.EnsureHasSpaceFor(_tx, implicitLeftKeyToInsert, -1);
				to.AddPageRefNode(1, implicitLeftKeyToInsert, leftPageNumber);

				to.ChangeImplicitRefPageNode(pageNum); // setup the new implicit node
            }
            else
            {
				to.AddPageRefNode(to.LastSearchPosition, originalFromKey, pageNum);
            }

            if (from.LastSearchPositionOrLastEntry == 0)
            {
                var rightPageNumber = from.GetNode(1)->PageNumber;
                from.RemoveNode(0); // remove the original implicit node
                from.ChangeImplicitRefPageNode(rightPageNumber); // setup the new implicit node
                Debug.Assert(from.NumberOfEntries >= 2);
            }
            else
            {
                from.RemoveNode(from.LastSearchPositionOrLastEntry);
            }

            var pos = parentPage.LastSearchPositionOrLastEntry;
            parentPage.RemoveNode(pos);
            var newSeparatorKey = GetActualKey(to, 0); // get the next smallest key it has now
            var pageNumber = to.PageNumber;
            if (parentPage.GetNode(0)->PageNumber == to.PageNumber)
            {
                pageNumber = from.PageNumber;
                newSeparatorKey = GetActualKey(from, 0);
            }

			AddSeparatorToParentPage(parentPage, pageNumber, newSeparatorKey, pos);
        }
Пример #15
0
        private void MoveLeafNode(Page parentPage, Page from, Page to)
        {
            Debug.Assert(from.IsBranch == false);
            var originalFromKeyStart = GetActualKey(from, from.LastSearchPositionOrLastEntry);

            var fromNode = from.GetNode(from.LastSearchPosition);
			byte* val = @from.Base + @from.KeysOffsets[@from.LastSearchPosition] + Constants.NodeHeaderSize + originalFromKeyStart.Size;

			var nodeVersion = fromNode->Version; // every time new node is allocated the version is increased, but in this case we do not want to increase it
			if (nodeVersion > 0)
				nodeVersion -= 1;

	        var prefixedOriginalFromKey = to.PrepareKeyToInsert(originalFromKeyStart, to.LastSearchPosition);

	        byte* dataPos;
	        var fromDataSize = fromNode->DataSize;
	        switch (fromNode->Flags)
	        {
				case NodeFlags.PageRef:
					to.EnsureHasSpaceFor(_tx, prefixedOriginalFromKey, -1);
					dataPos = to.AddPageRefNode(to.LastSearchPosition, prefixedOriginalFromKey, fromNode->PageNumber);
					break;
				case NodeFlags.Data:
					to.EnsureHasSpaceFor(_tx, prefixedOriginalFromKey, fromDataSize);
					dataPos = to.AddDataNode(to.LastSearchPosition, prefixedOriginalFromKey, fromDataSize, nodeVersion);
					break;
				case NodeFlags.MultiValuePageRef:
					to.EnsureHasSpaceFor(_tx, prefixedOriginalFromKey, fromDataSize);
					dataPos = to.AddMultiValueNode(to.LastSearchPosition, prefixedOriginalFromKey, fromDataSize, nodeVersion);
					break;
				default:
			        throw new NotSupportedException("Invalid node type to move: " + fromNode->Flags);
	        }
			
			if(dataPos != null && fromDataSize > 0)
                Memory.Copy(dataPos, val, fromDataSize);
            
            from.RemoveNode(from.LastSearchPositionOrLastEntry);

            var pos = parentPage.LastSearchPositionOrLastEntry;
            parentPage.RemoveNode(pos);

            var newSeparatorKey = GetActualKey(to, 0); // get the next smallest key it has now
            var pageNumber = to.PageNumber;
            if (parentPage.GetNode(0)->PageNumber == to.PageNumber)
            {
                pageNumber = from.PageNumber;
                newSeparatorKey = GetActualKey(from, 0);
            }

			AddSeparatorToParentPage(parentPage, pageNumber, newSeparatorKey, pos);
        }
Пример #16
0
		private bool TryMergePages(Page parentPage, Page left, Page right)
		{
			TemporaryPage tmp;
			using (_tx.Environment.GetTemporaryPage(_tx, out tmp))
			{
				var mergedPage = tmp.GetTempPage(left.KeysPrefixed);
                Memory.Copy(mergedPage.Base, left.Base, left.PageSize);

				var previousSearchPosition = right.LastSearchPosition;

				for (int i = 0; i < right.NumberOfEntries; i++)
				{
					right.LastSearchPosition = i;
					var key = GetActualKey(right, right.LastSearchPositionOrLastEntry);
					var node = right.GetNode(i);

					var prefixedKey = mergedPage.PrepareKeyToInsert(key, mergedPage.NumberOfEntries);

					if (mergedPage.HasSpaceFor(_tx, SizeOf.NodeEntryWithAnotherKey(node, prefixedKey) + Constants.NodeOffsetSize + SizeOf.NewPrefix(prefixedKey)) == false)
					{
						right.LastSearchPosition = previousSearchPosition; //previous position --> prevent mutation of parameter
						return false;
					}

					mergedPage.CopyNodeDataToEndOfPage(node, prefixedKey);
				}

                Memory.Copy(left.Base, mergedPage.Base, left.PageSize);
			}

			parentPage.RemoveNode(parentPage.LastSearchPositionOrLastEntry); // unlink the right sibling
			_tx.FreePage(right.PageNumber);

			return true;
		}
Пример #17
0
		private void RemoveLeafNode(Transaction tx, Page page, out ushort nodeVersion)
		{
			var node = page.GetNode(page.LastSearchPosition);
			nodeVersion = node->Version;
			if (node->Flags == (NodeFlags.PageRef)) // this is an overflow pointer
			{
				var overflowPage = tx.GetReadOnlyPage(node->PageNumber);
				var numberOfPages = tx.DataPager.GetNumberOfOverflowPages(overflowPage.OverflowSize);
				for (int i = 0; i < numberOfPages; i++)
				{
					tx.FreePage(overflowPage.PageNumber + i);
				}

				State.OverflowPages -= numberOfPages;
				State.PageCount -= numberOfPages;
			}
			page.RemoveNode(page.LastSearchPosition);
		}
Пример #18
0
        public byte *Execute()
        {
            using (_tree.IsFreeSpaceTree ? _tx.Environment.FreeSpaceHandling.Disable() : null)
            {
                Page rightPage = _tree.NewPage(_page.Flags, 1);

                if (_cursor.PageCount == 0) // we need to do a root split
                {
                    Page newRootPage = _tree.NewPage(_tree.KeysPrefixing ? PageFlags.Branch | PageFlags.KeysPrefixed : PageFlags.Branch, 1);
                    _cursor.Push(newRootPage);
                    _tree.State.RootPageNumber = newRootPage.PageNumber;
                    _tree.State.Depth++;

                    // now add implicit left page
                    newRootPage.AddPageRefNode(0, _tree.KeysPrefixing ? (MemorySlice)PrefixedSlice.BeforeAllKeys : Slice.BeforeAllKeys, _page.PageNumber);
                    _parentPage = newRootPage;
                    _parentPage.LastSearchPosition++;
                }
                else
                {
                    // we already popped the page, so the current one on the stack is the parent of the page

                    _parentPage = _tx.ModifyPage(_cursor.CurrentPage.PageNumber, _tree, _cursor.CurrentPage);

                    _cursor.Update(_cursor.Pages.First, _parentPage);
                }

                if (_page.IsLeaf)
                {
                    _tree.ClearRecentFoundPages();
                }

                if (_page.LastSearchPosition >= _page.NumberOfEntries)
                {
                    // when we get a split at the end of the page, we take that as a hint that the user is doing
                    // sequential inserts, at that point, we are going to keep the current page as is and create a new
                    // page, this will allow us to do minimal amount of work to get the best density

                    Page branchOfSeparator;

                    byte *pos;
                    if (_page.IsBranch)
                    {
                        if (_page.NumberOfEntries > 2)
                        {
                            // here we steal the last entry from the current page so we maintain the implicit null left entry

                            NodeHeader *node = _page.GetNode(_page.NumberOfEntries - 1);
                            Debug.Assert(node->Flags == NodeFlags.PageRef);
                            rightPage.AddPageRefNode(0, _tree.KeysPrefixing ? (MemorySlice)PrefixedSlice.BeforeAllKeys : Slice.BeforeAllKeys, node->PageNumber);
                            pos = AddNodeToPage(rightPage, 1);

                            var separatorKey = _page.GetNodeKey(node);

                            AddSeparatorToParentPage(rightPage.PageNumber, separatorKey, out branchOfSeparator);

                            _page.RemoveNode(_page.NumberOfEntries - 1);
                        }
                        else
                        {
                            _tree.FreePage(rightPage); // return the unnecessary right page
                            pos = AddSeparatorToParentPage(_pageNumber, _newKey, out branchOfSeparator);

                            if (_cursor.CurrentPage.PageNumber != branchOfSeparator.PageNumber)
                            {
                                _cursor.Push(branchOfSeparator);
                            }

                            return(pos);
                        }
                    }
                    else
                    {
                        AddSeparatorToParentPage(rightPage.PageNumber, _newKey, out branchOfSeparator);
                        pos = AddNodeToPage(rightPage, 0);
                    }
                    _cursor.Push(rightPage);
                    return(pos);
                }

                return(SplitPageInHalf(rightPage));
            }
        }
Пример #19
0
        private void MergePages(Page parentPage, Page left, Page right)
        {
            for (int i = 0; i < right.NumberOfEntries; i++)
            {
                right.LastSearchPosition = i;
                var key = GetActualKey(right, right.LastSearchPositionOrLastEntry);
                var node = right.GetNode(i);

                left.CopyNodeDataToEndOfPage(node, key);
            }

            parentPage.RemoveNode(parentPage.LastSearchPositionOrLastEntry); // unlink the right sibling
            _tx.FreePage(right.PageNumber);
        }
Пример #20
0
        private void MoveBranchNode(Page parentPage, Page from, Page to)
        {
            Debug.Assert(from.IsBranch);
            var originalFromKeyStart = GetActualKey(from, from.LastSearchPositionOrLastEntry);

            to.EnsureHasSpaceFor(_tx, originalFromKeyStart, -1);

            var fromNode = from.GetNode(from.LastSearchPosition);
            long pageNum = fromNode->PageNumber;

            if (to.LastSearchPosition == 0)
            {
                // cannot add to left implicit side, adjust by moving the left node
                // to the right by one, then adding the new one as the left

                var implicitLeftKey = GetActualKey(to, 0);
                var leftPageNumber = to.GetNode(0)->PageNumber;
                to.AddPageRefNode(1, implicitLeftKey, leftPageNumber);
                to.AddPageRefNode(0, Slice.BeforeAllKeys, pageNum);
                to.RemoveNode(1);
            }
            else
            {
				to.AddPageRefNode(to.LastSearchPosition, originalFromKeyStart, pageNum);
            }

            if (from.LastSearchPositionOrLastEntry == 0)
            {
                // cannot just remove the left node, need to adjust those
                var rightPageNumber = from.GetNode(1)->PageNumber;
                from.RemoveNode(0); // remove the original implicit node
                from.RemoveNode(0); // remove the next node that we now turned into implicit
                from.EnsureHasSpaceFor(_tx, Slice.BeforeAllKeys, -1);
                from.AddPageRefNode(0, Slice.BeforeAllKeys, rightPageNumber);
                Debug.Assert(from.NumberOfEntries >= 2);
            }
            else
            {
                from.RemoveNode(from.LastSearchPositionOrLastEntry);
            }

            var pos = parentPage.LastSearchPositionOrLastEntry;
            parentPage.RemoveNode(pos);
            var newKey = GetActualKey(to, 0); // get the next smallest key it has now
            var pageNumber = to.PageNumber;
            if (parentPage.GetNode(0)->PageNumber == to.PageNumber)
            {
                pageNumber = from.PageNumber;
                newKey = GetActualKey(from, 0);
            }
            parentPage.EnsureHasSpaceFor(_tx, newKey, -1);
			parentPage.AddPageRefNode(pos, newKey, pageNumber);
        }
Пример #21
0
        public void MultiDelete(Slice key, Slice value, ushort?version = null)
        {
            State.IsModified = true;
            Lazy <Cursor> lazy;
            NodeHeader *  node;
            var           page = FindPageFor(key, out node, out lazy);

            if (page == null || page.LastMatch != 0)
            {
                return;                 //nothing to delete - key not found
            }

            page = _tx.ModifyPage(page.PageNumber, page);

            var item = page.GetNode(page.LastSearchPosition);

            if (item->Flags == NodeFlags.MultiValuePageRef)             //multi-value tree exists
            {
                var tree = OpenMultiValueTree(_tx, key, item);

                tree.Delete(value, version);

                // previously, we would convert back to a simple model if we dropped to a single entry
                // however, it doesn't really make sense, once you got enough values to go to an actual nested
                // tree, you are probably going to remain that way, or be removed completely.
                if (tree.State.EntriesCount != 0)
                {
                    return;
                }
                _tx.TryRemoveMultiValueTree(this, key);
                _tx.FreePage(tree.State.RootPageNumber);
                Delete(key);
            }
            else             // we use a nested page here
            {
                var nestedPage = new Page(NodeHeader.DirectAccess(_tx, item), "multi tree", (ushort)NodeHeader.GetDataSize(_tx, item));
                var nestedItem = nestedPage.Search(value);
                if (nestedItem == null)                 // value not found
                {
                    return;
                }

                byte *nestedPagePtr;
                if (item->Flags == NodeFlags.PageRef)
                {
                    var overFlowPage = _tx.ModifyPage(item->PageNumber, null);
                    nestedPagePtr = overFlowPage.Base + Constants.PageHeaderSize;
                }
                else
                {
                    nestedPagePtr = NodeHeader.DirectAccess(_tx, item);
                }

                nestedPage = new Page(nestedPagePtr, "multi tree", (ushort)NodeHeader.GetDataSize(_tx, item))
                {
                    LastSearchPosition = nestedPage.LastSearchPosition
                };

                CheckConcurrency(key, value, version, nestedItem->Version, TreeActionType.Delete);
                nestedPage.RemoveNode(nestedPage.LastSearchPosition);
                if (nestedPage.NumberOfEntries == 0)
                {
                    Delete(key);
                }
            }
        }
Пример #22
0
        public void MultiAdd(Slice key, Slice value, ushort?version = null)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            int maxNodeSize = AbstractPager.NodeMaxSize;

            if (value.Size > maxNodeSize)
            {
                throw new ArgumentException(
                          "Cannot add a value to child tree that is over " + maxNodeSize + " bytes in size", "value");
            }
            if (value.Size == 0)
            {
                throw new ArgumentException("Cannot add empty value to child tree");
            }

            State.IsModified = true;

            Lazy <Cursor> lazy;
            NodeHeader *  node;
            var           page = FindPageFor(key, out node, out lazy);

            if ((page == null || page.LastMatch != 0))
            {
                MultiAddOnNewValue(_tx, key, value, version, maxNodeSize);
                return;
            }

            page = _tx.ModifyPage(page.PageNumber, page);
            var item = page.GetNode(page.LastSearchPosition);

            // already was turned into a multi tree, not much to do here
            if (item->Flags == NodeFlags.MultiValuePageRef)
            {
                var existingTree = OpenMultiValueTree(_tx, key, item);
                existingTree.DirectAdd(value, 0, version: version);
                return;
            }

            byte *nestedPagePtr;

            if (item->Flags == NodeFlags.PageRef)
            {
                var overFlowPage = _tx.ModifyPage(item->PageNumber, null);
                nestedPagePtr = overFlowPage.Base + Constants.PageHeaderSize;
            }
            else
            {
                nestedPagePtr = NodeHeader.DirectAccess(_tx, item);
            }

            var nestedPage = new Page(nestedPagePtr, "multi tree", (ushort)NodeHeader.GetDataSize(_tx, item));

            var existingItem = nestedPage.Search(value);

            if (nestedPage.LastMatch != 0)
            {
                existingItem = null;                // not an actual match, just greater than
            }
            ushort previousNodeRevision = existingItem != null ?  existingItem->Version : (ushort)0;

            CheckConcurrency(key, value, version, previousNodeRevision, TreeActionType.Add);

            if (existingItem != null)
            {
                // maybe same value added twice?
                var tmpKey = page.GetNodeKey(item);
                if (tmpKey.Compare(value) == 0)
                {
                    return;                     // already there, turning into a no-op
                }
                nestedPage.RemoveNode(nestedPage.LastSearchPosition);
            }

            var valueToInsert = nestedPage.PrepareKeyToInsert(value, nestedPage.LastSearchPosition);

            if (nestedPage.HasSpaceFor(_tx, valueToInsert, 0))
            {
                // we are now working on top of the modified root page, we can just modify the memory directly
                nestedPage.AddDataNode(nestedPage.LastSearchPosition, valueToInsert, 0, previousNodeRevision);
                return;
            }

            int pageSize        = nestedPage.CalcSizeUsed() + Constants.PageHeaderSize;
            var newRequiredSize = pageSize + nestedPage.GetRequiredSpace(valueToInsert, 0);

            if (newRequiredSize <= maxNodeSize)
            {
                // we can just expand the current value... no need to create a nested tree yet
                var actualPageSize = (ushort)Math.Min(Utils.NearestPowerOfTwo(newRequiredSize), maxNodeSize);

                var currentDataSize = NodeHeader.GetDataSize(_tx, item);
                ExpandMultiTreeNestedPageSize(_tx, key, value, nestedPagePtr, actualPageSize, currentDataSize);

                return;
            }
            // we now have to convert this into a tree instance, instead of just a nested page
            var tree = Create(_tx, KeysPrefixing, TreeFlags.MultiValue);

            for (int i = 0; i < nestedPage.NumberOfEntries; i++)
            {
                var existingValue = nestedPage.GetNodeKey(i);
                tree.DirectAdd(existingValue, 0);
            }
            tree.DirectAdd(value, 0, version: version);
            _tx.AddMultiValueTree(this, key, tree);
            // we need to record that we switched to tree mode here, so the next call wouldn't also try to create the tree again
            DirectAdd(key, sizeof(TreeRootHeader), NodeFlags.MultiValuePageRef);
        }
Пример #23
0
        private void MoveBranchNode(Page parentPage, Page from, Page to)
        {
            Debug.Assert(from.IsBranch);

            var originalFromKey = to.PrepareKeyToInsert(GetActualKey(from, from.LastSearchPositionOrLastEntry), to.LastSearchPosition);

            to.EnsureHasSpaceFor(_tx, originalFromKey, -1);

            var  fromNode = from.GetNode(from.LastSearchPosition);
            long pageNum  = fromNode->PageNumber;

            if (to.LastSearchPosition == 0)
            {
                // cannot add to left implicit side, adjust by moving the left node
                // to the right by one, then adding the new one as the left

                NodeHeader *actualKeyNode;
                var         implicitLeftKey  = GetActualKey(to, 0, out actualKeyNode);
                var         implicitLeftNode = to.GetNode(0);
                var         leftPageNumber   = implicitLeftNode->PageNumber;

                MemorySlice implicitLeftKeyToInsert;

                if (implicitLeftNode == actualKeyNode)
                {
                    // no need to create a prefix, just use the existing prefixed key from the node
                    // this also prevents from creating a prefix which is the full key given in 'implicitLeftKey'

                    if (_tree.KeysPrefixing)
                    {
                        implicitLeftKeyToInsert = new PrefixedSlice(actualKeyNode);
                    }
                    else
                    {
                        implicitLeftKeyToInsert = new Slice(actualKeyNode);
                    }
                }
                else
                {
                    implicitLeftKeyToInsert = to.PrepareKeyToInsert(implicitLeftKey, 1);
                }

                to.EnsureHasSpaceFor(_tx, implicitLeftKeyToInsert, -1);
                to.AddPageRefNode(1, implicitLeftKeyToInsert, leftPageNumber);

                to.ChangeImplicitRefPageNode(pageNum);                 // setup the new implicit node
            }
            else
            {
                to.AddPageRefNode(to.LastSearchPosition, originalFromKey, pageNum);
            }

            if (from.LastSearchPositionOrLastEntry == 0)
            {
                var rightPageNumber = from.GetNode(1)->PageNumber;
                from.RemoveNode(0);                              // remove the original implicit node
                from.ChangeImplicitRefPageNode(rightPageNumber); // setup the new implicit node
                Debug.Assert(from.NumberOfEntries >= 2);
            }
            else
            {
                from.RemoveNode(from.LastSearchPositionOrLastEntry);
            }

            var pos = parentPage.LastSearchPositionOrLastEntry;

            parentPage.RemoveNode(pos);
            var newSeparatorKey = GetActualKey(to, 0); // get the next smallest key it has now
            var pageNumber      = to.PageNumber;

            if (parentPage.GetNode(0)->PageNumber == to.PageNumber)
            {
                pageNumber      = from.PageNumber;
                newSeparatorKey = GetActualKey(from, 0);
            }

            AddSeparatorToParentPage(parentPage, pageNumber, newSeparatorKey, pos);
        }
Пример #24
0
        public byte *Execute()
        {
            Page rightPage = Tree.NewPage(_tx, _page.Flags, 1);

            _treeState.RecordNewPage(_page, 1);
            rightPage.Flags = _page.Flags;
            if (_cursor.PageCount == 0) // we need to do a root split
            {
                Page newRootPage = Tree.NewPage(_tx, PageFlags.Branch, 1);
                _cursor.Push(newRootPage);
                _treeState.RootPageNumber = newRootPage.PageNumber;
                _treeState.Depth++;
                _treeState.RecordNewPage(newRootPage, 1);

                // now add implicit left page
                newRootPage.AddPageRefNode(0, Slice.BeforeAllKeys, _page.PageNumber);
                _parentPage = newRootPage;
                _parentPage.LastSearchPosition++;
            }
            else
            {
                // we already popped the page, so the current one on the stack is what the parent of the page
                _parentPage = _tx.ModifyPage(_cursor.CurrentPage.PageNumber, _cursor.CurrentPage);
                _cursor.Update(_cursor.Pages.First, _parentPage);
            }

            if (_page.IsLeaf)
            {
                _tx.ClearRecentFoundPages(_tree);
            }

            if (_page.LastSearchPosition >= _page.NumberOfEntries)
            {
                // when we get a split at the end of the page, we take that as a hint that the user is doing
                // sequential inserts, at that point, we are going to keep the current page as is and create a new
                // page, this will allow us to do minimal amount of work to get the best density

                byte *pos;
                if (_page.IsBranch)
                {
                    // here we steal the last entry from the current page so we maintain the implicit null left entry
                    NodeHeader *node = _page.GetNode(_page.NumberOfEntries - 1);
                    Debug.Assert(node->Flags == NodeFlags.PageRef);
                    rightPage.AddPageRefNode(0, Slice.Empty, node->PageNumber);
                    pos = AddNodeToPage(rightPage, 1);

                    AddSeparatorToParentPage(rightPage, new Slice(node));

                    _page.RemoveNode(_page.NumberOfEntries - 1);
                }
                else
                {
                    AddSeparatorToParentPage(rightPage, _newKey);
                    pos = AddNodeToPage(rightPage, 0);
                }
                _cursor.Push(rightPage);
                return(pos);
            }

            return(SplitPageInHalf(rightPage));
        }