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 ¶
- Variables
- type Model
- func (m *Model) Apply(row Row, op Op) (Row, error)
- func (m *Model) CaptureText(op Op, rowAfter []byte) (map[string][]byte, error)
- func (m *Model) ClearUnits(value []byte, units []string) ([]byte, error)
- func (m *Model) CopyUnits(dst, src []byte, units []string) ([]byte, error)
- func (m *Model) Descriptor() protoreflect.MessageDescriptor
- func (m *Model) Equal(a, b Row) bool
- func (m *Model) FoldTextDeltas(confirmed Row, change *syncplanev1.RowChange) (Row, error)
- func (m *Model) HasText() bool
- func (m *Model) Replay(confirmed Row, ops []Op) (Row, error)
- func (m *Model) Requeue(op Op, later []Op) (Op, bool, error)
- func (m *Model) SetUnits(value []byte, units []string) ([]string, error)
- func (m *Model) TextDocuments(row []byte) (map[string][]byte, error)
- func (m *Model) UpstreamFragments(op Op, confirmed []byte) ([]byte, error)
- func (m *Model) UpstreamValue(op Op) ([]byte, error)
- type Op
- type Row
- type UndeclaredTextError
Constants ¶
This section is empty.
Variables ¶
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") )
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 ¶
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 ¶
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) ClearUnits ¶ added in v0.8.2
ClearUnits returns the encoded row value with each unit that units names cleared: a top-level field by its name, or every field of a oneof by the oneof's name. A name that is neither is ignored. It returns value unchanged when units is empty, and a deterministic encoding otherwise. It returns an error when value does not decode.
func (*Model) CopyUnits ¶ added in v0.9.0
CopyUnits returns the encoded row dst with each unit that units names replaced by the value src holds for it, cleared where src leaves it unset. A name that is neither a top-level field nor a oneof is ignored. It returns dst unchanged when units is empty, and a deterministic encoding otherwise. It returns an error when dst or src 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 ¶
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 ¶
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. The error is an *UndeclaredTextError when a delta names a field the Model does not declare as text. Fields the change lists as hidden stay as the change has them: cleared.
func (*Model) Replay ¶
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) Requeue ¶ added in v0.7.0
Requeue returns op as it is sent again behind later, the writes to the same row that were made after op and that the origin applies first. Applied after later, the result leaves the fields of the row as op followed by later leaves them. Collaborative text is an exception: text unions in any order, so op keeps its documents and the text that later writes is not folded in.
- A DELETE or PUT in later supersedes the fields op writes. When the last of them is a DELETE, Requeue reports false: nothing of op is left to send. When it is a PUT, the result is a PATCH of the documents in op.Text, and Requeue reports false when op.Text is empty.
- A DELETE op is kept as it is when later holds only PATCHes.
- Otherwise later holds only PATCHes, and the result is op with each of them applied to op.Value as Apply applies it, mask paths that name a text document left out. A PATCH op keeps its mask.
With later empty, Requeue returns op unchanged. It returns an error when op.Value does not decode, and any error Replay returns. Requeue does not retain or change op, later or their slices and maps; the result shares op.Text.
func (*Model) SetUnits ¶ added in v0.9.0
SetUnits returns the units of units that the encoded row value sets, in the order units names them: a top-level field by its name, or a oneof by its name when one of its fields is set. A name that is neither is left out. It returns an error when value does not decode.
func (*Model) TextDocuments ¶
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) UpstreamFragments ¶ added in v0.7.0
UpstreamFragments returns the value to send the origin for the PATCH op when the origin holds the encoded row confirmed: UpstreamValue's value with every text document the write reaches replaced by a fragment of the state it holds beyond the document confirmed holds at that position. A position where confirmed holds no document keeps the full document.
The fragment applies at a receiver whose document covers confirmed's. It returns the errors UpstreamValue returns, an error for an op that is not a PATCH, and an error when confirmed does not decode or a document does not merge with confirmed's.
func (*Model) UpstreamValue ¶
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. For a PUT it names the
// top-level fields and oneofs the PUT replaces, and nil means all of them.
// Ignored for 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.
type UndeclaredTextError ¶ added in v0.7.0
type UndeclaredTextError struct {
// Entity is the RowChange's entity, Key its row key and Field the
// instance path the delta names.
Entity, Key, Field string
}
UndeclaredTextError reports a RowChange whose text delta state names a field the Model's message does not declare as collaborative text, such as a field a newer schema of the entity added. No confirmed row can rebuild that field from a fragment; the recovery is to ask for the entity's rows as full state. It matches ErrTextDeltaUnusable under errors.Is.
func (*UndeclaredTextError) Error ¶ added in v0.7.0
func (e *UndeclaredTextError) Error() string
func (*UndeclaredTextError) Unwrap ¶ added in v0.7.0
func (e *UndeclaredTextError) Unwrap() error