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
impl Collection
Sourcepub const fn new(
pull_capability: &'static str,
table_prefix: &'static str,
schemas: &'static [u32],
) -> Self
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.
Sourcepub const fn with_environment(self, env: &'static dyn Environment) -> Self
pub const fn with_environment(self, env: &'static dyn Environment) -> Self
Replaces the clock and the epoch source.
Sourcepub const fn with_tombstone_horizon(
self,
max_age_ms: u64,
max_count: u64,
) -> Self
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).
Sourcepub fn ensure<S: LedgerStore + ?Sized>(
&self,
store: &S,
) -> Result<(), ReplicaError>
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.
Sourcepub fn reconcile<S: LedgerStore + ?Sized>(
&self,
store: &S,
rows: &[Row],
) -> Result<Option<u64>, ReplicaError>
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.
Sourcepub fn touch<S: LedgerStore + ?Sized>(
&self,
store: &S,
row: &Row,
) -> Result<u64, ReplicaError>
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.
Sourcepub fn remove<S: LedgerStore + ?Sized>(
&self,
store: &S,
key: &str,
) -> Result<u64, ReplicaError>
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.
Sourcepub fn mark_dirty<S: LedgerStore + ?Sized>(
&self,
store: &S,
) -> Result<(), ReplicaError>
pub fn mark_dirty<S: LedgerStore + ?Sized>( &self, store: &S, ) -> Result<(), ReplicaError>
“Something may have changed”: the next pull reconciles before answering.
Sourcepub fn reset<S: LedgerStore + ?Sized>(
&self,
store: &S,
) -> Result<bool, ReplicaError>
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.
Sourcepub fn head<S: LedgerStore + ?Sized>(
&self,
store: &S,
) -> Result<Head, ReplicaError>
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.
Sourcepub fn answer<S, L>(
&self,
store: &S,
request: &PullRequest,
load: L,
) -> Result<Answered, ReplicaError>
pub fn answer<S, L>( &self, store: &S, request: &PullRequest, load: L, ) -> Result<Answered, ReplicaError>
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.
Sourcepub fn pull<S, L>(
&self,
store: &S,
request: &Value,
load: L,
) -> Result<Value, String>
pub fn pull<S, L>( &self, store: &S, request: &Value, load: L, ) -> Result<Value, 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.
Sourcepub fn announce<S: LedgerStore + ?Sized>(
&self,
store: &S,
bus: &dyn Announcer,
) -> AnnounceOutcome
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.
Sourcepub fn description(&self) -> String
pub fn description(&self) -> String
The description for this capability’s provided_capabilities entry.