Skip to content

Commit

Permalink
Merge pull request #151 from msaf1980/tagged_find_optimize
Browse files Browse the repository at this point in the history
Optimize tag find (low priority for globbed eq)
  • Loading branch information
Felixoid authored Apr 24, 2021
2 parents 7ea9950 + d8b09d7 commit 035b56d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
14 changes: 11 additions & 3 deletions finder/tagged.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ const (
)

type TaggedTerm struct {
Key string
Op TaggedTermOp
Value string
Key string
Op TaggedTermOp
Value string
HasWildcard bool // only for TaggedTermEq
}

type TaggedTermList []TaggedTerm
Expand All @@ -45,6 +46,12 @@ func (s TaggedTermList) Less(i, j int) bool {
if s[i].Op > s[j].Op {
return false
}

if s[i].Op == TaggedTermEq && !s[i].HasWildcard && s[j].HasWildcard {
// globs as fist eq might be have a bad perfomance
return true
}

if s[i].Key == "__name__" && s[j].Key != "__name__" {
return true
}
Expand Down Expand Up @@ -209,6 +216,7 @@ func ParseTaggedConditions(conditions []string) ([]TaggedTerm, error) {
switch op {
case "=":
terms[i].Op = TaggedTermEq
terms[i].HasWildcard = where.HasWildcard(terms[i].Value)
case "!=":
terms[i].Op = TaggedTermNe
case "=~":
Expand Down
3 changes: 3 additions & 0 deletions finder/tagged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func TestTaggedWhere(t *testing.T) {
}{
// info about _tag "directory"
{"seriesByTag('key=value')", "Tag1='key=value'", "", false},
// test case for wildcarded name, must be not first check
{"seriesByTag('name=*', 'key=value')", "(Tag1='key=value') AND (arrayExists((x) -> x LIKE '__name__=%', Tags))", "", false},
{"seriesByTag('name=*', 'key=value*')", "(Tag1 LIKE '__name__=%') AND (arrayExists((x) -> x LIKE 'key=value%', Tags))", "", false},
{"seriesByTag('name=rps')", "Tag1='__name__=rps'", "", false},
{"seriesByTag('name=~cpu.usage')", "Tag1 LIKE '\\\\_\\\\_name\\\\_\\\\_=%' AND match(Tag1, '^__name__=.*cpu.usage')", "Tag1 LIKE '\\\\_\\\\_name\\\\_\\\\_=%' AND match(Tag1, '^__name__=.*cpu.usage')", false},
{"seriesByTag('name=~cpu|mem')", "Tag1 LIKE '\\\\_\\\\_name\\\\_\\\\_=%' AND match(Tag1, '^__name__=.*cpu|mem')", "Tag1 LIKE '\\\\_\\\\_name\\\\_\\\\_=%' AND match(Tag1, '^__name__=.*cpu|mem')", false},
Expand Down

0 comments on commit 035b56d

Please sign in to comment.