CausalMap

@Serializable(with = CausalMapSerializer::class)
class CausalMap<K, V : DotStore<V>> : Quilted<CausalMap<K, V>>

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

Link copied to clipboard
class Change<V : DotStore<V>>(val store: V, val retired: Set<Dot> = emptySet())

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.

Link copied to clipboard
object Companion
Link copied to clipboard
class Slot<V : DotStore<V>>

A key's current store, and the one way to mint a dot while changing it.

Properties

Link copied to clipboard

The one shared causal context: every dot this map has witnessed, at any depth.

Link copied to clipboard
val keys: Set<K>

The keys present: those whose store is not bottom.

Functions

Link copied to clipboard
fun apply(replica: ReplicaId, key: K, bottom: V, mutator: (CausalMap.Slot<V>) -> CausalMap.Change<V>): Patch<CausalMap<K, V>>

Change the store under key on behalf of replica — and return the change: one key, the fragment mutator wrote, and a context naming exactly the dots it minted, re-carried or retired.

Link copied to clipboard
open fun causalDots(): Set<Dot>

The causal Dots this state has delivered — (author, author-seq) per op.

Link copied to clipboard

The per-author high-water of dots this state delivered and has since compacted away without retaining their identities.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
operator fun get(key: K): V?

The store under key, or null when the key is absent.

Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard
open override fun piece(other: CausalMap<K, V>): CausalMap<K, V>

The join: the least-upper-bound of this and other.

Link copied to clipboard
fun remove(key: K): Patch<CausalMap<K, V>>

Remove key — and return the change: no store, and a context naming every dot held under the key, at every depth. Absorbing it retires all of them, so only writes this replica had not seen survive anywhere (Fig. 17's remove).

Link copied to clipboard
open override fun toString(): String