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 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 #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 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 #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: 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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
Source File: HadoopPoolShimService.java    From tinkerpop with 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 #13
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 #14
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 #15
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 #16
Source File: KryoUtil.java    From hugegraph with 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 #17
Source File: AbstractGryoClassResolver.java    From tinkerpop with 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 #18
Source File: ShadedSerializerAdapter.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 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 #19
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 #20
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 #21
Source File: GryoMapperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldOverrideExistingSerializer() throws Exception {
    final GryoMapper mapper = builder.get().addCustom(Duration.class, new OverrideDurationSerializer()).create();

    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);
        mapper.createMapper().writeObject(out, Duration.ZERO);
        fail("The OverrideDurationSerializer throws exceptions so this should not have worked");
    } catch (Exception ex) {
        assertThat(ex, instanceOf(UnsupportedOperationException.class));
        assertEquals("I don't do anything", ex.getMessage());
    }
}
 
Example #22
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 #23
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 #24
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 #25
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 #26
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 #27
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 #28
Source File: WrappedArraySerializer.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 WrappedArray<T> iterable) {
    output.writeVarInt(iterable.size(), true);
    JavaConversions.asJavaCollection(iterable).forEach(t -> {
        kryo.writeClassAndObject(output, t);
    });
}
 
Example #29
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();
    }
}
 
Example #30
Source File: RecordId.java    From sqlg with MIT License 5 votes vote down vote up
@Override
public void write(Kryo kryo, Output output) {
    output.writeString(this.getSchemaTable().getSchema());
    output.writeString(this.getSchemaTable().getTable());
    if (hasSequenceId()) {
        output.writeString("s");
        output.writeLong(this.getID().sequenceId);
    } else {
        output.writeString("i");
        output.writeInt(getIdentifiers().size());
        for (Comparable identifier : getIdentifiers()) {
            output.writeString((CharSequence) identifier);
        }
    }
}