What the following () means in TinkerPop

I am currently reading the TinkerPop3 Documentation

What bothers me is that I cannot find any explanation about next() .

For example, w / next () or w / o next () returns the same vertext

 gremlin> gV().has('name', 'marko') ==>v[1] gremlin> gV().has('name', 'marko').next() ==>v[1] 

but class names are different from each other.

 gremlin> gV().has('name', 'marko').getClass() ==>class org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal gremlin> gV().has('name', 'marko').next().getClass() ==>class org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex 

Without 'next ()', the assigned variable does not matter.

 gremlin> marko = gV().has('name', 'marko') ==>v[1] gremlin> marko 

Even with clockWithResult() outputs are completely different.

 gremlin> clockWithResult(1){gV().both().barrier().both().barrier().both().barrier().count().next()} ==>1.079524 ==>72 gremlin> clockWithResult(1){gV().both().barrier().both().barrier().both().barrier().count()} ==>0.11863599999999999 ==>[GraphStep([],vertex), VertexStep(BOTH,vertex), NoOpBarrierStep(2147483647), VertexStep(BOTH,vertex), NoOpBarrierStep(2147483647), VertexStep(BOTH,vertex), NoOpBarrierStep(2147483647), CountGlobalStep] 

or this example:

 gremlin> gV(1).out('knows').values('name').fold() ==>[vadas, josh] gremlin> gV(1).out('knows').values('name').fold().next() ==>vadas ==>josh 

in the manual, there are many other examples that make me confused.

I hope that Marco and his friends will help me.

Thanks for the advanced.

+5
source share
1 answer

The short answer is that the Gremlin console iterates the results automatically.

 x = gV().has('name', 'marko') 

In the above example, x will be an instance of Traversal , which is a type of Iterator . When the console encounters an Iterator , it automatically expands it so you can see the results. In this case:

 x = gV().has('name', 'marko').next() 

adding next() just says you want to call Iterator.next() - in other words, you want to get the first element from Iterator . So, in the case above, x will be Vertex .

In this case:

 gremlin> marko = gV().has('name', 'marko') ==>v[1] gremlin> marko 

Now you know that marko is an Iterator , so when you evaluate it again, the console tries to iterate it. Of course, the console has already repeated it on the previous line, so when he tries to do it again, there is nothing extra to repeat. Here is an example that makes it more obvious what is happening:

 gremlin> x = gV();null ==>null gremlin> x.next() ==>v[1] gremlin> x.next() ==>v[2] gremlin> x ==>v[3] ==>v[4] ==>v[5] ==>v[6] 

Note the use of ;null in the first line, which prohibits iteration of the x console. What for? because my script returns null not x .

Now it should be clear what your example does with clock ... the first line that calls next() measures the execution of the traversal, and the second measures the execution of the Traversal construct. Why do you need to call next() in this case? Since Traversal is inside a closure, remember that the console only iterates the return value of a function, not every Iterator in your scripts.

Finally:

 gremlin> gV(1).out('knows').values('name').fold() ==>[vadas, josh] gremlin> gV(1).out('knows').values('name').fold().next() ==>vadas ==>josh 

I hope that everything else that I mentioned above allows you to understand why next() creates this behavior, but just in case, this is what the console essentially does:

 gremlin> x = gV(1).out('knows').values('name').fold();null ==>null gremlin> y = x.next();null ==>null gremlin> y ==>vadas ==>josh 
+8
source

Source: https://habr.com/ru/post/1237156/


All Articles