Search in sources :

Example 1 with LoopTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal in project sqlg by pietermartin.

the class BaseStrategy method unoptimizableRepeatStep.

private boolean unoptimizableRepeatStep() {
    List<RepeatStep> repeatSteps = TraversalHelper.getStepsOfAssignableClassRecursively(RepeatStep.class, this.traversal);
    boolean hasUntil = repeatSteps.stream().filter(s -> s.getClass().equals(RepeatStep.class)).allMatch(repeatStep -> repeatStep.getUntilTraversal() != null);
    boolean hasUnoptimizableUntil = false;
    if (hasUntil) {
        hasUnoptimizableUntil = repeatSteps.stream().filter(s -> s.getClass().equals(RepeatStep.class)).allMatch(repeatStep -> !(repeatStep.getUntilTraversal() instanceof LoopTraversal));
    }
    boolean badRepeat = !hasUntil || hasUnoptimizableUntil;
    // Check if the repeat step only contains optimizable steps
    if (!badRepeat) {
        List<Step> collectedRepeatInternalSteps = new ArrayList<>();
        for (Step step : repeatSteps) {
            RepeatStep repeatStep = (RepeatStep) step;
            List<Traversal.Admin> repeatTraversals = repeatStep.<Traversal.Admin>getGlobalChildren();
            Traversal.Admin admin = repeatTraversals.get(0);
            List<Step> repeatInternalSteps = admin.getSteps();
            collectedRepeatInternalSteps.addAll(repeatInternalSteps);
        }
        return !collectedRepeatInternalSteps.stream().filter(s -> !s.getClass().equals(RepeatStep.RepeatEndStep.class)).allMatch((s) -> isReplaceableStep(s.getClass()));
    } else {
        return true;
    }
}
Also used : org.apache.tinkerpop.gremlin.process.traversal(org.apache.tinkerpop.gremlin.process.traversal) AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) java.util(java.util) MutableInt(org.apache.commons.lang3.mutable.MutableInt) SqlgUtil(org.umlg.sqlg.util.SqlgUtil) Graph(org.apache.tinkerpop.gremlin.structure.Graph) FullText(org.umlg.sqlg.predicate.FullText) ZonedDateTime(java.time.ZonedDateTime) Text(org.umlg.sqlg.predicate.Text) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) 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) BiPredicate(java.util.function.BiPredicate) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) SqlgTraversalUtil(org.umlg.sqlg.util.SqlgTraversalUtil) Duration(java.time.Duration) OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) ComputerAwareStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep) HasContainerHolder(org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder) Range(org.apache.commons.lang3.Range) EventStrategy(org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy) Period(java.time.Period) org.apache.tinkerpop.gremlin.process.traversal.step.map(org.apache.tinkerpop.gremlin.process.traversal.step.map) TraversalHelper(org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) ReplacedStepTree(org.umlg.sqlg.sql.parse.ReplacedStepTree) org.apache.tinkerpop.gremlin.process.traversal.step.filter(org.apache.tinkerpop.gremlin.process.traversal.step.filter) Predicate(java.util.function.Predicate) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) T(org.apache.tinkerpop.gremlin.structure.T) Collectors(java.util.stream.Collectors) Pair(org.javatuples.Pair) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) 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) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer) Existence(org.umlg.sqlg.predicate.Existence) Preconditions(com.google.common.base.Preconditions) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) LoopTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) 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)

Example 2 with LoopTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal 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)2 ChooseStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep)2 LocalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep)2 OptionalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep)2 RepeatStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep)2 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)2 SackValueStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SackValueStep)2 TreeSideEffectStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TreeSideEffectStep)2 AbstractStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep)2 ComputerAwareStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep)2 ReplacedStep (org.umlg.sqlg.sql.parse.ReplacedStep)2 SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)2 SqlgStep (org.umlg.sqlg.step.SqlgStep)2 SqlgVertexStep (org.umlg.sqlg.step.SqlgVertexStep)2 Preconditions (com.google.common.base.Preconditions)1 Duration (java.time.Duration)1 Period (java.time.Period)1 ZonedDateTime (java.time.ZonedDateTime)1 java.util (java.util)1 BiPredicate (java.util.function.BiPredicate)1