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

The following examples show how to use org.apache.tinkerpop.shaded.kryo.Kryo#writeClassAndObject() . 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: GryoMapperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRegisterMultipleIoRegistryToSerialize() throws Exception {
    final GryoMapper mapper = builder.get().addRegistry(IoXIoRegistry.InstanceBased.instance())
            .addRegistry(IoYIoRegistry.InstanceBased.instance()).create();
    final Kryo kryo = mapper.createMapper();
    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);
        final IoX x = new IoX("x");
        final IoY y = new IoY(100, 200);
        kryo.writeClassAndObject(out, x);
        kryo.writeClassAndObject(out, y);

        try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) {
            final Input input = new Input(inputStream);
            final IoX readX = (IoX) kryo.readClassAndObject(input);
            final IoY readY = (IoY) kryo.readClassAndObject(input);
            assertEquals(x, readX);
            assertEquals(y, readY);
        }
    }
}
 
Example 2
Source File: GryoMapperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeWithoutRegistration() throws Exception {
    final GryoMapper mapper = builder.get().registrationRequired(false).create();
    final Kryo kryo = mapper.createMapper();
    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);
        final IoX x = new IoX("x");
        final IoY y = new IoY(100, 200);
        kryo.writeClassAndObject(out, x);
        kryo.writeClassAndObject(out, y);

        try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) {
            final Input input = new Input(inputStream);
            final IoX readX = (IoX) kryo.readClassAndObject(input);
            final IoY readY = (IoY) kryo.readClassAndObject(input);
            assertEquals(x, readX);
            assertEquals(y, readY);
        }
    }
}
 
Example 3
Source File: GryoMapperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeWithCustomClassResolverToHashMap() throws Exception {
    final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier();
    final GryoMapper mapper = builder.get().classResolver(classResolver).create();
    final Kryo kryo = mapper.createMapper();
    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);
        final IoY y = new IoY(100, 200);

        kryo.writeClassAndObject(out, y);

        final GryoMapper mapperWithoutKnowledgeOfIoy = builder.get().create();
        final Kryo kryoWithoutKnowledgeOfIox = mapperWithoutKnowledgeOfIoy.createMapper();
        try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) {
            final Input input = new Input(inputStream);
            final Map readY = (HashMap) kryoWithoutKnowledgeOfIox.readClassAndObject(input);
            assertEquals("100-200", readY.get("y"));
        }
    }
}
 
Example 4
Source File: GryoMapperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeDeserialize() throws Exception {
    final GryoMapper mapper = builder.get().create();
    final Kryo kryo = mapper.createMapper();
    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);

        final Map<String,Object> props = new HashMap<>();
        final List<Map<String, Object>> propertyNames = new ArrayList<>(1);
        final Map<String,Object> propertyName = new HashMap<>();
        propertyName.put(GraphSONTokens.ID, "x");
        propertyName.put(GraphSONTokens.KEY, "x");
        propertyName.put(GraphSONTokens.VALUE, "no-way-this-will-ever-work");
        propertyNames.add(propertyName);
        props.put("x", propertyNames);
        final DetachedVertex v = new DetachedVertex(100, Vertex.DEFAULT_LABEL, props);

        kryo.writeClassAndObject(out, v);

        try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) {
            final Input input = new Input(inputStream);
            final DetachedVertex readX = (DetachedVertex) kryo.readClassAndObject(input);
            assertEquals("no-way-this-will-ever-work", readX.value("x"));
        }
    }
}
 
Example 5
Source File: GryoMapperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeWithCustomClassResolverToDetachedVertex() throws Exception {
    final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier();
    final GryoMapper mapper = builder.get().classResolver(classResolver).create();
    final Kryo kryo = mapper.createMapper();
    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);
        final IoX x = new IoX("no-way-this-will-ever-work");

        kryo.writeClassAndObject(out, x);

        final GryoMapper mapperWithoutKnowledgeOfIox = builder.get().create();
        final Kryo kryoWithoutKnowledgeOfIox = mapperWithoutKnowledgeOfIox.createMapper();
        try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) {
            final Input input = new Input(inputStream);
            final DetachedVertex readX = (DetachedVertex) kryoWithoutKnowledgeOfIox.readClassAndObject(input);
            assertEquals("no-way-this-will-ever-work", readX.value("x"));
        }
    }
}
 
Example 6
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 7
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 8
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 9
Source File: GryoMapperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldExpectReadFailureAsIoRegistryOrderIsNotRespected() throws Exception {
    final GryoMapper mapperWrite = builder.get().addRegistry(IoXIoRegistry.InstanceBased.instance())
            .addRegistry(IoYIoRegistry.InstanceBased.instance()).create();

    final GryoMapper mapperRead = GryoMapper.build()
            .addRegistry(IoYIoRegistry.InstanceBased.instance())
            .addRegistry(IoXIoRegistry.InstanceBased.instance()).create();

    final Kryo kryoWriter = mapperWrite.createMapper();
    final Kryo kryoReader = mapperRead.createMapper();
    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);
        final IoX x = new IoX("x");
        final IoY y = new IoY(100, 200);
        kryoWriter.writeClassAndObject(out, x);
        kryoWriter.writeClassAndObject(out, y);

        try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) {
            final Input input = new Input(inputStream);

            // kryo will read a IoY instance as we've reversed the registries.  it is neither an X or a Y
            // so assert that both are incorrect
            final IoY readY = (IoY) kryoReader.readClassAndObject(input);
            assertNotEquals(y, readY);
            assertNotEquals(x, readY);
        }
    }
}
 
Example 10
Source File: Tuple3Serializer.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 Tuple3<A, B, C> tuple3) {
    kryo.writeClassAndObject(output, tuple3._1());
    kryo.writeClassAndObject(output, tuple3._2());
    kryo.writeClassAndObject(output, tuple3._3());
}
 
Example 11
Source File: Tuple2Serializer.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 Tuple2<A, B> tuple2) {
    kryo.writeClassAndObject(output, tuple2._1());
    kryo.writeClassAndObject(output, tuple2._2());
}
 
Example 12
Source File: UtilSerializers.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 Map.Entry entry) {
    kryo.writeClassAndObject(output, entry.getKey());
    kryo.writeClassAndObject(output, entry.getValue());
}
 
Example 13
Source File: GryoLiteMessageSerializerV1d0.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 Path path) {
    kryo.writeClassAndObject(output, ReferenceFactory.detach(path));
}
 
Example 14
Source File: GryoLiteMessageSerializerV1d0.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 VertexProperty vertexProperty) {
    kryo.writeClassAndObject(output, ReferenceFactory.detach(vertexProperty));
}
 
Example 15
Source File: GryoLiteMessageSerializerV1d0.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 Property property) {
    kryo.writeClassAndObject(output, property instanceof VertexProperty ? ReferenceFactory.detach((VertexProperty) property) : ReferenceFactory.detach(property));
}
 
Example 16
Source File: GryoLiteMessageSerializerV1d0.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 Vertex vertex) {
    kryo.writeClassAndObject(output, ReferenceFactory.detach(vertex));
}
 
Example 17
Source File: GryoLiteMessageSerializerV1d0.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 Edge edge) {
    kryo.writeClassAndObject(output, ReferenceFactory.detach(edge));
}