14 #error "<lmdb++.h> requires a C++ compiler"
17 #if __cplusplus < 201103L
18 #if !defined(_MSC_VER) || _MSC_VER < 1900
19 #error "<lmdb++.h> requires a C++11 compiler (CXXFLAGS='-std=c++11')"
35 #include <type_traits>
49 class key_exist_error;
50 class not_found_error;
51 class corrupted_error;
53 class version_mismatch_error;
71 [[noreturn]]
static inline void raise(
const char*
origin,
int rc);
77 const int rc) noexcept
99 #if !defined(__APPLE__)
100 static thread_local
char buffer[1024];
104 #if defined(_MSC_VER)
146 using runtime_error::runtime_error;
156 using runtime_error::runtime_error;
196 using runtime_error::runtime_error;
207 using runtime_error::runtime_error;
242 #if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 14)
262 #if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 11)
289 const char*
const path,
290 const unsigned int flags,
306 const char*
const path,
307 const unsigned int flags = 0) {
310 const char*
const path) {
327 const unsigned int flags = 0) {
370 const bool force =
true) {
391 const unsigned int flags,
392 const bool onoff =
true) {
405 unsigned int*
const flags) {
444 const std::size_t
size) {
457 const unsigned int count) {
470 unsigned int*
const count) {
493 static inline unsigned int
499 return static_cast<unsigned int>(rc);
502 #if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 11)
518 #if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 11)
537 static inline std::size_t txn_id(
MDB_txn* txn) noexcept;
552 const unsigned int flags,
572 static inline std::size_t
573 lmdb::txn_id(
MDB_txn*
const txn) noexcept {
645 const char*
const name,
646 const unsigned int flags,
675 unsigned int*
const flags) {
697 const bool del =
false) {
787 const unsigned int flags = 0) {
902 const unsigned int flags = 0) {
915 const unsigned int flags = 0) {
928 std::size_t&
count) {
974 const std::size_t
size) noexcept
1023 template<
typename T>
1031 template<
typename T>
1053 template<
typename T>
1055 const std::size_t
size) noexcept {
1057 _val.
mv_data =
const_cast<void*
>(
reinterpret_cast<const void*
>(
data));
1076 #if !(defined(__COVERITY__) || defined(_MSC_VER))
1078 static_assert(
sizeof(
lmdb::val) ==
sizeof(
MDB_val),
"sizeof(lmdb::val) != sizeof(MDB_val)");
1145 if (
this != &other) {
1155 try {
close(); }
catch (...) {}
1178 void sync(
const bool force =
true) {
1216 const bool onoff =
true) {
1278 MDB_txn*
const parent =
nullptr,
1307 if (
this != &other) {
1318 try {
abort(); }
catch (...) {}
1413 const char*
const name =
nullptr,
1439 if (
this != &other) {
1508 const bool del =
false) {
1546 template<
typename K>
1548 const K&
key)
const {
1562 template<
typename K,
typename V>
1583 template<
typename V>
1585 const char*
const key,
1620 template<
typename K>
1638 template<
typename K,
typename V>
1657 template<
typename V>
1659 const char*
const key,
1677 const char*
const key,
1678 const char*
const val,
1704 template<
typename K>
1769 if (
this != &other) {
1779 try {
close(); }
catch (...) {}
1842 return get(
key,
nullptr, op);
1854 return get(
key,
nullptr, op);
1897 const bool found =
get(k, v, op);
1899 key.assign(k.data(), k.size());
1912 template<
typename K>
1916 return get(k,
nullptr, op);
Exception class for `MDB_BAD_DBI` errors.
Exception class for `MDB_CORRUPTED` errors.
Resource class for `MDB_cursor*` handles.
bool get(lmdb::val &key, const MDB_cursor_op op)
Retrieves a key from the database.
MDB_cursor * handle() const noexcept
Returns the underlying `MDB_cursor*` handle.
MDB_txn * txn() const noexcept
Returns the cursor's transaction handle.
bool get(lmdb::val &key, lmdb::val &val, const MDB_cursor_op op)
Retrieves a key/value pair from the database.
void close() noexcept
Closes this cursor.
~cursor() noexcept
Destructor.
bool get(MDB_val *const key, const MDB_cursor_op op)
Retrieves a key from the database.
bool find(const K &key, const MDB_cursor_op op=MDB_SET)
Positions this cursor at the given key.
bool get(std::string &key, std::string &val, const MDB_cursor_op op)
Retrieves a key/value pair from the database.
cursor(cursor &&other) noexcept
Move constructor.
MDB_dbi dbi() const noexcept
Returns the cursor's database handle.
static cursor open(MDB_txn *const txn, const MDB_dbi dbi)
Creates an LMDB cursor.
cursor(MDB_cursor *const handle) noexcept
Constructor.
bool get(MDB_val *const key, MDB_val *const val, const MDB_cursor_op op)
Retrieves a key/value pair from the database.
cursor & operator=(cursor &&other) noexcept
Move assignment operator.
static constexpr unsigned int default_flags
void renew(MDB_txn *const txn)
Renews this cursor.
Resource class for `MDB_dbi` handles.
std::size_t size(MDB_txn *const txn) const
Returns the number of records in this database.
bool put(MDB_txn *const txn, const K &key, const V &val, const unsigned int flags=default_put_flags)
Stores a key/value pair into this database.
bool put(MDB_txn *const txn, const val &key, val &data, const unsigned int flags=default_put_flags)
Stores a key/value pair into this database.
dbi(const MDB_dbi handle) noexcept
Constructor.
bool del(MDB_txn *const txn, const val &key)
Removes a key/value pair from this database.
dbi & operator=(dbi &&other) noexcept
Move assignment operator.
~dbi() noexcept
Destructor.
bool get(MDB_txn *const txn, const val &key, val &data)
Retrieves a key/value pair from this database.
MDB_stat stat(MDB_txn *const txn) const
Returns statistics for this database.
bool put(MDB_txn *const txn, const char *const key, const V &val, const unsigned int flags=default_put_flags)
Stores a key/value pair into this database.
unsigned int flags(MDB_txn *const txn) const
Retrieves the flags for this database handle.
bool del(MDB_txn *const txn, const K &key)
Removes a key/value pair from this database.
static dbi open(MDB_txn *const txn, const char *const name=nullptr, const unsigned int flags=default_flags)
Opens a database handle.
dbi & set_compare(MDB_txn *const txn, MDB_cmp_func *const cmp=nullptr)
Sets a custom key comparison function for this database.
static constexpr unsigned int default_flags
static constexpr unsigned int default_put_flags
bool get(MDB_txn *const txn, const K &key, V &val) const
Retrieves a key/value pair from this database.
void drop(MDB_txn *const txn, const bool del=false)
bool put(MDB_txn *const txn, const char *const key, const char *const val, const unsigned int flags=default_put_flags)
Stores a key/value pair into this database.
bool get(MDB_txn *const txn, const char *const key, V &val) const
Retrieves a key/value pair from this database.
dbi(dbi &&other) noexcept
Move constructor.
bool get(MDB_txn *const txn, const K &key) const
Retrieves a key from this database.
MDB_dbi handle() const noexcept
Returns the underlying `MDB_dbi` handle.
bool put(MDB_txn *const txn, const K &key, const unsigned int flags=default_put_flags)
Stores a key into this database.
Resource class for `MDB_env*` handles.
void close() noexcept
Closes this environment, releasing the memory map.
env & set_flags(const unsigned int flags, const bool onoff=true)
~env() noexcept
Destructor.
MDB_env * handle() const noexcept
Returns the underlying `MDB_env*` handle.
env(MDB_env *const handle) noexcept
Constructor.
static constexpr mode default_mode
env(env &&other) noexcept
Move constructor.
env & operator=(env &&other) noexcept
Move assignment operator.
env & open(const char *const path, const unsigned int flags=default_flags, const mode mode=default_mode)
Opens this environment.
void sync(const bool force=true)
Flushes data buffers to disk.
static env create(const unsigned int flags=default_flags)
Creates a new LMDB environment.
static constexpr unsigned int default_flags
env & set_max_readers(const unsigned int count)
env & set_max_dbs(const MDB_dbi count)
env & set_mapsize(const std::size_t size)
Base class for LMDB exception conditions.
error(const char *const origin, const int rc) noexcept
Constructor.
virtual const char * what() const noexcept
Returns the underlying LMDB error code.
int code() const noexcept
Returns the underlying LMDB error code.
static void raise(const char *origin, int rc)
Throws an error based on the given LMDB return code.
const char * origin() const noexcept
Returns the origin of the LMDB error.
Base class for fatal error conditions.
Exception class for `MDB_KEYEXIST` errors.
Base class for logic error conditions.
Exception class for `MDB_MAP_FULL` errors.
Exception class for `MDB_NOTFOUND` errors.
Exception class for `MDB_PANIC` errors.
Base class for runtime error conditions.
Resource class for `MDB_txn*` handles.
void commit()
Commits this transaction.
MDB_env * env() const noexcept
Returns the transaction's `MDB_env*` handle.
txn & operator=(txn &&other) noexcept
Move assignment operator.
void abort() noexcept
Aborts this transaction.
void reset() noexcept
Resets this read-only transaction.
~txn() noexcept
Destructor.
void renew()
Renews this read-only transaction.
static constexpr unsigned int default_flags
txn(txn &&other) noexcept
Move constructor.
txn(MDB_txn *const handle) noexcept
Constructor.
static txn begin(MDB_env *const env, MDB_txn *const parent=nullptr, const unsigned int flags=default_flags)
Creates a new LMDB transaction.
MDB_txn * handle() const noexcept
Returns the underlying `MDB_txn*` handle.
Wrapper class for `MDB_val` structures.
val(const char *const data) noexcept
Constructor.
val & assign(const T *const data, const std::size_t size) noexcept
Assigns the value.
val(const void *const data, const std::size_t size) noexcept
Constructor.
const T * data() const noexcept
Returns a pointer to the data.
bool empty() const noexcept
Determines whether this value is empty.
const char * data() const noexcept
Returns a pointer to the data.
val(val &&other) noexcept=default
Move constructor.
T * data() noexcept
Returns a pointer to the data.
val() noexcept=default
Default constructor.
char * data() noexcept
Returns a pointer to the data.
val & assign(const char *const data) noexcept
Assigns the value.
~val() noexcept=default
Destructor.
std::size_t size() const noexcept
Returns the size of the data.
val & operator=(val &&other) noexcept=default
Move assignment operator.
val & assign(const std::string &data) noexcept
Assigns the value.
Exception class for `MDB_VERSION_MISMATCH` errors.
static void fatal_error(const char *msg)
void swap(NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair1, NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair2)
#define MDB_VERINT(a, b, c)
Combine args a,b,c into a single integer for easy version comparisons.
#define MDB_VERSION_FULL
The full library version as a single integer.
#define MDB_KEYEXIST
key/data pair already exists
#define MDB_MAP_FULL
Environment mapsize reached.
#define MDB_VERSION_MISMATCH
Environment version mismatch.
#define MDB_BAD_DBI
The specified DBI was changed unexpectedly.
#define MDB_NOTFOUND
key/data pair not found (EOF)
#define MDB_SUCCESS
Successful result.
#define MDB_PANIC
Update of meta page failed or environment had fatal error.
#define MDB_CORRUPTED
Located page was wrong type.
void mdb_txn_reset(MDB_txn *txn)
Reset a read-only transaction.
int mdb_env_info(MDB_env *env, MDB_envinfo *stat)
Return information about the LMDB environment.
int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data, unsigned int flags)
Store by cursor.
int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags)
Delete current key/data pair.
int mdb_env_get_flags(MDB_env *env, unsigned int *flags)
Get environment flags.
MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor)
Return the cursor's database handle.
int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
Open an environment handle.
int mdb_env_copy2(MDB_env *env, const char *path, unsigned int flags)
Copy an LMDB environment to the specified path, with options.
int mdb_cursor_count(MDB_cursor *cursor, size_t *countp)
Return count of duplicates for current key.
void mdb_env_close(MDB_env *env)
Close the environment and release the memory map.
int mdb_env_copyfd(MDB_env *env, mdb_filehandle_t fd)
Copy an LMDB environment to the specified file descriptor.
int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
Retrieve by cursor.
int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data, unsigned int flags)
Store items into a database.
int mdb_env_copyfd2(MDB_env *env, mdb_filehandle_t fd, unsigned int flags)
Copy an LMDB environment to the specified file descriptor, with options.
void mdb_dbi_close(MDB_env *env, MDB_dbi dbi)
Close a database handle.
int mdb_env_copy(MDB_env *env, const char *path)
Copy an LMDB environment to the specified path.
int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
Set a custom key comparison function for a database.
int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel)
Set a relocation function for a MDB_FIXEDMAP database.
int mdb_txn_renew(MDB_txn *txn)
Renew a read-only transaction.
void * mdb_env_get_userctx(MDB_env *env)
Get the application information associated with the MDB_env.
int mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers)
Get the maximum number of threads/reader slots for the environment.
void mdb_txn_abort(MDB_txn *txn)
Abandon all the operations of the transaction instead of saving them.
int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx)
Set a context pointer for a MDB_FIXEDMAP database's relocation function.
int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff)
Set environment flags.
int mdb_txn_commit(MDB_txn *txn)
Commit all the operations of a transaction into the database.
int mdb_env_sync(MDB_env *env, int force)
Flush the data buffers to disk.
int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data)
Get items from a database.
int mdb_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags)
Retrieve the DB flags for a database handle.
char * mdb_strerror(int err)
Return a string describing a given error code.
int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor)
Create a cursor handle.
int mdb_env_set_mapsize(MDB_env *env, size_t size)
Set the size of the memory map to use for this environment.
int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs)
Set the maximum number of named databases for the environment.
int mdb_env_create(MDB_env **env)
Create an LMDB environment handle.
int mdb_env_get_maxkeysize(MDB_env *env)
Get the maximum size of keys and MDB_DUPSORT data we can write.
int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data)
Delete items from a database.
int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del)
Empty or delete+close a database.
int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
Open a database in the environment.
int mdb_env_get_path(MDB_env *env, const char **path)
Return the path that was used in mdb_env_open().
int mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor)
Renew a cursor handle.
int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
Set a custom data comparison function for a MDB_DUPSORT database.
void mdb_cursor_close(MDB_cursor *cursor)
Close a cursor handle.
int mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn)
Create a transaction for use with the environment.
size_t mdb_txn_id(MDB_txn *txn)
Return the transaction's ID.
MDB_cursor_op
Cursor Get operations.
int mdb_env_set_maxreaders(MDB_env *env, unsigned int readers)
Set the maximum number of threads/reader slots for the environment.
int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat)
Retrieve statistics for a database.
MDB_txn * mdb_cursor_txn(MDB_cursor *cursor)
Return the cursor's transaction handle.
int mdb_env_get_fd(MDB_env *env, mdb_filehandle_t *fd)
Return the filedescriptor for the given environment.
int mdb_env_set_userctx(MDB_env *env, void *ctx)
Set application information associated with the MDB_env.
int mdb_env_stat(MDB_env *env, MDB_stat *stat)
Return statistics about the LMDB environment.
MDB_env * mdb_txn_env(MDB_txn *txn)
Returns the transaction's MDB_env.
@ MDB_SET
Position at specified key.
size_t ms_entries
Number of data items.
size_t mv_size
size of the data item
void * mv_data
address of the data item
void() MDB_rel_func(MDB_val *item, void *oldptr, void *newptr, void *relctx)
A callback function used to relocate a position-dependent data item in a fixed-address database.
int() MDB_cmp_func(const MDB_val *a, const MDB_val *b)
A callback function used to compare two keys in a database.
unsigned int MDB_dbi
A handle for an individual database in the DB environment.
<lmdb++.h> - C++11 wrapper for LMDB.
static void env_info(MDB_env *env, MDB_envinfo *stat)
static void * env_get_userctx(MDB_env *env)
static void dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags)
static void cursor_renew(MDB_txn *txn, MDB_cursor *cursor)
static void env_copy_fd(MDB_env *env, mdb_filehandle_t fd, unsigned int flags)
static void txn_commit(MDB_txn *txn)
static void env_open(MDB_env *env, const char *path, unsigned int flags, mode mode)
static MDB_txn * cursor_txn(MDB_cursor *cursor) noexcept
static void dbi_drop(MDB_txn *txn, MDB_dbi dbi, bool del)
static bool dbi_put(MDB_txn *txn, MDB_dbi dbi, const MDB_val *key, MDB_val *data, unsigned int flags)
static void env_create(MDB_env **env)
static unsigned int env_get_max_keysize(MDB_env *env)
static void cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor)
static void cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data, unsigned int flags)
static void env_get_fd(MDB_env *env, mdb_filehandle_t *fd)
static void cursor_del(MDB_cursor *cursor, unsigned int flags)
static void dbi_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel)
static void dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
static void dbi_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
static void env_set_max_readers(MDB_env *env, unsigned int count)
static MDB_env * txn_env(MDB_txn *txn) noexcept
static void env_set_mapsize(MDB_env *env, std::size_t size)
static void env_set_max_dbs(MDB_env *env, MDB_dbi count)
static void env_close(MDB_env *env) noexcept
static void env_set_flags(MDB_env *env, unsigned int flags, bool onoff)
static void txn_renew(MDB_txn *txn)
static void cursor_close(MDB_cursor *cursor) noexcept
static bool cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
static void cursor_count(MDB_cursor *cursor, std::size_t &count)
static void env_get_max_readers(MDB_env *env, unsigned int *count)
static MDB_dbi cursor_dbi(MDB_cursor *cursor) noexcept
static void txn_abort(MDB_txn *txn) noexcept
static void env_get_path(MDB_env *env, const char **path)
static void dbi_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat)
static bool dbi_del(MDB_txn *txn, MDB_dbi dbi, const MDB_val *key, const MDB_val *data)
static bool dbi_get(MDB_txn *txn, MDB_dbi dbi, const MDB_val *key, MDB_val *data)
static void env_copy(MDB_env *env, const char *path, unsigned int flags)
static void txn_reset(MDB_txn *txn) noexcept
static void env_set_userctx(MDB_env *env, void *ctx)
static void dbi_close(MDB_env *env, MDB_dbi dbi) noexcept
static void env_stat(MDB_env *env, MDB_stat *stat)
static void env_sync(MDB_env *env, bool force)
static void dbi_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
static void env_get_flags(MDB_env *env, unsigned int *flags)
static void txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn)
static void dbi_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx)
const struct ncbi::grid::netcache::search::fields::SIZE size
const struct ncbi::grid::netcache::search::fields::KEY key
const GenericPointer< typename T::ValueType > T2 value
static const GLdouble origin[]
int mdb_filehandle_t
An abstraction for a file handle.
mode_t mdb_mode_t
Unix permissions for creating files, or dummy definition for Windows.
Cursors are used for all DB operations.
The database environment.
Information about the environment.
Statistics for a database in the environment.
Generic structure used for passing keys and data in and out of the database.