org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SideEffectStep Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SideEffectStep. 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: TinkerGraphCountStrategy.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!(traversal.getParent() instanceof EmptyStep) || TraversalHelper.onGraphComputer(traversal))
        return;
    final List<Step> steps = traversal.getSteps();
    if (steps.size() < 2 ||
            !(steps.get(0) instanceof GraphStep) ||
            0 != ((GraphStep) steps.get(0)).getIds().length ||
            !(steps.get(steps.size() - 1) instanceof CountGlobalStep))
        return;
    for (int i = 1; i < steps.size() - 1; i++) {
        final Step current = steps.get(i);
        if (!(//current instanceof MapStep ||  // MapSteps will not necessarily emit an element as demonstrated in https://issues.apache.org/jira/browse/TINKERPOP-1958
                current instanceof IdentityStep ||
                current instanceof NoOpBarrierStep ||
                current instanceof CollectingBarrierStep) ||
                (current instanceof TraversalParent &&
                        TraversalHelper.anyStepRecursively(s -> (s instanceof SideEffectStep || s instanceof AggregateStep), (TraversalParent) current)))
            return;
    }
    final Class<? extends Element> elementClass = ((GraphStep<?, ?>) steps.get(0)).getReturnClass();
    TraversalHelper.removeAllSteps(traversal);
    traversal.addStep(new TinkerCountGlobalStep<>(traversal, elementClass));
}
 
Example #2
Source File: SubgraphStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static final char processesPropertyType(Step step) {
    while (!(step instanceof EmptyStep)) {
        if (step instanceof FilterStep || step instanceof SideEffectStep)
            step = step.getPreviousStep();
        else if (step instanceof GraphStep && ((GraphStep) step).returnsVertex())
            return 'v';
        else if (step instanceof EdgeVertexStep)
            return 'v';
        else if (step instanceof VertexStep)
            return ((VertexStep) step).returnsVertex() ? 'v' : 'p';
        else if (step instanceof PropertyMapStep || step instanceof PropertiesStep)
            return 'p';
        else
            return 'x';
    }
    return 'x';
}
 
Example #3
Source File: TinkerGraphCountStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!(traversal.isRoot()) || TraversalHelper.onGraphComputer(traversal))
        return;
    final List<Step> steps = traversal.getSteps();
    if (steps.size() < 2 ||
            !(steps.get(0) instanceof GraphStep) ||
            0 != ((GraphStep) steps.get(0)).getIds().length ||
            !(steps.get(steps.size() - 1) instanceof CountGlobalStep))
        return;
    for (int i = 1; i < steps.size() - 1; i++) {
        final Step current = steps.get(i);
        if (!(//current instanceof MapStep ||  // MapSteps will not necessarily emit an element as demonstrated in https://issues.apache.org/jira/browse/TINKERPOP-1958
                current instanceof IdentityStep ||
                current instanceof NoOpBarrierStep ||
                current instanceof CollectingBarrierStep) ||
                (current instanceof TraversalParent &&
                        TraversalHelper.anyStepRecursively(s -> (s instanceof SideEffectStep || s instanceof AggregateGlobalStep), (TraversalParent) current)))
            return;
    }
    final Class<? extends Element> elementClass = ((GraphStep<?, ?>) steps.get(0)).getReturnClass();
    TraversalHelper.removeAllSteps(traversal);
    traversal.addStep(new TinkerCountGlobalStep<>(traversal, elementClass));
}
 
Example #4
Source File: JanusGraphVertexStep.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Many parent traversals drip feed their start vertices in one at a time. To best exploit
 * the multiQuery we need to load all possible starts in one go so this method will attempt
 * to find a JanusGraphMultiQueryStep with the starts of the parent, and if found cache it.
 */
private void setParentMultiQueryStep() {
    Step firstStep = traversal.getStartStep();
    while (firstStep instanceof StartStep || firstStep instanceof SideEffectStep) {
        // Want the next step if this is a side effect
        firstStep = firstStep.getNextStep();
    }
    if (this.equals(firstStep)) {
        Step<?, ?> parentStep = traversal.getParent().asStep();
        if (JanusGraphTraversalUtil.isMultiQueryCompatibleStep(parentStep)) {
            Step<?, ?> parentPreviousStep = parentStep.getPreviousStep();
            if (parentStep instanceof RepeatStep) {
                RepeatStep repeatStep = (RepeatStep) parentStep;
                List<RepeatEndStep> repeatEndSteps = TraversalHelper.getStepsOfClass(RepeatEndStep.class, repeatStep.getRepeatTraversal());
                if (repeatEndSteps.size() == 1) {
                    parentPreviousStep = repeatEndSteps.get(0).getPreviousStep();
                }
            }
            if (parentPreviousStep instanceof ProfileStep) {
                parentPreviousStep = parentPreviousStep.getPreviousStep();
            }
            if (parentPreviousStep instanceof JanusGraphMultiQueryStep) {
                parentMultiQueryStep = (JanusGraphMultiQueryStep) parentPreviousStep;
            }
        }
    }
}
 
Example #5
Source File: JanusGraphTraversalUtil.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void getMultiQueryCompatibleStepsFromChildTraversal(Traversal.Admin<?, ?> childTraversal, Step parentStep, Set<Step> multiQueryCompatibleSteps) {
    Step firstStep = childTraversal.getStartStep();
    while (firstStep instanceof StartStep || firstStep instanceof SideEffectStep) {
        // Want the next step if this is a side effect
        firstStep = firstStep.getNextStep();
    }
    if (firstStep.getClass().isAssignableFrom(VertexStep.class)) {
        multiQueryCompatibleSteps.add(parentStep);
    }
}
 
Example #6
Source File: EarlyLimitStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {

    final List<Step> steps = traversal.getSteps();
    Step insertAfter = null;
    boolean merge = false;
    for (int i = 0, j = steps.size(); i < j; i++) {
        final Step step = steps.get(i);
        if (step instanceof RangeGlobalStep) {
            if (insertAfter != null) {
                // RangeStep was found, move it to the earliest possible step or merge it with a
                // previous RangeStep; keep the RangeStep's labels at its preceding step
                TraversalHelper.copyLabels(step, step.getPreviousStep(), true);
                insertAfter = moveRangeStep((RangeGlobalStep) step, insertAfter, traversal, merge);
                if (insertAfter instanceof NoneStep) {
                    // any step besides a SideEffectCapStep after a NoneStep would be pointless
                    final int noneStepIndex = TraversalHelper.stepIndex(insertAfter, traversal);
                    for (i = j - 2; i > noneStepIndex; i--) {
                        if (!(steps.get(i) instanceof SideEffectCapStep) && !(steps.get(i) instanceof ProfileSideEffectStep)) {
                            traversal.removeStep(i);
                        }
                    }
                    break;
                }
                j = steps.size();
            }
        } else if (!(step instanceof MapStep || step instanceof SideEffectStep)) {
            // remember the last step that can be used to move any RangeStep to
            // any RangeStep can be moved in front of all its preceding map- and sideEffect-steps
            insertAfter = step;
            merge = true;
        } else if (step instanceof SideEffectCapable) {
            // if there's any SideEffectCapable step along the way, RangeSteps cannot be merged as this could
            // change the final traversal's internal memory
            merge = false;
        }
    }
}
 
Example #7
Source File: MessagePassingReductionStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static final boolean endsWithElement(Step<?, ?> currentStep) {
    while (!(currentStep instanceof EmptyStep)) {
        if (currentStep instanceof VertexStep) // only inE, in, and out send messages
            return (((VertexStep) currentStep).returnsVertex() || !((VertexStep) currentStep).getDirection().equals(Direction.OUT));
        else if (currentStep instanceof EdgeVertexStep) // TODO: add GraphStep but only if its mid-traversal V()/E()
            return true;
        else if (currentStep instanceof TraversalFlatMapStep || currentStep instanceof TraversalMapStep || currentStep instanceof LocalStep)
            return endsWithElement(((TraversalParent) currentStep).getLocalChildren().get(0).getEndStep());
        else if (!(currentStep instanceof FilterStep || currentStep instanceof SideEffectStep || currentStep instanceof IdentityStep || currentStep instanceof Barrier))
            return false;
        currentStep = currentStep.getPreviousStep();
    }
    return false;
}
 
Example #8
Source File: HugeCountStepStrategy.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void apply(Traversal.Admin<?, ?> traversal) {
    TraversalUtil.convAllHasSteps(traversal);

    // Extract CountGlobalStep
    List<CountGlobalStep> steps = TraversalHelper.getStepsOfClass(
                                  CountGlobalStep.class, traversal);
    if (steps.isEmpty()) {
        return;
    }

    // Find HugeGraphStep before count()
    CountGlobalStep<?> originStep = steps.get(0);
    List<Step<?, ?>> originSteps = new ArrayList<>();
    HugeGraphStep<?, ? extends Element> graphStep = null;
    Step<?, ?> step = originStep;
    do {
        if (!(step instanceof CountGlobalStep ||
              step instanceof GraphStep ||
              step instanceof IdentityStep ||
              step instanceof NoOpBarrierStep ||
              step instanceof CollectingBarrierStep) ||
             (step instanceof TraversalParent &&
              TraversalHelper.anyStepRecursively(s -> {
                  return s instanceof SideEffectStep ||
                         s instanceof AggregateStep;
              }, (TraversalParent) step))) {
            return;
        }
        originSteps.add(step);
        if (step instanceof HugeGraphStep) {
            graphStep = (HugeGraphStep<?, ? extends Element>) step;
            break;
        }
        step = step.getPreviousStep();
    } while (step != null);

    if (graphStep == null) {
        return;
    }

    // Replace with HugeCountStep
    graphStep.queryInfo().aggregate(AggregateFunc.COUNT, null);
    HugeCountStep<?> countStep = new HugeCountStep<>(traversal, graphStep);
    for (Step<?, ?> origin : originSteps) {
        traversal.removeStep(origin);
    }
    traversal.addStep(0, countStep);
}