Module lmdb_rs::core [] [src]

High level wrapper of LMDB APIs

Requires knowledge of LMDB terminology

Environment

Environment is actually the center point of LMDB, it's a container of everything else. As some settings couldn't be adjusted after opening, Environment is constructed using EnvBuilder, which sets up maximum size, maximum count of named databases, maximum readers which could be used from different threads without locking and so on.

Database

Actual key-value store. The most crucial aspect is whether a database allows duplicates or not. It is specified on creation and couldn't be changed later. Entries for the same key are called items.

There are a couple of optmizations to use, like marking keys or data as integer, allowing sorting using reverse key, marking keys/data as fixed size.

Transaction

Absolutely every db operation happens in a transaction. It could be a read-only transaction (reader), which is lockless and therefore cheap. Or it could be a read-write transaction, which is unique, i.e. there could be only one writer at a time.

While readers are cheap and lockless, they work better being short-lived as in other case they may lock pages from being reused. Readers have a special API for marking as finished and renewing.

It is perfectly fine to create nested transactions.

Example

Reexports

pub use MdbError::NotFound;
pub use MdbError::KeyExists;
pub use MdbError::Other;
pub use MdbError::StateError;
pub use MdbError::Corrupted;
pub use MdbError::Panic;
pub use MdbError::InvalidPath;
pub use MdbError::TxnFull;
pub use MdbError::CursorFull;
pub use MdbError::PageFull;
pub use MdbError::CacheError;

Structs

Cursor
CursorFromKeyIter
CursorItemAccessor
CursorItemIter
CursorIter
CursorIterator
CursorKeyRangeIter
CursorToKeyIter
CursorValue
Database

Database

DbFlags

A set of database flags

DbHandle

A handle to a database

EnvBuilder

Constructs environment with settigs which couldn't be changed after opening. By default it tries to create corresponding dir if it doesn't exist, use autocreate_dir() to override that behavior

EnvCreateFlags

A set of all environment flags

EnvFlags

A set of environment flags which could be changed after opening

Environment

Represents LMDB Environment. Should be opened using EnvBuilder

MdbValue
ReadonlyTransaction
Transaction

Enums

MdbError

MdbError wraps information about LMDB error

Constants

DbAllowDups

Duplicate keys may be used in the database. (Or, from another perspective, keys may have multiple data items, stored in sorted order.) By default keys must be unique and may have only a single data item.

DbAllowIntDups

This option specifies that duplicate data items are also integers, and should be sorted as such.

DbCreate

Create the named database if it doesn't exist. This option is not allowed in a read-only transaction or a read-only environment.

DbDupFixed

This flag may only be used in combination with ffi::MDB_DUPSORT. This option tells the library that the data items for this database are all the same size, which allows further optimizations in storage and retrieval. When all data items are the same size, the ffi::MDB_GET_MULTIPLE and ffi::MDB_NEXT_MULTIPLE cursor operations may be used to retrieve multiple items at once.

DbIntKey

Keys are binary integers in native byte order. Setting this option requires all keys to be the same size, typically sizeof(int) or sizeof(size_t).

DbReverseKey

Keys are strings to be compared in reverse order, from the end of the strings to the beginning. By default, Keys are treated as strings and compared from beginning to end.

DbReversedDups

This option specifies that duplicate data items should be compared as strings in reverse order.

EnvCreataMapAsync

When using MDB_WRITEMAP, use asynchronous flushes to disk. As with MDB_NOSYNC, a system crash can then corrupt the database or lose the last transactions. Calling mdb_env_sync() ensures on-disk database integrity until next commit. This flag may be changed at any time using mdb_env_set_flags().

EnvCreateFixedMap

Use a fixed address for the mmap region. This flag must be specified when creating the environment, and is stored persistently in the environment. If successful, the memory map will always reside at the same virtual address and pointers used to reference data items in the database will be constant across multiple invocations. This option may not always work, depending on how the operating system has allocated memory to shared libraries and other uses. The feature is highly experimental.

EnvCreateNoLock

Don't do any locking. If concurrent access is anticipated, the caller must manage all concurrency itself. For proper operation the caller must enforce single-writer semantics, and must ensure that no readers are using old transactions while a writer is active. The simplest approach is to use an exclusive lock so that no readers may be active at all when a writer begins.

EnvCreateNoMemInit

Don't initialize malloc'd memory before writing to unused spaces in the data file. By default, memory for pages written to the data file is obtained using malloc. While these pages may be reused in subsequent transactions, freshly malloc'd pages will be initialized to zeroes before use. This avoids persisting leftover data from other code (that used the heap and subsequently freed the memory) into the data file. Note that many other system libraries may allocate and free memory from the heap for arbitrary uses. E.g., stdio may use the heap for file I/O buffers. This initialization step has a modest performance cost so some applications may want to disable it using this flag. This option can be a problem for applications which handle sensitive data like passwords, and it makes memory checkers like Valgrind noisy. This flag is not needed with MDB_WRITEMAP, which writes directly to the mmap instead of using malloc for pages. The initialization is also skipped if MDB_RESERVE is used; the caller is expected to overwrite all of the memory that was reserved in that case. This flag may be changed at any time using mdb_env_set_flags().

EnvCreateNoMetaSync

Flush system buffers to disk only once per transaction, omit the metadata flush. Defer that until the system flushes files to disk, or next non-MDB_RDONLY commit or mdb_env_sync(). This optimization maintains database integrity, but a system crash may undo the last committed transaction. I.e. it preserves the ACI (atomicity, consistency, isolation) but not D (durability) database property. This flag may be changed at any time using mdb_env_set_flags().

EnvCreateNoReadAhead

Turn off readahead. Most operating systems perform readahead on read requests by default. This option turns it off if the OS supports it. Turning it off may help random read performance when the DB is larger than RAM and system RAM is full. The option is not implemented on Windows.

EnvCreateNoSubDir

By default, LMDB creates its environment in a directory whose pathname is given in path, and creates its data and lock files under that directory. With this option, path is used as-is for the database main data file. The database lock file is the path with "-lock" appended.

EnvCreateNoSync

Don't flush system buffers to disk when committing a transaction. This optimization means a system crash can corrupt the database or lose the last transactions if buffers are not yet flushed to disk. The risk is governed by how often the system flushes dirty buffers to disk and how often mdb_env_sync() is called. However, if the filesystem preserves write order and the MDB_WRITEMAP flag is not used, transactions exhibit ACI (atomicity, consistency, isolation) properties and only lose D (durability). I.e. database integrity is maintained, but a system crash may undo the final transactions. Note that (MDB_NOSYNC | MDB_WRITEMAP) leaves the system with no hint for when to write transactions to disk, unless mdb_env_sync() is called. (MDB_MAPASYNC | MDB_WRITEMAP) may be preferable. This flag may be changed at any time using mdb_env_set_flags().

EnvCreateNoTls

Don't use Thread-Local Storage. Tie reader locktable slots to ffi::MDB_txn objects instead of to threads. I.e. mdb_txn_reset() keeps the slot reseved for the ffi::MDB_txn object. A thread may use parallel read-only transactions. A read-only transaction may span threads if the user synchronizes its use. Applications that multiplex many user threads over individual OS threads need this option. Such an application must also serialize the write transactions in an OS thread, since LMDB's write locking is unaware of the user threads.

EnvCreateReadOnly

Open the environment in read-only mode. No write operations will be allowed. LMDB will still modify the lock file - except on read-only filesystems, where LMDB does not use locks.

EnvCreateWriteMap

Use a writeable memory map unless MDB_RDONLY is set. This is faster and uses fewer mallocs, but loses protection from application bugs like wild pointer writes and other bad updates into the database. Incompatible with nested transactions. Processes with and without MDB_WRITEMAP on the same environment do not cooperate well.

EnvMapAsync

When using MDB_WRITEMAP, use asynchronous flushes to disk. As with MDB_NOSYNC, a system crash can then corrupt the database or lose the last transactions. Calling mdb_env_sync() ensures on-disk database integrity until next commit. This flag may be changed at any time using mdb_env_set_flags().

EnvNoMemInit

Don't initialize malloc'd memory before writing to unused spaces in the data file. By default, memory for pages written to the data file is obtained using malloc. While these pages may be reused in subsequent transactions, freshly malloc'd pages will be initialized to zeroes before use. This avoids persisting leftover data from other code (that used the heap and subsequently freed the memory) into the data file. Note that many other system libraries may allocate and free memory from the heap for arbitrary uses. E.g., stdio may use the heap for file I/O buffers. This initialization step has a modest performance cost so some applications may want to disable it using this flag. This option can be a problem for applications which handle sensitive data like passwords, and it makes memory checkers like Valgrind noisy. This flag is not needed with MDB_WRITEMAP, which writes directly to the mmap instead of using malloc for pages. The initialization is also skipped if MDB_RESERVE is used; the caller is expected to overwrite all of the memory that was reserved in that case. This flag may be changed at any time using mdb_env_set_flags().

EnvNoMetaSync

Flush system buffers to disk only once per transaction, omit the metadata flush. Defer that until the system flushes files to disk, or next non-MDB_RDONLY commit or mdb_env_sync(). This optimization maintains database integrity, but a system crash may undo the last committed transaction. I.e. it preserves the ACI (atomicity, consistency, isolation) but not D (durability) database property. This flag may be changed at any time using mdb_env_set_flags().

EnvNoSync

Don't flush system buffers to disk when committing a transaction. This optimization means a system crash can corrupt the database or lose the last transactions if buffers are not yet flushed to disk. The risk is governed by how often the system flushes dirty buffers to disk and how often mdb_env_sync() is called. However, if the filesystem preserves write order and the MDB_WRITEMAP flag is not used, transactions exhibit ACI (atomicity, consistency, isolation) properties and only lose D (durability). I.e. database integrity is maintained, but a system crash may undo the final transactions. Note that (MDB_NOSYNC | MDB_WRITEMAP) leaves the system with no hint for when to write transactions to disk, unless mdb_env_sync() is called. (MDB_MAPASYNC | MDB_WRITEMAP) may be preferable. This flag may be changed at any time using mdb_env_set_flags().

Traits

IterateCursor

Allows the cration of custom cursor iteration behaviours.

Type Definitions

MdbResult