Search in sources :

Example 36 with GraphTraversal

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

the class JanusGraphIndexTest method testOrderByWithRange.

/**
 * This test builds a mixed index and tests index queries with order and range/limit.
 * It also tests if index query cache is utilised correctly.
 */
@Test
public void testOrderByWithRange() {
    final PropertyKey age = makeKey("age", Integer.class);
    final JanusGraphIndex mixed = mgmt.buildIndex("mixed", Vertex.class).addKey(age).buildMixedIndex(INDEX);
    finishSchema();
    for (int i = 0; i < 100; i++) {
        tx.addVertex("age", i);
    }
    tx.commit();
    Supplier<GraphTraversal> common = () -> graph.traversal().V().has("age", P.gte(0)).order();
    Supplier<GraphTraversal> traversal;
    // traverse with limit 30 (cache cold miss)
    traversal = () -> common.get().by(ORDER_AGE_ASC).limit(30).values("age");
    assertBackendHit((TraversalMetrics) traversal.get().profile().next());
    assertIntRange(traversal.get(), 0, 30);
    traversal = () -> common.get().by(ORDER_AGE_DESC).limit(30).values("age");
    assertBackendHit((TraversalMetrics) traversal.get().profile().next());
    assertIntRange(traversal.get(), 99, 69);
    // traverse with limit 30 (cache hit)
    traversal = () -> common.get().by(ORDER_AGE_ASC).limit(30).values("age");
    assertNoBackendHit((TraversalMetrics) traversal.get().profile().next());
    assertIntRange(traversal.get(), 0, 30);
    // traverse with limit followed by orderBy
    traversal = () -> graph.traversal().V().has("age", P.gte(0)).limit(30).order().by(ORDER_AGE_ASC).values("age");
    assertBackendHit((TraversalMetrics) traversal.get().profile().next());
    assertNoBackendHit((TraversalMetrics) traversal.get().profile().next());
    // traverse with range(10, 20) (cache hit)
    traversal = () -> common.get().by(ORDER_AGE_DESC).range(10, 20).values("age");
    assertNoBackendHit((TraversalMetrics) traversal.get().profile().next());
    assertIntRange(traversal.get(), 89, 79);
}
Also used : GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) RepeatedIfExceptionsTest(io.github.artsok.RepeatedIfExceptionsTest) Test(org.junit.jupiter.api.Test)

Example 37 with GraphTraversal

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

the class JanusGraphSONModuleTest method testTinkerPopPredicatesAsGraphSON.

@ParameterizedTest(name = "GraphSON Version: {0}")
@EnumSource(value = GraphSONVersion.class, mode = EnumSource.Mode.EXCLUDE, names = { "V1_0" })
public void testTinkerPopPredicatesAsGraphSON(GraphSONVersion graphSONVersion) throws Exception {
    Graph graph = EmptyGraph.instance();
    GraphTraversalSource g = graph.traversal();
    GraphTraversal[] traversals = { g.V().has("age", gt(13)), g.V().has("age", within(20, 29)), g.V().has("age", P.not(within(20, 29))) };
    graphsonSerializationTest(traversals, graphSONVersion);
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Graph(org.apache.tinkerpop.gremlin.structure.Graph) EmptyGraph(org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 38 with GraphTraversal

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

the class JanusGraphSONModuleTest method graphsonSerializationTest.

private void graphsonSerializationTest(GraphTraversal[] traversals, GraphSONVersion version) throws Exception {
    final GraphSONMapper mapper = GraphSONMapper.build().version(version).typeInfo(TypeInfo.PARTIAL_TYPES).addRegistry(JanusGraphIoRegistry.instance()).create();
    final GraphSONWriter writer = GraphSONWriter.build().mapper(mapper).create();
    final GraphSONReader reader = GraphSONReader.build().mapper(mapper).create();
    for (GraphTraversal traversal : traversals) {
        Bytecode expectedBytecode = traversal.asAdmin().getBytecode();
        ByteArrayOutputStream serializationStream = new ByteArrayOutputStream();
        writer.writeObject(serializationStream, expectedBytecode);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(serializationStream.toByteArray());
        Bytecode result = reader.readObject(inputStream, Bytecode.class);
        assertEquals(expectedBytecode, result);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) GraphSONMapper(org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper) GraphSONWriter(org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Bytecode(org.apache.tinkerpop.gremlin.process.traversal.Bytecode) GraphSONReader(org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 39 with GraphTraversal

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

the class JanusGraphIoRegistryTest method testTokenIoRegistyInConfig.

@Test
public void testTokenIoRegistyInConfig() throws SerializationException {
    final GraphSONMessageSerializerV3d0 serializer = new GraphSONMessageSerializerV3d0();
    final Map<String, Object> config = new HashMap<>();
    config.put(TOKEN_IO_REGISTRIES, Collections.singletonList(JanusGraphIoRegistry.class.getName()));
    serializer.configure(config, Collections.emptyMap());
    GraphTraversal traversal = EmptyGraph.instance().traversal().addV().property("loc", Geoshape.point(1.0f, 1.0f));
    Bytecode expectedBytecode = traversal.asAdmin().getBytecode();
    String serializedMessage = serializer.serializeRequestAsString(RequestMessage.build(Tokens.OPS_BYTECODE).processor("traversal").addArg(Tokens.ARGS_GREMLIN, expectedBytecode).create());
    RequestMessage requestMessage1 = serializer.deserializeRequest(serializedMessage);
    Bytecode result = (Bytecode) requestMessage1.getArgs().get(Tokens.ARGS_GREMLIN);
    assertEquals(expectedBytecode, result);
}
Also used : HashMap(java.util.HashMap) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) RequestMessage(org.apache.tinkerpop.gremlin.driver.message.RequestMessage) GraphSONMessageSerializerV3d0(org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV3d0) Bytecode(org.apache.tinkerpop.gremlin.process.traversal.Bytecode) Test(org.junit.jupiter.api.Test)

Example 40 with GraphTraversal

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

the class JanusGraphTest method testLimitBatchSizeForMultiQuery.

@Test
public void testLimitBatchSizeForMultiQuery() {
    int numV = 100;
    JanusGraphVertex a = graph.addVertex();
    JanusGraphVertex[] bs = new JanusGraphVertex[numV];
    JanusGraphVertex[] cs = new JanusGraphVertex[numV];
    for (int i = 0; i < numV; ++i) {
        bs[i] = graph.addVertex();
        cs[i] = graph.addVertex();
        cs[i].property("foo", "bar");
        a.addEdge("knows", bs[i]);
        bs[i].addEdge("knows", cs[i]);
    }
    int barrierSize = 27;
    int limit = 40;
    // test batching for `out()`
    Supplier<GraphTraversal<?, ?>> traversal = () -> graph.traversal().V(bs).barrier(barrierSize).out();
    assertEqualResultWithAndWithoutLimitBatchSize(traversal);
    clopen(option(USE_MULTIQUERY), true, option(LIMIT_BATCH_SIZE), true);
    TraversalMetrics profile = traversal.get().profile().next();
    assertEquals(3, countBackendQueriesOfSize(barrierSize * 2, profile.getMetrics()));
    assertEquals(1, countBackendQueriesOfSize((numV - 3 * barrierSize) * 2, profile.getMetrics()));
    // test early abort with limit for `out()`
    traversal = () -> graph.traversal().V(bs).barrier(barrierSize).out().limit(limit);
    assertEqualResultWithAndWithoutLimitBatchSize(traversal);
    clopen(option(USE_MULTIQUERY), true, option(LIMIT_BATCH_SIZE), true);
    profile = traversal.get().profile().next();
    assertEquals((int) Math.ceil((double) limit / barrierSize), countBackendQueriesOfSize(barrierSize * 2, profile.getMetrics()));
    // test batching for `values()`
    traversal = () -> graph.traversal().V(cs).barrier(barrierSize).values("foo");
    assertEqualResultWithAndWithoutLimitBatchSize(traversal);
    clopen(option(USE_MULTIQUERY), true, option(LIMIT_BATCH_SIZE), true);
    profile = traversal.get().profile().next();
    assertEquals(3, countBackendQueriesOfSize(barrierSize, profile.getMetrics()));
    assertEquals(1, countBackendQueriesOfSize(numV - 3 * barrierSize, profile.getMetrics()));
    // test early abort with limit for `values()`
    traversal = () -> graph.traversal().V(cs).barrier(barrierSize).values("foo").limit(limit);
    assertEqualResultWithAndWithoutLimitBatchSize(traversal);
    clopen(option(USE_MULTIQUERY), true, option(LIMIT_BATCH_SIZE), true);
    profile = traversal.get().profile().next();
    assertEquals((int) Math.ceil((double) limit / barrierSize), countBackendQueriesOfSize(barrierSize, profile.getMetrics()));
    // test batching with unlimited batch size
    traversal = () -> graph.traversal().V(bs).barrier(barrierSize).out();
    assertEqualResultWithAndWithoutLimitBatchSize(traversal);
    clopen(option(USE_MULTIQUERY), true, option(LIMIT_BATCH_SIZE), false);
    profile = traversal.get().profile().next();
    assertEquals(0, countBackendQueriesOfSize(barrierSize, profile.getMetrics()));
    assertEquals(0, countBackendQueriesOfSize(barrierSize * 2, profile.getMetrics()));
    assertEquals(1, countBackendQueriesOfSize(bs.length * 2, profile.getMetrics()));
    // test nested VertexStep with unlimited batch size
    traversal = () -> graph.traversal().V(bs).barrier(barrierSize).where(__.out());
    assertEqualResultWithAndWithoutLimitBatchSize(traversal);
    clopen(option(USE_MULTIQUERY), true, option(LIMIT_BATCH_SIZE), false);
    profile = traversal.get().profile().next();
    assertEquals(0, countBackendQueriesOfSize(barrierSize, profile.getMetrics()));
    assertEquals(0, countBackendQueriesOfSize(barrierSize * 2, profile.getMetrics()));
    assertEquals(1, countBackendQueriesOfSize(bs.length * 2, profile.getMetrics()));
    // test nested VertexStep with non-nested barrier
    traversal = () -> graph.traversal().V(bs).barrier(barrierSize).where(__.out());
    assertEqualResultWithAndWithoutLimitBatchSize(traversal);
    clopen(option(USE_MULTIQUERY), true, option(LIMIT_BATCH_SIZE), true);
    profile = traversal.get().profile().next();
    assertEquals(3, countBackendQueriesOfSize(barrierSize * 2, profile.getMetrics()));
    assertEquals(1, countBackendQueriesOfSize((numV - 3 * barrierSize) * 2, profile.getMetrics()));
    // test batching with repeat step
    traversal = () -> graph.traversal().V(a).repeat(__.barrier(barrierSize).out()).times(2);
    assertEqualResultWithAndWithoutLimitBatchSize(traversal);
    clopen(option(USE_MULTIQUERY), true, option(LIMIT_BATCH_SIZE), true);
    profile = traversal.get().profile().next();
    assertEquals(3, countBackendQueriesOfSize(barrierSize * 2, profile.getMetrics()));
    assertEquals(1, countBackendQueriesOfSize((numV - 3 * barrierSize) * 2, profile.getMetrics()));
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) TraversalMetrics(org.apache.tinkerpop.gremlin.process.traversal.util.TraversalMetrics) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) 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