Skip to main content

Collection

Struct Collection 

Source
pub struct Collection { /* private fields */ }
Expand description

One replicated collection: its pull capability, the prefix of the ledger’s tables, and the schema numbers it serves. Declare it as a static.

Implementations§

Source§

impl Collection

Source

pub const fn new( pull_capability: &'static str, table_prefix: &'static str, schemas: &'static [u32], ) -> Self

pull_capability is <app>.<collection>.pull and names the collection in the ledger; table_prefix ([A-Za-z0-9_]{0,48}) names the ledger’s tables (friends_ → friends_replica_collections, friends_replica_rows); schemas are the Ns of the .vN names this app serves.

Source

pub const fn with_environment(self, env: &'static dyn Environment) -> Self

Replaces the clock and the epoch source.

Source

pub const fn with_tombstone_horizon( self, max_age_ms: u64, max_count: u64, ) -> Self

Replaces the tombstone horizon (default 30 days and 5,000 per collection).

Source

pub fn name(&self) -> &'static str

The pull capability.

Source

pub fn schemas(&self) -> &'static [u32]

The schema numbers this collection serves.

Source

pub fn ensure<S: LedgerStore + ?Sized>( &self, store: &S, ) -> Result<(), ReplicaError>

Creates the ledger’s tables if they do not exist. Call it where the app creates its own tables, on every start: it is idempotent.

Source

pub fn reconcile<S: LedgerStore + ?Sized>( &self, store: &S, rows: &[Row], ) -> Result<Option<u64>, ReplicaError>

Whole-list mode: hand over every current row; the helper finds what changed. Returns the new head version when the head moved. Rows are checked against the contract’s limits first; one bad row refuses the whole list.

Source

pub fn touch<S: LedgerStore + ?Sized>( &self, store: &S, row: &Row, ) -> Result<u64, ReplicaError>

Records one row written at a write site. Returns the head version.

Source

pub fn remove<S: LedgerStore + ?Sized>( &self, store: &S, key: &str, ) -> Result<u64, ReplicaError>

Records one row deleted at a write site. Returns the head version.

Source

pub fn mark_dirty<S: LedgerStore + ?Sized>( &self, store: &S, ) -> Result<(), ReplicaError>

“Something may have changed”: the next pull reconciles before answering.

Source

pub fn reset<S: LedgerStore + ?Sized>( &self, store: &S, ) -> Result<bool, ReplicaError>

Starts a new epoch (the app’s data was replaced) and marks the collection dirty. Every device takes one full copy. Returns whether it rotated now; within a minute of the last rotation the rotation is owed instead (it happens before the next version is assigned, or at the first ledger operation after the minute). The collection is marked dirty either way.

A database restored from a backup needs no call: the helper finds that out by itself (a device ahead of the head, the node’s rotate, or this process having handed out more) and rotates at once.

Source

pub fn head<S: LedgerStore + ?Sized>( &self, store: &S, ) -> Result<Head, ReplicaError>

The ledger’s head: what an announce reports. A plain read of the committed header (no write lock), unless the ledger has none yet, went back in time or owes a rotation that is due: then the write path settles that first.

Source

pub fn answer<S, L>( &self, store: &S, request: &PullRequest, load: L, ) -> Result<Answered, ReplicaError>
where S: LedgerStore + ?Sized, L: FnMut(&S, Option<&[String]>) -> Result<Vec<Row>, String>,

Answers one pull. load returns the app’s current rows: every row for None, else the rows of the given keys (keys with no row are left out). It runs inside the ledger’s write transaction, so on SQLite it reads the same snapshot the ledger does; it must not call this collection again.

Source

pub fn pull<S, L>( &self, store: &S, request: &Value, load: L, ) -> Result<Value, String>
where S: LedgerStore + ?Sized, L: FnMut(&S, Option<&[String]>) -> Result<Vec<Row>, String>,

The pull capability’s handler: parses the payload (ignoring the keys the router stamps in), answers, and returns the reply object. The error text starts with a stable prefix (replica_schema_unsupported: and others).

A Value object keeps serde_json’s key order, not the contract’s; the node decodes it into PullReply and writes the device’s bytes from the typed form. Use Collection::answer for the typed reply.

Source

pub fn announce<S: LedgerStore + ?Sized>( &self, store: &S, bus: &dyn Announcer, ) -> AnnounceOutcome

Tells the node this collection’s head (core.replica.announce). Call it after a write that moved the head and once at app start. When the node answers {"rotate":true} the helper rotates the epoch and announces again.

Source

pub fn description(&self) -> String

The description for this capability’s provided_capabilities entry.

Trait Implementations§

Source§

impl Debug for Collection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.