Electroneum
lmdb Namespace Reference

Namespaces

 stream
 

Classes

struct  abort_txn
 
struct  abort_write_txn
 
struct  basic_table
 Helper for grouping typical LMDB DBI options when key and value are fixed types. More...
 
struct  close_cursor
 
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...
 
struct  identity
 Prevent instantiation of std::underlying_type<T> when T is not enum. More...
 
class  key_iterator
 
class  key_stream
 
struct  release_read_txn
 
struct  table
 Helper for grouping typical LMDB DBI options. 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 = typename std::conditional< std::is_enum< T >::value, std::underlying_type< T >, identity< T > >::type::type
 

Enumerations

enum  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_tto_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 = typedef std::unique_ptr<MDB_env, close_env>

Definition at line 51 of file database.h.

◆ native_type

template<typename T >
using lmdb::native_type = typedef typename std::conditional< std::is_enum<T>::value, std::underlying_type<T>, identity<T> >::type::type

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 77 of file util.h.

◆ read_txn

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

Definition at line 93 of file transaction.h.

◆ suspended_txn

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

Definition at line 92 of file transaction.h.

◆ write_txn

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

Definition at line 94 of file transaction.h.

Enumeration Type Documentation

◆ error

enum lmdb::error : int
strong

Tracks LMDB error codes.

Definition at line 44 of file error.h.

44  : int
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.

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  }
const uint32_t T[512]
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  }
const uint32_t T[512]
void * memcpy(void *a, const void *b, size_t c)
int bool
Definition: stdbool.h:36
Here is the call graph for this function:
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:

◆ 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;
86  ELECTRONEUM_LMDB_CHECK(mdb_cursor_open(&txn, tbl, &cur));
87  return std::unique_ptr<MDB_cursor, D>{cur};
88  }
#define ELECTRONEUM_LMDB_CHECK(...)
Executes a LMDB command, and returns errors via lmdb::error enum.
Definition: error.h:33
struct MDB_cursor MDB_cursor
Opaque structure for navigating through a database.
Definition: lmdb.h:273
int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor)
Create a cursor handle.
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 
87  ELECTRONEUM_LMDB_CHECK(mdb_env_open(out.get(), path, 0, open_flags));
88  return {std::move(out)};
89  }
#define ELECTRONEUM_LMDB_CHECK(...)
Executes a LMDB command, and returns errors via lmdb::error enum.
Definition: error.h:33
int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs)
Set the maximum number of named databases for the environment.
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
int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
Open an environment handle.
int mdb_env_create(MDB_env **env)
Create an LMDB environment handle.
const T & move(const T &t)
Definition: gtest-port.h:1317
#define ELECTRONEUM_PRECOND(...)
If precondition fails, return ::error::kInvalidArgument in current scope.
Definition: expect.h:39
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  }

◆ 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  }

◆ 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()

constexpr epee::span<const std::uint8_t> lmdb::to_byte_span ( MDB_val  value)
inlinenoexcept
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  }
unsigned char uint8_t
Definition: stdint.h:124
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
Here is the caller graph for this function:

◆ to_native()

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

Definition at line 81 of file util.h.

82  {
83  return U(value);
84  }
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
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
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
Here is the caller graph for this function: