GCounter[optional A: (Integer[A] val & Unsigned)]¶
A mutable grow-only counter. That is, the value can only be increased.
It is limited by the maximum value of the used unsigned integer datatype. Any operation that would lead to an overflow (if the maximum is the maximum value for the used unsigned integer type) will result in the value being set to the maximum. So once the maximum is reached, the GCounter will never change.
This data type tracks the state seen from each replica, thus the size of the state will grow proportionally with the number of total replicas. New replicas may be added as peers at any time, provided that they use unique ids. Read-only replicas which never change state and only observe need not use unique ids, and should use an id of zero, by convention.
The state of each replica represents the value incremented so far by that particular replica. This local value may only ever increase, never decrease. The total value of the counter is the sum of the local value of all replicas.
When converging state from other replicas, we retain the maximum observed value from each replica id. Because a higher value always implies later logical time for that replica, and we only keep the highest value seen from each replica, we will always retain the latest value seen from each replica.
Because the view of values from each other replica is eventually consistent, the summed value of the overall counter is also eventually consistent.
All mutator methods accept and return a convergent delta-state.
class ref GCounter[optional A: (Integer[A] val & Unsigned)] is
Comparable[GCounter[A] ref] ref,
Convergent[GCounter[A] ref] ref,
Replicated ref
Implements¶
- Comparable[GCounter[A] ref] ref
- Convergent[GCounter[A] ref] ref
- Replicated ref
Constructors¶
create¶
Instantiate the GCounter under the given unique replica id.
Parameters¶
- id': ID
Returns¶
- GCounter[A] ref^
Public Functions¶
is_empty¶
Return true if the data structure contains no information (bottom state).
Returns¶
- Bool val
apply¶
Return the current value of the counter (the sum of all replica values).
Returns¶
- A
value¶
Return the current value of the counter (the sum of all replica values).
Returns¶
- A
increment[optional D: GCounter[A] ref]¶
Increment the counter by the given value. Accepts and returns a convergent delta-state.
fun ref increment[optional D: GCounter[A] ref](
value': A = 1,
delta': D = recover GCounter[A](0) end)
: D^
Parameters¶
- value': A = 1
- delta': D = recover GCounterA end
Returns¶
- D^
converge¶
Converge from the given GCounter into this one. For each replica state, we select the maximum value seen so far (grow-only). Returns true if the convergence added new information to the data structure.
Parameters¶
- that: GCounter[A] box
Returns¶
- Bool val
string¶
Return a best effort at printing the register. If A is Stringable, use the string representation of the value; otherwise print as a question mark.
Returns¶
- String iso^
eq¶
Parameters¶
- that: GCounter[A] box
Returns¶
- Bool val
ne¶
Parameters¶
- that: GCounter[A] box
Returns¶
- Bool val
lt¶
Parameters¶
- that: GCounter[A] box
Returns¶
- Bool val
le¶
Parameters¶
- that: GCounter[A] box
Returns¶
- Bool val
gt¶
Parameters¶
- that: GCounter[A] box
Returns¶
- Bool val
ge¶
Parameters¶
- that: GCounter[A] box
Returns¶
- Bool val
from_tokens¶
Deserialize an instance of this data structure from a stream of tokens.
Parameters¶
- that: TokensIterator ref
Returns¶
- None val ?
each_token¶
Serialize the data structure, capturing each token into the given Tokens.
Parameters¶
- tokens: Tokens ref
Returns¶
- None val
compare¶
Parameters¶
- that: GCounter[A] box