Struct lmdb_zero::WriteTransaction [] [src]

pub struct WriteTransaction<'env>(_);

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.

Methods

impl<'env> WriteTransaction<'env>
[src]

[src]

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

[src]

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

[src]

Commits this write transaction.

[src]

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

[src]

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!

[src]

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.

[src]

Returns the internal id of this transaction.

[src]

Retrieves statistics for a database.

[src]

Retrieve the DB flags for a database handle.

Trait Implementations

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

[src]

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. Read more

impl<'env> Debug for WriteTransaction<'env>
[src]

[src]

Formats the value using the given formatter. Read more

impl<'env> Deref for WriteTransaction<'env>
[src]

The resulting type after dereferencing.

[src]

Dereferences the value.

impl<'env> DerefMut for WriteTransaction<'env>
[src]

[src]

Mutably dereferences the value.

Auto Trait Implementations

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

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