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

The following examples show how to use org.apache.tinkerpop.shaded.kryo.io.Output#flush() . 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 5 votes vote down vote up
@Test
public void shouldSerializeWithColorClassResolverToTinkerGraph() throws Exception {
    final Map<String,Color> colors = new HashMap<>();
    colors.put("red", Color.RED);
    colors.put("green", Color.GREEN);

    final ArrayList<Color> colorList = new ArrayList<>(Arrays.asList(Color.RED, Color.GREEN));

    final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier();
    final GryoMapper mapper = GryoMapper.build().version(GryoVersion.V3_0).addRegistry(TinkerIoRegistryV3d0.instance()).classResolver(classResolver).create();
    final Kryo kryo = mapper.createMapper();
    try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);

        kryo.writeObject(out, colorList);
        out.flush();
        final byte[] b = stream.toByteArray();

        try (final InputStream inputStream = new ByteArrayInputStream(b)) {
            final Input input = new Input(inputStream);
            final List m = kryo.readObject(input, ArrayList.class);
            final TinkerGraph readX = (TinkerGraph) m.get(0);
            assertEquals(104, IteratorUtils.count(readX.vertices()));
            assertEquals(102, IteratorUtils.count(readX.edges()));
        }
    }
}
 
Example 2
Source File: TinkerGraphTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeWithColorClassResolverToTinkerGraphUsingDeprecatedTinkerIoRegistry() throws Exception {
    final Map<String,Color> colors = new HashMap<>();
    colors.put("red", Color.RED);
    colors.put("green", Color.GREEN);

    final ArrayList<Color> colorList = new ArrayList<>(Arrays.asList(Color.RED, Color.GREEN));

    final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier();
    final GryoMapper mapper = GryoMapper.build().version(GryoVersion.V3_0).addRegistry(TinkerIoRegistryV3d0.instance()).classResolver(classResolver).create();
    final Kryo kryo = mapper.createMapper();
    try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);

        kryo.writeObject(out, colorList);
        out.flush();
        final byte[] b = stream.toByteArray();

        try (final InputStream inputStream = new ByteArrayInputStream(b)) {
            final Input input = new Input(inputStream);
            final List m = kryo.readObject(input, ArrayList.class);
            final TinkerGraph readX = (TinkerGraph) m.get(0);
            assertEquals(104, IteratorUtils.count(readX.vertices()));
            assertEquals(102, IteratorUtils.count(readX.edges()));
        }
    }
}
 
Example 3
Source File: AbstractGryoMessageSerializerV1d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage, 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);

            // request id - if present
            kryo.writeObjectOrNull(output, responseMessage.getRequestId() != null ? responseMessage.getRequestId() : null, UUID.class);

            // status
            output.writeShort(responseMessage.getStatus().getCode().getValue());
            output.writeString(responseMessage.getStatus().getMessage());
            kryo.writeClassAndObject(output, responseMessage.getStatus().getAttributes());

            // result
            kryo.writeClassAndObject(output, serializeToString ? serializeResultToString(responseMessage) : responseMessage.getResult().getData());
            kryo.writeClassAndObject(output, responseMessage.getResult().getMeta());

            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("Response [%s] could not be serialized by %s.", responseMessage, AbstractGryoMessageSerializerV1d0.class.getName()), ex);
        throw new SerializationException(ex);
    }
}
 
Example 4
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 5
Source File: AbstractGryoMessageSerializerV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage, 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 ResponseMessage msgToWrite = !serializeToString ? responseMessage :
                    ResponseMessage.build(responseMessage.getRequestId())
                        .code(responseMessage.getStatus().getCode())
                        .statusAttributes(responseMessage.getStatus().getAttributes())
                        .responseMetaData(responseMessage.getResult().getMeta())
                        .result(serializeResultToString(responseMessage))
                        .statusMessage(responseMessage.getStatus().getMessage()).create();
            kryo.writeObject(output, msgToWrite);

            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("Response [%s] could not be serialized by %s.", responseMessage, AbstractGryoMessageSerializerV3d0.class.getName()), ex);
        throw new SerializationException(ex);
    }
}
 
Example 6
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 7
Source File: GryoWriter.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * {@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));
}
 
Example 8
Source File: GryoWriter.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * {@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));
}
 
Example 9
Source File: GryoWriter.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void writeEdge(final OutputStream outputStream, final Edge e) throws IOException {
    final Output output = new Output(outputStream);
    writeHeader(output);
    kryo.writeObject(output, DetachedFactory.detach(e, true));
    output.flush();
}
 
Example 10
Source File: GryoWriter.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * {@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();
}
 
Example 11
Source File: GryoWriter.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * {@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();
}
 
Example 12
Source File: GryoWriter.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void writeObject(final OutputStream outputStream, final Object object) {
    final Output output = new Output(outputStream);
    this.kryo.writeClassAndObject(output, object);
    output.flush();
}
 
Example 13
Source File: GryoMapperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public <T> T serializeDeserialize(final Object o, final Class<T> clazz) throws Exception {
    final Kryo kryo = builder.get().create().createMapper();
    try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);
        kryo.writeObject(out, o);
        out.flush();

        try (final InputStream inputStream = new ByteArrayInputStream(stream.toByteArray())) {
            final Input input = new Input(inputStream);
            return kryo.readObject(input, clazz);
        }
    }
}
 
Example 14
Source File: TinkerGraphTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeWithColorClassResolverToTinkerGraph() throws Exception {
    final Map<String,Color> colors = new HashMap<>();
    colors.put("red", Color.RED);
    colors.put("green", Color.GREEN);

    final ArrayList<Color> colorList = new ArrayList<>(Arrays.asList(Color.RED, Color.GREEN));

    final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier();
    final GryoMapper mapper = GryoMapper.build().version(GryoVersion.V3_0).addRegistry(TinkerIoRegistryV3d0.instance()).classResolver(classResolver).create();
    final Kryo kryo = mapper.createMapper();
    try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);

        kryo.writeObject(out, colorList);
        out.flush();
        final byte[] b = stream.toByteArray();

        try (final InputStream inputStream = new ByteArrayInputStream(b)) {
            final Input input = new Input(inputStream);
            final List m = kryo.readObject(input, ArrayList.class);
            final TinkerGraph readX = (TinkerGraph) m.get(0);
            assertEquals(104, IteratorUtils.count(readX.vertices()));
            assertEquals(102, IteratorUtils.count(readX.edges()));
        }
    }
}
 
Example 15
Source File: TinkerGraphTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeWithColorClassResolverToTinkerGraphUsingDeprecatedTinkerIoRegistry() throws Exception {
    final Map<String,Color> colors = new HashMap<>();
    colors.put("red", Color.RED);
    colors.put("green", Color.GREEN);

    final ArrayList<Color> colorList = new ArrayList<>(Arrays.asList(Color.RED, Color.GREEN));

    final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier();
    final GryoMapper mapper = GryoMapper.build().version(GryoVersion.V3_0).addRegistry(TinkerIoRegistryV3d0.instance()).classResolver(classResolver).create();
    final Kryo kryo = mapper.createMapper();
    try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);

        kryo.writeObject(out, colorList);
        out.flush();
        final byte[] b = stream.toByteArray();

        try (final InputStream inputStream = new ByteArrayInputStream(b)) {
            final Input input = new Input(inputStream);
            final List m = kryo.readObject(input, ArrayList.class);
            final TinkerGraph readX = (TinkerGraph) m.get(0);
            assertEquals(104, IteratorUtils.count(readX.vertices()));
            assertEquals(102, IteratorUtils.count(readX.edges()));
        }
    }
}
 
Example 16
Source File: GryoCompatibilityTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] write(final Object o, final Class<?> clazz) throws Exception  {
    try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final Output output = new Output(stream);
        mapper.writeObject(output, o);
        output.flush();
        return stream.toByteArray();
    }
}