Exemplo n.º 1
0
// delete removes a range from the range tree.
// Since this tree is not stored in memory but persisted through the ranges, in
// place deletion is not possible. Instead, we use the helper function
// swapNodes above.
func (tc *treeContext) delete(node *roachpb.RangeTreeNode) *roachpb.Error {
	key := node.Key
	if node.LeftKey != nil && node.RightKey != nil {
		left, pErr := tc.getNode(node.LeftKey)
		if pErr != nil {
			return pErr
		}
		predecessor, pErr := tc.getMaxNode(left)
		if pErr != nil {
			return pErr
		}
		node, _, pErr = tc.swapNodes(node, predecessor)
		if pErr != nil {
			return pErr
		}
	}

	// Node will always have at most one child.
	var child *roachpb.RangeTreeNode
	var pErr *roachpb.Error
	if node.LeftKey != nil {
		if child, pErr = tc.getNode(node.LeftKey); pErr != nil {
			return pErr
		}
	} else if node.RightKey != nil {
		if child, pErr = tc.getNode(node.RightKey); pErr != nil {
			return pErr
		}
	}
	if !isRed(node) {
		// Paint the node to the color of the child node.
		node.Black = !isRed(child)
		tc.setNode(node)
		if pErr := tc.deleteCase1(node); pErr != nil {
			return pErr
		}
	}
	if _, pErr := tc.replaceNode(node, child); pErr != nil {
		return pErr
	}

	// Always set the root back to black
	if node, pErr = tc.getNode(node.Key); pErr != nil {
		return pErr
	}
	if child != nil && node.ParentKey == nil {
		if child, pErr = tc.getNode(child.Key); pErr != nil {
			return pErr
		}
		child.Black = true
		tc.setNode(child)
	}

	tc.dropNode(key)
	return nil
}
Exemplo n.º 2
0
// insertCase1 handles the case when the inserted node is the root node.
func (tc *treeContext) insertCase1(node *roachpb.RangeTreeNode) *roachpb.Error {
	if node.ParentKey == nil {
		node.Black = true
		tc.setNode(node)
		return nil
	}
	return tc.insertCase2(node)
}