pub struct Database<M: Memory> { /* private fields */ }Expand description
Admitted database owning the supplier’s exclusive collection lease.
Implementations§
Source§impl<M: Memory> Database<M>
impl<M: Memory> Database<M>
Sourcepub fn begin_maintenance(
self,
transition: &SchemaTransition,
) -> Result<Maintenance<M>, Error>
pub fn begin_maintenance( self, transition: &SchemaTransition, ) -> Result<Maintenance<M>, Error>
Close ordinary access and bind one transition, consuming the live handle. Performs one synchronous manifest replacement after complete preparation. A later trap in the enclosing IC message rolls back the freeze as well.
§Errors
Refuses a transition for another source contract before persistent mutation.
§Panics
Supplier failures during mutation trap the enclosing IC message.
Source§impl<M: Memory> Database<M>
impl<M: Memory> Database<M>
Sourcepub fn get_many<E: Entity>(
&self,
keys: &[impl Copy + Into<Id<E>>],
) -> GetMany<'_, E, M>
pub fn get_many<E: Entity>( &self, keys: &[impl Copy + Into<Id<E>>], ) -> GetMany<'_, E, M>
Build a lookup for up to 32 raw primary keys or entity-typed IDs, returning one slot per input position.
Each distinct key is looked up once. Missing keys yield None; repeats have independent output slots.
An empty slice needs an element type, e.g. &[] as &[u64] for a Nat64 key.
use ic_db::{Database, Entity, Id};
fn wrong<A: Entity, B: Entity<Key = A::Key>>(db: &Database, ids: &[Id<A>]) {
let _ = db.get_many::<B>(ids);
}Source§impl<M: Memory> Database<M>
impl<M: Memory> Database<M>
Sourcepub fn create(
memory: RuntimeMemory<M>,
schema: Schema,
incarnation: [u8; 16],
) -> Result<Self, Error>
pub fn create( memory: RuntimeMemory<M>, schema: Schema, incarnation: [u8; 16], ) -> Result<Self, Error>
Sourcepub fn open(memory: RuntimeMemory<M>, schema: Schema) -> Result<Self, Error>
pub fn open(memory: RuntimeMemory<M>, schema: Schema) -> Result<Self, Error>
Open only an exact manifest/compiled-contract match, without mutation. This is not a whole-database integrity scan. Entity metadata is checked when read explicitly or when preparing a write to that entity.
§Errors
Refuses active maintenance, missing/corrupt manifests, foreign contracts or supplier formats.
§Panics
Supplier corruption, including overlong stored keys, traps without repair.
Sourcepub const fn schema(&self) -> &Schema
pub const fn schema(&self) -> &Schema
Borrow the admitted executable description for inspection.
Sourcepub const fn incarnation(&self) -> [u8; 16]
pub const fn incarnation(&self) -> [u8; 16]
Durable database identity, distinct from schema identity.
Sourcepub fn revision<E: Entity>(&self) -> Result<u64, Error>
pub fn revision<E: Entity>(&self) -> Result<u64, Error>
Read the atomic revision of one admitted entity.
§Errors
Refuses unknown entities and malformed revision metadata.
Sourcepub fn cardinality<E: Entity>(&self) -> Result<u64, Error>
pub fn cardinality<E: Entity>(&self) -> Result<u64, Error>
Read the exact maintained whole-entity row count without scanning rows. This trusts atomic entity metadata; it does not validate row/index integrity. Filtered counts and row-validating enumeration use the fluent query API.
§Errors
Refuses unadmitted entities and missing or malformed entity metadata.
§Panics
Supplier corruption may trap; this operation never repairs storage.
Sourcepub fn cardinality_by_name(&self, name: &str) -> Result<u64, Error>
pub fn cardinality_by_name(&self, name: &str) -> Result<u64, Error>
Inspect the maintained whole-entity count by exact admitted schema name.
Like Self::cardinality, this reads metadata without validating rows or indexes.
§Errors
Refuses unknown names and missing or malformed entity metadata.
§Panics
Supplier corruption may trap; this operation never repairs storage.
Sourcepub fn get<E: Entity>(&self, key: impl Into<Id<E>>) -> Result<Option<E>, Error>
pub fn get<E: Entity>(&self, key: impl Into<Id<E>>) -> Result<Option<E>, Error>
Read a typed primary key without planning a scan.
§Errors
Refuses unadmitted types or malformed rows.
Sourcepub fn get_unique<K: UniqueKey>(
&self,
key: &K,
) -> Result<Option<K::Entity>, Error>
pub fn get_unique<K: UniqueKey>( &self, key: &K, ) -> Result<Option<K::Entity>, Error>
Read the owner of a complete generated unique key, or None on a miss.
§Errors
Propagates schema, key admission and row integrity errors.
Sourcepub fn page<E: Entity>(
&self,
after: Option<E::Key>,
limit: usize,
) -> Result<Page<E>, Error>
pub fn page<E: Entity>( &self, after: Option<E::Key>, limit: usize, ) -> Result<Page<E>, Error>
Sourcepub fn index_prefix<E: Entity>(
&self,
index: &str,
values: &[IndexValue<'_>],
limit: usize,
) -> Result<Vec<E>, Error>
pub fn index_prefix<E: Entity>( &self, index: &str, values: &[IndexValue<'_>], limit: usize, ) -> Result<Vec<E>, Error>
Read the first bounded collection under an exact, nonempty leading index prefix.
Exact operands obey field bounds and the 512-byte complete-key budget.
Use IndexValue::Absent for an absent optional component. A present-only
unique index refuses absent operands because it does not enumerate those rows.
§Errors
Refuses unknown indexes, mismatched operand types, invalid prefix/limit or inconsistent index entries. This method does not yet return continuation.
Sourcepub fn commit(&mut self, batch: Batch) -> Result<(), Error>
pub fn commit(&mut self, batch: Batch) -> Result<(), Error>
Prepare final state, then synchronously apply rows, indexes, revisions and counts. A normal refusal occurs before persistent writes. Success is not a separate durability point from the surrounding IC message. Empty/no-op batches preserve revisions.
§Errors
Refuses missing/existing rows, duplicate targets, uniqueness/reference violations, corruption or limits. References use the intended final batch state.
§Panics
Supplier failures after the first mutation trap; never catch and continue an IC commit.
Sourcepub fn upsert<E: Entity>(&mut self, row: &E) -> Result<E, Error>
pub fn upsert<E: Entity>(&mut self, row: &E) -> Result<E, Error>
Insert or replace one row by primary key and return its stored value.
§Errors
Propagates batch preparation and commit errors, including unique conflicts.
§Panics
Supplier failures during publication trap as in Self::commit.
Sourcepub fn commit_returning<E: Entity>(
&mut self,
batch: Batch,
key: impl Into<Id<E>>,
) -> Result<E, Error>
pub fn commit_returning<E: Entity>( &mut self, batch: Batch, key: impl Into<Id<E>>, ) -> Result<E, Error>
Commit the whole batch and return the selected insert or replacement, including its final managed timestamps. The row is decoded during preparation.
§Errors
Returns Error::Missing if the batch has no insert or replacement for
this entity and key. All ordinary Self::commit refusals apply.
§Panics
Supplier failures during publication trap as in Self::commit.