Struct Database

Source
pub struct Database<E>
where E: DatabaseKind,
{ /* private fields */ }
Expand description

Supports multiple tables, all residing in the same shared-memory map.

Implementations§

Source§

impl<E> Database<E>
where E: DatabaseKind,

Source

pub fn open(path: impl AsRef<Path>) -> Result<Database<E>>

Open a database.

Source

pub fn open_with_options( path: impl AsRef<Path>, options: DatabaseOptions, ) -> Result<Database<E>>

Source

pub fn ptr(&self) -> DbPtr

Returns a raw pointer to the underlying MDBX database.

The caller must ensure that the pointer is not dereferenced after the lifetime of the database.

Source

pub fn begin_ro_txn(&self) -> Result<Transaction<'_, RO, E>>

Create a read-only transaction for use with the database.

Source

pub fn begin_rw_txn(&self) -> Result<Transaction<'_, RW, E>>

Create a read-write transaction for use with the database. This method will block while there are any other read-write transactions open on the database.

Source

pub fn sync(&self, force: bool) -> Result<bool>

Flush the database data buffers to disk.

Source

pub fn stat(&self) -> Result<Stat>

Retrieves statistics about this database.

Source

pub fn info(&self) -> Result<Info>

Retrieves info about this database.

Source

pub fn freelist(&self) -> Result<usize>

Retrieves the total number of pages on the freelist.

Along with Database::info(), this can be used to calculate the exact number of used pages as well as free pages in this database.

let dir = tempfile::tempdir().unwrap();
let db = Database::<NoWriteMap>::open(&dir).unwrap();
let info = db.info().unwrap();
let stat = db.stat().unwrap();
let freelist = db.freelist().unwrap();
let last_pgno = info.last_pgno() + 1; // pgno is 0 based.
let total_pgs = info.map_size() / stat.page_size() as usize;
let pgs_in_use = last_pgno - freelist;
let pgs_free = total_pgs - pgs_in_use;

Note:

  • MDBX stores all the freelists in the designated table 0 in each database, and the freelist count is stored at the beginning of the value as 32-bit integer in the native byte order.

  • It will create a read transaction to traverse the freelist table.

Trait Implementations§

Source§

impl<E> Debug for Database<E>
where E: DatabaseKind,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<E> Drop for Database<E>
where E: DatabaseKind,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<E> Freeze for Database<E>

§

impl<E> RefUnwindSafe for Database<E>
where E: RefUnwindSafe,

§

impl<E> Send for Database<E>
where E: Send,

§

impl<E> Sync for Database<E>
where E: Sync,

§

impl<E> Unpin for Database<E>
where E: Unpin,

§

impl<E> UnwindSafe for Database<E>
where E: UnwindSafe,

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.