Skip to main content

Database

Struct Database 

Source
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>

Source

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>

Source

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>

Source

pub fn query<E: Entity>(&self) -> Query<'_, E, M>

Begin a live primary-key-ordered query. Admission occurs on fetch.

Source§

impl<M: Memory> Database<M>

Source

pub fn create( memory: RuntimeMemory<M>, schema: Schema, incarnation: [u8; 16], ) -> Result<Self, Error>

Create in a proven empty governed allocation. Never resets existing data.

§Errors

Refuses an all-zero incarnation or nonempty/unsupported supplier allocation.

§Panics

Supplier failures after initialization starts trap the enclosing IC message.

Source

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.

Source

pub const fn schema(&self) -> &Schema

Borrow the admitted executable description for inspection.

Source

pub const fn incarnation(&self) -> [u8; 16]

Durable database identity, distinct from schema identity.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn page<E: Entity>( &self, after: Option<E::Key>, limit: usize, ) -> Result<Page<E>, Error>

Read at most 32 rows in primary-key order; continuation is live, not a snapshot.

§Errors

Refuses zero/oversized limits, unadmitted types or corrupt rows.

§Panics

Supplier corruption, including overlong stored keys, traps without repair.

Source

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.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

§

impl<M> !Freeze for Database<M>

§

impl<M> !RefUnwindSafe for Database<M>

§

impl<M> !Send for Database<M>

§

impl<M> !Sync for Database<M>

§

impl<M> !UnwindSafe for Database<M>

§

impl<M> Forget for Database<M>

§

impl<M> Move for Database<M>

§

impl<M> Unpin for Database<M>
where StableBTreeMap<StoreKey, Vec<u8>, M>: Unpin,

§

impl<M> UnsafeUnpin for Database<M>
where StableBTreeMap<StoreKey, Vec<u8>, M>: UnsafeUnpin,

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.