Debian Codegraph @

src/lmdb/table.h

raw symbols SCIP
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 //! Helper for grouping typical LMDB DBI options.
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 //! \pre `name != nullptr` \return Open table.
22 expect<MDB_dbi> open(MDB_txn& write_txn) const noexcept;
23 };
24
25 //! Helper for grouping typical LMDB DBI options when key and value are fixed types.
26 template<typename K, typename V>
27 struct basic_table : table
28 {
29 using key_type = K;
30 using value_type = V;
31
32 //! \return Additional LMDB flags based on `flags` value.
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 \tparam U must be same as `V`; used for sanity checking.
44 \tparam F is the type within `U` that is being extracted.
45 \tparam offset to `F` within `U`.
46
47 \note If using `F` and `offset` to retrieve a specific field, use
48 `MONERO_FIELD` macro in `src/lmdb/util.h` which calculates the
49 offset automatically.
50
51 \return Value of type `F` at `offset` within `value` which has
52 type `U`.
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 \pre `cur != nullptr`.
71 \param cur Active cursor on table. Returned in object on success,
72 otherwise destroyed.
73 \return A handle to the first key/value in the table linked
74 to `cur` or an empty `key_stream`.
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(); // return empty set
90 }
91 return key_stream<K, V, D>{std::move(cur)};
92 }
93
94 /*!
95 \pre `cur != nullptr`.
96 \param cur Active cursor on table. Returned in object on success,
97 otherwise destroyed.
98 \return A handle to the first value at `key` in the table linked
99 to `cur` or an empty `value_stream`.
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(); // return empty set
115 }
116 return value_stream<V, D>{std::move(cur)};
117 }
118 };
119 } // lmdb
120