Ejemplo n.º 1
0
    /// <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
      }
    }
Ejemplo n.º 2
0
    /// <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();
    }