Skip to content

Commit 59bbbc6

Browse files
committed
WIP Move the open/create_database method on the Ro/RwTxn
1 parent 1af05e7 commit 59bbbc6

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

heed/src/env.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl fmt::Debug for Env {
295295

296296
struct EnvInner {
297297
env: *mut ffi::MDB_env,
298-
dbi_open_mutex: sync::Mutex<HashMap<u32, Option<(TypeId, TypeId)>>>,
298+
dbi_open_mutex: sync::Mutex<HashMap<u32, DatabaseType>>,
299299
path: PathBuf,
300300
}
301301

@@ -320,6 +320,17 @@ impl Drop for EnvInner {
320320
}
321321
}
322322

323+
/// The type of the database.
324+
pub enum DatabaseType {
325+
/// The first state of a database, unknown until an [`open_database`] method
326+
/// is called to define the type for the first time.
327+
UnknownYet,
328+
/// Defines the types of a [`Database`].
329+
Typed { key_type: TypeId, data_type: TypeId },
330+
/// Defines the types of a [`PolyDatabase`].
331+
Untyped,
332+
}
333+
323334
/// Whether to perform compaction while copying an environment.
324335
#[derive(Debug, Copy, Clone)]
325336
pub enum CompactionOption {
@@ -572,6 +583,11 @@ impl Env {
572583
let flags = if create { ffi::MDB_CREATE } else { 0 };
573584
match self.raw_open_dbi(raw_txn, name, flags) {
574585
Ok(dbi) => {
586+
let types = match types {
587+
Some((key_type, data_type)) => DatabaseType::Typed { key_type, data_type },
588+
None => DatabaseType::Untyped,
589+
};
590+
575591
let old_types = lock.entry(dbi).or_insert(types);
576592
if *old_types == types {
577593
Ok(dbi)

heed/src/txn.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ptr;
33

44
use crate::mdb::error::mdb_result;
55
use crate::mdb::ffi;
6-
use crate::{Env, Result};
6+
use crate::{Database, Env, PolyDatabase, Result};
77

88
/// A read-only transaction.
99
pub struct RoTxn<'e> {
@@ -30,6 +30,21 @@ impl<'e> RoTxn<'e> {
3030
pub(crate) fn env_mut_ptr(&self) -> *mut ffi::MDB_env {
3131
self.env.env_mut_ptr()
3232
}
33+
34+
pub fn open_database<'t, KC, DC>(
35+
&self,
36+
name: Option<&str>,
37+
) -> Result<Option<Database<'t, KC, DC>>>
38+
where
39+
KC: 'static,
40+
DC: 'static,
41+
{
42+
todo!("get the dbi from the env without any call to LMDB")
43+
}
44+
45+
pub fn open_poly_database<'t>(&self, name: Option<&str>) -> Result<Option<PolyDatabase<'t>>> {
46+
todo!("get the dbi from the env without any call to LMDB")
47+
}
3348
}
3449

3550
impl Drop for RoTxn<'_> {
@@ -76,6 +91,33 @@ impl<'p> RwTxn<'p> {
7691
self.txn.env.env_mut_ptr()
7792
}
7893

94+
pub fn open_database<'t, KC, DC>(
95+
&self,
96+
name: Option<&str>,
97+
) -> Result<Option<Database<'t, KC, DC>>>
98+
where
99+
KC: 'static,
100+
DC: 'static,
101+
{
102+
todo!("call mdb_dbi_open and store the new type in the env")
103+
}
104+
105+
pub fn open_poly_database<'t>(&self, name: Option<&str>) -> Result<Option<PolyDatabase<'t>>> {
106+
todo!("call mdb_dbi_open and store the new type in the env")
107+
}
108+
109+
pub fn create_database<'t, KC, DC>(&self, name: Option<&str>) -> Result<Database<'t, KC, DC>>
110+
where
111+
KC: 'static,
112+
DC: 'static,
113+
{
114+
todo!("call mdb_dbi_open(create) and store the new type in the env")
115+
}
116+
117+
pub fn create_poly_database<'t>(&self, name: Option<&str>) -> Result<PolyDatabase<'t>> {
118+
todo!("call mdb_dbi_open(create) and store the new type in the env")
119+
}
120+
79121
pub fn commit(mut self) -> Result<()> {
80122
let result = unsafe { mdb_result(ffi::mdb_txn_commit(self.txn.txn)) };
81123
self.txn.txn = ptr::null_mut();

0 commit comments

Comments
 (0)