Skip to content

Commit

Permalink
enable self referential rtxn
Browse files Browse the repository at this point in the history
  • Loading branch information
irevoire committed May 6, 2024
1 parent 522cf8c commit a3339c8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions heed/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,10 @@ impl Env {
RoTxn::new(self)
}

pub fn self_referenced_read_txn(&self) -> Result<RoTxn> {
RoTxn::new_self_reference(self)
}

/// Copy an LMDB environment to the specified path, with options.
///
/// This function may be used to make a backup of an existing environment.
Expand Down
25 changes: 21 additions & 4 deletions heed/src/txn.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::ops::Deref;
use std::ptr;

Expand Down Expand Up @@ -26,7 +27,7 @@ use crate::{Env, Result};
/// You may increase the limit by editing it **at your own risk**: `/Library/LaunchDaemons/sysctl.plist`
pub struct RoTxn<'e> {
pub(crate) txn: *mut ffi::MDB_txn,
env: &'e Env,
env: Cow<'e, Env>,
}

impl<'e> RoTxn<'e> {
Expand All @@ -42,7 +43,23 @@ impl<'e> RoTxn<'e> {
))?
};

Ok(RoTxn { txn, env })
Ok(RoTxn { txn, env: Cow::Borrowed(env) })
}

pub(crate) fn new_self_reference(env: &Env) -> Result<RoTxn<'static>> {
let env = env.clone();
let mut txn: *mut ffi::MDB_txn = ptr::null_mut();

unsafe {
mdb_result(ffi::mdb_txn_begin(
env.env_mut_ptr(),
ptr::null_mut(),
ffi::MDB_RDONLY,
&mut txn,
))?
};

Ok(RoTxn { txn, env: Cow::Owned(env) })
}

pub(crate) fn env_mut_ptr(&self) -> *mut ffi::MDB_env {
Expand Down Expand Up @@ -110,7 +127,7 @@ impl<'p> RwTxn<'p> {

unsafe { mdb_result(ffi::mdb_txn_begin(env.env_mut_ptr(), ptr::null_mut(), 0, &mut txn))? };

Ok(RwTxn { txn: RoTxn { txn, env } })
Ok(RwTxn { txn: RoTxn { txn, env: Cow::Borrowed(env) } })
}

pub(crate) fn nested(env: &'p Env, parent: &'p mut RwTxn) -> Result<RwTxn<'p>> {
Expand All @@ -119,7 +136,7 @@ impl<'p> RwTxn<'p> {

unsafe { mdb_result(ffi::mdb_txn_begin(env.env_mut_ptr(), parent_ptr, 0, &mut txn))? };

Ok(RwTxn { txn: RoTxn { txn, env } })
Ok(RwTxn { txn: RoTxn { txn, env: Cow::Borrowed(env) } })
}

pub(crate) fn env_mut_ptr(&self) -> *mut ffi::MDB_env {
Expand Down

0 comments on commit a3339c8

Please sign in to comment.