pub struct ReplicaState { /* private fields */ }Expand description
A durable per-replica CRDT state — one participant (an editor’s FFI node, or the supervisor) in a multi-replica document session.
Unlike crate::crdt::CrdtDoc used by the rebuild-from-text merge, a
ReplicaState is kept across cycles: it accumulates ops and exchanges
only deltas with its peers via ReplicaState::state_vector /
ReplicaState::diff / ReplicaState::apply_update.
Implementations§
Source§impl ReplicaState
impl ReplicaState
Sourcepub fn new(client_id: u64) -> Self
pub fn new(client_id: u64) -> Self
Create a fresh empty replica with a stable client_id. Distinct replicas
MUST use distinct client ids so concurrent inserts order deterministically
(yrs orders concurrent inserts at the same position by ascending client
id, matching the agent-before-human convention in crate::crdt).
Sourcepub fn from_encoded(client_id: u64, state: &[u8]) -> Result<Self>
pub fn from_encoded(client_id: u64, state: &[u8]) -> Result<Self>
Bootstrap a replica from a previously encoded state (the durable boundary checkpoint / a peer’s full snapshot on first contact).
Sourcepub fn apply_local_edit(&self, offset: u32, delete_len: u32, insert: &str)
pub fn apply_local_edit(&self, offset: u32, delete_len: u32, insert: &str)
Apply a local edit: delete delete_len chars at offset, then insert
insert there (one transaction). Mirrors crate::crdt::CrdtDoc::apply_edit.
Sourcepub fn state_vector(&self) -> Vec<u8> ⓘ
pub fn state_vector(&self) -> Vec<u8> ⓘ
This replica’s state vector, encoded for the wire. The state vector is the
compact per-client causal summary (each client’s clock), NOT the whole
document: a peer replies with diff carrying only the ops
this vector is missing.
Sourcepub fn diff(&self, their_sv: &[u8]) -> Result<Vec<u8>>
pub fn diff(&self, their_sv: &[u8]) -> Result<Vec<u8>>
The incremental update carrying exactly the ops their_sv is missing
(encode_state_as_update(their_sv)) — a delta, never a whole-document
snapshot. This is the sync reply a replica sends a peer that announced
their_sv.
Sourcepub fn apply_update(&self, update: &[u8]) -> Result<()>
pub fn apply_update(&self, update: &[u8]) -> Result<()>
Apply a remote update. Idempotent (re-applying a known update is a no-op) and causal-buffered by yrs (an update whose causal dependencies have not arrived is buffered and integrated once they do), so duplicate or out-of-order delivery converges rather than corrupting.
Sourcepub fn encode_state(&self) -> Vec<u8> ⓘ
pub fn encode_state(&self) -> Vec<u8> ⓘ
The full encoded state — a durable projection / boundary checkpoint, or the snapshot a peer needs on first contact (no shared history yet).