lww

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package lww implements last-write-wins CRDT collections.

The HLC tag is the complete conflict-resolution rule: a higher tag wins. Therefore callers that reuse a replica ID must persist ClockState before a restart, just as they do for OR-Set.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidReplicaID = errors.New("lww: invalid replica ID")
	ErrNilSet           = errors.New("lww: nil set")
	ErrNilMap           = errors.New("lww: nil map")
	ErrInvalidKey       = errors.New("lww: invalid key")
	ErrInvalidDelta     = errors.New("lww: invalid map delta")
	ErrInvalidSnapshot  = errors.New("lww: invalid map snapshot")
	ErrTagConflict      = errors.New("lww: conflicting values for one tag")
)

Functions

This section is empty.

Types

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map is a byte-value LWW map. Returning and accepting copies prevents a caller from modifying replicated state through a shared slice. Values are deliberately opaque; applications may use a deterministic JSON, protobuf, or domain codec above this type.

func NewMap

func NewMap(replicaID string) (*Map, error)

func NewMapFromClock

func NewMapFromClock(state clock.State) (*Map, error)

func NewMapFromSnapshot

func NewMapFromSnapshot(saved snapshot.Snapshot) (*Map, error)

NewMapFromSnapshot restores a map and its HLC state. Snapshots without a clock state are rejected because they cannot safely reuse a replica ID.

func (*Map) ApplyDelta

func (m *Map) ApplyDelta(delta MapDelta) error

ApplyDelta joins a validated partial map state into m. It validates every entry and detects equal-tag conflicts before mutating the map or HLC.

func (*Map) ClockState

func (m *Map) ClockState() clock.State

func (*Map) Delete

func (m *Map) Delete(key string) error

Delete removes key and preserves the original non-delta API.

func (*Map) DeleteWithDelta

func (m *Map) DeleteWithDelta(key string) (MapDelta, error)

DeleteWithDelta removes key and returns the joinable delete delta.

func (*Map) Frontier

func (m *Map) Frontier() map[string]crdt.Tag

Frontier returns the greatest map-entry tag per replica. The returned map is owned by the caller and includes delete tombstones.

func (*Map) Get

func (m *Map) Get(key string) ([]byte, bool)

func (*Map) Keys

func (m *Map) Keys() []string

Keys returns the visible keys in lexical order, which keeps callers from accidentally depending on Go's randomized map iteration order.

func (*Map) MarshalBinary

func (m *Map) MarshalBinary() ([]byte, error)

MarshalBinary returns the canonical framed LWW-Map state.

func (*Map) MarshalBinaryWithClockState

func (m *Map) MarshalBinaryWithClockState() ([]byte, clock.State, error)

MarshalBinaryWithClockState captures state and HLC state for atomic persistence before a replica ID is reused after restart.

func (*Map) MarshalJSON added in v1.0.5

func (m *Map) MarshalJSON() ([]byte, error)

MarshalJSON returns a diagnostic summary for structured logs. It omits map keys, values, tags, and clock state, and cannot restore the map.

func (*Map) Merge

func (m *Map) Merge(other *Map) error

func (*Map) Set

func (m *Map) Set(key string, value []byte) error

Set writes a value and preserves the original non-delta API.

func (*Map) SetWithDelta

func (m *Map) SetWithDelta(key string, value []byte) (MapDelta, error)

SetWithDelta writes a value and returns the joinable delta for this write.

func (*Map) Snapshot

func (m *Map) Snapshot(frontier map[string]crdt.Tag) (snapshot.Snapshot, error)

Snapshot creates an immutable map state snapshot with caller-supplied replication frontier and the local HLC state.

func (*Map) SnapshotCurrentState

func (m *Map) SnapshotCurrentState() (snapshot.Snapshot, error)

SnapshotCurrentState creates a snapshot whose frontier is derived from all visible and deleted map entries.

func (*Map) State

func (m *Map) State() crdt.StateSnapshot

func (*Map) UnmarshalBinary

func (m *Map) UnmarshalBinary(data []byte) error

UnmarshalBinary atomically replaces m with a valid complete LWW-Map state.

func (*Map) UnmarshalBinaryWithLimits

func (m *Map) UnmarshalBinaryWithLimits(data []byte, limits frame.Limits) error

UnmarshalBinaryWithLimits atomically replaces m with a bounded LWW-Map state. A malformed frame leaves both m and its HLC unchanged.

type MapDelta

type MapDelta struct {
	// contains filtered or unexported fields
}

MapDelta is a joinable partial LWW-Map state. Its contents are deliberately opaque so callers cannot mutate an entry after it has been handed to a replica or coalescer.

func UnmarshalMapDelta

func UnmarshalMapDelta(data []byte) (MapDelta, error)

UnmarshalMapDelta decodes one bounded, canonical LWW-Map delta frame.

func UnmarshalMapDeltaWithLimits

func UnmarshalMapDeltaWithLimits(data []byte, limits frame.Limits) (MapDelta, error)

UnmarshalMapDeltaWithLimits decodes one bounded, canonical LWW-Map delta.

func (MapDelta) MarshalBinary

func (d MapDelta) MarshalBinary() ([]byte, error)

MarshalBinary returns the canonical framed LWW-Map delta.

func (MapDelta) MarshalJSON added in v1.0.5

func (d MapDelta) MarshalJSON() ([]byte, error)

MarshalJSON returns a diagnostic summary for structured logs. It omits map keys, values, tags, and clock state and cannot be applied as a delta from JSON.

func (MapDelta) Merge

func (d MapDelta) Merge(other MapDelta) (MapDelta, error)

Merge joins two map deltas without modifying either input.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is an LWW element set. Concurrent add/remove operations are resolved by the canonical Tag ordering, rather than by an implicit wall-clock tie rule.

func NewSet

func NewSet[T comparable](replicaID string) (*Set[T], error)

func NewSetFromClock

func NewSetFromClock[T comparable](state clock.State) (*Set[T], error)

func (*Set[T]) Add

func (s *Set[T]) Add(value T) error

func (*Set[T]) ClockState

func (s *Set[T]) ClockState() clock.State

func (*Set[T]) Contains

func (s *Set[T]) Contains(value T) bool

func (*Set[T]) Elements

func (s *Set[T]) Elements() []T

func (*Set[T]) MarshalJSON added in v1.0.5

func (s *Set[T]) MarshalJSON() ([]byte, error)

MarshalJSON returns a diagnostic summary for structured logs. It omits elements, keys, values, tags, and clock state, and cannot restore the set.

func (*Set[T]) Merge

func (s *Set[T]) Merge(other *Set[T]) error

Merge selects the highest tag for every element. It snapshots other before locking s, so reciprocal concurrent merges cannot deadlock.

func (*Set[T]) Remove

func (s *Set[T]) Remove(value T) error

func (*Set[T]) State

func (s *Set[T]) State() crdt.StateSnapshot

Jump to

Keyboard shortcuts

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