Struct Database

Source
pub struct Database<'a> { /* private fields */ }
Expand description

The database instance. Allows you to create rw_transaction and r_transaction, watch queries, and unwatch etc.

§Example

use native_db::*;

fn main() -> Result<(), db_type::Error> {
   let models = Models::new();
   // Define models ...
   let db = Builder::new().create_in_memory(&models)?;
   // Open transactions
   // Watch data
   // Create snapshots
   // etc...
   Ok(())
}

Implementations§

Source§

impl Database<'_>

Source

pub fn rw_transaction(&self) -> Result<RwTransaction<'_>>

Creates a new read-write transaction. This transaction allows you to read and write data.

  • Write operations:
    • insert - Insert a item.
    • update - Update a item.
    • remove - Remove a item.
    • migrate - Migrate a model, affect all items.
    • commit - Commit the transaction.
    • abort - Abort the transaction.
  • Read operations:
    • get - Get a item.
    • scan - Scan items.
    • len - Get the number of items.
Source

pub fn r_transaction(&self) -> Result<RTransaction<'_>>

Creates a new read-only transaction. This transaction allows you to read data.

  • Read operations:
    • get - Get a item.
    • scan - Scan items.
    • len - Get the number of items.
Source§

impl Database<'_>

Source

pub fn watch(&self) -> Watch<'_>

Watch queries.

  • get - Watch a item.
  • scan - Watch items.
Source

pub fn unwatch(&self, id: u64) -> Result<bool>

Unwatch the given id. You can get the id from the return value of watch. If the id is not valid anymore, this function will do nothing and return false. If the id is valid, the corresponding watcher will be removed and return true. If the id is valid but the watcher is already removed, this function will return false.

Source§

impl<'a> Database<'a>

Source

pub fn metadata(&self) -> &Metadata

Returns the Metadata of the database.

Source

pub fn check_integrity(&mut self) -> Result<bool>

Check the integrity of the database.

Similar to redb::Database::check_integrity().

Source

pub fn compact(&mut self) -> Result<bool>

Compact the database.

Similar to redb::Database::compact().

Source

pub fn upgrading_from_version(&self, selector: &str) -> Result<bool>

Returns true if the database is upgrading from the given version selector.

  • If the database is the old version, not matching the selector the function will return `false.
  • If the database is not upgrading, the function will return always false.

Generally used with the method refresh, to refresh the data for the given model.

Check release notes to know when to use this method.

§Example
if db.upgrading_from_version("<0.8.0") {
    // Do something that runs only when the database is upgrading from version <0.8.0.
    // If the database is already at version 0.8.0, the function will return false and
    // the code will not be executed.
    let rw = db.rw_transaction().unwrap();
    rw.refresh::<Item1>().unwrap();
    rw.refresh::<Item2>().unwrap();
    rw.commit().unwrap();
}
Source

pub fn redb_stats(&self) -> Result<Stats>

Source§

impl Database<'_>

Source

pub fn snapshot<'a>( &self, models: &'a Models, path: &Path, ) -> Result<Database<'a>>

Auto Trait Implementations§

§

impl<'a> !Freeze for Database<'a>

§

impl<'a> !RefUnwindSafe for Database<'a>

§

impl<'a> Send for Database<'a>

§

impl<'a> Sync for Database<'a>

§

impl<'a> Unpin for Database<'a>

§

impl<'a> !UnwindSafe for Database<'a>

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.