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); } }
/** * Write a vertex to a stream without writing its edges. * * @param outputStream the stream to write to. * @param vertexIterator a iterator that returns a list of vertices. */ public default void writeVertices(final OutputStream outputStream, final Iterator<Vertex> vertexIterator) throws IOException { while (vertexIterator.hasNext()) { writeVertex(outputStream, vertexIterator.next()); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldReadWritePropertyGraphSON() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); final Property p = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight"); writer.writeProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldReadWriteVertexPropertyNoMetaProperties() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); final VertexProperty p = g.V(convertToVertexId("marko")).next().property("name"); writer.writeVertexProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readVertexProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); assertEquals(0, IteratorUtils.count(propertyAttachable.get().properties())); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } }
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(graph); writer.writeEdge(os, e);
public static String writeValueAsString(Object valueToWrite) throws IOException { try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { GRAPHSON_WRITER_2_0.writeObject(out, valueToWrite); return out.toString("UTF-8"); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldReadWriteVertexPropertyWithMetaProperties() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); // select any vertexproperty that has both start/end time final VertexProperty p = (VertexProperty) g.V(convertToVertexId("marko")).properties("location").as("p").has("endTime").select("p").next(); writer.writeVertexProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readVertexProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); assertEquals(IteratorUtils.count(p.properties()), IteratorUtils.count(propertyAttachable.get().properties())); assertEquals(p.property("startTime").value(), ((Property) propertyAttachable.get().properties("startTime").next()).value()); assertEquals(p.property("endTime").value(), ((Property) propertyAttachable.get().properties("endTime").next()).value()); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } }
writer.writeEdge(os, e);
@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); }
/** * Write a list of vertices from a {@link Traversal} to a stream with its associated edges. Only write edges as * defined by the requested direction. * * @param outputStream the stream to write to. * @param vertexIterator a traversal that returns a list of vertices. * @param direction the direction of edges to write or null if no edges are to be written. */ public default void writeVertices(final OutputStream outputStream, final Iterator<Vertex> vertexIterator, final Direction direction) throws IOException { while (vertexIterator.hasNext()) { writeVertex(outputStream, vertexIterator.next(), direction); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldReadWriteVertexPropertyNoMetaProperties() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); final VertexProperty p = g.V(convertToVertexId("marko")).next().property("name"); writer.writeVertexProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readVertexProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); assertEquals(0, IteratorUtils.count(propertyAttachable.get().properties())); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } }
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(graph); writer.writeEdge(os, e);
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldReadWritePropertyGraphSON() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); final Property p = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight"); writer.writeProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } } }
protected Traverser.Admin<S> write(final File file) { try (final OutputStream stream = new FileOutputStream(file)) { final Graph graph = (Graph) this.traversal.getGraph().get(); constructWriter().writeGraph(stream, graph); return EmptyTraverser.instance(); } catch (IOException ioe) { throw new IllegalStateException(String.format("Could not write file %s from graph", this.file), ioe); } }
private void assertVertexToSerialize(final Vertex toSerialize, final boolean assertProperties) throws IOException { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); writer.writeVertex(os, toSerialize);
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldReadWriteVertexPropertyWithMetaProperties() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); // select any vertexproperty that has both start/end time final VertexProperty p = (VertexProperty) g.V(convertToVertexId("marko")).properties("location").as("p").has("endTime").select("p").next(); writer.writeVertexProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readVertexProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); assertEquals(IteratorUtils.count(p.properties()), IteratorUtils.count(propertyAttachable.get().properties())); assertEquals(p.property("startTime").value(), ((Property) propertyAttachable.get().properties("startTime").next()).value()); assertEquals(p.property("endTime").value(), ((Property) propertyAttachable.get().properties("endTime").next()).value()); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } }
writer.writeEdge(os, e);
writer.writeGraph(outPipe, fromGraph); outPipe.flush(); outPipe.close();
writer.writeVertex(os, v1);
@Test @LoadGraphWith(LoadGraphWith.GraphData.CLASSIC) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReadWriteClassic() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = graph.io(ioBuilderToTest).writer().create(); writer.writeGraph(os, graph); final Configuration configuration = graphProvider.newGraphConfiguration("readGraph", this.getClass(), name.getMethodName(), LoadGraphWith.GraphData.CLASSIC); graphProvider.clear(configuration); final Graph g1 = graphProvider.openTestGraph(configuration); final GraphReader reader = graph.io(ioBuilderToTest).reader().create(); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readGraph(bais, g1); } IoTest.assertClassicGraph(g1, assertDouble, lossyForId); graphProvider.clear(g1, configuration); } }