OxL Reference

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:

oxl✓ checked in CI
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()
}
Output
1 0 1
pub(api) fn txn<R, effect F>(body: fn(KvTx) -> R F) -> R | KvError  db, F

Thread safety. `body` may run more than once; keep it free of side effects.

Since. 1.0.0-beta

Parameters

NameType
bodyfn(KvTx) -> R F

Returns

R

Errors

  • 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).

Effects

none declared (pure unless the body says otherwise)

Defined in: std/kv/kv.oxl:593

See also