From e068c7332d3a2cb23ac9f291b848a3f3d36f3bee Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 5 Aug 2025 13:09:29 +0100 Subject: [PATCH] [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 --- tsdb/index/postings.go | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/tsdb/index/postings.go b/tsdb/index/postings.go index 98369e3326..223b01b012 100644 --- a/tsdb/index/postings.go +++ b/tsdb/index/postings.go @@ -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() }