2017-04-10 27 views
0

問題暗號鏈表併發插入

我試圖用暗號和Neo4j的建設linked list。如果插入一次發生,該查詢將起作用。

但是,如果插入發生在同一時間然後linked list變得一團糟。

插圖 藍色節點代表一個組。 黃色節點代表帖子。這些帖子應作爲鏈接列表連接到組。

(GROUP)-[LAST_POST]->(POST)-[PREVIOUS_POST]->(POST)-...->(POST) 

enter image description here

我明白,我們不能鎖定寫做一個寫的時候,因爲這會影響性能。有沒有辦法強制執行一個LAST_POST關係?

如果你想保持連接老尾...(POST)-[PREVIOUS_POST]->(POST)-...->(POST)我使用

MATCH(group:group{id:{groupId}}) 
OPTIONAL MATCH (group)-[r:LAST_POST]->(oldPost) 
DELETE r      
merge (post:post{id: {postId}, type:{type}}) 
CREATE (group)-[:LAST_POST]->(post) 
WITH post, collect(oldPost) as oldLatestPosts 
FOREACH (x in oldLatestPosts|CREATE (post)-[:PREVIOUS_POST]->(x)) 
RETURN post 

回答

1

我通過鎖定節點並釋放一次我完成我的操作解決了這個問題。

查詢看起來應該像下面這樣:

MATCH(group:group{id:{groupId}}) 
SET group.__lock = true      
WITH group 
OPTIONAL MATCH (group)-[r:LAST_POST]->(oldPost) 
DELETE r      
merge (post:post{id: {postId}, type:{type}}) 
CREATE (group)-[:LAST_POST]->(post) 
WITH group, post, collect(oldPost) as oldLatestPosts 
FOREACH (x in oldLatestPosts|CREATE (post)-[:PREVIOUS_POST]->(x)) 
SET group.__lock = false 
RETURN post 
0

查詢,然後將舊錶的第一個元素匹配和元件連接到新的頭部後是什麼你想要做的。

MATCH(group:group{id:{groupId}}) 
OPTIONAL MATCH (group)-[r:LAST_POST]->(oldPost) 
DELETE r      
MERGE (group)-[:LAST_POST]->(post:post{id: {postId}, type:{type}}) 
WITH post, oldPost 
CREATE (post)-[:PREVIOUS_POST]->(oldPost) 
RETURN post