Skip to main content

TypedCollection

Trait TypedCollection 

Source
pub trait TypedCollection<M>: Send + Sync{
    // Required methods
    fn upsert(&self, doc_id: &str, message: &M) -> Result<()>;
    fn get(&self, doc_id: &str) -> Result<Option<M>>;
    fn delete(&self, doc_id: &str) -> Result<()>;
    fn scan(&self) -> Result<Vec<(String, M)>>;
    fn find(
        &self,
        predicate: Box<dyn Fn(&M) -> bool + Send>,
    ) -> Result<Vec<(String, M)>>;
    fn count(&self) -> Result<usize>;
}
Expand description

Typed collection trait for CRDT-optimized storage

Backends that implement CrdtCapable provide this trait to enable field-level conflict resolution via CRDT semantics.

§Type Parameters

  • M - Protobuf message type with serde support

§CRDT Semantics

Different field types get different CRDT semantics:

  • Arrays: OR-Set (observed-remove set) - concurrent additions merge
  • Scalars: LWW-Register (last-write-wins) - timestamp-based resolution
  • Nested objects: Recursive application of above rules

§Example

use peat_protocol::storage::{CrdtCapable, TypedCollection};
use peat_schema::hierarchy::v1::SquadSummary;

let backend = AutomergeIrohBackend::new(config);
let squads: Arc<dyn TypedCollection<SquadSummary>> =
    backend.typed_collection("squads");

// Field-level updates
let mut summary = squads.get("squad-1")?.unwrap();
summary.member_ids.push("node-4".to_string());  // OR-Set addition
squads.upsert("squad-1", &summary)?;
// → Only member_ids field is transmitted, not entire document

Required Methods§

Source

fn upsert(&self, doc_id: &str, message: &M) -> Result<()>

Insert or update a typed document with CRDT merging

Backends convert the message to their CRDT format:

  • Automerge: message → Automerge document
Source

fn get(&self, doc_id: &str) -> Result<Option<M>>

Get a typed document by ID

Source

fn delete(&self, doc_id: &str) -> Result<()>

Delete a typed document by ID

Source

fn scan(&self) -> Result<Vec<(String, M)>>

Scan all typed documents in the collection

Source

fn find( &self, predicate: Box<dyn Fn(&M) -> bool + Send>, ) -> Result<Vec<(String, M)>>

Find typed documents matching a predicate

Source

fn count(&self) -> Result<usize>

Count typed documents in the collection

Implementors§

Source§

impl<M> TypedCollection<M> for AutomergeTypedCollection<M>

Available on crate feature automerge-backend only.