Search in sources :

Example 1 with WherePredicateStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep 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 WherePredicateStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep in project sqlg by pietermartin.

the class SqlgWhereStrategy method apply.

@SuppressWarnings("resource")
@Override
public void apply(Admin<?, ?> traversal) {
    if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
        return;
    }
    SqlgGraph sqlgGraph = (SqlgGraph) traversal.getGraph().get();
    // The query will read from the cache if this is for a cached vertex
    if (sqlgGraph.features().supportsBatchMode() && sqlgGraph.tx().isInNormalBatchMode()) {
        sqlgGraph.tx().flush();
    }
    List<Step<?, ?>> steps = new ArrayList(traversal.asAdmin().getSteps());
    ListIterator<Step<?, ?>> stepIterator = steps.listIterator();
    // get all steps per label
    Map<String, Object> stepsByLabel = new HashMap<>();
    stepIterator = steps.listIterator();
    Step<?, ?> previous = null;
    int idx = 0;
    while (stepIterator.hasNext()) {
        Step<?, ?> step = stepIterator.next();
        captureLabels(step, stepsByLabel);
        if (step instanceof WherePredicateStep<?>) {
            WherePredicateStep<?> wps = (WherePredicateStep<?>) step;
            if (wps.getPredicate().isPresent() && wps.getPredicate().get().getBiPredicate() instanceof FullText) {
                Object referTo = previous;
                if (wps.getStartKey().isPresent()) {
                    referTo = stepsByLabel.get(wps.getStartKey().get());
                }
                if (referTo instanceof SqlgGraphStep<?, ?>) {
                    SqlgGraphStep<?, ?> sgs = (SqlgGraphStep<?, ?>) referTo;
                    if (sgs.getReplacedSteps().size() > 0) {
                        referTo = sgs.getReplacedSteps().get(sgs.getReplacedSteps().size() - 1);
                    }
                }
                if (referTo instanceof ReplacedStep<?, ?>) {
                    ReplacedStep<?, ?> rs = (ReplacedStep<?, ?>) referTo;
                    rs.addHasContainer(new HasContainer("__dummy__", wps.getPredicate().get()));
                    traversal.removeStep(idx);
                }
            }
        }
        previous = step;
        idx++;
    }
}
Also used : WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) FullText(org.umlg.sqlg.predicate.FullText)

Aggregations

Step (org.apache.tinkerpop.gremlin.process.traversal.Step)2 WherePredicateStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep)2 TraversalFilterStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep)1 GraphStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep)1 PropertiesStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep)1 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1 FullText (org.umlg.sqlg.predicate.FullText)1 ReplacedStep (org.umlg.sqlg.sql.parse.ReplacedStep)1 SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)1 SqlgGraph (org.umlg.sqlg.structure.SqlgGraph)1