Grove.Batch (Grove v0.1.1)

View Source

Atomic batch operations for Grove CRDTs.

Allows multiple operations to be applied atomically with rollback semantics on failure. All operations in a batch produce a single consolidated delta for efficient replication.

Example

alias Grove.Set.ORSet

set = ORSet.new(:node_a)

{:ok, updated} = Grove.Batch.run(set, fn s ->
  s
  |> ORSet.add("a")
  |> ORSet.add("b")
  |> ORSet.add("c")
end)

# Delta contains all three additions
delta = ORSet.delta(updated)

Guarantees

  • Atomicity: On error, the original CRDT is returned unchanged
  • Causality: Operations within the batch form a causal chain
  • Convergence: Batch deltas merge identically to sequential deltas
  • Isolation: Each batch is isolated to the calling process

Summary

Functions

Executes a batch of operations atomically.

Like run/2 but raises on error.

Types

batch_result(t)

@type batch_result(t) :: {:ok, t} | {:error, term(), t}

Functions

run(crdt, fun)

@spec run(crdt :: struct(), fun :: (struct() -> struct())) :: batch_result(struct())

Executes a batch of operations atomically.

The function receives the CRDT and should return the modified CRDT. If any operation raises an error, returns {:error, reason, original_crdt}.

Examples

{:ok, set} = Grove.Batch.run(set, fn s ->
  s |> ORSet.add("a") |> ORSet.add("b")
end)

{:error, %RuntimeError{}, original} = Grove.Batch.run(set, fn s ->
  s |> ORSet.add("a") |> then(fn _ -> raise "oops" end)
end)

run!(crdt, fun)

@spec run!(crdt :: struct(), fun :: (struct() -> struct())) :: struct()

Like run/2 but raises on error.

Examples

set = Grove.Batch.run!(set, fn s ->
  s |> ORSet.add("a") |> ORSet.add("b")
end)