set

fun set(replica: ReplicaId, timestamp: Long, key: K, value: V): Patch<LWWMap<K, V>>

Write value for key tagged with (timestamp, replica) — and return the change, one key's cell, rather than the whole map.

This is what to put on the wire. A replicator broadcasts a patch's delta verbatim, so a mutator that handed back the new map would ship every key on every write, at a cost that grows with the map; this frame carries one cell and its size does not depend on how many keys the map holds. The idiom is quilter.mutate { it.set(replica, timestamp, key, value) } — read-modify-write inside the replicator's own lock. To hold the resulting map locally, absorb the patch: map.piece(map.set(…)).

A delta is itself an LWWMap, so a peer absorbs it with the ordinary piece join, in any order, with any repeats, and lands on a state that encodes byte-for-byte identically to the writer's own. There is no causal context to carry and nothing to buffer: this map's merge is a per-key max of independent tags.

Precondition — tag uniqueness. The (replica, timestamp) pair MUST uniquely identify this write for the given key. Reusing the same (replica, timestamp) across two writes to the same key with different values produces non-deterministic convergence under merge — which value survives depends on merge order, not write order. Use a monotonic timestamp source per replica and never reuse a (replica, timestamp) pair. Not enforced at runtime.

Domain of the one-cell delta. The equivalence above holds exactly while that precondition and a monotonic timestamp source are honoured — that is, while the write's (timestamp, replica) tag dominates the key's current one (#2087). Outside that domain no delta exists at all, because a write whose tag loses is dropped by the join while an assigning mutator would still show it locally until the next merge took it away. That case is one this map's own clock-skew warning says is already lost; every replica converges on the same value either way.

Samples

val a = ReplicaId("A")
val b = ReplicaId("B")

// Two peers have converged on a settings map.
var alpha = LWWMap.empty<String, String>()
    .piece { it.set(a, timestamp = 1L, key = "lang", value = "en") }
    .piece { it.set(a, timestamp = 2L, key = "tz", value = "UTC") }
    .piece { it.set(a, timestamp = 3L, key = "theme", value = "dark") }
var bravo = alpha

// B changes one setting and puts only that cell on the wire. The frame is the same size
// whether the map holds three keys or ten thousand, and the other keys are untouched.
val change = alpha.set(b, timestamp = 4L, key = "theme", value = "light")
alpha = alpha.piece(change)
bravo = bravo.piece(change)
check(alpha == bravo)
check(alpha["theme"] == "light")
check(alpha["lang"] == "en")

// Per key the higher (timestamp, replica) tag wins, and a remove is a write like any other,
// so its delta is a one-cell tombstone map…
val forget = bravo.remove(b, timestamp = 5L, key = "lang")
check(alpha.piece(forget)["lang"] == null)

// …and never an empty map, which is the lattice identity and carries no removal at all.
check(alpha.piece(LWWMap.empty<String, String>())["lang"] == "en")