How to find-or-create a node in a thread safe way?

51 views
Skip to first unread message

Yehonathan Sharvit

unread,
Feb 18, 2018, 5:34:08 AM2/18/18
to Gremlin-users
What's the recommended way in Gremlin to find-or-create a node?
By find-or-create, I mean: look for a node having some value and if such a node doesn't exist, create it.

For instance, find or create a user named "john doe".

I thought about using coalesce.  But I am wondering whether my code is thread-safe. Is it part of gremlin or does it depend on the database implementation of gremlin?

Something like this:

g.V().coalesce(
   __.V().has('user', 'name', 'john doe'),
   __.addV('user').property('name', john doe)
)


Is there a better way? 
Is it thread safe?
Is there a way to guarantee atomicity in Gremlin?

Stephen Mallette

unread,
Feb 20, 2018, 7:44:43 AM2/20/18
to Gremlin-users
Gremlin/TinkerPop make no guarantees of this sort. It's generally up to the underlying graph database to decide upon these semantics.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/6c6080bd-a1fa-4f08-8e46-68eeb08512e3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jean-Philippe B

unread,
Mar 7, 2018, 4:25:35 AM3/7/18
to gremli...@googlegroups.com
I'm looking also for a way to do that, but with this example if the
vertex exists I get the matched vertex * the nb of vertices in the
graph:

gremlin> g.V().count()
==>991

# vertex already exists
gremlin> g.V().coalesce(V().hasId('8f1f2c14-bd12-4ec3-9b2d-019f9242b2dd'), g.addV('foo').property(id, '8f1f2c14-bd12-4ec3-9b2d-019f9242b2dd')).count()
==>991
gremlin> g.V().coalesce(hasId('8f1f2c14-bd12-4ec3-9b2d-019f9242b2dd'), g.addV('foo').property(id, '8f1f2c14-bd12-4ec3-9b2d-019f9242b2dd'))
Vertex with id already exists: 8f1f2c14-bd12-4ec3-9b2d-019f9242b2dd
Type ':help' or ':h' for help.
Display stack trace? [yN]

Is there some other solution ?

# vertex does not exists
gremlin> g.V().coalesce(hasId('e6b5951c-c918-4916-b0a6-f4c9468fb25d'), g.addV('foo').property(id, 'e6b5951c-c918-4916-b0a6-f4c9468fb25d'))
==>v[e6b5951c-c918-4916-b0a6-f4c9468fb25d]

That is fine...

Thanks,

JP

Excerpts from Yehonathan Sharvit's message of February 18, 2018 11:34 am:
> --
> You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Daniel Kuppitz

unread,
Mar 7, 2018, 2:06:25 PM3/7/18
to gremli...@googlegroups.com
Your query should be:

g.V().hasId('8f1f2c14-bd12-4ec3-9b2d-019f9242b2dd').
  fold().
  coalesce(unfold(),
           addV('foo').
             property(id, '8f1f2c14-bd12-4ec3-9b2d-019f9242b2dd'))

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/1520414290.lf2g70xbj0.astroid%40gnion.none.

Jean-Philippe B

unread,
Mar 8, 2018, 3:35:29 AM3/8/18
to gremli...@googlegroups.com
Thanks Daniel, didn't tought about that, but it makes sense

JP

Excerpts from Daniel Kuppitz's message of March 7, 2018 8:06 pm:
>>> email to gremlin-user...@googlegroups.com.
>>> To view this discussion on the web visit https://groups.google.com/d/ms
>>> gid/gremlin-users/6c6080bd-a1fa-4f08-8e46-68eeb08512e3%40googlegroups.com
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Gremlin-users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to gremlin-user...@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/ms
>> gid/gremlin-users/1520414290.lf2g70xbj0.astroid%40gnion.none.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/CA%2Bf9seUuKSfc8B5vySABzzB8vdSjgZxDcgNJ63nME5Db6wbrpg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages