pub struct EnvOpenOptions { /* private fields */ }
Expand description
Options and flags which can be used to configure how an environment is opened.
Implementations§
Source§impl EnvOpenOptions
impl EnvOpenOptions
Sourcepub fn new() -> EnvOpenOptions
pub fn new() -> EnvOpenOptions
Creates a blank new set of options ready for configuration.
Sourcepub fn map_size(&mut self, size: usize) -> &mut Self
pub fn map_size(&mut self, size: usize) -> &mut Self
Set the size of the memory map to use for this environment.
Sourcepub fn max_readers(&mut self, readers: u32) -> &mut Self
pub fn max_readers(&mut self, readers: u32) -> &mut Self
Set the maximum number of threads/reader slots for the environment.
Sourcepub fn max_dbs(&mut self, dbs: u32) -> &mut Self
pub fn max_dbs(&mut self, dbs: u32) -> &mut Self
Set the maximum number of named databases for the environment.
Sourcepub unsafe fn flags(&mut self, flags: EnvFlags) -> &mut Self
pub unsafe fn flags(&mut self, flags: EnvFlags) -> &mut Self
Set one or more LMDB flags.
use std::fs;
use std::path::Path;
use heed::{EnvOpenOptions, Database, EnvFlags};
use heed::types::*;
fs::create_dir_all(Path::new("target").join("database.mdb"))?;
let mut env_builder = EnvOpenOptions::new();
unsafe { env_builder.flags(EnvFlags::NO_TLS | EnvFlags::NO_META_SYNC); }
let dir = tempfile::tempdir().unwrap();
let env = unsafe { env_builder.open(dir.path())? };
// we will open the default unnamed database
let mut wtxn = env.write_txn()?;
let db: Database<Str, U32<byteorder::NativeEndian>> = env.create_database(&mut wtxn, None)?;
// opening a write transaction
db.put(&mut wtxn, "seven", &7)?;
db.put(&mut wtxn, "zero", &0)?;
db.put(&mut wtxn, "five", &5)?;
db.put(&mut wtxn, "three", &3)?;
wtxn.commit()?;
// force the OS to flush the buffers (see Flag::NoSync and Flag::NoMetaSync).
env.force_sync();
// opening a read transaction
// to check if those values are now available
let mut rtxn = env.read_txn()?;
let ret = db.get(&rtxn, "zero")?;
assert_eq!(ret, Some(0));
let ret = db.get(&rtxn, "five")?;
assert_eq!(ret, Some(5));
§Safety
It is unsafe to use unsafe LMDB flags such as NO_SYNC
, NO_META_SYNC
, or NO_LOCK
.
Sourcepub unsafe fn open<P: AsRef<Path>>(&self, path: P) -> Result<Env>
pub unsafe fn open<P: AsRef<Path>>(&self, path: P) -> Result<Env>
Open an environment that will be located at the specified path.
§Safety
LMDB is backed by a memory map 1 which comes with some safety precautions.
Memory map constructors are marked unsafe
because of the potential
for Undefined Behavior (UB) using the map if the underlying file is
subsequently modified, in or out of process.
LMDB itself has a locking system that solves this problem, but it will not save you from making mistakes yourself.
These are some things to take note of:
- Avoid long-lived transactions, they will cause the database to grow quickly 2
- Avoid aborting your process with an active transaction 3
- Do not use LMDB on remote filesystems, even between processes on the same host 4
- You must manage concurrent accesses yourself if using
EnvFlags::NO_LOCK
5 - Anything that causes LMDB’s lock file to be broken will cause synchronization issues and may introduce UB 6
heed
itself upholds some safety invariants, including but not limited to:
- Calling
EnvOpenOptions::open
twice in the same process, at the same time is OK 7
For more details, it is highly recommended to read LMDB’s official documentation. 8
https://github.com/LMDB/lmdb/blob/b8e54b4c31378932b69f1298972de54a565185b1/libraries/liblmdb/lmdb.h#L107-L114 ↩
https://github.com/LMDB/lmdb/blob/b8e54b4c31378932b69f1298972de54a565185b1/libraries/liblmdb/lmdb.h#L118-L121 ↩
https://github.com/LMDB/lmdb/blob/b8e54b4c31378932b69f1298972de54a565185b1/libraries/liblmdb/lmdb.h#L129 ↩
https://github.com/LMDB/lmdb/blob/b8e54b4c31378932b69f1298972de54a565185b1/libraries/liblmdb/lmdb.h#L129 ↩
https://github.com/LMDB/lmdb/blob/b8e54b4c31378932b69f1298972de54a565185b1/libraries/liblmdb/lmdb.h#L49-L52 ↩
https://github.com/LMDB/lmdb/blob/b8e54b4c31378932b69f1298972de54a565185b1/libraries/liblmdb/lmdb.h#L102-L105 ↩
Trait Implementations§
Source§impl Clone for EnvOpenOptions
impl Clone for EnvOpenOptions
Source§fn clone(&self) -> EnvOpenOptions
fn clone(&self) -> EnvOpenOptions
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for EnvOpenOptions
impl Debug for EnvOpenOptions
Source§impl Default for EnvOpenOptions
impl Default for EnvOpenOptions
Source§impl<'de> Deserialize<'de> for EnvOpenOptions
impl<'de> Deserialize<'de> for EnvOpenOptions
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for EnvOpenOptions
impl PartialEq for EnvOpenOptions
Source§impl Serialize for EnvOpenOptions
impl Serialize for EnvOpenOptions
impl StructuralPartialEq for EnvOpenOptions
Auto Trait Implementations§
impl Freeze for EnvOpenOptions
impl RefUnwindSafe for EnvOpenOptions
impl Send for EnvOpenOptions
impl Sync for EnvOpenOptions
impl Unpin for EnvOpenOptions
impl UnwindSafe for EnvOpenOptions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 40 bytes