Search in sources :

Example 1 with GraphWriter

use of org.apache.tinkerpop.gremlin.structure.io.GraphWriter in project titan by thinkaurelius.

the class TitanIoTest method testGeoShapeSerializationReadWriteAsGraphSONEmbedded.

@Test
public void testGeoShapeSerializationReadWriteAsGraphSONEmbedded() throws Exception {
    GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true);
    GraphSONMapper m = graph.io(IoCore.graphson()).mapper().embedTypes(true).create();
    GraphWriter writer = graph.io(IoCore.graphson()).writer().mapper(m).create();
    FileOutputStream fos = new FileOutputStream("/tmp/test.json");
    writer.writeGraph(fos, graph);
    clearGraph(config);
    open(config);
    GraphReader reader = graph.io(IoCore.graphson()).reader().mapper(m).create();
    FileInputStream fis = new FileInputStream("/tmp/test.json");
    reader.readGraph(fis, graph);
    TitanIndexTest.assertGraphOfTheGods(graph);
}
Also used : GraphReader(org.apache.tinkerpop.gremlin.structure.io.GraphReader) GraphSONMapper(org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper) FileOutputStream(java.io.FileOutputStream) GraphWriter(org.apache.tinkerpop.gremlin.structure.io.GraphWriter) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with GraphWriter

use of org.apache.tinkerpop.gremlin.structure.io.GraphWriter in project janusgraph by JanusGraph.

the class JanusGraphIoTest method testSerialization.

private void testSerialization(Function<Geoshape, Geoshape> makeGeoshape) throws Exception {
    if (makeGeoshape != null) {
        addGeoshape(makeGeoshape);
    }
    GraphWriter writer = writerMaker.apply(graph);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    writer.writeGraph(outputStream, graph);
    clearGraph(config);
    open(config);
    GraphReader reader = readerMaker.apply(graph);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    reader.readGraph(inputStream, graph);
    JanusGraphIndexTest.assertGraphOfTheGods(graph);
    if (makeGeoshape != null) {
        assertGeoshape(makeGeoshape);
    }
}
Also used : GraphReader(org.apache.tinkerpop.gremlin.structure.io.GraphReader) ByteArrayInputStream(java.io.ByteArrayInputStream) GraphWriter(org.apache.tinkerpop.gremlin.structure.io.GraphWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with GraphWriter

use of org.apache.tinkerpop.gremlin.structure.io.GraphWriter in project sqlg by pietermartin.

the class TestIo method shouldReadWriteVertexWithBOTHEdges.

@Test
public void shouldReadWriteVertexWithBOTHEdges() throws Exception {
    this.ioType = "graphson-v1-embedded";
    this.assertViaDirectEquality = true;
    this.assertEdgesAtSameTimeAsVertex = false;
    this.readerMaker = g -> g.io(IoCore.graphson()).reader().mapper(g.io(IoCore.graphson()).mapper().create()).create();
    this.writerMaker = g -> g.io(IoCore.graphson()).writer().mapper(g.io(IoCore.graphson()).mapper().create()).create();
    Graph graph = this.sqlgGraph;
    final Vertex v1 = graph.addVertex("name", "marko", T.label, "person");
    final Vertex v2 = graph.addVertex(T.label, "person");
    final Edge e1 = v2.addEdge("friends", v1, "weight", 0.5d);
    final Edge e2 = v1.addEdge("friends", v2, "weight", 1.0d);
    try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        final GraphWriter writer = writerMaker.apply(graph);
        writer.writeVertex(os, v1, Direction.BOTH);
        final AtomicBoolean calledVertex = new AtomicBoolean(false);
        final AtomicBoolean calledEdge1 = new AtomicBoolean(false);
        final AtomicBoolean calledEdge2 = new AtomicBoolean(false);
        final GraphReader reader = readerMaker.apply(graph);
        try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) {
            reader.readVertex(bais, attachable -> {
                final Vertex detachedVertex = attachable.get();
                if (assertViaDirectEquality) {
                    TestHelper.validateVertexEquality(v1, detachedVertex, assertEdgesAtSameTimeAsVertex);
                } else {
                    Assert.assertEquals(v1.id(), graph.vertices(detachedVertex.id().toString()).next().id());
                    Assert.assertEquals(v1.label(), detachedVertex.label());
                    Assert.assertEquals(1, IteratorUtils.count(detachedVertex.properties()));
                    Assert.assertEquals("marko", detachedVertex.value("name"));
                }
                calledVertex.set(true);
                return detachedVertex;
            }, attachable -> {
                final Edge detachedEdge = attachable.get();
                final Predicate<Edge> matcher = assertViaDirectEquality ? e -> detachedEdge.id().equals(e.id()) : e -> graph.edges(detachedEdge.id().toString()).next().id().equals(e.id());
                if (matcher.test(e1)) {
                    if (assertViaDirectEquality) {
                        TestHelper.validateEdgeEquality(e1, detachedEdge);
                    } else {
                        Assert.assertEquals(e1.id(), graph.edges(detachedEdge.id().toString()).next().id());
                        Assert.assertEquals(v1.id(), graph.vertices(detachedEdge.inVertex().id().toString()).next().id());
                        Assert.assertEquals(v2.id(), graph.vertices(detachedEdge.outVertex().id().toString()).next().id());
                        Assert.assertEquals(v2.label(), detachedEdge.inVertex().label());
                        Assert.assertEquals(e1.label(), detachedEdge.label());
                        Assert.assertEquals(1, IteratorUtils.count(detachedEdge.properties()));
                        Assert.assertEquals(0.5d, detachedEdge.value("weight"), 0.000001d);
                    }
                    calledEdge1.set(true);
                } else if (matcher.test(e2)) {
                    if (assertViaDirectEquality) {
                        TestHelper.validateEdgeEquality(e2, detachedEdge);
                    } else {
                        Assert.assertEquals(e2.id(), graph.edges(detachedEdge.id().toString()).next().id());
                        Assert.assertEquals(v2.id(), graph.vertices(detachedEdge.inVertex().id().toString()).next().id());
                        Assert.assertEquals(v1.id(), graph.vertices(detachedEdge.outVertex().id().toString()).next().id());
                        Assert.assertEquals(v1.label(), detachedEdge.outVertex().label());
                        Assert.assertEquals(e2.label(), detachedEdge.label());
                        Assert.assertEquals(1, IteratorUtils.count(detachedEdge.properties()));
                        Assert.assertEquals(1.0d, detachedEdge.value("weight"), 0.000001d);
                    }
                    calledEdge2.set(true);
                } else {
                    Assert.fail("An edge id generated that does not exist");
                }
                return null;
            }, Direction.BOTH);
        }
        Assert.assertTrue(calledVertex.get());
        Assert.assertTrue(calledEdge1.get());
        Assert.assertTrue(calledEdge2.get());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GraphReader(org.apache.tinkerpop.gremlin.structure.io.GraphReader) ByteArrayInputStream(java.io.ByteArrayInputStream) GraphWriter(org.apache.tinkerpop.gremlin.structure.io.GraphWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 4 with GraphWriter

use of org.apache.tinkerpop.gremlin.structure.io.GraphWriter in project sqlg by pietermartin.

the class TestIoEdge method assertEdge.

// @Test
// public void shouldReadWriteDetachedEdge() throws Exception {
// final Vertex v1 = this.sqlgGraph.addVertex(T.label, "person");
// final Vertex v2 = this.sqlgGraph.addVertex(T.label, "person");
// final Edge e = DetachedFactory.detach(v1.addEdge("friend", v2, "weight", 0.5d, "acl", "rw"), true);
// 
// assertEdge(v1, v2, e, true);
// }
// 
// @Test
// public void shouldReadWriteDetachedEdgeAsReference() throws Exception {
// final Vertex v1 = this.sqlgGraph.addVertex(T.label, "person");
// final Vertex v2 = this.sqlgGraph.addVertex(T.label, "person");
// final Edge e = DetachedFactory.detach(v1.addEdge("friend", v2, "weight", 0.5d, "acl", "rw"), false);
// 
// assertEdge(v1, v2, e, false);
// }
private void assertEdge(final Vertex v1, final Vertex v2, final Edge e, final boolean assertProperties) throws IOException {
    try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        final GraphWriter writer = writerMaker.apply(this.sqlgGraph);
        writer.writeEdge(os, e);
        final AtomicBoolean called = new AtomicBoolean(false);
        final GraphReader reader = readerMaker.apply(this.sqlgGraph);
        try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) {
            reader.readEdge(bais, edge -> {
                final Edge detachedEdge = (Edge) edge;
                Assert.assertEquals(e.id(), assertIdDirectly ? detachedEdge.id() : this.sqlgGraph.edges(detachedEdge.id().toString()).next().id());
                Assert.assertEquals(v1.id(), assertIdDirectly ? detachedEdge.outVertex().id() : this.sqlgGraph.vertices(detachedEdge.outVertex().id().toString()).next().id());
                Assert.assertEquals(v2.id(), assertIdDirectly ? detachedEdge.inVertex().id() : this.sqlgGraph.vertices(detachedEdge.inVertex().id().toString()).next().id());
                Assert.assertEquals(v1.label(), detachedEdge.outVertex().label());
                Assert.assertEquals(v2.label(), detachedEdge.inVertex().label());
                Assert.assertEquals(e.label(), detachedEdge.label());
                if (assertProperties) {
                    Assert.assertEquals(assertDouble ? 0.5d : 0.5f, e.properties("weight").next().value());
                    Assert.assertEquals("rw", e.properties("acl").next().value());
                } else {
                    Assert.assertEquals(e.keys().size(), IteratorUtils.count(detachedEdge.properties()));
                }
                called.set(true);
                return null;
            });
        }
        Assert.assertTrue(called.get());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GraphReader(org.apache.tinkerpop.gremlin.structure.io.GraphReader) ByteArrayInputStream(java.io.ByteArrayInputStream) GraphWriter(org.apache.tinkerpop.gremlin.structure.io.GraphWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Aggregations

GraphReader (org.apache.tinkerpop.gremlin.structure.io.GraphReader)4 GraphWriter (org.apache.tinkerpop.gremlin.structure.io.GraphWriter)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Test (org.junit.Test)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 Edge (org.apache.tinkerpop.gremlin.structure.Edge)1 GraphSONMapper (org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper)1 BaseTest (org.umlg.sqlg.test.BaseTest)1