Struct Database

Source
pub struct Database<'a> { /* private fields */ }
Expand description

Database

Implementations§

Source§

impl<'a> Database<'a>

Source

pub fn stat(&'a self) -> MdbResult<MDB_stat>

Retrieves current db’s statistics.

Source

pub fn get<V: FromMdbValue + 'a>(&'a self, key: &dyn ToMdbValue) -> MdbResult<V>

Retrieves a value by key. In case of DbAllowDups it will be the first value

Examples found in repository?
examples/simple.rs (line 32)
5fn main() {
6    let env = EnvBuilder::new().open("test-lmdb", 0o777).unwrap();
7
8    let db_handle = env.get_default_db(DbFlags::empty()).unwrap();
9    let txn = env.new_transaction().unwrap();
10    {
11        let db = txn.bind(&db_handle); // get a database bound to this transaction
12
13        let pairs = vec![("Albert", "Einstein",),
14                         ("Joe", "Smith",),
15                         ("Jack", "Daniels")];
16
17        for &(name, surname) in pairs.iter() {
18            db.set(&surname, &name).unwrap();
19        }
20    }
21
22    // Note: `commit` is choosen to be explicit as
23    // in case of failure it is responsibility of
24    // the client to handle the error
25    match txn.commit() {
26        Err(_) => panic!("failed to commit!"),
27        Ok(_) => ()
28    }
29
30    let reader = env.get_reader().unwrap();
31    let db = reader.bind(&db_handle);
32    let name = db.get::<&str>(&"Smith").unwrap();
33    println!("It's {} Smith", name);
34}
Source

pub fn set(&self, key: &dyn ToMdbValue, value: &dyn ToMdbValue) -> MdbResult<()>

Sets value for key. In case of DbAllowDups it will add a new item

Examples found in repository?
examples/simple.rs (line 18)
5fn main() {
6    let env = EnvBuilder::new().open("test-lmdb", 0o777).unwrap();
7
8    let db_handle = env.get_default_db(DbFlags::empty()).unwrap();
9    let txn = env.new_transaction().unwrap();
10    {
11        let db = txn.bind(&db_handle); // get a database bound to this transaction
12
13        let pairs = vec![("Albert", "Einstein",),
14                         ("Joe", "Smith",),
15                         ("Jack", "Daniels")];
16
17        for &(name, surname) in pairs.iter() {
18            db.set(&surname, &name).unwrap();
19        }
20    }
21
22    // Note: `commit` is choosen to be explicit as
23    // in case of failure it is responsibility of
24    // the client to handle the error
25    match txn.commit() {
26        Err(_) => panic!("failed to commit!"),
27        Ok(_) => ()
28    }
29
30    let reader = env.get_reader().unwrap();
31    let db = reader.bind(&db_handle);
32    let name = db.get::<&str>(&"Smith").unwrap();
33    println!("It's {} Smith", name);
34}
Source

pub fn append<K: ToMdbValue, V: ToMdbValue>( &self, key: &K, value: &V, ) -> MdbResult<()>

Appends new key-value pair to database, starting a new page instead of splitting an existing one if necessary. Requires that key be >= all existing keys in the database (or will return KeyExists error).

Source

pub fn append_duplicate<K: ToMdbValue, V: ToMdbValue>( &self, key: &K, value: &V, ) -> MdbResult<()>

Appends new value for the given key (requires DbAllowDups), starting a new page instead of splitting an existing one if necessary. Requires that value be >= all existing values for the given key (or will return KeyExists error).

Source

pub fn insert( &self, key: &dyn ToMdbValue, value: &dyn ToMdbValue, ) -> MdbResult<()>

Set value for key. Fails if key already exists, even when duplicates are allowed.

Source

pub fn del(&self, key: &dyn ToMdbValue) -> MdbResult<()>

Deletes value for key.

Source

pub fn del_item( &self, key: &dyn ToMdbValue, data: &dyn ToMdbValue, ) -> MdbResult<()>

Should be used only with DbAllowDups. Deletes corresponding (key, value)

Source

pub fn new_cursor(&'a self) -> MdbResult<Cursor<'a>>

Returns a new cursor

Source

pub fn del_db(self) -> MdbResult<()>

Deletes current db, also moves it out

Source

pub fn clear(&self) -> MdbResult<()>

Removes all key/values from db

Source

pub fn iter(&'a self) -> MdbResult<CursorIterator<'a, CursorIter>>

Returns an iterator for all values in database

Source

pub fn keyrange_from<'c, K: ToMdbValue>( &'c self, start_key: &'c K, ) -> MdbResult<CursorIterator<'c, CursorFromKeyIter<'_>>>

Returns an iterator through keys starting with start_key (>=), start_key is included

Source

pub fn keyrange_to<'c, K: ToMdbValue + 'c>( &'c self, end_key: &'c K, ) -> MdbResult<CursorIterator<'c, CursorToKeyIter<'_>>>

Returns an iterator through keys less than end_key, end_key is not included

Source

pub fn keyrange_from_to<'c, K: ToMdbValue + 'c>( &'c self, start_key: &'c K, end_key: &'c K, ) -> MdbResult<CursorIterator<'c, CursorKeyRangeIter<'_>>>

Returns an iterator through keys start_key <= x < end_key. This is, start_key is included in the iteration, while end_key is kept excluded.

Source

pub fn keyrange<'c, K: ToMdbValue + 'c>( &'c self, start_key: &'c K, end_key: &'c K, ) -> MdbResult<CursorIterator<'c, CursorKeyRangeIter<'_>>>

Returns an iterator for values between start_key and end_key (included). Currently it works only for unique keys (i.e. it will skip multiple items when DB created with ffi::MDB_DUPSORT). Iterator is valid while cursor is valid

Source

pub fn item_iter<'c, 'db: 'c, K: ToMdbValue>( &'db self, key: &'c K, ) -> MdbResult<CursorIterator<'c, CursorItemIter<'c>>>

Returns an iterator for all items (i.e. values with same key)

Source

pub fn set_compare( &self, cmp_fn: extern "C" fn(*const MDB_val, *const MDB_val) -> c_int, ) -> MdbResult<()>

Sets the key compare function for this database.

Warning: This function must be called before any data access functions are used, otherwise data corruption may occur. The same comparison function must be used by every program accessing the database, every time the database is used.

If not called, keys are compared lexically, with shorter keys collating before longer keys.

Setting lasts for the lifetime of the underlying db handle.

Source

pub fn set_dupsort( &self, cmp_fn: extern "C" fn(*const MDB_val, *const MDB_val) -> c_int, ) -> MdbResult<()>

Sets the value comparison function for values of the same key in this database.

Warning: This function must be called before any data access functions are used, otherwise data corruption may occur. The same dupsort function must be used by every program accessing the database, every time the database is used.

If not called, values are compared lexically, with shorter values collating before longer values.

Only used when DbAllowDups is true. Setting lasts for the lifetime of the underlying db handle.

Trait Implementations§

Source§

impl<'a> Debug for Database<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Database<'a>

§

impl<'a> RefUnwindSafe for Database<'a>

§

impl<'a> !Send for Database<'a>

§

impl<'a> !Sync for Database<'a>

§

impl<'a> Unpin for Database<'a>

§

impl<'a> UnwindSafe for Database<'a>

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.