Repository navigation
Expand file tree
/
Copy pathlmdb_wrapper_impl.h
More file actions
332 lines (297 loc) · 11.5 KB
/
Copy pathlmdb_wrapper_impl.h
File metadata and controls
332 lines (297 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#pragma once
#include <lmdb.h>
//#include "unique_resource.h"
#include <memory>
#include <string_view>
#include <stdexcept>
#pragma warning(disable:4275) // non dll-interface class 'std::runtime_error' used as base for dll-interface class 'fmt::v6::format_error'
#include <fmt/format.h>
#include <tuple>
#include "span.hpp"
namespace mdb {
namespace impl {
static inline void handle_mdb_error(int err, const char* context) {
if (err != 0) {
auto msg = fmt::format("{} because: {}", context, mdb_strerror(err));
//fmt::print(msg);
throw std::runtime_error(msg);
}
}
static inline auto make_mdb_dbi(MDB_txn* txn, const char* name, unsigned int flags) {
MDB_dbi dbi;
int ret = mdb_dbi_open(txn, name, flags, &dbi);
//MDB_env* env = mdb_txn_env(txn); // txn may be closed before the dbi is closed
handle_mdb_error(ret, "failed to open lmdb database");
return dbi; // todo: the docs say this doesn't really need to be closed ?
}
static inline auto make_mdb_cursor(MDB_txn* txn, MDB_dbi dbi) {
MDB_cursor* cursor;
int ret = mdb_cursor_open(txn, dbi, &cursor);
handle_mdb_error(ret, "failed to open cursor");
return std::unique_ptr<MDB_cursor, void(*)(MDB_cursor*)> { cursor, mdb_cursor_close };
}
static inline auto make_mdb_env() {
MDB_env* env = nullptr;
int ret = mdb_env_create(&env);
handle_mdb_error(ret, "failed to create lmdb environment");
return std::unique_ptr<MDB_env, void(*)(MDB_env*)> { env, mdb_env_close };
}
// from https://www.reddit.com/r/cpp/comments/4yp7fv/c17_structured_bindings_convert_struct_to_a_tuple/
template <class T, class... TArgs> decltype(void(T { std::declval<TArgs>()... }), std::true_type {}) test_is_braces_constructible(int);
template <class, class...> std::false_type test_is_braces_constructible(...);
template <class T, class... TArgs> using is_braces_constructible = decltype(test_is_braces_constructible<T, TArgs...>(0));
struct any_type {
// added the const because of std::optional, based on
// https://github.com/apolukhin/magic_get/commit/1b138a4bd76b1118c433ed5623f0447f64cf057d
template<class T>
constexpr operator T() const; // non explicit
};
template<class T>
auto to_tuple(T&& object) noexcept {
using type = std::decay_t<T>;
if constexpr (is_braces_constructible<type, any_type, any_type, any_type, any_type, any_type, any_type>{}) {
auto&& [p1, p2, p3, p4, p5, p6] = (type&)object;
return std::tuple(p1, p2, p3, p4, p5, p6);
} else if constexpr (is_braces_constructible<type, any_type, any_type, any_type, any_type, any_type>{}) {
auto&& [p1, p2, p3, p4, p5] = (type&)object;
return std::tuple(p1, p2, p3, p4, p5);
} else if constexpr (is_braces_constructible<type, any_type, any_type, any_type, any_type>{}) {
auto&& [p1, p2, p3, p4] = (type&)object;
return std::tuple(p1, p2, p3, p4);
} else if constexpr (is_braces_constructible<type, any_type, any_type, any_type>{}) {
auto&& [p1, p2, p3] = (type&)object;
return std::tuple(p1, p2, p3);
} else if constexpr (is_braces_constructible<type, any_type, any_type>{}) {
auto&& [p1, p2] = (type&)object;
return std::tuple(p1, p2);
} else if constexpr (is_braces_constructible<type, any_type>{}) {
auto&& [p1] = (type&)object;
return std::tuple(p1);
} else {
return std::tuple();
}
}
template< class T > // todo: should be standard in C++20
struct type_identity {
using type = T;
};
template<typename T>
struct is_string_view : std::false_type {};
template <>
struct is_string_view<std::string_view> : std::true_type {};
template<typename T>
constexpr bool is_string_view_v = is_string_view<T>::value;
template<typename T>
struct is_span : std::false_type {};
template <typename T>
struct is_span<tcb::span<T>> : std::true_type {};
template<typename T>
constexpr bool is_span_v = is_span<T>::value;
template<typename T>
using is_view = std::disjunction< is_string_view<T>, is_span<T> >;
template<typename T>
constexpr bool is_view_v = is_view<T>::value;
template <typename Tuple>
struct has_view;
template <typename... Us>
struct has_view<std::tuple<Us...>> : std::disjunction<is_view<Us>...> {};
template<typename T>
constexpr bool contains_a_view() {
if constexpr (std::is_aggregate_v<T>)
return has_view< decltype(to_tuple(T {})) > ::value;
return false;
}
using stored_size_type = uint32_t;
template<typename... TypeIdent, std::size_t... Is>
constexpr std::size_t get_total_non_view_size_bytes(std::index_sequence<Is...>, TypeIdent... type_idents)
{
auto get_single = [&](auto type_ident, auto idx_c) -> std::size_t {
using T = typename decltype(type_ident)::type;
constexpr std::size_t idx = decltype(idx_c)::value;
if constexpr (is_view_v<T>) {
if constexpr (idx == sizeof...(Is) - 1)
return 0;
return sizeof(stored_size_type);
} else {
return sizeof(T);
}
};
return (get_single(type_idents, std::integral_constant<std::size_t, Is>{}) + ...);
}
template<typename T>
constexpr std::size_t get_view_size_bytes(const T& elem) {
if constexpr (is_string_view_v<T>)
return elem.size();
else if constexpr (is_span_v<T>)
return elem.size() * sizeof(typename T::element_type);
return 0;
}
template<typename... Ts, std::size_t... Is>
std::size_t get_aggregate_size(const std::tuple<Ts...>& tup, std::index_sequence<Is...>)
{
return std::apply([&](auto&... elems) {
constexpr std::size_t non_view_size = get_total_non_view_size_bytes(
std::index_sequence<Is...>{}, type_identity<Ts> {} ...);
std::size_t total_size = non_view_size + (get_view_size_bytes(elems) + ...);
return total_size;
}, tup);
}
template<bool check_size, typename... Ts, std::size_t... Is>
MDB_val to_val_from_aggregate(const std::tuple<Ts...> &tup, std::index_sequence<Is...> idxs,
char *buf, std::size_t buf_size)
{
std::size_t ofs = 0;
auto copy = [&](auto& elem, auto idx_c) {
using T = std::decay_t<decltype(elem)>;
constexpr std::size_t idx = decltype(idx_c)::value;
if constexpr (is_view_v<T>) {
stored_size_type size = get_view_size_bytes(elem);
if constexpr (idx != sizeof...(Is) - 1) {
memcpy(&buf[ofs], &size, sizeof(stored_size_type));
ofs += sizeof(stored_size_type);
}
if (size == 0) // accessing elem.data() is UB if size == 0
return;
memcpy(&buf[ofs], elem.data(), size);
ofs += size;
} else {
memcpy(&buf[ofs], &elem, sizeof(T));
ofs += sizeof(T);
}
};
if constexpr (check_size) {
// check if the input data is valid, todo: disable this in release ?
std::size_t total_size = get_aggregate_size(tup, idxs);
if (total_size >= buf_size)
throw std::invalid_argument("not enough buffer space");
}
std::apply([&](auto&... elems) {
(copy(elems, std::integral_constant<std::size_t, Is>{}), ...);
}, tup);
return MDB_val { ofs, buf };
}
#if 0 // bug in VS 16.4.0 Preview 2
template<bool check_size = true, typename T>
MDB_val to_val(const T& t, char *buf, int buf_size) {
if constexpr (std::is_convertible_v<T, std::string_view>) {
auto sv = std::string_view { t };
return { sv.size(), (void*)sv.data() };
} else if constexpr (contains_a_view<T>()) { // it tries to compile this branch even when if constexpr (false)
auto tup = to_tuple(t);
auto idxs = std::make_index_sequence<std::tuple_size_v<decltype(tup)>> {};
return to_val_from_aggregate<check_size>(tup, idxs, buf, buf_size);
} else {
return { sizeof(T), (void*)&t };
}
}
#else
template<bool check_size = true, typename T>
std::enable_if_t<
std::is_convertible_v<T, std::string_view>,
MDB_val> to_val(const T& t, char* buf, int buf_size) {
auto sv = std::string_view { t };
return { sv.size(), (void*)sv.data() };
}
template<bool check_size = true, typename T>
std::enable_if_t<
contains_a_view<T>(),
MDB_val> to_val(const T& t, char* buf, int buf_size) {
static_assert(!std::is_same_v<T, uint32_t>, "must not be key");
auto tup = to_tuple(t);
auto idxs = std::make_index_sequence<std::tuple_size_v<decltype(tup)>> {};
return to_val_from_aggregate<check_size>(tup, idxs, buf, buf_size);
}
template<bool check_size = true, typename T>
MDB_val to_val(const T& t, char* buf, int buf_size, std::enable_if_t<
!std::is_convertible_v<T, std::string_view> && !contains_a_view<T>(),
void> * v = nullptr) {
return { sizeof(T), (void*)&t };
}
#endif
template<typename T>
std::size_t get_val_size(const T& t) {
if constexpr (std::is_convertible_v<T, std::string_view>) {
auto sv = std::string_view { t };
return sv.size();
} else if constexpr (contains_a_view<T>()) {
auto tup = to_tuple(t);
auto idxs = std::make_index_sequence<std::tuple_size_v<decltype(tup)>> {};
return get_aggregate_size(tup, idxs);
} else {
return sizeof(T);
}
}
template<typename A, typename... Ts, std::size_t... Is>
decltype(auto) from_val_to_aggregate(MDB_val val, std::tuple<Ts...> tup, std::index_sequence<Is...>) {
auto get_view_size_without_last = [&](auto& elem, auto idx_c) -> std::size_t {
using T = std::decay_t<decltype(elem)>;
constexpr std::size_t idx = decltype(idx_c)::value;
if constexpr (is_view_v<T> && idx != sizeof...(Is) - 1)
return get_view_size_bytes(elem);
return 0;
};
auto data_ptr = (char*)val.mv_data;
auto copy_to = [&](auto & elem, auto idx_c) {
using T = std::decay_t<decltype(elem)>;
constexpr std::size_t idx = decltype(idx_c)::value;
if constexpr (is_view_v<T>) {
std::size_t size_bytes = 0;
if constexpr (idx == sizeof...(Is) - 1) {
size_bytes = val.mv_size - (data_ptr - (char*)val.mv_data);
} else {
stored_size_type szb; // todo: maybe use a variable length binary integer ?
memcpy(&szb, data_ptr, sizeof(stored_size_type)); // todo: std::bless
size_bytes = szb;
data_ptr += sizeof(stored_size_type);
}
if constexpr (is_string_view_v<T>) {
elem = T { data_ptr, size_bytes };
} else if constexpr (is_span_v<T>) {
using TT = typename T::element_type;
elem = T { reinterpret_cast<TT*>(data_ptr), (std::size_t)(size_bytes / sizeof(TT)) };
}
data_ptr += size_bytes;
} else {
memcpy(&elem, data_ptr, sizeof(T)); // todo: maybe use elem = std::bless<T>(data_ptr); in c++2x ?
data_ptr += sizeof(T);
}
};
return std::apply([&](auto&... elems) {
// check if the input data is valid, todo: disable this in release ?
constexpr std::size_t non_view_size = get_total_non_view_size_bytes(std::index_sequence<Is...>{}, type_identity<Ts> {} ...);
std::size_t size_without_first_view = non_view_size +
(get_view_size_without_last(elems, std::integral_constant<std::size_t, Is>{}) + ...);
if (size_without_first_view > val.mv_size)
throw std::runtime_error("size mismatch");
// copy the data from val into tup
(copy_to(elems, std::integral_constant<std::size_t, Is>{}), ...);
// then convert tup to the aggregate type
return A { elems... };
}, tup);
}
template<typename T>
decltype(auto) from_val(MDB_val val) {
if constexpr (std::is_same_v<T, std::string_view>) {
return std::string_view { (const char*)val.mv_data, val.mv_size };
} else if constexpr (contains_a_view<T>()) {
auto tup = to_tuple(T {});
auto idxs = std::make_index_sequence<std::tuple_size_v<decltype(tup)>> {};
return from_val_to_aggregate<T>(val, tup, idxs);
} else {
if (val.mv_size != sizeof(T)) {
throw std::runtime_error("size mismatch");
}
// todo: use std::bless/start_lifetime_as<T> ?
// return *reinterpret_cast<T*>(val.mv_data); // l-value reference to the data
// returning a reference to the data would requite it to be aligned according to T
// but e.g the key/value might contain arbitrary length strings
// so instead copy it to a properly aligned local variable
// todo: if T is large we may still want to allow returning by reference
// and letting the caller ensure proper alignment (e.g by padding strings)
T ret;
memcpy(&ret, val.mv_data, sizeof(T));
return ret;
}
}
} // namespace impl
} // namespace mdb