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