Skip to content

Commit

Permalink
WIP Move the open/create_database method on the Ro/RwTxn
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerollmops committed Apr 8, 2023
1 parent 1af05e7 commit 59bbbc6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
18 changes: 17 additions & 1 deletion heed/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl fmt::Debug for Env {

struct EnvInner {
env: *mut ffi::MDB_env,
dbi_open_mutex: sync::Mutex<HashMap<u32, Option<(TypeId, TypeId)>>>,
dbi_open_mutex: sync::Mutex<HashMap<u32, DatabaseType>>,
path: PathBuf,
}

Expand All @@ -320,6 +320,17 @@ impl Drop for EnvInner {
}
}

/// The type of the database.
pub enum DatabaseType {
/// The first state of a database, unknown until an [`open_database`] method
/// is called to define the type for the first time.
UnknownYet,
/// Defines the types of a [`Database`].
Typed { key_type: TypeId, data_type: TypeId },
/// Defines the types of a [`PolyDatabase`].
Untyped,
}

/// Whether to perform compaction while copying an environment.
#[derive(Debug, Copy, Clone)]
pub enum CompactionOption {
Expand Down Expand Up @@ -572,6 +583,11 @@ impl Env {
let flags = if create { ffi::MDB_CREATE } else { 0 };
match self.raw_open_dbi(raw_txn, name, flags) {
Ok(dbi) => {
let types = match types {
Some((key_type, data_type)) => DatabaseType::Typed { key_type, data_type },
None => DatabaseType::Untyped,
};

let old_types = lock.entry(dbi).or_insert(types);
if *old_types == types {
Ok(dbi)
Expand Down
44 changes: 43 additions & 1 deletion heed/src/txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ptr;

use crate::mdb::error::mdb_result;
use crate::mdb::ffi;
use crate::{Env, Result};
use crate::{Database, Env, PolyDatabase, Result};

/// A read-only transaction.
pub struct RoTxn<'e> {
Expand All @@ -30,6 +30,21 @@ impl<'e> RoTxn<'e> {
pub(crate) fn env_mut_ptr(&self) -> *mut ffi::MDB_env {
self.env.env_mut_ptr()
}

pub fn open_database<'t, KC, DC>(
&self,
name: Option<&str>,
) -> Result<Option<Database<'t, KC, DC>>>
where
KC: 'static,
DC: 'static,
{
todo!("get the dbi from the env without any call to LMDB")
}

pub fn open_poly_database<'t>(&self, name: Option<&str>) -> Result<Option<PolyDatabase<'t>>> {
todo!("get the dbi from the env without any call to LMDB")
}
}

impl Drop for RoTxn<'_> {
Expand Down Expand Up @@ -76,6 +91,33 @@ impl<'p> RwTxn<'p> {
self.txn.env.env_mut_ptr()
}

pub fn open_database<'t, KC, DC>(
&self,
name: Option<&str>,
) -> Result<Option<Database<'t, KC, DC>>>
where
KC: 'static,
DC: 'static,
{
todo!("call mdb_dbi_open and store the new type in the env")
}

pub fn open_poly_database<'t>(&self, name: Option<&str>) -> Result<Option<PolyDatabase<'t>>> {
todo!("call mdb_dbi_open and store the new type in the env")
}

pub fn create_database<'t, KC, DC>(&self, name: Option<&str>) -> Result<Database<'t, KC, DC>>
where
KC: 'static,
DC: 'static,
{
todo!("call mdb_dbi_open(create) and store the new type in the env")
}

pub fn create_poly_database<'t>(&self, name: Option<&str>) -> Result<PolyDatabase<'t>> {
todo!("call mdb_dbi_open(create) and store the new type in the env")
}

pub fn commit(mut self) -> Result<()> {
let result = unsafe { mdb_result(ffi::mdb_txn_commit(self.txn.txn)) };
self.txn.txn = ptr::null_mut();
Expand Down

0 comments on commit 59bbbc6

Please sign in to comment.