Skip to main content

signet_libmdbx/tx/
database.rs

1use crate::flags::DatabaseFlags;
2
3/// A handle to an individual database in an environment.
4///
5/// A database handle denotes the name and parameters of a database in an
6/// environment.
7///
8/// `Database` is a simple data container holding the database handle index
9/// (dbi) and its flags. It does not own any resources and can be freely
10/// copied.
11///
12/// # Lifetime
13///
14/// The database handle is only valid within the lifetime of the environment
15/// that created it. Users must ensure that `Database` instances are not used
16/// after the environment has been closed.
17#[derive(Debug, Clone, Copy)]
18pub struct Database {
19    dbi: ffi::MDBX_dbi,
20    flags: DatabaseFlags,
21}
22
23impl Database {
24    /// Creates a new Database from a dbi and flags.
25    pub(crate) const fn new(dbi: ffi::MDBX_dbi, flags: DatabaseFlags) -> Self {
26        Self { dbi, flags }
27    }
28
29    /// Opens the freelist database with DBI `0`.
30    pub const fn freelist_db() -> Self {
31        Self { dbi: 0, flags: DatabaseFlags::empty() }
32    }
33
34    /// Returns the underlying MDBX database handle.
35    ///
36    /// The caller **must** ensure that the handle is not used after the
37    /// lifetime of the environment, or after the database has been closed.
38    pub const fn dbi(&self) -> ffi::MDBX_dbi {
39        self.dbi
40    }
41
42    /// Returns the database flags.
43    pub const fn flags(&self) -> DatabaseFlags {
44        self.flags
45    }
46}