Loom.LWWRegister

A Last-write-wins register

While alone, this kinda defeats the point of a CRDT, there are times, such as in a map or other kind of composite CRDT, where an individual value should be last-write-wins, but we’d like to preserve causality between all the properties.

This is one of the most simple CRDT’s possible.

Summary

join(a, a)

Joins 2 LWWRegisters

new()

Returns a new LWWRegister CRDT

new(value)

Returns a new LWWRegister CRDT. Initializes to value

new(value, clock)

Returns a new LWWRegister CRDT. Initializes to value with another clock

set(reg, value)

Sets a value using the built-in clock

set(lwwregister, value, clock)

Set a value according to your own clock

value(lwwregister)

Returns the natural value of the register. Can be any type, really

Types

t :: %Loom.LWWRegister{value: term, clock: nil | pos_integer}

Functions

join(a, a)

Specs:

  • join(t, t) :: t

Joins 2 LWWRegisters

iex> alias Loom.LWWRegister, as: Reg
iex> a = Reg.new("test") |> Reg.set("test2")
iex> :timer.sleep(1)
iex> Reg.new("take over") |> Reg.join(a) |> Reg.value
"take over"

In the event that 2 have the same clock, it simply takes the biggest according to Elixir’s rules. If you want something more portable, string comparisons are likely to be the same across languages.

iex> alias Loom.LWWRegister, as: Reg
iex> a = Reg.new("test", 10) |> Reg.set("test2", 11)
iex> b = Reg.new("take over", 11)
...> Reg.join(a,b) |> Reg.value
"test2"
new()

Specs:

  • new :: t

Returns a new LWWRegister CRDT.

nil is a new CRDT’s identity value, and by default the system time in microseconds is used as the clock value.

iex> Loom.LWWRegister.new |> Loom.LWWRegister.value
nil
new(value)

Specs:

  • new(term) :: t

Returns a new LWWRegister CRDT. Initializes to value.

iex> Loom.LWWRegister.new("test") |> Loom.LWWRegister.value
"test"
new(value, clock)

Specs:

  • new(term, pos_integer) :: t

Returns a new LWWRegister CRDT. Initializes to value with another clock

iex> Loom.LWWRegister.new("test", 5) |> Loom.LWWRegister.value
"test"
set(reg, value)

Specs:

  • set(t, term) :: t

Sets a value using the built-in clock

iex> alias Loom.LWWRegister, as: Reg
iex> Reg.new("test")
...> |> Reg.set("test2")
...> |> Reg.value
"test2"
set(lwwregister, value, clock)

Specs:

  • set(t, term, pos_integer) :: t

Set a value according to your own clock.

iex> alias Loom.LWWRegister, as: Reg
iex> Reg.new("test", 5)
...> |> Reg.set("test2", 10)
...> |> Reg.set("won't set.", 2)
...> |> Reg.value
"test2"
value(lwwregister)

Specs:

  • value(t) :: term

Returns the natural value of the register. Can be any type, really.