Grove.Counter.PNCounter (Grove v0.1.1)
View SourceA positive-negative counter (PN-Counter) CRDT.
Supports both increment and decrement operations by maintaining two G-Counters: one for increments (P) and one for decrements (N). The value is P - N.
Delta-State Support
This CRDT supports delta-state replication for efficient synchronization:
delta/1- Returns accumulated changes since last resetreset_delta/1- Clears the delta buffer after synchronization
Example
iex> counter = Grove.Counter.PNCounter.new(:node_a)
iex> counter = Grove.Counter.PNCounter.increment(counter, 10)
iex> counter = Grove.Counter.PNCounter.decrement(counter, 3)
iex> Grove.Counter.PNCounter.value(counter)
7
Summary
Functions
Decrements the counter by 1.
Decrements the counter by the given amount.
Returns the accumulated delta since the last reset.
Increments the counter by 1.
Increments the counter by the given amount.
Merges two PN-Counters.
Returns the total negative count.
Creates a new PN-Counter for the given actor.
Returns the total positive count.
Resets the delta buffer after synchronization.
Returns the current value of the counter (positive - negative).
Types
@type actor() :: term()
@type t() :: %Grove.Counter.PNCounter{ actor: actor(), negative: Grove.Counter.GCounter.t(), positive: Grove.Counter.GCounter.t() }
Functions
Decrements the counter by 1.
@spec decrement(t(), non_neg_integer()) :: t()
Decrements the counter by the given amount.
Returns the accumulated delta since the last reset.
The delta contains only the changes made locally to both the positive and negative counters.
Increments the counter by 1.
@spec increment(t(), non_neg_integer()) :: t()
Increments the counter by the given amount.
Merges two PN-Counters.
@spec negative_value(t()) :: non_neg_integer()
Returns the total negative count.
Creates a new PN-Counter for the given actor.
@spec positive_value(t()) :: non_neg_integer()
Returns the total positive count.
Resets the delta buffer after synchronization.
Call this after sending the delta to other replicas.
Returns the current value of the counter (positive - negative).