org.apache.tinkerpop.gremlin.process.traversal.step.map.CountGlobalStep Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.map.CountGlobalStep. 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: AdjacentToIncidentStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    final List<Step> steps = traversal.getSteps();
    final int size = steps.size() - 1;
    Step prev = null;
    for (int i = 0; i <= size; i++) {
        final Step curr = steps.get(i);
        if (i == size && isOptimizable(curr)) {
            final TraversalParent parent = curr.getTraversal().getParent();
            if (parent instanceof NotStep || parent instanceof TraversalFilterStep || parent instanceof WhereTraversalStep || parent instanceof ConnectiveStep) {
                optimizeStep(traversal, curr);
            }
        } else if (isOptimizable(prev)) {
            if (curr instanceof CountGlobalStep) {
                optimizeStep(traversal, prev);
            }
        }
        if (!(curr instanceof RangeGlobalStep)) {
            prev = curr;
        }
    }
}
 
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: SparkStarBarrierInterceptor.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public static boolean isLegal(final Traversal.Admin<?, ?> traversal) {
    final Step<?, ?> startStep = traversal.getStartStep();
    final Step<?, ?> endStep = traversal.getEndStep();
    // right now this is not supported because of how the SparkStarBarrierInterceptor mutates the traversal prior to local evaluation
    if (traversal.getStrategies().getStrategy(SubgraphStrategy.class).isPresent())
        return false;
    if (!startStep.getClass().equals(GraphStep.class) || ((GraphStep) startStep).returnsEdge())
        return false;
    if (!endStep.getClass().equals(CountGlobalStep.class) &&
            !endStep.getClass().equals(SumGlobalStep.class) &&
            !endStep.getClass().equals(MeanGlobalStep.class) &&
            !endStep.getClass().equals(MaxGlobalStep.class) &&
            !endStep.getClass().equals(MinGlobalStep.class) &&
            !endStep.getClass().equals(FoldStep.class) &&
            !endStep.getClass().equals(GroupStep.class) &&
            !endStep.getClass().equals(GroupCountStep.class))
        // TODO: tree()
        return false;
    if (TraversalHelper.getStepsOfAssignableClassRecursively(Scope.global, Barrier.class, traversal).size() != 1)
        return false;
    if (traversal.getTraverserRequirements().contains(TraverserRequirement.SACK))
        return false;
    return TraversalHelper.isLocalStarGraph(traversal);
}
 
Example #5
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public static void extractCount(Step<?, ?> newStep,
                                Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof CountGlobalStep) {
            QueryHolder holder = (QueryHolder) newStep;
            holder.setCount();
        }
    } while (step instanceof CountGlobalStep ||
             step instanceof FilterStep ||
             step instanceof IdentityStep ||
             step instanceof NoOpBarrierStep);
}
 
Example #6
Source File: CountStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private boolean doStrategy(final Step step) {
    if (!(step instanceof CountGlobalStep) ||
            !(step.getNextStep() instanceof IsStep) ||
            step.getPreviousStep() instanceof RangeGlobalStep) // if a RangeStep was provided, assume that the user knows what he's doing
        return false;

    final Step parent = step.getTraversal().getParent().asStep();
    return (parent instanceof FilterStep || parent.getLabels().isEmpty()) && // if the parent is labeled, then the count matters
            !(parent.getNextStep() instanceof MatchStep.MatchEndStep && // if this is in a pattern match, then don't do it.
                    ((MatchStep.MatchEndStep) parent.getNextStep()).getMatchKey().isPresent());
}
 
Example #7
Source File: MessagePassingReductionStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static boolean insertElementId(final Step<?, ?> barrier) {
    if (!(barrier instanceof Barrier))
        return false;
    else if (!endsWithElement(barrier.getPreviousStep()))
        return false;
    else if (barrier instanceof CountGlobalStep)
        return true;
    else if (barrier instanceof DedupGlobalStep &&
            ((DedupGlobalStep) barrier).getScopeKeys().isEmpty() &&
            ((DedupGlobalStep) barrier).getLocalChildren().isEmpty() &&
            barrier.getNextStep() instanceof CountGlobalStep)
        return true;
    else
        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);
}
 
Example #9
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
public static void extractAggregateFunc(Step<?, ?> newStep,
                                        Traversal.Admin<?, ?> traversal) {
    PropertiesStep<?> propertiesStep = null;
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof PropertiesStep) {
            @SuppressWarnings("resource")
            PropertiesStep<?> propStep = (PropertiesStep<?>) step;
            if (propStep.getReturnType() == PropertyType.VALUE &&
                propStep.getPropertyKeys().length == 1) {
                propertiesStep = propStep;
            }
        } else if (propertiesStep != null &&
                   step instanceof ReducingBarrierStep) {
            AggregateFunc aggregateFunc;
            if (step instanceof CountGlobalStep) {
                aggregateFunc = AggregateFunc.COUNT;
            } else if (step instanceof MaxGlobalStep) {
                aggregateFunc = AggregateFunc.MAX;
            } else if (step instanceof MinGlobalStep) {
                aggregateFunc = AggregateFunc.MIN;
            } else if (step instanceof MeanGlobalStep) {
                aggregateFunc = AggregateFunc.AVG;
            } else if (step instanceof SumGlobalStep) {
                aggregateFunc = AggregateFunc.SUM;
            } else {
                aggregateFunc = null;
            }

            if (aggregateFunc != null) {
                QueryHolder holder = (QueryHolder) newStep;
                holder.setAggregate(aggregateFunc,
                                    propertiesStep.getPropertyKeys()[0]);
                traversal.removeStep(step);
                traversal.removeStep(propertiesStep);
            }
        }
    } while (step instanceof FilterStep ||
             step instanceof PropertiesStep ||
             step instanceof IdentityStep ||
             step instanceof NoOpBarrierStep);
}
 
Example #10
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Map the traversal stream to its reduction as a sum of the {@link Traverser#bulk} values (i.e. count the number
 * of traversers up to this point).
 *
 * @return the traversal with an appended {@link CountGlobalStep}.
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#count-step" target="_blank">Reference Documentation - Count Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, Long> count() {
    this.asAdmin().getBytecode().addStep(Symbols.count);
    return this.asAdmin().addStep(new CountGlobalStep<>(this.asAdmin()));
}
 
Example #11
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Map the traversal stream to its reduction as a sum of the {@link Traverser#bulk} values given the specified
 * {@link Scope} (i.e. count the number of traversers up to this point).
 *
 * @return the traversal with an appended {@link CountGlobalStep} or {@link CountLocalStep} depending on the {@link Scope}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#count-step" target="_blank">Reference Documentation - Count Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, Long> count(final Scope scope) {
    this.asAdmin().getBytecode().addStep(Symbols.count, scope);
    return this.asAdmin().addStep(scope.equals(Scope.global) ? new CountGlobalStep<>(this.asAdmin()) : new CountLocalStep<>(this.asAdmin()));
}
 
Example #12
Source File: AdjacentToIncidentStrategy.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Checks whether a given step is optimizable or not.
 *
 * @param step the step to check
 * @return <code>true</code> if the step is optimizable, otherwise <code>false</code>
 */
private static boolean isOptimizable(final Step step) {
    return ((step instanceof VertexStep && ((VertexStep) step).returnsVertex()) ||
            (step instanceof PropertiesStep && PropertyType.VALUE.equals(((PropertiesStep) step).getReturnType()))) && (step.getTraversal().getEndStep().getLabels().isEmpty() || step.getNextStep() instanceof CountGlobalStep);
}