Loom.GCounter
Grow only counters
GCounters can only ever increment. They are useful for view and hit counters, which will never shrink.
They are not delta-CRDT’s, as they are rather lightweight in general. A delta- CRDT implementation would just return the latest value for an actor.
They do, however, implement the CRDT protocol, and can be coposed into larger CRDT datastructures.
Summary
inc(gcounter, actor, int \\ 1) | Increment a counter on behalf of the actor |
join(gcounter, gcounter) | Joins 2 counters |
new() | Instantiate a new GCounter. Starts at 0 |
new(opts) | Instantiate a new GCounter with previous values |
value(gcounter) | Get the value of a counter |
Functions
Specs:
Increment a counter on behalf of the actor.
If you need to decrement, see Loom.PNCounter
iex> Loom.GCounter.new |> Loom.GCounter.inc(:a, 1) |> Loom.GCounter.inc(:a, 29) |> Loom.GCounter.value() 30
Specs:
Joins 2 counters.
Because counters monotonically increase, we can just merge them.
iex> alias Loom.GCounter iex> ctr1 = GCounter.new |> GCounter.inc(:a) |> GCounter.inc(:a, 10) iex> ctr2 = GCounter.new |> GCounter.inc(:b) |> GCounter.inc(:b, 5) iex> GCounter.join(ctr1,ctr2) |> GCounter.value 17
Specs:
- new :: t
Instantiate a new GCounter. Starts at 0.
iex> Loom.GCounter.new |> Loom.GCounter.value 0
Specs:
Instantiate a new GCounter with previous values.
iex> Loom.GCounter.new(values: [a: 10, b: 5, c: 27]) |> Loom.GCounter.value 42