org.apache.tinkerpop.gremlin.structure.io.GraphReader Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.structure.io.GraphReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsTraversalMetrics() {
    final TinkerGraph tg = TinkerFactory.createModern();

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final TraversalMetrics tm = tg.traversal().V(1).as("a").has("name").as("b").
            out("knows").out("created").as("c").
            has("name", "ripple").values("name").as("d").
            identity().as("e").profile().next();

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, tm);
        final String json = out.toString();

        final TraversalMetrics traversalMetricsRead = (TraversalMetrics)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        // toString should be enough to compare TraversalMetrics
        assertTrue(tm.toString().equals(traversalMetricsRead.toString()));
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #2
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Edge ids.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForEdgeIds() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);
    final  Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin");
    v1.addEdge("hello", sampleGraph1.traversal().V().has("name", "marko").next(), T.id, 101L);
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized edge Id.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #3
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Vertex props.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForVertexProps() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);

    sampleGraph1.addVertex(T.id, 100, "name", "kevin", "uuid", UUID.randomUUID());
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized vertex prop.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #4
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Vertex ids.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForVertexIds() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);
    sampleGraph1.addVertex(T.id, 100L, "name", "kevin");
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized vertex Id.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #5
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Thorough types verification for Vertex ids, Vertex props, Edge ids, Edge props
 */
@Test
public void shouldDeserializeGraphSONIntoTinkerGraphKeepingTypes() throws IOException {
    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final Graph sampleGraph1 = TinkerFactory.createModern();
    final Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin", "theUUID", UUID.randomUUID());
    final Vertex v2 = sampleGraph1.addVertex(T.id, 101L, "name", "henri", "theUUID", UUID.randomUUID());
    v1.addEdge("hello", v2, T.id, 101L,
            "uuid", UUID.randomUUID());

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, sampleGraph1);
        final String json = out.toString();

        final TinkerGraph read = reader.readObject(new ByteArrayInputStream(json.getBytes()), TinkerGraph.class);
        assertTrue(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #6
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Edge props.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForEdgeProps() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final Graph sampleGraph1 = TinkerFactory.createModern();

    final Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin");
    v1.addEdge("hello", sampleGraph1.traversal().V().has("name", "marko").next(), T.id, 101,
            "uuid", UUID.randomUUID());
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized edge prop.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #7
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@org.junit.Ignore("https://issues.apache.org/jira/browse/TINKERPOP-1509")
public void deserializersTestsTree() {
    final TinkerGraph tg = TinkerFactory.createModern();

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final Tree t = tg.traversal().V().out().out().tree().next();

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, t);
        final String json = out.toString();

        final Tree treeRead = (Tree)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        //Map's equals should check each component of the tree recursively
        //on each it will call "equals()" which for Vertices will compare ids, which
        //is ok. Complete vertex deser is checked elsewhere.
        assertEquals(t, treeRead);

    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #8
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsVertex() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    v.property("born", LocalDateTime.of(1971, 1, 2, 20, 50));
    v.property("dead", LocalDateTime.of(1971, 1, 7, 20, 50));

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, v);
        final String json = out.toString();

        // Object works, because there's a type in the payload now
        // Vertex.class would work as well
        // Anything else would not because we check the type in param here with what's in the JSON, for safety.
        final Vertex vRead = (Vertex)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        assertEquals(v, vRead);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #9
Source File: TinkerpopTest.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testTail() throws IOException {
    Graph graph = this.sqlgGraph;
    final GraphReader reader = GryoReader.build()
            .mapper(graph.io(GryoIo.build()).mapper().create())
            .create();
    try (final InputStream stream = AbstractGremlinTest.class.getResourceAsStream("/tinkerpop-modern.kryo")) {
        reader.readGraph(stream, graph);
    }
    assertModernGraph(graph, true, false);
    GraphTraversalSource g = graph.traversal();
    int tail = 0;
    for (int i = 1; i < 72; i++) {
        tail = i;
        List<Vertex> vertices = g.V().repeat(both()).times(3).tail(tail).toList();
        if (tail != vertices.size()) {
            System.out.println("expected " + tail + " found " + vertices.size());
        }
    }
}
 
Example #10
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsEdge() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    final Vertex v2 = tg.addVertex("vertexTest");

    final Edge ed = v.addEdge("knows", v2, "time", LocalDateTime.now());

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, ed);
        final String json = out.toString();

        // Object works, because there's a type in the payload now
        // Edge.class would work as well
        // Anything else would not because we check the type in param here with what's in the JSON, for safety.
        final Edge eRead = (Edge)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        assertEquals(ed, eRead);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #11
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsTinkerGraph() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    final Vertex v2 = tg.addVertex("vertexTest");

    v.addEdge("knows", v2);

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, tg);
        final String json = out.toString();

        final Graph gRead = (Graph)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        assertTrue(approximateGraphsCheck(tg, gRead));
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #12
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
@org.junit.Ignore("https://issues.apache.org/jira/browse/TINKERPOP-1509")
public void deserializersTestsTree() {
    final TinkerGraph tg = TinkerFactory.createModern();

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final Tree t = tg.traversal().V().out().out().tree().next();

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, t);
        final String json = out.toString();

        final Tree treeRead = (Tree)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        //Map's equals should check each component of the tree recursively
        //on each it will call "equals()" which for Vertices will compare ids, which
        //is ok. Complete vertex deser is checked elsewhere.
        assertEquals(t, treeRead);

    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #13
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsTraversalMetrics() {
    final TinkerGraph tg = TinkerFactory.createModern();

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final TraversalMetrics tm = tg.traversal().V(1).as("a").has("name").as("b").
            out("knows").out("created").as("c").
            has("name", "ripple").values("name").as("d").
            identity().as("e").profile().next();

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, tm);
        final String json = out.toString();

        final TraversalMetrics traversalMetricsRead = (TraversalMetrics)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        // toString should be enough to compare TraversalMetrics
        assertTrue(tm.toString().equals(traversalMetricsRead.toString()));
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #14
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsVertexProperty() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final VertexProperty prop = v.property("born", LocalDateTime.of(1971, 1, 2, 20, 50));

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, prop);
        final String json = out.toString();

        final VertexProperty vPropRead = (VertexProperty)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        //only classes and ids are checked, that's ok, full vertex property ser/de
        //is checked elsewhere.
        assertEquals(prop, vPropRead);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #15
Source File: TestIoAgain.java    From sqlg with MIT License 6 votes vote down vote up
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> data() {
    return Arrays.asList(new Object[][]{
            {"graphson-v1", false, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().create()},
            {"graphson-v1-embedded", true, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()},
            {"graphson-v2", false, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.NO_TYPES).create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.NO_TYPES).create()).create()},
            {"graphson-v2-embedded", true, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()},
            {"graphson-v3", true, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().create()).create()},
            {"gryo-v1", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).reader().create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).writer().create()},
            {"gryo-v3", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).reader().create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).writer().create()}
    });
}
 
Example #16
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsTinkerGraph() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    final Vertex v2 = tg.addVertex("vertexTest");

    v.addEdge("knows", v2);

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, tg);
        final String json = out.toString();

        final Graph gRead = (Graph)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        assertTrue(approximateGraphsCheck(tg, gRead));
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #17
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsEdge() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    final Vertex v2 = tg.addVertex("vertexTest");

    final Edge ed = v.addEdge("knows", v2, "time", LocalDateTime.now());

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, ed);
        final String json = out.toString();

        // Object works, because there's a type in the payload now
        // Edge.class would work as well
        // Anything else would not because we check the type in param here with what's in the JSON, for safety.
        final Edge eRead = (Edge)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        assertEquals(ed, eRead);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #18
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsVertex() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    v.property("born", LocalDateTime.of(1971, 1, 2, 20, 50));
    v.property("dead", LocalDateTime.of(1971, 1, 7, 20, 50));

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, v);
        final String json = out.toString();

        // Object works, because there's a type in the payload now
        // Vertex.class would work as well
        // Anything else would not because we check the type in param here with what's in the JSON, for safety.
        final Vertex vRead = (Vertex)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        assertEquals(v, vRead);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #19
Source File: TestIoEdge.java    From sqlg with MIT License 6 votes vote down vote up
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> data() {
    return Arrays.asList(new Object[][]{
            {"graphson-v1", false, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().create()},
            {"graphson-v1-embedded", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()},
            {"graphson-v2", false, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.NO_TYPES).create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.NO_TYPES).create()).create()},
            {"graphson-v2-embedded", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()},
            {"graphson-v3", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().create()).create()},
            {"gryo-v1", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).reader().create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).writer().create()},
            {"gryo-v3", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).reader().create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).writer().create()}
    });
}
 
Example #20
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Edge props.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForEdgeProps() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final Graph sampleGraph1 = TinkerFactory.createModern();

    final Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin");
    v1.addEdge("hello", sampleGraph1.traversal().V().has("name", "marko").next(), T.id, 101,
            "uuid", UUID.randomUUID());
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized edge prop.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #21
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Edge ids.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForEdgeIds() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);
    final  Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin");
    v1.addEdge("hello", sampleGraph1.traversal().V().has("name", "marko").next(), T.id, 101L);
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized edge Id.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #22
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Vertex props.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForVertexProps() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);

    sampleGraph1.addVertex(T.id, 100, "name", "kevin", "uuid", UUID.randomUUID());
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized vertex prop.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #23
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Vertex ids.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForVertexIds() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);
    sampleGraph1.addVertex(T.id, 100L, "name", "kevin");
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized vertex Id.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #24
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Thorough types verification for Vertex ids, Vertex props, Edge ids, Edge props
 */
@Test
public void shouldDeserializeGraphSONIntoTinkerGraphKeepingTypes() throws IOException {
    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final Graph sampleGraph1 = TinkerFactory.createModern();
    final Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin", "theUUID", UUID.randomUUID());
    final Vertex v2 = sampleGraph1.addVertex(T.id, 101L, "name", "henri", "theUUID", UUID.randomUUID());
    v1.addEdge("hello", v2, T.id, 101L,
            "uuid", UUID.randomUUID());

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, sampleGraph1);
        final String json = out.toString();

        final TinkerGraph read = reader.readObject(new ByteArrayInputStream(json.getBytes()), TinkerGraph.class);
        assertTrue(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #25
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsVertexProperty() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final VertexProperty prop = v.property("born", LocalDateTime.of(1971, 1, 2, 20, 50));

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, prop);
        final String json = out.toString();

        final VertexProperty vPropRead = (VertexProperty)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        //only classes and ids are checked, that's ok, full vertex property ser/de
        //is checked elsewhere.
        assertEquals(prop, vPropRead);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #26
Source File: BaseTest.java    From sqlg with MIT License 5 votes vote down vote up
protected void loadModern(SqlgGraph sqlgGraph) {
    Io.Builder<GraphSONIo> builder = GraphSONIo.build(GraphSONVersion.V3_0);
    final GraphReader reader = sqlgGraph.io(builder).reader().create();
    try (final InputStream stream = AbstractGremlinTest.class.getResourceAsStream("/tinkerpop-modern-v3d0.json")) {
        reader.readGraph(stream, sqlgGraph);
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}
 
Example #27
Source File: TinkerGraphTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeTinkerGraphToGraphSONWithTypes() throws Exception {
    final TinkerGraph graph = TinkerFactory.createModern();
    final Mapper<ObjectMapper> mapper = graph.io(IoCore.graphson()).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create();
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        final GraphWriter writer = GraphSONWriter.build().mapper(mapper).create();
        writer.writeObject(out, graph);
        try (final ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray())) {
            final GraphReader reader = GraphSONReader.build().mapper(mapper).create();
            final TinkerGraph target = reader.readObject(inputStream, TinkerGraph.class);
            IoTest.assertModernGraph(target, true, false);
        }
    }
}
 
Example #28
Source File: ToyGraphInputRDD.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public JavaPairRDD<Object, VertexWritable> readGraphRDD(final Configuration configuration, final JavaSparkContext sparkContext) {
    KryoShimServiceLoader.applyConfiguration(TinkerGraph.open().configuration());
    final List<VertexWritable> vertices;
    if (configuration.getString(Constants.GREMLIN_HADOOP_INPUT_LOCATION).contains("modern"))
        vertices = IteratorUtils.list(IteratorUtils.map(TinkerFactory.createModern().vertices(), VertexWritable::new));
    else if (configuration.getString(Constants.GREMLIN_HADOOP_INPUT_LOCATION).contains("classic"))
        vertices = IteratorUtils.list(IteratorUtils.map(TinkerFactory.createClassic().vertices(), VertexWritable::new));
    else if (configuration.getString(Constants.GREMLIN_HADOOP_INPUT_LOCATION).contains("crew"))
        vertices = IteratorUtils.list(IteratorUtils.map(TinkerFactory.createTheCrew().vertices(), VertexWritable::new));
    else if (configuration.getString(Constants.GREMLIN_HADOOP_INPUT_LOCATION).contains("sink"))
        vertices = IteratorUtils.list(IteratorUtils.map(TinkerFactory.createKitchenSink().vertices(), VertexWritable::new));
    else if (configuration.getString(Constants.GREMLIN_HADOOP_INPUT_LOCATION).contains("grateful")) {
        try {
            final Graph graph = TinkerGraph.open();
            final GraphReader reader = GryoReader.build().mapper(graph.io(GryoIo.build()).mapper().create()).create();
            try (final InputStream stream = GryoResourceAccess.class.getResourceAsStream("grateful-dead-v3d0.kryo")) {
                reader.readGraph(stream, graph);
            }
            vertices = IteratorUtils.list(IteratorUtils.map(graph.vertices(), VertexWritable::new));
        } catch (final IOException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    } else
        throw new IllegalArgumentException("No legal toy graph was provided to load: " + configuration.getProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION));

    return sparkContext.parallelize(vertices).mapToPair(vertex -> new Tuple2<>(vertex.get().id(), vertex));
}
 
Example #29
Source File: AbstractGraphBenchmark.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Opens a new {@link TinkerGraph} instance and optionally preloads it with one of the test data sets enumerated
 * in {@link LoadGraphWith}.
 *
 * @throws IOException on failure to load graph
 */
@Setup
public void prepare() throws IOException {
    graph = TinkerGraph.open();
    g = graph.traversal();

    final LoadGraphWith[] loadGraphWiths = this.getClass().getAnnotationsByType(LoadGraphWith.class);
    final LoadGraphWith loadGraphWith = loadGraphWiths.length == 0 ? null : loadGraphWiths[0];
    final LoadGraphWith.GraphData loadGraphWithData = null == loadGraphWith ? null : loadGraphWith.value();

    String graphFile;
    if(loadGraphWithData != null) {
        if (loadGraphWithData.equals(LoadGraphWith.GraphData.GRATEFUL)) {
            graphFile = "grateful-dead-v3d0.kryo";
        } else if (loadGraphWithData.equals(LoadGraphWith.GraphData.MODERN)) {
            graphFile = "tinkerpop-modern-v3d0.kryo";
        } else if (loadGraphWithData.equals(LoadGraphWith.GraphData.CLASSIC)) {
            graphFile = "tinkerpop-classic-v3d0.kryo";
        } else if (loadGraphWithData.equals(LoadGraphWith.GraphData.CREW)) {
            graphFile = "tinkerpop-crew-v3d0.kryo";
        } else {
            throw new RuntimeException("Could not load graph with " + loadGraphWithData);
        }

        final GraphReader reader = GryoReader.build().create();
        try (final InputStream stream = AbstractGraphBenchmark.class.
                getResourceAsStream(PATH + graphFile)) {
            reader.readGraph(stream, graph);
        }
    }
}
 
Example #30
Source File: TinkerpopTest.java    From sqlg with MIT License 5 votes vote down vote up
public void g_V_chooseXlabel_eq_person__unionX__out_lang__out_nameX__in_labelX() throws IOException {
    Graph graph = this.sqlgGraph;
    final GraphReader reader = GryoReader.build()
            .mapper(graph.io(GryoIo.build()).mapper().create())
            .create();
    try (final InputStream stream = AbstractGremlinTest.class.getResourceAsStream("/tinkerpop-modern.kryo")) {
        reader.readGraph(stream, graph);
    }
    assertModernGraph(graph, true, false);
    GraphTraversalSource g = graph.traversal();
    List<Vertex> traversala2 =  g.V().hasId(convertToVertexId("marko")).toList();
    Assert.assertEquals(1, traversala2.size());
    Assert.assertEquals(convertToVertex(graph, "marko"), traversala2.get(0));

}