heed icon indicating copy to clipboard operation
heed copied to clipboard

Unable to create database when MDB_WRITEMAP is set

Open akhilles opened this issue 3 years ago • 4 comments

To reproduce:

let path = Path::new("target").join("test.mdb");
fs::create_dir_all(&path)?;
let mut env_builder = EnvOpenOptions::new();
unsafe {
    env_builder.flag(Flags::MdbNoSync);
    env_builder.flag(Flags::MdbWriteMap);
}
let env = env_builder.map_size(10 * 1024 * 1024 * 1024).max_dbs(1000).open(path)?;
let db: Database<ByteSlice, Unit> = env.create_database(Some("test"))?;

fails with:

Error: Mdb(BadTxn)

Works fine if env_builder.flag(Flags::MdbWriteMap); is commented out.

akhilles avatar Jan 22 '21 23:01 akhilles

Hey, I am not sure why as I only call the LMDB methods under the hood.

Kerollmops avatar Jan 25 '21 19:01 Kerollmops

Hey, I am not sure why as I only call the LMDB methods under the hood.

Will try to reproduce using only the C library.

akhilles avatar Jan 25 '21 20:01 akhilles

Wasn't able to reproduce using the C library. I think the issue is with nested write transactions. The following patch fixes the issue:

diff --git a/heed/src/env.rs b/heed/src/env.rs
index a8305c5..d126cc9 100644
--- a/heed/src/env.rs
+++ b/heed/src/env.rs
@@ -318,23 +318,20 @@ impl Env {
         KC: 'static,
         DC: 'static,
     {
-        let mut parent_wtxn = self.write_txn()?;
-        let db = self.create_database_with_txn(name, &mut parent_wtxn)?;
-        parent_wtxn.commit()?;
+        let db = self.create_database_with_txn(name)?;
         Ok(db)
     }
 
     pub fn create_database_with_txn<KC, DC>(
         &self,
         name: Option<&str>,
-        parent_wtxn: &mut RwTxn,
     ) -> Result<Database<KC, DC>>
     where
         KC: 'static,
         DC: 'static,
     {
         let types = (TypeId::of::<KC>(), TypeId::of::<DC>());
-        self.raw_create_database(name, types, parent_wtxn)
+        self.raw_create_database(name, types)
             .map(|db| Database::new(self.env_mut_ptr() as _, db))
     }
 
@@ -342,9 +339,8 @@ impl Env {
         &self,
         name: Option<&str>,
         types: (TypeId, TypeId),
-        parent_wtxn: &mut RwTxn,
     ) -> Result<u32> {
-        let wtxn = self.nested_write_txn(parent_wtxn)?;
+        let wtxn = self.write_txn()?;
 
         let mut dbi = 0;
         let name = name.map(|n| CString::new(n).unwrap());

akhilles avatar Jan 28 '21 15:01 akhilles

@timokoesters that's indeed a duplicate of the the issue you opened recently but I have no plan to fix it.

Maybe you could submit a PR, it you do please make sur to start from the right branch (v0.12). It depends on the version you were using you could also start from v0.11.

Kerollmops avatar Jul 30 '21 10:07 Kerollmops

Closed by #128.

Kerollmops avatar Jan 11 '23 13:01 Kerollmops