diff --git a/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/AllFilterBenchmark.java b/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/AllFilterBenchmark.java index 2faff3027e..1940295dc5 100644 --- a/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/AllFilterBenchmark.java +++ b/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/AllFilterBenchmark.java @@ -50,7 +50,7 @@ public static class AllFilterWithExactFilter extends FilterBenchmark { @Override protected Filter createFilter() { - return new AllFilter(List.of(makeExactFilter())); + return AllFilter.newFilter(List.of(makeExactFilter())); } @Override @@ -63,7 +63,7 @@ public static class AllFilterMatchAllSubFilters extends FilterBenchmark { @Override protected Filter createFilter() { - return new AllFilter(List.of(makeExactFilter(), makePrefixFilter(), makeSuffixFilter())); + return AllFilter.newFilter(List.of(makeExactFilter(), makePrefixFilter(), makeSuffixFilter())); } @Override @@ -76,7 +76,8 @@ public static class AllFilterFirstMatchEndOfArray extends FilterBenchmark { @Override protected Filter createFilter() { - return new AllFilter(List.of(makePrefixFilterNoMatch(), makeSuffixFilterNoMatch(), makeExactFilter())); + return AllFilter.newFilter( + List.of(makePrefixFilterNoMatch(), makeSuffixFilterNoMatch(), makeExactFilter())); } @Override @@ -89,7 +90,8 @@ public static class AllFilterFirstMatchStartOfArray extends FilterBenchmark { @Override protected Filter createFilter() { - return new AllFilter(List.of(makeExactFilter(), makePrefixFilterNoMatch(), makeSuffixFilterNoMatch())); + return AllFilter.newFilter( + List.of(makeExactFilter(), makePrefixFilterNoMatch(), makeSuffixFilterNoMatch())); } @Override @@ -102,7 +104,7 @@ public static class AllFilterOneNonMatchingFilterInMiddle extends FilterBenchmar @Override protected Filter createFilter() { - return new AllFilter(List.of(makeExactFilter(), makePrefixFilterNoMatch(), makePrefixFilter())); + return AllFilter.newFilter(List.of(makeExactFilter(), makePrefixFilterNoMatch(), makePrefixFilter())); } @Override @@ -115,7 +117,7 @@ public static class AllFilterNoMatchingFilters extends FilterBenchmark { @Override protected Filter createFilter() { - return new AllFilter(List.of(makePrefixFilterNoMatch(), makeSuffixFilterNoMatch())); + return AllFilter.newFilter(List.of(makePrefixFilterNoMatch(), makeSuffixFilterNoMatch())); } @Override diff --git a/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/AnyFilterBenchmark.java b/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/AnyFilterBenchmark.java index 702bc75d7f..36d0111351 100644 --- a/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/AnyFilterBenchmark.java +++ b/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/AnyFilterBenchmark.java @@ -56,7 +56,7 @@ public static class AnyFilterWithExactFilterBenchmark extends FilterBenchmark { @Override protected Filter createFilter() { - return new AnyFilter(List.of(makeExactFilter())); + return AnyFilter.newFilter(List.of(makeExactFilter())); } @Override @@ -69,7 +69,7 @@ public static class AnyFilterMatchAllSubfilters extends FilterBenchmark { @Override protected Filter createFilter() { - return new AnyFilter(List.of(makeExactFilter(), makePrefixFilter(), makeSuffixFilter())); + return AnyFilter.newFilter(List.of(makeExactFilter(), makePrefixFilter(), makeSuffixFilter())); } @Override @@ -82,7 +82,8 @@ public static class AnyFilterFirstMatchAtEnd extends FilterBenchmark { @Override protected Filter createFilter() { - return new AnyFilter(List.of(makePrefixFilterNoMatch(), makeSufficFilterNoMatch(), makeExactFilter())); + return AnyFilter.newFilter( + List.of(makePrefixFilterNoMatch(), makeSufficFilterNoMatch(), makeExactFilter())); } @Override @@ -95,7 +96,8 @@ public static class AnyFilterFirstMatchAtStart extends FilterBenchmark { @Override protected Filter createFilter() { - return new AnyFilter(List.of(makeExactFilter(), makePrefixFilterNoMatch(), makeSufficFilterNoMatch())); + return AnyFilter.newFilter( + List.of(makeExactFilter(), makePrefixFilterNoMatch(), makeSufficFilterNoMatch())); } @Override @@ -108,7 +110,7 @@ public static class AnyFilter2EventsMatch2DifferentFilters extends FilterBenchma @Override protected Filter createFilter() { - return new AnyFilter(List.of(makePrefixFilter(), makePrefixFilterNoMatch())); + return AnyFilter.newFilter(List.of(makePrefixFilter(), makePrefixFilterNoMatch())); } @Override @@ -120,7 +122,8 @@ protected CloudEvent createEvent() { public static class AnyFilter2EventsMatch2DifferentFiltersOneFilterMatchesNeither extends FilterBenchmark { @Override protected Filter createFilter() { - return new AnyFilter(List.of(makeSufficFilterNoMatch(), makePrefixFilter(), makePrefixFilterNoMatch())); + return AnyFilter.newFilter( + List.of(makeSufficFilterNoMatch(), makePrefixFilter(), makePrefixFilterNoMatch())); } @Override diff --git a/data-plane/dispatcher/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AllFilter.java b/data-plane/dispatcher/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AllFilter.java index e1a0ab87a0..9763e5c0ff 100644 --- a/data-plane/dispatcher/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AllFilter.java +++ b/data-plane/dispatcher/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AllFilter.java @@ -26,7 +26,15 @@ public class AllFilter implements Filter { private final List filters; private static final Logger logger = LoggerFactory.getLogger(AllFilter.class); - public AllFilter(List filters) { + public static Filter newFilter(List filters) { + if (filters.size() == 1) { + return filters.getFirst(); + } + + return new AllFilter(filters); + } + + private AllFilter(List filters) { this.filters = filters; } diff --git a/data-plane/dispatcher/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AnyFilter.java b/data-plane/dispatcher/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AnyFilter.java index 2325300801..2180b2dd49 100644 --- a/data-plane/dispatcher/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AnyFilter.java +++ b/data-plane/dispatcher/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AnyFilter.java @@ -27,7 +27,15 @@ public class AnyFilter implements Filter { private final List filters; - public AnyFilter(List filters) { + public static Filter newFilter(List filters) { + if (filters.size() == 1) { + return filters.getFirst(); + } + + return new AnyFilter(filters); + } + + private AnyFilter(List filters) { this.filters = filters; } diff --git a/data-plane/dispatcher/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/main/ConsumerVerticleBuilder.java b/data-plane/dispatcher/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/main/ConsumerVerticleBuilder.java index 230993df7d..0d51907aca 100644 --- a/data-plane/dispatcher/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/main/ConsumerVerticleBuilder.java +++ b/data-plane/dispatcher/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/main/ConsumerVerticleBuilder.java @@ -193,7 +193,7 @@ private Filter getFilter() { } private static Filter getFilter(List filters) { - return new AllFilter( + return AllFilter.newFilter( filters.stream().map(ConsumerVerticleBuilder::getFilter).collect(Collectors.toList())); } @@ -203,10 +203,10 @@ private static Filter getFilter(DataPlaneContract.DialectedFilter filter) { case PREFIX -> new PrefixFilter(filter.getPrefix().getAttributesMap()); case SUFFIX -> new SuffixFilter(filter.getSuffix().getAttributesMap()); case NOT -> new NotFilter(getFilter(filter.getNot().getFilter())); - case ANY -> new AnyFilter(filter.getAny().getFiltersList().stream() + case ANY -> AnyFilter.newFilter(filter.getAny().getFiltersList().stream() .map(ConsumerVerticleBuilder::getFilter) .collect(Collectors.toList())); - case ALL -> new AllFilter(filter.getAll().getFiltersList().stream() + case ALL -> AllFilter.newFilter(filter.getAll().getFiltersList().stream() .map(ConsumerVerticleBuilder::getFilter) .collect(Collectors.toList())); case CESQL -> new CeSqlFilter(filter.getCesql().getExpression()); diff --git a/data-plane/dispatcher/src/test/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AllFilterTest.java b/data-plane/dispatcher/src/test/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AllFilterTest.java index c77d6209a1..1461217a5c 100644 --- a/data-plane/dispatcher/src/test/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AllFilterTest.java +++ b/data-plane/dispatcher/src/test/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AllFilterTest.java @@ -51,25 +51,25 @@ public void match(CloudEvent event, Filter filter, boolean shouldMatch) { static Stream testCases() { return Stream.of( - Arguments.of(event, new AllFilter(List.of(new ExactFilter(Map.of("id", "123-42")))), true), + Arguments.of(event, AllFilter.newFilter(List.of(new ExactFilter(Map.of("id", "123-42")))), true), Arguments.of( event, - new AllFilter(List.of( + AllFilter.newFilter(List.of( new ExactFilter(Map.of("id", "123-42")), new ExactFilter(Map.of("source", "/api/some-source")))), true), Arguments.of( event, - new AllFilter(List.of( + AllFilter.newFilter(List.of( new ExactFilter(Map.of("id", "123")), new ExactFilter(Map.of("source", "/api/some-source")))), false), Arguments.of( event, - new AllFilter(List.of( + AllFilter.newFilter(List.of( new ExactFilter(Map.of("id", "123-42")), new ExactFilter(Map.of("source", "/api/something-else")))), false), - Arguments.of(event, new AllFilter(Collections.emptyList()), true)); + Arguments.of(event, AllFilter.newFilter(Collections.emptyList()), true)); } } diff --git a/data-plane/dispatcher/src/test/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AnyFilterTest.java b/data-plane/dispatcher/src/test/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AnyFilterTest.java index c1564c4cb4..e704895a9d 100644 --- a/data-plane/dispatcher/src/test/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AnyFilterTest.java +++ b/data-plane/dispatcher/src/test/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/subscriptionsapi/AnyFilterTest.java @@ -51,31 +51,31 @@ public void match(CloudEvent event, Filter filter, boolean shouldMatch) { static Stream testCases() { return Stream.of( - Arguments.of(event, new AnyFilter(List.of(new ExactFilter(Map.of("id", "123-42")))), true), + Arguments.of(event, AnyFilter.newFilter(List.of(new ExactFilter(Map.of("id", "123-42")))), true), Arguments.of( event, - new AnyFilter(List.of( + AnyFilter.newFilter(List.of( new ExactFilter(Map.of("id", "123-42")), new ExactFilter(Map.of("source", "/api/some-source")))), true), Arguments.of( event, - new AnyFilter(List.of( + AnyFilter.newFilter(List.of( new ExactFilter(Map.of("id", "123")), new ExactFilter(Map.of("source", "/api/some-source")))), true), Arguments.of( event, - new AnyFilter(List.of( + AnyFilter.newFilter(List.of( new ExactFilter(Map.of("id", "123-42")), new ExactFilter(Map.of("source", "/api/something-else")))), true), Arguments.of( event, - new AnyFilter(List.of( + AnyFilter.newFilter(List.of( new ExactFilter(Map.of("id", "123")), new ExactFilter(Map.of("source", "/api/something-else")))), false), - Arguments.of(event, new AnyFilter(Collections.emptyList()), false)); + Arguments.of(event, AnyFilter.newFilter(Collections.emptyList()), false)); } }