Skip to content

Commit

Permalink
centralize generation of exprId
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Jul 26, 2023
1 parent 39f45ce commit ac8078e
Showing 1 changed file with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,31 @@ public PrototypePatternDef expr(PrototypeExpression left, ConstraintOperator ope
Prototype prototype = getPrototype();
Function1<PrototypeFact, Object> leftExtractor = left.asFunction(prototype);

AlphaIndex alphaIndex = null;
String exprId = "expr:" + left + ":" + operator + ":" + right;
Set<String> reactOnFields = new HashSet<>();
reactOnFields.addAll(left.getImpactedFields());
reactOnFields.addAll(right.getImpactedFields());

expr(createExprId(left, operator, right),
asPredicate1(leftExtractor, operator, right.asFunction(prototype)),
createAlphaIndex(left, operator, right, prototype, leftExtractor),
reactOn( reactOnFields.toArray(new String[reactOnFields.size()])) );

return this;
}

private static AlphaIndex createAlphaIndex(PrototypeExpression left, ConstraintOperator operator, PrototypeExpression right, Prototype prototype, Function1<PrototypeFact, Object> leftExtractor) {
if (left.getIndexingKey().isPresent() && right instanceof PrototypeExpression.FixedValue && operator instanceof Index.ConstraintType) {
String fieldName = left.getIndexingKey().get();
Index.ConstraintType constraintType = (Index.ConstraintType) operator;
Prototype.Field field = prototype.getField(fieldName);
Object value = ((PrototypeExpression.FixedValue) right).getValue();
exprId = "expr:" + fieldName + ":" + operator + ":" + value;

Class<Object> fieldClass = (Class<Object>) (field != null && field.isTyped() ? field.getType() : value != null ? value.getClass() : null);
if (fieldClass != null) {
alphaIndex = alphaIndexedBy(fieldClass, constraintType, getFieldIndex(prototype, fieldName, field), leftExtractor, value);
return alphaIndexedBy(fieldClass, constraintType, getFieldIndex(prototype, fieldName, field), leftExtractor, value);
}
}

Set<String> reactOnFields = new HashSet<>();
reactOnFields.addAll(left.getImpactedFields());
reactOnFields.addAll(right.getImpactedFields());

expr(exprId,
asPredicate1(leftExtractor, operator, right.asFunction(prototype)),
alphaIndex,
reactOn( reactOnFields.toArray(new String[reactOnFields.size()])) );

return this;
return null;
}

private static int getFieldIndex(Prototype prototype, String fieldName, Prototype.Field field) {
Expand All @@ -178,14 +177,20 @@ public PrototypePatternDef expr(PrototypeExpression left, ConstraintOperator ope
reactOnFields.addAll(left.getImpactedFields());
reactOnFields.addAll(right.getImpactedFields());

expr("expr:" + left.getIndexingKey().orElse(left.toString()) + ":" + operator + ":" + right,
expr(createExprId(left, operator, right),
other, asPredicate2(left.asFunction(prototype), operator, right.asFunction(otherPrototype)),
createBetaIndex(left, operator, right, prototype, otherPrototype),
reactOn( reactOnFields.toArray(new String[reactOnFields.size()])) );

return this;
}

private static String createExprId(PrototypeExpression left, ConstraintOperator operator, PrototypeExpression right) {
Object leftId = left.getIndexingKey().orElse(left.toString());
Object rightId = right instanceof PrototypeExpression.FixedValue ? ((PrototypeExpression.FixedValue) right).getValue() : right;
return "expr:" + leftId + ":" + operator + ":" + rightId;
}

private BetaIndex createBetaIndex(PrototypeExpression left, ConstraintOperator operator, PrototypeExpression right, Prototype prototype, Prototype otherPrototype) {
if (left.getIndexingKey().isPresent() && operator instanceof Index.ConstraintType && right.getIndexingKey().isPresent()) {
String fieldName = left.getIndexingKey().get();
Expand Down

0 comments on commit ac8078e

Please sign in to comment.