-
Notifications
You must be signed in to change notification settings - Fork 239
Traversal Optimization
In Gremlin, the same path expression can be written in numerous ways. Gremlin is an imperative language in that the developer explicitly instructs Gremlin which path (as defined by an abstract path description) to take through the graph. However, Gremlin doesn’t always do as he is told. There are various optimizations that Gremlin can automatically take advantage of. This section describes such automatic optimizations.
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 off such query optimizations.
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)]
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()
if path information will be required by the expression.
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]