Struct ord_lmdb_zero::WriteTransaction
source · [−]pub struct WriteTransaction<'env>(_);
Expand description
A read-write LMDB transaction.
In addition to all operations valid on ConstTransaction
, it is also
possible to perform writes to the underlying databases.
Ownership
WriteTransaction
s can be created with all three ownership modes (but
owned mode is not useful).
Example — Shared mode
use std::sync::Arc;
let env = Arc::new(create_env());
let db = Arc::new(lmdb::Database::open(
env.clone(), None, &lmdb::DatabaseOptions::defaults()).unwrap());
// Type and lifetime annotated explicitly for clarity
let txn: lmdb::WriteTransaction<'static> = lmdb::WriteTransaction::new(
env.clone()).unwrap();
// Do stuff with `txn`...
txn.commit().unwrap();
Lifetime
All notes for ConstTransaction
apply.
Implementations
sourceimpl<'env> WriteTransaction<'env>
impl<'env> WriteTransaction<'env>
sourcepub fn new<E>(env: E) -> Result<Self> where
E: Into<NonSyncSupercow<'env, Environment>>,
pub fn new<E>(env: E) -> Result<Self> where
E: Into<NonSyncSupercow<'env, Environment>>,
Creates a new, read-write transaction in the given environment.
Note
A transaction and its cursors must only be used by a single thread
(enforced by the rust compiler), and a thread may only have a single
read-write transaction at a time (even if NOTLS
is in use — trying
to start two top-level read-write transactions on the same thread will
deadlock).
sourcepub fn child_tx<'a>(&'a mut self) -> Result<WriteTransaction<'a>> where
'env: 'a,
pub fn child_tx<'a>(&'a mut self) -> Result<WriteTransaction<'a>> where
'env: 'a,
Opens a new, read-write transaction as a child transaction of the given parent. While the new transaction exists, no operations may be performed on the parent or any of its cursors. (These bindings are actually stricter, and do not permit cursors or other references into the parent to coexist with the child transaction.)
After this call, whether or not it succeeds, it is possible to call
access()
on the original transaction again one more time, since the
Rust borrow rules guarantee the old accessor was destroyed by the
caller already.
Note
A transaction and its cursors must only be used by a single thread (enforced by the rust compiler).
Example
let db = lmdb::Database::open(
&env, None, &lmdb::DatabaseOptions::defaults()).unwrap();
let mut txn = lmdb::WriteTransaction::new(&env).unwrap();
let f = lmdb::put::Flags::empty();
{
let mut access = txn.access();
access.put(&db, "Germany", "Berlin", f).unwrap();
access.put(&db, "Latvia", "Rīga", f).unwrap();
access.put(&db, "France", "Paris", f).unwrap();
}
{
// Open a child transaction and do some more reading and writing.
let subtx = txn.child_tx().unwrap();
let mut access = subtx.access();
assert_eq!("Berlin", access.get::<str,str>(&db, "Germany").unwrap());
access.put(&db, "Germany", "Frankfurt", f).unwrap();
assert_eq!("Frankfurt", access.get::<str,str>(&db, "Germany").unwrap());
// Don't commit --- let the child transaction abort (roll back)
}
{
let mut access = txn.access();
// Now we can do some more reading and writing on the original
// transaction.
// The effect of the aborted child transaction are not visible.
access.put(&db, "United Kingdom", "London", f).unwrap();
assert_eq!("Berlin", access.get::<str,str>(&db, "Germany").unwrap());
}
{
// Another child.
let subtx = txn.child_tx().unwrap();
{
let mut access = subtx.access();
access.put(&db, "Spain", "Madrid", f).unwrap();
}
// Commit this one this time.
subtx.commit().unwrap();
}
{
// Now the changes from the child are visible to this transaction,
// but still not outside it.
let mut access = txn.access();
assert_eq!("Madrid", access.get::<str,str>(&db, "Spain").unwrap());
}
txn.commit().unwrap();
sourcepub fn access(&self) -> WriteAccessor<'_>
pub fn access(&self) -> WriteAccessor<'_>
Returns a read/write accessor on this transaction.
Panics
Panics if an accessor has already been obtained from this transaction and not yet dropped.
Methods from Deref<Target = ConstTransaction<'env>>
sourcepub fn access(&self) -> ConstAccessor<'_>
pub fn access(&self) -> ConstAccessor<'_>
Returns an accessor used to manipulate data in this transaction.
Ownership
Unlike most other lmdb-zero APIs, accessors do not support shared
ownership modes (e.g., where the accessor would hold on to a
Rc<ConstTransaction>
). If you need dynamically-managed lifetime,
instead simply drop the accessor and get a new one the next time one is
needed.
Panics
Panics if this function has already been called on this transaction and the returned value has not yet been dropped.
Example
let txn = lmdb::ReadTransaction::new(&env).unwrap();
// Get access the first time
let access = txn.access();
// You can't get the accessor again in the same scope, since this
// would create two references to the same logical memory and allow
// creating aliased mutable references and so forth.
let access2 = txn.access(); // PANIC!
sourcepub fn cursor<'txn, 'db, DB>(&'txn self, db: DB) -> Result<Cursor<'txn, 'db>> where
DB: Into<Supercow<'db, Database<'db>>>,
pub fn cursor<'txn, 'db, DB>(&'txn self, db: DB) -> Result<Cursor<'txn, 'db>> where
DB: Into<Supercow<'db, Database<'db>>>,
Creates a new cursor scoped to this transaction, bound to the given database.
This method is functionally equivalent to the method on CreateCursor
and exists for convenience and backwards-compatibility.
If you have an, e.g., Rc<ReadTransaction>
and want to get a
Cursor<'static,'db>
, make sure you have the CreateCursor
trait
imported so that the needed alternate implementations of this method
are available.
Trait Implementations
sourceimpl<'txn, 'env: 'txn> CreateCursor<'txn> for &'txn WriteTransaction<'env>
impl<'txn, 'env: 'txn> CreateCursor<'txn> for &'txn WriteTransaction<'env>
sourceimpl<'env> Debug for WriteTransaction<'env>
impl<'env> Debug for WriteTransaction<'env>
sourceimpl<'env> Deref for WriteTransaction<'env>
impl<'env> Deref for WriteTransaction<'env>
type Target = ConstTransaction<'env>
type Target = ConstTransaction<'env>
The resulting type after dereferencing.
sourcefn deref(&self) -> &ConstTransaction<'env>
fn deref(&self) -> &ConstTransaction<'env>
Dereferences the value.
sourceimpl<'env> DerefMut for WriteTransaction<'env>
impl<'env> DerefMut for WriteTransaction<'env>
sourcefn deref_mut(&mut self) -> &mut ConstTransaction<'env>
fn deref_mut(&mut self) -> &mut ConstTransaction<'env>
Mutably dereferences the value.
Auto Trait Implementations
impl<'env> !RefUnwindSafe for WriteTransaction<'env>
impl<'env> !Send for WriteTransaction<'env>
impl<'env> !Sync for WriteTransaction<'env>
impl<'env> Unpin for WriteTransaction<'env>
impl<'env> !UnwindSafe for WriteTransaction<'env>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> SafeBorrow<T> for T where
T: ?Sized,
impl<T> SafeBorrow<T> for T where
T: ?Sized,
sourcefn borrow_replacement(ptr: &T) -> &T
fn borrow_replacement(ptr: &T) -> &T
Given ptr
, which was obtained from a prior call to Self::borrow()
,
return a value with the same nominal lifetime which is guaranteed to
survive mutations to Self
. Read more