Search in sources :

Example 1 with LocalStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep 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)

Aggregations

StandardTitanGraph (com.thinkaurelius.titan.graphdb.database.StandardTitanGraph)1 StandardTitanTx (com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx)1 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)1 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)1 LocalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep)1 RangeGlobalStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep)1 PropertiesStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep)1 VertexStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep)1 Graph (org.apache.tinkerpop.gremlin.structure.Graph)1