/** * {@inheritDoc} */ @Override public void writeGraph(final String file) throws IOException { try (final OutputStream out = new FileOutputStream(file)) { writer().create().writeGraph(out, graph); } }
@Parameterized.Parameters(name = "{0}") public static Iterable<Object[]> data() { final GraphSONMapper v1mapper = GraphSONMapper.build().version(GraphSONVersion.V1_0).typeInfo(TypeInfo.PARTIAL_TYPES).addRegistry(JanusGraphIoRegistryV1d0.getInstance()).create(); final GraphSONMapper v2mapper = GraphSONMapper.build().version(GraphSONVersion.V2_0).typeInfo(TypeInfo.PARTIAL_TYPES).addRegistry(JanusGraphIoRegistry.getInstance()).create(); final GraphSONMapper v3mapper = GraphSONMapper.build().version(GraphSONVersion.V3_0).typeInfo(TypeInfo.PARTIAL_TYPES).addRegistry(JanusGraphIoRegistry.getInstance()).create(); return Arrays.asList(new Object[][]{ {"graphson-v1-embedded", (Function<Graph, GraphReader>) g -> GraphSONReader.build().mapper(v1mapper).create(), (Function<Graph, GraphWriter>) g -> GraphSONWriter.build().mapper(v1mapper).create()}, {"graphson-v2-embedded", (Function<Graph, GraphReader>) g -> GraphSONReader.build().mapper(v2mapper).create(), (Function<Graph, GraphWriter>) g -> GraphSONWriter.build().mapper(v2mapper).create()}, {"graphson-v3", (Function<Graph, GraphReader>) g -> GraphSONReader.build().mapper(v3mapper).create(), (Function<Graph, GraphWriter>) g -> GraphSONWriter.build().mapper(v3mapper).create()}, {"gryo", (Function<Graph, GraphReader>) g -> g.io(IoCore.gryo()).reader().mapper(g.io(IoCore.gryo()).mapper().create()).create(), (Function<Graph, GraphWriter>) g -> g.io(IoCore.gryo()).writer().mapper(g.io(IoCore.gryo()).mapper().create()).create()} }); }
private Pair<StarGraph, Integer> serializeDeserialize(final StarGraph starGraph) { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { graph.io(IoCore.gryo()).writer().create().writeObject(outputStream, starGraph); return Pair.with(graph.io(IoCore.gryo()).reader().create().readObject(new ByteArrayInputStream(outputStream.toByteArray()), StarGraph.class), outputStream.size()); } catch (IOException ioe) { throw new RuntimeException(ioe); } } }
private Pair<StarGraph, Integer> serializeDeserialize(final StarGraph starGraph) { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { graph.io(IoCore.gryo()).writer().create().writeObject(outputStream, starGraph); return Pair.with(graph.io(IoCore.gryo()).reader().create().readObject(new ByteArrayInputStream(outputStream.toByteArray()), StarGraph.class), outputStream.size()); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTraversalMetrics() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final TraversalMetrics before = (TraversalMetrics) g.V().both().profile().next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, before); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final TraversalMetrics after = gryoReader.readObject(inputStream, TraversalMetrics.class); assertNotNull(after); assertEquals(before.getMetrics().size(), after.getMetrics().size()); assertEquals(before.getDuration(TimeUnit.MILLISECONDS), after.getDuration(TimeUnit.MILLISECONDS)); assertEquals(before.getMetrics(0).getCounts(), after.getMetrics().stream().findFirst().get().getCounts()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTraversalMetrics() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final TraversalMetrics before = g.V().both().profile().next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, before); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final TraversalMetrics after = gryoReader.readObject(inputStream, TraversalMetrics.class); assertNotNull(after); assertEquals(before.getMetrics().size(), after.getMetrics().size()); assertEquals(before.getDuration(TimeUnit.MILLISECONDS), after.getDuration(TimeUnit.MILLISECONDS)); assertEquals(before.getMetrics(0).getCounts(), after.getMetrics().stream().findFirst().get().getCounts()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTree() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final Tree before = g.V(convertToVertexId("marko")).out().properties("name").tree().next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, before); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final Tree after = gryoReader.readObject(inputStream, Tree.class); assertNotNull(after); //The following assertions should be sufficent. assertThat("Type does not match", after, instanceOf(Tree.class)); assertEquals("The objects differ", after, before); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithPropertiesAsDetached() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final VertexProperty<?> vertexProperty = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, vertexProperty); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final VertexProperty<?> detached = gryoReader.readObject(inputStream, DetachedVertexProperty.class); assertNotNull(detached); assertEquals(vertexProperty.label(), detached.label()); assertEquals(vertexProperty.id(), detached.id()); assertEquals(vertexProperty.value(), detached.value()); assertEquals(vertexProperty.values("startTime").next(), detached.values("startTime").next()); assertEquals(vertexProperty.properties("startTime").next().key(), detached.properties("startTime").next().key()); assertEquals(vertexProperty.values("endTime").next(), detached.values("endTime").next()); assertEquals(vertexProperty.properties("endTime").next().key(), detached.properties("endTime").next().key()); }
/** * Use Gryo to pipe the data from one graph to another graph. Uses readers and writers generated from each * {@link Graph} via the {@link Graph#io(Io.Builder)} method. */ public static void migrateGraph(final Graph fromGraph, final Graph toGraph) throws IOException { migrateGraph(fromGraph, toGraph, fromGraph.io(gryo()).reader().create(), toGraph.io(gryo()).writer().create()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithPropertiesAsDetached() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final VertexProperty<?> vertexProperty = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, vertexProperty); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final VertexProperty<?> detached = gryoReader.readObject(inputStream, DetachedVertexProperty.class); assertNotNull(detached); assertEquals(vertexProperty.label(), detached.label()); assertEquals(vertexProperty.id(), detached.id()); assertEquals(vertexProperty.value(), detached.value()); assertEquals(vertexProperty.values("startTime").next(), detached.values("startTime").next()); assertEquals(vertexProperty.properties("startTime").next().key(), detached.properties("startTime").next().key()); assertEquals(vertexProperty.values("endTime").next(), detached.values("endTime").next()); assertEquals(vertexProperty.properties("endTime").next().key(), detached.properties("endTime").next().key()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTree() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final Tree before = g.V(convertToVertexId("marko")).out().properties("name").tree().next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, before); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final Tree after = gryoReader.readObject(inputStream, Tree.class); assertNotNull(after); //The following assertions should be sufficent. assertThat("Type does not match", after, instanceOf(Tree.class)); assertEquals("The objects differ", after, before); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeVertexAsDetached() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final Vertex v = graph.vertices(convertToVertexId("marko")).next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, v); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final Vertex detached = gryoReader.readObject(inputStream, DetachedVertex.class); assertNotNull(detached); assertEquals(v.label(), detached.label()); assertEquals(v.id(), detached.id()); assertEquals(v.value("name").toString(), detached.value("name")); assertEquals((Integer) v.value("age"), detached.value("age")); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializePropertyAsDetached() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final Property property = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight"); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, property); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final Property detached = gryoReader.readObject(inputStream, DetachedProperty.class); assertNotNull(detached); assertEquals(property.key(), detached.key()); assertEquals(property.value(), detached.value()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeVertexAsDetached() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final Vertex v = graph.vertices(convertToVertexId("marko")).next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, v); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final Vertex detached = gryoReader.readObject(inputStream, DetachedVertex.class); assertNotNull(detached); assertEquals(v.label(), detached.label()); assertEquals(v.id(), detached.id()); assertEquals(v.value("name").toString(), detached.value("name")); assertEquals((Integer) v.value("age"), detached.value("age")); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeVertexPropertyAsDetached() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final VertexProperty vertexProperty = graph.vertices(convertToVertexId("marko")).next().property("name"); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, vertexProperty); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final VertexProperty detached = gryoReader.readObject(inputStream, DetachedVertexProperty.class); assertNotNull(detached); assertEquals(vertexProperty.label(), detached.label()); assertEquals(vertexProperty.id(), detached.id()); assertEquals(vertexProperty.value(), detached.value()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializePropertyAsDetached() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final Property property = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight"); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, property); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final Property detached = gryoReader.readObject(inputStream, DetachedProperty.class); assertNotNull(detached); assertEquals(property.key(), detached.key()); assertEquals(property.value(), detached.value()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeVertexPropertyAsDetached() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final VertexProperty vertexProperty = graph.vertices(convertToVertexId("marko")).next().property("name"); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, vertexProperty); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final VertexProperty detached = gryoReader.readObject(inputStream, DetachedVertexProperty.class); assertNotNull(detached); assertEquals(vertexProperty.label(), detached.label()); assertEquals(vertexProperty.id(), detached.id()); assertEquals(vertexProperty.value(), detached.value()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeEdgeAsDetached() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final Edge e = g.E(convertToEdgeId("marko", "knows", "vadas")).next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, e); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final Edge detached = gryoReader.readObject(inputStream, DetachedEdge.class); assertNotNull(detached); assertEquals(e.label(), detached.label()); assertEquals(e.id(), detached.id()); assertEquals((Double) e.value("weight"), detached.value("weight")); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeEdgeAsDetached() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final Edge e = g.E(convertToEdgeId("marko", "knows", "vadas")).next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, e); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final Edge detached = gryoReader.readObject(inputStream, DetachedEdge.class); assertNotNull(detached); assertEquals(e.label(), detached.label()); assertEquals(e.id(), detached.id()); assertEquals((Double) e.value("weight"), detached.value("weight")); }
@Parameterized.Parameters(name = "{0}") public static Iterable<Object[]> data() { final SimpleModule moduleV1d0 = new SimpleModule(); moduleV1d0.addSerializer(CustomId.class, new CustomId.CustomIdJacksonSerializerV1d0()); final SimpleModule moduleV2d0 = new CustomId.CustomIdTinkerPopJacksonModuleV2d0(); final SimpleModule modulev3d0 = new CustomId.CustomIdTinkerPopJacksonModuleV3d0(); return Arrays.asList(new Object[][]{ {"graphson-v1-embedded", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().addCustomModule(moduleV1d0).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().addCustomModule(moduleV1d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v2-embedded", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().addCustomModule(moduleV2d0).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().addCustomModule(moduleV2d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v3", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().addCustomModule(modulev3d0).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().addCustomModule(modulev3d0).create()).create()}, {"gryo-v1", true, (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).reader().mapper(g.io(GryoIo.build(GryoVersion.V1_0)).mapper().version(GryoVersion.V1_0).addCustom(CustomId.class).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).writer().mapper(g.io(GryoIo.build(GryoVersion.V1_0)).mapper().version(GryoVersion.V1_0).addCustom(CustomId.class).create()).create()}, {"gryo-v3", true, (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).reader().mapper(g.io(GryoIo.build(GryoVersion.V3_0)).mapper().version(GryoVersion.V3_0).addCustom(CustomId.class).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).writer().mapper(g.io(GryoIo.build(GryoVersion.V3_0)).mapper().version(GryoVersion.V3_0).addCustom(CustomId.class).create()).create()} }); }