Search in sources :

Example 31 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project sqlg by pietermartin.

the class TestHasLabelAndId method testOutEWithHasLabelAndId.

@Test
public void testOutEWithHasLabelAndId() {
    Vertex a = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex c = this.sqlgGraph.addVertex(T.label, "C");
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C");
    Vertex d = this.sqlgGraph.addVertex(T.label, "D");
    Vertex d2 = this.sqlgGraph.addVertex(T.label, "D");
    Edge ab = b.addEdge("ab", a);
    Edge ab2 = b2.addEdge("ab", a);
    Edge ac = c.addEdge("ac", a);
    Edge ac2 = c2.addEdge("ac", a);
    Edge ad = d.addEdge("ad", a);
    Edge ad2 = d2.addEdge("ad", a);
    this.sqlgGraph.tx().commit();
    GraphTraversal<Edge, Vertex> traversal = this.sqlgGraph.traversal().E().hasLabel("ab").inV();
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(2, vertices.size());
    Assert.assertEquals(a, vertices.get(0));
    Assert.assertEquals(a, vertices.get(1));
    traversal = this.sqlgGraph.traversal().E().hasLabel("ab").inV().hasLabel("B");
    vertices = traversal.toList();
    Assert.assertEquals(0, vertices.size());
    traversal = this.sqlgGraph.traversal().E().hasLabel("ab").inV().hasLabel("A");
    vertices = traversal.toList();
    Assert.assertEquals(2, vertices.size());
    Assert.assertEquals(a, vertices.get(0));
    Assert.assertEquals(a, vertices.get(1));
    traversal = this.sqlgGraph.traversal().E().has(T.label, P.within("ab", "ac", "ad")).inV().hasLabel("A", "B");
    vertices = traversal.toList();
    Assert.assertEquals(6, vertices.size());
    Assert.assertTrue(vertices.stream().allMatch(v -> v.equals(a)));
    traversal = this.sqlgGraph.traversal().E().has(T.label, P.within("ab", "ac", "ad")).outV().hasLabel("C", "B");
    vertices = traversal.toList();
    Assert.assertEquals(4, vertices.size());
    Assert.assertTrue(vertices.contains(b) && vertices.contains(b2) && vertices.contains(c) && vertices.contains(c2));
    traversal = this.sqlgGraph.traversal().E().has(T.label, P.within("ab", "ac", "ad")).outV().hasLabel("C", "B").has(T.id, P.eq(b));
    vertices = traversal.toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(b, vertices.get(0));
    traversal = this.sqlgGraph.traversal().E().has(T.label, P.within("ab", "ac", "ad")).outV().hasLabel("C", "B").has(T.id, P.neq(b));
    vertices = traversal.toList();
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(vertices.contains(b2) && vertices.contains(c) && vertices.contains(c2));
    traversal = this.sqlgGraph.traversal().E().has(T.label, P.within("ab", "ac", "ad")).outV().hasLabel("C", "B").has(T.id, P.within(b.id(), b2.id(), c.id()));
    vertices = traversal.toList();
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(vertices.contains(b) && vertices.contains(b2) && vertices.contains(c));
    traversal = this.sqlgGraph.traversal().E().has(T.label, P.within("ab", "ac", "ad")).outV().hasLabel("C", "B").has(T.id, P.without(b.id(), b2.id(), c.id()));
    vertices = traversal.toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(c2, vertices.get(0));
}
Also used : Arrays(java.util.Arrays) 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) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) T(org.apache.tinkerpop.gremlin.structure.T) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) HashSet(java.util.HashSet) List(java.util.List) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) Assert(org.junit.Assert) Collections(java.util.Collections) P(org.apache.tinkerpop.gremlin.process.traversal.P) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Edge(org.apache.tinkerpop.gremlin.structure.Edge) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 32 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project sqlg by pietermartin.

the class TestTinkerPopJira method testLazy1AddE.

// This return 3 for Sqlg and 2 on TinkerGraph
@Test
public void testLazy1AddE() {
    final Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    final Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    final Vertex c1 = this.sqlgGraph.addVertex(T.label, "C");
    a1.addEdge("ab", b1);
    a1.addEdge("ac", c1);
    this.sqlgGraph.tx().commit();
    GraphTraversal t = this.sqlgGraph.traversal().V(a1).both().addE("ab").from(a1).to(b1);
    printTraversalForm(t);
    List<Edge> edges = t.toList();
    this.sqlgGraph.tx().commit();
    Assert.assertEquals(3, edges.size());
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Edge(org.apache.tinkerpop.gremlin.structure.Edge) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 33 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project janusgraph by JanusGraph.

the class JanusGraphLocalQueryOptimizerStrategyTest method testDefaultConfiguration.

@Test
public void testDefaultConfiguration() {
    makeSampleGraph();
    // Edge
    assertNumStep(numV / 5, 1, g.V(sv[0]).outE("knows").has("weight", 1), JanusGraphVertexStep.class);
    assertNumStep(numV, 1, g.V(sv[0]).outE("knows"), JanusGraphVertexStep.class);
    assertNumStep(numV, 1, g.V(sv[0]).out("knows"), JanusGraphVertexStep.class);
    assertNumStep(10, 1, g.V(sv[0]).local(__.outE("knows").limit(10)), JanusGraphVertexStep.class);
    assertNumStep(10, 1, g.V(sv[0]).local(__.outE("knows").range(10, 20)), LocalStep.class);
    assertNumStep(numV, 2, g.V(sv[0]).outE("knows").order().by("weight", desc), JanusGraphVertexStep.class, OrderGlobalStep.class);
    // Ensure the LocalStep is dropped because the Order can be folded in the JanusGraphVertexStep which in turn
    // will allow JanusGraphLocalQueryOptimizationStrategy to drop the LocalStep as the local ordering will be
    // provided by the single JanusGraphVertex step
    assertNumStep(10, 0, g.V(sv[0]).local(__.outE("knows").order().by("weight", desc).limit(10)), LocalStep.class);
    assertNumStep(numV / 5, 2, g.V(sv[0]).outE("knows").has("weight", 1).order().by("weight", asc), JanusGraphVertexStep.class, OrderGlobalStep.class);
    assertNumStep(10, 0, g.V(sv[0]).local(__.outE("knows").has("weight", 1).order().by("weight", asc).limit(10)), LocalStep.class);
    // Note that for this test, the upper offset of the range will be folded into the JanusGraphVertexStep
    // by JanusGraphLocalQueryOptimizationStrategy, but not the lower offset. The RangeGlobalStep will in turn be kept
    // to enforce this lower bound and the LocalStep will be left as is as the local behavior will have not been
    // entirely subsumed by the JanusGraphVertexStep
    assertNumStep(5, 1, g.V(sv[0]).local(__.outE("knows").has("weight", 1).has("weight", 1).order().by("weight", asc).range(10, 15)), LocalStep.class);
    // Property
    assertNumStep(numV / 5, 1, g.V(sv[0]).properties("names").has("weight", 1), JanusGraphPropertiesStep.class);
    assertNumStep(numV, 1, g.V(sv[0]).properties("names"), JanusGraphPropertiesStep.class);
    assertNumStep(10, 0, g.V(sv[0]).local(__.properties("names").order().by("weight", desc).limit(10)), LocalStep.class);
    assertNumStep(numV, 2, g.V(sv[0]).outE("knows").values("weight"), JanusGraphVertexStep.class, JanusGraphPropertiesStep.class);
    // Global graph queries
    assertNumStep(1, 1, g.V().has("id", numV / 5), JanusGraphStep.class);
    assertNumStep(1, 1, g.V().has("id", numV / 5).has("weight", (numV / 5) % 5), JanusGraphStep.class);
    assertNumStep(numV / 5, 1, g.V().has("weight", 1), JanusGraphStep.class);
    assertNumStep(10, 1, g.V().has("weight", 1).range(0, 10), JanusGraphStep.class);
    assertNumStep(superV, 1, g.V().has("id", sid), JanusGraphStep.class);
    // Ensure that as steps don't interfere
    assertNumStep(1, 1, g.V().has("id", numV / 5).as("x"), JanusGraphStep.class);
    assertNumStep(1, 1, g.V().has("id", numV / 5).has("weight", (numV / 5) % 5).as("x"), JanusGraphStep.class);
    assertNumStep(superV * (numV / 5), 2, g.V().has("id", sid).outE("knows").has("weight", 1), JanusGraphStep.class, JanusGraphVertexStep.class);
    assertNumStep(superV * (numV / 5 * 2), 2, g.V().has("id", sid).outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)), JanusGraphStep.class, JanusGraphVertexStep.class);
    assertNumStep(superV * (numV / 5 * 2), 2, g.V().has("id", sid).outE("knows").has("weight", P.between(1, 3)), JanusGraphStep.class, JanusGraphVertexStep.class);
    assertNumStep(superV * 10, 2, g.V().has("id", sid).local(__.outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)).limit(10)), JanusGraphStep.class, JanusGraphVertexStep.class);
    assertNumStep(superV * 10, 1, g.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", desc).limit(10)), JanusGraphStep.class);
    assertNumStep(superV * 10, 0, g.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", desc).limit(10)), LocalStep.class);
    // Verify that the batch property pre-fetching is not applied when the configuration option is not set
    Traversal t = g.V().has("id", sid).outE("knows").has("weight", P.between(1, 3)).inV().has("weight", P.between(1, 3)).profile("~metrics");
    assertNumStep(superV * (numV / 5 * 2), 2, (GraphTraversal) t, JanusGraphStep.class, JanusGraphVertexStep.class);
    assertFalse(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIPREFETCH_ANNOTATION));
}
Also used : GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Test(org.junit.jupiter.api.Test)

Example 34 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project janusgraph by JanusGraph.

the class JanusGraphLocalQueryOptimizerStrategyTest method testBatchPropertyPrefetching.

@Test
public void testBatchPropertyPrefetching() {
    clopen(option(BATCH_PROPERTY_PREFETCHING), true);
    makeSampleGraph();
    // This tests an edge property before inV and will trigger the multiQuery property pre-fetch optimisation in JanusGraphEdgeVertexStep
    Traversal t = g.V().has("id", sid).outE("knows").has("weight", P.between(1, 3)).inV().has("weight", P.between(1, 3)).profile("~metrics");
    assertNumStep(superV * (numV / 5 * 2), 2, (GraphTraversal) t, JanusGraphStep.class, JanusGraphVertexStep.class);
    assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIPREFETCH_ANNOTATION));
    // This tests a vertex property after inV and will trigger the multiQuery property pre-fetch optimisation in JanusGraphVertexStep
    t = g.V().has("id", sid).outE("knows").inV().has("weight", P.between(1, 3)).profile("~metrics");
    assertNumStep(superV * (numV / 5 * 2), 2, (GraphTraversal) t, JanusGraphStep.class, JanusGraphVertexStep.class);
    assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIPREFETCH_ANNOTATION));
    // As above but with a limit after the has step meaning property pre-fetch won't know how much to fetch and so should not be used
    t = g.V().has("id", sid).outE("knows").inV().has("weight", P.between(1, 3)).limit(1000).profile("~metrics");
    assertNumStep(superV * (numV / 5 * 2), 2, (GraphTraversal) t, JanusGraphStep.class, JanusGraphVertexStep.class);
    assertFalse(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIPREFETCH_ANNOTATION));
}
Also used : GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Test(org.junit.jupiter.api.Test)

Example 35 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project janusgraph by JanusGraph.

the class JanusGraphMultiQueryStrategyTest method testMultiQuery.

@Test
public void testMultiQuery() {
    clopen(option(USE_MULTIQUERY), true);
    makeSampleGraph();
    Traversal t = g.V(sv[0]).outE().inV().choose(__.inE("knows").has("weight", 0), __.inE("knows").has("weight", 1), __.inE("knows").has("weight", 2)).profile("~metrics");
    assertNumStep(numV * 2, 2, (GraphTraversal) t, ChooseStep.class, JanusGraphVertexStep.class);
    assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
    t = g.V(sv[0]).outE().inV().union(__.inE("knows").has("weight", 0), __.inE("knows").has("weight", 1), __.inE("knows").has("weight", 2)).profile("~metrics");
    assertNumStep(numV * 6, 2, (GraphTraversal) t, UnionStep.class, JanusGraphVertexStep.class);
    assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
    // repeat starts from vertex with id 0 and goes in to the sv[0] vertex then loops back out to the vertex with the next id
    int[] loop = { 0 };
    t = g.V(vs[0], vs[1], vs[2]).repeat(__.inE("knows").outV().hasId(sv[0].id()).out(// TINKERPOP-2342
    "knows").sideEffect(e -> loop[0] = e.loops()).has("id", loop[0])).times(numV).profile("~metrics");
    assertNumStep(3, 1, (GraphTraversal) t, RepeatStep.class);
    assertEquals(numV - 1, loop[0]);
    assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
    t = g.V(vs[0], vs[1], vs[2]).optional(__.inE("knows").has("weight", 0)).profile("~metrics");
    assertNumStep(12, 1, (GraphTraversal) t, OptionalStep.class);
    assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
    t = g.V(vs[0], vs[1], vs[2]).filter(__.inE("knows").has("weight", 0)).profile("~metrics");
    assertNumStep(1, 1, (GraphTraversal) t, TraversalFilterStep.class);
    assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
    assertNumStep(superV * (numV / 5), 2, g.V().has("id", sid).outE("knows").has("weight", 1), JanusGraphStep.class, JanusGraphVertexStep.class);
    assertNumStep(superV * (numV / 5 * 2), 2, g.V().has("id", sid).outE("knows").has("weight", P.between(1, 3)), JanusGraphStep.class, JanusGraphVertexStep.class);
    assertNumStep(superV * 10, 2, g.V().has("id", sid).local(__.outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)).limit(10)), JanusGraphStep.class, JanusGraphVertexStep.class);
    assertNumStep(superV * 10, 1, g.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", desc).limit(10)), JanusGraphStep.class);
    assertNumStep(superV * 10, 0, g.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", desc).limit(10)), LocalStep.class);
    assertNumStep(superV * numV, 2, g.V().has("id", sid).values("names"), JanusGraphStep.class, JanusGraphPropertiesStep.class);
    // Verify traversal metrics when all reads are from cache (i.e. no backend queries)
    t = g.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", desc).limit(10)).profile("~metrics");
    assertCount(superV * 10, t);
    assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
    // Verify that properties also use multi query
    t = g.V().has("id", sid).values("names").profile("~metrics");
    assertCount(superV * numV, t);
    assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
}
Also used : Order.desc(org.apache.tinkerpop.gremlin.process.traversal.Order.desc) JanusGraphAssert.assertCount(org.janusgraph.testutil.JanusGraphAssert.assertCount) LIMIT_BATCH_SIZE(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.LIMIT_BATCH_SIZE) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) UnionStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.UnionStep) JanusGraphVertexStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphVertexStep) JanusGraphStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphStep) JanusGraphLocalQueryOptimizerStrategy(org.janusgraph.graphdb.tinkerpop.optimize.strategy.JanusGraphLocalQueryOptimizerStrategy) JanusGraphPropertiesStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphPropertiesStep) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) P(org.apache.tinkerpop.gremlin.process.traversal.P) USE_MULTIQUERY(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.USE_MULTIQUERY) Edge(org.apache.tinkerpop.gremlin.structure.Edge) NoOpBarrierStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep) QueryProfiler(org.janusgraph.graphdb.query.profile.QueryProfiler) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Test(org.junit.jupiter.api.Test) List(java.util.List) JanusGraphBaseTest.option(org.janusgraph.graphdb.JanusGraphBaseTest.option) JanusGraphAssert.assertNumStep(org.janusgraph.testutil.JanusGraphAssert.assertNumStep) JanusGraphAssert.queryProfilerAnnotationIsPresent(org.janusgraph.testutil.JanusGraphAssert.queryProfilerAnnotationIsPresent) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep) JanusGraphMultiQueryStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphMultiQueryStep) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Test(org.junit.jupiter.api.Test)

Aggregations

GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)93 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)67 Test (org.junit.Test)57 List (java.util.List)51 TestGraphBuilder.newGraph (nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph)50 VertexMatcher.likeVertex (nl.knaw.huygens.timbuctoo.util.VertexMatcher.likeVertex)44 Before (org.junit.Before)42 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)40 Matchers.contains (org.hamcrest.Matchers.contains)39 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)35 Lists (com.google.common.collect.Lists)34 FacetValue (nl.knaw.huygens.timbuctoo.search.FacetValue)31 Optional (java.util.Optional)24 P (org.apache.tinkerpop.gremlin.process.traversal.P)21 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__)20 ListFacetValue (nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.ListFacetValue)19 Matchers.is (org.hamcrest.Matchers.is)17 Mockito.mock (org.mockito.Mockito.mock)17 Matchers.empty (org.hamcrest.Matchers.empty)15 Edge (org.apache.tinkerpop.gremlin.structure.Edge)14