Aravind Yarram
Aravind Yarram

Reputation: 80194

Is com.tinkerpop.pipes.Pipe implementation safe to cache and re-use later on other graph instances?

I am currently create a Pipe as shown in line 2 below.

Pipe pipe = Gremlin.compile("_().out('knows').name");

After it has been created I am caching it so that it can be re-used with different graphs below

Graph graph = TinkerGraphFactory.createTinkerGraph();
pipe.setStarts(new SingleIterator<Vertex>(graph.getVertex(1)));
for(Object name : pipe) 
{
  System.out.println((String) name);
}

I am wondering if this is alright? I ask because the javadoc of AbstractPipe says

public void reset()
Description copied from interface: Pipe
A pipe may maintain state. Reset is used to remove state. The general use case for reset() is to reuse a pipe in another computation without having to create a new Pipe object. An implementation of this method should be recursive whereby the starts (if a Pipe) should have this method called on it.
Specified by:
reset in interface Pipe<S,E>

Upvotes: 0

Views: 135

Answers (1)

stephen mallette
stephen mallette

Reputation: 46226

I've never trusted reset despite what the javadocs say on the matter, however this test seems to work:

gremlin> pipe = _().out('knows').name;null              
==>null
gremlin> pipe.setStarts(new SingleIterator<Vertex>(g.getVertex(1)));    
==>null
gremlin> pipe
==>vadas
==>josh
gremlin> pipe
gremlin> pipe.setStarts(new SingleIterator<Vertex>(g.getVertex(1)));
==>null
gremlin> pipe                                                       
==>vadas
==>josh

Calling setStarts seems to properly reset the iterator within the pipe, but reset on its own doesn't seem to have much effect:

gremlin> pipe.setStarts(new SingleIterator<Vertex>(g.getVertex(1)));
==>null
gremlin> pipe                                                       
==>vadas
==>josh
gremlin> pipe.reset()
==>null
gremlin> pipe
gremlin>

All that said, I'm not sure caching the Pipeline is saving you all that much. Pipeline creation is pretty cheap and Gremlin.compile() itself caches the script after compilation so future calls to "recreate" that pipeline should be considerably faster than the first call to compile.

Upvotes: 1

Related Questions