【问题标题】:Is there a way to do an atomic increment of a property in Gremlin / Tinkerpop?有没有办法在 Gremlin / Tinkerpop 中对属性进行原子增量?
【发布时间】:2023-01-15 08:05:52
【问题描述】:

我已经通读了 Tinkerpop 文档,但我没有看到(或者我错过了)一种在顶点上对属性进行原子递增的方法。

我想做一些事情,比如将文档添加到文件夹并自动更新属性以缓存计数

g.V('1234').as('folder')
 //how? .property('single','documentCount', documentCount++) 
 //how? .property('single','iNodeCount', iNodeCount++) 
 .addV('iNode').as('document')
 .property('single','type','document')
 .addE('contains').from('folder').to('document')
  

然后还可以缓存文件夹计数

g.V('1234').as('folder')
 //how? .property('single','folderCount', folderCount++)
 //how? .property('single','iNodeCount', iNodeCount++) 
 .addV('iNode').as('childFolder')
 .property('single','type','folder')
 .addE('contains').from('folder').to('childFolder')

这将有助于避免在需要计数时执行 count() 操作。

这可能吗?

【问题讨论】:

    标签: gremlin tinkerpop amazon-neptune aws-neptune


    【解决方案1】:

    你可以用sack()step来实现这样的事情——这是一个例子:

    gremlin> g = TinkerGraph.open().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
    gremlin> v = g.addV('folder').property('documentCount',0).next()
    ==>v[0]
    gremlin> g.V(v).sack(assign).by('documentCount').sack(sum).by(constant(1)).property('documentCount', sack())
    ==>v[0]
    gremlin> g.V(v).sack(assign).by('documentCount').sack(sum).by(constant(1)).property('documentCount', sack())
    ==>v[0]
    gremlin> g.V(v).elementMap()
    ==>[id:0,label:folder,documentCount:2]
    gremlin> g.V(v).sack(assign).by('documentCount').sack(sum).by(constant(1)).property('documentCount', sack())
    ==>v[0]
    gremlin> g.V(v).elementMap()
    ==>[id:0,label:folder,documentCount:3]
    

    【讨论】:

    • 斯蒂芬,这就像一个魅力。这是保证原子的吗?我需要担心并发吗?
    • 好吧,这是 Gremlin 的答案,但行为将特定于实现。我看到你用 Neptune 标记了你的问题,所以我假设你正在使用它。我没有对此进行准确测试,但使用海王星文档中描述的这种模式应该可以锁定要替换的属性键,从而防止并发事务:docs.aws.amazon.com/neptune/latest/userguide/…
    • 嗯,好吧,就像这样: g.V(folderID) .sack(assign).by('documentCount').sack(sum).by(constant(1)) .property(single, 'documentCount', sack()) .as('folder') .addV('document').as('document') .addE('contains').from('folder').to('document') 根据文档,Neptune 认为这是一个突变查询,因为它包含“addV”和“addE”,所以它以可重复读取方式运行。听起来这样可以确保 (assign).by(documentCount) 读取的内容是安全的。这一切听起来准确吗?
    • 实际上,它说“Neptune 提供了强有力的保证,即 NON-REPEATABLE 和 PHANTOM 读取都不会发生”,但是 Mutation Query 以 READ COMMITTED 运行,而不是 REPEATABLE READ。这让我认为不能保证计算出的 documentCount 是准确的。
    • 在仔细考虑并通读之后,我相信 READ COMMITED 足以确保安全。来自 docs“换句话说,当一个索引的范围被突变事务读取时,有一个强有力的保证,这个范围不会被任何并发事务修改,直到读取事务结束。这保证没有非- 将发生可重复读取。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多