crdt

package
v3.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CRDT

type CRDT[T any] struct {
	Value      T
	Clocks     map[string]hlc.HLC
	Tombstones map[string]hlc.HLC
	NodeID     string
	Clock      *hlc.Clock
	// contains filtered or unexported fields
}

CRDT represents a Conflict-free Replicated Data Type wrapper around type T.

func NewCRDT

func NewCRDT[T any](initial T, nodeID string) *CRDT[T]

NewCRDT creates a new CRDT wrapper.

func (*CRDT[T]) ApplyDelta

func (c *CRDT[T]) ApplyDelta(delta Delta[T]) bool

ApplyDelta applies a delta using LWW resolution.

func (*CRDT[T]) CreateDelta

func (c *CRDT[T]) CreateDelta(patch deep.Patch[T]) Delta[T]

CreateDelta takes an existing patch, applies it to the local value, updates local metadata, and returns a Delta. Use this if you have already generated a patch manually.

func (*CRDT[T]) Edit

func (c *CRDT[T]) Edit(fn func(*T)) Delta[T]

Edit applies changes and returns a Delta.

func (*CRDT[T]) MarshalJSON

func (c *CRDT[T]) MarshalJSON() ([]byte, error)

func (*CRDT[T]) Merge

func (c *CRDT[T]) Merge(other *CRDT[T]) bool

Merge merges another state.

func (*CRDT[T]) UnmarshalJSON

func (c *CRDT[T]) UnmarshalJSON(data []byte) error

func (*CRDT[T]) View

func (c *CRDT[T]) View() T

View returns a deep copy of the current value.

type Delta

type Delta[T any] struct {
	Patch     deep.Patch[T] `json:"p"`
	Timestamp hlc.HLC       `json:"t"`
}

Delta represents a set of changes with a causal timestamp.

type Text

type Text []TextRun

Text represents a CRDT-friendly text structure using runs.

func (Text) Delete

func (t Text) Delete(pos, length int) Text

Delete removes length characters starting at pos.

func (Text) Insert

func (t Text) Insert(pos int, value string, clock *hlc.Clock) Text

Insert inserts a string at the given character position. It returns the updated Text and the split or new runs. This is a helper for local edits.

func (Text) String

func (t Text) String() string

String returns the full text content.

type TextRun

type TextRun struct {
	ID    hlc.HLC `deep:"key" json:"id"`
	Value string  `json:"v"`
}

TextRun represents a contiguous run of characters with a unique starting ID. Individual characters within the run have implicit IDs: ID + index.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL