Java Code Examples for org.apache.tinkerpop.shaded.kryo.Kryo#writeObject()

The following examples show how to use org.apache.tinkerpop.shaded.kryo.Kryo#writeObject() . 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: 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 2
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 3
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, Optional<?> optional) {
    if (optional.isPresent()) {
        kryo.writeClassAndObject(output, optional.get());
    } else {
        kryo.writeObject(output, null);
    }
}
 
Example 4
Source File: HugeGryoModule.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private static void writeEntry(Kryo kryo,
                               Output output,
                               Map<HugeKeys, Object> schema) {
    /* Write columns size and data */
    output.writeInt(schema.keySet().size());
    for (Map.Entry<HugeKeys, Object> entry : schema.entrySet()) {
        kryo.writeObject(output, entry.getKey());
        kryo.writeClassAndObject(output, entry.getValue());
    }
}
 
Example 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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()));
        }
    }
}