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 Counter
- type Delta
- type LWW
- type Map
- func (m *Map[K, V]) Contains(key K) bool
- func (m *Map[K, V]) Delete(key K)
- func (m *Map[K, V]) Get(key K) (V, bool)
- func (m *Map[K, V]) Keys() []K
- func (m *Map[K, V]) Len() int
- func (m *Map[K, V]) Merge(other *Map[K, V]) bool
- func (m *Map[K, V]) NodeID() string
- func (m *Map[K, V]) Set(key K, value V)
- type Set
- 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 Counter ¶ added in v5.1.0
type Counter struct {
// contains filtered or unexported fields
}
Counter is a Positive-Negative Counter CRDT. Each node maintains independent increment and decrement totals; the observed value is sum(Inc) - sum(Dec).
func NewCounter ¶ added in v5.1.0
NewCounter creates a new Counter for the given nodeID.
func (*Counter) Decrement ¶ added in v5.1.0
Decrement adds delta to this node's decrement total. Ignored if delta <= 0.
func (*Counter) Increment ¶ added in v5.1.0
Increment adds delta to this node's increment total. Ignored if delta <= 0.
func (*Counter) Merge ¶ added in v5.1.0
Merge merges the state of other into this Counter. Returns true if any changes were applied.
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 Map ¶ added in v5.1.0
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is a distributed LWW key-value map CRDT built on top of CRDT.
Concurrent writes to the same key are resolved by Last-Write-Wins: the write with the strictly higher HLC timestamp wins. Deletions remove the key from the map and record a tombstone timestamp, so a delete with a newer timestamp wins over an older set, and a set with a newer timestamp wins over an older delete.
func NewMap ¶ added in v5.1.0
func NewMap[K comparable, V any](nodeID string) *Map[K, V]
NewMap returns an empty Map CRDT bound to the given node ID.
func (*Map[K, V]) Delete ¶ added in v5.1.0
func (m *Map[K, V]) Delete(key K)
Delete removes key from the map. It is a no-op if the key does not exist.
func (*Map[K, V]) Get ¶ added in v5.1.0
Get returns the value for key and true if the key exists. It returns the zero value and false otherwise.
func (*Map[K, V]) Keys ¶ added in v5.1.0
func (m *Map[K, V]) Keys() []K
Keys returns a slice of all live keys. The order is non-deterministic.
func (*Map[K, V]) Merge ¶ added in v5.1.0
Merge performs a full state-based LWW merge with another Map node. Returns true if the local state changed.
type Set ¶ added in v5.1.0
type Set[T comparable] struct { // contains filtered or unexported fields }
Set is an Add-Wins Observed-Remove Set (OR-Set) CRDT built on top of CRDT.
Each Add creates a uniquely-tagged entry using the node's HLC. Remove only tombstones entries that exist at call time; a concurrent Add from another node produces a different tag, so after Merge the element is still present (add wins over remove).
func NewSet ¶ added in v5.1.0
func NewSet[T comparable](nodeID string) *Set[T]
NewSet returns an empty Set CRDT bound to the given node ID.
func (*Set[T]) Add ¶ added in v5.1.0
func (s *Set[T]) Add(elem T)
Add appends a new uniquely-tagged entry for elem. The tag is the current HLC timestamp serialised as a string map key.
func (*Set[T]) Contains ¶ added in v5.1.0
Contains reports whether elem has at least one live (non-deleted) entry.
func (*Set[T]) Items ¶ added in v5.1.0
func (s *Set[T]) Items() []T
Items returns a deduplicated slice of all live elements.
func (*Set[T]) Merge ¶ added in v5.1.0
Merge performs a full state-based OR-Set merge with another Set node. Returns true if the local state changed.
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.