Search in sources :

Example 1 with LambdaFilterStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.filter.LambdaFilterStep in project sqlg by pietermartin.

the class SqlgChooseStepStrategy method apply.

@SuppressWarnings("unchecked")
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    // Only optimize SqlgGraph. StarGraph also passes through here.
    if (traversal.getGraph().isEmpty() || !(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) {
        return;
    }
    if (!SqlgTraversalUtil.mayOptimize(traversal)) {
        return;
    }
    List<ChooseStep> chooseSteps = TraversalHelper.getStepsOfAssignableClass(ChooseStep.class, traversal);
    for (ChooseStep<S, E, M> chooseStep : chooseSteps) {
        Traversal.Admin<S, M> predicateTraversal = chooseStep.getLocalChildren().get(0);
        // The predicate branch step is a local traversal.
        // As such if it contains a ReducingBarrierStep the SqlgBranchStepBarrier will not work.
        List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClass(ReducingBarrierStep.class, predicateTraversal);
        if (!reducingBarrierSteps.isEmpty()) {
            continue;
        }
        List<LambdaFilterStep> lambdaFilterSteps = TraversalHelper.getStepsOfAssignableClass(LambdaFilterStep.class, predicateTraversal);
        for (LambdaFilterStep lambdaFilterStep : lambdaFilterSteps) {
            SqlgLambdaFilterStep<?> sqlgLambdaFilterStep = new SqlgLambdaFilterStep<>(predicateTraversal, lambdaFilterStep.getPredicate());
            TraversalHelper.replaceStep(lambdaFilterStep, sqlgLambdaFilterStep, predicateTraversal);
            Step hasNextStep = predicateTraversal.getSteps().get(predicateTraversal.getSteps().size() - 1);
            if (hasNextStep instanceof HasNextStep) {
                SqlgHasNextStep<?> sqlgHasNextStep = new SqlgHasNextStep<>(predicateTraversal);
                TraversalHelper.replaceStep(hasNextStep, sqlgHasNextStep, predicateTraversal);
            }
        }
        SqlgBranchStepBarrier sqlgBranchStepBarrier = new SqlgChooseStepBarrier<>(traversal, predicateTraversal);
        for (String label : chooseStep.getLabels()) {
            sqlgBranchStepBarrier.addLabel(label);
        }
        try {
            // protected List<Pair<Traversal.Admin, Traversal.Admin<S, E>>> traversalOptions = new ArrayList<>();
            Field traversalOptionsField = chooseStep.getClass().getSuperclass().getDeclaredField("traversalOptions");
            traversalOptionsField.setAccessible(true);
            List<Pair<Traversal.Admin, Traversal.Admin<S, E>>> traversalOptions = (List<Pair<Traversal.Admin, Traversal.Admin<S, E>>>) traversalOptionsField.get(chooseStep);
            for (Pair<Traversal.Admin, Traversal.Admin<S, E>> traversalOptionPair : traversalOptions) {
                sqlgBranchStepBarrier.addGlobalChildOption(traversalOptionPair.getValue0(), traversalOptionPair.getValue1());
            }
            // protected Map<Pick, List<Traversal.Admin<S, E>>> traversalPickOptions = new HashMap<>();
            Field traversalPickOptionsField = chooseStep.getClass().getSuperclass().getDeclaredField("traversalPickOptions");
            traversalPickOptionsField.setAccessible(true);
            Map<TraversalOptionParent.Pick, List<Traversal.Admin<S, E>>> traversalPickOptions = (Map<TraversalOptionParent.Pick, List<Traversal.Admin<S, E>>>) traversalPickOptionsField.get(chooseStep);
            for (TraversalOptionParent.Pick pick : traversalPickOptions.keySet()) {
                for (Traversal.Admin<S, E> admin : traversalPickOptions.get(pick)) {
                    sqlgBranchStepBarrier.addGlobalChildOption(pick, admin);
                }
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        TraversalHelper.replaceStep(chooseStep, sqlgBranchStepBarrier, chooseStep.getTraversal());
    }
}
Also used : SqlgLambdaFilterStep(org.umlg.sqlg.step.SqlgLambdaFilterStep) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) SqlgHasNextStep(org.umlg.sqlg.step.SqlgHasNextStep) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) SqlgLambdaFilterStep(org.umlg.sqlg.step.SqlgLambdaFilterStep) LambdaFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.LambdaFilterStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) ReducingBarrierStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ReducingBarrierStep) HasNextStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.HasNextStep) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) SqlgHasNextStep(org.umlg.sqlg.step.SqlgHasNextStep) Field(java.lang.reflect.Field) List(java.util.List) ReducingBarrierStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ReducingBarrierStep) SqlgHasNextStep(org.umlg.sqlg.step.SqlgHasNextStep) HasNextStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.HasNextStep) Pair(org.javatuples.Pair) SqlgChooseStepBarrier(org.umlg.sqlg.step.barrier.SqlgChooseStepBarrier) Map(java.util.Map) SqlgBranchStepBarrier(org.umlg.sqlg.step.barrier.SqlgBranchStepBarrier) SqlgLambdaFilterStep(org.umlg.sqlg.step.SqlgLambdaFilterStep) LambdaFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.LambdaFilterStep) TraversalOptionParent(org.apache.tinkerpop.gremlin.process.traversal.step.TraversalOptionParent)

Aggregations

Field (java.lang.reflect.Field)1 List (java.util.List)1 Map (java.util.Map)1 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)1 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)1 TraversalOptionParent (org.apache.tinkerpop.gremlin.process.traversal.step.TraversalOptionParent)1 ChooseStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep)1 LambdaFilterStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.LambdaFilterStep)1 HasNextStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.HasNextStep)1 ReducingBarrierStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.ReducingBarrierStep)1 Pair (org.javatuples.Pair)1 SqlgHasNextStep (org.umlg.sqlg.step.SqlgHasNextStep)1 SqlgLambdaFilterStep (org.umlg.sqlg.step.SqlgLambdaFilterStep)1 SqlgBranchStepBarrier (org.umlg.sqlg.step.barrier.SqlgBranchStepBarrier)1 SqlgChooseStepBarrier (org.umlg.sqlg.step.barrier.SqlgChooseStepBarrier)1 SqlgGraph (org.umlg.sqlg.structure.SqlgGraph)1