Skip to content

filter-plugin: Inner Txn support. #1487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ignore:
- "idb/mocks"
- "idb/dummy"
- "util/test"
- "conduit/plugins/processors/filterprocessor/fields/generated_signed_txn_map.go"

coverage:
precision: 2
Expand Down
4 changes: 3 additions & 1 deletion conduit/plugins/processors/filterprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ type SubConfig struct {
<li>not-equal</li>
</ul>
*/
ExpressionType expression.FilterType `yaml:"expression-type"`
ExpressionType expression.Type `yaml:"expression-type"`
// <code>expression</code> is the user-supplied part of the search or comparison.
Expression string `yaml:"expression"`
}

// Config configuration for the filter processor
type Config struct {
// <code>search-inner</code> configures the filter processor to recursively search inner transactions for expressions.
SearchInner bool `yaml:"search-inner"`
/* <code>filters</code> are a list of SubConfig objects with an operation acting as the string key in the map

filters:
Expand Down
100 changes: 58 additions & 42 deletions conduit/plugins/processors/filterprocessor/expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,48 @@ import (
"strconv"
)

// FilterType is the type of the filter (i.e. const, regex, etc)
type FilterType string
// Type is the type of the filter (i.e. const, regex, etc)
type Type string

const (
// EqualToFilter a filter that applies numerical and string equal to operations
EqualToFilter FilterType = "equal"
// RegexFilter a filter that applies regex rules to the matching
RegexFilter FilterType = "regex"

// LessThanFilter a filter that applies numerical less than operation
LessThanFilter FilterType = "less-than"
// LessThanEqualFilter a filter that applies numerical less than or equal operation
LessThanEqualFilter FilterType = "less-than-equal"
// GreaterThanFilter a filter that applies numerical greater than operation
GreaterThanFilter FilterType = "greater-than"
// GreaterThanEqualFilter a filter that applies numerical greater than or equal operation
GreaterThanEqualFilter FilterType = "greater-than-equal"
// NotEqualToFilter a filter that applies numerical NOT equal to operation
NotEqualToFilter FilterType = "not-equal"
// EqualTo a filter that applies numerical and string equal to operations
EqualTo Type = "equal"
// Regex a filter that applies regex rules to the matching
Regex Type = "regex"

// LessThan a filter that applies numerical less than operation
LessThan Type = "less-than"
// LessThanEqual a filter that applies numerical less than or equal operation
LessThanEqual Type = "less-than-equal"
// GreaterThan a filter that applies numerical greater than operation
GreaterThan Type = "greater-than"
// GreaterThanEqual a filter that applies numerical greater than or equal operation
GreaterThanEqual Type = "greater-than-equal"
// NotEqualTo a filter that applies numerical NOT equal to operation
NotEqualTo Type = "not-equal"
)

// TypeMap contains all the expression types for validation.
var TypeMap = map[FilterType]interface{}{
RegexFilter: struct{}{},
LessThanFilter: struct{}{},
LessThanEqualFilter: struct{}{},
GreaterThanFilter: struct{}{},
GreaterThanEqualFilter: struct{}{},
EqualToFilter: struct{}{},
NotEqualToFilter: struct{}{},
var TypeMap = map[Type]interface{}{
Regex: struct{}{},
LessThan: struct{}{},
LessThanEqual: struct{}{},
GreaterThan: struct{}{},
GreaterThanEqual: struct{}{},
EqualTo: struct{}{},
NotEqualTo: struct{}{},
}

// Expression the expression interface
type Expression interface {
Search(input interface{}) (bool, error)
Match(input interface{}) (bool, error)
}

type regexExpression struct {
Regex *regexp.Regexp
}

func (e *regexExpression) Search(input interface{}) (bool, error) {
func (e *regexExpression) Match(input interface{}) (bool, error) {
switch v := input.(type) {
case string:
return e.Regex.MatchString(v), nil
Expand All @@ -56,10 +56,20 @@ func (e *regexExpression) Search(input interface{}) (bool, error) {
}
}

func makeRegexExpression(searchStr string, expressionType FilterType) (Expression, error) {
if expressionType != EqualToFilter && expressionType != RegexFilter {
return nil, fmt.Errorf("target type (string) does not support %s filters", expressionType)
type stringEqualExpression struct {
Str string
}

func (e *stringEqualExpression) Match(input interface{}) (bool, error) {
switch v := input.(type) {
case string:
return e.Str == v, nil
default:
return false, fmt.Errorf("unexpected regex search input type (%T)", v)
}
}
Comment on lines +59 to +70
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously this was using a special regex expression. In benchmarks using string equals was 8-12% faster.


func makeRegexExpression(searchStr string) (Expression, error) {
r, err := regexp.Compile(searchStr)
if err != nil {
return nil, err
Expand All @@ -68,8 +78,8 @@ func makeRegexExpression(searchStr string, expressionType FilterType) (Expressio
return &regexExpression{Regex: r}, nil
}

func makeSignedExpression(searchStr string, expressionType FilterType) (Expression, error) {
if expressionType == RegexFilter {
func makeSignedExpression(searchStr string, expressionType Type) (Expression, error) {
if expressionType == Regex {
return nil, fmt.Errorf("target type (numeric) does not support %s filters", expressionType)
}

Expand All @@ -84,8 +94,8 @@ func makeSignedExpression(searchStr string, expressionType FilterType) (Expressi
}, nil
}

func makeUnsignedExpression(searchStr string, expressionType FilterType) (Expression, error) {
if expressionType == RegexFilter {
func makeUnsignedExpression(searchStr string, expressionType Type) (Expression, error) {
if expressionType == Regex {
return nil, fmt.Errorf("target type (numeric) does not support %s filters", expressionType)
}

Expand All @@ -101,19 +111,25 @@ func makeUnsignedExpression(searchStr string, expressionType FilterType) (Expres
}

// MakeExpression creates an expression based on an expression type
func MakeExpression(filterType FilterType, expressionSearchStr string, target interface{}) (exp Expression, err error) {
func MakeExpression(expressionType Type, expressionSearchStr string, target interface{}) (exp Expression, err error) {
if _, ok := TypeMap[expressionType]; !ok {
return nil, fmt.Errorf("expression type (%s) is not supported", expressionType)
}

switch t := target.(type) {
case uint64:
return makeUnsignedExpression(expressionSearchStr, filterType)
return makeUnsignedExpression(expressionSearchStr, expressionType)
case int64:
return makeSignedExpression(expressionSearchStr, filterType)
return makeSignedExpression(expressionSearchStr, expressionType)
case string:
if filterType == EqualToFilter {
// Equal to for strings is a special case of the regex pattern.
expressionSearchStr = fmt.Sprintf("^%s$", regexp.QuoteMeta(expressionSearchStr))
switch expressionType {
case EqualTo:
return &stringEqualExpression{Str: expressionSearchStr}, nil
case Regex:
return makeRegexExpression(expressionSearchStr)
default:
return nil, fmt.Errorf("target type (string) does not support %s filters", expressionType)
}
return makeRegexExpression(expressionSearchStr, filterType)

default:
return nil, fmt.Errorf("unknown expression type: %T", t)
}
Expand Down
Loading