Java Code Examples for org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils#filter()

The following examples show how to use org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils#filter() . 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: TinkerGraphStep.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
private Iterator<? extends Vertex> vertices() {
    final TinkerGraph graph = (TinkerGraph) this.getTraversal().getGraph().get();
    final HasContainer indexedContainer = getIndexKey(Vertex.class);
    final Optional<HasContainer> hasLabelContainer = findHasLabelStep();
    // ids are present, filter on them first
    if (null == this.ids)
        return Collections.emptyIterator();
    else if (this.ids.length > 0)
        return this.iteratorList(graph.vertices(this.ids));
    else if (graph.ondiskOverflowEnabled && hasLabelContainer.isPresent())
        return graph.verticesByLabel((P<String>) hasLabelContainer.get().getPredicate());
    else
        return null == indexedContainer ?
                this.iteratorList(graph.vertices()) :
                IteratorUtils.filter(TinkerHelper.queryVertexIndex(graph, indexedContainer.getKey(), indexedContainer.getPredicate().getValue()).iterator(),
                        vertex -> HasContainer.testAll(vertex, this.hasContainers));
}
 
Example 2
Source File: TinkerGraphStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private Iterator<? extends Vertex> vertices() {
    final TinkerGraph graph = (TinkerGraph) this.getTraversal().getGraph().get();
    final HasContainer indexedContainer = getIndexKey(Vertex.class);
    Iterator<? extends Vertex> iterator;
    // ids are present, filter on them first
    if (null == this.ids)
        iterator = Collections.emptyIterator();
    else if (this.ids.length > 0)
        iterator = this.iteratorList(graph.vertices(this.ids));
    else
        iterator = (null == indexedContainer ?
                this.iteratorList(graph.vertices()) :
                IteratorUtils.filter(TinkerHelper.queryVertexIndex(graph, indexedContainer.getKey(), indexedContainer.getPredicate().getValue()).iterator(),
                                     vertex -> HasContainer.testAll(vertex, this.hasContainers)));

    iterators.add(iterator);

    return iterator;
}
 
Example 3
Source File: TinkerVertex.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<Edge> edges(final Direction direction, final String... edgeLabels) {
    final Iterator<Edge> edgeIterator = (Iterator) TinkerHelper.getEdges(this, direction, edgeLabels);
    return TinkerHelper.inComputerMode(this.graph) ?
            IteratorUtils.filter(edgeIterator, edge -> this.graph.graphComputerView.legalEdge(this, edge)) :
            edgeIterator;
}
 
Example 4
Source File: TinkerGraphStep.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
private HasContainer getIndexKey(final Class<? extends Element> indexedClass) {
    final Set<String> indexedKeys = ((TinkerGraph) this.getTraversal().getGraph().get()).getIndexedKeys(indexedClass);

    final Iterator<HasContainer> itty = IteratorUtils.filter(hasContainers.iterator(),
            c -> c.getPredicate().getBiPredicate() == Compare.eq && indexedKeys.contains(c.getKey()));
    return itty.hasNext() ? itty.next() : null;
}
 
Example 5
Source File: ClusterMemberMapReduce.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Map<Serializable, Set<ConceptId>> generateFinalResult(Iterator<KeyValue<Serializable, Set<ConceptId>>> keyValues) {
    if (this.persistentProperties.containsKey(CLUSTER_SIZE)) {
        long clusterSize = (long) persistentProperties.get(CLUSTER_SIZE);
        keyValues = IteratorUtils.filter(keyValues, pair -> Long.valueOf(pair.getValue().size()).equals(clusterSize));
    }
    final Map<Serializable, Set<ConceptId>> clusterPopulation = Utility.keyValuesToMap(keyValues);
    clusterPopulation.remove(NullObject.instance());
    return clusterPopulation;
}
 
Example 6
Source File: ClusterSizeMapReduce.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Map<Serializable, Long> generateFinalResult(Iterator<KeyValue<Serializable, Long>> keyValues) {
    if (this.persistentProperties.containsKey(CLUSTER_SIZE)) {
        long clusterSize = (long) persistentProperties.get(CLUSTER_SIZE);
        keyValues = IteratorUtils.filter(keyValues, pair -> pair.getValue().equals(clusterSize));
    }
    final Map<Serializable, Long> clusterPopulation = Utility.keyValuesToMap(keyValues);
    clusterPopulation.remove(NullObject.instance());
    return clusterPopulation;
}
 
Example 7
Source File: HBaseEdge.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Override
public <V> Iterator<Property<V>> properties(final String... propertyKeys) {
    Iterable<String> keys = getPropertyKeys();
    Iterator<String> filter = IteratorUtils.filter(keys.iterator(),
            key -> ElementHelper.keyExists(key, propertyKeys));
    return IteratorUtils.map(filter,
            key -> new HBaseProperty<>(graph, this, key, getProperty(key)));
}
 
Example 8
Source File: HBaseVertex.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Override
public <V> Iterator<VertexProperty<V>> properties(final String... propertyKeys) {
    Iterable<String> keys = getPropertyKeys();
    Iterator<String> filter = IteratorUtils.filter(keys.iterator(),
            key -> ElementHelper.keyExists(key, propertyKeys));
    return IteratorUtils.map(filter,
            key -> new HBaseVertexProperty<>(graph, this, key, getProperty(key)));
}
 
Example 9
Source File: HadoopGraph.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<Vertex> vertices(final Object... vertexIds) {
    try {
        if (0 == vertexIds.length) {
            return new HadoopVertexIterator(this);
        } else {
            // base the conversion function on the first item in the id list as the expectation is that these
            // id values will be a uniform list
            if (vertexIds[0] instanceof Vertex) {
                // based on the first item assume all vertices in the argument list
                if (!Stream.of(vertexIds).allMatch(id -> id instanceof Vertex))
                    throw Graph.Exceptions.idArgsMustBeEitherIdOrElement();

                // no need to get the vertices again, so just flip it back - some implementation may want to treat this
                // as a refresh operation. that's not necessary for hadoopgraph.
                return Stream.of(vertexIds).map(id -> (Vertex) id).iterator();
            } else {
                final Class<?> firstClass = vertexIds[0].getClass();
                if (!Stream.of(vertexIds).map(Object::getClass).allMatch(firstClass::equals))
                    throw Graph.Exceptions.idArgsMustBeEitherIdOrElement();     // todo: change exception to be ids of the same type
                return IteratorUtils.filter(new HadoopVertexIterator(this), vertex -> ElementHelper.idExists(vertex.id(), vertexIds));
            }
        }
    } catch (final IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example 10
Source File: HadoopGraph.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<Edge> edges(final Object... edgeIds) {
    try {
        if (0 == edgeIds.length) {
            return new HadoopEdgeIterator(this);
        } else {
            // base the conversion function on the first item in the id list as the expectation is that these
            // id values will be a uniform list
            if (edgeIds[0] instanceof Edge) {
                // based on the first item assume all Edges in the argument list
                if (!Stream.of(edgeIds).allMatch(id -> id instanceof Edge))
                    throw Graph.Exceptions.idArgsMustBeEitherIdOrElement();

                // no need to get the vertices again, so just flip it back - some implementation may want to treat this
                // as a refresh operation. that's not necessary for hadoopgraph.
                return Stream.of(edgeIds).map(id -> (Edge) id).iterator();
            } else {
                final Class<?> firstClass = edgeIds[0].getClass();
                if (!Stream.of(edgeIds).map(Object::getClass).allMatch(firstClass::equals))
                    throw Graph.Exceptions.idArgsMustBeEitherIdOrElement();     // todo: change exception to be ids of the same type
                return IteratorUtils.filter(new HadoopEdgeIterator(this), vertex -> ElementHelper.idExists(vertex.id(), edgeIds));
            }
        }
    } catch (final IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example 11
Source File: TinkerVertex.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<Edge> edges(final Direction direction, final String... edgeLabels) {
    final Iterator<Edge> edgeIterator = (Iterator) TinkerHelper.getEdges(this, direction, edgeLabels);
    return TinkerHelper.inComputerMode(this.graph) ?
            IteratorUtils.filter(edgeIterator, edge -> this.graph.graphComputerView.legalEdge(this, edge)) :
            edgeIterator;
}
 
Example 12
Source File: TinkerGraphStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private HasContainer getIndexKey(final Class<? extends Element> indexedClass) {
    final Set<String> indexedKeys = ((TinkerGraph) this.getTraversal().getGraph().get()).getIndexedKeys(indexedClass);

    final Iterator<HasContainer> itty = IteratorUtils.filter(hasContainers.iterator(),
            c -> c.getPredicate().getBiPredicate() == Compare.eq && indexedKeys.contains(c.getKey()));
    return itty.hasNext() ? itty.next() : null;

}
 
Example 13
Source File: Neo4jEdge.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public <V> Iterator<Property<V>> properties(final String... propertyKeys) {
    this.graph.tx().readWrite();
    Iterable<String> keys = this.baseElement.getKeys();
    Iterator<String> filter = IteratorUtils.filter(keys.iterator(),
            key -> ElementHelper.keyExists(key, propertyKeys));
    return IteratorUtils.map(filter,
            key -> new Neo4jProperty<>(this, key, (V) this.baseElement.getProperty(key)));
}
 
Example 14
Source File: HBaseVertex.java    From hgraphdb with Apache License 2.0 4 votes vote down vote up
public Iterator<Edge> getEdgesFromCache(Tuple cacheKey) {
    if (edgeCache == null || !isCached()) return null;
    List<Edge> edges = edgeCache.getIfPresent(cacheKey);
    return edges != null ? IteratorUtils.filter(edges.iterator(), edge -> !((HBaseEdge) edge).isDeleted()) : null;
}
 
Example 15
Source File: ComputerGraph.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public <V> Iterator<? extends Property<V>> properties(final String... propertyKeys) {
    return (Iterator) IteratorUtils.filter(this.element.properties(propertyKeys), property -> !computeKeys.contains(property.key()));
}
 
Example 16
Source File: Neo4jGraphStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
private Iterator<? extends Edge> edges() {
    if (null == this.ids)
        return Collections.emptyIterator();
    return IteratorUtils.filter(this.getTraversal().getGraph().get().edges(this.ids), edge -> HasContainer.testAll(edge, this.hasContainers));
}