Search code examples
gremlintinkerpopamazon-neptune

Is there a way to do an atomic increment of a property in Gremlin / Tinkerpop?


I've read thru the Tinkerpop documentation but I don't see (or I missed) a way to do atomic incrementing of properties on a vertex.

I'd like to do something like adding a document to a folder and atomically update a property to cache counts

g.V('1234').as('folder')
 //how? .property('single','documentCount', documentCount++) 
 //how? .property('single','iNodeCount', iNodeCount++) 
 .addV('iNode').as('document')
 .property('single','type','document')
 .addE('contains').from('folder').to('document')
  

and then could also cache a folder count

g.V('1234').as('folder')
 //how? .property('single','folderCount', folderCount++)
 //how? .property('single','iNodeCount', iNodeCount++) 
 .addV('iNode').as('childFolder')
 .property('single','type','folder')
 .addE('contains').from('folder').to('childFolder')

This would help avoid doing count() operations when requiring the counts.

Is this possible?


Solution

  • You can implement such a thing with sack() step - here's an example:

    gremlin> g = TinkerGraph.open().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
    gremlin> v = g.addV('folder').property('documentCount',0).next()
    ==>v[0]
    gremlin> g.V(v).sack(assign).by('documentCount').sack(sum).by(constant(1)).property('documentCount', sack())
    ==>v[0]
    gremlin> g.V(v).sack(assign).by('documentCount').sack(sum).by(constant(1)).property('documentCount', sack())
    ==>v[0]
    gremlin> g.V(v).elementMap()
    ==>[id:0,label:folder,documentCount:2]
    gremlin> g.V(v).sack(assign).by('documentCount').sack(sum).by(constant(1)).property('documentCount', sack())
    ==>v[0]
    gremlin> g.V(v).elementMap()
    ==>[id:0,label:folder,documentCount:3]