pub struct CrdtState { /* private fields */ }Expand description
A CRDT state for a single tenant/namespace.
Implementations§
Source§impl CrdtState
impl CrdtState
Sourcepub fn upsert(
&self,
collection: &str,
row_id: &str,
fields: &[(&str, LoroValue)],
) -> Result<()>
pub fn upsert( &self, collection: &str, row_id: &str, fields: &[(&str, LoroValue)], ) -> Result<()>
Insert or update a row in a collection.
Sourcepub fn delete(&self, collection: &str, row_id: &str) -> Result<()>
pub fn delete(&self, collection: &str, row_id: &str) -> Result<()>
Delete a row from a collection.
Sourcepub fn clear_collection(&self, collection: &str) -> Result<usize>
pub fn clear_collection(&self, collection: &str) -> Result<usize>
Delete all rows in a collection. Returns the number of rows deleted.
Sourcepub fn read_row(&self, collection: &str, row_id: &str) -> Option<LoroValue>
pub fn read_row(&self, collection: &str, row_id: &str) -> Option<LoroValue>
Read a single row’s fields as a LoroValue::Map.
Navigates via LoroMap::get() to avoid the expensive recursive
get_deep_value() clone on the entire row container.
Sourcepub fn read_field(
&self,
collection: &str,
row_id: &str,
field: &str,
) -> Option<LoroValue>
pub fn read_field( &self, collection: &str, row_id: &str, field: &str, ) -> Option<LoroValue>
Read a single field from a row without cloning the entire row.
This is the fast path for KV-style access where only one field is needed. Avoids allocating a full Map for single-field reads.
Shares the same doc.get_map(collection).get(row_id) lookup pattern
as read_row, but returns a single field value instead of the whole
row map — different return granularity, intentionally kept separate.
Sourcepub fn row_exists(&self, collection: &str, row_id: &str) -> bool
pub fn row_exists(&self, collection: &str, row_id: &str) -> bool
Check if a row exists in a collection.
Sourcepub fn collection_names(&self) -> Vec<String>
pub fn collection_names(&self) -> Vec<String>
List all collection names (top-level map keys in the Loro doc).
Sourcepub fn field_value_exists(
&self,
collection: &str,
field: &str,
value: &LoroValue,
) -> bool
pub fn field_value_exists( &self, collection: &str, field: &str, value: &LoroValue, ) -> bool
Check if a value exists for the given field across all rows in a collection. Used for UNIQUE constraint checking.
Sourcepub fn export_snapshot(&self) -> Result<Vec<u8>>
pub fn export_snapshot(&self) -> Result<Vec<u8>>
Export the current state as bytes for sync.
Sourcepub fn compact_history(&mut self) -> Result<()>
pub fn compact_history(&mut self) -> Result<()>
Compact the CRDT history by replacing the internal LoroDoc with a shallow snapshot.
A shallow snapshot contains the current state but discards the full operation history. This is the CRDT equivalent of WAL truncation after checkpoint.
After compaction:
- All current state is preserved (reads return same values).
- New deltas can still be applied and merged.
- Historical operations before the snapshot point are gone.
- Peers that sync after compaction receive a full snapshot instead of incremental deltas (acceptable for long-offline peers).
Call this periodically (e.g., every 30 minutes or when memory pressure exceeds threshold) to prevent unbounded history growth.
Sourcepub fn oplog_version_vector(&self) -> VersionVector
pub fn oplog_version_vector(&self) -> VersionVector
Get the current oplog version vector.
Sourcepub fn read_at_version(
&self,
collection: &str,
row_id: &str,
version: &VersionVector,
) -> Result<Option<LoroValue>>
pub fn read_at_version( &self, collection: &str, row_id: &str, version: &VersionVector, ) -> Result<Option<LoroValue>>
Read the document state at a historical version.
Uses fork_at to create a lightweight copy at the target version
and reads the specified row. Returns None if the row didn’t exist.
Cost: O(oplog_size) for the fork — not for hot-path queries.
Sourcepub fn export_updates_since(
&self,
from_version: &VersionVector,
) -> Result<Vec<u8>>
pub fn export_updates_since( &self, from_version: &VersionVector, ) -> Result<Vec<u8>>
Export the oplog delta from a version to the current state.
Returns the operations that transform from_version into current state.
Used for DIFF rendering and delta sync.
Sourcepub fn compact_at_version(&mut self, version: &VersionVector) -> Result<()>
pub fn compact_at_version(&mut self, version: &VersionVector) -> Result<()>
Compact history at a specific version (not just current frontiers).
Discards oplog entries before the target version. Current state and all versions after the target are preserved.
Sourcepub fn restore_to_version(
&self,
collection: &str,
row_id: &str,
version: &VersionVector,
) -> Result<Vec<u8>>
pub fn restore_to_version( &self, collection: &str, row_id: &str, version: &VersionVector, ) -> Result<Vec<u8>>
Restore a document to a historical version by creating a forward delta.
Reads the state at the target version, then generates a new mutation that sets the current state to match the historical state. History is preserved — this is a forward operation, not a rollback.
Returns the delta bytes to be applied through the normal write path.
Sourcepub fn estimated_memory_bytes(&self) -> usize
pub fn estimated_memory_bytes(&self) -> usize
Estimated memory usage of the CRDT state (bytes).
Includes operation history, current state, and internal caches.
Use this to decide when to trigger compact_history().
Auto Trait Implementations§
impl Freeze for CrdtState
impl RefUnwindSafe for CrdtState
impl Send for CrdtState
impl Sync for CrdtState
impl Unpin for CrdtState
impl UnsafeUnpin for CrdtState
impl UnwindSafe for CrdtState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);