org.apache.tinkerpop.gremlin.process.traversal.step.PathProcessor Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.PathProcessor. 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: TraversalSelectStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
protected Traverser.Admin<E> processNextStart() {
    final Traverser.Admin<S> traverser = this.starts.next();
    final Iterator<E> keyIterator = TraversalUtil.applyAll(traverser, this.keyTraversal);
    if (keyIterator.hasNext()) {
        final E key = keyIterator.next();
        try {
            final E end = getScopeValue(pop, key, traverser);
            final Traverser.Admin<E> outTraverser = traverser.split(null == end ? null : TraversalUtil.applyNullable(end, this.selectTraversal), this);
            if (!(this.getTraversal().getParent() instanceof MatchStep)) {
                PathProcessor.processTraverserPathLabels(outTraverser, this.keepLabels);
            }
            return outTraverser;
        } catch (KeyNotFoundException nfe) {
            return EmptyTraverser.instance();
        }
    } else {
        return EmptyTraverser.instance();
    }
}
 
Example #2
Source File: SelectStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
protected Traverser.Admin<Map<String, E>> processNextStart() throws NoSuchElementException {
    final Traverser.Admin<S> traverser = this.starts.next();
    final Map<String, E> bindings = new LinkedHashMap<>(this.selectKeys.size(), 1.0f);
    try {
        for (final String selectKey : this.selectKeys) {
            final E end = this.getScopeValue(this.pop, selectKey, traverser);
            bindings.put(selectKey, TraversalUtil.applyNullable(end, this.traversalRing.next()));
        }
    } catch (KeyNotFoundException nfe) {
        return EmptyTraverser.instance();
    } finally {
        this.traversalRing.reset();
    }

    return PathProcessor.processTraverserPathLabels(traverser.split(bindings, this), this.keepLabels);
}
 
Example #3
Source File: PathRetractionStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private List<Object> getKeepLabels(final Traversal.Admin<?, ?> traversal) {
    List<Object> keepLabels = new ArrayList<>();
    for (Step step : traversal.getSteps()) {
        if (step instanceof PathProcessor) {
            final Set<String> keepers = ((PathProcessor) step).getKeepLabels();
            if (keepers != null)
                keepLabels.add(keepers);
        }
        if (step instanceof TraversalParent) {
            final TraversalParent parent = (TraversalParent) step;
            final List<Traversal.Admin<?, ?>> children = new ArrayList<>();
            children.addAll(parent.getGlobalChildren());
            children.addAll(parent.getLocalChildren());
            for (final Traversal.Admin<?, ?> child : children) {
                final List<Object> childLabels = getKeepLabels(child);
                if (childLabels.size() > 0) {
                    keepLabels.add(childLabels);
                }
            }
        }
    }
    return keepLabels;
}
 
Example #4
Source File: MathStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public ElementRequirement getMaxRequirement() {
    // this is a trick i saw in DedupGlobalStep that allows ComputerVerificationStrategy to be happy for OLAP.
    // it's a bit more of a hack here. in DedupGlobalStep, the dedup operation really only just needs the ID, but
    // here the true max requirement is PROPERTIES, but because of how map() works in this implementation in
    // relation to CURRENT, if we don't access the path labels, then we really only just operate on the stargraph
    // and are thus OLAP safe. In tracing around the code a bit, I don't see a problem with taking this approach,
    // but I suppose a better way might be make it more clear when this step is dealing with an actual path and
    // when it is not and/or adjust ComputerVerificationStrategy to cope with the situation where math() is only
    // dealing with the local stargraph.
    return (this.expression.getVariables().contains(CURRENT) && this.expression.getVariables().size() == 1) ?
            ElementRequirement.ID : PathProcessor.super.getMaxRequirement();
}
 
Example #5
Source File: MatchStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private <S> Traverser.Admin<S> retractUnnecessaryLabels(final Traverser.Admin<S> traverser) {
    if (null == this.parent.getKeepLabels())
        return traverser;

    final Set<String> keepers = new HashSet<>(this.parent.getKeepLabels());
    final Set<String> tags = traverser.getTags();
    for (final Traversal.Admin<?, ?> matchTraversal : this.parent.matchTraversals) { // get remaining traversal patterns for the traverser
        final String startStepId = matchTraversal.getStartStep().getId();
        if (!tags.contains(startStepId)) {
            keepers.addAll(this.parent.getReferencedLabelsMap().get(startStepId)); // get the reference labels required for those remaining traversals
        }
    }
    return PathProcessor.processTraverserPathLabels(traverser, keepers); // remove all reference labels that are no longer required
}
 
Example #6
Source File: SelectOneStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
protected Traverser.Admin<E> processNextStart() throws NoSuchElementException {
    final Traverser.Admin<S> traverser = this.starts.next();

    try {
        final S o = getScopeValue(pop, selectKey, traverser);
        if (null == o) return traverser.split(null, this);
        final Traverser.Admin<E> outTraverser = traverser.split(TraversalUtil.applyNullable(o, this.selectTraversal), this);
        if (!(this.getTraversal().getParent() instanceof MatchStep))
            PathProcessor.processTraverserPathLabels(outTraverser, this.keepLabels);
        return outTraverser;
    } catch (KeyNotFoundException nfe) {
        return EmptyTraverser.instance();
    }
}
 
Example #7
Source File: ComputerVerificationStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!TraversalHelper.onGraphComputer(traversal))
        return;

    if (traversal.getParent() instanceof TraversalVertexProgramStep) {
        if (TraversalHelper.getStepsOfAssignableClassRecursively(GraphStep.class, traversal).size() > 1)
            throw new VerificationException("Mid-traversal V()/E() is currently not supported on GraphComputer", traversal);
        if (TraversalHelper.hasStepOfAssignableClassRecursively(ProfileStep.class, traversal) && TraversalHelper.getStepsOfAssignableClass(VertexProgramStep.class, TraversalHelper.getRootTraversal(traversal)).size() > 1)
            throw new VerificationException("Profiling a multi-VertexProgramStep traversal is currently not supported on GraphComputer", traversal);
    }

    // this is a problem because sideEffect.merge() is transient on the OLAP reduction
    if (TraversalHelper.getRootTraversal(traversal).getTraverserRequirements().contains(TraverserRequirement.ONE_BULK))
        throw new VerificationException("One bulk is currently not supported on GraphComputer: " + traversal, traversal);

    // you can not traverse past the local star graph with localChildren (e.g. by()-modulators).
    if (!TraversalHelper.isGlobalChild(traversal) && !TraversalHelper.isLocalStarGraph(traversal))
        throw new VerificationException("Local traversals may not traverse past the local star-graph on GraphComputer: " + traversal, traversal);

    for (final Step<?, ?> step : traversal.getSteps()) {
        if (step instanceof PathProcessor && ((PathProcessor) step).getMaxRequirement() != PathProcessor.ElementRequirement.ID)
            throw new VerificationException("It is not possible to access more than a path element's id on GraphComputer: " + step + " requires " + ((PathProcessor) step).getMaxRequirement(), traversal);

        if (UNSUPPORTED_STEPS.stream().filter(c -> c.isAssignableFrom(step.getClass())).findFirst().isPresent())
            throw new VerificationException("The following step is currently not supported on GraphComputer: " + step, traversal);
    }

    Step<?, ?> nextParentStep = traversal.getParent().asStep();
    while (!(nextParentStep instanceof EmptyStep)) {
        if (nextParentStep instanceof PathProcessor && ((PathProcessor) nextParentStep).getMaxRequirement() != PathProcessor.ElementRequirement.ID)
            throw new VerificationException("The following path processor step requires more than the element id on GraphComputer: " + nextParentStep + " requires " + ((PathProcessor) nextParentStep).getMaxRequirement(), traversal);
        nextParentStep = nextParentStep.getNextStep();
    }
}
 
Example #8
Source File: PathRetractionStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private void addLabels(final PathProcessor s, final Set<String> keepLabels) {
    final Set<String> labelsCopy = new HashSet<>(keepLabels);
    if (null == s.getKeepLabels())
        s.setKeepLabels(labelsCopy);
    else
        s.getKeepLabels().addAll(labelsCopy);
}
 
Example #9
Source File: DedupGlobalStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
protected Traverser.Admin<S> processNextStart() {
    if (null != this.barrier) {
        this.barrierIterator = this.barrier.entrySet().iterator();
        this.barrier = null;
    }
    while (this.barrierIterator != null && this.barrierIterator.hasNext()) {
        if (null == this.barrierIterator)
            this.barrierIterator = this.barrier.entrySet().iterator();
        final Map.Entry<Object, Traverser.Admin<S>> entry = this.barrierIterator.next();
        if (this.duplicateSet.add(entry.getKey()))
            return PathProcessor.processTraverserPathLabels(entry.getValue(), this.keepLabels);
    }
    return PathProcessor.processTraverserPathLabels(super.processNextStart(), this.keepLabels);
}
 
Example #10
Source File: PixyCoalesceStep.java    From pixy with Apache License 2.0 4 votes vote down vote up
@Override
protected Traverser.Admin processNextStart() {
    return PathProcessor.processTraverserPathLabels(super.processNextStart(), this.keepLabels);
}
 
Example #11
Source File: MathStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected Traverser.Admin<Double> processNextStart() {
    return PathProcessor.processTraverserPathLabels(super.processNextStart(), this.keepLabels);
}
 
Example #12
Source File: SqlgWhereTraversalStepBarrier.java    From sqlg with MIT License 4 votes vote down vote up
@Override
public ElementRequirement getMaxRequirement() {
    return TraversalHelper.getVariableLocations(this.whereTraversal).contains(Scoping.Variable.START) ?
            PathProcessor.super.getMaxRequirement() :
            ElementRequirement.ID;
}
 
Example #13
Source File: PathRetractionStrategy.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
private void addLabels(final Traversal.Admin traversal, final Set<String> keepLabels) {
    for (final Object s : traversal.getSteps()) {
        if (s instanceof PathProcessor)
            addLabels((PathProcessor) s, keepLabels);
    }
}
 
Example #14
Source File: LazyBarrierStrategy.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    // drop() is a problem for bulked edge/meta properties because of Property equality changes in TINKERPOP-2318
    // which made it so that a Property is equal if the key/value is equal. as a result, they bulk together which
    // is fine for almost all cases except when you wish to drop the property.
    if (TraversalHelper.onGraphComputer(traversal) ||
            traversal.getTraverserRequirements().contains(TraverserRequirement.PATH) ||
            TraversalHelper.hasStepOfAssignableClass(DropStep.class, traversal))
        return;

    boolean foundFlatMap = false;
    boolean labeledPath = false;
    for (int i = 0; i < traversal.getSteps().size(); i++) {
        final Step<?, ?> step = traversal.getSteps().get(i);

        if (step instanceof PathProcessor) {
            final Set<String> keepLabels = ((PathProcessor) step).getKeepLabels();
            if (null != keepLabels && keepLabels.isEmpty()) // if no more path data, then start barrier'ing again
                labeledPath = false;
        }
        if (step instanceof FlatMapStep &&
                !(step instanceof VertexStep && ((VertexStep) step).returnsEdge()) ||
                (step instanceof GraphStep &&
                        (i > 0 || ((GraphStep) step).getIds().length >= BIG_START_SIZE ||
                                (((GraphStep) step).getIds().length == 0 && !(step.getNextStep() instanceof HasStep))))) {

            // NoneStep, EmptyStep signify the end of the traversal where no barriers are really going to be
            // helpful after that. ProfileSideEffectStep means the traversal had profile() called on it and if
            // we don't account for that a barrier will inject at the end of the traversal where it wouldn't
            // be otherwise. LazyBarrierStrategy executes before the finalization strategy of ProfileStrategy
            // so additionally injected ProfileSideEffectStep instances should not have effect here.
            if (foundFlatMap && !labeledPath &&
                    !(step.getNextStep() instanceof Barrier) &&
                    !(step.getNextStep() instanceof NoneStep) &&
                    !(step.getNextStep() instanceof EmptyStep) &&
                    !(step.getNextStep() instanceof ProfileSideEffectStep)) {
                final Step noOpBarrierStep = new NoOpBarrierStep<>(traversal, MAX_BARRIER_SIZE);
                TraversalHelper.copyLabels(step, noOpBarrierStep, true);
                TraversalHelper.insertAfterStep(noOpBarrierStep, step, traversal);
            } else
                foundFlatMap = true;
        }
        if (!step.getLabels().isEmpty())
            labeledPath = true;

    }
}
 
Example #15
Source File: DedupGlobalStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public ElementRequirement getMaxRequirement() {
    return null == this.dedupLabels ? ElementRequirement.ID : PathProcessor.super.getMaxRequirement();
}
 
Example #16
Source File: WhereTraversalStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected Traverser.Admin<S> processNextStart() {
    return PathProcessor.processTraverserPathLabels(super.processNextStart(), this.keepLabels);
}
 
Example #17
Source File: WhereTraversalStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public ElementRequirement getMaxRequirement() {
    return TraversalHelper.getVariableLocations(this.whereTraversal).contains(Scoping.Variable.START) ?
            PathProcessor.super.getMaxRequirement() :
            ElementRequirement.ID;
}
 
Example #18
Source File: PathFilterStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected Traverser.Admin<S> processNextStart() {
    return PathProcessor.processTraverserPathLabels(super.processNextStart(), this.keepLabels);
}
 
Example #19
Source File: WherePredicateStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected Traverser.Admin<S> processNextStart() {
    return PathProcessor.processTraverserPathLabels(super.processNextStart(), this.keepLabels);
}
 
Example #20
Source File: TreeSideEffectStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected Traverser.Admin<S> processNextStart() {
    return PathProcessor.processTraverserPathLabels(super.processNextStart(), this.keepLabels);
}
 
Example #21
Source File: PathStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected Traverser.Admin<Path> processNextStart() {
    return PathProcessor.processTraverserPathLabels(super.processNextStart(), this.keepLabels);
}