This commit is contained in:
Bryan Boreham 2025-08-05 13:10:53 +01:00 committed by GitHub
commit c1371bbf37
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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()
} }