Store wire: sending records to the client store
The store wire is the generic format in which the server sends records and field values to the web client:
inside an RPC answer, or as a bus frame after a commit. It is owned in Rust by the erp-store-wire crate
(builders, strict decoder, snapshot descriptor) and read in TypeScript by @erpforall/store-wire (decoder and
reference applier). The behavior is fixed by docs/specs/mail/messaging-store.md (cases SPEC-MSGSTORE-001
to 034) and ADR 0148 D2. The codec contains no mail code: it knows models and fields only through a schema it
is given. Chatter itself is described in Chatter storage. An addon cannot depend on the wire crate: it reaches the builders, the
schema types and the decoder as erp_sdk::store_wire (a re-export of erp-store-wire through erp-orm); the mail
thread answers of Call the thread from the client are the first producer.
How a store field reaches the wire
Section titled “How a store field reaches the wire”- The producer (a fetch topic or a bus emitter) lists the records it wants the client to hold, grouped by model.
- For each field it builds a typed value with
FieldValue. The schema decides the allowed kind; a value of the wrong kind, an unknown model or field, or an id outside1..2^53-1is refused when the value is set, never when the payload is read. - An answer is built with
AnswerBuilder::new(schema, asof), whereasofis the snapshot descriptor of the transaction that read the data (Transaction::store_snapshot()inerp-orm). A frame is built withFrameBuilder::new(schema, cursor, asof)wherecursoris(xid, event id)andasof.ownequals the cursor’s xid. set(model, id, field, value)adds one field;delete(model, id)adds a delete marker (a delete and field values for the same record in one payload are refused);encode()returns the canonical bytes.- The client decodes the bytes against its schema with the strict decoder and applies the result to the reference table (below). A payload either decodes whole or is refused with a code and a path.
Field kinds and sync classes
Section titled “Field kinds and sync classes”The kinds are bool, int, float, string, html (typed as markup by the schema, never by an in-band tag),
date, datetime, selection, many2one, reference (a model and id pair), x2many and counter. The set is
closed and part of the wire major.
Each schema field has a sync class:
| Class | Meaning | Where its values come from |
|---|---|---|
state (default) | An absolute value; its writers are serialized on a row or unique key. | Answers and events. |
counter | An integer changed by deltas; an optional cap makes “the cap or more” a valid answer. | Answers carry the absolute value, frames carry inc. |
answer_only | A value the server never announces by event. | Answers only; an event may not carry it. |
An x2many field is a set: an answer sends the full id list (replace), a frame sends add and remove lists.
A frame never sends a replace.
Producer obligations A1-A5
Section titled “Producer obligations A1-A5”The codec cannot check these. Every producer must keep them, and each producing slice tests its part.
- A1, one snapshot per answer. The store data and the descriptor of an answer come from one REPEATABLE READ
transaction on the primary, with the descriptor read after the transaction’s last write. A writing answer may
mark the state fields its transaction wrote with
mark_written; counters cannot be marked. - A2, nested snapshots. Snapshots taken on the primary have nested completed sets. This is a PostgreSQL property that the design relies on and has not been proved here (see the specification, open question 2).
- A3, serialized writers. Every
statecoordinate is backed by a row or unique key that each of its writers writes, so the later of two committed writers saw the earlier one. - A4, complete and exact events. A commit that changes a coordinate the client may hold emits, in the same
transaction, an event for exactly the coordinates it wrote: the final value for
state,incdeltas that sum to the net change forcounter,addandremovefor x2many elements it changed, a delete marker for records it deleted. An event for a coordinate the transaction did not write breaks convergence (the negative control inconvergence-property.test.tsshows it). - A5, ids are not reused within a model.
Read on the primary only
Section titled “Read on the primary only”Answers must be read on the primary. A snapshot taken on a hot standby is not shown to nest with primary
snapshots, so the fetch topics and any read routing (decision D-4 of the slice plan) must keep store answers off
standbys until that is shown. Transaction::store_snapshot enforces it: a transaction routed to the read pool
(effect = read RPC methods can be) is refused with a Programming error naming A2, so a producer that answers
on a deployment with a read pool fails loudly until read routing can pin a method to the primary.
Versions
Section titled “Versions”- The wire major (
WireMajor::CURRENT, currently 1) covers the payload shape, the field kinds and the operations. Adding a kind or an operation bumps it on both sides together.accept_client_majorrefuses any other major, older or newer, withclient_outdated; the transport decides the status or close code. - The schema generation is an opaque token on every payload and on the schema. A payload with a generation
the client does not hold decodes to
needs_schema: the client fetches the schema and decodes again. Change the token whenever a model, field, kind, target, sync class or cap changes.
The reference applier
Section titled “The reference applier”StoreTable in @erpforall/store-wire implements the coordinate rules of spec section 4.4 and is what the client
store wraps. The host decodes with Decoder, then calls one of:
applyAnswer(payload, request?)for an answer;applyFrame(payload)for a bus frame, in the order the bus delivers them.
Both return { resync }: counter coordinates that went stale (log overflow, or a decrement of a capped value) and
must be fetched again. A stale counter shows no value until an answer from a request sent after the last dropped
delta arrives.
Requests are tracked so that a deleted record cannot be brought back by an older answer:
const request = table.beginRequest(); // when the fetch is sent// ... the answer arrives and was decoded ...table.applyAnswer(payload, request);table.settleRequest(request); // also when the fetch is given upbeginRequest() returns an id; a tombstone made while the request is outstanding is kept until it settles.
settleRequest(id) ends a request and releases tombstones that no longer wait for any request; an id that is not
outstanding throws ApplyError. A host that never calls beginRequest gets no tombstone protection and must not
apply answers that could predate a delete.
Applying a payload twice changes nothing, and within one payload every upsert applies before every delete.
Related
Section titled “Related”- Specification:
docs/specs/mail/messaging-store.md. - Generated declarations and fixtures:
docs/dev-setup.md, section “Store wire generated files”.