gak
gak

Reputation: 32763

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

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.

Upvotes: 2

Views: 1313

Answers (2)

stephen mallette
stephen mallette

Reputation: 46216

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

Upvotes: 3

gak
gak

Reputation: 32763

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

Upvotes: 0

Related Questions