org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep. 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: HasStepFolder.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
static void foldInRange(HasStepFolder janusgraphStep, Step<?, ?> tinkerpopStep, Traversal.Admin<?, ?> traversal, List<HasContainer> hasContainers) {
    Step<?, ?> nextStep = tinkerpopStep instanceof IdentityStep ? JanusGraphTraversalUtil.getNextNonIdentityStep(tinkerpopStep) : tinkerpopStep;
    if (nextStep instanceof RangeGlobalStep) {
        RangeGlobalStep range = (RangeGlobalStep) nextStep;
        int low = 0;
        if (janusgraphStep instanceof JanusGraphStep) {
            low = QueryUtil.convertLimit(range.getLowRange());
            low = QueryUtil.mergeLowLimits(low, hasContainers == null ? janusgraphStep.getLowLimit() : janusgraphStep.getLocalLowLimit(hasContainers));
        }
        int high = QueryUtil.convertLimit(range.getHighRange());
        high = QueryUtil.mergeHighLimits(high, hasContainers == null ? janusgraphStep.getHighLimit() : janusgraphStep.getLocalHighLimit(hasContainers));
        if (hasContainers == null) {
            janusgraphStep.setLimit(low, high);
        } else {
            janusgraphStep.setLocalLimit(hasContainers, low, high);
        }
        if (janusgraphStep instanceof JanusGraphStep || range.getLowRange() == 0) { //Range can be removed since there is JanusGraphStep or no offset
            nextStep.getLabels().forEach(janusgraphStep::addLabel);
            traversal.removeStep(nextStep);
        }
    }
}
 
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: InlineFilterStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static final boolean processTraversalFilterStep(final TraversalFilterStep<?> step, final Traversal.Admin<?, ?> traversal) {
    final Traversal.Admin<?, ?> childTraversal = step.getLocalChildren().get(0);
    if (TraversalHelper.hasAllStepsOfClass(childTraversal, FilterStep.class) &&
            !TraversalHelper.hasStepOfClass(childTraversal,
                    DropStep.class,
                    RangeGlobalStep.class,
                    DedupGlobalStep.class,
                    LambdaHolder.class)) {
        TraversalHelper.applySingleLevelStrategies(traversal, childTraversal, InlineFilterStrategy.class);
        final Step<?, ?> finalStep = childTraversal.getEndStep();
        TraversalHelper.insertTraversal((Step) step, childTraversal, traversal);
        TraversalHelper.copyLabels(step, finalStep, false);
        traversal.removeStep(step);
        return true;
    }
    return false;
}
 
Example #4
Source File: ReplacedStepTree.java    From sqlg with MIT License 6 votes vote down vote up
void walkReplacedSteps(Set<SchemaTableTree> schemaTableTrees) {
    //The tree only has one linear path from root to the deepest leaf node.
    //This represents the regular path where each ReplacedStep goes one step deeper down the graph.
    //First build the SchemaTableTrees for this path.
    //The other nodes in this ReplacedStepTree are nodes that need to join onto the left join nodes coming from optional steps.
    List<ReplacedStep<?, ?>> replacedSteps = linearPathToLeafNode();

    for (ReplacedStep<?, ?> replacedStep : replacedSteps) {
        //skip the graph step
        if (replacedStep.getStep() instanceof GraphStep) {
            continue;
        }
        if (!replacedStep.isFake() && !(replacedStep.getStep() instanceof OrderGlobalStep) && !(replacedStep.getStep() instanceof RangeGlobalStep)) {
            //This schemaTableTree represents the tree nodes as build up to this depth. Each replacedStep goes a level further
            schemaTableTrees = replacedStep.calculatePathForStep(schemaTableTrees);
        }
    }
}
 
Example #5
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public static void extractRange(Step<?, ?> newStep,
                                Traversal.Admin<?, ?> traversal,
                                boolean extractOnlyLimit) {
    QueryHolder holder = (QueryHolder) newStep;
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof RangeGlobalStep) {
            @SuppressWarnings("unchecked")
            RangeGlobalStep<Object> range = (RangeGlobalStep<Object>) step;
            /*
             * NOTE: keep the step to limit results after query from DB
             * due to `limit`(in DB) may not be implemented accurately.
             * but the backend driver should ensure `offset` accurately.
             */
            // TraversalHelper.copyLabels(step, newStep, false);
            // traversal.removeStep(step);
            if (extractOnlyLimit) {
                // May need to retain offset for multiple sub-queries
                holder.setRange(0, range.getHighRange());
            } else {
                long limit = holder.setRange(range.getLowRange(),
                                             range.getHighRange());
                RangeGlobalStep<Object> newRange = new RangeGlobalStep<>(
                                                   traversal, 0, limit);
                TraversalHelper.replaceStep(range, newRange, traversal);
            }
        }
    } while (step instanceof RangeGlobalStep ||
             step instanceof IdentityStep ||
             step instanceof NoOpBarrierStep);
}
 
Example #6
Source File: HasStepFolder.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
static boolean validFoldInHasContainer(Step<?, ?> tinkerpopStep, boolean defaultValue) {
    Step<?, ?> currentStep = tinkerpopStep;
    Boolean toReturn = null;
    while (!(currentStep instanceof EmptyStep)) {
        if (currentStep instanceof HasContainerHolder) {
            Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers();
            toReturn = toReturn == null ? validJanusGraphHas(containers) : toReturn && validJanusGraphHas(containers);
        } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof NoOpBarrierStep) && !(currentStep instanceof RangeGlobalStep) && !(currentStep instanceof OrderGlobalStep)) {
            toReturn = toReturn != null && (toReturn && defaultValue);
            break;
        }
        currentStep = currentStep.getNextStep();
    }
    return Boolean.TRUE.equals(toReturn);
}
 
Example #7
Source File: HasStepFolder.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
public static <E extends Ranging> void foldInRange(final HasStepFolder titanStep, final Traversal.Admin<?, ?> traversal) {
    Step<?, ?> nextStep = TitanTraversalUtil.getNextNonIdentityStep(titanStep);

    if (nextStep instanceof RangeGlobalStep) {
        RangeGlobalStep range = (RangeGlobalStep) nextStep;
        int limit = QueryUtil.convertLimit(range.getHighRange());
        titanStep.setLimit(QueryUtil.mergeLimits(limit, titanStep.getLimit()));
        if (range.getLowRange() == 0) { //Range can be removed since there is no offset
            nextStep.getLabels().forEach(titanStep::addLabel);
            traversal.removeStep(nextStep);
        }
    }
}
 
Example #8
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 #9
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 #10
Source File: MasterExecutor.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static boolean isLocalElement(final Step<?, ?> step) {
    return step instanceof PropertiesStep || step instanceof PropertyMapStep ||
            step instanceof IdStep || step instanceof LabelStep || step instanceof SackStep ||
            step instanceof PropertyKeyStep || step instanceof PropertyValueStep ||
            step instanceof TailGlobalStep || step instanceof RangeGlobalStep || step instanceof HasStep ||
            step instanceof ConnectiveStep;
}
 
Example #11
Source File: TestRangeLimit.java    From sqlg with MIT License 5 votes vote down vote up
/**
 * ensure once we've built the traversal, it contains a RangeGlobalStep
 *
 * @param g
 */
private void ensureRangeGlobal(GraphTraversal<?, ?> g) {
    DefaultGraphTraversal<?, ?> dgt = (DefaultGraphTraversal<?, ?>) g;
    boolean found = false;
    for (Step<?, ?> s : dgt.getSteps()) {
        found |= (s instanceof RangeGlobalStep<?>);
    }
    Assert.assertTrue(found);
}
 
Example #12
Source File: TestRangeLimit.java    From sqlg with MIT License 5 votes vote down vote up
/**
 * once we've run the traversal, it shouldn't contain the RangeGlobalStep,
 * since it was changed into a Range on the ReplacedStep
 *
 * @param g
 */
private void ensureNoRangeGlobal(GraphTraversal<?, ?> g) {
    DefaultGraphTraversal<?, ?> dgt = (DefaultGraphTraversal<?, ?>) g;
    for (Step<?, ?> s : dgt.getSteps()) {
        Assert.assertFalse(s instanceof RangeGlobalStep<?>);
    }
}
 
Example #13
Source File: TestUnoptimizedRepeatStep.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testRepeatStepWithLimit() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    a1.addEdge("ab", b1);
    a2.addEdge("ab", b2);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .until(__.has(T.label, "B"))
            .repeat(__.out())
            .limit(1);

    Assert.assertEquals(4, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(2) instanceof RepeatStep);
    Assert.assertTrue(traversal.getSteps().get(3) instanceof RangeGlobalStep);
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgRepeatStepBarrier);
    Assert.assertEquals(1, vertices.size());
    Assert.assertTrue(vertices.contains(b1) || vertices.contains(b2));

    vertices = this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .repeat(__.out())
            .until(__.has(T.label, "B"))
            .limit(1)
            .toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertTrue(vertices.contains(b1) || vertices.contains(b2));

}
 
Example #14
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 3 votes vote down vote up
/**
 * Filter the objects in the traversal by the number of them to pass through the stream as constrained by the
 * {@link Scope}. Those before the value of {@code low} do not pass through and those that exceed the value of
 * {@code high} will end the iteration.
 *
 * @param scope the scope of how to apply the {@code range}
 * @param low   the number at which to start allowing objects through the stream
 * @param high  the number at which to end the stream - use {@code -1} to emit all remaining objects
 * @return the traversal with an appended {@link RangeGlobalStep} or {@link RangeLocalStep} depending on {@code scope}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#range-step" target="_blank">Reference Documentation - Range Step</a>
 * @since 3.0.0-incubating
 */
public default <E2> GraphTraversal<S, E2> range(final Scope scope, final long low, final long high) {
    this.asAdmin().getBytecode().addStep(Symbols.range, scope, low, high);
    return this.asAdmin().addStep(scope.equals(Scope.global)
            ? new RangeGlobalStep<>(this.asAdmin(), low, high)
            : new RangeLocalStep<>(this.asAdmin(), low, high));
}
 
Example #15
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Filter the objects in the traversal by the number of them to pass through the stream. Those before the value
 * of {@code low} do not pass through and those that exceed the value of {@code high} will end the iteration.
 *
 * @param low  the number at which to start allowing objects through the stream
 * @param high the number at which to end the stream - use {@code -1} to emit all remaining objects
 * @return the traversal with an appended {@link RangeGlobalStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#range-step" target="_blank">Reference Documentation - Range Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> range(final long low, final long high) {
    this.asAdmin().getBytecode().addStep(Symbols.range, low, high);
    return this.asAdmin().addStep(new RangeGlobalStep<>(this.asAdmin(), low, high));
}