|
| 1 | +use std::marker; |
| 2 | + |
| 3 | +use crate::mdb::ffi; |
| 4 | +#[allow(unused_imports)] |
| 5 | +use crate::{RoTxn, RwTxn}; |
| 6 | + |
1 | 7 | mod database; |
2 | 8 | mod poly_database; |
3 | 9 |
|
4 | 10 | pub use self::database::Database; |
5 | 11 | pub use self::poly_database::PolyDatabase; |
| 12 | + |
| 13 | +/// A dummy handle to a [`PolyDatabase`] that can be used with an [`OpenedDatabases`] |
| 14 | +/// to retrieve a global handle to a database and operate on them. |
| 15 | +/// |
| 16 | +/// An `OpenedDatabases` type can be retrieve via a call to [`RoTxn::commit`] or [`RwTxn::commit`]. |
| 17 | +pub struct RememberedPolyDatabase { |
| 18 | + pub(crate) unique_txn_id: u64, |
| 19 | + pub(crate) env_ident: usize, |
| 20 | + pub(crate) dbi: ffi::MDB_dbi, |
| 21 | +} |
| 22 | + |
| 23 | +/// A dummy handle to a [`Database`] that can be used with an [`OpenedDatabases`] |
| 24 | +/// to retrieve a global handle to a database and operate on them. |
| 25 | +/// |
| 26 | +/// An `OpenedDatabases` type can be retrieve via a call to [`RoTxn::commit`] or [`RwTxn::commit`]. |
| 27 | +pub struct RememberedDatabase<KC, DC> { |
| 28 | + pub(crate) unique_txn_id: u64, |
| 29 | + pub(crate) env_ident: usize, |
| 30 | + pub(crate) dbi: ffi::MDB_dbi, |
| 31 | + pub(crate) marker: marker::PhantomData<(KC, DC)>, |
| 32 | +} |
| 33 | + |
| 34 | +/// This is used to retrieve global handles to [`Database`]s and [`PolyDatabase`]s |
| 35 | +/// once a [`RoTxn::commit`]/[`RwTxn::commit`] has been executed. |
| 36 | +pub struct OpenedDatabases { |
| 37 | + pub(crate) unique_txn_id: u64, |
| 38 | +} |
| 39 | + |
| 40 | +impl OpenedDatabases { |
| 41 | + /// Give it a [`RememberedPolyDatabase`] and it will return a global handle to this very [`PolyDatabase`]. |
| 42 | + pub fn retrieve_poly_database( |
| 43 | + &self, |
| 44 | + remembered: RememberedPolyDatabase, |
| 45 | + ) -> PolyDatabase<'static> { |
| 46 | + let RememberedPolyDatabase { unique_txn_id, env_ident, dbi } = remembered; |
| 47 | + // create a macro like the `assert_eq_env_txn` or `assert_eq_env_db_txn` ones |
| 48 | + assert_eq!( |
| 49 | + self.unique_txn_id, unique_txn_id, |
| 50 | + "A PolyDatabase must be retrieved from the same transaction as the one it was opened with" |
| 51 | + ); |
| 52 | + PolyDatabase::new(env_ident, dbi) |
| 53 | + } |
| 54 | + |
| 55 | + /// Give it a [`RememberedDatabase`] and it will return a global handle to this very [`Database`]. |
| 56 | + pub fn retrieve_database<KC, DC>( |
| 57 | + &self, |
| 58 | + remembered: RememberedDatabase<KC, DC>, |
| 59 | + ) -> Database<'static, KC, DC> { |
| 60 | + let RememberedDatabase { unique_txn_id, env_ident, dbi, marker: _ } = remembered; |
| 61 | + assert_eq!( |
| 62 | + self.unique_txn_id, unique_txn_id, |
| 63 | + "A Database must be retrieved from the same transaction as the one it was opened with" |
| 64 | + ); |
| 65 | + Database::new(env_ident, dbi) |
| 66 | + } |
| 67 | +} |
0 commit comments