node iterator seek: compare paths without terminator byte

This commit is contained in:
Roy Crihfield 2023-08-02 20:49:14 +08:00 committed by Gary Rong
parent e206d3f897
commit 7d25dfe6f8

View file

@ -311,7 +311,7 @@ func (it *nodeIterator) seek(prefix []byte) error {
return errIteratorEnd
} else if err != nil {
return seekError{prefix, err}
} else if bytes.Compare(path, key) >= 0 {
} else if reachedPath(path, key) {
return nil
}
it.push(state, parentIndex, path)
@ -554,6 +554,15 @@ func (it *nodeIterator) pop() {
it.putInPool(last)
}
// reachedPath normalizes a path by truncating a terminator if present, and returns true if it is
// greater than or equal to the target.
func reachedPath(path, target []byte) bool {
if hasTerm(path) {
path = path[:len(path)-1]
}
return bytes.Compare(path, target) >= 0
}
func compareNodes(a, b NodeIterator) int {
if cmp := bytes.Compare(a.Path(), b.Path()); cmp != 0 {
return cmp