Gremlin/Tinkerpop中是否支持顶点属性的原子递增操作?
TinkerPop 顶点属性原子递增实现方案
完全可行,TinkerPop支持原子性的属性递增操作,用math()结合property()就能实现,这种缓存计数的方式能有效规避count()遍历带来的性能问题。
新增文档时的原子更新代码
g.V('1234').as('folder') // 原子递增documentCount,首次操作默认从0开始 .property('single', 'documentCount', coalesce(values('documentCount'), constant(0)).math('_ + 1')) // 原子递增iNodeCount .property('single', 'iNodeCount', coalesce(values('iNodeCount'), constant(0)).math('_ + 1')) .addV('iNode').as('document') .property('single', 'type', 'document') .addE('contains').from('folder').to('document')
新增子文件夹时的原子更新代码
g.V('1234').as('folder') // 原子递增folderCount .property('single', 'folderCount', coalesce(values('folderCount'), constant(0)).math('_ + 1')) // 原子递增iNodeCount .property('single', 'iNodeCount', coalesce(values('iNodeCount'), constant(0)).math('_ + 1')) .addV('iNode').as('childFolder') .property('single', 'type', 'folder') .addE('contains').from('folder').to('childFolder')
核心逻辑说明
coalesce(values('xxx'), constant(0)):处理属性未初始化的场景,保证第一次递增时默认值为0math('_ + 1'):基于当前属性值执行原子性加1,底层由图数据库的事务机制保证操作的原子性(前提是数据库本身支持原子操作)- 这种缓存计数的方案非常适合性能敏感场景,避免每次查询都遍历边统计数量
内容的提问来源于stack exchange,提问作者Adam
相关产品推荐
相关产品推荐

