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

The following examples show how to use org.apache.tinkerpop.shaded.kryo.io.Input. 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: AbstractGryoClassResolver.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Registration readClass(final Input input) {
    final int classID = input.readVarInt(true);
    switch (classID) {
        case Kryo.NULL:
            return null;
        case NAME + 2: // Offset for NAME and NULL.
            return readName(input);
    }

    if (classID == memoizedClassId) return memoizedClassIdValue;
    final Registration registration = idToRegistration.get(classID - 2);
    if (registration == null) throw new KryoException("Encountered unregistered class ID: " + (classID - 2));
    memoizedClassId = classID;
    memoizedClassIdValue = registration;
    return registration;
}
 
Example #2
Source File: RecordId.java    From sqlg with MIT License 6 votes vote down vote up
@Override
public void read(Kryo kryo, Input input) {
    this.schemaTable = SchemaTable.of(input.readString(), input.readString());
    String s = input.readString();
    if (s.equals("s")) {
        //sequence
        this.id = ID.from(input.readLong());
    } else {
        int size = input.readInt();
        List<Comparable> identifiers = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            String identifier = input.readString();
            identifiers.add(identifier);
        }
        this.id = ID.from(identifiers);
    }
}
 
Example #3
Source File: AbstractGryoMessageSerializerV1d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public RequestMessage deserializeRequest(final ByteBuf msg) throws SerializationException {
    try {
        final Kryo kryo = kryoThreadLocal.get();
        final byte[] payload = new byte[msg.readableBytes()];
        msg.readBytes(payload);
        try (final Input input = new Input(payload)) {
            // by the time the message gets here, the mime length/type have been already read, so this part just
            // needs to process the payload.
            final UUID id = kryo.readObject(input, UUID.class);
            final String processor = input.readString();
            final String op = input.readString();

            final RequestMessage.Builder builder = RequestMessage.build(op)
                    .overrideRequestId(id)
                    .processor(processor);

            final Map<String, Object> args = kryo.readObject(input, HashMap.class);
            args.forEach(builder::addArg);
            return builder.create();
        }
    } catch (Exception ex) {
        logger.warn(String.format("Request [%s] could not be deserialized by %s.", msg, AbstractGryoMessageSerializerV1d0.class.getName()), ex);
        throw new SerializationException(ex);
    }
}
 
Example #4
Source File: AbstractGryoMessageSerializerV3d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public RequestMessage deserializeRequest(final ByteBuf msg) throws SerializationException {
    try {
        final Kryo kryo = kryoThreadLocal.get();
        final byte[] payload = new byte[msg.readableBytes()];
        msg.readBytes(payload);
        try (final Input input = new Input(payload)) {
            // by the time the message gets here, the mime length/type have been already read, so this part just
            // needs to process the payload.
            return kryo.readObject(input, RequestMessage.class);
        }
    } catch (Exception ex) {
        logger.warn(String.format("Request [%s] could not be deserialized by %s.", msg, AbstractGryoMessageSerializerV3d0.class.getName()), ex);
        throw new SerializationException(ex);
    }
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
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 #10
Source File: AbstractGryoClassResolver.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
protected Registration readName(final Input input) {
    final int nameId = input.readVarInt(true);
    if (nameIdToClass == null) nameIdToClass = new IntMap<>();
    Class type = nameIdToClass.get(nameId);
    if (type == null) {
        // Only read the class name the first time encountered in object graph.
        final String className = input.readString();
        type = getTypeByName(className);
        if (type == null) {
            try {
                type = Class.forName(className, false, kryo.getClassLoader());
            } catch (ClassNotFoundException ex) {
                throw new KryoException("Unable to find class: " + className, ex);
            }
            if (nameToClass == null) nameToClass = new ObjectMap<>();
            nameToClass.put(className, type);
        }
        nameIdToClass.put(nameId, type);
    }
    return kryo.getRegistration(type);
}
 
Example #11
Source File: TinkerIoRegistryV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public TinkerGraph read(final Kryo kryo, final Input input, final Class<TinkerGraph> tinkerGraphClass) {
    final Configuration conf = new BaseConfiguration();
    conf.setProperty("gremlin.tinkergraph.defaultVertexPropertyCardinality", "list");
    final TinkerGraph graph = TinkerGraph.open(conf);
    final int len = input.readInt();
    final byte[] bytes = input.readBytes(len);
    try (final ByteArrayInputStream stream = new ByteArrayInputStream(bytes)) {
        GryoReader.build().mapper(() -> kryo).create().readGraph(stream, graph);
    } catch (Exception io) {
        throw new RuntimeException(io);
    }

    return graph;
}
 
Example #12
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 #13
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 #14
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 #15
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 #16
Source File: TinkerIoRegistryV2d0.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
public TinkerGraph read(final Kryo kryo, final Input input, final Class<TinkerGraph> tinkerGraphClass) {
    final Configuration conf = new BaseConfiguration();
    conf.setProperty("gremlin.tinkergraph.defaultVertexPropertyCardinality", "list");
    final TinkerGraph graph = TinkerGraph.open(conf);
    final int len = input.readInt();
    final byte[] bytes = input.readBytes(len);
    try (final ByteArrayInputStream stream = new ByteArrayInputStream(bytes)) {
        GryoReader.build().mapper(() -> kryo).create().readGraph(stream, graph);
    } catch (Exception io) {
        throw new RuntimeException(io);
    }

    return graph;
}
 
Example #17
Source File: TinkerIoRegistryV3d0.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
public TinkerGraph read(final Kryo kryo, final Input input, final Class<TinkerGraph> tinkerGraphClass) {
    final Configuration conf = new BaseConfiguration();
    conf.setProperty("gremlin.tinkergraph.defaultVertexPropertyCardinality", "list");
    final TinkerGraph graph = TinkerGraph.open(conf);
    final int len = input.readInt();
    final byte[] bytes = input.readBytes(len);
    try (final ByteArrayInputStream stream = new ByteArrayInputStream(bytes)) {
        GryoReader.build().mapper(() -> kryo).create().readGraph(stream, graph);
    } catch (Exception io) {
        throw new RuntimeException(io);
    }

    return graph;
}
 
Example #18
Source File: TinkerIoRegistryV2d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public TinkerGraph read(final Kryo kryo, final Input input, final Class<TinkerGraph> tinkerGraphClass) {
    final Configuration conf = new BaseConfiguration();
    conf.setProperty("gremlin.tinkergraph.defaultVertexPropertyCardinality", "list");
    final TinkerGraph graph = TinkerGraph.open(conf);
    final int len = input.readInt();
    final byte[] bytes = input.readBytes(len);
    try (final ByteArrayInputStream stream = new ByteArrayInputStream(bytes)) {
        GryoReader.build().mapper(() -> kryo).create().readGraph(stream, graph);
    } catch (Exception io) {
        throw new RuntimeException(io);
    }

    return graph;
}
 
Example #19
Source File: GryoReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private Vertex readVertexInternal(final Function<Attachable<Vertex>, Vertex> vertexMaker,
                                  final Function<Attachable<Edge>, Edge> edgeMaker,
                                  final Direction d,
                                  final Input input) throws IOException {
    readHeader(input);
    final StarGraph starGraph = kryo.readObject(input, StarGraph.class);

    // read the terminator
    kryo.readClassAndObject(input);

    final Vertex v = vertexMaker.apply(starGraph.getStarVertex());
    if (edgeMaker != null)
        starGraph.getStarVertex().edges(d).forEachRemaining(e -> edgeMaker.apply((Attachable<Edge>) e));
    return v;
}
 
Example #20
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 #21
Source File: AbstractGryoMessageSerializerV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public ResponseMessage deserializeResponse(final ByteBuf msg) throws SerializationException {
    try {
        final Kryo kryo = kryoThreadLocal.get();
        final byte[] payload = new byte[msg.capacity()];
        msg.readBytes(payload);
        try (final Input input = new Input(payload)) {
            return kryo.readObject(input, ResponseMessage.class);
        }
    } catch (Exception ex) {
        logger.warn(String.format("Response [%s] could not be deserialized by %s.", msg, AbstractGryoMessageSerializerV3d0.class.getName()), ex);
        throw new SerializationException(ex);
    }
}
 
Example #22
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 #23
Source File: ShadedSerializerAdapter.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public T read(final Kryo kryo, final Input input, final Class<T> aClass) {
    // Same caching opportunity as in write(...)
    final ShadedKryoAdapter shadedKryoAdapter = new ShadedKryoAdapter(kryo);
    final ShadedInputAdapter shadedInputAdapter = new ShadedInputAdapter(input);
    return serializer.read(shadedKryoAdapter, shadedInputAdapter, aClass);
}
 
Example #24
Source File: GryoReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Read a {@link Property} from output generated by  {@link GryoWriter#writeProperty(OutputStream, Property)} or
 * via an {@link Property} passed to {@link GryoWriter#writeObject(OutputStream, Object)}.
 *
 * @param inputStream          a stream containing at least one {@link Property} as written by the accompanying
 *                             {@link GraphWriter#writeProperty(OutputStream, Property)} method.
 * @param propertyAttachMethod a function that creates re-attaches a {@link Property} to a {@link Host} object.
 */
@Override
public Property readProperty(final InputStream inputStream,
                             final Function<Attachable<Property>, Property> propertyAttachMethod) throws IOException {
    final Input input = new Input(inputStream);
    readHeader(input);
    final Attachable<Property> attachable = kryo.readObject(input, DetachedProperty.class);
    return propertyAttachMethod.apply(attachable);
}
 
Example #25
Source File: GryoReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Read data into a {@link Graph} from output generated by any of the {@link GryoWriter} {@code writeVertex} or
 * {@code writeVertices} methods or by {@link GryoWriter#writeGraph(OutputStream, Graph)}.
 *
 * @param inputStream    a stream containing an entire graph of vertices and edges as defined by the accompanying
 *                       {@link GraphWriter#writeGraph(OutputStream, Graph)}.
 * @param graphToWriteTo the graph to write to when reading from the stream.
 * @throws IOException
 */
@Override
public void readGraph(final InputStream inputStream, final Graph graphToWriteTo) throws IOException {
    // dual pass - create all vertices and store to cache the ids.  then create edges.  as long as we don't
    // have vertex labels in the output we can't do this single pass
    final Map<StarGraph.StarVertex, Vertex> cache = new HashMap<>();
    final AtomicLong counter = new AtomicLong(0);

    final Graph.Features.EdgeFeatures edgeFeatures = graphToWriteTo.features().edge();
    final boolean supportsTx = graphToWriteTo.features().graph().supportsTransactions();

    IteratorUtils.iterate(new VertexInputIterator(new Input(inputStream), attachable -> {
        final Vertex v = cache.put((StarGraph.StarVertex) attachable.get(), attachable.attach(Attachable.Method.create(graphToWriteTo)));
        if (supportsTx && counter.incrementAndGet() % batchSize == 0)
            graphToWriteTo.tx().commit();
        return v;
    }, null, null));
    cache.entrySet().forEach(kv -> kv.getKey().edges(Direction.IN).forEachRemaining(e -> {
        // can't use a standard Attachable attach method here because we have to use the cache for those
        // graphs that don't support userSuppliedIds on edges. note that outVertex/inVertex methods return
        // StarAdjacentVertex whose equality should match StarVertex.
        final Vertex cachedOutV = cache.get(e.outVertex());
        final Vertex cachedInV = cache.get(e.inVertex());

        if (null == cachedOutV) throw new IllegalStateException(String.format("Could not find outV with id [%s] to create edge with id [%s]", e.outVertex().id(), e.id()));
        if (null == cachedInV) throw new IllegalStateException(String.format("Could not find inV with id [%s] to create edge with id [%s]", e.inVertex().id(), e.id()));

        final Edge newEdge = edgeFeatures.willAllowId(e.id()) ? cachedOutV.addEdge(e.label(), cachedInV, T.id, e.id()) : cachedOutV.addEdge(e.label(), cachedInV);
        e.properties().forEachRemaining(p -> newEdge.property(p.key(), p.value()));
        if (supportsTx && counter.incrementAndGet() % batchSize == 0)
            graphToWriteTo.tx().commit();
    }));

    if (supportsTx) graphToWriteTo.tx().commit();
}
 
Example #26
Source File: GryoReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Vertex> readVertex(final InputStream inputStream, final GraphFilter graphFilter) throws IOException {
    StarGraphGryoSerializer serializer = this.graphFilterCache.get(graphFilter);
    if (null == serializer) {
        serializer = StarGraphGryoSerializer.withGraphFilter(graphFilter);
        this.graphFilterCache.put(graphFilter, serializer);
    }
    final Input input = new Input(inputStream);
    this.readHeader(input);
    final StarGraph starGraph = this.kryo.readObject(input, StarGraph.class, serializer);
    // read the terminator
    this.kryo.readClassAndObject(input);
    return Optional.ofNullable(starGraph == null ? null : starGraph.getStarVertex());
}
 
Example #27
Source File: HugeGryoModule.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
private static Map<HugeKeys, Object> readEntry(Kryo kryo, Input input) {
    int columnSize = input.readInt();
    Map<HugeKeys, Object> map = new LinkedHashMap<>();
    for (int i = 0; i < columnSize; i++) {
        HugeKeys key = kryo.readObject(input, HugeKeys.class);
        Object val = kryo.readClassAndObject(input);
        map.put(key, val);
    }
    return map;
}
 
Example #28
Source File: WrappedArraySerializer.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public WrappedArray<T> read(final Kryo kryo, final Input input, final Class<WrappedArray<T>> aClass) {
    final int size = input.readVarInt(true);
    final Object[] array = new Object[size];
    for (int i = 0; i < size; i++) {
        array[i] = kryo.readClassAndObject(input);
    }
    return new WrappedArray.ofRef<>((T[]) array);
}
 
Example #29
Source File: HugeGryoModule.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
public Id read(Kryo kryo, Input input, Class<Id> clazz) {
    int type = input.readByteUnsigned();
    int length = input.read();
    byte[] idBytes = input.readBytes(length);
    return IdGenerator.of(idBytes, IdType.values()[type]);
}
 
Example #30
Source File: GryoReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private void readHeader(final Input input) throws IOException {
    if (!Arrays.equals(GryoMapper.GIO, input.readBytes(3)))
        throw new IOException("Invalid format - first three bytes of header do not match expected value");

    // skip the next 13 bytes - for future use
    input.readBytes(13);
}