pub struct Bucket { /* private fields */ }Expand description
Bucket represents a collection of key/value pairs inside the database.
Implementations§
source§impl Bucket
 
impl Bucket
pub fn tx(&self) -> Result<Tx, Error>
pub fn db(&self) -> Result<DB, Error>
pub fn root(&self) -> u64
sourcepub fn cursor(&self) -> Result<Cursor<'_, &Bucket>, Error>
 
pub fn cursor(&self) -> Result<Cursor<'_, &Bucket>, Error>
Creates a cursor associated with the bucket.
sourcepub fn bucket(&self, key: &[u8]) -> Option<&Bucket>
 
pub fn bucket(&self, key: &[u8]) -> Option<&Bucket>
Retrieves a nested bucket by name. Returns None if the bucket does not exist or found item is not bucket.
sourcepub fn bucket_mut(&mut self, key: &[u8]) -> Option<&mut Bucket>
 
pub fn bucket_mut(&mut self, key: &[u8]) -> Option<&mut Bucket>
Retrieves a nested mutable bucket by name. Returns None if the bucket does not exist or found item is not bucket.
sourcepub fn create_bucket_if_not_exists(
    &mut self,
    key: &[u8]
) -> Result<&mut Bucket, Error>
 
pub fn create_bucket_if_not_exists( &mut self, key: &[u8] ) -> Result<&mut Bucket, Error>
Creates bucket if it not exists
sourcepub fn delete_bucket(&mut self, key: &[u8]) -> Result<(), Error>
 
pub fn delete_bucket(&mut self, key: &[u8]) -> Result<(), Error>
Removes bucket and its contents
Returns error if bucket not found or value is not bucket
sourcepub fn get(&self, key: &[u8]) -> Option<&[u8]>
 
pub fn get(&self, key: &[u8]) -> Option<&[u8]>
Retrieves the value for a key in the bucket. Returns a None value if the key does not exist or if the key is a nested bucket.
§Example
use nut::DBBuilder;
let db = DBBuilder::new("./test.db").build().unwrap();
let tx = db.begin_tx().unwrap();
let flowers = tx.bucket(b"flowers").unwrap();
assert_eq!(flowers.get(b"irises").unwrap(), &b"Iris is a genus of species of flowering plants"[..]);sourcepub fn put(&mut self, key: &[u8], value: Vec<u8>) -> Result<(), Error>
 
pub fn put(&mut self, key: &[u8], value: Vec<u8>) -> Result<(), Error>
Sets the value for a key in the bucket. If the key exist then its previous value will be overwritten. Returns an error if the bucket was created from a read-only transaction, if the key is blank, if the key is too large, or if the value is too large.
§Example
use nut::DBBuilder;
let mut db = DBBuilder::new("./test.db").build().unwrap();
let mut tx = db.begin_rw_tx().unwrap();
let mut flowers = tx.create_bucket(b"flowers").unwrap();
flowers.put(b"irises", b"Iris is a genus of species of flowering plants".to_vec()).unwrap()sourcepub fn delete(&mut self, key: &[u8]) -> Result<(), Error>
 
pub fn delete(&mut self, key: &[u8]) -> Result<(), Error>
Removes a key from the bucket. If the key does not exist then nothing is done. Returns an error if the bucket was created from a read-only transaction.
§Example
use nut::DBBuilder;
let mut db = DBBuilder::new("./test.db").build().unwrap();
let mut tx = db.begin_rw_tx().unwrap();
let mut flowers = tx.bucket_mut(b"flowers").unwrap();
flowers.delete(b"irises").unwrap()sourcepub fn sequence(&self) -> u64
 
pub fn sequence(&self) -> u64
Returns the current integer for the bucket without incrementing it.
sourcepub fn next_sequence(&mut self) -> Result<u64, Error>
 
pub fn next_sequence(&mut self) -> Result<u64, Error>
Returns an autoincrementing integer for the bucket.
sourcepub fn set_sequence(&mut self, value: u64) -> Result<(), Error>
 
pub fn set_sequence(&mut self, value: u64) -> Result<(), Error>
Updates the sequence number for the bucket.
sourcepub fn for_each<'a, E: Into<Error>>(
    &self,
    handler: Box<dyn FnMut(&[u8], Option<&[u8]>) -> Result<(), E> + 'a>
) -> Result<(), Error>
 
pub fn for_each<'a, E: Into<Error>>( &self, handler: Box<dyn FnMut(&[u8], Option<&[u8]>) -> Result<(), E> + 'a> ) -> Result<(), Error>
Executes a function for each key/value pair in a bucket. If the provided function returns an error then the iteration is stopped and the error is returned to the caller.
sourcepub fn stats(&self) -> BucketStats
 
pub fn stats(&self) -> BucketStats
Returns stats on a bucket.