list

package
v1.0.24-beta.2 Latest Latest
Warning

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

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

Documentation

Overview

Package list implements a generic, ordered Replicated Growable Array (RGA).

Each position holds one canonical caller-coded value. Positions, rather than offsets, are replicated, so duplicate and out-of-order delivery converge.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilList          = errors.New("list: nil RGA")
	ErrInvalidReplica   = errors.New("list: invalid replica ID")
	ErrInvalidCodec     = errors.New("list: invalid element codec")
	ErrRange            = errors.New("list: range outside visible list")
	ErrInvalidDelta     = errors.New("list: invalid RGA delta")
	ErrTagConflict      = errors.New("list: conflicting node for one tag")
	ErrIncompleteState  = errors.New("list: incomplete RGA state")
	ErrResourceLimit    = errors.New("list: RGA resource limit exceeded")
	ErrUnsafeCompaction = errors.New("list: unsafe RGA tombstone compaction")
)

Functions

This section is empty.

Types

type Delta

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

Delta is an opaque, joinable partial list state. It is constructed by local mutations or bounded frame decoding only.

func UnmarshalDelta

func UnmarshalDelta[T any](data []byte, codec ElementCodec[T]) (Delta, error)

UnmarshalDelta decodes a bounded canonical list delta for codec.

func UnmarshalDeltaWithLimits

func UnmarshalDeltaWithLimits[T any](data []byte, codec ElementCodec[T], limits frame.DecoderLimits) (Delta, error)

UnmarshalDeltaWithLimits decodes a bounded canonical list delta for codec.

func (Delta) MarshalBinary

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

MarshalBinary returns one canonical list delta frame.

func (Delta) MarshalBinaryWithLimits

func (d Delta) MarshalBinaryWithLimits(limits frame.DecoderLimits) ([]byte, error)

MarshalBinaryWithLimits returns a canonical bounded list delta frame.

type ElementCodec

type ElementCodec[T any] interface {
	ID() string
	Marshal(T) ([]byte, error)
	Unmarshal([]byte) (T, error)
}

ElementCodec defines one application value's canonical wire representation. Marshal must encode semantically equal values identically. The RGA verifies every locally created and remotely decoded value by a decode/re-encode round trip before it can enter replicated state.

type Options

type Options struct {
	MaxNodes        int
	MaxTombstones   int
	MaxPendingNodes int
	MaxPendingBytes int
	MaxValueBytes   int
}

Options bounds retained list state. Applications receiving untrusted peers should set a group-appropriate bound rather than rely on process memory.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns conservative per-list retention limits.

type Position

type Position = crdt.Tag

Position is a stable, opaque list-element identity.

type RGA

type RGA[T any] struct {
	// contains filtered or unexported fields
}

RGA is a generic collaborative list. Deletion retains a structural tombstone: an element deleted before its insertion arrives remains hidden when that insertion is eventually delivered.

func New

func New[T any](replicaID string, codec ElementCodec[T]) (*RGA[T], error)

New constructs a list with default retention limits.

func NewFromClock

func NewFromClock[T any](state clock.State, codec ElementCodec[T]) (*RGA[T], error)

NewFromClock restores a replica clock with default list limits.

func NewFromClockWithOptions

func NewFromClockWithOptions[T any](state clock.State, codec ElementCodec[T], options Options) (*RGA[T], error)

NewFromClockWithOptions restores a replica clock. Persist its state atomically with a complete list snapshot before reusing a replica ID.

func NewFromSnapshot

func NewFromSnapshot[T any](saved snapshot.Snapshot, codec ElementCodec[T]) (*RGA[T], error)

NewFromSnapshot restores a complete HLC-backed list snapshot.

func NewFromSnapshotWithOptions

func NewFromSnapshotWithOptions[T any](saved snapshot.Snapshot, codec ElementCodec[T], options Options, limits frame.DecoderLimits) (*RGA[T], error)

NewFromSnapshotWithOptions restores a complete snapshot within caller retention and decoder limits.

func NewWithOptions

func NewWithOptions[T any](replicaID string, codec ElementCodec[T], options Options) (*RGA[T], error)

NewWithOptions constructs a list with explicit retained-state limits.

func (*RGA[T]) Append

func (r *RGA[T]) Append(values []T) (Delta, error)

Append adds values after the current visible tail.

func (*RGA[T]) ApplyDelta

func (r *RGA[T]) ApplyDelta(delta Delta) error

ApplyDelta atomically integrates a locally created or decoded delta.

func (*RGA[T]) At

func (r *RGA[T]) At(offset int) (T, error)

At returns one visible element by offset.

func (*RGA[T]) ClockState

func (r *RGA[T]) ClockState() clock.State

ClockState returns the HLC state that must be persisted with a snapshot.

func (*RGA[T]) CompactTombstones

func (r *RGA[T]) CompactTombstones(tags []Position) (int, error)

CompactTombstones removes exactly requested tombstoned leaves. The caller must first obtain exact authenticated acknowledgements for one epoch, save a post-compaction checkpoint, and retire old deltas. Any retained child or unresolved dependent blocks the entire request.

func (*RGA[T]) Delete

func (r *RGA[T]) Delete(offset, count int) (Delta, error)

Delete marks count visible elements starting at offset as removed.

func (*RGA[T]) Insert

func (r *RGA[T]) Insert(offset int, values []T) (Delta, error)

Insert inserts values before visible element offset. One value becomes one RGA position, even when its canonical encoding is large or multi-byte.

func (*RGA[T]) MarshalBinary

func (r *RGA[T]) MarshalBinary() ([]byte, error)

MarshalBinary returns a canonical complete list state frame. Pending out-of-order dependencies are deliberately not serializable as a snapshot.

func (*RGA[T]) MarshalBinaryWithClockState

func (r *RGA[T]) MarshalBinaryWithClockState() ([]byte, clock.State, error)

MarshalBinaryWithClockState returns one complete frame and the HLC state that must be durably stored with it.

func (*RGA[T]) MarshalBinaryWithLimits

func (r *RGA[T]) MarshalBinaryWithLimits(limits frame.DecoderLimits) ([]byte, error)

MarshalBinaryWithLimits returns a canonical complete list state constrained by caller-selected transport limits.

func (*RGA[T]) Merge

func (r *RGA[T]) Merge(other *RGA[T]) error

Merge joins every retained node and tombstone from other.

func (*RGA[T]) MissingParents

func (r *RGA[T]) MissingParents() []Position

MissingParents returns unique unresolved parent IDs in canonical order.

func (*RGA[T]) PendingCount

func (r *RGA[T]) PendingCount() int

PendingCount returns unresolved out-of-order nodes. Any non-zero value prevents a complete state snapshot from being emitted.

func (*RGA[T]) Positions

func (r *RGA[T]) Positions() []Position

Positions returns a copy of visible stable IDs in list order.

func (*RGA[T]) SnapshotCurrentState

func (r *RGA[T]) SnapshotCurrentState() (snapshot.Snapshot, error)

SnapshotCurrentState creates a complete HLC-backed list snapshot.

func (*RGA[T]) State

func (r *RGA[T]) State() crdt.StateSnapshot

State returns an immutable diagnostic summary that contains no values, positions, clocks, or frame bytes.

func (*RGA[T]) TombstoneTags

func (r *RGA[T]) TombstoneTags() []Position

TombstoneTags returns every retained deletion identity in canonical order. It is an exact-acknowledgement input, never proof of safe collection.

func (*RGA[T]) UnmarshalBinary

func (r *RGA[T]) UnmarshalBinary(data []byte) error

UnmarshalBinary validates a full list state before atomically replacing r.

func (*RGA[T]) UnmarshalBinaryWithLimits

func (r *RGA[T]) UnmarshalBinaryWithLimits(data []byte, limits frame.DecoderLimits) error

UnmarshalBinaryWithLimits validates a bounded full state before atomically replacing r. It refuses incomplete states and non-canonical values.

func (*RGA[T]) Values

func (r *RGA[T]) Values() ([]T, error)

Values returns a fresh decoded projection in visible order.

Jump to

Keyboard shortcuts

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