Beispiel #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>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
      }
    }
Beispiel #2
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
      }
    }