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
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.
Methods
impl<'env> WriteTransaction<'env>
[src]
pub fn new<E>(env: E) -> Result<Self> where
E: Into<NonSyncSupercow<'env, Environment>>,
[src]
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).
pub fn child_tx<'a>(&'a mut self) -> Result<WriteTransaction<'a>> where
'env: 'a,
[src]
'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();
pub fn commit(self) -> Result<()>
[src]
Commits this write transaction.
pub fn access(&self) -> WriteAccessor
[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>>
pub fn access(&self) -> ConstAccessor
[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!
pub fn cursor<'txn, 'db, DB>(&'txn self, db: DB) -> Result<Cursor<'txn, 'db>> where
DB: Into<Supercow<'db, Database<'db>>>,
[src]
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.
pub fn id(&self) -> usize
[src]
Returns the internal id of this transaction.
pub fn db_stat(&self, db: &Database) -> Result<Stat>
[src]
Retrieves statistics for a database.
pub fn db_flags(&self, db: &Database) -> Result<Flags>
[src]
Retrieve the DB flags for a database handle.
Trait Implementations
impl<'txn, 'env: 'txn> CreateCursor<'txn> for &'txn WriteTransaction<'env>
[src]
fn cursor<'db, DB>(&self, db: DB) -> Result<Cursor<'txn, 'db>> where
DB: Into<Supercow<'db, Database<'db>>>,
[src]
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. Read more
impl<'env> Debug for WriteTransaction<'env>
[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl<'env> Deref for WriteTransaction<'env>
[src]
type Target = ConstTransaction<'env>
The resulting type after dereferencing.
fn deref(&self) -> &ConstTransaction<'env>
[src]
Dereferences the value.
impl<'env> DerefMut for WriteTransaction<'env>
[src]
fn deref_mut(&mut self) -> &mut ConstTransaction<'env>
[src]
Mutably dereferences the value.