Gremlin-如果超出限制,则添加新顶点 [英] Gremlin - adding new vertex if it exceeds the limit
问题描述
样本数据:
我有两个名为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点
那么我需要向
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.
我如何实现这一目标.
谢谢.
推荐答案
- 选择具有最低
totalPoints
值的用户Points
顶点. - 将
totalPoints
与新的点数相加. - 如果总和超过500,请将
totalPoints
属性值设置为500,并添加一个新的Points
顶点,其totalPoints
值为sum-500
. - 如果总和不超过500,请将其设置为新的
totalPoints
属性值.
- Pick the user's
Points
vertex with the lowesttotalPoints
value. - Sum the
totalPoints
with the new number of points. - If the sum exceeds 500, set the
totalPoints
property value to 500 and add a newPoints
vertex with atotalPoints
value ofsum-500
. - 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屋!