Documentation
¶
Overview ¶
Package documenttree implements a bounded, framed 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 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) ID() ObjectID
- func (a *Array) Insert(index int, value []byte) (Delta, error)
- func (a *Array) InsertArray(index int) (*Array, Delta, error)
- func (a *Array) InsertMap(index int) (*Map, Delta, error)
- func (a *Array) InsertSubdocument(index int, id string) (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) Subdocuments() []SubdocumentRef
- 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) 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) SetSubdocument(key, id string) (Delta, error)
- type ObjectID
- type ObjectRef
- type Options
- type Registry
- type RegistryOptions
- type SubdocumentRef
- type Value
- type ValueKind
Constants ¶
const SemanticsVersion uint64 = crdt.SemanticsVersionDocumentTree
SemanticsVersion is the immutable document-tree-v1 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") 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 StableFrameType ¶
StableFrameType returns the state/delta pair selected by a document-tree-v1 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) InsertSubdocument ¶
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) Subdocuments ¶
func (d *Document) Subdocuments() []SubdocumentRef
Subdocuments returns the de-duplicated subdocument references reachable from currently visible roots. A reference is metadata only: callers must authenticate and authorize the separately negotiated subdocument manifest before opening a provider for it.
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.
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
MaxSubdocumentIDBytes 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 every parent/subdocument group.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns conservative per-document retention limits.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry tracks which visible subdocuments a local consumer has requested to load. Sync, Load, and Unload do not mutate the parent CRDT and do not perform network I/O; a provider uses Loaded to decide which independent manifest/group to fetch or release.
func NewRegistry ¶
func NewRegistry(options RegistryOptions) (*Registry, error)
func (*Registry) Available ¶
func (r *Registry) Available() []SubdocumentRef
Available returns sorted visible references, regardless of local load state. The returned slice is caller-owned.
func (*Registry) Load ¶
func (r *Registry) Load(id string) (SubdocumentRef, bool)
Load records a local request to load one currently visible subdocument. Callers open their own provider after this returns true.
type RegistryOptions ¶
RegistryOptions bounds local, ephemeral subdocument lifecycle metadata. It is intentionally independent of Document.Options because opened content belongs to separately authorized replication groups.
func DefaultRegistryOptions ¶
func DefaultRegistryOptions() RegistryOptions
type SubdocumentRef ¶
type SubdocumentRef struct{ ID string }
SubdocumentRef identifies a separate, independently authorized replication group. Its bytes never contain the subdocument state or grant access to it. A provider can use Registry to track local load/unload requests.
type Value ¶
type Value struct {
Kind ValueKind
Bytes []byte
Object ObjectRef
Subdocument SubdocumentRef
}
Value is an owned projection value. Bytes is copied on both input and output; applications must treat its content as schema-selected opaque data.