Trait TxRwRefApi

Source
pub trait TxRwRefApi<'tx>: TxApi<'tx> {
    // Required methods
    fn bucket_mut<'a, T: AsRef<[u8]>>(
        &'a mut self,
        name: T,
    ) -> Option<BucketRwImpl<'tx, 'a>>;
    fn bucket_mut_path<'a, T: AsRef<[u8]>>(
        &'a mut self,
        names: &[T],
    ) -> Option<BucketRwImpl<'tx, 'a>>;
    fn create_bucket<'a, T: AsRef<[u8]>>(
        &'a mut self,
        name: T,
    ) -> Result<BucketRwImpl<'tx, 'a>>;
    fn create_bucket_if_not_exists<'a, T: AsRef<[u8]>>(
        &'a mut self,
        name: T,
    ) -> Result<BucketRwImpl<'tx, 'a>>;
    fn create_bucket_path<'a, T: AsRef<[u8]>>(
        &'a mut self,
        names: &[T],
    ) -> Result<BucketRwImpl<'tx, 'a>>;
    fn delete_bucket<T: AsRef<[u8]>>(&mut self, name: T) -> Result<()>;
    fn on_commit<F: FnMut() + 'tx>(&mut self, f: F);
    fn iter_mut_buckets<'a>(&'a mut self) -> BucketIterMut<'tx, 'a> ;
}
Expand description

RW transaction API

Required Methods§

Source

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

Retrieves a mutable 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.update(|mut tx| {
    let mut b = tx.bucket_mut("test").unwrap();
    b.put("key", "new value")?;
    Ok(())
  })?;

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

  Ok(())
}
Source

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

Source

fn create_bucket<'a, T: AsRef<[u8]>>( &'a mut self, name: T, ) -> Result<BucketRwImpl<'tx, 'a>>

Creates a new bucket.

Returns an error if the bucket already exists, if the bucket name is blank, or if the bucket name is too long.

use bbolt_rs::*;

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

  db.update(|mut tx| {
    let mut b = tx.create_bucket("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 create_bucket_if_not_exists<'a, T: AsRef<[u8]>>( &'a mut self, name: T, ) -> Result<BucketRwImpl<'tx, 'a>>

Creates a new bucket if it doesn’t already exist.

Returns an error if the bucket name is blank, or if the bucket name is too long.

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 create_bucket_path<'a, T: AsRef<[u8]>>( &'a mut self, names: &[T], ) -> Result<BucketRwImpl<'tx, 'a>>

Source

fn delete_bucket<T: AsRef<[u8]>>(&mut self, name: T) -> Result<()>

DeleteBucket deletes a bucket. Returns an error if the bucket cannot be found or if the key represents a non-bucket value.

use bbolt_rs::*;

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

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

  db.update(|mut tx| {
    tx.delete_bucket("test")?;
    Ok(())
  })?;

  db.view(|tx| {
    assert_eq!(false, tx.bucket("test").is_some());
    Ok(())
  })?;

  Ok(())
}
Source

fn on_commit<F: FnMut() + 'tx>(&mut self, f: F)

OnCommit adds a handler function to be executed after the transaction successfully commits.

use bbolt_rs::*;
use std::cell::RefCell;

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

  let tx_committed = RefCell::new(false);
  db.update(|mut tx| {
    let mut b = tx.create_bucket_if_not_exists("test")?;
    tx.on_commit(|| *tx_committed.borrow_mut() = true);
    Ok(())
  })?;

  assert_eq!(true, *tx_committed.borrow());
  Ok(())
}
Source

fn iter_mut_buckets<'a>(&'a mut self) -> BucketIterMut<'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> TxRwRefApi<'tx> for TxRwImpl<'tx>

Source§

impl<'tx> TxRwRefApi<'tx> for TxRwRef<'tx>