/// <summary> /// Creates a new node with the right part of this node. /// </summary> /// <remarks> /// Creates a new node with the right part of this node. This should only be called on a full node /// </remarks> public virtual IBTreeNode ExtractRightPart() { if (!IsFull()) { throw new BTreeException("extract right part called on non full node"); } // Creates an empty new node var rightPartNode = Btree.BuildNode(); var j = 0; for (var i = _degree; i < _maxNbKeys; i++) { rightPartNode.SetKeyAndValueAt(Keys[i], Values[i], j, false, false); Keys[i] = null; Values[i] = null; rightPartNode.SetChildAt(this, i, j, false); // TODO must we load all nodes to set new parent var bTreeNode = rightPartNode.GetChildAt(j, false); if (bTreeNode != null) { bTreeNode.SetParent(rightPartNode); } SetNullChildAt(i); j++; } rightPartNode.SetChildAt(this, MaxNbChildren - 1, j, false); // correct father id var c1TreeNode = rightPartNode.GetChildAt(j, false); if (c1TreeNode != null) { c1TreeNode.SetParent(rightPartNode); } SetNullChildAt(MaxNbChildren - 1); // resets last child Keys[_degree - 1] = null; // resets median element Values[_degree - 1] = null; // set numbers NbKeys = _degree - 1; var originalNbChildren = NbChildren; NbChildren = Math.Min(NbChildren, _degree); rightPartNode.SetNbKeys(_degree - 1); rightPartNode.SetNbChildren(originalNbChildren - NbChildren); BTreeValidator.ValidateNode(this); BTreeValidator.ValidateNode(rightPartNode); BTreeValidator.CheckDuplicateChildren(this, rightPartNode); return(rightPartNode); }
private IBTreeNode PrepareForDelete(IBTreeNode parent, IBTreeNode child, int childIndex) { BTreeValidator.ValidateNode(parent); BTreeValidator.ValidateNode(child); // case 3a IBTreeNode leftSibling = null; IBTreeNode rightSibling = null; if (childIndex > 0 && parent.GetNbChildren() > 0) { leftSibling = parent.GetChildAt(childIndex - 1, false); } if (childIndex < parent.GetNbChildren() - 1) { rightSibling = parent.GetChildAt(childIndex + 1, false); } // case 3a left if (leftSibling != null && leftSibling.GetNbKeys() >= _degree) { var elementToMoveDown = parent.GetKeyAndValueAt(childIndex - 1); var elementToMoveUp = leftSibling.GetLastKeyAndValue(); parent.SetKeyAndValueAt(elementToMoveUp, childIndex - 1); child.InsertKeyAndValue(elementToMoveDown.GetKey(), elementToMoveDown.GetValue()); if (leftSibling.GetNbChildren() > leftSibling.GetNbKeys()) { // Take the last child of the left sibling and set it the // first child of the 'child' (incoming parameter) // child.setChildAt(leftSibling.getChildAt(leftSibling.getNbChildren() // - 1, true), 0); child.SetChildAt(leftSibling, leftSibling.GetNbChildren() - 1, 0, true); child.IncrementNbChildren(); } leftSibling.DeleteKeyAndValueAt(leftSibling.GetNbKeys() - 1, false); if (!leftSibling.IsLeaf()) { leftSibling.DeleteChildAt(leftSibling.GetNbChildren() - 1); } _persister.SaveNode(parent); _persister.SaveNode(child); _persister.SaveNode(leftSibling); if (OdbConfiguration.IsBTreeValidationEnabled()) { BTreeValidator.ValidateNode(parent, parent == _root); BTreeValidator.ValidateNode(child, false); BTreeValidator.ValidateNode(leftSibling, false); BTreeValidator.CheckDuplicateChildren(leftSibling, child); } return(parent); } // case 3a right if (rightSibling != null && rightSibling.GetNbKeys() >= _degree) { var elementToMoveDown = parent.GetKeyAndValueAt(childIndex); var elementToMoveUp = rightSibling.GetKeyAndValueAt(0); parent.SetKeyAndValueAt(elementToMoveUp, childIndex); child.InsertKeyAndValue(elementToMoveDown.GetKey(), elementToMoveDown.GetValue()); if (rightSibling.GetNbChildren() > 0) { // Take the first child of the right sibling and set it the // last child of the 'child' (incoming parameter) child.SetChildAt(rightSibling, 0, child.GetNbChildren(), true); child.IncrementNbChildren(); } rightSibling.DeleteKeyAndValueAt(0, true); _persister.SaveNode(parent); _persister.SaveNode(child); _persister.SaveNode(rightSibling); if (OdbConfiguration.IsBTreeValidationEnabled()) { BTreeValidator.ValidateNode(parent, parent == _root); BTreeValidator.ValidateNode(child, false); BTreeValidator.ValidateNode(rightSibling, false); BTreeValidator.CheckDuplicateChildren(rightSibling, child); } return(parent); } // case 3b var isCase3B = (leftSibling != null && leftSibling.GetNbKeys() == _degree - 1) || (rightSibling != null && rightSibling.GetNbKeys() >= _degree - 1); var parentWasSetToNull = false; if (isCase3B) { // choose left sibling to execute merge if (leftSibling != null) { var elementToMoveDown = parent.GetKeyAndValueAt(childIndex - 1); leftSibling.InsertKeyAndValue(elementToMoveDown.GetKey(), elementToMoveDown.GetValue()); leftSibling.MergeWith(child); parent.DeleteKeyAndValueAt(childIndex - 1, true); if (parent.GetNbKeys() == 0) { // this is the root if (!parent.HasParent()) { _root = leftSibling; _root.SetParent(null); _height--; parentWasSetToNull = true; } else { const string errorMessage = "Unexpected empty node that is node the root!"; throw new BTreeNodeValidationException(errorMessage); } } else { parent.SetChildAt(leftSibling, childIndex - 1); } if (parentWasSetToNull) { _persister.DeleteNode(parent); } else { _persister.SaveNode(parent); BTreeValidator.ValidateNode(parent, parent == _root); } // child was merged with another node it must be deleted _persister.DeleteNode(child); _persister.SaveNode(leftSibling); // Validator.validateNode(child, child == root); BTreeValidator.ValidateNode(leftSibling, leftSibling == _root); // Validator.checkDuplicateChildren(leftSibling, child); return(parentWasSetToNull ? _root : parent); } // choose right sibling to execute merge { var elementToMoveDown = parent.GetKeyAndValueAt(childIndex); child.InsertKeyAndValue(elementToMoveDown.GetKey(), elementToMoveDown.GetValue()); child.MergeWith(rightSibling); parent.DeleteKeyAndValueAt(childIndex, true); if (parent.GetNbKeys() == 0) { // this is the root if (!parent.HasParent()) { _root = child; _root.SetParent(null); _height--; parentWasSetToNull = true; } else { throw new BTreeNodeValidationException("Unexpected empty root node!"); } } else { parent.SetChildAt(child, childIndex); } if (parentWasSetToNull) { _persister.DeleteNode(parent); } else { _persister.SaveNode(parent); BTreeValidator.ValidateNode(parent, parent == _root); } _persister.DeleteNode(rightSibling); _persister.SaveNode(child); BTreeValidator.ValidateNode(child, child == _root); // Validator.validateNode(rightSibling, rightSibling == // root); // Validator.checkDuplicateChildren(rightSibling, child); return(parentWasSetToNull ? _root : parent); } } throw new BTreeNodeValidationException("Unexpected case in executing prepare for delete"); }