Skip to main content

Module crdt

Module crdt 

Source
Expand description

Reading a Yjs/CRDT document as plain JSON, so the same index manifest works for CRDT apps (prezillo, ideallo, quillo, calcillo) as for RTDB ones.

§Why this lives here and not in the CRDT adapter

cloudillo_types::crdt_adapter::CrdtAdapter stores opaque binary updates and must stay that way — teaching a storage adapter to parse content would put document semantics in the persistence layer. This module reads the same updates back through the adapter’s public API and materialises them, which keeps the knowledge of what a document means in the search crate where the rest of it already lives.

§The collection model

An RTDB document is a set of collections, each holding documents keyed by id — which is exactly what a manifest’s parts[].kind names. A Yjs document has named root types instead, so this module maps them onto the same shape:

  • a root map is a collection; its keys are document ids
  • a root sequence is a collection, if its entries are structured; its indices are document ids
  • a root Y.Text is a collection holding exactly one document, keyed _, carrying the whole text stream
  • anything else — a list of loose scalars — is skipped

The text case is whole-document granularity on purpose. Prose has no per-entry identity to point a hit at, so there is no interior anchor to offer; collapsing the stream into one entry makes the document findable by its text while keeping the id stable under every edit. Skipping the last case is likewise deliberate rather than a gap: a bare number or bool list is neither prose nor addressable, and the file’s own 'F' row already makes such a document findable by name and tags.

§Undeclared root types

A document replayed purely from its update log has root types the local store has never seen declared, so root_refs() reports them as Out::UndefinedRef rather than as a map or an array. That is the normal case here — nothing in this crate ever calls get_or_insert_map — so the shape is recovered from the branch’s contents instead: a branch with keys is a map, a branch with a sequence is a sequence. Y.Text is indistinguishable from an array at that level, so it is separated by reading the branch as text first: only Y.Text stores its content as string items, so a genuine array reads back as empty text and falls through to the sequence rules.

Functions§

export_all
Materialise a CRDT document as (path, value) pairs in the same "{collection}/{doc_id}" shape cloudillo_types::rtdb_adapter::RtdbAdapter::export_all returns, so the indexer treats both stores identically.