azuriousdown
azuriousdown

Reputation: 23

How to increment an edge property counter in Gremlin graph

I have a graph with edges that each have a "count" property, for the number of times that transition is made between its two vertices. I want to increment the value whenever that transition is made; is there an efficient way to do this?

I want to avoid reading the value, incrementing, and then updating, because the increments may come from multiple sources and I want to avoid a race condition scenario.

I have a query that does a similar thing, but only seems to work for vertex properties:

g.V('10b56175-b3e5-db7c-6d1f-04f9fe7caba8').property(single, 'count', union(values('count'), constant(1.0)).sum()).valueMap()

Is there any way to make the above query compatible with edge properties?

Thanks!

Upvotes: 2

Views: 791

Answers (1)

stephen mallette
stephen mallette

Reputation: 46216

It works fine on edges, but there is no overload for property() with Cardinality for edges.

gremlin> g.E(11).property('count', union(values('count'), constant(1.0)).sum()).valueMap()
==>[count:1.0,weight:0.4]
gremlin> g.E(11).property('count', union(values('count'), constant(1.0)).sum()).valueMap()
==>[count:2.0,weight:0.4]
gremlin> g.E(11).property('count', union(values('count'), constant(1.0)).sum()).valueMap()
==>[count:3.0,weight:0.4]

I don't think you can make your increment happen any nicer directly through Gremlin. There will always be a read before write, unless your underlying graph database has some native method for doing counters.

Upvotes: 1

Related Questions