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

feat: remove unnecessary filter creations #4014

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ public class AllFilter implements Filter {
private final List<Filter> filters;
private static final Logger logger = LoggerFactory.getLogger(AllFilter.class);

public AllFilter(List<Filter> filters) {
public static Filter newFilter(List<Filter> filters) {
if (filters.size() == 1) {
return filters.getFirst();
}

return new AllFilter(filters);
}

private AllFilter(List<Filter> filters) {
this.filters = filters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ public class AnyFilter implements Filter {

private final List<Filter> filters;

public AnyFilter(List<Filter> filters) {
public static Filter newFilter(List<Filter> filters) {
if (filters.size() == 1) {
return filters.getFirst();
}

return new AnyFilter(filters);
}

private AnyFilter(List<Filter> filters) {
this.filters = filters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private Filter getFilter() {
}

private static Filter getFilter(List<DataPlaneContract.DialectedFilter> filters) {
return new AllFilter(
return AllFilter.newFilter(
filters.stream().map(ConsumerVerticleBuilder::getFilter).collect(Collectors.toList()));
}

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,25 @@ public void match(CloudEvent event, Filter filter, boolean shouldMatch) {

static Stream<Arguments> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,31 @@ public void match(CloudEvent event, Filter filter, boolean shouldMatch) {

static Stream<Arguments> 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));
}
}
Loading