Search in sources :

Example 1 with SqlgGraphStep

use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.

the class SqlgWhereStrategy method apply.

@SuppressWarnings("resource")
@Override
public void apply(Admin<?, ?> traversal) {
    if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
        return;
    }
    SqlgGraph sqlgGraph = (SqlgGraph) traversal.getGraph().get();
    // The query will read from the cache if this is for a cached vertex
    if (sqlgGraph.features().supportsBatchMode() && sqlgGraph.tx().isInNormalBatchMode()) {
        sqlgGraph.tx().flush();
    }
    List<Step<?, ?>> steps = new ArrayList(traversal.asAdmin().getSteps());
    ListIterator<Step<?, ?>> stepIterator = steps.listIterator();
    // get all steps per label
    Map<String, Object> stepsByLabel = new HashMap<>();
    stepIterator = steps.listIterator();
    Step<?, ?> previous = null;
    int idx = 0;
    while (stepIterator.hasNext()) {
        Step<?, ?> step = stepIterator.next();
        captureLabels(step, stepsByLabel);
        if (step instanceof WherePredicateStep<?>) {
            WherePredicateStep<?> wps = (WherePredicateStep<?>) step;
            if (wps.getPredicate().isPresent() && wps.getPredicate().get().getBiPredicate() instanceof FullText) {
                Object referTo = previous;
                if (wps.getStartKey().isPresent()) {
                    referTo = stepsByLabel.get(wps.getStartKey().get());
                }
                if (referTo instanceof SqlgGraphStep<?, ?>) {
                    SqlgGraphStep<?, ?> sgs = (SqlgGraphStep<?, ?>) referTo;
                    if (sgs.getReplacedSteps().size() > 0) {
                        referTo = sgs.getReplacedSteps().get(sgs.getReplacedSteps().size() - 1);
                    }
                }
                if (referTo instanceof ReplacedStep<?, ?>) {
                    ReplacedStep<?, ?> rs = (ReplacedStep<?, ?>) referTo;
                    rs.addHasContainer(new HasContainer("__dummy__", wps.getPredicate().get()));
                    traversal.removeStep(idx);
                }
            }
        }
        previous = step;
        idx++;
    }
}
Also used : WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) FullText(org.umlg.sqlg.predicate.FullText)

Example 2 with SqlgGraphStep

use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.

the class TestPathStep method g_V_hasXlabel_personX_asXaX_localXoutXcreatedX_asXbXX_selectXa_bX_byXnameX_by.

@Test
public void g_V_hasXlabel_personX_asXaX_localXoutXcreatedX_asXbXX_selectXa_bX_byXnameX_by() throws IOException {
    Graph graph = this.sqlgGraph;
    loadModern(this.sqlgGraph);
    assertModernGraph(graph, true, false);
    GraphTraversalSource g = graph.traversal();
    DefaultGraphTraversal<Vertex, Map<String, Object>> traversal = (DefaultGraphTraversal<Vertex, Map<String, Object>>) g.V().has(T.label, "person").as("a").local(__.out("created").as("b")).select("a", "b").by("name").by(T.id);
    Assert.assertEquals(4, traversal.getSteps().size());
    printTraversalForm(traversal);
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertEquals(4, traversal.getSteps().size());
    int counter = 0;
    while (traversal.hasNext()) {
        final Map<String, Object> map = traversal.next();
        counter++;
        Assert.assertEquals(2, map.size());
        if (map.get("a").equals("marko")) {
            Assert.assertEquals(convertToVertexId("lop"), map.get("b"));
        } else if (map.get("a").equals("josh")) {
            Assert.assertTrue(convertToVertexId("lop").equals(map.get("b")) || convertToVertexId("ripple").equals(map.get("b")));
        } else if (map.get("a").equals("peter")) {
            Assert.assertEquals(convertToVertexId("lop"), map.get("b"));
        } else {
            Assert.fail("The following map should not have been returned: " + map);
        }
    }
    Assert.assertEquals(4, counter);
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) Graph(org.apache.tinkerpop.gremlin.structure.Graph) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) Map(java.util.Map) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 3 with SqlgGraphStep

use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.

the class TestPathStep method testSimplePath.

@Test
public void testSimplePath() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1", "age", 1);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal().V().as("a").has("name", "a1").as("b").has("age", 1).as("c").path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertEquals(3, traversal.getSteps().size());
    Assert.assertEquals(1, paths.size());
    Assert.assertEquals(1, paths.get(0).size());
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 4 with SqlgGraphStep

use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.

the class TestRangeLimit method testRangeOnVertexLabels.

@Test
public void testRangeOnVertexLabels() {
    for (int i = 0; i < 100; i++) {
        this.sqlgGraph.addVertex(T.label, "A", "name", "a" + i);
    }
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Object> g = (DefaultGraphTraversal<Vertex, Object>) this.sqlgGraph.traversal().V().hasLabel("A").order().by("name").range(1, 4).values("name");
    Assert.assertEquals(5, g.getSteps().size());
    ensureRangeGlobal(g);
    int cnt = 0;
    Set<String> names = new HashSet<>();
    String previous = null;
    if (g.hasNext()) {
        Assert.assertEquals(2, g.getSteps().size());
        Assert.assertTrue(g.getSteps().get(0) instanceof SqlgGraphStep);
        SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) g.getSteps().get(0);
        assertStep(sqlgGraphStep, true, false, false, false, true);
    }
    while (g.hasNext()) {
        String n = (String) g.next();
        names.add(n);
        if (previous != null) {
            Assert.assertTrue(previous.compareTo(n) < 0);
        }
        previous = n;
        cnt++;
    }
    ensureNoRangeGlobal(g);
    Assert.assertEquals(3, cnt);
    Assert.assertEquals(names.toString(), 3, names.size());
    Assert.assertTrue(names.toString(), names.contains("a1"));
    Assert.assertTrue(names.toString(), names.contains("a10"));
    Assert.assertTrue(names.toString(), names.contains("a11"));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) HashSet(java.util.HashSet) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 5 with SqlgGraphStep

use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.

the class TestRangeLimit method testLimitAfterNonOptimizedStep.

@Test
public void testLimitAfterNonOptimizedStep() {
    for (int i = 0; i < 100; i++) {
        Vertex column = this.sqlgGraph.addVertex(T.label, "BigData.Column");
        Vertex tag = this.sqlgGraph.addVertex(T.label, "BigData.Tag", "name", "NonAnonymized");
        tag.addEdge("tag", column);
    }
    for (int i = 0; i < 100; i++) {
        Vertex column = this.sqlgGraph.addVertex(T.label, "BigData.Column");
        Vertex tag = this.sqlgGraph.addVertex(T.label, "BigData.Tag", "name", "Anonymized");
        tag.addEdge("tag", column);
    }
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("BigData.Column").where(__.in("tag").hasLabel("BigData.Tag").has("name", "Anonymized")).limit(3);
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(3, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) traversal.getSteps().get(0);
    assertStep(sqlgGraphStep, true, false, false, true, true);
    Assert.assertEquals(3, vertices.size());
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Aggregations

SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)59 DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)57 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)57 Test (org.junit.Test)57 BaseTest (org.umlg.sqlg.test.BaseTest)57 ReplacedStep (org.umlg.sqlg.sql.parse.ReplacedStep)31 HashSet (java.util.HashSet)10 SqlgVertexStep (org.umlg.sqlg.step.SqlgVertexStep)6 Path (org.apache.tinkerpop.gremlin.process.traversal.Path)3 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)3 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)3 SqlgLocalStepBarrier (org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier)3 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)2 GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)2 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__)2 OrderGlobalStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep)2