crdt.bend checks
raw source on the hub · import bend-kit-crdt@0.2.1.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 198 · raw
Data
PN@positive:List<&2, U32> -> @negative:List<&2, U32> -> PNCounter
type KCounter source · line 284 · 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.le source · line 97 · 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 106 · raw
@t:List<&2, U32> -> @last:U32 -> Bool
def GCounter.valid source · line 114 · 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 121 · raw
@s:List<&2, U32> -> U32
def GCounter.to_cbor.items source · line 128 · raw
@s:List<&2, U32> -> List<&1, 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val>
def GCounter.to_cbor source · line 136 · 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 139 · raw
@ok:Bool -> @lo:U32 -> @rest:Maybe<&2, List<&2, U32>> -> Maybe<&2, List<&2, U32>>
def GCounter.from_cbor.item source · line 146 · raw
@v:0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val -> @rest:Maybe<&2, List<&2, U32>> -> Maybe<&2, List<&2, U32>>
def GCounter.from_cbor.items source · line 154 · raw
@xs:List<&1, 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val> -> Maybe<&2, List<&2, U32>>
def GCounter.from_cbor.gate source · line 161 · raw
@ok:Bool -> @s:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def GCounter.from_cbor.check source · line 168 · raw
@m:Maybe<&2, List<&2, U32>> -> @+cap:U32 -> Maybe<&2, List<&2, U32>>
def GCounter.from_cbor source · line 176 · 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 184 · raw
@s:List<&2, U32> -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>
Always Some: every item is an unsigned integer.
def GCounter.decode.go source · line 187 · raw
@m:Maybe<&1, 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val> -> @+cap:U32 -> Maybe<&2, List<&2, U32>>
def GCounter.decode source · line 195 · 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 201 · raw
PNCounter
def PNCounter.make source · line 204 · raw
@p:Maybe<&2, List<&2, U32>> -> @n:Maybe<&2, List<&2, U32>> -> Maybe<&2, PNCounter>
def PNCounter.inc source · line 210 · raw
@s:PNCounter -> @i:Nat -> Maybe<&2, PNCounter>
def PNCounter.dec source · line 214 · raw
@s:PNCounter -> @i:Nat -> Maybe<&2, PNCounter>
def PNCounter.merge source · line 218 · raw
@a:PNCounter -> @b:PNCounter -> PNCounter
def PNCounter.merge_all source · line 223 · raw
@xs:List<&2, PNCounter> -> PNCounter
def PNCounter.le source · line 230 · raw
@a:PNCounter -> @b:PNCounter -> Bool
def PNCounter.valid source · line 235 · raw
@s:PNCounter -> Bool
def PNCounter.value.diff source · line 239 · raw
@negative:Bool -> @p:U32 -> @n:U32 -> Pair(Bool, U32)
def PNCounter.value.totals source · line 246 · raw
@p:Maybe<&2, U32> -> @n:Maybe<&2, U32> -> Maybe<&1, Pair(Bool, U32)>
def PNCounter.value source · line 254 · raw
@s:PNCounter -> Maybe<&1, Pair(Bool, U32)>
The sign (True means negative) and magnitude; None if either half overflows.
def PNCounter.to_cbor source · line 258 · raw
@s:PNCounter -> 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val
def PNCounter.from_cbor source · line 262 · raw
@v:0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val -> @+cap:U32 -> Maybe<&2, PNCounter>
def PNCounter.encode source · line 269 · raw
@s:PNCounter -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>
def PNCounter.decode.go source · line 272 · raw
@m:Maybe<&1, 0x8ed6fcae06bf1a414d09d56fe0ada5c3/cbor.Val> -> @cap:U32 -> Maybe<&2, PNCounter>
def PNCounter.decode source · line 279 · raw
@b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @cap:U32 -> Maybe<&2, PNCounter>
def KCounter.new source · line 289 · raw
@-K:Data -> KCounter<K>
The counter with no increments.
def KCounter.hit source · line 308 · raw
@c:Cmp -> @n:U32 -> U32
def KCounter.cons source · line 351 · raw
@-K:Data -> @k:K -> @c:U32 -> @m:Maybe<&2, KCounter<K>> -> Maybe<&2, KCounter<K>>
def KCounter.inc.at source · line 359 · 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 382 · raw
@-K:Data -> @s:KCounter<K> -> @acc:Maybe<&2, U32> -> Maybe<&2, U32>
def KCounter.value source · line 390 · 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 417 · raw
@-K:Data -> @s:KCounter<K> -> U32
def KCounter.from_cbor.word source · line 435 · 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 442 · 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 457 · raw
@-K:Data -> @ok:Bool -> @s:KCounter<K> -> Maybe<&2, KCounter<K>>
Templates
template KCounter.head source · line 293 · 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 301 · 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 318 · 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 326 · 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 340 · 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 344 · 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 367 · 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 379 · 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 394 · 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 402 · 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 410 · 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 424 · 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 432 · 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 450 · 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 464 · raw
@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @m:Maybe<&2, KCounter<K>> -> @+cap:U32 -> Maybe<&2, KCounter<K>>
template KCounter.from_cbor source · line 472 · 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 482 · 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 485 · 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 495 · 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.