method Kv.txn
Runs body in an optimistic transaction: reads are validated and writes applied in one atomic batch. If a key read by body
changed meanwhile, body runs again (up to 100 times, then Conflict). body must be free of side effects other than the store.
Example:
use std.kv { Kv }
fn main() io, db {
let kv = Kv.open("docs-kvtxn")!
kv.put("a", b"1")!
let moved = kv.txn(tx => {
let v = tx.get("a")
tx.delete("a")
tx.put("b", v ?? b"")
1
})
print("{moved!} {kv.version("a")!} {kv.get("b")!.len()}")
kv.close()
}Вывод
1 0 1
pub(api) fn txn<R, effect F>(body: fn(KvTx) -> R F) -> R | KvError db, FПотокобезопасность. `body` may run more than once; keep it free of side effects.
С версии. 1.0.0-beta
Параметры
| Имя | Тип |
|---|---|
body | fn(KvTx) -> R F |
Возвращает
R
Ошибки
KvError db, F- `KvError` (`Conflict` when a key read by `body` changed and the retries ran out; `CrossShardTx` when the keys live on different shards).
Эффекты
не объявлены (чистая, пока тело не говорит иное)
Определено в: std/kv/kv.oxl:593