Skip to content

Commit

Permalink
Fix iterator seeking
Browse files Browse the repository at this point in the history
Signed-off-by: Arve Knudsen <[email protected]>
  • Loading branch information
aknuds1 committed Sep 11, 2023
1 parent 2393ebd commit 16292cf
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions tsdb/index/postings.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,17 +855,22 @@ func (it *ListPostings) Next() bool {

func (it *ListPostings) Seek(x storage.SeriesRef) bool {
// If the current value satisfies, then return.
if it.list[it.i] >= x {
if it.i >= 0 && it.list[it.i] >= x {
return true
}

start := it.i
if start < 0 {
start = 0
}

// Do binary search between current position and end.
l := it.list[it.i:]
l := it.list[start:]
i := sort.Search(len(l), func(i int) bool {
return l[i] >= x
})
if i < len(l) {
it.i += i
it.i = start + i
return true
}

Expand Down Expand Up @@ -921,7 +926,12 @@ func (it *bigEndianPostings) Seek(x storage.SeriesRef) bool {
return true
}

l := it.list[it.i:]
start := it.i
if start < 0 {
start = 0
}

l := it.list[start:]
num := len(l) / 4
// Do binary search between current position and end.
i := sort.Search(num, func(i int) bool {
Expand All @@ -930,7 +940,7 @@ func (it *bigEndianPostings) Seek(x storage.SeriesRef) bool {
if i < num {
j := i * 4
it.cur = binary.BigEndian.Uint32(l[j:])
it.i += j
it.i = start + j
return true
}

Expand Down

0 comments on commit 16292cf

Please sign in to comment.