当前位置: 首页>>代码示例>>Golang>>正文


Golang RangeTreeNode.ParentKey方法代码示例

本文整理汇总了Golang中github.com/cockroachdb/cockroach/proto.RangeTreeNode.ParentKey方法的典型用法代码示例。如果您正苦于以下问题:Golang RangeTreeNode.ParentKey方法的具体用法?Golang RangeTreeNode.ParentKey怎么用?Golang RangeTreeNode.ParentKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/cockroachdb/cockroach/proto.RangeTreeNode的用法示例。


在下文中一共展示了RangeTreeNode.ParentKey方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: replaceNode

// replaceNode cuts a node away form its parent, substituting a new node or
// nil. The updated new node is returned. Note that this does not in fact alter
// the old node in any way, but only the old node's parent and the new node.
func (tc *treeContext) replaceNode(oldNode, newNode *proto.RangeTreeNode) (*proto.RangeTreeNode, error) {
	if oldNode.ParentKey == nil {
		if newNode == nil {
			return nil, util.Error("cannot replace the root node with nil")
		}
		// Update the root key if this was the root.
		tc.setRootKey(newNode.Key)
	} else {
		oldParent, err := tc.getNode(oldNode.ParentKey)
		if err != nil {
			return nil, err
		}
		if oldParent.LeftKey != nil && oldNode.Key.Equal(oldParent.LeftKey) {
			if newNode == nil {
				oldParent.LeftKey = nil
			} else {
				oldParent.LeftKey = newNode.Key
			}
		} else {
			if newNode == nil {
				oldParent.RightKey = nil
			} else {
				oldParent.RightKey = newNode.Key
			}
		}
		tc.setNode(oldParent)
	}
	if newNode != nil {
		newNode.ParentKey = oldNode.ParentKey
		tc.setNode(newNode)
	}
	return newNode, nil
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:36,代码来源:range_tree.go

示例2: insert

// insert performs the insertion of a new node into the tree. It walks the tree
// until it finds the correct location. It will fail if the node already exists
// as that case should not occur. After inserting the node, it checks all insert
// cases to ensure the tree is balanced and adjusts it if needed.
func (tc *treeContext) insert(node *proto.RangeTreeNode) error {
	if tc.tree.RootKey == nil {
		tc.setRootKey(node.Key)
	} else {
		// Walk the tree to find the right place to insert the new node.
		currentKey := tc.tree.RootKey
		for {
			currentNode, err := tc.getNode(currentKey)
			if err != nil {
				return err
			}
			if node.Key.Equal(currentNode.Key) {
				return util.Errorf("key %s already exists in the range tree", node.Key)
			}
			if node.Key.Less(currentNode.Key) {
				if currentNode.LeftKey == nil {
					currentNode.LeftKey = node.Key
					tc.setNode(currentNode)
					break
				} else {
					currentKey = currentNode.LeftKey
				}
			} else {
				if currentNode.RightKey == nil {
					currentNode.RightKey = node.Key
					tc.setNode(currentNode)
					break
				} else {
					currentKey = currentNode.RightKey
				}
			}
		}
		node.ParentKey = currentKey
		tc.setNode(node)
	}
	return tc.insertCase1(node)
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:41,代码来源:range_tree.go

示例3: rotateRight

// rotateRight performs a right rotation around the node.
func (tc *treeContext) rotateRight(node *proto.RangeTreeNode) (*proto.RangeTreeNode, error) {
	left, err := tc.getNode(node.LeftKey)
	if err != nil {
		return nil, err
	}
	left, err = tc.replaceNode(node, left)
	if err != nil {
		return nil, err
	}
	node.LeftKey = left.RightKey
	if left.RightKey != nil {
		leftRight, err := tc.getNode(left.RightKey)
		if err != nil {
			return nil, err
		}
		leftRight.ParentKey = node.Key
		tc.setNode(leftRight)
	}
	left.RightKey = node.Key
	node.ParentKey = left.Key
	tc.setNode(left)
	tc.setNode(node)
	return left, nil
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:25,代码来源:range_tree.go

示例4: rotateLeft

// rotateLeft performs a left rotation around the node.
func (tc *treeContext) rotateLeft(node *proto.RangeTreeNode) (*proto.RangeTreeNode, error) {
	right, err := tc.getNode(node.RightKey)
	if err != nil {
		return nil, err
	}
	right, err = tc.replaceNode(node, right)
	if err != nil {
		return nil, err
	}
	node.RightKey = right.LeftKey
	if right.LeftKey != nil {
		rightLeft, err := tc.getNode(right.LeftKey)
		if err != nil {
			return nil, err
		}
		rightLeft.ParentKey = node.Key
		tc.setNode(rightLeft)
	}
	right.LeftKey = node.Key
	node.ParentKey = right.Key
	tc.setNode(right)
	tc.setNode(node)
	return right, nil
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:25,代码来源:range_tree.go


注:本文中的github.com/cockroachdb/cockroach/proto.RangeTreeNode.ParentKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。