Skip to content

Commit 270d08c

Browse files
committed
Introduce the RememberedPoly/Database and OpenedDatabases types
1 parent 4e9a0be commit 270d08c

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

heed/src/db/mod.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,67 @@
1+
use std::marker;
2+
3+
use crate::mdb::ffi;
4+
#[allow(unused_imports)]
5+
use crate::{RoTxn, RwTxn};
6+
17
mod database;
28
mod poly_database;
39

410
pub use self::database::Database;
511
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

Comments
 (0)