Skip to content
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

Prefix filter optimizations #7309

Merged
merged 4 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 15 additions & 4 deletions pkg/eventfilter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (
)

const (
PassFilter FilterResult = "pass"
FailFilter FilterResult = "fail"
NoFilter FilterResult = "no_filter"
PassFilter FilterResult = iota
FailFilter
NoFilter
)

// FilterResult has the result of the filtering operation.
type FilterResult string
type FilterResult int

func (x FilterResult) And(y FilterResult) FilterResult {
if x == NoFilter {
Expand Down Expand Up @@ -57,6 +57,17 @@ func (x FilterResult) Or(y FilterResult) FilterResult {
return FailFilter
}

func (x FilterResult) String() string {
switch x {
case PassFilter:
return "PassFilter"
case FailFilter:
return "FailFilter"
default:
return "NoFilter"
}
}

// Filter is an interface representing an event filter of the trigger filter
type Filter interface {
// Filter compute the predicate on the provided event and returns the result of the matching
Expand Down
11 changes: 5 additions & 6 deletions pkg/eventfilter/subscriptionsapi/prefix_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ func (filter *prefixFilter) Filter(ctx context.Context, event cloudevents.Event)
if filter == nil {
return eventfilter.NoFilter
}
for attribute, value := range filter.filters {
if attribute == "" || value == "" {
return eventfilter.NoFilter
}
}
logger := logging.FromContext(ctx)
logger.Debugw("Performing a prefix match ", zap.Any("filters", filter.filters), zap.Any("event", event))
for k, v := range filter.filters {
Expand All @@ -64,7 +59,11 @@ func (filter *prefixFilter) Filter(ctx context.Context, event cloudevents.Event)
zap.Any("event", event))
return eventfilter.FailFilter
}
if !strings.HasPrefix(fmt.Sprintf("%v", value), v) {
var s string
if s, ok = value.(string); !ok {
s = fmt.Sprintf("%v", value)
}
if !strings.HasPrefix(s, v) {
return eventfilter.FailFilter
}
}
Expand Down
Loading