Generating friendly id sequence in Neo4j

耗尽温柔 提交于 2019-12-06 13:27:57

问题


I have a bunch of relationships where I would like to create a friendly id for the child objects so that I can have friendlier URLs.

Every time I create a child object for a particular parent object I would like to automatically include a friendly id which increments sequentially. So the first child would have friendly id 1, the second would have friendly id 2, etc. I don't want to reuse id's if children are deleted. To complicate matters, there are many such relationships that I would like to do this for.

Currently I'm caching some state on the parent node and using that to populate the friendly id when creating children:

CREATE (o:Foo {name: 'parent', nextChildId: 1})

MATCH (o:Foo)
WHERE o.name = 'parent'
CREATE (o)-[:HAS]->(c:Child {name: 'child1', friendlyId: o.nextChildId})
SET o.nextChildId = o.nextChildId + 1

MATCH (o:Foo)
WHERE o.name = 'parent'
CREATE (o)-[:HAS]->(c:Child {name: 'child2', friendlyId: o.nextChildId})
SET o.nextChildId = o.nextChildId + 1

The problem is that there is a chance that two children will end up with the same friendly id since two clients could try and create children at the same time. I'm not sure how to prevent this from happening.


回答1:


For anyone else that is looking to do this, I got a workaround from the Neo4j Google group. The trick is to create a lock on the node by removing a non-existent property. This serializes access to the node and prevents multiple children from getting the same id.

CREATE (o:Foo {name: 'parent', nextChildId: 1})

MATCH (o:Foo)
WHERE o.name = 'parent'
REMOVE o.lock  // non-existent property
CREATE (o)-[:HAS]->(c:Child {name: 'child1', friendlyId: o.nextChildId})
SET o.nextChildId = o.nextChildId + 1



回答2:


Neo4j is fully ACID compliant. You can run this in a transaction with SERIALIZABLE isolation level and acquiring the lock on the node of interest. Remember however that this might be degrade the performance.



来源:https://stackoverflow.com/questions/18391681/generating-friendly-id-sequence-in-neo4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!