reth_libmdbx/
database.rs

1use crate::{
2    error::{mdbx_result, Result},
3    transaction::TransactionKind,
4    Environment, Transaction,
5};
6use ffi::MDBX_db_flags_t;
7use std::{ffi::CStr, ptr};
8
9/// A handle to an individual database in an environment.
10///
11/// A database handle denotes the name and parameters of a database in an environment.
12#[derive(Debug)]
13pub struct Database {
14    dbi: ffi::MDBX_dbi,
15    /// The environment that this database belongs to keeps it alive as long as the database
16    /// instance exists.
17    _env: Option<Environment>,
18}
19
20impl Database {
21    /// Opens a new database handle in the given transaction.
22    ///
23    /// Prefer using `Environment::open_db`, `Environment::create_db`, `TransactionExt::open_db`,
24    /// or `RwTransaction::create_db`.
25    pub(crate) fn new<K: TransactionKind>(
26        txn: &Transaction<K>,
27        name: Option<&str>,
28        flags: MDBX_db_flags_t,
29    ) -> Result<Self> {
30        let mut c_name_buf = smallvec::SmallVec::<[u8; 32]>::new();
31        let c_name = name.map(|n| {
32            c_name_buf.extend_from_slice(n.as_bytes());
33            c_name_buf.push(0);
34            CStr::from_bytes_with_nul(&c_name_buf).unwrap()
35        });
36        let name_ptr = if let Some(c_name) = c_name { c_name.as_ptr() } else { ptr::null() };
37        let mut dbi: ffi::MDBX_dbi = 0;
38        txn.txn_execute(|txn_ptr| {
39            mdbx_result(unsafe { ffi::mdbx_dbi_open(txn_ptr, name_ptr, flags, &mut dbi) })
40        })??;
41        Ok(Self::new_from_ptr(dbi, txn.env().clone()))
42    }
43
44    pub(crate) const fn new_from_ptr(dbi: ffi::MDBX_dbi, env: Environment) -> Self {
45        Self { dbi, _env: Some(env) }
46    }
47
48    /// Opens the freelist database with DBI `0`.
49    pub const fn freelist_db() -> Self {
50        Self { dbi: 0, _env: None }
51    }
52
53    /// Returns the underlying MDBX database handle.
54    ///
55    /// The caller **must** ensure that the handle is not used after the lifetime of the
56    /// environment, or after the database has been closed.
57    pub const fn dbi(&self) -> ffi::MDBX_dbi {
58        self.dbi
59    }
60}
61
62unsafe impl Send for Database {}
63unsafe impl Sync for Database {}