diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/lmdb-rkv/src/database.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/lmdb-rkv/src/database.rs')
-rw-r--r-- | third_party/rust/lmdb-rkv/src/database.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/third_party/rust/lmdb-rkv/src/database.rs b/third_party/rust/lmdb-rkv/src/database.rs new file mode 100644 index 0000000000..848af89380 --- /dev/null +++ b/third_party/rust/lmdb-rkv/src/database.rs @@ -0,0 +1,56 @@ +use libc::c_uint; +use std::ffi::CString; +use std::ptr; + +use ffi; + +use error::{ + lmdb_result, + Result, +}; + +/// A handle to an individual database in an environment. +/// +/// A database handle denotes the name and parameters of a database in an environment. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct Database { + dbi: ffi::MDB_dbi, +} + +impl Database { + /// Opens a new database handle in the given transaction. + /// + /// Prefer using `Environment::open_db`, `Environment::create_db`, `TransactionExt::open_db`, + /// or `RwTransaction::create_db`. + pub(crate) unsafe fn new(txn: *mut ffi::MDB_txn, name: Option<&str>, flags: c_uint) -> Result<Database> { + let c_name = name.map(|n| CString::new(n).unwrap()); + let name_ptr = if let Some(ref c_name) = c_name { + c_name.as_ptr() + } else { + ptr::null() + }; + let mut dbi: ffi::MDB_dbi = 0; + lmdb_result(ffi::mdb_dbi_open(txn, name_ptr, flags, &mut dbi))?; + Ok(Database { + dbi, + }) + } + + pub(crate) fn freelist_db() -> Database { + Database { + dbi: 0, + } + } + + /// Returns the underlying LMDB database handle. + /// + /// The caller **must** ensure that the handle is not used after the lifetime of the + /// environment, or after the database has been closed. + #[allow(clippy::trivially_copy_pass_by_ref)] + pub fn dbi(&self) -> ffi::MDB_dbi { + self.dbi + } +} + +unsafe impl Sync for Database {} +unsafe impl Send for Database {} |