Rethinkdb 原子操作 [英] Rethinkdb atomic operations

查看:69
本文介绍了Rethinkdb 原子操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个文档

{
    id: 1,
    fruits: []
}

这里的水果充当 SET

fruits here acts as a SET

现在我想原子地为带有主键 = 1 的文档添加一个值到水果数组 OR 如果它不存在则创建这样的文档(即在下面使用 SetInsert ReQL引擎盖)

Now I want to atomically add a value to fruits array for document with primary key = 1 OR create such document if it does not exist(i.e. use SetInsert ReQL under the hood)

我也需要为 increment(ReQL .Add) 做同样的事情

I also need to do the same for increment(ReQL .Add)

显然这不能在客户端代码中完成,因为它破坏了原子性并且我最终得到了不一致的数据

Obviously this can't be done in client code as it breaks atomicity and I end up with inconsistent data

我希望这样的事情成为可能

I wish something like this was possible

r.table('results').insert({
  id: '62c70132-6516-4279-9803-8daf407ffa5c',
  counter: r.row('counter').add(1).default(0)
}, {conflict: "update"})

但它以RqlCompileError: r.row is not defined in this context in"而死亡

but it dies with "RqlCompileError: r.row is not defined in this context in"

感谢任何帮助/指导,谢谢!

Any help/guidance is appreciated, thanks!

推荐答案

目前使用 insert 无法做到这一点.另一个提到的解决方案不是原子的,因为它使用子查询.我们正在 https://github.com/rethinkdb/rethinkdb/issues 中为此制定解决方案/3753 .

This is not possible with insert at the moment. The other mentioned solution is not atomic because it uses a subquery. We're working on a solution for this in https://github.com/rethinkdb/rethinkdb/issues/3753 .

然而,您可以使用 replace 来执行原子更新插入:

You can however use replace to perform an atomic upsert:

r.table('results').get('62c70132-6516-4279-9803-8daf407ffa5c')
 .replace({
  id: '62c70132-6516-4279-9803-8daf407ffa5c',
  counter: r.row('counter').add(1).default(0)
})

如果文档不存在,

replace 实际上会执行插入操作.

replace will actually perform an insert if the document doesn't exist.

这篇关于Rethinkdb 原子操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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