Coalsece going through both traverser

30 views
Skip to first unread message

himesh k

unread,
Mar 17, 2018, 7:54:51 PM3/17/18
to Gremlin-users
Hi all, 
I was trying to create vertex if not present. 

One of the ways I found online was : 
g.V(1000).fold().coalesce(unfold(), constant('x'))

The above works, however, wanted to see if there is a more efficient way to achieve this.

Hence, tried the following query:
g.V(1000).as('a').inject(1).coalesce(select('a'), constant('x'))

The above query works too but only in the negative case! For example the following query prints the results from both traversal in coalesce:

g.V(1).as('a').inject(1).coalesce(select('a'), constant('x'))

output:
==>x
==>v[1]

Would someone be able to explain why this is happening? And which of the 2 would be more efficient to do?

Yours Gratefully, 
Himesh Kakkar

himesh k

unread,
Mar 17, 2018, 8:16:54 PM3/17/18
to Gremlin-users
Also, g.inject(1).coalesce(g.V(1), constant('x'))

which of the three is efficient?

HadoopMarc

unread,
Mar 18, 2018, 11:30:29 AM3/18/18
to Gremlin-users
Hi Himesh,

As far as I can see "get or create vertex" is not easily done in gremlin only. So:

if (! g.V().has('name','stephen').hasNext()) {
    g
.addV('person').property('name','stephen')
}

Do not forget to create an index on the relevant properties if your graph is large.

Regarding your own query: after g.V(1).as('a').inject(1) your traversal has separate traversers for v[1] and 1. v[1] coalesces to v[1] while 1 coalesces to 'x' as it does not "emit at least one element".

HTH,     Marc



Op zondag 18 maart 2018 01:16:54 UTC+1 schreef himesh k:

HadoopMarc

unread,
Mar 18, 2018, 11:40:58 AM3/18/18
to Gremlin-users
Some additional explanation below:

Op zondag 18 maart 2018 16:30:29 UTC+1 schreef HadoopMarc:

Regarding your own query: after g.V(1).as('a').inject(1) your traversal has separate traversers for v[1] and 1. v[1] coalesces to v[1] while 1 coalesces to 'x' as it does not "emit at least one element".


The traverser with 1 was created later in the traversal and does not know about 'a' and therefore select('a') does not emit an element. Btw "element" is a confusing term here, because you could think element is either a vertex or an edge. In this situation it can be any collection item.

 
Reply all
Reply to author
Forward
0 new messages