gpt4 book ai didi

ruby - 创建不会为 Titan 复制的 addEdge() Gremlin 查询

转载 作者:数据小太阳 更新时间:2023-10-29 07:14:23 25 4
gpt4 key购买 nike

有没有办法在 Titan 图上的两个顶点之间创建唯一边并确认它不能再次创建,除非删除并重新创建?

基本上我需要创建:

vertex1--follows-->vertex2

但我一直为同一个关系创建多个边:

vertex1--follows-->vertex2
vertex1--follows-->vertex2
vertex1--follows-->vertex2
vertex1--follows-->vertex2

我的基本 addEdge 查询是这样的:

def follow(target)
grem = "g.addEdge(
g.V('id', '#{id}').next(),
g.V('id', '#{target.id}').next(),
'follows',
[since:#{Time.now.year}]
)"

$graph.execute(grem).results
end

我想找的是这样的东西

def follow(target)
grem = "g.addEdge(
g.V('id', '#{id}').next(),
g.V('id', '#{target.id}').next(),
'follows',
[since:#{Time.now.year}]
).unique(Direction.OUT)"

$graph.execute(grem).results
end

在本文档中有一个名为 unique 的方法,但我似乎无法让它在边上工作,只能在顶点的属性上工作。

https://github.com/thinkaurelius/titan/wiki/Type-Definition-Overview

我可以在创建 addEdge 之前运行一个查询来检查现有的边缘,但这看起来很老套并且可能导致竞争条件问题。

是否有可能存在一种方法,我可以将其附加到 addEdge 上,如果边已经存在,这将防止创建重复的边?

或者,有没有办法在边上创建一个独特的属性标签?

这是问题的 gremlin session :

gremlin>  g.makeType().name('follows').unique(IN).makeEdgeLabel();
==>v[36028797018964558]
gremlin> u = g.addVertex([name:'brett'])
==>v[120004]
gremlin> u2 = g.addVertex([name:'brettU'])
==>v[120008]
gremlin> e = g.addEdge(u, u2, 'follows')
==>e[2w5N-vdy-2F0LaTPQK2][120004-follows->120008]
gremlin> e = g.addEdge(u, u2, 'follows')
An edge with the given type already exists on the in-vertex
Display stack trace? [yN]
gremlin> e = g.addEdge(u2, u, 'follows')
==>e[2w5P-vdC-2F0LaTPQK2][120008-follows->120004]
gremlin> u3 = g.addVertex([name:'brett3'])
==>v[120012]
gremlin> e = g.addEdge(u3, u, 'follows')
An edge with the given type already exists on the in-vertex
Display stack trace? [yN] N
gremlin> g.E
==>e[2w5N-vdy-2F0LaTPQK2][120004-follows->120008]
==>e[2w5P-vdC-2F0LaTPQK2][120008-follows->120004]

设置 unique(IN|BOTH|OUT) 会产生一个问题,即每个用户只能有一个关注者。这当然会使 user -> follows -> [users] 关系不可能存在。

这是尝试在边上设置唯一属性的另一个示例,这也失败了:

gremlin> g.makeType().name('follows_id').unique(BOTH).makeEdgeLabel();
==>v[36028797018964942]
gremlin> u = g.addVertex([name:'brett'])
==>v[200004]
gremlin> u2 = g.addVertex([name:'brett2'])
==>v[200008]
gremlin> u3 = g.addVertex([name:'brett3'])
==>v[200012]
gremlin> e = g.addEdge(u, u2, 'follows', [follows_id:'200004-20008'])
Value must be a vertex
Display stack trace? [yN] N
gremlin> g.E
==>e[4c9z-Q1S-2F0LaTPQQu][200004-follows->200008]
gremlin> e = g.addEdge(u, u2, 'follows', [follows_id:'200004-20008'])
Value must be a vertex
Display stack trace? [yN] N
gremlin> g.E
==>e[4c9z-Q1S-2F0LaTPQQu][200004-follows->200008]
==>e[4c9B-Q1S-2F0LaTPQQu][200004-follows->200008]

最佳答案

为了在这里结束循环,这个问题在 Aurelius Graphs Mailing List 中得到了回答。 .基本上:

we don't really see a use case for a uniqueness constraints to apply to pairs of vertices (a la - only one edge can exist between vertex A and B) for these reasons:

  • most times, you can get rid of the duplication quite cheaply on the query side with a dedup(): v.out('follows').dedup().....
  • the likelihood of conflict is much lower (due to the N^2 combinations of vertices) which makes locks just waaaay to expensive compared to the likelihood of conflict.

简而言之,您应该验证应用程序中是否存在边缘,因为 Titan 无法强制执行。

关于ruby - 创建不会为 Titan 复制的 addEdge() Gremlin 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18775831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com