Java Code Examples for org.apache.tinkerpop.gremlin.structure.util.star.StarGraph#StarVertex

The following examples show how to use org.apache.tinkerpop.gremlin.structure.util.star.StarGraph#StarVertex . 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: InputFormatHadoop.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    while (reader.nextKeyValue()) {
        // TODO janusgraph05 integration -- the duplicate() call may be unnecessary
        TinkerVertex maybeNullTinkerVertex = deserializer.readHadoopVertex(reader.getCurrentKey(), reader.getCurrentValue());
        if (null != maybeNullTinkerVertex) {
            vertex = new VertexWritable(maybeNullTinkerVertex);
            if (graphFilter == null) {
                return true;
            } else {
                final Optional<StarGraph.StarVertex> vertexWritable = vertex.get().applyGraphFilter(graphFilter);
                if (vertexWritable.isPresent()) {
                    vertex.set(vertexWritable.get());
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 2
Source File: ScriptRecordReader.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public boolean nextKeyValue() throws IOException {
    while (true) {
        if (!this.lineRecordReader.nextKeyValue()) return false;
        try {
            final Bindings bindings = this.engine.createBindings();
            final StarGraph graph = StarGraph.open();
            bindings.put(GRAPH, graph);
            bindings.put(LINE, this.lineRecordReader.getCurrentValue().toString());
            final StarGraph.StarVertex sv = (StarGraph.StarVertex) script.eval(bindings);
            if (sv != null) {
                final Optional<StarGraph.StarVertex> vertex = sv.applyGraphFilter(this.graphFilter);
                if (vertex.isPresent()) {
                    this.vertexWritable.set(vertex.get());
                    return true;
                }
            }
        } catch (final ScriptException e) {
            throw new IOException(e.getMessage());
        }
    }
}
 
Example 3
Source File: GraphFilterRecordReader.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    if (null == this.graphFilter) {
        return this.recordReader.nextKeyValue();
    } else {
        while (true) {
            if (this.recordReader.nextKeyValue()) {
                final VertexWritable vertexWritable = this.recordReader.getCurrentValue();
                final Optional<StarGraph.StarVertex> vertex = vertexWritable.get().applyGraphFilter(this.graphFilter);
                if (vertex.isPresent()) {
                    vertexWritable.set(vertex.get());
                    return true;
                }
            } else {
                return false;
            }
        }
    }
}
 
Example 4
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 5
Source File: GraphSONReader.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 GraphSONWriter} {@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 GraphSONWriter#writeGraph(OutputStream, Graph)}.
 * @param graphToWriteTo the graph to write to when reading from the stream.
 */
@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 boolean supportsTx = graphToWriteTo.features().graph().supportsTransactions();
    final Graph.Features.EdgeFeatures edgeFeatures = graphToWriteTo.features().edge();

    readVertexStrings(inputStream).<Vertex>map(FunctionUtils.wrapFunction(line -> readVertex(new ByteArrayInputStream(line.getBytes()), null, null, Direction.IN))).forEach(vertex -> {
        final Attachable<Vertex> attachable = (Attachable<Vertex>) vertex;
        cache.put((StarGraph.StarVertex) attachable.get(), attachable.attach(Attachable.Method.create(graphToWriteTo)));
        if (supportsTx && counter.incrementAndGet() % batchSize == 0)
            graphToWriteTo.tx().commit();
    });
    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 6
Source File: VertexWritable.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public void set(final Vertex vertex) {
    this.vertex = vertex instanceof StarGraph.StarVertex ?
            (StarGraph.StarVertex) vertex :
            StarGraph.of(vertex).getStarVertex();
}
 
Example 7
Source File: VertexWritable.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public StarGraph.StarVertex get() {
    return this.vertex;
}