-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENG-48446: Implementation for new Filter condition and filter convers…
…ion (#228) * remove lock changes * fix type * refactor and review comments * added filter builder and upsert * resolved review comments * nit * spotless * review comment
- Loading branch information
1 parent
7a59a85
commit 20f405e
Showing
7 changed files
with
421 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
config-service-impl/src/main/java/org/hypertrace/config/service/store/FilterBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package org.hypertrace.config.service.store; | ||
|
||
import static org.hypertrace.config.service.store.ConfigDocument.CONFIG_FIELD_NAME; | ||
|
||
import io.grpc.Status; | ||
import org.hypertrace.config.service.v1.LogicalFilter; | ||
import org.hypertrace.config.service.v1.RelationalFilter; | ||
import org.hypertrace.core.documentstore.Filter; | ||
|
||
class FilterBuilder { | ||
|
||
public Filter buildDocStoreFilter(org.hypertrace.config.service.v1.Filter filter) { | ||
switch (filter.getTypeCase()) { | ||
case LOGICAL_FILTER: | ||
return evaluateCompositeExpression(filter.getLogicalFilter()); | ||
case RELATIONAL_FILTER: | ||
return evaluateLeafExpression(filter.getRelationalFilter()); | ||
case TYPE_NOT_SET: | ||
default: | ||
throw Status.INVALID_ARGUMENT.withDescription("Filter type unset").asRuntimeException(); | ||
} | ||
} | ||
|
||
private Filter evaluateCompositeExpression(LogicalFilter logicalFilter) { | ||
switch (logicalFilter.getOperator()) { | ||
case LOGICAL_OPERATOR_OR: | ||
{ | ||
Filter[] childFilters = | ||
logicalFilter.getOperandsList().stream() | ||
.map(this::buildDocStoreFilter) | ||
.toArray(Filter[]::new); | ||
Filter filter = new Filter(); | ||
filter.setOp(Filter.Op.OR); | ||
filter.setChildFilters(childFilters); | ||
return filter; | ||
} | ||
case LOGICAL_OPERATOR_AND: | ||
{ | ||
Filter[] childFilters = | ||
logicalFilter.getOperandsList().stream() | ||
.map(this::buildDocStoreFilter) | ||
.toArray(Filter[]::new); | ||
Filter filter = new Filter(); | ||
filter.setOp(Filter.Op.AND); | ||
filter.setChildFilters(childFilters); | ||
return filter; | ||
} | ||
case LOGICAL_OPERATOR_UNSPECIFIED: | ||
default: | ||
throw Status.INVALID_ARGUMENT | ||
.withDescription("Unknown logical operator while building filter") | ||
.asRuntimeException(); | ||
} | ||
} | ||
|
||
private Filter evaluateLeafExpression(RelationalFilter relationalFilter) { | ||
switch (relationalFilter.getOperator()) { | ||
case RELATIONAL_OPERATOR_EQ: | ||
return new Filter( | ||
Filter.Op.EQ, | ||
buildConfigFieldPath(relationalFilter.getConfigJsonPath()), | ||
relationalFilter.getValue()); | ||
case RELATIONAL_OPERATOR_NEQ: | ||
return new Filter( | ||
Filter.Op.NEQ, | ||
buildConfigFieldPath(relationalFilter.getConfigJsonPath()), | ||
relationalFilter.getValue()); | ||
case RELATIONAL_OPERATOR_IN: | ||
return new Filter( | ||
Filter.Op.IN, | ||
buildConfigFieldPath(relationalFilter.getConfigJsonPath()), | ||
relationalFilter.getValue()); | ||
case RELATIONAL_OPERATOR_NOT_IN: | ||
return new Filter( | ||
Filter.Op.NOT_IN, | ||
buildConfigFieldPath(relationalFilter.getConfigJsonPath()), | ||
relationalFilter.getValue()); | ||
case RELATIONAL_OPERATOR_LT: | ||
return new Filter( | ||
Filter.Op.LT, | ||
buildConfigFieldPath(relationalFilter.getConfigJsonPath()), | ||
relationalFilter.getValue()); | ||
case RELATIONAL_OPERATOR_GT: | ||
return new Filter( | ||
Filter.Op.GT, | ||
buildConfigFieldPath(relationalFilter.getConfigJsonPath()), | ||
relationalFilter.getValue()); | ||
case RELATIONAL_OPERATOR_LTE: | ||
return new Filter( | ||
Filter.Op.LTE, | ||
buildConfigFieldPath(relationalFilter.getConfigJsonPath()), | ||
relationalFilter.getValue()); | ||
case RELATIONAL_OPERATOR_GTE: | ||
return new Filter( | ||
Filter.Op.GTE, | ||
buildConfigFieldPath(relationalFilter.getConfigJsonPath()), | ||
relationalFilter.getValue()); | ||
case UNRECOGNIZED: | ||
default: | ||
throw Status.INVALID_ARGUMENT | ||
.withDescription("Unknown relational operator while building filter") | ||
.asRuntimeException(); | ||
} | ||
} | ||
|
||
private String buildConfigFieldPath(String configJsonPath) { | ||
return String.format("%s.%s", CONFIG_FIELD_NAME, configJsonPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.