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 287e4a3
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions tsdb/index/postings.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,11 @@ func newListPostings(list ...storage.SeriesRef) *ListPostings {
}

func (it *ListPostings) At() storage.SeriesRef {
if it.i >= len(it.list) {
// Not entirely sure why postingsWithIndexHeap needs to keep exhausted iterators around
return it.list[len(it.list)-1]
}

return it.list[it.i]
}

Expand All @@ -855,17 +860,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 +931,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 +945,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 287e4a3

Please sign in to comment.