Search in sources :

Example 1 with RepeatStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep in project sqlg by pietermartin.

the class TestUnoptimizedRepeatStep method testRepeatStepWithLimit.

@Test
public void testRepeatStepWithLimit() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    a1.addEdge("ab", b1);
    a2.addEdge("ab", b2);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").until(__.has(T.label, "B")).repeat(__.out()).limit(1);
    Assert.assertEquals(4, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(2) instanceof RepeatStep);
    Assert.assertTrue(traversal.getSteps().get(3) instanceof RangeGlobalStep);
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgRepeatStepBarrier);
    Assert.assertEquals(1, vertices.size());
    Assert.assertTrue(vertices.contains(b1) || vertices.contains(b2));
    vertices = this.sqlgGraph.traversal().V().hasLabel("A").repeat(__.out()).until(__.has(T.label, "B")).limit(1).toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertTrue(vertices.contains(b1) || vertices.contains(b2));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgRepeatStepBarrier(org.umlg.sqlg.step.barrier.SqlgRepeatStepBarrier) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 2 with RepeatStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep in project unipop by unipop-graph.

the class UniGraphUnionStepStrategy method apply.

@Override
public void apply(Traversal.Admin<?, ?> traversal) {
    Graph graph = traversal.getGraph().get();
    if (!(graph instanceof UniGraph)) {
        return;
    }
    UniGraph uniGraph = (UniGraph) graph;
    TraversalHelper.getStepsOfClass(UnionStep.class, traversal).forEach(unionStep -> {
        Traversal.Admin[] traversals = (Traversal.Admin[]) unionStep.getGlobalChildren().toArray(new Traversal.Admin[0]);
        for (Traversal.Admin admin : traversals) {
            if (TraversalHelper.getLastStepOfAssignableClass(ReducingBarrierStep.class, admin).isPresent() || traversal.getParent() instanceof RepeatStep)
                return;
        }
        UniGraphUnionStep uniGraphUnionStep = new UniGraphUnionStep(traversal, uniGraph, traversals);
        if (TraversalHelper.stepIndex(unionStep, traversal) != -1) {
            TraversalHelper.replaceStep(unionStep, uniGraphUnionStep, traversal);
        } else {
            TraversalHelper.getStepsOfAssignableClass(TraversalParent.class, traversal).forEach(traversalParent -> {
                traversalParent.getLocalChildren().forEach(child -> {
                    if (TraversalHelper.stepIndex(unionStep, child) != -1) {
                        TraversalHelper.replaceStep(unionStep, uniGraphUnionStep, child);
                    }
                });
                traversalParent.getGlobalChildren().forEach(child -> {
                    if (TraversalHelper.stepIndex(unionStep, child) != -1) {
                        TraversalHelper.replaceStep(unionStep, uniGraphUnionStep, child);
                    }
                });
            });
        }
    });
}
Also used : UniGraph(org.unipop.structure.UniGraph) TraversalParent(org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent) Graph(org.apache.tinkerpop.gremlin.structure.Graph) UniGraph(org.unipop.structure.UniGraph) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) UnionStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.UnionStep)

Example 3 with RepeatStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep in project unipop by unipop-graph.

the class UniGraphRepeatStepStrategy method apply.

@Override
public void apply(Traversal.Admin<?, ?> traversal) {
    if (TraversalHelper.onGraphComputer(traversal))
        return;
    Graph graph = traversal.getGraph().get();
    if (!(graph instanceof UniGraph)) {
        return;
    }
    UniGraph uniGraph = (UniGraph) graph;
    TraversalHelper.getStepsOfClass(RepeatStep.class, traversal).forEach(repeatStep -> {
        if (TraversalHelper.hasStepOfClass(UnionStep.class, (Traversal.Admin) repeatStep.getGlobalChildren().get(0))) {
            return;
        }
        UniGraphRepeatStep uniGraphRepeatStep = new UniGraphRepeatStep(repeatStep, traversal.asAdmin(), uniGraph);
        if (repeatStep.getUntilTraversal() != null && TraversalHelper.getFirstStepOfAssignableClass(ReducingBarrierStep.class, repeatStep.getUntilTraversal()).isPresent())
            return;
        Traversal.Admin<?, ?> repeatTraversal = uniGraphRepeatStep.getRepeatTraversal();
        TraversalHelper.replaceStep(repeatStep, uniGraphRepeatStep, traversal);
        TraversalHelper.getStepsOfClass(RepeatStep.RepeatEndStep.class, repeatTraversal).forEach(repeatEndStep -> {
            UniGraphRepeatStep.RepeatEndStep uniGraphRepeatEndStep = new UniGraphRepeatStep.RepeatEndStep(repeatTraversal, uniGraphRepeatStep);
            TraversalHelper.replaceStep(repeatEndStep, uniGraphRepeatEndStep, repeatTraversal);
        });
    });
}
Also used : UniGraph(org.unipop.structure.UniGraph) Graph(org.apache.tinkerpop.gremlin.structure.Graph) UniGraph(org.unipop.structure.UniGraph) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal)

Example 4 with RepeatStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep 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 5 with RepeatStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep 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

RepeatStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep)5 Graph (org.apache.tinkerpop.gremlin.structure.Graph)3 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)2 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 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