pub struct DbBuilder<K: ?Sized, V: ?Sized, C, N> { /* private fields */ }
Expand description
Options for opening a database
This struct follows the builder pattern. Start with DbBuilder::new
and
pass completed builder by (shared) reference to Env::open_db
or
EnvRw::create_db
to open or create a database.
Caveats / Safety
When opening an already existing database, all options must be compatible
with the previously used options. It has be be ensured by the caller that
this is true. Thus Env::open_db
and EnvRw::create_db
are marked as
unsafe
.
DbBuilder::name
has to be called last (after setting all other
options). This is due to some restrictions regarding const fn
s.
A compiler error will be reported if the methods are invoked in the wrong
order.
Type arguments
The type arguments K
, V
, and C
reflect the type used for keys and
values, and whether duplicate keys are allowed (C
being KeysUnique
or
KeysDuplicate
), respectively.
The fourth type parameter N
determines if a name
(or the unnamed database)
has been set, or if a name must be selected yet. N
is ()
if no database
name has been selected yet. Once a name has been selected, you can use the
type alias DbSpec<K, V, C>
to refer to the completed builder.
Example
// question mark operator may return with `std::io::Error`
use mmtkvdb::{self as kv, Txn as _};
use tempfile::tempdir;
let location = tempdir()?;
let env_builder = kv::EnvBuilder::new().dir(location.path()).max_dbs(1);
// SAFETY:
// * database isn't opened twice in this process at the same time
// * it is assumed no other processes access the environment
// * since environment is newly created, it matches the platform
let mut env_rw = unsafe { env_builder.open_rw() }?;
let db_opts = kv::DbBuilder::new()
.key_type::<str>()
.value_type::<i32>()
.name("account_balance");
// SAFETY: database doesn't exist yet (or would need compatible options)
let db = unsafe { env_rw.create_db(&db_opts) }?;
Implementations§
source§impl<K: ?Sized, V: ?Sized, C> DbBuilder<K, V, C, ()>where
C: Constraint,
impl<K: ?Sized, V: ?Sized, C> DbBuilder<K, V, C, ()>where C: Constraint,
sourcepub const fn keys_unique(self) -> DbBuilder<K, V, KeysUnique, ()>
pub const fn keys_unique(self) -> DbBuilder<K, V, KeysUnique, ()>
Clear dupsort
option (also clears reversedup
option)
See DbBuilder::keys_duplicate
for setting dupsort
option and
DbBuilder::has_duplicate_keys
for getting current state of flag.
sourcepub const fn keys_duplicate(self) -> DbBuilder<K, V, KeysDuplicate, ()>
pub const fn keys_duplicate(self) -> DbBuilder<K, V, KeysDuplicate, ()>
Set dupsort
option
Note: Using this option will put an additional constraint on the size
of each value stored in the database (see Env::max_keysize
).
See DbBuilder::keys_unique
for clearing dupsort
option and
DbBuilder::has_duplicate_keys
for getting current state of flag.
source§impl<K: ?Sized, V: ?Sized, C, N> DbBuilder<K, V, C, N>where
C: Constraint,
impl<K: ?Sized, V: ?Sized, C, N> DbBuilder<K, V, C, N>where C: Constraint,
sourcepub const fn get_reversekey(&self) -> bool
pub const fn get_reversekey(&self) -> bool
Get reversekey
option
sourcepub const fn reversekey(self, flag: bool) -> Self
pub const fn reversekey(self, flag: bool) -> Self
Set or clear reversekey
option
sourcepub fn unnamed(self) -> DbSpec<K, V, C>
pub fn unnamed(self) -> DbSpec<K, V, C>
Use unnamed database
Should be used as last builder method because some other builder methods are not available anymore after calling this method.
sourcepub fn name<T: Into<Vec<u8>>>(self, name: T) -> DbSpec<K, V, C>
pub fn name<T: Into<Vec<u8>>>(self, name: T) -> DbSpec<K, V, C>
Use named database (name must not contain a null byte)
Should be used as last builder method because some other builder methods are not available anymore after calling this method.
sourcepub fn strip_name<T: Into<Vec<u8>>>(self) -> DbBuilder<K, V, C, ()>
pub fn strip_name<T: Into<Vec<u8>>>(self) -> DbBuilder<K, V, C, ()>
Remove name information
NOTE: This does not select the unnamed database. To select the unnamed
database, use DbBuilder::unnamed
.
source§impl<K: ?Sized, V: ?Sized, C: Constraint, N> DbBuilder<K, V, C, N>
impl<K: ?Sized, V: ?Sized, C: Constraint, N> DbBuilder<K, V, C, N>
sourcepub fn has_duplicate_keys(&self) -> bool
pub fn has_duplicate_keys(&self) -> bool
Returns true if duplicate keys are allowed (i.e. is dupsort
option set?)
source§impl<K: ?Sized, V: ?Sized, N> DbBuilder<K, V, KeysDuplicate, N>
impl<K: ?Sized, V: ?Sized, N> DbBuilder<K, V, KeysDuplicate, N>
sourcepub const fn get_reversedup(&self) -> bool
pub const fn get_reversedup(&self) -> bool
Get reversedup
option
sourcepub const fn reversedup(self, flag: bool) -> Self
pub const fn reversedup(self, flag: bool) -> Self
Set or clear reversedup
option