MatchNotRegexp optimization
Signed-off-by: Alan Protasio <alanprot@gmail.com>
This commit is contained in:
parent
bd98fc8c45
commit
c0f1abb574
|
@ -371,6 +371,14 @@ func inversePostingsForMatcher(ix IndexReader, m *labels.Matcher) (index.Posting
|
||||||
if m.Type == labels.MatchEqual && m.Value == "" {
|
if m.Type == labels.MatchEqual && m.Value == "" {
|
||||||
res = vals
|
res = vals
|
||||||
} else {
|
} else {
|
||||||
|
// Inverse of a MatchNotRegexp is MatchRegexp (double negation).
|
||||||
|
// Fast-path for set matching.
|
||||||
|
if m.Type == labels.MatchNotRegexp {
|
||||||
|
setMatches := findSetMatches(m.GetRegexString())
|
||||||
|
if len(setMatches) > 0 {
|
||||||
|
return ix.Postings(m.Name, setMatches...)
|
||||||
|
}
|
||||||
|
}
|
||||||
for _, val := range vals {
|
for _, val := range vals {
|
||||||
if !m.Matches(val) {
|
if !m.Matches(val) {
|
||||||
res = append(res, val)
|
res = append(res, val)
|
||||||
|
|
|
@ -114,6 +114,7 @@ func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) {
|
||||||
iCharSet := labels.MustNewMatcher(labels.MatchRegexp, "i", "1[0-9]")
|
iCharSet := labels.MustNewMatcher(labels.MatchRegexp, "i", "1[0-9]")
|
||||||
iAlternate := labels.MustNewMatcher(labels.MatchRegexp, "i", "(1|2|3|4|5|6|20|55)")
|
iAlternate := labels.MustNewMatcher(labels.MatchRegexp, "i", "(1|2|3|4|5|6|20|55)")
|
||||||
iXYZ := labels.MustNewMatcher(labels.MatchRegexp, "i", "X|Y|Z")
|
iXYZ := labels.MustNewMatcher(labels.MatchRegexp, "i", "X|Y|Z")
|
||||||
|
iNotXYZ := labels.MustNewMatcher(labels.MatchNotRegexp, "i", "X|Y|Z")
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
name string
|
name string
|
||||||
matchers []*labels.Matcher
|
matchers []*labels.Matcher
|
||||||
|
@ -131,6 +132,7 @@ func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) {
|
||||||
{`j=~"X.+"`, []*labels.Matcher{jXplus}},
|
{`j=~"X.+"`, []*labels.Matcher{jXplus}},
|
||||||
{`i=~"(1|2|3|4|5|6|20|55)"`, []*labels.Matcher{iAlternate}},
|
{`i=~"(1|2|3|4|5|6|20|55)"`, []*labels.Matcher{iAlternate}},
|
||||||
{`i=~"X|Y|Z"`, []*labels.Matcher{iXYZ}},
|
{`i=~"X|Y|Z"`, []*labels.Matcher{iXYZ}},
|
||||||
|
{`i!~"X|Y|Z"`, []*labels.Matcher{iNotXYZ}},
|
||||||
{`i=~".*"`, []*labels.Matcher{iStar}},
|
{`i=~".*"`, []*labels.Matcher{iStar}},
|
||||||
{`i=~"1.*"`, []*labels.Matcher{i1Star}},
|
{`i=~"1.*"`, []*labels.Matcher{i1Star}},
|
||||||
{`i=~".*1"`, []*labels.Matcher{iStar1}},
|
{`i=~".*1"`, []*labels.Matcher{iStar1}},
|
||||||
|
@ -146,6 +148,7 @@ func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) {
|
||||||
{`n="1",i!="",j=~"X.+"`, []*labels.Matcher{n1, iNotEmpty, jXplus}},
|
{`n="1",i!="",j=~"X.+"`, []*labels.Matcher{n1, iNotEmpty, jXplus}},
|
||||||
{`n="1",i!="",j=~"XXX|YYY"`, []*labels.Matcher{n1, iNotEmpty, jXXXYYY}},
|
{`n="1",i!="",j=~"XXX|YYY"`, []*labels.Matcher{n1, iNotEmpty, jXXXYYY}},
|
||||||
{`n="1",i=~"X|Y|Z",j="foo"`, []*labels.Matcher{n1, iXYZ, jFoo}},
|
{`n="1",i=~"X|Y|Z",j="foo"`, []*labels.Matcher{n1, iXYZ, jFoo}},
|
||||||
|
{`n="1",i!~"X|Y|Z",j="foo"`, []*labels.Matcher{n1, iNotXYZ, jFoo}},
|
||||||
{`n="1",i=~".+",j="foo"`, []*labels.Matcher{n1, iPlus, jFoo}},
|
{`n="1",i=~".+",j="foo"`, []*labels.Matcher{n1, iPlus, jFoo}},
|
||||||
{`n="1",i=~"1.+",j="foo"`, []*labels.Matcher{n1, i1Plus, jFoo}},
|
{`n="1",i=~"1.+",j="foo"`, []*labels.Matcher{n1, i1Plus, jFoo}},
|
||||||
{`n="1",i=~".*1.*",j="foo"`, []*labels.Matcher{n1, iStar1Star, jFoo}},
|
{`n="1",i=~".*1.*",j="foo"`, []*labels.Matcher{n1, iStar1Star, jFoo}},
|
||||||
|
|
|
@ -1801,6 +1801,19 @@ func TestPostingsForMatchers(t *testing.T) {
|
||||||
labels.FromStrings("n", "2.5"),
|
labels.FromStrings("n", "2.5"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchNotRegexp, "n", "1")},
|
||||||
|
exp: []labels.Labels{
|
||||||
|
labels.FromStrings("n", "2"),
|
||||||
|
labels.FromStrings("n", "2.5"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchNotRegexp, "n", "1|2.5")},
|
||||||
|
exp: []labels.Labels{
|
||||||
|
labels.FromStrings("n", "2"),
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchEqual, "n", "1"), labels.MustNewMatcher(labels.MatchNotRegexp, "i", "^a$")},
|
matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchEqual, "n", "1"), labels.MustNewMatcher(labels.MatchNotRegexp, "i", "^a$")},
|
||||||
exp: []labels.Labels{
|
exp: []labels.Labels{
|
||||||
|
@ -1890,6 +1903,14 @@ func TestPostingsForMatchers(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
|
name := ""
|
||||||
|
for i, matcher := range c.matchers {
|
||||||
|
if i > 0 {
|
||||||
|
name += ","
|
||||||
|
}
|
||||||
|
name += matcher.String()
|
||||||
|
}
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
exp := map[string]struct{}{}
|
exp := map[string]struct{}{}
|
||||||
for _, l := range c.exp {
|
for _, l := range c.exp {
|
||||||
exp[l.String()] = struct{}{}
|
exp[l.String()] = struct{}{}
|
||||||
|
@ -1911,6 +1932,7 @@ func TestPostingsForMatchers(t *testing.T) {
|
||||||
if len(exp) != 0 {
|
if len(exp) != 0 {
|
||||||
t.Errorf("Evaluating %v, missing results %+v", c.matchers, exp)
|
t.Errorf("Evaluating %v, missing results %+v", c.matchers, exp)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue