CausalMap
A map whose keys and everything nested under them share one causal context, which is never reset, so no dot is ever minted twice: not after a remove, not after the key is recreated, not at any depth.
Think of a shared poll: {poll: {question, votes}}. One person deletes the poll and starts it again while someone else, at the same moment, votes on the old one. Both changes have to land as two different events. A map whose nested values each keep their own history cannot promise that: the recreated poll starts counting from scratch, reuses the names of the events it replaced, and a peer that saw the old ones takes the new ones as already seen and drops them (#3126). Here the poll and its fields are named from one counter that only grows, so the second poll's events can never be mistaken for the first's.
This is the map embedding of Almeida, Shoker and Baquero, Delta State Replicated Data Types (arXiv:1603.01529, §7, Fig. 17): Causal<DotMap<K, V>> with each value a bare DotStore (not a full causal CRDT), every dot minted through the shared context, and
apply(oδ, k, (m, c)) = ({k ↦ v}, c′) where (v, c′) = oδ((m(k), c))
remove(k, (m, c)) = ({}, dots(m(k)))What a remove means: reset, not resurrect. remove retires every dot the remover had seen under the key, at every depth. Only writes it had not seen survive, so a key removed while a peer was writing to it comes back holding that peer's unseen writes and nothing else. A key is present exactly when its store is not bottom; there are no per-key tags and no tombstones, and a removed key costs nothing once the context compacts.
Writing goes through apply only. A nested mutator gets a Slot: the key's current store and a Slot.mint that draws from the shared context. It returns a Change: the new store fragment and the dots it retires. apply builds the delta's context itself, from exactly those dots, and checks the change before shipping it — see apply for the checks. A mutator never sees or returns a context, which is what keeps it from minting from the wrong one, or shipping the whole one.
Its join is Causal's, unchanged: associative, commutative and idempotent.
"Never minted twice" holds along one replica's history of absorbed states, which is how kuilt's mutators are used: two apply calls on the same state, without absorbing the first, mint the same dot, as any delta mutator does.
Serialized by CausalMapSerializer, as the bare Causal<DotMap<K, V>>.
Samples
val a = ReplicaId("A")
val b = ReplicaId("B")
// One field of a nested document: an MV-register write mints one dot and retires the old ones.
fun CausalMap<String, DotMap<String, DotFun<Int>>>.vote(replica: ReplicaId, value: Int) =
apply(replica, "poll", DotMap()) { slot ->
val votes = slot.narrow(slot.store.entries["votes"] ?: DotFun())
CausalMap.Change(DotMap(mapOf("votes" to DotFun(mapOf(votes.mint() to value)))), retired = votes.store.dots)
}
val start = CausalMap.empty<String, DotMap<String, DotFun<Int>>>().piece { it.vote(a, 3) }
// A removes the poll and recreates it; B, concurrently, votes on the old one.
val byA = start.piece { it.remove("poll") }.piece { it.vote(a, 5) }
val byB = start.piece { it.vote(b, 4) }
// A's recreated vote minted a fresh dot, not the one its remove retired, so B's peer cannot
// mistake it for something already seen. Both votes survive, as a conflict.
val merged = byA.piece(byB)
check(merged["poll"]?.entries?.get("votes")?.values?.values?.toSet() == setOf(4, 5))
check(merged == byB.piece(byA))Types
What a mutator changed under one key: the fragment it stores, and the held dots it retired. Written dots that were already held are re-carried (for a payload that grows, as in LatticeDotFun); held dots neither written nor retired are left as they are.
Properties
Functions
The causal Dots this state has delivered — (author, author-seq) per op.
The per-author high-water of dots this state delivered and has since compacted away without retaining their identities.