Integer sequence generator in Neo4J/APOC?

Our current system uses an oracle sequence and grabs a value to populate into some of our Neo4J graph nodes. I'm at a point where I may be able to refactor some of the inner-workings of some of this code, but I still may have to retain the integer sequence value for backward compatibility. This call to Oracle has always bothered me. I'm wondering if there is now a mechanism for doing this internal to Neo4J. I remember chatting with @michael.hunger about this on the defunct Neo4J slack channel over a year ago and it was mentioned the counters existed internally in a cluster safe manner and possibly this could be exposed via APOC as a general purpose sequence generator. After browsing the APOC docs, I don't see that this has been added. Can anyone update me on the current state of this type of functionality? I'm hoping it already exists and I'm overlooking it. Does it really exist internally? Safe for HA or Causal cluster? exposed via APOC? or could be leveraged via a custom function?

Thanks

1 Like

I think the best was is to use apoc.atomic.add:
1st, create the sequence node:
CREATE (s:Sequence {key:"my_seq", value: 0})

Then, use this to get new values:
MATCH (s:Sequence {key:"my_seq"})
CALL apoc.atomic.add(s,'value',1,10)
YIELD newValue as seq
RETURN seq

1 is the number you want to add to your sequence, 10 is how many times to retry if value is locked

Or you can use the sequence in your query when creating/updating a value:
MATCH (s:Sequence {key:"my_seq"})
CALL apoc.atomic.add(s,'value',1,10)
YIELD newValue as seq
MERGE (p:Person { pid : "Person1"})
on create set p.somedata = seq
on match set p.somedata = seq
return p

I think the last cypher should NOT include this line:

on match set p.somedata = seq

and instead read:

MATCH (s:Sequence {key:"my_seq"})
CALL apoc.atomic.add(s,'value',1,10)
YIELD newValue as seq
MERGE (p:Person { pid : "Person1"})
on create set p.somedata = seq
return p

If the merge is not creating a new record then we can just leave the data value as it was.