Documentation
¶
Overview ¶
Package documenttree implements a bounded, framed, fully nested document-tree CRDT.
A document tree combines LWW maps and RGA arrays through single-owner child objects. It intentionally does not reuse the wire formats of lww.Map, list.RGA, or Yjs: a manifest selects this complete protocol as one unit.
Index ¶
- Constants
- Variables
- func MigrateV1Delta(data []byte, options Options, limits frame.DecoderLimits) ([]byte, error)
- func MigrateV1State(data []byte, options Options, limits frame.DecoderLimits) ([]byte, error)
- func StableFrameType() crdt.FrameType
- type Array
- func (a *Array) Array(index int) (*Array, bool)
- func (a *Array) Delete(index, count int) (Delta, error)
- func (a *Array) Get(index int) (Value, bool)
- func (a *Array) GetJSON(index int) (any, bool, error)
- func (a *Array) ID() ObjectID
- func (a *Array) Insert(index int, value []byte) (Delta, error)
- func (a *Array) InsertArray(index int) (*Array, Delta, error)
- func (a *Array) InsertJSON(index int, value any) (Delta, error)
- func (a *Array) InsertMap(index int) (*Map, Delta, error)
- func (a *Array) Len() int
- func (a *Array) Map(index int) (*Map, bool)
- type Delta
- type Document
- func New(replicaID string) (*Document, error)
- func NewFromClockWithOptions(state clock.State, options Options) (*Document, error)
- func NewFromClockWithOptionsAndOutputLimits(state clock.State, options Options, outputLimits frame.DecoderLimits) (*Document, error)
- func NewFromSnapshot(saved snapshot.Snapshot) (*Document, error)
- func NewFromSnapshotWithOptions(saved snapshot.Snapshot, options Options, limits frame.DecoderLimits) (*Document, error)
- func NewWithOptions(replicaID string, options Options) (*Document, error)
- func NewWithOptionsAndOutputLimits(replicaID string, options Options, outputLimits frame.DecoderLimits) (*Document, error)
- func (d *Document) ApplyDelta(delta Delta) error
- func (d *Document) Array(id ObjectID) (*Array, bool)
- func (d *Document) ClockState() clock.State
- func (d *Document) CreateRootArray(name string) (*Array, Delta, error)
- func (d *Document) CreateRootMap(name string) (*Map, Delta, error)
- func (d *Document) Map(id ObjectID) (*Map, bool)
- func (d *Document) MarshalBinary() ([]byte, error)
- func (d *Document) MarshalBinaryWithClockState() ([]byte, clock.State, error)
- func (d *Document) MarshalBinaryWithClockStateAndLimits(limits frame.DecoderLimits) ([]byte, clock.State, error)
- func (d *Document) MarshalBinaryWithLimits(limits frame.DecoderLimits) ([]byte, error)
- func (d *Document) MarshalDeltaWithLimits(delta Delta, limits frame.DecoderLimits) ([]byte, error)
- func (d *Document) MarshalJSON() ([]byte, error)
- func (d *Document) MarshalLocalDelta(delta Delta) ([]byte, error)
- func (d *Document) Merge(other *Document) error
- func (d *Document) RootArray(name string) (*Array, bool)
- func (d *Document) RootMap(name string) (*Map, bool)
- func (d *Document) SnapshotCurrentState() (snapshot.Snapshot, error)
- func (d *Document) SnapshotCurrentStateWithLimits(limits frame.DecoderLimits) (snapshot.Snapshot, error)
- func (d *Document) State() crdt.StateSnapshot
- func (d *Document) UnmarshalBinary(data []byte) error
- func (d *Document) UnmarshalBinaryWithLimits(data []byte, limits frame.DecoderLimits) error
- type Kind
- type Map
- func (m *Map) Array(key string) (*Array, bool)
- func (m *Map) CreateArray(key string) (*Array, Delta, error)
- func (m *Map) CreateMap(key string) (*Map, Delta, error)
- func (m *Map) Delete(key string) (Delta, error)
- func (m *Map) Get(key string) (Value, bool)
- func (m *Map) GetJSON(key string) (any, bool, error)
- func (m *Map) ID() ObjectID
- func (m *Map) Keys() []string
- func (m *Map) Map(key string) (*Map, bool)
- func (m *Map) Set(key string, value []byte) (Delta, error)
- func (m *Map) SetJSON(key string, value any) (Delta, error)
- type ObjectID
- type ObjectRef
- type Options
- type Value
- type ValueKind
Constants ¶
const SemanticsVersion uint64 = crdt.SemanticsVersionDocumentTree
SemanticsVersion is the immutable document-tree-v2 protocol contract.
Variables ¶
var ( ErrNilDocument = errors.New("documenttree: nil document") ErrInvalidReplica = errors.New("documenttree: invalid replica ID") ErrInvalidRoot = errors.New("documenttree: invalid root name") ErrInvalidKey = errors.New("documenttree: invalid map key") ErrInvalidValue = errors.New("documenttree: invalid value") ErrInvalidJSON = errors.New("documenttree: invalid JSON scalar") ErrInvalidDelta = errors.New("documenttree: invalid document-tree delta") ErrInvalidState = errors.New("documenttree: invalid document-tree state") ErrIncompleteState = errors.New("documenttree: incomplete document-tree state") ErrTypeMismatch = errors.New("documenttree: object type mismatch") ErrUnknownObject = errors.New("documenttree: unknown object") ErrRange = errors.New("documenttree: array range outside visible projection") ErrTagConflict = errors.New("documenttree: conflicting use of a mutation tag") ErrResourceLimit = errors.New("documenttree: resource limit exceeded") )
Functions ¶
func MigrateV1Delta ¶ added in v1.0.33
MigrateV1Delta converts one bounded v1 delta into a v2 delta. It is safe only for a controlled cutover where every participant has stopped sending v1 frames; hosts must deliver the resulting v2 frame to a v2 replication group with the same authenticated schema and compatible limits.
func MigrateV1State ¶ added in v1.0.33
MigrateV1State converts one complete v1 state frame into a complete v2 frame without changing its object graph or HLC tags. It is an offline, one-time migration tool, not a replication decoder. A v1 frame containing its former lazy-reference value is rejected: the referenced content was not in the old frame, so treating it as a fully nested value would lose data.
func StableFrameType ¶
StableFrameType returns the state/delta pair selected by a document-tree-v2 replication manifest. The protocol owns its HLC because roots, map writes, and array positions all use HLC tags.
Types ¶
type Array ¶
type Array struct {
// contains filtered or unexported fields
}
Array is a lightweight handle to one RGA array object.
func (*Array) GetJSON ¶ added in v1.0.36
GetJSON returns a decoded JSON scalar at one visible array position.
func (*Array) InsertJSON ¶ added in v1.0.36
InsertJSON inserts one canonical JSON scalar. Nested JSON containers must be modelled with InsertMap or InsertArray so their independent edits converge.
type Delta ¶
type Delta struct {
// contains filtered or unexported fields
}
Delta is an opaque, joinable partial document-tree state. Local mutation or UnmarshalDelta are the only ways to construct a meaningful Delta. A local delta may retain its already-validated canonical outbox frame so a facade can hand it off without serializing the same mutation twice.
func UnmarshalDelta ¶
UnmarshalDelta decodes one bounded canonical delta.
func UnmarshalDeltaWithLimits ¶
func UnmarshalDeltaWithLimits(data []byte, limits frame.DecoderLimits) (Delta, error)
UnmarshalDeltaWithLimits decodes a delta with the default retained-value policy. ApplyDelta always rechecks it against the receiver's local limits.
func UnmarshalDeltaWithOptions ¶
func UnmarshalDeltaWithOptions(data []byte, options Options, limits frame.DecoderLimits) (Delta, error)
UnmarshalDeltaWithOptions validates one delta before it can enter a receiver-owned pending queue.
func (Delta) MarshalBinary ¶
MarshalBinary returns a canonical bounded document-tree delta frame.
func (Delta) MarshalBinaryWithLimits ¶
func (d Delta) MarshalBinaryWithLimits(limits frame.DecoderLimits) ([]byte, error)
MarshalBinaryWithLimits enforces the exact output frame budget selected by the local group before returning a delta for an outbox.
func (Delta) MarshalJSON ¶
MarshalJSON returns a payload-free diagnostic summary for a delta.
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document is a bounded collection of LWW maps and RGA arrays. The document lock protects every object so a mutation that creates a child is atomic with its parent reference; handles never carry independent mutable state.
func NewFromClockWithOptions ¶
NewFromClockWithOptions restores an empty document using a persisted HLC.
func NewFromClockWithOptionsAndOutputLimits ¶ added in v1.0.31
func NewFromClockWithOptionsAndOutputLimits(state clock.State, options Options, outputLimits frame.DecoderLimits) (*Document, error)
NewFromClockWithOptionsAndOutputLimits restores a document with explicit retained-state and local outbox budgets. The output budget is checked before every local mutation is committed, so an encoding failure cannot create a local-only update.
func NewFromSnapshot ¶
NewFromSnapshot restores a complete snapshot using default local limits.
func NewFromSnapshotWithOptions ¶
func NewFromSnapshotWithOptions(saved snapshot.Snapshot, options Options, limits frame.DecoderLimits) (*Document, error)
NewFromSnapshotWithOptions restores under caller-selected retention and decoder limits; a snapshot cannot widen either local budget.
func NewWithOptions ¶
NewWithOptions constructs an empty document with explicit local limits.
func NewWithOptionsAndOutputLimits ¶ added in v1.0.31
func NewWithOptionsAndOutputLimits(replicaID string, options Options, outputLimits frame.DecoderLimits) (*Document, error)
NewWithOptionsAndOutputLimits constructs a document that rejects a local mutation before it changes state when its canonical delta cannot fit the selected outbox frame budget. Remote decoding remains independently bounded by the limits passed to UnmarshalDeltaWithOptions.
func (*Document) ApplyDelta ¶
ApplyDelta joins a decoded delta atomically. It retains bounded unresolved records for parent-before-child reordering but never serializes them as a complete state.
func (*Document) ClockState ¶
ClockState returns the state that must be atomically persisted with a complete document snapshot before this replica ID is used after restart.
func (*Document) CreateRootArray ¶
CreateRootArray creates one named array root, or returns the current array root without a delta when it already exists.
func (*Document) CreateRootMap ¶
CreateRootMap creates one named map root, or returns the current map root without a delta when it already exists. Concurrent roots of the same name resolve by their creation tag; a schema should not reuse a root name for a different Kind.
func (*Document) MarshalBinary ¶
MarshalBinary returns a canonical complete document-tree state. A state with missing parent/object records is intentionally not serializable.
func (*Document) MarshalBinaryWithClockState ¶
MarshalBinaryWithClockState returns state and the HLC state to persist in one transaction before a replica ID can be reused.
func (*Document) MarshalBinaryWithClockStateAndLimits ¶
func (*Document) MarshalBinaryWithLimits ¶
func (d *Document) MarshalBinaryWithLimits(limits frame.DecoderLimits) ([]byte, error)
func (*Document) MarshalDeltaWithLimits ¶ added in v1.0.31
MarshalDeltaWithLimits encodes delta using this document's retained-value policy and an explicit transport budget. Documents created with an output budget have already checked the same encoding before accepting local delta, so this is the safe outbox handoff for those mutations.
func (*Document) MarshalJSON ¶
MarshalJSON returns a payload-free diagnostic summary.
func (*Document) MarshalLocalDelta ¶ added in v1.0.31
MarshalLocalDelta returns the already-validated frame for a delta returned by a local mutation on d when d has an output budget. It otherwise encodes with that budget. Callers must use it only with a delta from this document; untrusted deltas must go through UnmarshalDeltaWithOptions instead.
func (*Document) Merge ¶
Merge joins another complete or incomplete document state without exposing mutable internals. Both documents retain their independent HLC identities.
func (*Document) RootMap ¶
RootMap returns the current visible map root. It is false while a root declaration is waiting for its object record, or when the root is an array.
func (*Document) SnapshotCurrentState ¶
SnapshotCurrentState creates a validated HLC-backed recovery snapshot.
func (*Document) SnapshotCurrentStateWithLimits ¶
func (*Document) State ¶
func (d *Document) State() crdt.StateSnapshot
State returns a diagnostic summary. It excludes user values and object IDs.
func (*Document) UnmarshalBinary ¶
UnmarshalBinary atomically replaces a document with one complete canonical state frame. It never accepts pending state into a recovery boundary.
func (*Document) UnmarshalBinaryWithLimits ¶
func (d *Document) UnmarshalBinaryWithLimits(data []byte, limits frame.DecoderLimits) error
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is a lightweight handle to one integrated map object.
func (*Map) GetJSON ¶ added in v1.0.36
GetJSON returns a decoded JSON scalar written through SetJSON. It rejects a raw byte value that is not a canonical JSON scalar rather than guessing how an application intended to interpret it.
type ObjectID ¶
ObjectID is the immutable identity of a created child object or array position. The zero ID is reserved as the RGA array root anchor.
type ObjectRef ¶
ObjectRef names a child object. It is returned as a Value and is immutable: an object can have exactly one parent creation record and cannot be moved or inserted a second time.
type Options ¶
type Options struct {
MaxRoots int
MaxObjects int
MaxMapEntries int
MaxArrayNodes int
MaxArrayTombstones int
MaxPendingOperations int
MaxPendingBytes int
MaxDepth int
MaxKeyBytes int
MaxValueBytes int
}
Options bounds retained and incomplete document state. Limits apply to one document-tree group; hosts must still reject oversized transport bodies before allocating them and authorize the one complete replication group.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns conservative per-document retention limits.