Skip to content

Commit

Permalink
Fix bad range results on indexed fields
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Apr 30, 2018
1 parent a86fa5e commit bda68da
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
24 changes: 24 additions & 0 deletions index/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,30 @@ func TestListIndexRange(t *testing.T) {
require.Len(t, list, 2)
require.NoError(t, err)
assertEncodedIntListEqual(t, []int{5, 4}, list)

// issue #218
val, _ := gob.Codec.Marshal(8)
err = idx.Remove(val)
require.NoError(t, err)

// normal with gaps
min, _ = gob.Codec.Marshal(6)
max, _ = gob.Codec.Marshal(8)
list, err = idx.Range(min, max, nil)
require.Len(t, list, 2)
require.NoError(t, err)
assertEncodedIntListEqual(t, []int{6, 7}, list)

// reverse with gaps
min, _ = gob.Codec.Marshal(6)
max, _ = gob.Codec.Marshal(8)
opts = index.NewOptions()
opts.Reverse = true
list, err = idx.Range(min, max, opts)
require.Len(t, list, 2)
require.NoError(t, err)
assertEncodedIntListEqual(t, []int{7, 6}, list)

return nil
})
}
Expand Down
11 changes: 10 additions & 1 deletion internal/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ type RangeCursor struct {
// First element
func (c *RangeCursor) First() ([]byte, []byte) {
if c.Reverse {
return c.C.Seek(c.Max)
k, v := c.C.Seek(c.Max)

// If Seek doesn't find a key it goes to the next.
// If so, we need to get the previous one to avoid
// including bigger values. #218
if !bytes.HasPrefix(k, c.Max) && k != nil {
k, v = c.C.Prev()
}

return k, v
}

return c.C.Seek(c.Min)
Expand Down

0 comments on commit bda68da

Please sign in to comment.