// starting from start node, along the path specified by prefix, locate the node pointed
// to by prefix and return. If tracers is not nil, the location path will be stored in
// tracers in order.
// lastNode: The parent node of the target node or leaf value. If the parent node is not
//           located, it is nil
// index: The position of the target node or leaf value in its parent node, if the target
//        node or leaf value does not exist, return -1
// value: Target leaf value, nil if it does not exist
// exist: Whether the target node or leaf value exists
// err: error
starting from start node, along the path specified by prefix, locate the node pointed
to by prefix and return. If tracers is not nil, the location path will be stored in
tracers in order.
lastNode: The parent node of the target node or leaf value. If the parent node is not
located, it is nil
index: The position of the target node or leaf value in its parent node, if the target
node or leaf value does not exist, return -1
value: Target leaf value, nil if it does not exist
exist: Whether the target node or leaf value exists
err: error
// starting from start node along the path specified by prefix locate the node pointed // to by prefix and return . If tracers is not nil the location path will be stored in // tracers in order . // lastNode : The parent node of the target node or leaf value . If the parent node is not // located it is nil // index : The position of the target node or leaf value in its parent node if the target // node or leaf value does not exist return - 1 // value : Target leaf value nil if it does not exist // exist : Whether the target node or leaf value exists // err : error
func locateNode(start *TreeNode, prefix []byte, adapter db.DataAdapter, tracers *[]nodeTracer) (lastNode *TreeNode,
	index int, value []byte, exist bool, err error) {
	if start == nil {
		return nil, -1, nil, false, common.ErrNotFound
	}
	node := start
	if len(prefix) > HistoryTreeDepth {
		return nil, -1, nil, false, common.ErrIllegalParams
	}
	for i := 0; i < len(prefix); i++ {
		index := int(prefix[i])
		if index >= childrenLength {
			return nil, -1, nil, false, common.ErrIllegalParams
		}
		if node.isCollapsed() {
			err = node.Expand(adapter)
			if err != nil {
				return nil, -1, nil, false, err
			}
		}
		if node.IsLeaf() {
			if i != (len(prefix) - 1) {
				return nil, -1, nil, false, common.ErrIllegalParams
			}
			if node.Leafs[int(prefix[i])] == nil {
				return node, -1, nil, false, nil
			} else {
				return node, index, node.Leafs[index], true, nil
			}
		} else {
			if i == (len(prefix) - 1) {
				if node.Children[index] != nil {
					return node, index, nil, true, nil
				} else {
					return node, -1, nil, false, nil
				}
			}
			if node.Children[index] == nil {
				return node, -1, nil, false, nil
			} else {
				node = node.Children[index]
				if tracers != nil {
					*tracers = append(*tracers, nodeTracer{pos: prefix[i], nodeInPos: node})
				}
			}
		}
	}
	return nil, -1, nil, false, common.ErrUnknown
}
func locateNode ( start * TreeNode , prefix [ ] byte , adapter db . DataAdapter , tracers * [ ] nodeTracer ) ( lastNode * TreeNode , index int , value [ ] byte , exist bool , err error ) { if start == nil { return nil , - 1 , nil , false , common . ErrNotFound } node := start if len ( prefix ) > HistoryTreeDepth { return nil , - 1 , nil , false , common . ErrIllegalParams } for i := 0 ; i < len ( prefix ) ; i ++ { index := int ( prefix [ i ] ) if index >= childrenLength { return nil , - 1 , nil , false , common . ErrIllegalParams } if node . isCollapsed ( ) { err = node . Expand ( adapter ) if err != nil { return nil , - 1 , nil , false , err } } if node . IsLeaf ( ) { if i != ( len ( prefix ) - 1 ) { return nil , - 1 , nil , false , common . ErrIllegalParams } if node . Leafs [ int ( prefix [ i ] ) ] == nil { return node , - 1 , nil , false , nil } else { return node , index , node . Leafs [ index ] , true , nil } } else { if i == ( len ( prefix ) - 1 ) { if node . Children [ index ] != nil { return node , index , nil , true , nil } else { return node , - 1 , nil , false , nil } } if node . Children [ index ] == nil { return node , - 1 , nil , false , nil } else { node = node . Children [ index ] if tracers != nil { * tracers = append ( * tracers , nodeTracer { pos : prefix [ i ] , nodeInPos : node } ) } } } } return nil , - 1 , nil , false , common . ErrUnknown }
starting from start node, along the path specified by prefix, locate the node pointed
to by prefix and return.
starting from start node along the path specified by prefix locate the node pointed to by prefix and return .
// reaches the leaf node without the prefix ending, error // return nil, -1, nil, false, common.ErrIllegalParams // found result in advance // should not be here