rebase

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package rebase computes the rows a replica holds from the rows its origin confirmed and the local writes the origin has not confirmed yet.

Everything here is pure: no I/O, no clock, no randomness. The same inputs give the same bytes, because every value this package encodes is marshaled with deterministic protobuf encoding. Inputs it returns unchanged keep the encoding they came with, so compare rows with Model.Equal rather than with bytes.Equal.

A Model describes one entity: its collaborative-text fields and the positions they take in a row (one document per element when the field sits under a keyed list or a map), its references, and its merge hooks. With it:

  • FoldTextDeltas turns a RowChange that carries text delta state into the full row it stands for.
  • Apply and Replay replay local writes on top of a confirmed row, with the semantics the origin applies them with.
  • CaptureText and UpstreamValue turn a local write into the value to send to the origin, with every collaborative-text document as a full document.
  • Equal compares two row states.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRefused reports an op the origin refuses whatever the row holds,
	// or refuses on the row it is applied to. Such an op leaves the row as it
	// was.
	ErrRefused = errors.New("rebase: op refused")

	// ErrNoTarget reports a PATCH of an absent row. A PATCH is not an
	// upsert, so the row stays absent. It wraps ErrRefused.
	ErrNoTarget = fmt.Errorf("%w: PATCH of an absent row", ErrRefused)

	// ErrUncapturedText reports a text document the op writes as delta state
	// or as a plain-string value with no full document in Op.Text to stand in
	// for it. The bytes only meant something to the replica that took the
	// write, so the op cannot be replayed or forwarded.
	ErrUncapturedText = errors.New("rebase: text document has no captured full state")
)
View Source
var ErrTextDeltaUnusable = errors.New("rebase: text delta state does not apply to the confirmed row")

ErrTextDeltaUnusable reports a RowChange whose text delta state does not apply to the confirmed row it was folded onto. The confirmed state is not what the origin cut the fragments against, so no change of the page that carried it can be trusted; the recovery is a fresh bootstrap.

Functions

This section is empty.

Types

type Model

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

Model is what replay needs to know about one entity. It is immutable after NewModel and safe for concurrent use, so one Model per entity can be built once and shared.

func NewModel

func NewModel(md protoreflect.MessageDescriptor, mergers lww.Mergers) (*Model, error)

NewModel describes the entity whose rows are messages of md.

mergers holds the entity's app-registered merge hooks, keyed by the dotted schema path of an opaque field, as the engine's WithMerger declares them; nil means none. Every collaborative-text field gets the text union without being named. The map is copied.

NewModel returns an error for a text field the engine refuses to register: one that is not a singular bytes field, or that sits inside a oneof or an unkeyed message list. It also returns an error when mergers names a text field.

func (*Model) Apply

func (m *Model) Apply(row Row, op Op) (Row, error)

Apply returns the row that results from one op on row, with the semantics the origin applies a client write with:

  • PUT replaces the row with Op.Value, except that each collaborative-text document becomes the union of the document row holds at that position and the one the op writes. A position the op leaves empty keeps row's document; an element the op drops loses its document with it.
  • PATCH merges the fields Op.Mask names onto row, per field, keyed list and map items included, and runs the Model's mergers. A text document a mask path names outright is the union of both sides; one a mask path covers through an ancestor is replaced with the subtree. A document the PATCH clears keeps its version vector, as an empty document. A PATCH of an absent row returns ErrNoTarget.
  • DELETE returns the absent row.

Text in Value is taken from Op.Text where Op.Text holds a document for the path and Value carries non-empty bytes there; empty bytes stay empty, which is a clear for a PATCH. Reference stubs are stripped to their target's key fields. The result is encoded deterministically.

An error wrapping ErrRefused means the origin refuses the op on row: a value or a stored row that does not decode, a PATCH without a mask or with a mask that does not fit the schema, text documents that do not union, or ErrNoTarget. ErrUncapturedText reports a text document Apply cannot use. Neither changes anything. Apply does not retain row, op or their slices.

func (*Model) CaptureText

func (m *Model) CaptureText(op Op, rowAfter []byte) (map[string][]byte, error)

CaptureText returns the full text documents to keep with a local write, for Op.Text: for every position where op.Value carries non-empty text bytes that the write reaches, the document rowAfter holds there. rowAfter is the encoded row as the replica stored it after the write. A PUT reaches every position; a PATCH reaches the positions a mask path names or sits above. op.Text is ignored.

It returns nil for a DELETE, for an entity without text fields and for a write that carries no text. It returns an error when op.Value or rowAfter does not decode.

func (*Model) Descriptor

func (m *Model) Descriptor() protoreflect.MessageDescriptor

Descriptor returns the message descriptor the Model was built from.

func (*Model) Equal

func (m *Model) Equal(a, b Row) bool

Equal reports whether a and b are the same row state: both absent, or both present with the same field values. Two encodings of one message compare equal, which bytes.Equal does not promise for a row with a map field. A value that does not decode equals only identical bytes.

func (*Model) FoldTextDeltas

func (m *Model) FoldTextDeltas(confirmed Row, change *syncplanev1.RowChange) (Row, error)

FoldTextDeltas returns the full row a RowChange stands for, given the row confirmed before it.

A tombstone gives the absent row. A change without text deltas gives its value unchanged, bytes included. Otherwise each FieldDelta fills the document its instance path names: an empty fragment keeps the document confirmed holds there, and a non-empty fragment applies to that document, or to an empty one where confirmed holds none. The result is encoded deterministically.

FoldTextDeltas returns an error wrapping ErrTextDeltaUnusable when the value or confirmed does not decode, when a delta names a position the value does not hold, when an empty fragment names a document confirmed does not hold, or when a fragment does not apply. Fields the change lists as hidden stay as the change has them: cleared.

func (*Model) HasText

func (m *Model) HasText() bool

HasText reports whether the entity declares a collaborative-text field.

func (*Model) Replay

func (m *Model) Replay(confirmed Row, ops []Op) (Row, error)

Replay returns the row that results from applying ops, in order, to confirmed. An op Apply refuses (ErrRefused) leaves the row as it was and replay continues with the next one. Replay returns any other error Apply returns, with the zero Row.

Expected versions play no part: the origin decides compare-and-swap. With no ops, Replay returns confirmed unchanged, bytes included.

func (*Model) TextDocuments

func (m *Model) TextDocuments(row []byte) (map[string][]byte, error)

TextDocuments returns every non-empty collaborative-text document of an encoded row, keyed by instance path: the dotted schema path, with the element key inserted after each keyed list or map the path crosses ("notes.<key>.body"). That is the form a PATCH mask and a FieldDelta name a document by. It returns nil for an entity without text fields, and an error when row does not decode.

func (*Model) UpstreamValue

func (m *Model) UpstreamValue(op Op) ([]byte, error)

UpstreamValue returns the value to send the origin for op: op.Value with every text document the write reaches replaced by its document in op.Text. Text bytes the write leaves empty stay empty, so a PATCH that clears a document still clears it upstream.

It returns op.Value unchanged when nothing is replaced, and a deterministic encoding otherwise. It returns ErrUncapturedText when a reached document is delta state or a plain-string value with no entry in op.Text, and an error when op.Value does not decode. For a DELETE it returns nil.

type Op

type Op struct {
	// Kind is PUT, PATCH or DELETE.
	Kind syncplanev1.CrudOp
	// Value is the entity message the write submitted, encoded. Ignored for
	// DELETE.
	Value []byte
	// Mask holds the field-mask paths of a PATCH. Ignored for PUT and DELETE.
	Mask []string
	// Text maps instance paths to full text documents (see TextDocuments). A
	// document here stands in for the bytes Value carries at that path, which
	// may be delta state or a plain-string value only the writer could
	// resolve. CaptureText builds it. Nil when the write carries no text.
	Text map[string][]byte
}

Op is one local write to one row.

type Row

type Row struct {
	// Present is false for an absent or deleted row.
	Present bool
	// Value is the encoded entity message. Ignored when Present is false.
	Value []byte
}

Row is the state of one row. The zero value is an absent row, which is also how a tombstone reads.

Jump to

Keyboard shortcuts

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