-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce the RememberedPoly/Database and OpenedDatabases types
- Loading branch information
1 parent
4e9a0be
commit 270d08c
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,67 @@ | ||
use std::marker; | ||
|
||
use crate::mdb::ffi; | ||
#[allow(unused_imports)] | ||
use crate::{RoTxn, RwTxn}; | ||
|
||
mod database; | ||
mod poly_database; | ||
|
||
pub use self::database::Database; | ||
pub use self::poly_database::PolyDatabase; | ||
|
||
/// A dummy handle to a [`PolyDatabase`] that can be used with an [`OpenedDatabases`] | ||
/// to retrieve a global handle to a database and operate on them. | ||
/// | ||
/// An `OpenedDatabases` type can be retrieve via a call to [`RoTxn::commit`] or [`RwTxn::commit`]. | ||
pub struct RememberedPolyDatabase { | ||
pub(crate) unique_txn_id: u64, | ||
pub(crate) env_ident: usize, | ||
pub(crate) dbi: ffi::MDB_dbi, | ||
} | ||
|
||
/// A dummy handle to a [`Database`] that can be used with an [`OpenedDatabases`] | ||
/// to retrieve a global handle to a database and operate on them. | ||
/// | ||
/// An `OpenedDatabases` type can be retrieve via a call to [`RoTxn::commit`] or [`RwTxn::commit`]. | ||
pub struct RememberedDatabase<KC, DC> { | ||
pub(crate) unique_txn_id: u64, | ||
pub(crate) env_ident: usize, | ||
pub(crate) dbi: ffi::MDB_dbi, | ||
pub(crate) marker: marker::PhantomData<(KC, DC)>, | ||
} | ||
|
||
/// This is used to retrieve global handles to [`Database`]s and [`PolyDatabase`]s | ||
/// once a [`RoTxn::commit`]/[`RwTxn::commit`] has been executed. | ||
pub struct OpenedDatabases { | ||
pub(crate) unique_txn_id: u64, | ||
} | ||
|
||
impl OpenedDatabases { | ||
/// Give it a [`RememberedPolyDatabase`] and it will return a global handle to this very [`PolyDatabase`]. | ||
pub fn retrieve_poly_database( | ||
&self, | ||
remembered: RememberedPolyDatabase, | ||
) -> PolyDatabase<'static> { | ||
let RememberedPolyDatabase { unique_txn_id, env_ident, dbi } = remembered; | ||
// create a macro like the `assert_eq_env_txn` or `assert_eq_env_db_txn` ones | ||
assert_eq!( | ||
self.unique_txn_id, unique_txn_id, | ||
"A PolyDatabase must be retrieved from the same transaction as the one it was opened with" | ||
); | ||
PolyDatabase::new(env_ident, dbi) | ||
} | ||
|
||
/// Give it a [`RememberedDatabase`] and it will return a global handle to this very [`Database`]. | ||
pub fn retrieve_database<KC, DC>( | ||
&self, | ||
remembered: RememberedDatabase<KC, DC>, | ||
) -> Database<'static, KC, DC> { | ||
let RememberedDatabase { unique_txn_id, env_ident, dbi, marker: _ } = remembered; | ||
assert_eq!( | ||
self.unique_txn_id, unique_txn_id, | ||
"A Database must be retrieved from the same transaction as the one it was opened with" | ||
); | ||
Database::new(env_ident, dbi) | ||
} | ||
} |