Skip to content

Commit 2e6b8cf

Browse files
committed
Introduce static_write_txn
1 parent 14e3e49 commit 2e6b8cf

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

heed/src/envs/encrypted_env.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ impl<T> EncryptedEnv<T> {
176176

177177
/// Create a transaction with read and write access for use with the environment.
178178
///
179+
/// See [`Self::static_write_txn`] if you want the txn to own the environment.
180+
///
179181
/// ## LMDB Limitations
180182
///
181183
/// Only one [`RwTxn`] may exist simultaneously in the current environment.
@@ -186,6 +188,20 @@ impl<T> EncryptedEnv<T> {
186188
self.inner.write_txn()
187189
}
188190

191+
/// Create a transaction with read and write access for use with the environment.
192+
/// Contrary to [`Self::write_txn`], this version **owns** the environment, which
193+
/// means you won't be able to close the environment while this transaction is alive.
194+
///
195+
/// ## LMDB Limitations
196+
///
197+
/// Only one [`RwTxn`] may exist simultaneously in the current environment.
198+
/// If another write transaction is initiated, while another write transaction exists
199+
/// the thread initiating the new one will wait on a mutex upon completion of the previous
200+
/// transaction.
201+
pub fn static_write_txn(self) -> Result<RwTxn<'static>> {
202+
self.inner.static_write_txn()
203+
}
204+
189205
/// Create a nested transaction with read and write access for use with the environment.
190206
///
191207
/// The new transaction will be a nested transaction, with the transaction indicated by parent

heed/src/envs/env.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ impl<T> Env<T> {
374374

375375
/// Create a transaction with read and write access for use with the environment.
376376
///
377+
/// See [`Self::static_write_txn`] if you want the txn to own the environment.
378+
///
377379
/// ## LMDB Limitations
378380
///
379381
/// Only one [`RwTxn`] may exist simultaneously in the current environment.
@@ -384,6 +386,20 @@ impl<T> Env<T> {
384386
RwTxn::new(self)
385387
}
386388

389+
/// Create a transaction with read and write access for use with the environment.
390+
/// Contrary to [`Self::write_txn`], this version **owns** the environment, which
391+
/// means you won't be able to close the environment while this transaction is alive.
392+
///
393+
/// ## LMDB Limitations
394+
///
395+
/// Only one [`RwTxn`] may exist simultaneously in the current environment.
396+
/// If another write transaction is initiated, while another write transaction exists
397+
/// the thread initiating the new one will wait on a mutex upon completion of the previous
398+
/// transaction.
399+
pub fn static_write_txn(self) -> Result<RwTxn<'static>> {
400+
RwTxn::static_write_txn(self)
401+
}
402+
387403
/// Create a nested transaction with read and write access for use with the environment.
388404
///
389405
/// The new transaction will be a nested transaction, with the transaction indicated by parent

heed/src/txn.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,26 @@ impl<'p> RwTxn<'p> {
298298
})
299299
}
300300

301+
pub(crate) fn static_write_txn<T>(env: Env<T>) -> Result<RwTxn<'static>> {
302+
let mut txn: *mut ffi::MDB_txn = ptr::null_mut();
303+
304+
unsafe {
305+
mdb_result(ffi::mdb_txn_begin(
306+
env.env_mut_ptr().as_mut(),
307+
ptr::null_mut(),
308+
0,
309+
&mut txn,
310+
))?
311+
};
312+
313+
Ok(RwTxn {
314+
txn: RoTxn {
315+
inner: RoTxnInner { txn: NonNull::new(txn), env: Cow::Owned(env.inner) },
316+
_tls_marker: PhantomData,
317+
},
318+
})
319+
}
320+
301321
pub(crate) fn nested<T>(env: &'p Env<T>, parent: &'p mut RwTxn) -> Result<RwTxn<'p>> {
302322
let mut txn: *mut ffi::MDB_txn = ptr::null_mut();
303323
let parent_ptr: *mut ffi::MDB_txn = unsafe { parent.txn.inner.txn.unwrap().as_mut() };

0 commit comments

Comments
 (0)