Search in sources :

Example 1 with SqlgChooseStepBarrier

use of org.umlg.sqlg.step.barrier.SqlgChooseStepBarrier in project sqlg by pietermartin.

the class TestGremlinCompileChoose method testUnoptimizableChooseStep.

@Test
public void testUnoptimizableChooseStep() {
    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);
    a1.addEdge("ab", b2);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").choose(v -> v.label().equals("A"), __.out(), __.in());
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgChooseStepBarrier);
    Assert.assertEquals(2, vertices.size());
}
Also used : Arrays(java.util.Arrays) SqlgChooseStepBarrier(org.umlg.sqlg.step.barrier.SqlgChooseStepBarrier) BaseTest(org.umlg.sqlg.test.BaseTest) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Test(org.junit.Test) HashMap(java.util.HashMap) StopWatch(org.apache.commons.lang3.time.StopWatch) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) T(org.apache.tinkerpop.gremlin.structure.T) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) List(java.util.List) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Map(java.util.Map) TraversalOptionParent(org.apache.tinkerpop.gremlin.process.traversal.step.TraversalOptionParent) Assert(org.junit.Assert) MapHelper(org.apache.tinkerpop.gremlin.process.traversal.step.util.MapHelper) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgChooseStepBarrier(org.umlg.sqlg.step.barrier.SqlgChooseStepBarrier) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 2 with SqlgChooseStepBarrier

use of org.umlg.sqlg.step.barrier.SqlgChooseStepBarrier in project sqlg by pietermartin.

the class SqlgChooseStepStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    // noinspection OptionalGetWithoutIsPresent
    if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
        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;
        }
        if (predicateTraversal.getSteps().get(predicateTraversal.getSteps().size() - 1) instanceof HasNextStep) {
            predicateTraversal.removeStep(predicateTraversal.getSteps().get(predicateTraversal.getSteps().size() - 1));
        }
        SqlgBranchStepBarrier sqlgBranchStepBarrier = new SqlgChooseStepBarrier<>(traversal, predicateTraversal);
        for (String label : chooseStep.getLabels()) {
            sqlgBranchStepBarrier.addLabel(label);
        }
        try {
            Field traversalOptionsField = chooseStep.getClass().getSuperclass().getDeclaredField("traversalOptions");
            traversalOptionsField.setAccessible(true);
            Map<M, List<Traversal.Admin<S, E>>> traversalOptions = (Map<M, List<Traversal.Admin<S, E>>>) traversalOptionsField.get(chooseStep);
            for (Map.Entry<M, List<Traversal.Admin<S, E>>> entry : traversalOptions.entrySet()) {
                for (Traversal.Admin<S, E> admin : entry.getValue()) {
                    sqlgBranchStepBarrier.addGlobalChildOption(entry.getKey(), admin);
                }
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        TraversalHelper.replaceStep(chooseStep, sqlgBranchStepBarrier, chooseStep.getTraversal());
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) Field(java.lang.reflect.Field) List(java.util.List) ReducingBarrierStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ReducingBarrierStep) HasNextStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.HasNextStep) SqlgChooseStepBarrier(org.umlg.sqlg.step.barrier.SqlgChooseStepBarrier) Map(java.util.Map) SqlgBranchStepBarrier(org.umlg.sqlg.step.barrier.SqlgBranchStepBarrier)

Aggregations

List (java.util.List)2 Map (java.util.Map)2 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)2 SqlgChooseStepBarrier (org.umlg.sqlg.step.barrier.SqlgChooseStepBarrier)2 Field (java.lang.reflect.Field)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 StopWatch (org.apache.commons.lang3.time.StopWatch)1 DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)1 GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)1 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__)1 TraversalOptionParent (org.apache.tinkerpop.gremlin.process.traversal.step.TraversalOptionParent)1 ChooseStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep)1 HasNextStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.HasNextStep)1 MapHelper (org.apache.tinkerpop.gremlin.process.traversal.step.util.MapHelper)1 ReducingBarrierStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.ReducingBarrierStep)1 T (org.apache.tinkerpop.gremlin.structure.T)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1