org.apache.tinkerpop.shaded.kryo.io.Output Java Examples

The following examples show how to use org.apache.tinkerpop.shaded.kryo.io.Output. 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 Project: tinkergraph-gremlin   Author: ShiftLeftSecurity   File: TinkerGraphTest.java    License: 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 Project: tinkerpop   Author: apache   File: GryoMapperTest.java    License: 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 #3
Source Project: tinkerpop   Author: apache   File: GryoMapperTest.java    License: 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 #4
Source Project: tinkerpop   Author: apache   File: GryoMapperTest.java    License: 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 #5
Source Project: tinkerpop   Author: apache   File: GryoMapperTest.java    License: 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 #6
Source Project: tinkerpop   Author: apache   File: GryoMapperTest.java    License: 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 #7
Source Project: tinkerpop   Author: apache   File: IoYIoRegistry.java    License: 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 #8
Source Project: tinkerpop   Author: apache   File: IoXIoRegistry.java    License: 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 #9
Source Project: tinkerpop   Author: apache   File: TinkerGraphTest.java    License: 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 #10
Source Project: hugegraph   Author: hugegraph   File: KryoUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static byte[] toKryo(Object value) {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
         Output output = new Output(bos, 256)) {
        kryo().writeObject(output, value);
        output.flush();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new BackendException("Failed to serialize: %s", e, value);
    }
}
 
Example #11
Source Project: hugegraph   Author: hugegraph   File: KryoUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static byte[] toKryoWithType(Object value) {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
         Output output = new Output(bos, 256)) {
        kryo().writeClassAndObject(output, value);
        output.flush();
        return bos.toByteArray();
   } catch (IOException e) {
       throw new BackendException("Failed to serialize: %s", e, value);
   }
}
 
Example #12
Source Project: hugegraph   Author: hugegraph   File: HugeGryoModule.java    License: 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 #13
Source Project: hugegraph   Author: hugegraph   File: HugeGryoModule.java    License: 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 #14
Source Project: hugegraph   Author: hugegraph   File: HugeGryoModule.java    License: 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 #15
Source Project: tinkergraph-gremlin   Author: ShiftLeftSecurity   File: TinkerIoRegistryV1d0.java    License: 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 #16
Source Project: tinkergraph-gremlin   Author: ShiftLeftSecurity   File: TinkerIoRegistryV3d0.java    License: 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 #17
Source Project: tinkergraph-gremlin   Author: ShiftLeftSecurity   File: TinkerIoRegistryV2d0.java    License: 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 #18
Source Project: tinkergraph-gremlin   Author: ShiftLeftSecurity   File: TinkerGraphTest.java    License: 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 #19
Source Project: tinkergraph-gremlin   Author: ShiftLeftSecurity   File: TinkerGraphTest.java    License: 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 #20
Source Project: tinkerpop   Author: apache   File: HadoopPoolShimService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void writeClassAndObject(final Object object, final OutputStream outputStream) {
    HadoopPools.getGryoPool().writeWithKryo(kryo -> {
        final Output output = new Output(outputStream);
        kryo.writeClassAndObject(output, object);
        output.flush();
    });
}
 
Example #21
Source Project: tinkerpop   Author: apache   File: AbstractGryoMessageSerializerV1d0.java    License: 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 #22
Source Project: tinkerpop   Author: apache   File: AbstractGryoMessageSerializerV1d0.java    License: 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 #23
Source Project: tinkerpop   Author: apache   File: AbstractGryoMessageSerializerV3d0.java    License: 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 #24
Source Project: tinkerpop   Author: apache   File: AbstractGryoMessageSerializerV3d0.java    License: 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 #25
Source Project: tinkerpop   Author: apache   File: ShadedSerializerAdapter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final T t) {
    /* These adapters could be cached pretty efficiently in instance fields if it were guaranteed that this
     * class was never subject to concurrent use.  That's true of Kryo instances, but it is not clear that
     * it is true of Serializer instances.
     */
    final ShadedKryoAdapter shadedKryoAdapter = new ShadedKryoAdapter(kryo);
    final ShadedOutputAdapter shadedOutputAdapter = new ShadedOutputAdapter(output);
    serializer.write(shadedKryoAdapter, shadedOutputAdapter, t);
}
 
Example #26
Source Project: tinkerpop   Author: apache   File: AbstractGryoClassResolver.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Registration writeClass(final Output output, final Class type) {
    if (null == type) {
        output.writeVarInt(Kryo.NULL, true);
        return null;
    }

    final Registration registration = kryo.getRegistration(type);
    if (registration.getId() == NAME)
        writeName(output, type);
    else
        output.writeVarInt(registration.getId() + 2, true);

    return registration;
}
 
Example #27
Source Project: tinkerpop   Author: apache   File: GryoWriter.java    License: 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 #28
Source Project: tinkerpop   Author: apache   File: GryoWriter.java    License: 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 #29
Source Project: tinkerpop   Author: apache   File: GryoWriter.java    License: 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 #30
Source Project: tinkerpop   Author: apache   File: GryoWriter.java    License: 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();
}