| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <utility> |
| 4 |
|
| 5 |
#include "common/expect.h" |
| 6 |
#include "lmdb/error.h" |
| 7 |
#include "lmdb/key_stream.h" |
| 8 |
#include "lmdb/util.h" |
| 9 |
#include "lmdb/value_stream.h" |
| 10 |
|
| 11 |
namespace lmdb |
| 12 |
{ |
| 13 |
|
| 14 |
struct table |
| 15 |
{ |
| 16 |
char const* const name; |
| 17 |
const unsigned flags; |
| 18 |
MDB_cmp_func* const key_cmp; |
| 19 |
MDB_cmp_func* const value_cmp; |
| 20 |
|
| 21 |
|
| 22 |
expect<MDB_dbi> open(MDB_txn& write_txn) const noexcept; |
| 23 |
}; |
| 24 |
|
| 25 |
|
| 26 |
template<typename K, typename V> |
| 27 |
struct basic_table : table |
| 28 |
{ |
| 29 |
using key_type = K; |
| 30 |
using value_type = V; |
| 31 |
|
| 32 |
|
| 33 |
static constexpr unsigned compute_flags(const unsigned flags) noexcept |
| 34 |
{ |
| 35 |
return flags | ((flags & MDB_DUPSORT) ? MDB_DUPFIXED : 0); |
| 36 |
} |
| 37 |
|
| 38 |
constexpr explicit basic_table(const char* name, unsigned flags = 0, MDB_cmp_func value_cmp = nullptr) noexcept |
| 39 |
: table{name, compute_flags(flags), &lmdb::less<lmdb::native_type<K>>, value_cmp} |
| 40 |
{} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
template<typename U, typename F = U, std::size_t offset = 0> |
| 55 |
static expect<F> get_value(MDB_val value) noexcept |
| 56 |
{ |
| 57 |
static_assert(std::is_same<U, V>(), "bad MONERO_FIELD?"); |
| 58 |
static_assert(std::is_trivially_copyable<F>(), "F must be memcpy safe"); |
| 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 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
template<typename D> |
| 77 |
expect<key_stream<K, V, D>> |
| 78 |
static get_key_stream(std::unique_ptr<MDB_cursor, D> cur) noexcept |
| 79 |
{ |
| 80 |
MONERO_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(); |
| 90 |
} |
| 91 |
return key_stream<K, V, D>{std::move(cur)}; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
template<typename D> |
| 102 |
expect<value_stream<V, D>> |
| 103 |
static get_value_stream(K const& key, std::unique_ptr<MDB_cursor, D> cur) noexcept |
| 104 |
{ |
| 105 |
MONERO_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(); |
| 115 |
} |
| 116 |
return value_stream<V, D>{std::move(cur)}; |
| 117 |
} |
| 118 |
}; |
| 119 |
} |
| 120 |
|