Gremlin-如果超出限制,则添加新顶点 [英] Gremlin - adding new vertex if it exceeds the limit

查看:143
本文介绍了Gremlin-如果超出限制,则添加新顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

样本数据:

我有两个名为User,点的顶点

I have two vertices by names User , Points

首先为顶点用户

g.addV('User').property('id',1).
  addV('User').property('id',2).
  addV('User').property('id',3).iterate()

现在添加 Points 顶点并将 addingPoints Edge从 User 连接到 Points

Now adding Points vertices and connecting addingPoints Edge from User to Points

g.V().hasLabel('User').has('id',1).as('curUser1').
  V().hasLabel('User').has('id',2).as('curUser2').
  V().hasLabel('User').has('id',3).as('curUser3').
  addV('Points').property('totalPoints',0).property('userPoints',0).
  property('createDate',1560316666).property('name','user1').
  addE('addingPoints').from('curUser1').
  addV('Points').property('totalPoints',0).property('userPoints',0).
  property('createDate',1560318666).property('name','user2').
  addE('addingPoints').from('curUser2').
  addV('Points').property('totalPoints',0).property('userPoints',0).
  property('createDate',1560318657).property('name','user3').
  addE('addingPoints').from('curUser3').iterate()

现在每个用户具有至少一个 Points 个顶点.

Now each User is having atleast one Points vertex.

现在,我想向 id 为1

在添加点时,我有以下三种情况:

while adding the points, I have three cases:

1.如果 totalPoints 为lt500,那么我只需要使用 id 为1.

1.If totalPoints are lt500 Then I just need to update the totalPoints property of Points vertex of user with id as 1.

2.如果 totalPoints 是eq500,那么我应该创建新的 Points 顶点并将点添加到 Points totalPoints 属性中 id 为1的用户的strong>顶点.

2.If totalPoints are eq500 Then I should create new Points vertex and add points to totalPoints property of Points vertex of user with id as 1.

3.如果 totalPoints 是490,则不是eq500,而是lt500.但是现在,如果我需要在 totalPoints 属性中添加30点 那么我需要向 id 为1的用户的旧 Points 顶点中添加10点,然后将剩余的20点添加到新的 Points 顶点中 id 为1的用户的数量.

3.If totalPoints are 490 which is not eq500 but lt500. But now if I need to add 30 points to the totalPoints property then I need to add 10 points to the old Points vertex of user with id as 1 and I should add remaining 20 points to new Points vertex of user with id as 1.

我如何实现这一目标.

谢谢.

推荐答案

  1. 选择具有最低totalPoints值的用户Points顶点.
  2. totalPoints与新的点数相加.
  3. 如果总和超过500,请将totalPoints属性值设置为500,并添加一个新的Points顶点,其totalPoints值为sum-500.
  4. 如果总和不超过500,请将其设置为新的totalPoints属性值.
  1. Pick the user's Points vertex with the lowest totalPoints value.
  2. Sum the totalPoints with the new number of points.
  3. If the sum exceeds 500, set the totalPoints property value to 500 and add a new Points vertex with a totalPoints value of sum-500.
  4. If the sum doesn't exceed 500, set it as the new totalPoints property value.

将这4个步骤翻译成遍历:

These 4 steps translated into a traversal:

g.withSack(points).
  V().has('User','id',user).as('u').
    out('addingPoints').
    order().
      by('totalPoints').
    limit(1).
    sack(sum).
      by('totalPoints').
    choose(sack().is(gt(maxPoints)),
             sack(minus).
               by(constant(maxPoints)).
             property('totalPoints', maxPoints).
             addV('Points').
             sideEffect(addE('addingPoints').
                          from('u'))).
    property('totalPoints', sack())

还有一个小型控制台示例(我用totalPoints=400初始化了第一个Points顶点,并用totalPoints=480初始化了第二个Points顶点):

And a small console example (I initialized the first Points vertex with totalPoints=400 and the second Points vertex with totalPoints=480):

gremlin> showUserPoints = {
......1>   g.V().as('u').out('addingPoints').
......2>     group().
......3>       by(select('u').by('id')).
......4>       by('totalPoints').next()
......5> }
==>groovysh_evaluate$_run_closure1@7c2b58c0

gremlin> addPoints = { user, points, maxPoints = 500 ->
......1>   g.withSack(points).
......2>     V().has('User','id',user).as('u').
......3>       out('addingPoints').
......4>       order().
......5>         by('totalPoints').
......6>       limit(1).
......7>       sack(sum).
......8>         by('totalPoints').
......9>       choose(sack().is(gt(maxPoints)),
.....10>                sack(minus).
.....11>                  by(constant(maxPoints)).
.....12>                property('totalPoints', maxPoints).
.....13>                addV('Points').
.....14>                sideEffect(addE('addingPoints').
.....15>                             from('u'))).
.....16>       property('totalPoints', sack()).iterate()
.....17> 
.....17>   showUserPoints()
.....18> }
==>groovysh_evaluate$_run_closure1@31d6f3fe

gremlin> showUserPoints()
==>1=[400]
==>2=[480]
==>3=[0]

gremlin> addPoints(1, 10)
==>1=[410]
==>2=[480]
==>3=[0]
gremlin> addPoints(1, 90)
==>1=[500]
==>2=[480]
==>3=[0]
gremlin> addPoints(2, 30)
==>1=[500]
==>2=[500, 10]
==>3=[0]
gremlin> addPoints(2, 40)
==>1=[500]
==>2=[500, 50]
==>3=[0]
gremlin> addPoints(3, 100)
==>1=[500]
==>2=[500, 50]
==>3=[100]

这篇关于Gremlin-如果超出限制,则添加新顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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