Debian Codegraph @

third_party/rust/lmdb-rkv/src/database.rs

raw
1 use libc::c_uint;
2 use std::ffi::CString;
3 use std::ptr;
4
5 use ffi;
6
7 use error::{
8 lmdb_result,
9 Result,
10 };
11
12 /// A handle to an individual database in an environment.
13 ///
14 /// A database handle denotes the name and parameters of a database in an environment.
15 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
16 pub struct Database {
17 dbi: ffi::MDB_dbi,
18 }
19
20 impl 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) unsafe fn new(txn: *mut ffi::MDB_txn, name: Option<&str>, flags: c_uint) -> Result<Database> {
26 let c_name = name.map(|n| CString::new(n).unwrap());
27 let name_ptr = if let Some(ref c_name) = c_name {
28 c_name.as_ptr()
29 } else {
30 ptr::null()
31 };
32 let mut dbi: ffi::MDB_dbi = 0;
33 lmdb_result(ffi::mdb_dbi_open(txn, name_ptr, flags, &mut dbi))?;
34 Ok(Database {
35 dbi,
36 })
37 }
38
39 pub(crate) fn freelist_db() -> Database {
40 Database {
41 dbi: 0,
42 }
43 }
44
45 /// Returns the underlying LMDB database handle.
46 ///
47 /// The caller **must** ensure that the handle is not used after the lifetime of the
48 /// environment, or after the database has been closed.
49 #[allow(clippy::trivially_copy_pass_by_ref)]
50 pub fn dbi(&self) -> ffi::MDB_dbi {
51 self.dbi
52 }
53 }
54
55 unsafe impl Sync for Database {}
56 unsafe impl Send for Database {}