Search in sources :

Example 1 with AbstractStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep in project sqlg by pietermartin.

the class BaseStrategy method handleChooseStep.

private void handleChooseStep(int chooseStepNestedCount, ChooseStep<?, ?, ?> chooseStep, Traversal.Admin<?, ?> traversal, MutableInt pathCount) {
    // The currentTreeNode here is the node that will need the left join in the sql generation
    this.chooseStepStack.add(this.currentTreeNodeNode);
    Preconditions.checkState(this.chooseStepStack.size() == chooseStepNestedCount);
    List<? extends Traversal.Admin<?, ?>> globalChildren = chooseStep.getGlobalChildren();
    Preconditions.checkState(globalChildren.size() == 2, "ChooseStep's globalChildren must have size 2, one for true and one for false");
    ReplacedStep<?, ?> previousReplacedStep = this.sqlgStep.getReplacedSteps().get(this.sqlgStep.getReplacedSteps().size() - 1);
    previousReplacedStep.setLeftJoin(true);
    Traversal.Admin<?, ?> trueTraversal;
    Traversal.Admin<?, ?> a = globalChildren.get(0);
    Traversal.Admin<?, ?> b = globalChildren.get(1);
    if (a.getSteps().stream().anyMatch(s -> s instanceof IdentityStep<?>)) {
        trueTraversal = b;
    } else {
        trueTraversal = a;
    }
    List<Step<?, ?>> trueTraversalSteps = new ArrayList(trueTraversal.getSteps());
    ListIterator<Step<?, ?>> trueTraversalStepsIterator = trueTraversalSteps.listIterator();
    while (trueTraversalStepsIterator.hasNext()) {
        Step internalChooseStep = trueTraversalStepsIterator.next();
        if (internalChooseStep instanceof VertexStep || internalChooseStep instanceof EdgeVertexStep || internalChooseStep instanceof EdgeOtherVertexStep) {
            handleVertexStep(trueTraversalStepsIterator, (AbstractStep<?, ?>) internalChooseStep, pathCount);
            // I.e. there was no results for the chooseSteps traversal.
            for (int i = chooseStepNestedCount; i < this.chooseStepStack.size(); i++) {
                ReplacedStepTree.TreeNode treeNode = this.chooseStepStack.get(i);
                this.currentReplacedStep.markAsJoinToLeftJoin();
                treeNode.addReplacedStep(this.currentReplacedStep);
            }
        } else if (internalChooseStep instanceof ChooseStep) {
            handleChooseStep(chooseStepNestedCount + 1, (ChooseStep) internalChooseStep, traversal, pathCount);
        } else if (internalChooseStep instanceof ComputerAwareStep.EndStep) {
            break;
        } else {
            throw new IllegalStateException("Unhandled step nested in ChooseStep " + internalChooseStep.getClass().getName());
        }
    }
    // In that case it will not be a direct step of the traversal.
    if (traversal.getSteps().contains(chooseStep)) {
        traversal.removeStep(chooseStep);
    }
}
Also used : LoopTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) SqlgStep(org.umlg.sqlg.step.SqlgStep) SackValueStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SackValueStep) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) ComputerAwareStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) AbstractStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep) TreeSideEffectStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TreeSideEffectStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) ReplacedStepTree(org.umlg.sqlg.sql.parse.ReplacedStepTree)

Example 2 with AbstractStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep in project sqlg by pietermartin.

the class BaseStrategy method handleOptionalStep.

private void handleOptionalStep(int optionalStepNestedCount, OptionalStep<?> optionalStep, Traversal.Admin<?, ?> traversal, MutableInt pathCount) {
    // The currentTreeNode here is the node that will need the left join in the sql generation
    this.optionalStepStack.add(this.currentTreeNodeNode);
    Preconditions.checkState(this.optionalStepStack.size() == optionalStepNestedCount);
    Traversal.Admin<?, ?> optionalTraversal = optionalStep.getLocalChildren().get(0);
    ReplacedStep<?, ?> previousReplacedStep = this.sqlgStep.getReplacedSteps().get(this.sqlgStep.getReplacedSteps().size() - 1);
    previousReplacedStep.setLeftJoin(true);
    List<Step<?, ?>> optionalTraversalSteps = new ArrayList(optionalTraversal.getSteps());
    ListIterator<Step<?, ?>> optionalStepsIterator = optionalTraversalSteps.listIterator();
    while (optionalStepsIterator.hasNext()) {
        Step internalOptionalStep = optionalStepsIterator.next();
        if (internalOptionalStep instanceof VertexStep || internalOptionalStep instanceof EdgeVertexStep || internalOptionalStep instanceof EdgeOtherVertexStep) {
            handleVertexStep(optionalStepsIterator, (AbstractStep<?, ?>) internalOptionalStep, pathCount);
            // I.e. there was no results for the chooseSteps traversal.
            for (int i = optionalStepNestedCount; i < this.chooseStepStack.size(); i++) {
                ReplacedStepTree.TreeNode treeNode = this.chooseStepStack.get(i);
                this.currentReplacedStep.markAsJoinToLeftJoin();
                treeNode.addReplacedStep(this.currentReplacedStep);
            }
        } else if (internalOptionalStep instanceof OptionalStep) {
            handleOptionalStep(optionalStepNestedCount + 1, (OptionalStep) internalOptionalStep, traversal, pathCount);
        } else if (internalOptionalStep instanceof ComputerAwareStep.EndStep) {
            break;
        } else if (internalOptionalStep instanceof HasStep) {
            handleHasSteps(optionalStepsIterator, pathCount.getValue());
        } else {
            throw new IllegalStateException("Unhandled step nested in OptionalStep " + internalOptionalStep.getClass().getName());
        }
    }
    // In that case it will not be a direct step of the traversal.
    if (traversal.getSteps().contains(optionalStep)) {
        traversal.removeStep(optionalStep);
    }
}
Also used : OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) LoopTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) SqlgStep(org.umlg.sqlg.step.SqlgStep) SackValueStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SackValueStep) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) ComputerAwareStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) AbstractStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep) TreeSideEffectStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TreeSideEffectStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) ReplacedStepTree(org.umlg.sqlg.sql.parse.ReplacedStepTree)

Example 3 with AbstractStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep in project sqlg by pietermartin.

the class BaseStrategy method handleRepeatStep.

private void handleRepeatStep(RepeatStep<?> repeatStep, MutableInt pathCount) {
    List<? extends Traversal.Admin<?, ?>> repeatTraversals = repeatStep.getGlobalChildren();
    Traversal.Admin admin = repeatTraversals.get(0);
    LoopTraversal loopTraversal;
    long numberOfLoops;
    loopTraversal = (LoopTraversal) repeatStep.getUntilTraversal();
    numberOfLoops = loopTraversal.getMaxLoops();
    for (int i = 0; i < numberOfLoops; i++) {
        ListIterator<Step<?, ?>> repeatStepIterator = admin.getSteps().listIterator();
        while (repeatStepIterator.hasNext()) {
            Step internalRepeatStep = repeatStepIterator.next();
            if (internalRepeatStep instanceof RepeatStep.RepeatEndStep) {
                break;
            } else if (internalRepeatStep instanceof VertexStep || internalRepeatStep instanceof EdgeVertexStep || internalRepeatStep instanceof EdgeOtherVertexStep) {
                ReplacedStep<?, ?> replacedStepToEmit;
                // i.e. the currentReplacedStep before running handleVertexStep needs to be emitted.
                if (repeatStep.emitFirst) {
                    replacedStepToEmit = this.currentReplacedStep;
                    pathCount.decrement();
                    // noinspection ConstantConditions
                    replacedStepToEmit.setEmit(repeatStep.getEmitTraversal() != null);
                    replacedStepToEmit.setUntilFirst(repeatStep.untilFirst);
                    if (repeatStep.getLabels().isEmpty()) {
                        replacedStepToEmit.addLabel(pathCount + BaseStrategy.EMIT_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_FAKE_LABEL);
                    } else {
                        for (String label : repeatStep.getLabels()) {
                            replacedStepToEmit.addLabel(pathCount + BaseStrategy.EMIT_LABEL_SUFFIX + label);
                        }
                    }
                    pathCount.increment();
                }
                handleVertexStep(repeatStepIterator, (AbstractStep<?, ?>) internalRepeatStep, pathCount);
                pathCount.decrement();
                if (!repeatStep.emitFirst) {
                    replacedStepToEmit = this.currentReplacedStep;
                    // noinspection ConstantConditions
                    replacedStepToEmit.setEmit(repeatStep.getEmitTraversal() != null);
                    replacedStepToEmit.setUntilFirst(repeatStep.untilFirst);
                    if (repeatStep.getLabels().isEmpty()) {
                        replacedStepToEmit.addLabel(pathCount + BaseStrategy.EMIT_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_FAKE_LABEL);
                    } else {
                        for (String label : repeatStep.getLabels()) {
                            replacedStepToEmit.addLabel(pathCount + BaseStrategy.EMIT_LABEL_SUFFIX + label);
                        }
                    }
                }
                pathCount.increment();
                // If there is an emit we can not continue the optimization.
                this.reset = repeatStep.getEmitTraversal() != null;
            } else {
                throw new IllegalStateException("Unhandled step nested in RepeatStep " + internalRepeatStep.getClass().getName());
            }
        }
    }
    this.traversal.removeStep(repeatStep);
}
Also used : ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) LoopTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) SqlgStep(org.umlg.sqlg.step.SqlgStep) SackValueStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SackValueStep) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) ComputerAwareStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) AbstractStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep) TreeSideEffectStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TreeSideEffectStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) AbstractStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) LoopTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal)

Aggregations

LoopTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal)3 ChooseStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep)3 LocalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep)3 OptionalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep)3 RepeatStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep)3 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)3 SackValueStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SackValueStep)3 TreeSideEffectStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TreeSideEffectStep)3 AbstractStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep)3 ComputerAwareStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep)3 ReplacedStep (org.umlg.sqlg.sql.parse.ReplacedStep)3 SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)3 SqlgStep (org.umlg.sqlg.step.SqlgStep)3 SqlgVertexStep (org.umlg.sqlg.step.SqlgVertexStep)3 ReplacedStepTree (org.umlg.sqlg.sql.parse.ReplacedStepTree)2