Neo4j:MERGE 创建重复节点 [英] Neo4j: MERGE creates duplicate nodes

查看:30
本文介绍了Neo4j:MERGE 创建重复节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库模型有用户和 MAC 地址.一个用户可以有多个MAC地址,但一个MAC只能属于一个用户.如果某个用户设置了他的 MAC,并且该 MAC 已经链接到另一个用户,则现有关系将被删除,并在新所有者和该 MAC 之间创建新关系.换句话说,MAC 在用户之间移动.

My database model has users and MAC addresses. A user can have multiple MAC addresses, but a MAC can only belong to one user. If some user sets his MAC and that MAC is already linked to another user, the existing relationship is removed and a new relationship is created between the new owner and that MAC. In other words, a MAC moves between users.

这是我用来分配 MAC 地址的 Cypher 查询的一个特定实例:

This is a particular instance of the Cypher query I'm using to assign MAC addresses:

MATCH (new:User { Id: 2 })
MERGE (mac:MacAddress { Value: "D857EFEF1CF6" })
WITH new, mac
OPTIONAL MATCH ()-[oldr:MAC_ADDRESS]->(mac)
DELETE oldr
MERGE (new)-[:MAC_ADDRESS]->(mac)

查询在我的测试中运行良好,但在生产中,由于某种奇怪的原因,它有时创建重复的 MacAddress 节点(以及用户和每个节点之间的新关系)那些节点).也就是说,一个特定的用户可以有多个具有相同 ValueMacAddress 节点.

The query runs fine in my tests, but in production, for some strange reason it sometimes creates duplicate MacAddress nodes (and a new relationship between the user and each of those nodes). That is, a particular user can have multiple MacAddress nodes with the same Value.

我可以说它们是不同的节点,因为它们具有不同的节点 ID.我也确定 Value 完全相同,因为我可以对它们执行 collect(distinct mac.Value) 并且结果是一个包含一个元素的集合.上面的查询是代码中唯一一个创建 MacAddress 节点的查询.

I can tell they are different nodes because they have different node ID's. I'm also sure the Values are exactly the same because I can do a collect(distinct mac.Value) on them and the result is a collection with one element. The query above is the only one in the code that creates MacAddress nodes.

我使用的是 Neo4j 2.1.2.这是怎么回事?

I'm using Neo4j 2.1.2. What's going on here?

谢谢,扬

推荐答案

这是我从 Neo4j 的支持中得到的回复(重点是我的):

This is the response I got back from Neo4j's support (emphasis mine):

我已经从我们的团队那里得到了一些反馈,目前已知这可能在没有约束的情况下发生.MERGE 实际上是 MATCH 或 CREATE - 这两个步骤在事务中独立运行.给定并发执行和读提交"隔离级别,两者之间存在竞争条件.

I got some feedback from our team already, and it's currently known that this can happen in the absence of a constraint. MERGE is effectively MATCH or CREATE - and those two steps are run independently within the transaction. Given concurrent execution, and the "read committed" isolation level, there's a race condition between the two.

团队已经讨论了如何在并发情况下提供更高的保证,并确实将其作为功能请求进行了考虑.

The team have done some discussion on how to provided a higher guarantee in the face of concurrency, and do have it noted as a feature request for consideration.

与此同时,他们向我保证,使用约束将提供您正在寻找的唯一性.

Meanwhile, they've assured me that using a constraint will provide the uniqueness you're looking for.

这篇关于Neo4j:MERGE 创建重复节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆