Skip to content

Commit

Permalink
topdown: glob|regex code nitpicks (#7071)
Browse files Browse the repository at this point in the history
No functional changes, just

- using ast.String(..) instead of ast.StringTerm(..).Value
- using const where package-level strings never change (metrics names)

Signed-off-by: Stephan Renatus <stephan@styra.com>
  • Loading branch information
srenatus authored Sep 26, 2024
1 parent 4ba95d0 commit 5cbc1e0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
22 changes: 8 additions & 14 deletions topdown/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import (
)

const globCacheMaxSize = 100
const globInterQueryValueCacheHits = "rego_builtin_glob_interquery_value_cache_hits"

var globCacheLock = sync.Mutex{}
var globCache map[string]glob.Glob

var globInterQueryValueCacheHits = "rego_builtin_glob_interquery_value_cache_hits"

func builtinGlobMatch(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
pattern, err := builtins.StringOperand(operands[0].Value, 1)
if err != nil {
Expand Down Expand Up @@ -62,34 +61,29 @@ func builtinGlobMatch(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.
func globCompileAndMatch(bctx BuiltinContext, id, pattern, match string, delimiters []rune) (bool, error) {

if bctx.InterQueryBuiltinValueCache != nil {
val, ok := bctx.InterQueryBuiltinValueCache.Get(ast.StringTerm(id).Value)
val, ok := bctx.InterQueryBuiltinValueCache.Get(ast.String(id))
if ok {
pat, valid := val.(glob.Glob)
if !valid {
// The cache key may exist for a different value type (eg. regex).
// In this case, we calculate the glob and return the result w/o updating the cache.
var res glob.Glob
var err error
if res, err = glob.Compile(pattern, delimiters...); err != nil {
if pat, err = glob.Compile(pattern, delimiters...); err != nil {
return false, err
}
out := res.Match(match)
return out, nil
return pat.Match(match), nil
}
bctx.Metrics.Counter(globInterQueryValueCacheHits).Incr()
out := pat.Match(match)
return out, nil
}

var res glob.Glob
var err error
if res, err = glob.Compile(pattern, delimiters...); err != nil {
res, err := glob.Compile(pattern, delimiters...)
if err != nil {
return false, err
}
bctx.InterQueryBuiltinValueCache.Insert(ast.StringTerm(id).Value, res)

out := res.Match(match)
return out, nil
bctx.InterQueryBuiltinValueCache.Insert(ast.String(id), res)
return res.Match(match), nil
}

globCacheLock.Lock()
Expand Down
13 changes: 4 additions & 9 deletions topdown/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import (
)

const regexCacheMaxSize = 100
const regexInterQueryValueCacheHits = "rego_builtin_regex_interquery_value_cache_hits"

var regexpCacheLock = sync.Mutex{}
var regexpCache map[string]*regexp.Regexp

var regexInterQueryValueCacheHits = "rego_builtin_regex_interquery_value_cache_hits"

func builtinRegexIsValid(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {

s, err := builtins.StringOperand(operands[0].Value, 1)
Expand Down Expand Up @@ -107,17 +106,13 @@ func builtinRegexSplit(bctx BuiltinContext, operands []*ast.Term, iter func(*ast

func getRegexp(bctx BuiltinContext, pat string) (*regexp.Regexp, error) {
if bctx.InterQueryBuiltinValueCache != nil {
val, ok := bctx.InterQueryBuiltinValueCache.Get(ast.StringTerm(pat).Value)
val, ok := bctx.InterQueryBuiltinValueCache.Get(ast.String(pat))
if ok {
res, valid := val.(*regexp.Regexp)
if !valid {
// The cache key may exist for a different value type (eg. glob).
// In this case, we calculate the regex and return the result w/o updating the cache.
re, err := regexp.Compile(pat)
if err != nil {
return nil, err
}
return re, nil
return regexp.Compile(pat)
}

bctx.Metrics.Counter(regexInterQueryValueCacheHits).Incr()
Expand All @@ -128,7 +123,7 @@ func getRegexp(bctx BuiltinContext, pat string) (*regexp.Regexp, error) {
if err != nil {
return nil, err
}
bctx.InterQueryBuiltinValueCache.Insert(ast.StringTerm(pat).Value, re)
bctx.InterQueryBuiltinValueCache.Insert(ast.String(pat), re)
return re, nil
}

Expand Down

0 comments on commit 5cbc1e0

Please sign in to comment.