Skip to content

Commit

Permalink
Prefix filter optimizations (#7309)
Browse files Browse the repository at this point in the history
* improved prefix filter

Signed-off-by: Calum Murray <[email protected]>

* switched to using iota instead of strings for filter results

Signed-off-by: Calum Murray <[email protected]>

* Implement Stringer interface for FilterResult

Signed-off-by: Calum Murray <[email protected]>

* Make closer to suffix filter

Signed-off-by: Calum Murray <[email protected]>

---------

Signed-off-by: Calum Murray <[email protected]>
  • Loading branch information
Cali0707 authored Oct 6, 2023
1 parent 8d2330c commit e40037b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
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

0 comments on commit e40037b

Please sign in to comment.