Gremlin - 如果超过限制则添加新顶点
Gremlin - adding new vertex if it exceeds the limit
示例数据:
我有两个名为 User 和 Points 的顶点
首先为顶点添加数据用户
g.addV('User').property('id',1).
addV('User').property('id',2).
addV('User').property('id',3).iterate()
现在添加 Points 个顶点并连接 addingPoints 边从 User 到 分
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()
现在每个 User 至少有一个 Points 顶点。
现在我想给 id[=60 用户的 totalPoints 属性 随机添加 10(或)20(或)30 个点=] 为 1
在加分的时候,我有三种情况:
1.If totalPoints 是 lt500 然后我只需要更新 totalPoints 属性 of 点用户的顶点id为1.
2.If totalPoints 是 eq500 然后我应该创建新的 Points 顶点并将点添加到 totalPoints 属性 of Points user with id as 1.
3.If totalPoints 是 490,不是 eq500,而是 lt500。但是现在如果我需要在 totalPoints 属性 中添加 30 个点
然后我需要向用户的旧 Points 顶点添加 10 个点,id 为 1,我应该将剩余的 20 个点添加到新的 点用户的顶点id为1.
我怎样才能做到这一点。
谢谢。
- 选择用户的
Points
具有最低 totalPoints
值的顶点。
- 将
totalPoints
与新的点数相加。
- 如果总和超过 500,将
totalPoints
属性 值设置为 500 并添加一个 totalPoints
值为 sum-500
的新 Points
顶点.
- 如果总和不超过 500,则将其设置为新的
totalPoints
属性 值。
这 4 个步骤转化为遍历:
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
顶点:
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]
示例数据:
我有两个名为 User 和 Points 的顶点
首先为顶点添加数据用户
g.addV('User').property('id',1).
addV('User').property('id',2).
addV('User').property('id',3).iterate()
现在添加 Points 个顶点并连接 addingPoints 边从 User 到 分
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()
现在每个 User 至少有一个 Points 顶点。
现在我想给 id[=60 用户的 totalPoints 属性 随机添加 10(或)20(或)30 个点=] 为 1
在加分的时候,我有三种情况:
1.If totalPoints 是 lt500 然后我只需要更新 totalPoints 属性 of 点用户的顶点id为1.
2.If totalPoints 是 eq500 然后我应该创建新的 Points 顶点并将点添加到 totalPoints 属性 of Points user with id as 1.
3.If totalPoints 是 490,不是 eq500,而是 lt500。但是现在如果我需要在 totalPoints 属性 中添加 30 个点 然后我需要向用户的旧 Points 顶点添加 10 个点,id 为 1,我应该将剩余的 20 个点添加到新的 点用户的顶点id为1.
我怎样才能做到这一点。
谢谢。
- 选择用户的
Points
具有最低totalPoints
值的顶点。 - 将
totalPoints
与新的点数相加。 - 如果总和超过 500,将
totalPoints
属性 值设置为 500 并添加一个totalPoints
值为sum-500
的新Points
顶点. - 如果总和不超过 500,则将其设置为新的
totalPoints
属性 值。
这 4 个步骤转化为遍历:
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
顶点:
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]