Documentation
¶
Overview ¶
Package crdt provides the shared contracts and protocol capability discovery used by this module's state-based CRDT implementations.
Applications normally use a concrete data type from a subpackage, such as counter for G-Counters and PN-Counters, set for add-wins OR-Sets, or clock for hybrid logical clocks. This root package contains the common CRDT, delta, snapshot, and mutation-tag contracts those implementations share.
The framed protocol table is intentionally closed. Use ProtocolPolicy during authenticated connection setup to advertise only the state and delta frame types a replication group has agreed to exchange. Experimental protocols require explicit opt-in and may change before stable promotion.
For installation, examples, and package-level guidance, see the module README at https://github.com/darkinno/crdt.
Example (LwwRegister) ¶
package main
import (
"fmt"
"github.com/DarkInno/crdt/register"
)
func main() {
writer, err := register.NewLWW("writer")
if err != nil {
panic(err)
}
reader, err := register.NewLWW("reader")
if err != nil {
panic(err)
}
if err := writer.Set([]byte("healthy")); err != nil {
panic(err)
}
if err := reader.Merge(writer); err != nil {
panic(err)
}
value, ok := reader.Get()
fmt.Println(ok, string(value))
}
Output: true healthy
Example (LwwSet) ¶
package main
import (
"fmt"
"github.com/DarkInno/crdt/lww"
)
func main() {
writer, err := lww.NewSet[string]("writer")
if err != nil {
panic(err)
}
reader, err := lww.NewSet[string]("reader")
if err != nil {
panic(err)
}
if err := writer.Add("on-call"); err != nil {
panic(err)
}
if err := reader.Merge(writer); err != nil {
panic(err)
}
fmt.Println(reader.Contains("on-call"))
}
Output: true
Example (MaxRegister) ¶
package main
import (
"fmt"
"github.com/DarkInno/crdt/register"
)
func main() {
local := register.NewMax()
remote := register.NewMax()
if err := local.Set(8); err != nil {
panic(err)
}
if err := remote.Set(13); err != nil {
panic(err)
}
if err := local.Merge(remote); err != nil {
panic(err)
}
value, ok := local.Get()
fmt.Println(ok, value)
}
Output: true 13
Index ¶
Examples ¶
Constants ¶
const ( TypeIDGCounterState uint64 = 1 TypeIDORSetState uint64 = 2 TypeIDGCounterDelta uint64 = 3 TypeIDORSetDelta uint64 = 4 TypeIDPNCounterState uint64 = 5 TypeIDPNCounterDelta uint64 = 6 TypeIDLWWSetState uint64 = 7 TypeIDLWWSetDelta uint64 = 8 TypeIDLWWMapState uint64 = 9 TypeIDLWWMapDelta uint64 = 10 TypeIDRGAState uint64 = 11 TypeIDRGADelta uint64 = 12 TypeIDGSetState uint64 = 13 TypeIDGSetDelta uint64 = 14 TypeIDMVRegisterState uint64 = 15 TypeIDMVRegisterDelta uint64 = 16 TypeIDORTreeState uint64 = 17 TypeIDORTreeDelta uint64 = 18 // RGA run frames retain scalar Position semantics while compacting linear // same-replica insertion chains. They are the default protocol for new RGA // replication groups; TypeIDRGAState and TypeIDRGADelta remain immutable v1 // contracts for explicitly negotiated legacy groups. TypeIDRGARunState uint64 = 19 TypeIDRGARunDelta uint64 = 20 )
Stable frame type assignments. Values are part of the v1 wire contract and must never be reused for a different payload shape.
Variables ¶
This section is empty.
Functions ¶
func IsExperimentalFrame ¶
IsExperimentalFrame reports whether typeID belongs to an implemented experimental protocol. Reserved or unknown type IDs return false.
func MarshalDiagnosticJSON ¶ added in v1.0.5
func MarshalDiagnosticJSON(summary StateSnapshot) ([]byte, error)
MarshalDiagnosticJSON encodes a caller-provided diagnostic summary. It is useful for CRDT delta log views that use the same schema as StateSnapshot. The summary must not contain application values or replication state.
func MarshalStateJSON ¶ added in v1.0.5
func MarshalStateJSON(value StateReporter) ([]byte, error)
MarshalStateJSON returns a compact JSON diagnostic summary for value.
This helper is intended for structured logs and human inspection. It does not encode CRDT state, deltas, or opaque application values, so its output cannot reconstruct a replica and must not be used as a wire format.
Types ¶
type CRDT ¶
type CRDT[T any] interface { Merge(other T) error State() StateSnapshot }
CRDT is the common contract for state-based CRDTs.
For every concrete state type T, Merge must be commutative, associative, and idempotent. If Merge returns an error, it must leave the receiver unchanged.
type DeltaCapable ¶
DeltaCapable is implemented by a state-based CRDT that accepts a concrete, type-safe delta D. Delta mutators return D directly; the library does not maintain an implicitly acknowledged delta buffer.
type FrameType ¶
FrameType describes one fully implemented framed CRDT protocol. The type table is deliberately closed: reserving an ID alone must not make a payload eligible for batching or recovery before its concrete codec is available.
func DefaultRGAFrameType ¶ added in v1.0.19
func DefaultRGAFrameType() FrameType
DefaultRGAFrameType returns the compact run-v2 protocol for new RGA replication groups. Legacy scalar RGA v1 frames remain available only when a group explicitly enables experimental protocols for migration.
func FrameTypeForDelta ¶
FrameTypeForDelta returns the supported protocol associated with deltaID.
func FrameTypeForState ¶
FrameTypeForState returns the supported protocol associated with stateID.
type ProtocolPolicy ¶
type ProtocolPolicy struct {
// AllowExperimental includes framed LWW-Set, LWW-Map, legacy scalar RGA v1,
// and OR-Tree protocols. Keep it false until the replication group has
// accepted their experimental API and tombstone-retention lifecycle.
AllowExperimental bool
}
ProtocolPolicy controls which implemented frame types one replication group advertises. It is a local, immutable-by-convention value for connection setup; it does not install a process-wide switch or permit runtime protocol registration.
Peers must compare FrameTypes before sending state or deltas. A matching TypeID remains necessary but is not sufficient: applications still own authentication, authorization, limits, and decoder selection.
Example ¶
package main
import (
"fmt"
"github.com/DarkInno/crdt"
)
func main() {
stable := crdt.ProtocolPolicy{}
experimental := crdt.ProtocolPolicy{AllowExperimental: true}
fmt.Println(stable.SupportsFrame(crdt.TypeIDRGAState))
fmt.Println(experimental.SupportsFrame(crdt.TypeIDRGAState))
}
Output: false true
func (ProtocolPolicy) FrameTypes ¶
func (p ProtocolPolicy) FrameTypes() []FrameType
FrameTypes returns a copy of every protocol enabled by p. The returned slice is stable in type-ID order and safe for callers to advertise or modify.
func (ProtocolPolicy) SupportsFrame ¶
func (p ProtocolPolicy) SupportsFrame(typeID uint64) bool
SupportsFrame reports whether typeID is both implemented by this module and enabled by p. It applies to either a state or delta frame type ID.
type StateReporter ¶ added in v1.0.5
type StateReporter interface {
State() StateSnapshot
}
StateReporter exposes an immutable CRDT diagnostic summary.
It intentionally excludes application values, mutation tags, clock state, and framed bytes. Use it for observability only, never to persist or replicate a CRDT.
type StateSnapshot ¶
type StateSnapshot struct {
Type string `json:"type"`
ReplicaID string `json:"replica_id"`
ElementCount int `json:"element_count"`
TombstoneCount int `json:"tombstone_count"`
}
StateSnapshot is an immutable summary of a CRDT state for diagnostics and observability. It never exposes mutable internal data.
type Tag ¶
Tag uniquely identifies a CRDT mutation. WallTime, Logical, and ReplicaID are compared in that order. ReplicaID must be globally unique among live logical replicas; callers that reuse an ID across restarts must persist the last emitted clock state.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package attachment replicates bounded references to externally stored images, audio, video, and arbitrary data.
|
Package attachment replicates bounded references to externally stored images, audio, video, and arbitrary data. |
|
Package clock implements a hybrid logical clock for CRDT mutation tags.
|
Package clock implements a hybrid logical clock for CRDT mutation tags. |
|
cmd
|
|
|
crdt-analyze
command
Command crdt-analyze reports bounded, transport-safe metadata about one canonical CRDT frame.
|
Command crdt-analyze reports bounded, transport-safe metadata about one canonical CRDT frame. |
|
crdt-rga-wasm
command
crdt-rga-wasm exposes the bounded RGA browser runtime through one small syscall/js surface.
|
crdt-rga-wasm exposes the bounded RGA browser runtime through one small syscall/js surface. |
|
crdt-sync-probe
command
Command crdt-sync-probe exercises CRDT delta delivery over real HTTP links.
|
Command crdt-sync-probe exercises CRDT delta delivery over real HTTP links. |
|
Package counter implements counter CRDT primitives.
|
Package counter implements counter CRDT primitives. |
|
Package delta provides bounded batching and coalescing for encoded CRDT deltas.
|
Package delta provides bounded batching and coalescing for encoded CRDT deltas. |
|
Package durable provides a single-writer WebSocket relay reference with a persistent operation log, bounded replay, and reconnect support.
|
Package durable provides a single-writer WebSocket relay reference with a persistent operation log, bounded replay, and reconnect support. |
|
Package encoding provides canonical, bounded binary frames for CRDT state.
|
Package encoding provides canonical, bounded binary frames for CRDT state. |
|
examples
|
|
|
attachment-collaboration
command
Command attachment-collaboration demonstrates one document using separate, authenticated replication groups for editable text and external media references.
|
Command attachment-collaboration demonstrates one document using separate, authenticated replication groups for editable text and external media references. |
|
collaborative-board
command
Command collaborative-board demonstrates how an application can use CRDT deltas for a field-maintenance workboard while replicas are disconnected.
|
Command collaborative-board demonstrates how an application can use CRDT deltas for a field-maintenance workboard while replicas are disconnected. |
|
experimental-collaboration
command
Command experimental-collaboration demonstrates bounded framed replication for LWW-Map, RGA, and OR-Tree after an application has authenticated and negotiated the experimental protocol policy for its replication group.
|
Command experimental-collaboration demonstrates bounded framed replication for LWW-Map, RGA, and OR-Tree after an application has authenticated and negotiated the experimental protocol policy for its replication group. |
|
extensions-provider
command
Command extensions-provider demonstrates the opt-in WebSocket and HTTP/SSE relay surfaces mounted into an application-owned HTTP mux.
|
Command extensions-provider demonstrates the opt-in WebSocket and HTTP/SSE relay surfaces mounted into an application-owned HTTP mux. |
|
warehouse-replication
command
Command warehouse-replication demonstrates framed G-Set and MV-Register replication between warehouse sites and an operations dashboard.
|
Command warehouse-replication demonstrates framed G-Set and MV-Register replication between warehouse sites and an operations dashboard. |
|
websocket-provider
command
Command websocket-provider runs the official WebSocket transport reference against two in-memory counter replicas.
|
Command websocket-provider runs the official WebSocket transport reference against two in-memory counter replicas. |
|
websocket-provider/provider
Package provider is a WebSocket CRDT transport reference implementation.
|
Package provider is a WebSocket CRDT transport reference implementation. |
|
Package extensions provides opt-in, bounded live transport adapters for CRDT replication groups.
|
Package extensions provides opt-in, bounded live transport adapters for CRDT replication groups. |
|
internal
|
|
|
wasm
Package wasm contains host-neutral state used by the browser-facing Wasm command.
|
Package wasm contains host-neutral state used by the browser-facing Wasm command. |
|
Package lww implements last-write-wins CRDT collections.
|
Package lww implements last-write-wins CRDT collections. |
|
Package membership provides a transport-independent, signed membership protocol reference for CRDT replication groups.
|
Package membership provides a transport-independent, signed membership protocol reference for CRDT replication groups. |
|
Package merkle provides deterministic state digests for anti-entropy.
|
Package merkle provides deterministic state digests for anti-entropy. |
|
providers
|
|
|
internal/sqlrelay
module
|
|
|
sqlite
module
|
|
|
Package register implements state-based register CRDTs.
|
Package register implements state-based register CRDTs. |
|
Package replica defines the transport-independent boundary around one framed CRDT replication group.
|
Package replica defines the transport-independent boundary around one framed CRDT replication group. |
|
Package set implements set CRDT primitives.
|
Package set implements set CRDT primitives. |
|
Package snapshot defines immutable, versioned CRDT state snapshots and bounded recovery plans.
|
Package snapshot defines immutable, versioned CRDT state snapshots and bounded recovery plans. |
|
telemetry
module
|
|
|
Package text implements a state-based Replicated Growable Array (RGA).
|
Package text implements a state-based Replicated Growable Array (RGA). |
|
Package tombstonegc coordinates safe, automatic tombstone collection.
|
Package tombstonegc coordinates safe, automatic tombstone collection. |
|
Package tree implements an observed-remove rooted tree CRDT.
|
Package tree implements an observed-remove rooted tree CRDT. |