分析两个顶点格蕾姆林之间的边缘

时间:2019-02-05 04:41:55

标签: graph-databases gremlin amazon-neptune

这是我的图

g.addV('user').property('id',1).as('1').
  addV('user').property('id',2).as('2').
  addV('user').property('id',3).as('3').
  addE('follow').from('1').to('2').
  addE('follow').from('1').to('3').iterate()

以下是当用户想要关注另一个用户并假设2想关注3时的我的方法

我首先检查2和3之间是否存在跟随边缘

if(g.V().has(id, 2).outE(follow).inV().has(id, 3).hasNext())
 {
   //if exists that means he already following him so i'm dropping the follow edge and adding unfollow edge to 2,3.
 }
 else if(g.V().has(id, 2).outE(unfollow).inV().has(id, 3).hasNext())
 {
   //if exists he already unfollowed him and he wants to follow him again i'm dropping the unfollow edge and adding the follow edge to 2,3.
 }
 else 
 {
   // there is no edges between 2,3 so he is following him first so i'm adding follow edge 2,3.
 }

但是这种方法的缺点是每次需要查询2次,这都会影响性能。你能建议我一个更好的方法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用choose()构建if-then-else语义。您的逻辑在那里直接翻译可能像这样:

gremlin> g.addV('user').property(id,1).as('1').
......1>   addV('user').property(id,2).as('2').
......2>   addV('user').property(id,3).as('3').
......3>   addE('follow').from('1').to('2').
......4>   addE('follow').from('1').to('3').iterate()
gremlin> g.V(3).as('target').
......1>   V(2).as('source').
......2>   choose(outE('follow').aggregate('d1').inV().hasId(3), 
......3>            sideEffect(addE('unfollow').from('source').to('target').
......4>                       select('d1').unfold().drop()).constant('unfollowed'),
......5>          choose(outE('unfollow').aggregate('d2').inV().hasId(3),
......6>                   sideEffect(addE('follow').from('source').to('target').
......7>                              select('d2').unfold().drop()).constant('followed'),
......8>                 addE('follow').from('source').to('target').constant('followed-first')))
==>followed-first
gremlin> g.E()
==>e[0][1-follow->2]
==>e[1][1-follow->3]
==>e[2][2-follow->3]
gremlin> g.V(3).as('target').
......1>   V(2).as('source').
......2>   choose(outE('follow').aggregate('d1').inV().hasId(3), 
......3>            sideEffect(addE('unfollow').from('source').to('target').
......4>                       select('d1').unfold().drop()).constant('unfollowed'),
......5>          choose(outE('unfollow').aggregate('d2').inV().hasId(3),
......6>                   sideEffect(addE('follow').from('source').to('target').
......7>                              select('d2').unfold().drop()).constant('followed'),
......8>                 addE('follow').from('source').to('target').constant('followed-first')))
==>unfollowed
gremlin> g.E()
==>e[0][1-follow->2]
==>e[1][1-follow->3]
==>e[3][2-unfollow->3]
gremlin> g.V(3).as('target').
......1>   V(2).as('source').
......2>   choose(outE('follow').aggregate('d1').inV().hasId(3), 
......3>            sideEffect(addE('unfollow').from('source').to('target').
......4>                       select('d1').unfold().drop()).constant('unfollowed'),
......5>          choose(outE('unfollow').aggregate('d2').inV().hasId(3),
......6>                   sideEffect(addE('follow').from('source').to('target').
......7>                              select('d2').unfold().drop()).constant('followed'),
......8>                 addE('follow').from('source').to('target').constant('followed-first')))
==>followed
gremlin> g.E()
==>e[0][1-follow->2]
==>e[1][1-follow->3]
==>e[4][2-follow->3]