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

Types

actor :: term

dot :: {actor, pos_integer}

t :: %Loom.GCounter{counter: %{actor => pos_integer}, ctx: %{actor => pos_integer}}

Functions

inc(gcounter, actor, int \\ 1)

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

join(gcounter, gcounter)

Specs:

  • join(t, t) :: t

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

new()

Specs:

  • new :: t

Instantiate a new GCounter. Starts at 0.

iex> Loom.GCounter.new |> Loom.GCounter.value 0

new(opts)

Specs:

  • new([{:values, [{actor, pos_integer}]}]) :: t

Instantiate a new GCounter with previous values.

iex> Loom.GCounter.new(values: [a: 10, b: 5, c: 27]) |> Loom.GCounter.value 42

value(gcounter)

Specs:

  • value(t) :: non_neg_integer

Get the value of a counter.

Will always be >=0.