Search in sources :

Example 1 with StarGraph

use of org.apache.tinkerpop.gremlin.structure.util.star.StarGraph in project GraphScope by alibaba.

the class MaxGraphGryoReader method readGraph.

/**
 * 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 {
    if (!(graphToWriteTo instanceof TinkerMaxGraph)) {
        throw new IOException("Only support snapshot maxgraph here.");
    }
    TinkerMaxGraph tinkerMaxGraph = (TinkerMaxGraph) graphToWriteTo;
    Transaction tx = tinkerMaxGraph.tx();
    tx.open();
    Input input = new Input(inputStream);
    // insert all vertices
    List<StarGraph.StarVertex> starVertexList = Lists.newArrayList();
    while (!input.eof()) {
        readHeader(input);
        final StarGraph starGraph = kryo.readObject(input, StarGraph.class);
        // read the terminator
        kryo.readClassAndObject(input);
        StarGraph.StarVertex starVertex = starGraph.getStarVertex();
        Iterator<VertexProperty<Object>> propertyIterator = starVertex.properties();
        List<Object> keyValues = Lists.newArrayList();
        keyValues.add(T.label);
        keyValues.add(starVertex.label());
        keyValues.add("id");
        keyValues.add(Long.parseLong(starVertex.id().toString()));
        while (propertyIterator.hasNext()) {
            VertexProperty<Object> vertexProperty = propertyIterator.next();
            keyValues.add(vertexProperty.key());
            keyValues.add(vertexProperty.value());
        }
        tinkerMaxGraph.addVertexAsync(keyValues.toArray());
        starVertexList.add(starVertex);
    }
    tx.commit();
    // query the inserted vertices
    Iterator<Vertex> vertexIterator = graphToWriteTo.vertices();
    Map<Integer, Vertex> insertedVertexMap = Maps.newHashMap();
    while (vertexIterator.hasNext()) {
        Vertex vertex = vertexIterator.next();
        insertedVertexMap.put(((Long) vertex.property("id").value()).intValue(), vertex);
    }
    // insert all the edges
    for (StarGraph.StarVertex starVertex : starVertexList) {
        Iterator<Edge> edgeIterator = starVertex.edges(Direction.OUT);
        while (edgeIterator.hasNext()) {
            Edge edge = edgeIterator.next();
            Vertex srcVertex = edge.outVertex();
            Vertex dstVertex = edge.inVertex();
            Vertex insertedSrcVertex = insertedVertexMap.get(srcVertex.id());
            Vertex insertedDstVertex = insertedVertexMap.get(dstVertex.id());
            checkNotNull(insertedSrcVertex, "src vertex cant be null for edge " + edge);
            checkNotNull(insertedDstVertex, "dst vertex cant be null for edge " + edge);
            List<Object> keyValues = Lists.newArrayList();
            Iterator<Property<Object>> propertyIterator = edge.properties();
            keyValues.add("id");
            keyValues.add(edge.id());
            while (propertyIterator.hasNext()) {
                Property<Object> property = propertyIterator.next();
                keyValues.add(property.key());
                keyValues.add(property.value());
            }
            tinkerMaxGraph.addEdgeAsync(insertedSrcVertex, insertedDstVertex, edge.label(), keyValues.toArray());
        }
    }
    tx.commit();
}
Also used : StarGraph(org.apache.tinkerpop.gremlin.structure.util.star.StarGraph) IOException(java.io.IOException) Input(org.apache.tinkerpop.shaded.kryo.io.Input) TinkerMaxGraph(com.alibaba.maxgraph.structure.graph.TinkerMaxGraph) DetachedVertexProperty(org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertexProperty) DetachedEdge(org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge) DetachedVertexProperty(org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertexProperty) DetachedProperty(org.apache.tinkerpop.gremlin.structure.util.detached.DetachedProperty)

Example 2 with StarGraph

use of org.apache.tinkerpop.gremlin.structure.util.star.StarGraph in project GraphScope by alibaba.

the class MaxGraphGryoReader method readVertex.

@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());
}
Also used : Input(org.apache.tinkerpop.shaded.kryo.io.Input) StarGraph(org.apache.tinkerpop.gremlin.structure.util.star.StarGraph) StarGraphGryoSerializer(org.apache.tinkerpop.gremlin.structure.util.star.StarGraphGryoSerializer)

Aggregations

StarGraph (org.apache.tinkerpop.gremlin.structure.util.star.StarGraph)2 Input (org.apache.tinkerpop.shaded.kryo.io.Input)2 TinkerMaxGraph (com.alibaba.maxgraph.structure.graph.TinkerMaxGraph)1 IOException (java.io.IOException)1 DetachedEdge (org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge)1 DetachedProperty (org.apache.tinkerpop.gremlin.structure.util.detached.DetachedProperty)1 DetachedVertexProperty (org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertexProperty)1 StarGraphGryoSerializer (org.apache.tinkerpop.gremlin.structure.util.star.StarGraphGryoSerializer)1