From d1f21552b9651e5059c34ccca0a26cdf43c0cd54 Mon Sep 17 00:00:00 2001 From: Yao Zengzeng Date: Fri, 13 Sep 2019 23:10:35 +0800 Subject: [PATCH] some refactor to make PostingsForMatchers more readable (#5897) Signed-off-by: YaoZengzeng --- tsdb/querier.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tsdb/querier.go b/tsdb/querier.go index aba0f4dd1..357232e46 100644 --- a/tsdb/querier.go +++ b/tsdb/querier.go @@ -328,6 +328,7 @@ func findSetMatches(pattern string) []string { func PostingsForMatchers(ix IndexReader, ms ...labels.Matcher) (index.Postings, error) { var its, notIts []index.Postings // See which label must be non-empty. + // Optimization for case like {l=~".", l!="1"}. labelMustBeSet := make(map[string]bool, len(ms)) for _, m := range ms { if !m.Matches("") { @@ -336,9 +337,9 @@ func PostingsForMatchers(ix IndexReader, ms ...labels.Matcher) (index.Postings, } for _, m := range ms { - matchesEmpty := m.Matches("") - if labelMustBeSet[m.Name()] || !matchesEmpty { + if labelMustBeSet[m.Name()] { // If this matcher must be non-empty, we can be smarter. + matchesEmpty := m.Matches("") nm, isNot := m.(*labels.NotMatcher) if isNot && matchesEmpty { // l!="foo" // If the label can't be empty and is a Not and the inner matcher @@ -444,7 +445,7 @@ func postingsForMatcher(ix IndexReader, m labels.Matcher) (index.Postings, error return index.Merge(rit...), nil } -// inversePostingsForMatcher eeturns the postings for the series with the label name set but not matching the matcher. +// inversePostingsForMatcher returns the postings for the series with the label name set but not matching the matcher. func inversePostingsForMatcher(ix IndexReader, m labels.Matcher) (index.Postings, error) { tpls, err := ix.LabelValues(m.Name()) if err != nil {