Documentation
¶
Overview ¶
Package crdt provides Conflict-free Replicated Data Types (CRDTs) built on top of the deep patch engine.
The central type is CRDT, a concurrency-safe wrapper around any value of type T. It tracks causal history using a per-field Hybrid Logical Clock (HLC) and resolves concurrent edits with Last-Write-Wins (LWW) semantics.
Basic workflow ¶
- Create nodes: nodeA := crdt.NewCRDT(initial, "node-a")
- Edit locally: delta := nodeA.Edit(func(v *T) { v.Field = newVal })
- Distribute: send delta (JSON-serializable) to peers
- Apply remotely: nodeB.ApplyDelta(delta)
For full-state synchronization between two nodes use CRDT.Merge.
Text CRDT ¶
Text is a convergent, ordered sequence of TextRun segments. It supports concurrent insertions and deletions across nodes and is integrated with CRDT directly — no separate registration required.
Index ¶
- type CRDT
- func (c *CRDT[T]) ApplyDelta(delta Delta[T]) bool
- func (c *CRDT[T]) Clock() *hlc.Clock
- func (c *CRDT[T]) Edit(fn func(*T)) Delta[T]
- func (c *CRDT[T]) MarshalJSON() ([]byte, error)
- func (c *CRDT[T]) Merge(other *CRDT[T]) bool
- func (c *CRDT[T]) NodeID() string
- func (c *CRDT[T]) UnmarshalJSON(data []byte) error
- func (c *CRDT[T]) View() T
- type Delta
- type LWW
- type Text
- type TextRun
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CRDT ¶
type CRDT[T any] struct { // contains filtered or unexported fields }
CRDT represents a Conflict-free Replicated Data Type wrapper around type T.
func (*CRDT[T]) ApplyDelta ¶
ApplyDelta applies a delta from a remote peer using Last-Write-Wins resolution. Returns true if any operations were accepted.
func (*CRDT[T]) Edit ¶
Edit applies fn to a copy of the current value, computes a delta, advances the local clock, and returns the delta for distribution to peers. Returns an empty Delta if the edit produces no changes.
func (*CRDT[T]) MarshalJSON ¶
func (*CRDT[T]) Merge ¶
Merge performs a full state-based merge with another CRDT node. For each changed field the node with the strictly newer effective timestamp (max of write clock and tombstone) wins. Text fields are always merged convergently via MergeTextRuns, bypassing LWW.
func (*CRDT[T]) UnmarshalJSON ¶
type Delta ¶
Delta represents a set of changes with a causal timestamp. Obtain a Delta via CRDT.Edit; apply it on remote nodes via CRDT.ApplyDelta.
func (Delta[T]) MarshalJSON ¶
func (*Delta[T]) UnmarshalJSON ¶
type LWW ¶
LWW represents a Last-Write-Wins register for type T. Embed LWW fields in a struct to track per-field causality. Use Set to update the value; it accepts the write only if ts is strictly newer.
type Text ¶
type Text []TextRun
Text represents a CRDT-friendly text structure using runs.
func MergeTextRuns ¶
MergeTextRuns merges two Text states into a single convergent state.