Struct WriteTransaction

Source
pub struct WriteTransaction<'env>(/* private fields */);
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

WriteTransactions 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§

Source§

impl<'env> WriteTransaction<'env>

Source

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).

Source

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();
Source

pub fn commit(self) -> Result<()>

Commits this write transaction.

Source

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>>§

Source

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!
Source

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.

Source

pub fn id(&self) -> usize

Returns the internal id of this transaction.

Source

pub fn db_stat(&self, db: &Database<'_>) -> Result<Stat>

Retrieves statistics for a database.

Source

pub fn db_flags(&self, db: &Database<'_>) -> Result<Flags>

Retrieve the DB flags for a database handle.

Trait Implementations§

Source§

impl<'txn, 'env: 'txn> CreateCursor<'txn> for &'txn WriteTransaction<'env>

Source§

fn cursor<'db, DB>(&self, db: DB) -> Result<Cursor<'txn, 'db>>
where DB: Into<Supercow<'db, Database<'db>>>,

Create a cursor using self as the reference to the containing transaction and db as the database the cursor will read from and write into.
Source§

impl<'env> Debug for WriteTransaction<'env>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'env> Deref for WriteTransaction<'env>

Source§

type Target = ConstTransaction<'env>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &ConstTransaction<'env>

Dereferences the value.
Source§

impl<'env> DerefMut for WriteTransaction<'env>

Source§

fn deref_mut(&mut self) -> &mut ConstTransaction<'env>

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<'env> !Freeze for WriteTransaction<'env>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> SafeBorrow<T> for T
where T: ?Sized,

Source§

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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.