Struct Txn

Source
pub struct Txn<'g, P: ImmutablePage<'g>, G: Guard<'g, P>> { /* private fields */ }
Expand description

Represents a database transaction.

A transaction provides a consistent view of the database. It can be either read-only (RTxn) or read-write (RWTxn). All operations on the database are performed within a transaction.

Implementations§

Source§

impl<'g, P: ImmutablePage<'g>, G: Guard<'g, P>> Txn<'g, P, G>

Source

pub fn get(&'g self, key: &[u8]) -> Result<Option<&'g [u8]>>

Retrieves the value associated with the given key.

§Parameters
  • key: The key to search for.
§Returns
  • Ok(Some(value)) if the key is found.
  • Ok(None) if the key is not found.
  • Err(TxnError) if an error occurs during the tree traversal.
§Safety

The returned slice &[u8] is valid as long as the transaction Txn is alive, as it points directly into the memory-mapped region.

Source

pub fn in_order_iter(&'g self) -> impl Iterator<Item = (&'g [u8], &'g [u8])>

Returns an iterator over all key-value pairs in the database, in key order.

The iterator yields tuples of (&[u8], &[u8]) representing key-value pairs.

Source

pub fn in_order_range_iter<R: RangeBounds<[u8]>>( &'g self, range: &R, ) -> impl Iterator<Item = (&'g [u8], &'g [u8])>

Returns an iterator over key-value pairs within the specified range, in key order.

§Parameters
  • range: A range bound (e.g., start_key..end_key, ..end_key, start_key..) that defines the keys to include in the iteration.

The iterator yields tuples of (&[u8], &[u8]) representing key-value pairs.

Source§

impl<'t, 'd> Txn<'t, WriterPage<'t, 'd>, Writer<'d>>

Source

pub fn insert(&mut self, key: &[u8], val: &[u8]) -> Result<()>

Inserts a new key-value pair into the database.

§Parameters
  • key: The key to insert.
  • val: The value to associate with the key.
§Errors

Returns TxnError if the key already exists or if an error occurs during the insertion process.

Source

pub fn update(&mut self, key: &[u8], val: &[u8]) -> Result<()>

Updates the value associated with an existing key.

§Parameters
  • key: The key whose value is to be updated.
  • val: The new value to associate with the key.
§Errors

Returns TxnError if the key does not exist or if an error occurs during the update process.

Source

pub fn delete(&mut self, key: &[u8]) -> Result<()>

Deletes a key-value pair from the database.

§Parameters
  • key: The key to delete.
§Errors

Returns TxnError if the key does not exist or if an error occurs during the deletion process.

Source

pub fn commit(self)

Commits the transaction, making all changes permanent and visible to subsequent transactions.

If commit is not called, the transaction will be automatically aborted when it goes out of scope.

Source

pub fn abort(self)

Aborts the transaction, discarding all changes made within it.

This is automatically called if the transaction goes out of scope without Txn::commit being called.

Auto Trait Implementations§

§

impl<'g, P, G> Freeze for Txn<'g, P, G>
where G: Freeze,

§

impl<'g, P, G> RefUnwindSafe for Txn<'g, P, G>

§

impl<'g, P, G> Send for Txn<'g, P, G>
where G: Send, P: Sync,

§

impl<'g, P, G> Sync for Txn<'g, P, G>
where G: Sync, P: Sync,

§

impl<'g, P, G> Unpin for Txn<'g, P, G>
where G: Unpin,

§

impl<'g, P, G> UnwindSafe for Txn<'g, P, G>

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