Skip to content

Traversal Optimization

okram edited this page May 16, 2012 · 14 revisions

In Gremlin, the same path expression can be written in numerous ways. Gremlin is a procedural language in that the developer explicitly instructs Gremlin which path to take through the graph. However, there are various optimizations that Gremlin can automatically take advantage of. This section describes such automatic optimizations.

Vertex Query Optimization

In Blueprints, their is a method called Vertex.query(). This method returns a Query object that can be configured to filter the edges/vertices associated with that Vertex according to edge labels, edge properties, edge property intervals, etc. Whenever a pattern such as outE....inV is seen by Gremlin, Gremlin will automatically compile that statement into a Query object.

gremlin> g.v(1).outE('knows','created').interval('weight',0.5,1.0).has('date',2012).inV.name.toString()
==>[StartPipe, QueryPipe(out,[knows, created],has:true,interval:true,vertex), PropertyPipe(name)]

However, when using the method GremlinPipeline.optimize(boolean), it is possible to turn of such query optimizations.

gremlin> gremlin> g.v(1).optimize(false).outE('knows','created').interval('weight',0.5,1.0).has('date',2012).inV.name.toString()
==>[StartPipe, OutEdgesPipe(knows,created), IntervalFilterPipe, PropertyFilterPipe(date,EQUAL,2012), InVertexPipe, PropertyPipe(name)]

Automatic Path Enabling

Pipes natively supports the recording of the history of a particular traversal. By default, Gremlin does not assume that path information will be required of the traversal unless a path specific step is called — e.g. path, simplePath. If the path information is required internal to a closure, Gremlin doesn’t know that as it can not interpret what is in a closure. As such, be sure to GremlinPipeline.enablePath().

gremlin> g.v(1).out.loop(1){it.loops < 3}{it.path.contains(g.v(4))}             
Cannot invoke method contains() on null object
Display stack trace? [yN] 
gremlin> g.v(1).out.loop(1){it.loops < 3}{it.path.contains(g.v(4))}.enablePath()
==>v[5]
==>v[3]