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

好久不见. 提交于 2019-12-10 11:04:17

问题


This works as expected:

gremlin> root.out.outE.has('size', 4).count()
==>3
gremlin> result = root.out.outE.has('size', 4).count()
==>3
gremlin> result
==>3
gremlin> root.out.outE.has('count', 4).getClass()         
==>class com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline

When I store the GremlinGroovyPipeline into a variable, I can't count() it anymore:

gremlin> result = root.out.outE.has('size', 4)        
==>e[359:200:36028797018964014][200-sizes->40]
==>e[669:404:36028797018964014][404-sizes->400]
==>e[855:516:36028797018964014][516-sizes->524]
gremlin> result.count()
==>0
gremlin> result.getClass()
==>class com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline

This is quite strange to me. It appears that once the variable is assigned, the results are gone.

I'm using Titan on BDB.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/13151141/gremlin-when-storing-a-gremlingroovypipeline-and-calling-count-on-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!