tsdb/index: fast-track postings for label=""

We need to special-case ""="" too, which is used in some tests to mean "everything".

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2023-01-05 14:05:29 +00:00
parent cf92cd2688
commit e61348d9f3
1 changed files with 16 additions and 4 deletions

View File

@ -239,7 +239,14 @@ func PostingsForMatchers(ix IndexReader, ms ...*labels.Matcher) (index.Postings,
} }
for _, m := range ms { for _, m := range ms {
if labelMustBeSet[m.Name] { if m.Name == "" && m.Value == "" { // Special-case for AllPostings, used in tests at least.
k, v := index.AllPostingsKey()
allPostings, err := ix.Postings(k, v)
if err != nil {
return nil, err
}
its = append(its, allPostings)
} else if labelMustBeSet[m.Name] {
// If this matcher must be non-empty, we can be smarter. // If this matcher must be non-empty, we can be smarter.
matchesEmpty := m.Matches("") matchesEmpty := m.Matches("")
isNot := m.Type == labels.MatchNotEqual || m.Type == labels.MatchNotRegexp isNot := m.Type == labels.MatchNotEqual || m.Type == labels.MatchNotRegexp
@ -352,9 +359,14 @@ func inversePostingsForMatcher(ix IndexReader, m *labels.Matcher) (index.Posting
} }
var res []string var res []string
for _, val := range vals { // If the inverse match is ="", we just want all the values.
if !m.Matches(val) { if m.Type == labels.MatchEqual && m.Value == "" {
res = append(res, val) res = vals
} else {
for _, val := range vals {
if !m.Matches(val) {
res = append(res, val)
}
} }
} }