RethinkDB 中的原子条件更新

Atomic conditional update in RethinkDB

RethinkDB 给了我们一些 atomicity guarantees under certain conditions。我正在尝试利用它使用修订字段来实现乐观并发 (c#)。这个想法是,如果在客户端编辑自己的修订版时修改了数据,则更新应该失败。应插入新条目。

var id = "some unique id";
var rev = "the old known revision";
var doc = new {id, Revision = Guid.NewGuid().ToString("N")};
var result = await R.Db("test").Table("table")
    .Get(id)
    .Replace(found => 
        R.Branch(
            found.Eq(null), doc,
            found["Revision"].Eq(rev), doc,
            found
        )
    ).RunResultAsync(connection);

上面的代码似乎 可用于此目的,但我想知道它是否确实会消除紧密并发访问的竞争条件。换句话说,这是 update/insert 还是 upsert atomic?如果可能,请指导我查看一些文档。

文档中提到了 检查和设置寄存器(也可能是 CAS,比较和交换),我认为这对解释哪个没有帮助操作实际上是原子的,我们如何保证它(例如使用集成测试)。

我的印象是,如果满足以下条件:

并且如果写入不涉及二次查询或其他文档,则写入是原子的。那些如果,我喜欢那里有太多的信念。如果有一个标志或操作必须是原子的,我更喜欢,否则核对它。那么在那之前,我能相信我的印象吗? 还有

您的示例查询是原子的。它是原子的,因为它使用 Replace 修改单个文档。如果它修改了多个文档,整个查询将不是原子的,但对每个文档的修改将是单独的原子操作。

ReplacenonAtomic 有一个标志,默认为 true。除非您将 nonAtomic 参数传递给 Replace,否则传递给 Replace 的函数将自动执行。

没有完整查询 atomicnonAtomic 标志。

当您尝试以原子方式执行非确定性操作时,RethinkDB 将抛出错误:

Could not prove argument deterministic. Maybe you want to use the non_atomic flag?

documentation you linked to 提到:

To read and modify a document in a single atomic operation, use the update or replace commands.

检查和设置寄存器只是可以自动执行的操作的一个例子。它还用于说明原子操作的注意事项:只有传递给 ReplaceUpdate 的函数是原子的。周围的查询不是,例如 FilterGetAll.

RethinkDB operations are never atomic across multiple keys

阅读 ,混淆似乎源于文档中的错误。以前说

If the user runs a query that cannot be executed atomically, by default RethinkDB will throw an error.

但被更正为

If an update or replace query cannot be executed atomically, by default RethinkDB will throw an error

另见 How does the atomicity model work?

Write atomicity is supported on a per-document basis – updates to a single JSON document are guaranteed to be atomic. RethinkDB is different from other NoSQL systems in that atomic document updates aren’t limited to a small subset of possible operations – any combination of operations that can be performed on a single document is guaranteed to update the document atomically.