Java Code Examples for org.apache.tinkerpop.shaded.kryo.io.Output#write()

The following examples show how to use org.apache.tinkerpop.shaded.kryo.io.Output#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TinkerGraphTest.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final Color color) {
    final TinkerGraph graph = TinkerGraph.open();
    final Vertex v = graph.addVertex(T.id, 1, T.label, "color", "name", color.toString());
    final Vertex vRed = graph.addVertex(T.id, 2, T.label, "primary", "name", "red");
    final Vertex vGreen = graph.addVertex(T.id, 3, T.label, "primary", "name", "green");
    final Vertex vBlue = graph.addVertex(T.id, 4, T.label, "primary", "name", "blue");

    v.addEdge("hasComponent", vRed, "amount", color.getRed());
    v.addEdge("hasComponent", vGreen, "amount", color.getGreen());
    v.addEdge("hasComponent", vBlue, "amount", color.getBlue());

    // make some junk so the graph is kinda big
    generate(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 ex) {
        ex.printStackTrace();
    }
}
 
Example 2
Source File: IoYIoRegistry.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final IoY ioy) {
    final Map<String, Object> map = new HashMap<>();
    map.put("y", ioy.toString());
    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output detachedOutput = new Output(stream);
        kryo.writeObject(detachedOutput, map);

        // have to remove the first byte because it marks a reference we don't want to have as this
        // serializer is trying to "act" like a Map
        final byte[] b = detachedOutput.toBytes();
        output.write(b, 1, b.length - 1);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
Example 3
Source File: IoXIoRegistry.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final IoX iox) {
    final Map<String,Object> props = new HashMap<>();
    addSingleProperty("x", iox.toString(), props);
    final DetachedVertex vertex = new DetachedVertex(100, Vertex.DEFAULT_LABEL, props);
    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output detachedOutput = new Output(stream);
        kryo.writeObject(detachedOutput, vertex);

        // have to remove the first byte because it marks a reference we don't want to have as this
        // serializer is trying to "act" like a DetachedVertex
        final byte[] b = detachedOutput.toBytes();
        output.write(b, 1, b.length - 1);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
Example 4
Source File: TinkerGraphTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final Color color) {
    final TinkerGraph graph = TinkerGraph.open();
    final Vertex v = graph.addVertex(T.id, 1, T.label, "color", "name", color.toString());
    final Vertex vRed = graph.addVertex(T.id, 2, T.label, "primary", "name", "red");
    final Vertex vGreen = graph.addVertex(T.id, 3, T.label, "primary", "name", "green");
    final Vertex vBlue = graph.addVertex(T.id, 4, T.label, "primary", "name", "blue");

    v.addEdge("hasComponent", vRed, "amount", color.getRed());
    v.addEdge("hasComponent", vGreen, "amount", color.getGreen());
    v.addEdge("hasComponent", vBlue, "amount", color.getBlue());

    // make some junk so the graph is kinda big
    generate(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 ex) {
        ex.printStackTrace();
    }
}
 
Example 5
Source File: HugeGryoModule.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, Id id) {
    output.writeByte(id.type().ordinal());
    byte[] idBytes = id.asBytes();
    output.write(idBytes.length);
    output.writeBytes(id.asBytes());
}
 
Example 6
Source File: TinkerIoRegistryV1d0.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 7
Source File: TinkerIoRegistryV3d0.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 8
Source File: TinkerIoRegistryV2d0.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 9
Source File: AbstractGryoMessageSerializerV1d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf serializeRequestAsBinary(final RequestMessage requestMessage, final ByteBufAllocator allocator) throws SerializationException {
    ByteBuf encodedMessage = null;
    try {
        final Kryo kryo = kryoThreadLocal.get();
        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            final Output output = new Output(baos, bufferSize);
            final String mimeType = mimeTypesSupported()[0];
            output.writeByte(mimeType.length());
            output.write(mimeType.getBytes(UTF8));

            kryo.writeObject(output, requestMessage.getRequestId());
            output.writeString(requestMessage.getProcessor());
            output.writeString(requestMessage.getOp());
            kryo.writeObject(output, requestMessage.getArgs());

            final long size = output.total();
            if (size > Integer.MAX_VALUE)
                throw new SerializationException(String.format("Message size of %s exceeds allocatable space", size));

            output.flush();
            encodedMessage = allocator.buffer((int) size);
            encodedMessage.writeBytes(baos.toByteArray());
        }

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage);

        logger.warn(String.format("Request [%s] could not be serialized by %s.", requestMessage, AbstractGryoMessageSerializerV1d0.class.getName()), ex);
        throw new SerializationException(ex);
    }
}
 
Example 10
Source File: AbstractGryoMessageSerializerV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf serializeRequestAsBinary(final RequestMessage requestMessage, final ByteBufAllocator allocator) throws SerializationException {
    ByteBuf encodedMessage = null;
    try {
        final Kryo kryo = kryoThreadLocal.get();
        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            final Output output = new Output(baos, bufferSize);
            final String mimeType = mimeTypesSupported()[0];
            output.writeByte(mimeType.length());
            output.write(mimeType.getBytes(UTF8));

            kryo.writeObject(output, requestMessage);

            final long size = output.total();
            if (size > Integer.MAX_VALUE)
                throw new SerializationException(String.format("Message size of %s exceeds allocatable space", size));

            output.flush();
            encodedMessage = allocator.buffer((int) size);
            encodedMessage.writeBytes(baos.toByteArray());
        }

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage);

        logger.warn(String.format("Request [%s] could not be serialized by %s.", requestMessage, AbstractGryoMessageSerializerV3d0.class.getName()), ex);
        throw new SerializationException(ex);
    }
}
 
Example 11
Source File: TinkerIoRegistryV1d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 12
Source File: TinkerIoRegistryV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 13
Source File: TinkerIoRegistryV2d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 14
Source File: HugeGryoModule.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, EdgeId edgeId) {
    byte[] idBytes = edgeId.asBytes();
    output.write(idBytes.length);
    output.writeBytes(edgeId.asBytes());
}
 
Example 15
Source File: GryoMessageSerializerV1d0Test.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final Color color) {
    output.write(color.equals(Color.RED) ? 1 : 0);
}