~/bend-docscommunity

crdt.bend checks

raw source on the hub · import bend-kit-crdt@0.2.0.0/crdt.bend as Crdt

Replicated data types: grow-only 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.0/bytes.bend as Bytes
import bend-kit-cbor@0.3.0.0/cbor.bend as Cbor

Types

type KCounter source · line 200 · 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.

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, 0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val>

def GCounter.to_cbor source · line 136 · raw

@s:List<&2, U32> -> 0xce226e215fd8376197a24b17bdc4cb2b/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:0xce226e215fd8376197a24b17bdc4cb2b/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, 0xce226e215fd8376197a24b17bdc4cb2b/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:0xce226e215fd8376197a24b17bdc4cb2b/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, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>

Always Some: every item is an unsigned integer.

def GCounter.decode.go source · line 187 · raw

@m:Maybe<&1, 0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val> -> @+cap:U32 -> Maybe<&2, List<&2, U32>>

def GCounter.decode source · line 195 · raw

@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+cap:U32 -> Maybe<&2, List<&2, U32>>

CBOR bytes to a valid state with at most cap replicas, or None.

def KCounter.new source · line 205 · raw

@-K:Data -> KCounter<K>

The counter with no increments.

def KCounter.hit source · line 224 · raw

@c:Cmp -> @n:U32 -> U32

def KCounter.cons source · line 267 · raw

@-K:Data -> @k:K -> @c:U32 -> @m:Maybe<&2, KCounter<K>> -> Maybe<&2, KCounter<K>>

def KCounter.inc.at source · line 275 · 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 298 · raw

@-K:Data -> @s:KCounter<K> -> @acc:Maybe<&2, U32> -> Maybe<&2, U32>

def KCounter.value source · line 306 · 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 333 · raw

@-K:Data -> @s:KCounter<K> -> U32

def KCounter.from_cbor.word source · line 351 · 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 358 · raw

@-K:Data -> @v:0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val -> @mk:Maybe<&2, K> -> @rest:Maybe<&2, KCounter<K>> -> Maybe<&2, KCounter<K>>

def KCounter.from_cbor.gate source · line 373 · raw

@-K:Data -> @ok:Bool -> @s:KCounter<K> -> Maybe<&2, KCounter<K>>

Templates

template KCounter.head source · line 209 · 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 217 · 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 234 · 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 242 · 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 256 · 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 260 · 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 283 · 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 295 · 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 310 · 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 318 · 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 326 · 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 340 · raw

@-K:Data -> @-enc:(@_:K -> 0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val) -> @s:KCounter<K> -> List<&1, Pair(0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val, 0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val)>

template KCounter.to_cbor source · line 348 · raw

@-K:Data -> @-enc:(@_:K -> 0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val) -> @s:KCounter<K> -> 0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val

A CBOR map from each encoded ID to its count, in ID order.

template KCounter.from_cbor.items source · line 366 · raw

@-K:Data -> @-dec:(@_:0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val -> Maybe<&2, K>) -> @xs:List<&1, Pair(0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val, 0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val)> -> Maybe<&2, KCounter<K>>

template KCounter.from_cbor.check source · line 380 · raw

@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @m:Maybe<&2, KCounter<K>> -> @+cap:U32 -> Maybe<&2, KCounter<K>>

template KCounter.from_cbor source · line 388 · raw

@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @-dec:(@_:0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val -> Maybe<&2, K>) -> @v:0xce226e215fd8376197a24b17bdc4cb2b/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 398 · raw

@-K:Data -> @-enc:(@_:K -> 0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val) -> @s:KCounter<K> -> Maybe<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>

None only when enc gives a value with no CBOR form.

template KCounter.decode.go source · line 401 · raw

@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @-dec:(@_:0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val -> Maybe<&2, K>) -> @m:Maybe<&1, 0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val> -> @+cap:U32 -> Maybe<&2, KCounter<K>>

template KCounter.decode source · line 411 · raw

@-K:Data -> @-cmp:(@_:K -> @_:K -> Cmp) -> @-dec:(@_:0xce226e215fd8376197a24b17bdc4cb2b/cbor.Val -> Maybe<&2, K>) -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+cap:U32 -> Maybe<&2, KCounter<K>>

CBOR bytes to a valid state with at most cap replicas, or None.