Search in sources :

Example 1 with GraphStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep in project grakn by graknlabs.

the class JanusPreviousPropertyStepStrategy method apply.

@Override
public void apply(Traversal.Admin<?, ?> traversal) {
    // Retrieve all graph (`V()`) steps - this is the step the strategy should replace
    List<GraphStep> graphSteps = TraversalHelper.getStepsOfAssignableClass(GraphStep.class, traversal);
    for (GraphStep graphStep : graphSteps) {
        if (!(graphStep.getNextStep() instanceof TraversalFilterStep))
            continue;
        TraversalFilterStep<Vertex> filterStep = (TraversalFilterStep<Vertex>) graphStep.getNextStep();
        // Retrieve the filter steps e.g. `__.properties(a).where(P.eq(b))`
        List<Step> steps = stepsFromFilterStep(filterStep);
        if (steps.size() < 2)
            continue;
        // This is `properties(a)`
        Step propertiesStep = steps.get(0);
        // This is `filter(__.where(P.eq(b)))`
        Step whereStep = steps.get(1);
        // Get the property key `a`
        if (!(propertiesStep instanceof PropertiesStep))
            continue;
        Optional<String> propertyKey = propertyFromPropertiesStep((PropertiesStep<Vertex>) propertiesStep);
        if (!propertyKey.isPresent())
            continue;
        // Get the step label `b`
        if (!(whereStep instanceof WherePredicateStep))
            continue;
        Optional<String> label = labelFromWhereEqPredicate((WherePredicateStep<Vertex>) whereStep);
        if (!label.isPresent())
            continue;
        executeStrategy(traversal, graphStep, filterStep, propertyKey.get(), label.get());
    }
}
Also used : WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) GraphStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep) WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) GraphStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep)

Example 2 with GraphStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep in project janusgraph by JanusGraph.

the class HasStepFolder method foldInIds.

static void foldInIds(final HasStepFolder janusgraphStep, final Traversal.Admin<?, ?> traversal) {
    Step<?, ?> currentStep = janusgraphStep.getNextStep();
    while (true) {
        if (currentStep instanceof HasContainerHolder) {
            final HasContainerHolder hasContainerHolder = (HasContainerHolder) currentStep;
            final GraphStep graphStep = (GraphStep) janusgraphStep;
            // HasContainer collection that we get back is immutable so we keep track of which containers
            // need to be deleted after they've been folded into the JanusGraphStep and then remove them from their
            // step using HasContainer.removeHasContainer
            final List<HasContainer> removableHasContainers = new ArrayList<>();
            final Set<String> stepLabels = currentStep.getLabels();
            hasContainerHolder.getHasContainers().forEach(hasContainer -> {
                if (GraphStep.processHasContainerIds(graphStep, hasContainer)) {
                    stepLabels.forEach(janusgraphStep::addLabel);
                    // this has container is no longer needed because its ids will be folded into the JanusGraphStep
                    removableHasContainers.add(hasContainer);
                }
            });
            if (!removableHasContainers.isEmpty()) {
                removableHasContainers.forEach(hasContainerHolder::removeHasContainer);
            }
            // if all has containers have been removed, the current step can be removed
            if (hasContainerHolder.getHasContainers().isEmpty()) {
                traversal.removeStep(currentStep);
            }
        } else if (currentStep instanceof IdentityStep) {
        // do nothing, has no impact
        } else if (currentStep instanceof NoOpBarrierStep) {
        // do nothing, has no impact
        } else {
            break;
        }
        currentStep = currentStep.getNextStep();
    }
}
Also used : IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) HasContainerHolder(org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder) GraphStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) ArrayList(java.util.ArrayList) NoOpBarrierStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep)

Aggregations

GraphStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep)2 ArrayList (java.util.ArrayList)1 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)1 HasContainerHolder (org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder)1 TraversalFilterStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep)1 WherePredicateStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep)1 NoOpBarrierStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep)1 PropertiesStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep)1 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)1 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1