Gremlin when storing a GremlinGroovyPipeline and calling .count() on it

跟風遠走 提交于 2019-12-06 06:04:32

The pipeline is an iterator, so once the pipeline is exhausted it's "empty". The Gremlin Console automatically iterates the pipeline for you so it effectively exhausts the list even though you've stored the pipeline into a variable.

Consider this example for clarity

gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> pipeline=g.v(1).out
==>v[2]
==>v[4]
==>v[3]
gremlin> pipeline.next()
com.tinkerpop.pipes.util.FastNoSuchElementException
Display stack trace? [yN] n
gremlin> pipeline.count()
==>0

Note that there is nothing left in the pipeline, just as in your example. So...if you want to "store" the non-iterated pipeline in a variable for later evaluation you need to prevent the console from auto-iterating it:

gremlin> pipeline=g.v(1).out;null
==>null
gremlin> pipeline.count()        
==>3

Of course, once you iterate it....it's empty:

gremlin> pipeline.count()
==>0

and you have to initialize that pipeline again:

gremlin> pipeline=g.v(1).out;null
==>null
gremlin> pipeline.next()         
==>v[2]
gremlin> pipeline.next()
==>v[4]
gremlin> pipeline.next()
==>v[3]
gremlin> pipeline.next()
com.tinkerpop.pipes.util.FastNoSuchElementException
Display stack trace? [yN] n
gremlin> pipeline.count()        
==>0

So the best thing you can do if you want to continue to work with the results is to iterate to a list as you did in your response to yourself.

gremlin> l=[];g.v(1).out.fill(l)    
==>v[2]
==>v[4]
==>v[3]
gremlin> l.size()
==>3

One solution I have discovered is to convert it to a list first, but doesn't seem ideal and doesn't really tell me why this variable is behaving differently:

gremlin> result = root.out.outE.has('size', 4).toList()
==>e[359:200:36028797018964014][200-sizes->40]
==>e[669:404:36028797018964014][404-sizes->400]
==>e[855:516:36028797018964014][516-sizes->524]
gremlin> result.size()
==>3
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!