import { Deno } from "https://deno.land/x/deno@v1.32.3/cli/tsc/dts/lib.deno.unstable.d.ts";
const { AtomicOperation } = Deno;
UNSTABLE: New API, yet to be vetted.
An operation on a Deno.Kv
that can be performed
atomically. Atomic operations do not auto-commit, and must be committed
explicitly by calling the commit
method.
Atomic operations can be used to perform multiple mutations on the KV store
in a single atomic transaction. They can also be used to perform
conditional mutations by specifying one or more
Deno.AtomicCheck
s that ensure that a mutation is only performed
if the key-value pair in the KV has a specific versionstamp. If any of the
checks fail, the entire operation will fail and no mutations will be made.
The ordering of mutations is guaranteed to be the same as the ordering of the mutations specified in the operation. Checks are performed before any mutations are performed. The ordering of checks is unobservable.
Atomic operations can be used to implement optimistic locking, where a mutation is only performed if the key-value pair in the KV store has not been modified since the last read. This can be done by specifying a check that ensures that the versionstamp of the key-value pair matches the versionstamp that was read. If the check fails, the mutation will not be performed and the operation will fail. One can then retry the read-modify- write operation in a loop until it succeeds.
The commit
method of an atomic operation returns a value indicating
whether checks passed and mutations were performed. If the operation failed
because of a failed check, the return value will be null
. If the
operation failed for any other reason (storage error, invalid value, etc.),
an exception will be thrown. If the operation succeeded, the return value
will be a Deno.KvCommitResult
object containing the
versionstamp of the value committed to KV.
Methods
Add to the operation a check that ensures that the versionstamp of the key-value pair in the KV store matches the given versionstamp. If the check fails, the entire operation will fail and no mutations will be performed during the commit.
Commit the operation to the KV store. Returns a value indicating whether
checks passed and mutations were performed. If the operation failed
because of a failed check, the return value will be null
. If the
operation failed for any other reason (storage error, invalid value,
etc.), an exception will be thrown. If the operation succeeded, the
return value will be a Deno.KvCommitResult
object containing
the versionstamp of the value committed to KV.
If the commit returns null
, one may create a new atomic operation with
updated checks and mutations and attempt to commit it again. See the note
on optimistic locking in the documentation for Deno.AtomicOperation
.
Add to the operation a mutation that deletes the specified key if all checks pass during the commit.
Add to the operation a mutation that performs the specified mutation on
the specified key if all checks pass during the commit. The types and
semantics of all available mutations are described in the documentation
for Deno.KvMutation
.