[REFACTOR] TSDB: Clarify intersectPostings

This is intended to make `intersectPostings` easier to follow.

Instead of cryptic `arr` and `cur`, name the members `postings` and
`current`.

Instead of updating `cur` to intermediate values encountered during
operations, introduce a local variable `target` meaning the ref we might
expect to find next, and only update `current` when an intersection is
found.

Name the function which implements seeking `Seek` instead of `doNext`.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2025-08-05 13:09:29 +01:00
parent 25aee26a57
commit e068c7332d

View File

@ -607,57 +607,54 @@ func Intersect(its ...Postings) Postings {
}
type intersectPostings struct {
arr []Postings
cur storage.SeriesRef
postings []Postings // These are the postings we will be intersecting.
current storage.SeriesRef // The current intersection, if Seek() or Next() has returned true.
}
func newIntersectPostings(its ...Postings) *intersectPostings {
return &intersectPostings{arr: its}
return &intersectPostings{postings: its}
}
func (it *intersectPostings) At() storage.SeriesRef {
return it.cur
return it.current
}
func (it *intersectPostings) doNext() bool {
func (it *intersectPostings) Seek(target storage.SeriesRef) bool {
for {
allEqual := true
for _, p := range it.arr {
if !p.Seek(it.cur) {
for _, p := range it.postings {
if !p.Seek(target) {
return false
}
if p.At() > it.cur {
it.cur = p.At()
if p.At() > target {
target = p.At()
allEqual = false
}
}
// if all p.At() are all equal, we found an intersection.
if allEqual {
it.current = target
return true
}
}
}
func (it *intersectPostings) Next() bool {
for _, p := range it.arr {
target := it.current
for _, p := range it.postings {
if !p.Next() {
return false
}
if p.At() > it.cur {
it.cur = p.At()
if p.At() > target {
target = p.At()
}
}
return it.doNext()
}
func (it *intersectPostings) Seek(id storage.SeriesRef) bool {
it.cur = id
return it.doNext()
return it.Seek(target)
}
func (it *intersectPostings) Err() error {
for _, p := range it.arr {
for _, p := range it.postings {
if p.Err() != nil {
return p.Err()
}