@Override public void write(final Kryo kryo, final Output output, final TinkerGraph graph) { try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) { GryoWriter.build().mapper(() -> kryo).create().writeGraph(stream, graph); final byte[] bytes = stream.toByteArray(); output.writeInt(bytes.length); output.write(bytes); } catch (Exception io) { throw new RuntimeException(io); } }
/** * {@inheritDoc} */ @Override public void writeVertex(final OutputStream outputStream, final Vertex v) throws IOException { writeVertex(outputStream, v, null); }
/** * Create the {@code GryoWriter}. */ public GryoWriter create() { return new GryoWriter(this.gryoMapper); } }
public GryoWriter takeWriter() { final GryoWriter writer = this.gryoWriters.poll(); return null == writer ? GryoWriter.build().mapper(mapper).create() : writer; }
/** * {@inheritDoc} */ @Override public void writeGraph(final String file) throws IOException { try (final OutputStream out = new FileOutputStream(file)) { writer().create().writeGraph(out, graph); } }
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); } } }
void writeVertexInternal(final Output output, final Vertex v) throws IOException { writeHeader(output); kryo.writeObject(output, StarGraph.of(v)); kryo.writeClassAndObject(output, VertexTerminator.INSTANCE); }
/** * {@inheritDoc} */ @Override public void writeVertices(final OutputStream outputStream, final Iterator<Vertex> vertexIterator, final Direction direction) throws IOException { kryo.getRegistration(StarGraph.class).setSerializer(StarGraphGryoSerializer.with(direction)); final Output output = new Output(outputStream); while (vertexIterator.hasNext()) { writeVertexInternal(output, vertexIterator.next()); } output.flush(); kryo.getRegistration(StarGraph.class).setSerializer(StarGraphGryoSerializer.with(Direction.BOTH)); }
/** * {@inheritDoc} */ @Override public GryoWriter.Builder writer() { return GryoWriter.build().mapper(mapper().create()); }
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReadWriteSelfLoopingEdges() { final Graph source = graph; final Vertex v1 = source.addVertex(); final Vertex v2 = source.addVertex(); v1.addEdge("CONTROL", v2); v1.addEdge("SELFLOOP", v1); final Configuration targetConf = graphProvider.newGraphConfiguration("target", this.getClass(), name.getMethodName(), null); final Graph target = graphProvider.openTestGraph(targetConf); ; try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { source.io(IoCore.gryo()).writer().create().writeGraph(os, source); try (ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray())) { target.io(IoCore.gryo()).reader().create().readGraph(is, target); } } catch (IOException ioe) { throw new RuntimeException(ioe); } assertEquals(IteratorUtils.count(source.vertices()), IteratorUtils.count(target.vertices())); assertEquals(IteratorUtils.count(source.edges()), IteratorUtils.count(target.edges())); } }
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); } }
/** * {@inheritDoc} */ @Override public void writeVertexProperty(final OutputStream outputStream, final VertexProperty vp) throws IOException { final Output output = new Output(outputStream); writeHeader(output); kryo.writeObject(output, DetachedFactory.detach(vp, true)); output.flush(); }
/** * {@inheritDoc} */ @Override public void writeVertex(final OutputStream outputStream, final Vertex v, final Direction direction) throws IOException { kryo.getRegistration(StarGraph.class).setSerializer(StarGraphGryoSerializer.with(direction)); final Output output = new Output(outputStream); writeVertexInternal(output, v); output.flush(); kryo.getRegistration(StarGraph.class).setSerializer(StarGraphGryoSerializer.with(Direction.BOTH)); }
@Override public void write(final Kryo kryo, final Output output, final TinkerGraph graph) { try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) { GryoWriter.build().mapper(() -> kryo).create().writeGraph(stream, graph); final byte[] bytes = stream.toByteArray(); output.writeInt(bytes.length); output.write(bytes); } catch (Exception io) { throw new RuntimeException(io); } }
private void createPool(final int poolSize, final Type type, final GryoMapper gryoMapper) { this.mapper = gryoMapper; if (type.equals(Type.READER) || type.equals(Type.READER_WRITER)) { gryoReaders = new LinkedBlockingQueue<>(poolSize); for (int i = 0; i < poolSize; i++) { gryoReaders.add(GryoReader.build().mapper(gryoMapper).create()); } } if (type.equals(Type.WRITER) || type.equals(Type.READER_WRITER)) { gryoWriters = new LinkedBlockingQueue<>(poolSize); for (int i = 0; i < poolSize; i++) { gryoWriters.add(GryoWriter.build().mapper(gryoMapper).create()); } } kryos = new LinkedBlockingQueue<>(poolSize); for (int i = 0; i < poolSize; i++) { kryos.add(gryoMapper.createMapper()); } }
/** * {@inheritDoc} */ @Override public void writeGraph(final String file) throws IOException { try (final OutputStream out = new FileOutputStream(file)) { writer().create().writeGraph(out, graph); } }
@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()); }
@Override public void write(final NullWritable key, final VertexWritable vertex) throws IOException { if (null != vertex) { if (this.hasEdges) gryoWriter.writeVertex(this.outputStream, vertex.get(), Direction.BOTH); else gryoWriter.writeVertex(this.outputStream, vertex.get()); } }
/** * {@inheritDoc} */ @Override public void writeProperty(final OutputStream outputStream, final Property p) throws IOException { final Output output = new Output(outputStream); writeHeader(output); kryo.writeObject(output, DetachedFactory.detach(p, true)); output.flush(); }
/** * {@inheritDoc} */ @Override public void writeVertices(final OutputStream outputStream, final Iterator<Vertex> vertexIterator, final Direction direction) throws IOException { kryo.getRegistration(StarGraph.class).setSerializer(StarGraphGryoSerializer.with(direction)); final Output output = new Output(outputStream); while (vertexIterator.hasNext()) { writeVertexInternal(output, vertexIterator.next()); } output.flush(); kryo.getRegistration(StarGraph.class).setSerializer(StarGraphGryoSerializer.with(Direction.BOTH)); }