Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: iterate lower bound may panic if search with the same prefix on keys #35

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion iradix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,10 @@ func TestIterateLowerBound(t *testing.T) {
"zap",
"zip",
}

specialKeys := []string{
string([]byte{1, 0, 1, 0, 0}),
string([]byte{1, 0, 1, 0, 1, 0}),
}
type exp struct {
keys []string
search string
Expand Down Expand Up @@ -1665,6 +1668,36 @@ func TestIterateLowerBound(t *testing.T) {
"cbacb",
[]string{"cbbaa", "cbbab", "cbbbc", "cbcbb", "cbcbc", "cbcca", "ccaaa", "ccabc", "ccaca", "ccacc", "ccbac", "cccaa", "cccac", "cccca"},
},
{
specialKeys,
"",
specialKeys,
},
{
specialKeys,
specialKeys[0][:4],
specialKeys,
},
{
specialKeys,
specialKeys[0],
specialKeys,
},
{
specialKeys,
specialKeys[1][:5],
[]string{specialKeys[1]},
},
{
specialKeys,
specialKeys[1],
[]string{specialKeys[1]},
},
{
specialKeys,
specialKeys[1] + "0",
[]string{},
},
}

for idx, test := range cases {
Expand Down
13 changes: 12 additions & 1 deletion iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (i *Iterator) SeekPrefixWatch(prefix []byte) (watch <-chan struct{}) {
watch = n.mutateCh
search := prefix
for {
// Check for key exhaution
// Check for key exhaustion
if len(search) == 0 {
i.node = n
return
Expand Down Expand Up @@ -132,6 +132,17 @@ func (i *Iterator) SeekLowerBound(key []byte) {
search = search[len(n.prefix):]
}

// Check for key exhaustion
if len(search) == 0 {
// Prefix is same with the search key and not leaf node, that means the lower bound is greater than the search
// and from now on we need to follow the minimum path to the smallest
// leaf under this subtree.
n = i.recurseMin(n)
if n != nil {
found(n)
}
return
}
// Otherwise, take the lower bound next edge.
idx, lbNode := n.getLowerBoundEdge(search[0])
if lbNode == nil {
Expand Down