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);
}
});
}
Aggregations