lmdb

Struct Environment

Source
pub struct Environment { /* private fields */ }
Expand description

An LMDB environment.

An environment supports multiple databases, all residing in the same shared-memory map.

Implementations§

Source§

impl Environment

Source

pub fn new() -> EnvironmentBuilder

Creates a new builder for specifying options for opening an LMDB environment.

Source

pub fn env(&self) -> *mut MDB_env

Returns a raw pointer to the underlying LMDB environment.

The caller must ensure that the pointer is not dereferenced after the lifetime of the environment.

Source

pub fn open_db<'env>(&'env self, name: Option<&str>) -> Result<Database>

Opens a handle to an LMDB database.

If name is None, then the returned handle will be for the default database.

If name is not None, then the returned handle will be for a named database. In this case the environment must be configured to allow named databases through EnvironmentBuilder::set_max_dbs.

The returned database handle may be shared among any transaction in the environment.

This function will fail with Error::BadRslot if called by a thread which has an ongoing transaction.

The database name may not contain the null character.

Source

pub fn create_db<'env>( &'env self, name: Option<&str>, flags: DatabaseFlags, ) -> Result<Database>

Opens a handle to an LMDB database, creating the database if necessary.

If the database is already created, the given option flags will be added to it.

If name is None, then the returned handle will be for the default database.

If name is not None, then the returned handle will be for a named database. In this case the environment must be configured to allow named databases through EnvironmentBuilder::set_max_dbs.

The returned database handle may be shared among any transaction in the environment.

This function will fail with Error::BadRslot if called by a thread with an open transaction.

Source

pub fn get_db_flags(&self, db: Database) -> Result<DatabaseFlags>

Retrieves the set of flags which the database is opened with.

The database must belong to to this environment.

Source

pub fn begin_ro_txn<'env>(&'env self) -> Result<RoTransaction<'env>>

Create a read-only transaction for use with the environment.

Source

pub fn begin_rw_txn<'env>(&'env self) -> Result<RwTransaction<'env>>

Create a read-write transaction for use with the environment. This method will block while there are any other read-write transactions open on the environment.

Source

pub fn sync(&self, force: bool) -> Result<()>

Flush data buffers to disk.

Data is always written to disk when Transaction::commit is called, but the operating system may keep it buffered. LMDB always flushes the OS buffers upon commit as well, unless the environment was opened with MDB_NOSYNC or in part MDB_NOMETASYNC.

Source

pub unsafe fn close_db(&mut self, db: Database)

Closes the database handle. Normally unnecessary.

Closing a database handle is not necessary, but lets Transaction::open_database reuse the handle value. Usually it’s better to set a bigger EnvironmentBuilder::set_max_dbs, unless that value would be large.

§Safety

This call is not mutex protected. Databases should only be closed by a single thread, and only if no other threads are going to reference the database handle or one of its cursors any further. Do not close a handle if an existing transaction has modified its database. Doing so can cause misbehavior from database corruption to errors like Error::BadValSize (since the DB name is gone).

Source

pub fn stat(&self) -> Result<Stat>

Retrieves statistics about this environment.

Source

pub fn info(&self) -> Result<Info>

Retrieves info about this environment.

Source

pub fn freelist(&self) -> Result<size_t>

Retrieves the total number of pages on the freelist.

Along with Environment::info(), this can be used to calculate the exact number of used pages as well as free pages in this environment.

let env = Environment::new().open("/tmp/test").unwrap();
let info = env.info().unwrap();
let stat = env.stat().unwrap();
let freelist = env.freelist().unwrap();
let last_pgno = info.last_pgno() + 1; // pgno is 0 based.
let total_pgs = info.map_size() / stat.page_size() as usize;
let pgs_in_use = last_pgno - freelist;
let pgs_free = total_pgs - pgs_in_use;

Note:

  • LMDB stores all the freelists in the designated database 0 in each environment, and the freelist count is stored at the beginning of the value as libc::size_t in the native byte order.

  • It will create a read transaction to traverse the freelist database.

Source

pub fn set_map_size(&self, size: size_t) -> Result<()>

Sets the size of the memory map to use for the environment.

This could be used to resize the map when the environment is already open.

Note:

  • No active transactions allowed when performing resizing in this process.

  • The size should be a multiple of the OS page size. Any attempt to set a size smaller than the space already consumed by the environment will be silently changed to the current size of the used space.

  • In the multi-process case, once a process resizes the map, other processes need to either re-open the environment, or call set_map_size with size 0 to update the environment. Otherwise, new transaction creation will fail with Error::MapResized.

Trait Implementations§

Source§

impl Debug for Environment

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for Environment

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Environment

Source§

impl Sync for Environment

Auto Trait Implementations§

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.