crdt.bend checks
raw source on the hub · import bend-kit-crdt@0.2.2.1/crdt.bend as Crdt
Replicated data types: grow-only and positive-negative counters with proved merges. Source: https://github.com/paymog/bend-kit/tree/main/crdt
A GCounter is a list of U32 counts. Entry i is the count of replica i, and a missing entry counts as 0. This is the state-based G-Counter of Shapiro et al. (2011). Each replica must increment only its own entry. A KCounter is the same counter keyed by sparse replica IDs of any Data type.
3 imports
import Base import bend-kit-bytes@0.3.3.1/bytes.bend as Bytes import bend-kit-cbor@0.3.0.1/cbor.bend as Cbor
Types
type PNCounter source · line 206 · raw
Data
PN@positive:List<&2, U32> -> @negative:List<&2, U32> -> PNCounter
type KCounter source · line 306 · raw
@-K:Data -> Data
A G-Counter keyed by replica IDs. A valid state lists IDs in ascending cmp order, each with a count above 0.
KNil@-K:Data -> KCounter<K>
KCon@-K:Data -> @id:K -> @count:U32 -> @rest:KCounter<K> -> KCounter<K>
Definitions
def GCounter.new source · line 12 · raw
List<&2, U32>
The counter with no increments.
def GCounter.get source · line 16 · raw
@s:List<&2, U32> -> @i:Nat -> U32
The count of replica i.
def GCounter.merge source · line 26 · raw
@a:List<&2, U32> -> @b:List<&2, U32> -> List<&2, U32>
Component-wise maximum. It is commutative, associative, and idempotent.
def GCounter.merge_all source · line 38 · raw
@xs:List<&2, List<&2, U32>> -> List<&2, U32>
The merge of every state in the list.
def GCounter.pad source · line 46 · raw
@i:Nat -> List<&2, U32>
Zeros for replicas 0 to i - 1, then 1 for replica i.
def GCounter.cons source · line 53 · raw
@c:U32 -> @m:Maybe<&2, List<&2, U32>> -> Maybe<&2, List<&2, U32>>
def GCounter.inc.at source · line 61 · raw
@ok:Bool -> @n:U32 -> @t:List<&2, U32> -> Maybe<&2, List<&2, U32>>
ok is false when n wrapped to 0.
def GCounter.inc source · line 69 · raw
@s:List<&2, U32> -> @i:Nat -> Maybe<&2, List<&2, U32>>
Adds 1 to the count of replica i. None when that count is 4294967295.
def GCounter.add source · line 78 · raw
@m:Maybe<&2, U32> -> @+c:U32 -> Maybe<&2, U32>
def GCounter.value.go source · line 85 · raw
@s:List<&2, U32> -> @acc:Maybe<&2, U32> -> Maybe<&2, U32>
def GCounter.value source · line 93 · raw
@s:List<&2, U32> -> Maybe<&2, U32>
The sum of all counts. None when the sum does not fit in a U32.
def GCounter.value_exact source · line 97 · raw
@s:List<&2, U32> -> Nat
The unbounded sum of all replica counts.
def GCounter.le source · line 105 · raw
@a:List<&2, U32> -> @b:List<&2, U32> -> Bool
True when a is at most b in every entry.
def GCounter.valid.go source · line 114 · raw
@t:List<&2, U32> -> @last:U32 -> Bool
def GCounter.valid source · line 122 · raw
@s:List<&2, U32> -> Bool
A valid state has no trailing 0, so each count vector has one form.
def GCounter.size source · line 129 · raw
@s:List<&2, U32> -> U32
def GCounter.to_cbor.items source · line 136 · raw
@s:List<&2, U32> -> List<&1, 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val>
def GCounter.to_cbor source · line 144 · raw
@s:List<&2, U32> -> 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val
A CBOR array of unsigned integers: the count of replica i is item i.
def GCounter.from_cbor.word source · line 147 · raw
@ok:Bool -> @lo:U32 -> @rest:Maybe<&2, List<&2, U32>> -> Maybe<&2, List<&2, U32>>
def GCounter.from_cbor.item source · line 154 · raw
@v:0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val -> @rest:Maybe<&2, List<&2, U32>> -> Maybe<&2, List<&2, U32>>
def GCounter.from_cbor.items source · line 162 · raw
@xs:List<&1, 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val> -> Maybe<&2, List<&2, U32>>
def GCounter.from_cbor.gate source · line 169 · raw
@ok:Bool -> @s:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def GCounter.from_cbor.check source · line 176 · raw
@m:Maybe<&2, List<&2, U32>> -> @+cap:U32 -> Maybe<&2, List<&2, U32>>
def GCounter.from_cbor source · line 184 · raw
@v:0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val -> @+cap:U32 -> Maybe<&2, List<&2, U32>>
A valid state with at most cap replicas, or None.
def GCounter.encode source · line 192 · raw
@s:List<&2, U32> -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>
Always Some: every item is an unsigned integer.
def GCounter.decode.go source · line 195 · raw
@m:Maybe<&1, 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val> -> @+cap:U32 -> Maybe<&2, List<&2, U32>>
def GCounter.decode source · line 203 · raw
@b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @+cap:U32 -> Maybe<&2, List<&2, U32>>
CBOR bytes to a valid state with at most cap replicas, or None.
def PNCounter.new source · line 209 · raw
PNCounter
def PNCounter.make source · line 212 · raw
@p:Maybe<&2, List<&2, U32>> -> @n:Maybe<&2, List<&2, U32>> -> Maybe<&2, PNCounter>
def PNCounter.inc source · line 218 · raw
@s:PNCounter -> @i:Nat -> Maybe<&2, PNCounter>
def PNCounter.dec source · line 222 · raw
@s:PNCounter -> @i:Nat -> Maybe<&2, PNCounter>
def PNCounter.merge source · line 226 · raw
@a:PNCounter -> @b:PNCounter -> PNCounter
def PNCounter.merge_all source · line 231 · raw
@xs:List<&2, PNCounter> -> PNCounter
def PNCounter.le source · line 238 · raw
@a:PNCounter -> @b:PNCounter -> Bool
def PNCounter.valid source · line 243 · raw
@s:PNCounter -> Bool
def PNCounter.value.diff source · line 247 · raw
@negative:Bool -> @p:U32 -> @n:U32 -> Pair(Bool, U32)
def PNCounter.value.totals source · line 254 · raw
@p:Maybe<&2, U32> -> @n:Maybe<&2, U32> -> Maybe<&1, Pair(Bool, U32)>
def PNCounter.value source · line 262 · raw
@s:PNCounter -> Maybe<&1, Pair(Bool, U32)>
The sign (True means negative) and magnitude; None if either half overflows.
def PNCounter.value_exact.diff source · line 266 · raw
@negative:Bool -> @p:Nat -> @n:Nat -> Pair(Bool, Nat)
def PNCounter.value_exact source · line 274 · raw
@s:PNCounter -> Pair(Bool, Nat)
The exact sign and unbounded magnitude; zero is never negative.
def PNCounter.to_cbor source · line 280 · raw
@s:PNCounter -> 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val
def PNCounter.from_cbor source · line 284 · raw
@v:0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val -> @+cap:U32 -> Maybe<&2, PNCounter>
def PNCounter.encode source · line 291 · raw
@s:PNCounter -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>
def PNCounter.decode.go source · line 294 · raw
@m:Maybe<&1, 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val> -> @cap:U32 -> Maybe<&2, PNCounter>
def PNCounter.decode source · line 301 · raw
@b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @cap:U32 -> Maybe<&2, PNCounter>
def KCounter.new source · line 311 · raw
@-K:Data -> KCounter<K>
The counter with no increments.
def KCounter.hit source · line 330 · raw
@c:Cmp -> @n:U32 -> U32
def KCounter.cons source · line 373 · raw
@-K:Data -> @k:K -> @c:U32 -> @m:Maybe<&2, KCounter<K>> -> Maybe<&2, KCounter<K>>
def KCounter.inc.at source · line 381 · raw
@-K:Data -> @ok:Bool -> @x:K -> @n:U32 -> @r:KCounter<K> -> Maybe<&2, KCounter<K>>
ok is false when n wrapped to 0.
def KCounter.value.go source · line 404 · raw
@-K:Data -> @s:KCounter<K> -> @acc:Maybe<&2, U32> -> Maybe<&2, U32>
def KCounter.value source · line 412 · raw
@-K:Data -> @s:KCounter<K> -> Maybe<&2, U32>
The sum of all counts. None when the sum does not fit in a U32.
def KCounter.size source · line 439 · raw
@-K:Data -> @s:KCounter<K> -> U32
def KCounter.from_cbor.word source · line 457 · raw
@-K:Data -> @ok:Bool -> @mk:Maybe<&2, K> -> @lo:U32 -> @rest:Maybe<&2, KCounter<K>> -> Maybe<&2, KCounter<K>>
def KCounter.from_cbor.count source · line 464 · raw
@-K:Data -> @v:0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val -> @mk:Maybe<&2, K> -> @rest:Maybe<&2, KCounter<K>> -> Maybe<&2, KCounter<K>>
def KCounter.from_cbor.gate source · line 479 · raw
@-K:Data -> @ok:Bool -> @s:KCounter<K> -> Maybe<&2, KCounter<K>>
Templates
template KCounter.head source · line 315 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @+x:K -> @b:KCounter<K> -> Cmp
cmp(x, first ID of b), or EQ when b is empty.
template KCounter.heads source · line 323 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @a:KCounter<K> -> @b:KCounter<K> -> Cmp
The comparison of the first IDs of a and b, or EQ when either is empty.
template KCounter.get source · line 340 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @s:KCounter<K> -> @+k:K -> U32
The count of replica k: the largest count under an ID that cmp makes equal to k.
template KCounter.merge.go source · line 348 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @+a:KCounter<K> -> @+b:KCounter<K> -> @q:Cmp -> KCounter<K>
q is KCounter.heads(a, b). Passing it in lets match pick the step without closures.
template KCounter.merge source · line 362 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @+a:KCounter<K> -> @+b:KCounter<K> -> KCounter<K>
The union of two ID-sorted states, with the maximum count where IDs are equal.
template KCounter.merge_all source · line 366 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @xs:List<&2, KCounter<K>> -> KCounter<K>
The merge of every state in the list.
template KCounter.inc.go source · line 389 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @s:KCounter<K> -> @+k:K -> @q:Cmp -> Maybe<&2, KCounter<K>>
q is KCounter.head(k, s).
template KCounter.inc source · line 401 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @+s:KCounter<K> -> @+k:K -> Maybe<&2, KCounter<K>>
Adds 1 to the count of replica k. None when that count is 4294967295.
template KCounter.le source · line 416 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @a:KCounter<K> -> @+b:KCounter<K> -> Bool
True when each count in a is at most the count of its ID in b.
template KCounter.above source · line 424 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @s:KCounter<K> -> @+lo:K -> Bool
True when s is empty or its first ID is above lo.
template KCounter.valid source · line 432 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @s:KCounter<K> -> Bool
A valid state has strictly ascending IDs and no 0 count, so each counter has one form.
template KCounter.to_cbor.items source · line 446 · raw
@-K:Data -> @-enc:(@_:K -> 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val) -> @s:KCounter<K> -> List<&1, Pair(0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val, 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val)>
template KCounter.to_cbor source · line 454 · raw
@-K:Data -> @-enc:(@_:K -> 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val) -> @s:KCounter<K> -> 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val
A CBOR map from each encoded ID to its count, in ID order.
template KCounter.from_cbor.items source · line 472 · raw
@-K:Data -> @-dec:(@_:0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val -> Maybe<&2, K>) -> @xs:List<&1, Pair(0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val, 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val)> -> Maybe<&2, KCounter<K>>
template KCounter.from_cbor.check source · line 486 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @m:Maybe<&2, KCounter<K>> -> @+cap:U32 -> Maybe<&2, KCounter<K>>
template KCounter.from_cbor source · line 494 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @-dec:(@_:0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val -> Maybe<&2, K>) -> @v:0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val -> @+cap:U32 -> Maybe<&2, KCounter<K>>
A valid state with at most cap replicas, or None. dec reads one ID.
template KCounter.encode source · line 504 · raw
@-K:Data -> @-enc:(@_:K -> 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val) -> @s:KCounter<K> -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>
None only when enc gives a value with no CBOR form.
template KCounter.decode.go source · line 507 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @-dec:(@_:0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val -> Maybe<&2, K>) -> @m:Maybe<&1, 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val> -> @+cap:U32 -> Maybe<&2, KCounter<K>>
template KCounter.decode source · line 517 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @-dec:(@_:0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val -> Maybe<&2, K>) -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @+cap:U32 -> Maybe<&2, KCounter<K>>
CBOR bytes to a valid state with at most cap replicas, or None.