Rethinkdb原子操作

Rethinkdb atomic operations

假设我有一个文档

{
    id: 1,
    fruits: []
}

这里的fruits作为一个SET

现在我想 自动 为主键 = 1 的文档添加一个值到 fruits 数组 或者 创建这样的文档不存在(即在后台使用 SetInsert ReQL)

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

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

我希望这样的事情是可能的

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"

任何 help/guidance 表示赞赏, 谢谢!

在 RethinkDB 中,所有单查询更新都是原子的。如果 Rethink 认为特定的 update/replace 操作不是原子操作,它将抛出错误并要求您向查询添加非原子标记。所以通常情况下,你不必太担心它。但是,这仅适用于 updatereplace 查询。使用 insert.

无法自动执行此操作

您是正确的,如果您检索该文档,更新它的客户端,然后将其放回数据库,那么它将是一个 非原子 本质上的更新嗯。

不过,在单个查询中,您可以执行以下操作,这实际上是一个更新插入,其方式与您使用 insert 的方式相同,但使用 replace:

r.table('FruitStore')
.get(1)
.replace({
  id : 1, 
  fruits : r.row('fruits').default([]).append('orange') 
})

...这将是原子的。同样,要使用加操作:

r.table('FruitStore')
.get(1)
.replace({
  id : 1, 
  count : r.row('count').default(0).add(1) 
})

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

但是您可以使用 replace 执行原子更新插入:

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 将实际执行插入。