Electroneum
Loading...
Searching...
No Matches
lmdb Namespace Reference

Namespaces

namespace  stream

Classes

struct  close_env
 Closes LMDB environment handle. More...
struct  context
 Context given to LMDB. More...
class  database
 Manages a LMDB environment for safe memory-map resizing. Thread-safe. More...
class  key_iterator
class  key_stream
struct  table
 Helper for grouping typical LMDB DBI options. More...
struct  basic_table
 Helper for grouping typical LMDB DBI options when key and value are fixed types. More...
struct  abort_txn
struct  release_read_txn
struct  abort_write_txn
struct  close_cursor
struct  identity
 Prevent instantiation of std::underlying_type<T> when T is not enum. More...
class  value_iterator
class  value_stream

Typedefs

using environment = std::unique_ptr<MDB_env, close_env>
using suspended_txn = std::unique_ptr<MDB_txn, abort_txn>
using read_txn = std::unique_ptr<MDB_txn, release_read_txn>
using write_txn = std::unique_ptr<MDB_txn, abort_write_txn>
template<typename T>
using native_type

Enumerations

enum class  error : int
 Tracks LMDB error codes. More...

Functions

expect< environmentopen_environment (const char *path, MDB_dbi max_dbs) noexcept
std::error_category const & error_category () noexcept
std::error_code make_error_code (error value) noexcept
template<typename K, typename V>
bool operator== (key_iterator< K, V > const &lhs, key_iterator< K, V > const &rhs) noexcept
template<typename K, typename V>
bool operator!= (key_iterator< K, V > const &lhs, key_iterator< K, V > const &rhs) noexcept
template<typename D>
expect< std::unique_ptr< MDB_cursor, D > > open_cursor (MDB_txn &txn, MDB_dbi tbl) noexcept
template<typename T, typename U = typename std::underlying_type<T>::type>
constexpr U to_native (T value) noexcept
template<typename T>
MDB_val to_val (T &&value) noexcept
constexpr epee::span< const std::uint8_t > to_byte_span (MDB_val value) noexcept
template<typename T, std::size_t offset = 0>
int less (MDB_val const *left, MDB_val const *right) noexcept
template<typename T, std::size_t offset = 0>
int compare (MDB_val const *left, MDB_val const *right) noexcept
template<typename T, typename F, std::size_t offset>
bool operator== (value_iterator< T, F, offset > const &lhs, value_iterator< T, F, offset > const &rhs) noexcept
template<typename T, typename F, std::size_t offset>
bool operator!= (value_iterator< T, F, offset > const &lhs, value_iterator< T, F, offset > const &rhs) noexcept

Typedef Documentation

◆ environment

using lmdb::environment = std::unique_ptr<MDB_env, close_env>

Definition at line 51 of file database.h.

◆ native_type

template<typename T>
using lmdb::native_type
Initial value:
typename std::conditional<
std::is_enum<T>::value, std::underlying_type<T>, identity<T>
>::type::type
Prevent instantiation of std::underlying_type<T> when T is not enum.
Definition util.h:62

Get the native type for enums, or return T unchanged. Useful for merging generated machine code for templated functions that use enums with identical size-widths without relying on aggressive identical comdat folding (ICF) support in linker. So with enum defintion enum class enum_foo : unsigned long {}; will always yield assert(&func_foo<unsigned long> == &func_foo<native_type<enum_foo>>).

Definition at line 75 of file util.h.

◆ read_txn

using lmdb::read_txn = std::unique_ptr<MDB_txn, release_read_txn>

Definition at line 93 of file transaction.h.

◆ suspended_txn

using lmdb::suspended_txn = std::unique_ptr<MDB_txn, abort_txn>

Definition at line 92 of file transaction.h.

◆ write_txn

using lmdb::write_txn = std::unique_ptr<MDB_txn, abort_write_txn>

Definition at line 94 of file transaction.h.

Enumeration Type Documentation

◆ error

enum class lmdb::error : int
strong

Tracks LMDB error codes.

Definition at line 44 of file error.h.

45 {
46 // 0 is reserved for no error, as per expect<T>
47 // All other errors are the values reported by LMDB
48 };

Function Documentation

◆ compare()

template<typename T, std::size_t offset = 0>
int lmdb::compare ( MDB_val const * left,
MDB_val const * right )
inlinenoexcept

A LMDB comparison function that uses std::memcmp.

\toaram T is !epee::has_padding

Template Parameters
offsetto T within the value.
Returns
The result of std::memcmp over the value.

Definition at line 135 of file util.h.

136 {
137 static_assert(!epee::has_padding<T>(), "memcmp will not work");
138 if (!left || !right || left->mv_size < sizeof(T) + offset || right->mv_size < sizeof(T) + offset)
139 {
140 assert("invalid use of custom comparison" == 0);
141 return -1;
142 }
143 return std::memcmp(
144 static_cast<char*>(left->mv_data) + offset,
145 static_cast<char*>(right->mv_data) + offset,
146 sizeof(T)
147 );
148 }
constexpr bool has_padding() noexcept
Definition span.h:138
#define T(x)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ error_category()

std::error_category const & lmdb::error_category ( )
noexcept

Definition at line 92 of file error.cpp.

93 {
94 static const category instance{};
95 return instance;
96 }
Here is the caller graph for this function:

◆ less()

template<typename T, std::size_t offset = 0>
int lmdb::less ( MDB_val const * left,
MDB_val const * right )
inlinenoexcept

A LMDB comparison function that uses operator<.

Template Parameters
Thas a defined operator< .
offsetto T within the value.
Returns
-1 if left < right, 1 if right < left, and 0 otherwise.

Definition at line 111 of file util.h.

112 {
113 if (!left || !right || left->mv_size < sizeof(T) + offset || right->mv_size < sizeof(T) + offset)
114 {
115 assert("invalid use of custom comparison" == 0);
116 return -1;
117 }
118
119 T left_val;
120 T right_val;
121 std::memcpy(std::addressof(left_val), static_cast<char*>(left->mv_data) + offset, sizeof(T));
122 std::memcpy(std::addressof(right_val), static_cast<char*>(right->mv_data) + offset, sizeof(T));
123 return left_val < right_val ? -1 : bool(right_val < left_val);
124 }
Here is the caller graph for this function:

◆ make_error_code()

std::error_code lmdb::make_error_code ( error value)
inlinenoexcept

Definition at line 52 of file error.h.

53 {
54 return std::error_code{int(value), error_category()};
55 }
std::error_category const & error_category() noexcept
Definition error.cpp:92
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
Here is the call graph for this function:
Here is the caller graph for this function:

◆ open_cursor()

template<typename D>
expect< std::unique_ptr< MDB_cursor, D > > lmdb::open_cursor ( MDB_txn & txn,
MDB_dbi tbl )
inlinenoexcept

Definition at line 83 of file transaction.h.

84 {
85 MDB_cursor* cur = nullptr;
87 return std::unique_ptr<MDB_cursor, D>{cur};
88 }
int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor)
Create a cursor handle.
struct MDB_cursor MDB_cursor
Opaque structure for navigating through a database.
Definition lmdb.h:273
#define ELECTRONEUM_LMDB_CHECK(...)
Executes a LMDB command, and returns errors via lmdb::error enum.
Definition error.h:33
Here is the call graph for this function:

◆ open_environment()

expect< environment > lmdb::open_environment ( const char * path,
MDB_dbi max_dbs )
noexcept
Returns
LMDB environment at path with a max of max_dbs tables.

Definition at line 78 of file database.cpp.

79 {
80 ELECTRONEUM_PRECOND(path != nullptr);
81
82 MDB_env* obj = nullptr;
83 ELECTRONEUM_LMDB_CHECK(mdb_env_create(std::addressof(obj)));
84 environment out{obj};
85
86 ELECTRONEUM_LMDB_CHECK(mdb_env_set_maxdbs(out.get(), max_dbs));
87 ELECTRONEUM_LMDB_CHECK(mdb_env_open(out.get(), path, 0, open_flags));
88 return {std::move(out)};
89 }
#define ELECTRONEUM_PRECOND(...)
If precondition fails, return error::kInvalidArgument in current scope.
Definition expect.h:39
int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
Open an environment handle.
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.
struct MDB_env MDB_env
Opaque structure for a database environment.
Definition lmdb.h:260
std::unique_ptr< MDB_env, close_env > environment
Definition database.h:51
Here is the call graph for this function:

◆ operator!=() [1/2]

template<typename K, typename V>
bool lmdb::operator!= ( key_iterator< K, V > const & lhs,
key_iterator< K, V > const & rhs )
inlinenoexcept

Definition at line 259 of file key_stream.h.

260 {
261 return !lhs.equal(rhs);
262 }
bool equal(key_iterator const &rhs) const noexcept
Definition key_stream.h:97

◆ operator!=() [2/2]

template<typename T, typename F, std::size_t offset>
bool lmdb::operator!= ( value_iterator< T, F, offset > const & lhs,
value_iterator< T, F, offset > const & rhs )
inlinenoexcept

Definition at line 282 of file value_stream.h.

283 {
284 return !lhs.equal(rhs);
285 }
bool equal(value_iterator const &rhs) const noexcept

◆ operator==() [1/2]

template<typename K, typename V>
bool lmdb::operator== ( key_iterator< K, V > const & lhs,
key_iterator< K, V > const & rhs )
inlinenoexcept

Definition at line 252 of file key_stream.h.

253 {
254 return lhs.equal(rhs);
255 }

◆ operator==() [2/2]

template<typename T, typename F, std::size_t offset>
bool lmdb::operator== ( value_iterator< T, F, offset > const & lhs,
value_iterator< T, F, offset > const & rhs )
inlinenoexcept

Definition at line 275 of file value_stream.h.

276 {
277 return lhs.equal(rhs);
278 }

◆ to_byte_span()

epee::span< const std::uint8_t > lmdb::to_byte_span ( MDB_val value)
inlineconstexprnoexcept
Returns
A span over the same chunk of memory as value.

Definition at line 97 of file util.h.

98 {
99 return {static_cast<const std::uint8_t*>(value.mv_data), value.mv_size};
100 }
Here is the caller graph for this function:

◆ to_native()

template<typename T, typename U = typename std::underlying_type<T>::type>
U lmdb::to_native ( T value)
inlineconstexprnoexcept
Returns
value as its native type.

Definition at line 81 of file util.h.

82 {
83 return U(value);
84 }
Here is the caller graph for this function:

◆ to_val()

template<typename T>
MDB_val lmdb::to_val ( T && value)
inlinenoexcept
Returns
value bytes in a LMDB MDB_val object.

Definition at line 88 of file util.h.

89 {
90 // lmdb does not touch user data, so const_cast is acceptable
91 static_assert(!std::is_rvalue_reference<T&&>(), "cannot use temporary value");
92 void const* const temp = reinterpret_cast<void const*>(std::addressof(value));
93 return MDB_val{sizeof(value), const_cast<void*>(temp)};
94 }
Generic structure used for passing keys and data in and out of the database.
Definition lmdb.h:286
Here is the caller graph for this function: