Trait TxApi

Source
pub trait TxApi<'tx>: TxCheck<'tx> {
    // Required methods
    fn id(&self) -> TxId;
    fn size(&self) -> u64;
    fn writable(&self) -> bool;
    fn cursor<'a>(&'a self) -> CursorImpl<'tx, 'a>;
    fn stats(&self) -> Arc<TxStats>;
    fn bucket<'a, T: AsRef<[u8]>>(
        &'a self,
        name: T,
    ) -> Option<BucketImpl<'tx, 'a>>;
    fn bucket_path<'a, T: AsRef<[u8]>>(
        &'a self,
        names: &[T],
    ) -> Option<BucketImpl<'tx, 'a>>;
    fn for_each<'a, F: FnMut(&'a [u8], BucketImpl<'tx, 'a>) -> Result<()>>(
        &self,
        f: F,
    ) -> Result<()>
       where 'tx: 'a;
    fn page(&self, id: PgId) -> Option<PageInfo>;
    fn iter_buckets<'a>(&'a self) -> BucketIter<'tx, 'a> ;
}
Expand description

Read-only transaction API

Required Methods§

Source

fn id(&self) -> TxId

Returns the transaction id.

use bbolt_rs::*;

fn main() -> Result<()> {
  let mut db = Bolt::open_mem()?;

  db.view(|tx| {
    assert_eq!(1, tx.id().0);
    Ok(())
  })?;

  db.update(|mut tx| {
    assert_eq!(2, tx.id().0);
    Ok(())
  })?;

  db.view(|tx| {
    assert_eq!(2, tx.id().0);
    Ok(())
  })?;

  Ok(())
}
Source

fn size(&self) -> u64

Returns current database size in bytes as seen by this transaction.

use bbolt_rs::*;

fn main() -> Result<()> {
  let mut db = Bolt::open_mem()?;

  db.update(|mut tx| {
    let mut b = tx.create_bucket_if_not_exists("test")?;
    Ok(())
  })?;

  db.view(|tx| {
    assert_eq!((page_size::get() * 6) as u64, tx.size());
    Ok(())
  })?;
  Ok(())
}
Source

fn writable(&self) -> bool

Returns whether the transaction can perform write operations.

use bbolt_rs::*;

fn main() -> Result<()> {
  let mut db = Bolt::open_mem()?;

  db.update(|mut tx| {
    assert_eq!(true, tx.writable());
    Ok(())
  })?;

  db.view(|tx| {
    assert_eq!(false, tx.writable());
    Ok(())
  })?;

  Ok(())
}
Source

fn cursor<'a>(&'a self) -> CursorImpl<'tx, 'a>

Creates a cursor associated with the root bucket. All items in the cursor will return None value because all root bucket keys point to buckets.

use bbolt_rs::*;

fn main() -> Result<()> {
  let mut db = Bolt::open_mem()?;

  db.update(|mut tx| {
    tx.create_bucket_if_not_exists("test1")?;
    tx.create_bucket_if_not_exists("test2")?;
    tx.create_bucket_if_not_exists("test3")?;
    Ok(())
  })?;

  db.view(|tx| {
    let mut c = tx.cursor();
    assert_eq!(Some((b"test1".as_slice(), None)), c.first());
    assert_eq!(Some((b"test2".as_slice(), None)), c.next());
    assert_eq!(Some((b"test3".as_slice(), None)), c.next());
    Ok(())
  })?;

  Ok(())
}
Source

fn stats(&self) -> Arc<TxStats>

Retrieves a copy of the current transaction statistics.

Source

fn bucket<'a, T: AsRef<[u8]>>(&'a self, name: T) -> Option<BucketImpl<'tx, 'a>>

Retrieves a bucket by name. Returns None if the bucket does not exist.

use bbolt_rs::*;

fn main() -> Result<()> {
  let mut db = Bolt::open_mem()?;

  db.update(|mut tx| {
    let mut b = tx.create_bucket_if_not_exists("test")?;
    b.put("key", "value")?;
    Ok(())
  })?;

  db.view(|tx| {
    let b = tx.bucket("test").unwrap();
    assert_eq!(Some(b"value".as_slice()), b.get("key"));
    Ok(())
  })?;

  Ok(())
}
Source

fn bucket_path<'a, T: AsRef<[u8]>>( &'a self, names: &[T], ) -> Option<BucketImpl<'tx, 'a>>

Source

fn for_each<'a, F: FnMut(&'a [u8], BucketImpl<'tx, 'a>) -> Result<()>>( &self, f: F, ) -> Result<()>
where 'tx: 'a,

👎Deprecated since 1.3.9: please use iter_* methods instead

Executes a function for each key/value pair in a bucket. Because ForEach uses a Cursor, the iteration over keys is in lexicographical order.

If the provided function returns an error then the iteration is stopped and the error is returned to the caller.

use bbolt_rs::*;

fn main() -> Result<()> {
  let mut db = Bolt::open_mem()?;

  db.update(|mut tx| {
    let mut b = tx.create_bucket_if_not_exists("test")?;
    b.put("key1", "value1")?;
    b.put("key2", "value2")?;
    b.put("key3", "value3")?;
    Ok(())
  })?;

  db.view(|tx| {;
    tx.for_each(|bk,b| {
      b.for_each(|k, v| {
        println!("{:?}->{:?}, {:?}", bk, k, v);
        Ok(())
      })?;
     Ok(())
    })?;
    Ok(())
  })?;

  Ok(())
}
Source

fn page(&self, id: PgId) -> Option<PageInfo>

Returns page information for a given page number.

This is only safe for concurrent use when used by a writable transaction.

use bbolt_rs::*;

fn main() -> Result<()> {
  let mut db = Bolt::open_mem()?;

  db.update(|mut tx| {
    let mut b = tx.create_bucket_if_not_exists("test")?;
    b.put("key", "value")?;
    Ok(())
  })?;

  db.view(|tx| {
    let b = tx.bucket("test").unwrap();
    let page_id = b.root();
    let page_info = tx.page(page_id).unwrap();
    println!("{:?}", page_info);
    Ok(())
  })?;

  Ok(())
}
Source

fn iter_buckets<'a>(&'a self) -> BucketIter<'tx, 'a>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'tx> TxApi<'tx> for TxImpl<'tx>

Source§

impl<'tx> TxApi<'tx> for TxRef<'tx>

Source§

impl<'tx> TxApi<'tx> for TxRwImpl<'tx>

Source§

impl<'tx> TxApi<'tx> for TxRwRef<'tx>