Search in sources :

Example 1 with PropertiesStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep in project titan by thinkaurelius.

the class TitanLocalQueryOptimizerStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!traversal.getGraph().isPresent())
        return;
    Graph graph = traversal.getGraph().get();
    //If this is a compute graph then we can't apply local traversal optimisation at this stage.
    StandardTitanGraph titanGraph = graph instanceof StandardTitanTx ? ((StandardTitanTx) graph).getGraph() : (StandardTitanGraph) graph;
    final boolean useMultiQuery = traversal.getEngine().isStandard() && titanGraph.getConfiguration().useMultiQuery();
    /*
                ====== VERTEX STEP ======
         */
    TraversalHelper.getStepsOfClass(VertexStep.class, traversal).forEach(originalStep -> {
        TitanVertexStep vstep = new TitanVertexStep(originalStep);
        TraversalHelper.replaceStep(originalStep, vstep, traversal);
        if (TitanTraversalUtil.isEdgeReturnStep(vstep)) {
            HasStepFolder.foldInHasContainer(vstep, traversal);
        }
        assert TitanTraversalUtil.isEdgeReturnStep(vstep) || TitanTraversalUtil.isVertexReturnStep(vstep);
        Step nextStep = TitanTraversalUtil.getNextNonIdentityStep(vstep);
        if (nextStep instanceof RangeGlobalStep) {
            int limit = QueryUtil.convertLimit(((RangeGlobalStep) nextStep).getHighRange());
            vstep.setLimit(QueryUtil.mergeLimits(limit, vstep.getLimit()));
        }
        if (useMultiQuery) {
            vstep.setUseMultiQuery(true);
        }
    });
    /*
                ====== PROPERTIES STEP ======
         */
    TraversalHelper.getStepsOfClass(PropertiesStep.class, traversal).forEach(originalStep -> {
        TitanPropertiesStep vstep = new TitanPropertiesStep(originalStep);
        TraversalHelper.replaceStep(originalStep, vstep, traversal);
        if (vstep.getReturnType().forProperties()) {
            HasStepFolder.foldInHasContainer(vstep, traversal);
        }
        if (useMultiQuery) {
            vstep.setUseMultiQuery(true);
        }
    });
    /*
                ====== EITHER INSIDE LOCAL ======
         */
    TraversalHelper.getStepsOfClass(LocalStep.class, traversal).forEach(localStep -> {
        Traversal.Admin localTraversal = ((LocalStep<?, ?>) localStep).getLocalChildren().get(0);
        Step localStart = localTraversal.getStartStep();
        if (localStart instanceof VertexStep) {
            TitanVertexStep vstep = new TitanVertexStep((VertexStep) localStart);
            TraversalHelper.replaceStep(localStart, vstep, localTraversal);
            if (TitanTraversalUtil.isEdgeReturnStep(vstep)) {
                HasStepFolder.foldInHasContainer(vstep, localTraversal);
                HasStepFolder.foldInOrder(vstep, localTraversal, traversal, false);
            }
            HasStepFolder.foldInRange(vstep, localTraversal);
            unfoldLocalTraversal(traversal, localStep, localTraversal, vstep, useMultiQuery);
        }
        if (localStart instanceof PropertiesStep) {
            TitanPropertiesStep vstep = new TitanPropertiesStep((PropertiesStep) localStart);
            TraversalHelper.replaceStep(localStart, vstep, localTraversal);
            if (vstep.getReturnType().forProperties()) {
                HasStepFolder.foldInHasContainer(vstep, localTraversal);
                HasStepFolder.foldInOrder(vstep, localTraversal, traversal, false);
            }
            HasStepFolder.foldInRange(vstep, localTraversal);
            unfoldLocalTraversal(traversal, localStep, localTraversal, vstep, useMultiQuery);
        }
    });
}
Also used : PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) Graph(org.apache.tinkerpop.gremlin.structure.Graph) StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep)

Example 2 with PropertiesStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep 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 3 with PropertiesStep

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

the class JanusGraphLocalQueryOptimizerStrategy method applyJanusGraphPropertiesSteps.

private void applyJanusGraphPropertiesSteps(Admin<?, ?> traversal) {
    TraversalHelper.getStepsOfAssignableClass(PropertiesStep.class, traversal).forEach(originalStep -> {
        final JanusGraphPropertiesStep propertiesStep = new JanusGraphPropertiesStep(originalStep);
        TraversalHelper.replaceStep(originalStep, propertiesStep, originalStep.getTraversal());
        if (propertiesStep.getReturnType().forProperties()) {
            HasStepFolder.foldInHasContainer(propertiesStep, originalStep.getTraversal(), originalStep.getTraversal());
        // We cannot fold in orders or ranges since they are not local
        }
    });
}
Also used : PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) JanusGraphPropertiesStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphPropertiesStep) JanusGraphPropertiesStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphPropertiesStep)

Example 4 with PropertiesStep

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

the class JanusGraphLocalQueryOptimizerStrategy method inspectLocalTraversals.

private void inspectLocalTraversals(final Admin<?, ?> traversal) {
    TraversalHelper.getStepsOfClass(LocalStep.class, traversal).forEach(localStep -> {
        final Admin localTraversal = ((LocalStep<?, ?>) localStep).getLocalChildren().get(0);
        final Step localStart = localTraversal.getStartStep();
        if (localStart instanceof VertexStep) {
            final JanusGraphVertexStep vertexStep = new JanusGraphVertexStep((VertexStep) localStart);
            TraversalHelper.replaceStep(localStart, vertexStep, localTraversal);
            if (JanusGraphTraversalUtil.isEdgeReturnStep(vertexStep)) {
                HasStepFolder.foldInHasContainer(vertexStep, localTraversal, traversal);
                HasStepFolder.foldInOrder(vertexStep, vertexStep.getNextStep(), localTraversal, traversal, false, null);
            }
            HasStepFolder.foldInRange(vertexStep, JanusGraphTraversalUtil.getNextNonIdentityStep(vertexStep), localTraversal, null);
            unfoldLocalTraversal(traversal, localStep, localTraversal, vertexStep);
        }
        if (localStart instanceof PropertiesStep) {
            final JanusGraphPropertiesStep propertiesStep = new JanusGraphPropertiesStep((PropertiesStep) localStart);
            TraversalHelper.replaceStep(localStart, propertiesStep, localTraversal);
            if (propertiesStep.getReturnType().forProperties()) {
                HasStepFolder.foldInHasContainer(propertiesStep, localTraversal, traversal);
                HasStepFolder.foldInOrder(propertiesStep, propertiesStep.getNextStep(), localTraversal, traversal, false, null);
            }
            HasStepFolder.foldInRange(propertiesStep, JanusGraphTraversalUtil.getNextNonIdentityStep(propertiesStep), localTraversal, null);
            unfoldLocalTraversal(traversal, localStep, localTraversal, propertiesStep);
        }
    });
}
Also used : EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) JanusGraphVertexStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphVertexStep) JanusGraphEdgeVertexStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphEdgeVertexStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) JanusGraphPropertiesStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphPropertiesStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) JanusGraphVertexStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphVertexStep) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) JanusGraphEdgeVertexStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphEdgeVertexStep) JanusGraphPropertiesStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphPropertiesStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) Admin(org.apache.tinkerpop.gremlin.process.traversal.Traversal.Admin) JanusGraphVertexStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphVertexStep) JanusGraphPropertiesStep(org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphPropertiesStep)

Example 5 with PropertiesStep

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

the class JanusGraphLocalQueryOptimizerStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!traversal.getGraph().isPresent())
        return;
    Graph graph = traversal.getGraph().get();
    // If this is a compute graph then we can't apply local traversal optimisation at this stage.
    StandardJanusGraph janusGraph = graph instanceof StandardJanusGraphTx ? ((StandardJanusGraphTx) graph).getGraph() : (StandardJanusGraph) graph;
    final boolean useMultiQuery = !TraversalHelper.onGraphComputer(traversal) && janusGraph.getConfiguration().useMultiQuery();
    /*
                ====== VERTEX STEP ======
         */
    TraversalHelper.getStepsOfClass(VertexStep.class, traversal).forEach(originalStep -> {
        JanusGraphVertexStep vertexStep = new JanusGraphVertexStep(originalStep);
        TraversalHelper.replaceStep(originalStep, vertexStep, traversal);
        if (JanusGraphTraversalUtil.isEdgeReturnStep(vertexStep)) {
            HasStepFolder.foldInHasContainer(vertexStep, traversal);
        // We cannot fold in orders or ranges since they are not local
        }
        assert JanusGraphTraversalUtil.isEdgeReturnStep(vertexStep) || JanusGraphTraversalUtil.isVertexReturnStep(vertexStep);
        Step nextStep = JanusGraphTraversalUtil.getNextNonIdentityStep(vertexStep);
        if (nextStep instanceof RangeGlobalStep) {
            int limit = QueryUtil.convertLimit(((RangeGlobalStep) nextStep).getHighRange());
            vertexStep.setLimit(QueryUtil.mergeLimits(limit, vertexStep.getLimit()));
        }
        if (useMultiQuery) {
            vertexStep.setUseMultiQuery(true);
        }
    });
    /*
                ====== PROPERTIES STEP ======
         */
    TraversalHelper.getStepsOfClass(PropertiesStep.class, traversal).forEach(originalStep -> {
        JanusGraphPropertiesStep propertiesStep = new JanusGraphPropertiesStep(originalStep);
        TraversalHelper.replaceStep(originalStep, propertiesStep, traversal);
        if (propertiesStep.getReturnType().forProperties()) {
            HasStepFolder.foldInHasContainer(propertiesStep, traversal);
        // We cannot fold in orders or ranges since they are not local
        }
        if (useMultiQuery) {
            propertiesStep.setUseMultiQuery(true);
        }
    });
    /*
                ====== EITHER INSIDE LOCAL ======
         */
    TraversalHelper.getStepsOfClass(LocalStep.class, traversal).forEach(localStep -> {
        Traversal.Admin localTraversal = ((LocalStep<?, ?>) localStep).getLocalChildren().get(0);
        Step localStart = localTraversal.getStartStep();
        if (localStart instanceof VertexStep) {
            JanusGraphVertexStep vertexStep = new JanusGraphVertexStep((VertexStep) localStart);
            TraversalHelper.replaceStep(localStart, vertexStep, localTraversal);
            if (JanusGraphTraversalUtil.isEdgeReturnStep(vertexStep)) {
                HasStepFolder.foldInHasContainer(vertexStep, localTraversal);
                HasStepFolder.foldInOrder(vertexStep, localTraversal, traversal, false);
            }
            HasStepFolder.foldInRange(vertexStep, localTraversal);
            unfoldLocalTraversal(traversal, localStep, localTraversal, vertexStep, useMultiQuery);
        }
        if (localStart instanceof PropertiesStep) {
            JanusGraphPropertiesStep propertiesStep = new JanusGraphPropertiesStep((PropertiesStep) localStart);
            TraversalHelper.replaceStep(localStart, propertiesStep, localTraversal);
            if (propertiesStep.getReturnType().forProperties()) {
                HasStepFolder.foldInHasContainer(propertiesStep, localTraversal);
                HasStepFolder.foldInOrder(propertiesStep, localTraversal, traversal, false);
            }
            HasStepFolder.foldInRange(propertiesStep, localTraversal);
            unfoldLocalTraversal(traversal, localStep, localTraversal, propertiesStep, useMultiQuery);
        }
    });
}
Also used : PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) Graph(org.apache.tinkerpop.gremlin.structure.Graph) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph)

Aggregations

PropertiesStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep)6 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)5 RangeGlobalStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep)4 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)3 LocalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep)3 VertexStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep)3 TraversalFilterStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep)2 GraphStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep)2 Graph (org.apache.tinkerpop.gremlin.structure.Graph)2 JanusGraphPropertiesStep (org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphPropertiesStep)2 StandardTitanGraph (com.thinkaurelius.titan.graphdb.database.StandardTitanGraph)1 StandardTitanTx (com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx)1 Admin (org.apache.tinkerpop.gremlin.process.traversal.Traversal.Admin)1 ValueTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.ValueTraversal)1 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)1 OrStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.OrStep)1 WherePredicateStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep)1 EdgeVertexStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep)1 FlatMapStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.FlatMapStep)1 NoOpBarrierStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep)1