/// <summary> /// 在节点中插入键值对并作为叶子节点 /// </summary> /// <param name="key">叶子节点的键</param> /// <param name="value">键对应的值</param> /// <param name="splitFirstKey">if not null then the smallest key in the new split leaf</param> /// <param name="splitNode">if not null then the node was split and this is the leaf to the right.</param> /// <returns>smallest key item in keys, or null if no change</returns> public string InsertLeaf(string key, long value, out string splitFirstKey, out BPlusTreeNode splitNode) { splitFirstKey = null; splitNode = null; if (!this.IsLeaf) { throw new BPlusTreeException("bad call to insert leaf, this is not a leaf"); } // 标示节点已被更改 this.Soil(); // 查找新键的位置 键可能已经存在 int insertPosition = this.FindAtOrNextPosition(key, false); bool doSplit = false; // 节点未满 if (insertPosition < this.Capacity) { // 如果键已存在,则更改其对应值及位置,不支持重复的条目 if (this.ChildKeys[insertPosition] == null || this.Tree.Compare(this.ChildKeys[insertPosition], key) == 0) { this.ChildKeys[insertPosition] = key; this.ChildValues[insertPosition] = value; // 返回键序列中的最小值,如果无更改则返回空 if (insertPosition == 0) { return key; } else { return null; } } // 插入点为比指定键稍大的键 } else { // 节点已满,准备分割节点 doSplit = true; } // 查看是否还有空位置 int nullIndex = insertPosition; while (nullIndex < this.ChildKeys.Length && this.ChildKeys[nullIndex] != null) { nullIndex++; } if (nullIndex >= this.ChildKeys.Length) { doSplit = true; } // 做分割的准备 数组增加1 if (doSplit) { this.PrepareBeforeSplit(); } // 将新数据插入至数组中,将已存在的值向右移动 string nextKey = this.ChildKeys[insertPosition]; long nextValue = this.ChildValues[insertPosition]; this.ChildKeys[insertPosition] = key; this.ChildValues[insertPosition] = value; while (nextKey != null) { key = nextKey; value = nextValue; insertPosition++; nextKey = this.ChildKeys[insertPosition]; nextValue = this.ChildValues[insertPosition]; this.ChildKeys[insertPosition] = key; this.ChildValues[insertPosition] = value; } // 如果需要分割 if (doSplit) { // 从中间开始分割 折半 int splitPoint = this.ChildKeys.Length / 2; int splitLength = this.ChildKeys.Length - splitPoint; // 新创建的分割出的节点,始终是右节点 splitNode = new BPlusTreeNode(this.Tree, this.Parent, -1, this.IsLeaf); // 将指定分割点左侧的数据拷贝至新的节点 Array.Copy(this.ChildKeys, splitPoint, splitNode.ChildKeys, 0, splitLength); Array.Copy(this.ChildValues, splitPoint, splitNode.ChildValues, 0, splitLength); Array.Copy(this.ChildNodes, splitPoint, splitNode.ChildNodes, 0, splitLength); // 记录分割节点的第一个键,右节点的第一个键 splitFirstKey = splitNode.ChildKeys[0]; // 存储新节点至块文件 splitNode.DumpToNewBlock(); // 分割完毕 恢复之前的准备 处理分割点右侧数据,保留左侧数据,删除右侧数据 this.RepairAfterSplit(splitPoint); // 记录新的节点 this.Tree.RecordTerminalNode(splitNode); // InsertLeaf // 新节点及其父节点需要处理 splitNode.Soil(); } if (insertPosition == 0) { return key; // smallest key changed. } else { return null; // no change in smallest key } }
/// <summary> /// 合并叶子节点,当节点的使用率不足50%时,则需要合并 /// </summary> /// <param name="left">左节点</param> /// <param name="right">右节点</param> /// <param name="canDeleteRightNode">是否可以删除右节点</param> public static void MergeLeaves(BPlusTreeNode left, BPlusTreeNode right, out bool canDeleteRightNode) { if (left == null) throw new ArgumentNullException("left"); if (right == null) throw new ArgumentNullException("right"); canDeleteRightNode = false; string[] allKeys = new string[left.Capacity * 2]; long[] allValues = new long[left.Capacity * 2]; int index = 0; for (int i = 0; i < left.Capacity; i++) { if (left.ChildKeys[i] == null) { break; } allKeys[index] = left.ChildKeys[i]; allValues[index] = left.ChildValues[i]; index++; } for (int i = 0; i < right.Capacity; i++) { if (right.ChildKeys[i] == null) { break; } allKeys[index] = right.ChildKeys[i]; allValues[index] = right.ChildValues[i]; index++; } // 如果左节点的容量足够,则可删除右节点 if (index <= left.Capacity) { canDeleteRightNode = true; left.Clear(); for (int i = 0; i < index; i++) { left.ChildKeys[i] = allKeys[i]; left.ChildValues[i] = allValues[i]; } left.Soil(); right.Free(); return; } // 左节点的容量不够了 left.Clear(); right.Clear(); left.Soil(); right.Soil(); int rightContent = index / 2; int leftContent = index - rightContent; int newIndex = 0; for (int i = 0; i < leftContent; i++) { left.ChildKeys[i] = allKeys[newIndex]; left.ChildValues[i] = allValues[newIndex]; newIndex++; } for (int i = 0; i < rightContent; i++) { right.ChildKeys[i] = allKeys[newIndex]; right.ChildValues[i] = allValues[newIndex]; newIndex++; } }
/// <summary> /// 在节点中插入键值对 /// </summary> /// <param name="key">叶子节点的键</param> /// <param name="value">键对应的值</param> /// <param name="splitFirstKey">if not null then the smallest key in the new split leaf</param> /// <param name="splitNode">if not null then the node was split and this is the leaf to the right.</param> /// <returns> /// null unless the smallest key under this node has changed, in which case it returns the smallest key. /// </returns> public string Insert(string key, long value, out string splitFirstKey, out BPlusTreeNode splitNode) { if (this.IsLeaf) { return this.InsertLeaf(key, value, out splitFirstKey, out splitNode); } // 我不是叶子 我是中间节点 找到Key对应的位置 在子节点中插入键值对 splitFirstKey = null; splitNode = null; // 查找新键的位置 由于是中间节点 则新键的位置必须存在 int insertPosition = this.FindAtOrNextPosition(key, false); // 非叶子节点中的值数组存储叶子节点的块序号 long insertValue = this.ChildValues[insertPosition]; if (insertValue == StoredConstants.NullBlockNumber) { throw new BPlusTreeException("key not followed by block number in non-leaf"); } // 加载子节点 BPlusTreeNode insertChild = this.LoadNodeAtIndex(insertPosition); string childSplitFirstKey; BPlusTreeNode childSplitNode; // 在子节点中插入新的键值对 string childInsert = insertChild.Insert(key, value, out childSplitFirstKey, out childSplitNode); // 发现子节点已满,也需要分割 if (childSplitNode != null) { // 我即将被更改 this.Soil(); // redundant -- a child will have a change so this node will need to be copied // 为新的子节点创建位置索引,即下一个 int newChildPosition = insertPosition + 1; // 自己是否已满 bool doSplit = false; // 如果我作为中间节点容量也满了,则中间节点也需要被分割 if (this.ChildValues[this.Capacity] != StoredConstants.NullBlockNumber) { doSplit = true; } if (doSplit) { // 做分割准备 this.PrepareBeforeSplit(); } // bubble over the current values to make space for new child // 新节点位置上及其右侧内容全部向右移动1位,为新节点空出位置 for (int i = this.ChildKeys.Length - 2; i >= newChildPosition - 1; i--) { int iPlus1 = i + 1; int iPlus2 = iPlus1 + 1; this.ChildKeys[iPlus1] = this.ChildKeys[i]; this.ChildValues[iPlus2] = this.ChildValues[iPlus1]; this.ChildNodes[iPlus2] = this.ChildNodes[iPlus1]; } // record the new child // 新节点的位置存放新节点的第一个键 this.ChildKeys[newChildPosition - 1] = childSplitFirstKey; // 被分割出的子节点的父节点为自己 childSplitNode.ResetParent(this, newChildPosition); // 如果我作为中间节点容量也满了,则中间节点也需要被分割 if (doSplit) { // 从中间开始分割 折半 int splitPoint = this.ChildNodes.Length / 2 - 1; // 分割出的新节点的第一个Key splitFirstKey = this.ChildKeys[splitPoint]; // 新建节点 包含分割点右侧所有数据 splitNode = new BPlusTreeNode(this.Tree, this.Parent, -1, this.IsLeaf); splitNode.Clear(); // redundant. // 记录已经扩充的数据结构 long[] values = this.ChildValues; string[] keys = this.ChildKeys; BPlusTreeNode[] nodes = this.ChildNodes; // 重置和清空数据 this.Clear(); // 将分割点左侧的数据拷贝至此节点 Array.Copy(keys, 0, this.ChildKeys, 0, splitPoint); Array.Copy(values, 0, this.ChildValues, 0, splitPoint + 1); Array.Copy(nodes, 0, this.ChildNodes, 0, splitPoint + 1); // 将分割点右侧的数据拷贝至新的分割节点 int remainingKeys = this.Capacity - splitPoint; Array.Copy(keys, splitPoint + 1, splitNode.ChildKeys, 0, remainingKeys); Array.Copy(values, splitPoint + 1, splitNode.ChildValues, 0, remainingKeys + 1); Array.Copy(nodes, splitPoint + 1, splitNode.ChildNodes, 0, remainingKeys + 1); // 重置新节点中所有的子节点的父节点 splitNode.ResetAllChildrenParent(); // 存储新节点 splitNode.DumpToNewBlock(); splitNode.CheckIfTerminal(); splitNode.Soil(); this.CheckIfTerminal(); } // end do split // 重置节点中所有的子节点的父节点 this.ResetAllChildrenParent(); } // 返回最小的那个键 if (insertPosition == 0) { return childInsert; // the smallest key may have changed } else { return null; // no change in smallest key } }
/// <summary> /// 合并节点,当节点的使用率不足50%时,则需要合并 /// </summary> /// <param name="left">左节点</param> /// <param name="keyBetween">左右节点的中间键</param> /// <param name="right">右节点</param> /// <param name="rightLeastKey">合并后的键的最小值</param> /// <param name="canDeleteRightNode">是否可以删除右节点</param> public static void Merge(BPlusTreeNode left, string keyBetween, BPlusTreeNode right, out string rightLeastKey, out bool canDeleteRightNode) { if (left == null) throw new ArgumentNullException("left"); if (right == null) throw new ArgumentNullException("right"); rightLeastKey = null; // only if DeleteRight // 合并叶子节点 if (left.IsLeaf || right.IsLeaf) { if (!(left.IsLeaf && right.IsLeaf)) { throw new BPlusTreeException("can't merge leaf with non-leaf"); } // 合并子节点 MergeLeaves(left, right, out canDeleteRightNode); rightLeastKey = right.ChildKeys[0]; return; } // 合并非叶子节点 canDeleteRightNode = false; if (left.ChildValues[0] == StoredConstants.NullBlockNumber || right.ChildValues[0] == StoredConstants.NullBlockNumber) { throw new BPlusTreeException("cannot merge empty non-leaf with non-leaf"); } string[] allKeys = new string[left.Capacity * 2 + 1]; long[] allValues = new long[left.Capacity * 2 + 2]; BPlusTreeNode[] allNodes = new BPlusTreeNode[left.Capacity * 2 + 2]; // 拷贝左节点的数据 int index = 0; allValues[0] = left.ChildValues[0]; allNodes[0] = left.ChildNodes[0]; for (int i = 0; i < left.Capacity; i++) { if (left.ChildKeys[i] == null) { break; } allKeys[index] = left.ChildKeys[i]; allValues[index + 1] = left.ChildValues[i + 1]; allNodes[index + 1] = left.ChildNodes[i + 1]; index++; } // 拷贝中间键 allKeys[index] = keyBetween; index++; // 拷贝右节点的数据 allValues[index] = right.ChildValues[0]; allNodes[index] = right.ChildNodes[0]; int rightCount = 0; for (int i = 0; i < right.Capacity; i++) { if (right.ChildKeys[i] == null) { break; } allKeys[index] = right.ChildKeys[i]; allValues[index + 1] = right.ChildValues[i + 1]; allNodes[index + 1] = right.ChildNodes[i + 1]; index++; rightCount++; } // 如果数量小于左节点的能力,则右节点可以删除掉 if (index <= left.Capacity) { // it will all fit in one node canDeleteRightNode = true; for (int i = 0; i < index; i++) { left.ChildKeys[i] = allKeys[i]; left.ChildValues[i] = allValues[i]; left.ChildNodes[i] = allNodes[i]; } left.ChildValues[index] = allValues[index]; left.ChildNodes[index] = allNodes[index]; left.ResetAllChildrenParent(); left.Soil(); right.Free(); return; } // otherwise split the content between the nodes left.Clear(); right.Clear(); left.Soil(); right.Soil(); int leftContent = index / 2; int rightContent = index - leftContent - 1; rightLeastKey = allKeys[leftContent]; int outputIndex = 0; for (int i = 0; i < leftContent; i++) { left.ChildKeys[i] = allKeys[outputIndex]; left.ChildValues[i] = allValues[outputIndex]; left.ChildNodes[i] = allNodes[outputIndex]; outputIndex++; } rightLeastKey = allKeys[outputIndex]; left.ChildValues[outputIndex] = allValues[outputIndex]; left.ChildNodes[outputIndex] = allNodes[outputIndex]; outputIndex++; rightCount = 0; for (int i = 0; i < rightContent; i++) { right.ChildKeys[i] = allKeys[outputIndex]; right.ChildValues[i] = allValues[outputIndex]; right.ChildNodes[i] = allNodes[outputIndex]; outputIndex++; rightCount++; } right.ChildValues[rightCount] = allValues[outputIndex]; right.ChildNodes[rightCount] = allNodes[outputIndex]; left.ResetAllChildrenParent(); right.ResetAllChildrenParent(); }