Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.Step#getNextStep()

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.Step#getNextStep() . 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: TraversalUtil.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public static void extractOrder(Step<?, ?> newStep,
                                Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof OrderGlobalStep) {
            QueryHolder holder = (QueryHolder) newStep;
            @SuppressWarnings("resource")
            OrderGlobalStep<?, ?> orderStep = (OrderGlobalStep<?, ?>) step;
            orderStep.getComparators().forEach(comp -> {
                ElementValueComparator<?> comparator =
                        (ElementValueComparator<?>) comp.getValue1();
                holder.orderBy(comparator.getPropertyKey(),
                               (Order) comparator.getValueComparator());
            });
            TraversalHelper.copyLabels(step, newStep, false);
            traversal.removeStep(step);
        }
        step = step.getNextStep();
    } while (step instanceof OrderGlobalStep ||
             step instanceof IdentityStep);
}
 
Example 2
Source File: TinkerGraphStepStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (TraversalHelper.onGraphComputer(traversal))
        return;

    for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
        final TinkerGraphStep<?, ?> tinkerGraphStep = new TinkerGraphStep<>(originalGraphStep);
        TraversalHelper.replaceStep(originalGraphStep, tinkerGraphStep, traversal);
        Step<?, ?> currentStep = tinkerGraphStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    if (!GraphStep.processHasContainerIds(tinkerGraphStep, hasContainer))
                        tinkerGraphStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example 3
Source File: HasStepFolder.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
public static void foldInHasContainer(final HasStepFolder titanStep, final Traversal.Admin<?, ?> traversal) {

        Step<?, ?> currentStep = titanStep.getNextStep();
        while (true) {
            if (currentStep instanceof HasContainerHolder) {
                Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers();
                if (validTitanHas(containers)) {
                    titanStep.addAll(containers);
                    currentStep.getLabels().forEach(titanStep::addLabel);
                    traversal.removeStep(currentStep);
                }
            } else if (currentStep instanceof IdentityStep) {
                // do nothing, has no impact
            } else {
                break;
            }
            currentStep = currentStep.getNextStep();
        }
    }
 
Example 4
Source File: HBaseGraphStepStrategy.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
        final HBaseGraphStep<?, ?> hbaseGraphStep = new HBaseGraphStep<>(originalGraphStep);
        TraversalHelper.replaceStep(originalGraphStep, hbaseGraphStep, traversal);
        Step<?, ?> currentStep = hbaseGraphStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    if (!GraphStep.processHasContainerIds(hbaseGraphStep, hasContainer))
                        hbaseGraphStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example 5
Source File: TinkerGraphStepStrategy.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (TraversalHelper.onGraphComputer(traversal))
        return;

    for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
        final TinkerGraphStep<?, ?> tinkerGraphStep = new TinkerGraphStep<>(originalGraphStep);
        TraversalHelper.replaceStep(originalGraphStep, tinkerGraphStep, traversal);
        Step<?, ?> currentStep = tinkerGraphStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    if (!GraphStep.processHasContainerIds(tinkerGraphStep, hasContainer))
                        tinkerGraphStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example 6
Source File: BitsyTraversalStrategy.java    From bitsy with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
	for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
		final BitsyGraphStep<?, ?> bitsyGraphStep = new BitsyGraphStep<>(originalGraphStep);
		TraversalHelper.replaceStep(originalGraphStep, bitsyGraphStep, traversal);
		Step<?, ?> currentStep = bitsyGraphStep.getNextStep();
		while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
			if (currentStep instanceof HasStep) {
				for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
					if (!GraphStep.processHasContainerIds(bitsyGraphStep, hasContainer))
						bitsyGraphStep.addHasContainer(hasContainer);
				}
				TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
				traversal.removeStep(currentStep);
			}
			currentStep = currentStep.getNextStep();
		}
	}
}
 
Example 7
Source File: PathUtil.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public static Set<String> getReferencedLabels(final Step step) {
    final Set<String> referencedLabels = new HashSet<>();

    if (step instanceof Scoping) {
        final Set<String> labels = new HashSet<>(((Scoping) step).getScopeKeys());
        if (step instanceof MatchStep) {
            // if this is the last step, keep everything, else just add founds
            if (step.getNextStep() instanceof EmptyStep) {
                labels.addAll(((MatchStep) step).getMatchEndLabels());
                labels.addAll(((MatchStep) step).getMatchStartLabels());
            }
        }
        referencedLabels.addAll(labels);

    }

    return referencedLabels;
}
 
Example 8
Source File: HasStepFolder.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
static void localFoldInHasContainer(HasStepFolder janusgraphStep, Step<?, ?> tinkerpopStep, Traversal.Admin<?, ?> traversal, Traversal<?, ?> rootTraversal) {
    Step<?, ?> currentStep = tinkerpopStep;
    while (true) {
        if (currentStep instanceof HasContainerHolder) {
            Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers().stream().map(c -> JanusGraphPredicate.Converter.convert(c)).collect(Collectors.toList());
            List<HasContainer> hasContainers = janusgraphStep.addLocalAll(containers);
            currentStep.getLabels().forEach(janusgraphStep::addLabel);
            traversal.removeStep(currentStep);
            currentStep = foldInOrder(janusgraphStep, currentStep, traversal, rootTraversal, janusgraphStep instanceof JanusGraphStep && ((JanusGraphStep) janusgraphStep).returnsVertex(), hasContainers);
            foldInRange(janusgraphStep, currentStep, traversal, hasContainers);
        } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof NoOpBarrierStep)) {
            break;
        }
        currentStep = currentStep.getNextStep();
    }
}
 
Example 9
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public static void extractHasContainer(HugeGraphStep<?, ?> newStep,
                                       Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof HasStep) {
            HasContainerHolder holder = (HasContainerHolder) step;
            for (HasContainer has : holder.getHasContainers()) {
                if (!GraphStep.processHasContainerIds(newStep, has)) {
                    newStep.addHasContainer(has);
                }
            }
            TraversalHelper.copyLabels(step, step.getPreviousStep(), false);
            traversal.removeStep(step);
        }
    } while (step instanceof HasStep || step instanceof NoOpBarrierStep);
}
 
Example 10
Source File: HasStepFolder.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
static void foldInHasContainer(HasStepFolder janusgraphStep, Traversal.Admin<?, ?> traversal, Traversal<?, ?> rootTraversal) {
    Step<?, ?> currentStep = janusgraphStep.getNextStep();
    while (true) {
        if (currentStep instanceof OrStep && janusgraphStep instanceof JanusGraphStep) {
            for (Traversal.Admin<?, ?> child : ((OrStep<?>) currentStep).getLocalChildren()) {
                if (!validFoldInHasContainer(child.getStartStep(), false)) {
                    return;
                }
            }
            ((OrStep<?>) currentStep).getLocalChildren().forEach(t -> localFoldInHasContainer(janusgraphStep, t.getStartStep(), t, rootTraversal));
            traversal.removeStep(currentStep);
        } else if (currentStep instanceof HasContainerHolder) {
            Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers().stream().map(c -> JanusGraphPredicate.Converter.convert(c)).collect(Collectors.toList());
            if (validFoldInHasContainer(currentStep, true)) {
                janusgraphStep.addAll(containers);
                currentStep.getLabels().forEach(janusgraphStep::addLabel);
                traversal.removeStep(currentStep);
            }
        } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof NoOpBarrierStep) && !(currentStep instanceof HasContainerHolder)) {
            break;
        }
        currentStep = currentStep.getNextStep();
    }
}
 
Example 11
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 12
Source File: PathUtil.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static Set<String> getReferencedLabelsAfterStep(Step<?, ?> step) {
    final Set<String> labels = new HashSet<>();
    while (!(step instanceof EmptyStep)) {
        labels.addAll(PathUtil.getReferencedLabels(step));
        step = step.getNextStep();
    }
    return labels;
}
 
Example 13
Source File: VertexProgramStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static Step<?, ?> getFirstLegalOLAPStep(Step<?, ?> currentStep) {
    while (!(currentStep instanceof EmptyStep)) {
        if (!(currentStep instanceof VertexComputing))
            return currentStep;
        currentStep = currentStep.getNextStep();
    }
    return EmptyStep.instance();
}
 
Example 14
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 15
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 16
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public static void extractHasContainer(HugeVertexStep<?> newStep,
                                       Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        if (step instanceof HasStep) {
            HasContainerHolder holder = (HasContainerHolder) step;
            for (HasContainer has : holder.getHasContainers()) {
                newStep.addHasContainer(has);
            }
            TraversalHelper.copyLabels(step, step.getPreviousStep(), false);
            traversal.removeStep(step);
        }
        step = step.getNextStep();
    } while (step instanceof HasStep || step instanceof NoOpBarrierStep);
}
 
Example 17
Source File: JanusGraphTraversalUtil.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Step getNextNonIdentityStep(Step start) {
    Step currentStep = start.getNextStep();
    //Skip over identity steps
    while (currentStep instanceof IdentityStep) currentStep = currentStep.getNextStep();
    return currentStep;
}
 
Example 18
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 19
Source File: TitanTraversalUtil.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
public static Step getNextNonIdentityStep(final Step start) {
    Step currentStep = start.getNextStep();
    //Skip over identity steps
    while (currentStep instanceof IdentityStep) currentStep = currentStep.getNextStep();
    return currentStep;
}
 
Example 20
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);
}