Electroneum
lmdb::basic_table< K, V > Struct Template Reference

Helper for grouping typical LMDB DBI options when key and value are fixed types. More...

#include <table.h>

Inheritance diagram for lmdb::basic_table< K, V >:
Collaboration diagram for lmdb::basic_table< K, V >:

Public Types

using key_type = K
 
using value_type = V
 

Public Member Functions

constexpr basic_table (const char *name, unsigned flags=0, MDB_cmp_func value_cmp=nullptr) noexcept
 
- Public Member Functions inherited from lmdb::table
expect< MDB_dbiopen (MDB_txn &write_txn) const noexcept
 

Static Public Member Functions

static constexpr unsigned compute_flags (const unsigned flags) noexcept
 
template<typename U , typename F = U, std::size_t offset = 0>
static expect< Fget_value (MDB_val value) noexcept
 
template<typename D >
static expect< key_stream< K, V, D > > get_key_stream (std::unique_ptr< MDB_cursor, D > cur) noexcept
 
template<typename D >
static expect< value_stream< V, D > > get_value_stream (K const &key, std::unique_ptr< MDB_cursor, D > cur) noexcept
 

Additional Inherited Members

- Public Attributes inherited from lmdb::table
char const *const name
 
const unsigned flags
 
MDB_cmp_func *const key_cmp
 
MDB_cmp_func *const value_cmp
 

Detailed Description

template<typename K, typename V>
struct lmdb::basic_table< K, V >

Helper for grouping typical LMDB DBI options when key and value are fixed types.

Definition at line 27 of file table.h.

Member Typedef Documentation

◆ key_type

template<typename K , typename V >
using lmdb::basic_table< K, V >::key_type = K

Definition at line 29 of file table.h.

◆ value_type

template<typename K , typename V >
using lmdb::basic_table< K, V >::value_type = V

Definition at line 30 of file table.h.

Constructor & Destructor Documentation

◆ basic_table()

template<typename K , typename V >
constexpr lmdb::basic_table< K, V >::basic_table ( const char *  name,
unsigned  flags = 0,
MDB_cmp_func  value_cmp = nullptr 
)
inlineexplicitnoexcept

Definition at line 38 of file table.h.

39  : table{name, compute_flags(flags), &lmdb::less<lmdb::native_type<K>>, value_cmp}
40  {}
std::vector< std::vector< _variant_t > > table
const unsigned flags
Definition: table.h:17
static constexpr unsigned compute_flags(const unsigned flags) noexcept
Definition: table.h:33
MDB_cmp_func *const value_cmp
Definition: table.h:19
char const *const name
Definition: table.h:16
Here is the call graph for this function:

Member Function Documentation

◆ compute_flags()

template<typename K , typename V >
static constexpr unsigned lmdb::basic_table< K, V >::compute_flags ( const unsigned  flags)
inlinestaticnoexcept
Returns
Additional LMDB flags based on flags value.

Definition at line 33 of file table.h.

34  {
35  return flags | ((flags & MDB_DUPSORT) ? MDB_DUPFIXED : 0);
36  }
const unsigned flags
Definition: table.h:17
#define MDB_DUPFIXED
Definition: lmdb.h:351
#define MDB_DUPSORT
Definition: lmdb.h:345
Here is the caller graph for this function:

◆ get_key_stream()

template<typename K , typename V >
template<typename D >
static expect<key_stream<K, V, D> > lmdb::basic_table< K, V >::get_key_stream ( std::unique_ptr< MDB_cursor, D >  cur)
inlinestaticnoexcept
Precondition
cur != nullptr.
Parameters
curActive cursor on table. Returned in object on success, otherwise destroyed.
Returns
A handle to the first key/value in the table linked to cur or an empty key_stream.

Definition at line 78 of file table.h.

79  {
80  ELECTRONEUM_PRECOND(cur != nullptr);
81 
82  MDB_val key;
83  MDB_val value;
84  const int err = mdb_cursor_get(cur.get(), &key, &value, MDB_FIRST);
85  if (err)
86  {
87  if (err != MDB_NOTFOUND)
88  return {lmdb::error(err)};
89  cur.reset(); // return empty set
90  }
91  return key_stream<K, V, D>{std::move(cur)};
92  }
#define MDB_NOTFOUND
Definition: lmdb.h:439
const char * key
Definition: hmac_keccak.cpp:39
int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
Retrieve by cursor.
Generic structure used for passing keys and data in and out of the database.
Definition: lmdb.h:286
const T & move(const T &t)
Definition: gtest-port.h:1317
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
#define ELECTRONEUM_PRECOND(...)
If precondition fails, return ::error::kInvalidArgument in current scope.
Definition: expect.h:39
error
Tracks LMDB error codes.
Definition: error.h:44
Here is the call graph for this function:

◆ get_value()

template<typename K , typename V >
template<typename U , typename F = U, std::size_t offset = 0>
static expect<F> lmdb::basic_table< K, V >::get_value ( MDB_val  value)
inlinestaticnoexcept
Template Parameters
Umust be same as V; used for sanity checking.
Fis the type within U that is being extracted.
offsetto F within U.
Note
If using F and offset to retrieve a specific field, use ELECTRONEUM_FIELD macro in src/lmdb/util.h which calculates the offset automatically.
Returns
Value of type F at offset within value which has type U.

Definition at line 55 of file table.h.

56  {
57  static_assert(std::is_same<U, V>(), "bad ELECTRONEUM_FIELD?");
58  static_assert(std::is_pod<F>(), "F must be POD");
59  static_assert(sizeof(F) + offset <= sizeof(U), "bad field type and/or offset");
60 
61  if (value.mv_size != sizeof(U))
62  return {lmdb::error(MDB_BAD_VALSIZE)};
63 
64  F out;
65  std::memcpy(std::addressof(out), static_cast<char*>(value.mv_data) + offset, sizeof(out));
66  return out;
67  }
#define MDB_BAD_VALSIZE
Definition: lmdb.h:480
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
void * memcpy(void *a, const void *b, size_t c)
#define F(s)
error
Tracks LMDB error codes.
Definition: error.h:44
Here is the call graph for this function:

◆ get_value_stream()

template<typename K , typename V >
template<typename D >
static expect<value_stream<V, D> > lmdb::basic_table< K, V >::get_value_stream ( K const &  key,
std::unique_ptr< MDB_cursor, D >  cur 
)
inlinestaticnoexcept
Precondition
cur != nullptr.
Parameters
curActive cursor on table. Returned in object on success, otherwise destroyed.
Returns
A handle to the first value at key in the table linked to cur or an empty value_stream.

Definition at line 103 of file table.h.

104  {
105  ELECTRONEUM_PRECOND(cur != nullptr);
106 
107  MDB_val key_bytes = lmdb::to_val(key);
108  MDB_val value;
109  const int err = mdb_cursor_get(cur.get(), &key_bytes, &value, MDB_SET);
110  if (err)
111  {
112  if (err != MDB_NOTFOUND)
113  return {lmdb::error(err)};
114  cur.reset(); // return empty set
115  }
116  return value_stream<V, D>{std::move(cur)};
117  }
#define MDB_NOTFOUND
Definition: lmdb.h:439
const char * key
Definition: hmac_keccak.cpp:39
Definition: lmdb.h:422
int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
Retrieve by cursor.
Generic structure used for passing keys and data in and out of the database.
Definition: lmdb.h:286
const T & move(const T &t)
Definition: gtest-port.h:1317
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
#define ELECTRONEUM_PRECOND(...)
If precondition fails, return ::error::kInvalidArgument in current scope.
Definition: expect.h:39
MDB_val to_val(T &&value) noexcept
Definition: util.h:88
error
Tracks LMDB error codes.
Definition: error.h:44
Here is the call graph for this function:

The documentation for this struct was generated from the following file: