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§
Sourcefn id(&self) -> TxId
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(())
}
Sourcefn size(&self) -> u64
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(())
}
Sourcefn writable(&self) -> bool
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(())
}
Sourcefn cursor<'a>(&'a self) -> CursorImpl<'tx, 'a>
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(())
}
Sourcefn bucket<'a, T: AsRef<[u8]>>(&'a self, name: T) -> Option<BucketImpl<'tx, 'a>>
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(())
}
fn bucket_path<'a, T: AsRef<[u8]>>( &'a self, names: &[T], ) -> Option<BucketImpl<'tx, 'a>>
Sourcefn 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
fn for_each<'a, F: FnMut(&'a [u8], BucketImpl<'tx, 'a>) -> Result<()>>(
&self,
f: F,
) -> Result<()>where
'tx: 'a,
iter_*
methods insteadExecutes 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(())
}
Sourcefn page(&self, id: PgId) -> Option<PageInfo>
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(())
}
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.