Skip to content

Commit

Permalink
Optimized the exact filter performance
Browse files Browse the repository at this point in the history
Signed-off-by: Calum Murray <[email protected]>
  • Loading branch information
Cali0707 committed Sep 27, 2023
1 parent 44aa15e commit 42d850a
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions pkg/eventfilter/subscriptionsapi/exact_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ import (
)

type exactFilter struct {
filters map[string]string
attrsFilter eventfilter.Filter
filters map[string]string
}

// NewExactFilter returns an event filter which passes if value exactly matches the value of the context
Expand All @@ -44,23 +43,31 @@ func NewExactFilter(filters map[string]string) (eventfilter.Filter, error) {
}
return &exactFilter{
filters: filters,
// we're creating this filter to leverage the same filter logic of the existing attributes filter
attrsFilter: attributes.NewAttributesFilter(filters),
}, nil
}

func (filter *exactFilter) Filter(ctx context.Context, event cloudevents.Event) eventfilter.FilterResult {
if filter.filters == nil {
return eventfilter.NoFilter
}
for attribute, value := range filter.filters {
if attribute == "" || value == "" {
return eventfilter.NoFilter
}
}
logger := logging.FromContext(ctx)
logger.Debugw("Performing an exact match ", zap.Any("filters", filter.filters), zap.Any("event", event))
return filter.attrsFilter.Filter(ctx, event)
for k, v := range filter.filters {
value, ok := attributes.LookupAttribute(event, k)
if !ok {
logger.Debug("Attribute not found", zap.String("attribute", k))
return eventfilter.FailFilter
}
var s string
if s, ok = value.(string); !ok {
s = fmt.Sprintf("%v", value)
}
if s != v {
logger.Debug("Attribute had non-matching value", zap.String("attribute", k), zap.String("filter", v), zap.Any("received", value))
return eventfilter.FailFilter
}
}
return eventfilter.PassFilter
}

func (filter *exactFilter) Cleanup() {}

0 comments on commit 42d850a

Please sign in to comment.