@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()} }); }
/** * {@inheritDoc} */ @Override public void writeGraph(final String file) throws IOException { try (final OutputStream out = new FileOutputStream(file)) { writer().create().writeGraph(out, graph); } }
/** * Writes a single {@link Vertex} with no edges serialized. * * @param outputStream the stream to write to. * @param v the vertex to write. */ @Override public void writeVertex(final OutputStream outputStream, final Vertex v) throws IOException { writeVertex(outputStream, v, null); }
public InputStream getGraphSON(String tenantId) { PipedInputStream in = new PipedInputStream(); new Thread(() -> { try (PipedOutputStream out = new PipedOutputStream(in)) { GraphSONWriter.build().create().writeGraph(out, context.getGraph()); } catch (IOException e) { throw new IllegalStateException("Unable to create the GraphSON dump.", e); } }).start(); return in; }
/** * Writes a list of vertices without edges. * * @param outputStream the stream to write to. * @param vertexIterator a iterator that returns a list of vertices. */ @Override public void writeVertices(final OutputStream outputStream, final Iterator<Vertex> vertexIterator) throws IOException { writeVertices(outputStream, vertexIterator, null); }
private String writeObject(Object object) { try (ByteArrayOutputStream out = new ByteArrayOutputStream(BUF_SIZE)) { this.writer.writeObject(out, object); return out.toString(API.CHARSET); } catch (Exception e) { throw new HugeException("Failed to serialize %s", e, object.getClass().getSimpleName()); } }
public GraphSONWriter create() { return new GraphSONWriter(this); } }
@Override public void exportToGson(OutputStream os) throws IOException { GraphSONMapper mapper = getGraph().io(IoCore.graphson()).mapper().create(); GraphSONWriter.Builder builder = GraphSONWriter.build(); builder.mapper(mapper); GraphSONWriter writer = builder.create(); writer.writeGraph(os, getGraph()); }
/** * Writes a {@link Graph} to stream in an adjacency list format where vertices are written with edges from both * directions. Under this serialization model, edges are grouped by label. * * @param outputStream the stream to write to. * @param g the graph to write to stream. */ @Override public void writeGraph(final OutputStream outputStream, final Graph g) throws IOException { writeVertices(outputStream, g.vertices(), Direction.BOTH); }
private String writeList(String label, List<?> list) { try (ByteArrayOutputStream out = new ByteArrayOutputStream(LBUF_SIZE)) { out.write(String.format("{\"%s\": ", label).getBytes(API.CHARSET)); this.writer.writeObject(out, list); out.write("}".getBytes(API.CHARSET)); return out.toString(API.CHARSET); } catch (Exception e) { throw new HugeException("Failed to serialize %s", e, label); } }
public GraphSONWriter create() { return new GraphSONWriter(this); } }
/** * Only need to execute this test with TinkerGraph or other graphs that support user supplied identifiers. */ @Test @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_USER_SUPPLIED_IDS) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_NUMERIC_IDS) @FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_USER_SUPPLIED_IDS) @FeatureRequirement(featureClass = Graph.Features.VariableFeatures.class, feature = FEATURE_VARIABLES) @LoadGraphWith(LoadGraphWith.GraphData.CLASSIC) public void shouldWriteNormalizedGraphSON() throws Exception { try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { final GraphSONMapper mapper = graph.io(graphson).mapper().version(GraphSONVersion.V1_0).normalize(true).create(); final GraphSONWriter w = graph.io(graphson).writer().mapper(mapper).create(); w.writeGraph(bos, graph); final String expected = streamToString(IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphSONResourceAccess.class) + "tinkerpop-classic-normalized-v1d0.json")); assertEquals(expected.replace("\n", "").replace("\r", ""), bos.toString().replace("\n", "").replace("\r", "")); } }
@Override public void exportToGson(OutputStream os) throws IOException { GraphSONMapper mapper = getGraph().io(IoCore.graphson()).mapper().create(); GraphSONWriter.Builder builder = GraphSONWriter.build(); builder.mapper(mapper); GraphSONWriter writer = builder.create(); writer.writeGraph(os, getGraph()); }
/** * {@inheritDoc} */ @Override public GraphSONWriter.Builder writer() { return GraphSONWriter.build().mapper(mapper().create()); }
@Override public void write(final NullWritable key, final VertexWritable vertex) throws IOException { if (null != vertex) { if (this.hasEdges) { graphsonWriter.writeVertex(this.outputStream, vertex.get(), Direction.BOTH); this.outputStream.write(NEWLINE); } else { graphsonWriter.writeVertex(this.outputStream, vertex.get()); this.outputStream.write(NEWLINE); } } }
/** * Writes a list of vertices without edges. * * @param outputStream the stream to write to. * @param vertexIterator a iterator that returns a list of vertices. */ @Override public void writeVertices(final OutputStream outputStream, final Iterator<Vertex> vertexIterator) throws IOException { writeVertices(outputStream, vertexIterator, null); }
first = false; this.writer.writeObject(out, itor.next());
/** * Only need to execute this test with TinkerGraph or other graphs that support user supplied identifiers. */ @Test @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_USER_SUPPLIED_IDS) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_NUMERIC_IDS) @FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_USER_SUPPLIED_IDS) @FeatureRequirement(featureClass = Graph.Features.VariableFeatures.class, feature = FEATURE_VARIABLES) @LoadGraphWith(LoadGraphWith.GraphData.CLASSIC) public void shouldWriteNormalizedGraphSON() throws Exception { try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { final GraphSONMapper mapper = graph.io(graphson).mapper().version(GraphSONVersion.V3_0).normalize(true).create(); final GraphSONWriter w = graph.io(graphson).writer().mapper(mapper).create(); w.writeGraph(bos, graph); final String expected = streamToString(IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphSONResourceAccess.class) + "tinkerpop-classic-normalized-v3d0.json")); assertEquals(expected.replace("\n", "").replace("\r", ""), bos.toString().replace("\n", "").replace("\r", "")); } }
final GraphSONMapper.Builder builder = GraphSONMapper.build(); detectRegistries().forEach(builder::addRegistry); return GraphSONWriter.build().mapper(builder.create()).create(); } else if (objectOrClass.equals(IO.gryo)){ final GryoMapper.Builder builder = GryoMapper.build();
/** * Writes a list of vertices in adjacency list format where vertices are written with edges from both * directions. Under this serialization model, edges are grouped by label. * * @param outputStream the stream to write to. * @param vertexIterator a traversal that returns a list of vertices. * @param direction if direction is null then no edges are written. */ @Override public void writeVertices(final OutputStream outputStream, final Iterator<Vertex> vertexIterator, final Direction direction) throws IOException { final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream)); try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { if (wrapAdjacencyList) writer.write("{\"" + GraphSONTokens.VERTICES + "\":["); while (vertexIterator.hasNext()) { writeVertex(baos, vertexIterator.next(), direction); writer.write(new String(baos.toByteArray())); if (wrapAdjacencyList) { if (vertexIterator.hasNext()) writer.write(","); } else { writer.newLine(); } baos.reset(); } if (wrapAdjacencyList) writer.write("]}"); } writer.flush(); }