All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tinkerpop.gremlin.tinkergraph.process.graph.step.sideEffect.TinkerGraphStep Maven / Gradle / Ivy

There is a newer version: 3.0.0.M7
Show newest version
package com.tinkerpop.gremlin.tinkergraph.process.graph.step.sideEffect;

import com.tinkerpop.gremlin.process.Traversal;
import com.tinkerpop.gremlin.process.graph.step.sideEffect.GraphStep;
import com.tinkerpop.gremlin.process.util.TraversalHelper;
import com.tinkerpop.gremlin.process.util.TraversalMetrics;
import com.tinkerpop.gremlin.structure.Compare;
import com.tinkerpop.gremlin.structure.Edge;
import com.tinkerpop.gremlin.structure.Element;
import com.tinkerpop.gremlin.structure.Vertex;
import com.tinkerpop.gremlin.structure.util.HasContainer;
import com.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import com.tinkerpop.gremlin.tinkergraph.structure.TinkerHelper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class TinkerGraphStep extends GraphStep {

    public final List hasContainers = new ArrayList<>();

    public TinkerGraphStep(final Traversal traversal, final Class returnClass) {
        super(traversal, returnClass);
    }

    @Override
    public void generateTraverserIterator(final boolean trackPaths) {
        if (PROFILING_ENABLED) TraversalMetrics.start(this);
        this.start = Vertex.class.isAssignableFrom(this.returnClass) ? this.vertices() : this.edges();
        super.generateTraverserIterator(trackPaths);
        if (PROFILING_ENABLED) TraversalMetrics.stop(this);
    }

    private Iterator edges() {
        final HasContainer indexedContainer = getIndexKey(Edge.class);
        final Stream edgeStream = (null == indexedContainer) ?
                TinkerHelper.getEdges((TinkerGraph)this.traversal.sideEffects().getGraph()).stream() :
                TinkerHelper.queryEdgeIndex((TinkerGraph)this.traversal.sideEffects().getGraph(), indexedContainer.key, indexedContainer.value).stream();

        // the copy to a new List is intentional as remove() operations will cause ConcurrentModificationException otherwise
        return edgeStream.filter(e -> HasContainer.testAll(e, hasContainers)).collect(Collectors.toList()).iterator();
    }

    private Iterator vertices() {
        final HasContainer indexedContainer = getIndexKey(Vertex.class);
        final Stream vertexStream = (null == indexedContainer) ?
                TinkerHelper.getVertices((TinkerGraph)this.traversal.sideEffects().getGraph()).stream() :
                TinkerHelper.queryVertexIndex((TinkerGraph)this.traversal.sideEffects().getGraph(), indexedContainer.key, indexedContainer.value).stream();

        // the copy to a new List is intentional as remove() operations will cause ConcurrentModificationException otherwise
        return vertexStream.filter(v -> HasContainer.testAll(v, this.hasContainers)).collect(Collectors.toList()).iterator();
    }

    private HasContainer getIndexKey(final Class indexedClass) {
        final Set indexedKeys = ((TinkerGraph)this.traversal.sideEffects().getGraph()).getIndexedKeys(indexedClass);
        return this.hasContainers.stream()
                .filter(c -> indexedKeys.contains(c.key) && c.predicate.equals(Compare.eq))
                .findAny()
                .orElseGet(() -> null);
    }

    public String toString() {
        return this.hasContainers.isEmpty() ? super.toString() : TraversalHelper.makeStepString(this, this.hasContainers);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy