Debian Codegraph @

lmdbxx/lmdb++.h

raw symbols SCIP
1 /* This is free and unencumbered software released into the public domain. */
2
3 #ifndef LMDBXX_H
4 #define LMDBXX_H
5
6 /**
7 * <lmdb++.h> - C++17 wrapper for LMDB.
8 *
9 * @author Arto Bendiken <[email protected]>
10 * @maintainer Doug Hoyte <[email protected]>
11 * @see https://github.com/hoytech/lmdbxx
12 */
13
14 #ifndef __cplusplus
15 #error "<lmdb++.h> requires a C++ compiler"
16 #endif
17
18 #if __cplusplus < 201703L
19 #error "<lmdb++.h> requires a C++17 compiler (CXXFLAGS='-std=c++17')"
20 #endif
21
22 ////////////////////////////////////////////////////////////////////////////////
23
24 #include <lmdb.h> /* for MDB_*, mdb_*() */
25
26 #ifdef LMDBXX_DEBUG
27 #include <cassert> /* for assert() */
28 #endif
29 #include <cstddef> /* for std::size_t */
30 #include <cstdio> /* for std::snprintf() */
31 #include <cstring> /* for std::memcpy() */
32 #include <stdexcept> /* for std::runtime_error */
33 #include <string_view> /* for std::string_view */
34 #include <limits> /* for std::numeric_limits<> */
35 #include <memory> /* for std::addressof */
36
37 namespace lmdb {
38 using mode = mdb_mode_t;
39 }
40
41 ////////////////////////////////////////////////////////////////////////////////
42 /* Error Handling */
43
44 namespace lmdb {
45 class error;
46 class logic_error;
47 class fatal_error;
48 class runtime_error;
49 class key_exist_error;
50 class not_found_error;
51 class corrupted_error;
52 class panic_error;
53 class version_mismatch_error;
54 class map_full_error;
55 class bad_dbi_error;
56 }
57
58 /**
59 * Base class for LMDB exception conditions.
60 *
61 * @see http://symas.com/mdb/doc/group__errors.html
62 */
63 class lmdb::error : public std::runtime_error {
64 protected:
65 const int _code;
66
67 public:
68 /**
69 * Throws an error based on the given LMDB return code.
70 */
71 [[noreturn]] static inline void raise(const char* origin, int rc);
72
73 /**
74 * Constructor.
75 */
76 error(const char* const origin,
77 const int rc) noexcept
78 : runtime_error{origin},
79 _code{rc} {}
80
81 /**
82 * Returns the underlying LMDB error code.
83 */
84 int code() const noexcept {
85 return _code;
86 }
87
88 /**
89 * Returns the origin of the LMDB error.
90 */
91 const char* origin() const noexcept {
92 return runtime_error::what();
93 }
94
95 /**
96 * Returns the underlying LMDB error code.
97 */
98 virtual const char* what() const noexcept {
99 static thread_local char buffer[1024];
100 std::snprintf(buffer, sizeof(buffer),
101 "%s: %s", origin(), ::mdb_strerror(code()));
102 return buffer;
103 }
104 };
105
106 /**
107 * Base class for logic error conditions.
108 */
109 class lmdb::logic_error : public lmdb::error {
110 public:
111 using error::error;
112 };
113
114 /**
115 * Base class for fatal error conditions.
116 */
117 class lmdb::fatal_error : public lmdb::error {
118 public:
119 using error::error;
120 };
121
122 /**
123 * Base class for runtime error conditions.
124 */
125 class lmdb::runtime_error : public lmdb::error {
126 public:
127 using error::error;
128 };
129
130 /**
131 * Exception class for `MDB_KEYEXIST` errors.
132 *
133 * @see http://symas.com/mdb/doc/group__errors.html#ga05dc5bbcc7da81a7345bd8676e8e0e3b
134 */
135 class lmdb::key_exist_error final : public lmdb::runtime_error {
136 public:
137 using runtime_error::runtime_error;
138 };
139
140 /**
141 * Exception class for `MDB_NOTFOUND` errors.
142 *
143 * @see http://symas.com/mdb/doc/group__errors.html#gabeb52e4c4be21b329e31c4add1b71926
144 */
145 class lmdb::not_found_error final : public lmdb::runtime_error {
146 public:
147 using runtime_error::runtime_error;
148 };
149
150 /**
151 * Exception class for `MDB_CORRUPTED` errors.
152 *
153 * @see http://symas.com/mdb/doc/group__errors.html#gaf8148bf1b85f58e264e57194bafb03ef
154 */
155 class lmdb::corrupted_error final : public lmdb::fatal_error {
156 public:
157 using fatal_error::fatal_error;
158 };
159
160 /**
161 * Exception class for `MDB_PANIC` errors.
162 *
163 * @see http://symas.com/mdb/doc/group__errors.html#gae37b9aedcb3767faba3de8c1cf6d3473
164 */
165 class lmdb::panic_error final : public lmdb::fatal_error {
166 public:
167 using fatal_error::fatal_error;
168 };
169
170 /**
171 * Exception class for `MDB_VERSION_MISMATCH` errors.
172 *
173 * @see http://symas.com/mdb/doc/group__errors.html#ga909b2db047fa90fb0d37a78f86a6f99b
174 */
175 class lmdb::version_mismatch_error final : public lmdb::fatal_error {
176 public:
177 using fatal_error::fatal_error;
178 };
179
180 /**
181 * Exception class for `MDB_MAP_FULL` errors.
182 *
183 * @see http://symas.com/mdb/doc/group__errors.html#ga0a83370402a060c9175100d4bbfb9f25
184 */
185 class lmdb::map_full_error final : public lmdb::runtime_error {
186 public:
187 using runtime_error::runtime_error;
188 };
189
190 /**
191 * Exception class for `MDB_BAD_DBI` errors.
192 *
193 * @since 0.9.14 (2014/09/20)
194 * @see http://symas.com/mdb/doc/group__errors.html#gab4c82e050391b60a18a5df08d22a7083
195 */
196 class lmdb::bad_dbi_error final : public lmdb::runtime_error {
197 public:
198 using runtime_error::runtime_error;
199 };
200
201 inline void
202 lmdb::error::raise(const char* const origin,
203 const int rc) {
204 switch (rc) {
205 case MDB_KEYEXIST: throw key_exist_error{origin, rc};
206 case MDB_NOTFOUND: throw not_found_error{origin, rc};
207 case MDB_CORRUPTED: throw corrupted_error{origin, rc};
208 case MDB_PANIC: throw panic_error{origin, rc};
209 case MDB_VERSION_MISMATCH: throw version_mismatch_error{origin, rc};
210 case MDB_MAP_FULL: throw map_full_error{origin, rc};
211 #ifdef MDB_BAD_DBI
212 case MDB_BAD_DBI: throw bad_dbi_error{origin, rc};
213 #endif
214 default: throw lmdb::runtime_error{origin, rc};
215 }
216 }
217
218 ////////////////////////////////////////////////////////////////////////////////
219 /* Procedural Interface: Metadata */
220
221 namespace lmdb {
222 // TODO: mdb_version()
223 // TODO: mdb_strerror()
224 }
225
226 ////////////////////////////////////////////////////////////////////////////////
227 /* Procedural Interface: Environment */
228
229 namespace lmdb {
230 static inline void env_create(MDB_env** env);
231 static inline void env_open(MDB_env* env,
232 const char* path, unsigned int flags, mode mode);
233 #if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 14)
234 static inline void env_copy(MDB_env* env, const char* path, unsigned int flags);
235 static inline void env_copy_fd(MDB_env* env, mdb_filehandle_t fd, unsigned int flags);
236 #else
237 static inline void env_copy(MDB_env* env, const char* path);
238 static inline void env_copy_fd(MDB_env* env, mdb_filehandle_t fd);
239 #endif
240 static inline void env_stat(MDB_env* env, MDB_stat* stat);
241 static inline void env_info(MDB_env* env, MDB_envinfo* stat);
242 static inline void env_sync(MDB_env* env, bool force);
243 static inline void env_close(MDB_env* env) noexcept;
244 static inline void env_set_flags(MDB_env* env, unsigned int flags, bool onoff);
245 static inline void env_get_flags(MDB_env* env, unsigned int* flags);
246 static inline void env_get_path(MDB_env* env, const char** path);
247 static inline void env_get_fd(MDB_env* env, mdb_filehandle_t* fd);
248 static inline void env_set_mapsize(MDB_env* env, std::size_t size);
249 static inline void env_set_max_readers(MDB_env* env, unsigned int count);
250 static inline void env_get_max_readers(MDB_env* env, unsigned int* count);
251 static inline void env_set_max_dbs(MDB_env* env, MDB_dbi count);
252 static inline unsigned int env_get_max_keysize(MDB_env* env);
253 #if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 11)
254 static inline void env_set_userctx(MDB_env* env, void* ctx);
255 static inline void* env_get_userctx(MDB_env* env);
256 #endif
257 // TODO: mdb_env_set_assert()
258 // TODO: mdb_reader_list()
259 static inline void reader_check(MDB_env *env, int *dead);
260 }
261
262 /**
263 * @throws lmdb::error on failure
264 * @see http://symas.com/mdb/doc/group__mdb.html#gaad6be3d8dcd4ea01f8df436f41d158d4
265 */
266 static inline void
267 lmdb::env_create(MDB_env** env) {
268 const int rc = ::mdb_env_create(env);
269 if (rc != MDB_SUCCESS) {
270 error::raise("mdb_env_create", rc);
271 }
272 }
273
274 /**
275 * @throws lmdb::error on failure
276 * @see http://symas.com/mdb/doc/group__mdb.html#ga32a193c6bf4d7d5c5d579e71f22e9340
277 */
278 static inline void
279 lmdb::env_open(MDB_env* const env,
280 const char* const path,
281 const unsigned int flags,
282 const mode mode) {
283 const int rc = ::mdb_env_open(env, path, flags, mode);
284 if (rc != MDB_SUCCESS) {
285 error::raise("mdb_env_open", rc);
286 }
287 }
288
289 /**
290 * @throws lmdb::error on failure
291 * @see http://symas.com/mdb/doc/group__mdb.html#ga3bf50d7793b36aaddf6b481a44e24244
292 * @see http://symas.com/mdb/doc/group__mdb.html#ga5d51d6130325f7353db0955dbedbc378
293 */
294 static inline void
295 lmdb::env_copy(MDB_env* const env,
296 #if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 14)
297 const char* const path,
298 const unsigned int flags = 0) {
299 const int rc = ::mdb_env_copy2(env, path, flags);
300 #else
301 const char* const path) {
302 const int rc = ::mdb_env_copy(env, path);
303 #endif
304 if (rc != MDB_SUCCESS) {
305 error::raise("mdb_env_copy2", rc);
306 }
307 }
308
309 /**
310 * @throws lmdb::error on failure
311 * @see http://symas.com/mdb/doc/group__mdb.html#ga5040d0de1f14000fa01fc0b522ff1f86
312 * @see http://symas.com/mdb/doc/group__mdb.html#ga470b0bcc64ac417de5de5930f20b1a28
313 */
314 static inline void
315 lmdb::env_copy_fd(MDB_env* const env,
316 #if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 14)
317 const mdb_filehandle_t fd,
318 const unsigned int flags = 0) {
319 const int rc = ::mdb_env_copyfd2(env, fd, flags);
320 #else
321 const mdb_filehandle_t fd) {
322 const int rc = ::mdb_env_copyfd(env, fd);
323 #endif
324 if (rc != MDB_SUCCESS) {
325 error::raise("mdb_env_copyfd2", rc);
326 }
327 }
328
329 /**
330 * @throws lmdb::error on failure
331 * @see http://symas.com/mdb/doc/group__mdb.html#gaf881dca452050efbd434cd16e4bae255
332 */
333 static inline void
334 lmdb::env_stat(MDB_env* const env,
335 MDB_stat* const stat) {
336 const int rc = ::mdb_env_stat(env, stat);
337 if (rc != MDB_SUCCESS) {
338 error::raise("mdb_env_stat", rc);
339 }
340 }
341
342 /**
343 * @throws lmdb::error on failure
344 * @see http://symas.com/mdb/doc/group__mdb.html#ga18769362c7e7d6cf91889a028a5c5947
345 */
346 static inline void
347 lmdb::env_info(MDB_env* const env,
348 MDB_envinfo* const stat) {
349 const int rc = ::mdb_env_info(env, stat);
350 if (rc != MDB_SUCCESS) {
351 error::raise("mdb_env_info", rc);
352 }
353 }
354
355 /**
356 * @throws lmdb::error on failure
357 * @see http://symas.com/mdb/doc/group__mdb.html#ga85e61f05aa68b520cc6c3b981dba5037
358 */
359 static inline void
360 lmdb::env_sync(MDB_env* const env,
361 const bool force = true) {
362 const int rc = ::mdb_env_sync(env, force);
363 if (rc != MDB_SUCCESS) {
364 error::raise("mdb_env_sync", rc);
365 }
366 }
367
368 /**
369 * @see http://symas.com/mdb/doc/group__mdb.html#ga4366c43ada8874588b6a62fbda2d1e95
370 */
371 static inline void
372 lmdb::env_close(MDB_env* const env) noexcept {
373 ::mdb_env_close(env);
374 }
375
376 /**
377 * @throws lmdb::error on failure
378 * @see http://symas.com/mdb/doc/group__mdb.html#ga83f66cf02bfd42119451e9468dc58445
379 */
380 static inline void
381 lmdb::env_set_flags(MDB_env* const env,
382 const unsigned int flags,
383 const bool onoff = true) {
384 const int rc = ::mdb_env_set_flags(env, flags, onoff ? 1 : 0);
385 if (rc != MDB_SUCCESS) {
386 error::raise("mdb_env_set_flags", rc);
387 }
388 }
389
390 /**
391 * @throws lmdb::error on failure
392 * @see http://symas.com/mdb/doc/group__mdb.html#ga2733aefc6f50beb49dd0c6eb19b067d9
393 */
394 static inline void
395 lmdb::env_get_flags(MDB_env* const env,
396 unsigned int* const flags) {
397 const int rc = ::mdb_env_get_flags(env, flags);
398 if (rc != MDB_SUCCESS) {
399 error::raise("mdb_env_get_flags", rc);
400 }
401 }
402
403 /**
404 * @throws lmdb::error on failure
405 * @see http://symas.com/mdb/doc/group__mdb.html#gac699fdd8c4f8013577cb933fb6a757fe
406 */
407 static inline void
408 lmdb::env_get_path(MDB_env* const env,
409 const char** path) {
410 const int rc = ::mdb_env_get_path(env, path);
411 if (rc != MDB_SUCCESS) {
412 error::raise("mdb_env_get_path", rc);
413 }
414 }
415
416 /**
417 * @throws lmdb::error on failure
418 * @see http://symas.com/mdb/doc/group__mdb.html#gaf1570e7c0e5a5d860fef1032cec7d5f2
419 */
420 static inline void
421 lmdb::env_get_fd(MDB_env* const env,
422 mdb_filehandle_t* const fd) {
423 const int rc = ::mdb_env_get_fd(env, fd);
424 if (rc != MDB_SUCCESS) {
425 error::raise("mdb_env_get_fd", rc);
426 }
427 }
428
429 /**
430 * @throws lmdb::error on failure
431 * @see http://symas.com/mdb/doc/group__mdb.html#gaa2506ec8dab3d969b0e609cd82e619e5
432 */
433 static inline void
434 lmdb::env_set_mapsize(MDB_env* const env,
435 const std::size_t size) {
436 const int rc = ::mdb_env_set_mapsize(env, size);
437 if (rc != MDB_SUCCESS) {
438 error::raise("mdb_env_set_mapsize", rc);
439 }
440 }
441
442 /**
443 * @throws lmdb::error on failure
444 * @see http://symas.com/mdb/doc/group__mdb.html#gae687966c24b790630be2a41573fe40e2
445 */
446 static inline void
447 lmdb::env_set_max_readers(MDB_env* const env,
448 const unsigned int count) {
449 const int rc = ::mdb_env_set_maxreaders(env, count);
450 if (rc != MDB_SUCCESS) {
451 error::raise("mdb_env_set_maxreaders", rc);
452 }
453 }
454
455 /**
456 * @throws lmdb::error on failure
457 * @see http://symas.com/mdb/doc/group__mdb.html#ga70e143cf11760d869f754c9c9956e6cc
458 */
459 static inline void
460 lmdb::env_get_max_readers(MDB_env* const env,
461 unsigned int* const count) {
462 const int rc = ::mdb_env_get_maxreaders(env, count);
463 if (rc != MDB_SUCCESS) {
464 error::raise("mdb_env_get_maxreaders", rc);
465 }
466 }
467
468 /**
469 * @throws lmdb::error on failure
470 * @see http://symas.com/mdb/doc/group__mdb.html#gaa2fc2f1f37cb1115e733b62cab2fcdbc
471 */
472 static inline void
473 lmdb::env_set_max_dbs(MDB_env* const env,
474 const MDB_dbi count) {
475 const int rc = ::mdb_env_set_maxdbs(env, count);
476 if (rc != MDB_SUCCESS) {
477 error::raise("mdb_env_set_maxdbs", rc);
478 }
479 }
480
481 /**
482 * @see http://symas.com/mdb/doc/group__mdb.html#gaaf0be004f33828bf2fb09d77eb3cef94
483 */
484 static inline unsigned int
485 lmdb::env_get_max_keysize(MDB_env* const env) {
486 const int rc = ::mdb_env_get_maxkeysize(env);
487 #ifdef LMDBXX_DEBUG
488 assert(rc >= 0);
489 #endif
490 return static_cast<unsigned int>(rc);
491 }
492
493 #if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 11)
494 /**
495 * @throws lmdb::error on failure
496 * @since 0.9.11 (2014/01/15)
497 * @see http://symas.com/mdb/doc/group__mdb.html#gaf2fe09eb9c96eeb915a76bf713eecc46
498 */
499 static inline void
500 lmdb::env_set_userctx(MDB_env* const env,
501 void* const ctx) {
502 const int rc = ::mdb_env_set_userctx(env, ctx);
503 if (rc != MDB_SUCCESS) {
504 error::raise("mdb_env_set_userctx", rc);
505 }
506 }
507 #endif
508
509 #if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 11)
510 /**
511 * @since 0.9.11 (2014/01/15)
512 * @see http://symas.com/mdb/doc/group__mdb.html#ga45df6a4fb150cda2316b5ae224ba52f1
513 */
514 static inline void*
515 lmdb::env_get_userctx(MDB_env* const env) {
516 return ::mdb_env_get_userctx(env);
517 }
518 #endif
519
520 static inline void
521 lmdb::reader_check(MDB_env *env, int *dead) {
522 const int rc = ::mdb_reader_check(env, dead);
523 if (rc != MDB_SUCCESS) {
524 error::raise("reader_check", rc);
525 }
526 }
527
528 ////////////////////////////////////////////////////////////////////////////////
529 /* Procedural Interface: Transactions */
530
531 namespace lmdb {
532 static inline void txn_begin(
533 MDB_env* env, MDB_txn* parent, unsigned int flags, MDB_txn** txn);
534 static inline MDB_env* txn_env(MDB_txn* txn) noexcept;
535 #ifdef LMDBXX_TXN_ID
536 static inline std::size_t txn_id(MDB_txn* txn) noexcept;
537 #endif
538 static inline void txn_commit(MDB_txn* txn);
539 static inline void txn_abort(MDB_txn* txn) noexcept;
540 static inline void txn_reset(MDB_txn* txn) noexcept;
541 static inline void txn_renew(MDB_txn* txn);
542 }
543
544 /**
545 * @throws lmdb::error on failure
546 * @see http://symas.com/mdb/doc/group__mdb.html#gad7ea55da06b77513609efebd44b26920
547 */
548 static inline void
549 lmdb::txn_begin(MDB_env* const env,
550 MDB_txn* const parent,
551 const unsigned int flags,
552 MDB_txn** txn) {
553 const int rc = ::mdb_txn_begin(env, parent, flags, txn);
554 if (rc != MDB_SUCCESS) {
555 error::raise("mdb_txn_begin", rc);
556 }
557 }
558
559 /**
560 * @see http://symas.com/mdb/doc/group__mdb.html#gaeb17735b8aaa2938a78a45cab85c06a0
561 */
562 static inline MDB_env*
563 lmdb::txn_env(MDB_txn* const txn) noexcept {
564 return ::mdb_txn_env(txn);
565 }
566
567 #ifdef LMDBXX_TXN_ID
568 /**
569 * @note Only available in HEAD, not yet in any 0.9.x release (as of 0.9.16).
570 */
571 static inline std::size_t
572 lmdb::txn_id(MDB_txn* const txn) noexcept {
573 return ::mdb_txn_id(txn);
574 }
575 #endif
576
577 /**
578 * @throws lmdb::error on failure
579 * @see http://symas.com/mdb/doc/group__mdb.html#ga846fbd6f46105617ac9f4d76476f6597
580 */
581 static inline void
582 lmdb::txn_commit(MDB_txn* const txn) {
583 const int rc = ::mdb_txn_commit(txn);
584 if (rc != MDB_SUCCESS) {
585 error::raise("mdb_txn_commit", rc);
586 }
587 }
588
589 /**
590 * @see http://symas.com/mdb/doc/group__mdb.html#ga73a5938ae4c3239ee11efa07eb22b882
591 */
592 static inline void
593 lmdb::txn_abort(MDB_txn* const txn) noexcept {
594 ::mdb_txn_abort(txn);
595 }
596
597 /**
598 * @see http://symas.com/mdb/doc/group__mdb.html#ga02b06706f8a66249769503c4e88c56cd
599 */
600 static inline void
601 lmdb::txn_reset(MDB_txn* const txn) noexcept {
602 ::mdb_txn_reset(txn);
603 }
604
605 /**
606 * @throws lmdb::error on failure
607 * @see http://symas.com/mdb/doc/group__mdb.html#ga6c6f917959517ede1c504cf7c720ce6d
608 */
609 static inline void
610 lmdb::txn_renew(MDB_txn* const txn) {
611 const int rc = ::mdb_txn_renew(txn);
612 if (rc != MDB_SUCCESS) {
613 error::raise("mdb_txn_renew", rc);
614 }
615 }
616
617 ////////////////////////////////////////////////////////////////////////////////
618 /* Procedural Interface: Databases */
619
620 namespace lmdb {
621 static inline void dbi_open(
622 MDB_txn* txn, const char* name, unsigned int flags, MDB_dbi* dbi);
623 static inline void dbi_stat(MDB_txn* txn, MDB_dbi dbi, MDB_stat* stat);
624 static inline void dbi_flags(MDB_txn* txn, MDB_dbi dbi, unsigned int* flags);
625 static inline void dbi_close(MDB_env* env, MDB_dbi dbi) noexcept;
626 static inline void dbi_drop(MDB_txn* txn, MDB_dbi dbi, bool del);
627 static inline void dbi_set_compare(MDB_txn* txn, MDB_dbi dbi, MDB_cmp_func* cmp);
628 static inline void dbi_set_dupsort(MDB_txn* txn, MDB_dbi dbi, MDB_cmp_func* cmp);
629 static inline void dbi_set_relfunc(MDB_txn* txn, MDB_dbi dbi, MDB_rel_func* rel);
630 static inline void dbi_set_relctx(MDB_txn* txn, MDB_dbi dbi, void* ctx);
631 static inline bool dbi_get(MDB_txn* txn, MDB_dbi dbi, const MDB_val* key, MDB_val* data);
632 static inline bool dbi_put(MDB_txn* txn, MDB_dbi dbi, const MDB_val* key, MDB_val* data, unsigned int flags);
633 static inline bool dbi_del(MDB_txn* txn, MDB_dbi dbi, const MDB_val* key, const MDB_val* data);
634 // TODO: mdb_cmp()
635 // TODO: mdb_dcmp()
636 }
637
638 /**
639 * @throws lmdb::error on failure
640 * @see http://symas.com/mdb/doc/group__mdb.html#gac08cad5b096925642ca359a6d6f0562a
641 */
642 static inline void
643 lmdb::dbi_open(MDB_txn* const txn,
644 const char* const name,
645 const unsigned int flags,
646 MDB_dbi* const dbi) {
647 const int rc = ::mdb_dbi_open(txn, name, flags, dbi);
648 if (rc != MDB_SUCCESS) {
649 error::raise("mdb_dbi_open", rc);
650 }
651 }
652
653 /**
654 * @throws lmdb::error on failure
655 * @see http://symas.com/mdb/doc/group__mdb.html#gae6c1069febe94299769dbdd032fadef6
656 */
657 static inline void
658 lmdb::dbi_stat(MDB_txn* const txn,
659 const MDB_dbi dbi,
660 MDB_stat* const result) {
661 const int rc = ::mdb_stat(txn, dbi, result);
662 if (rc != MDB_SUCCESS) {
663 error::raise("mdb_stat", rc);
664 }
665 }
666
667 /**
668 * @throws lmdb::error on failure
669 * @see http://symas.com/mdb/doc/group__mdb.html#ga95ba4cb721035478a8705e57b91ae4d4
670 */
671 static inline void
672 lmdb::dbi_flags(MDB_txn* const txn,
673 const MDB_dbi dbi,
674 unsigned int* const flags) {
675 const int rc = ::mdb_dbi_flags(txn, dbi, flags);
676 if (rc != MDB_SUCCESS) {
677 error::raise("mdb_dbi_flags", rc);
678 }
679 }
680
681 /**
682 * @see http://symas.com/mdb/doc/group__mdb.html#ga52dd98d0c542378370cd6b712ff961b5
683 */
684 static inline void
685 lmdb::dbi_close(MDB_env* const env,
686 const MDB_dbi dbi) noexcept {
687 ::mdb_dbi_close(env, dbi);
688 }
689
690 /**
691 * @see http://symas.com/mdb/doc/group__mdb.html#gab966fab3840fc54a6571dfb32b00f2db
692 */
693 static inline void
694 lmdb::dbi_drop(MDB_txn* const txn,
695 const MDB_dbi dbi,
696 const bool del = false) {
697 const int rc = ::mdb_drop(txn, dbi, del ? 1 : 0);
698 if (rc != MDB_SUCCESS) {
699 error::raise("mdb_drop", rc);
700 }
701 }
702
703 /**
704 * @throws lmdb::error on failure
705 * @see http://symas.com/mdb/doc/group__mdb.html#ga68e47ffcf72eceec553c72b1784ee0fe
706 */
707 static inline void
708 lmdb::dbi_set_compare(MDB_txn* const txn,
709 const MDB_dbi dbi,
710 MDB_cmp_func* const cmp = nullptr) {
711 const int rc = ::mdb_set_compare(txn, dbi, cmp);
712 if (rc != MDB_SUCCESS) {
713 error::raise("mdb_set_compare", rc);
714 }
715 }
716
717 /**
718 * @throws lmdb::error on failure
719 * @see http://symas.com/mdb/doc/group__mdb.html#gacef4ec3dab0bbd9bc978b73c19c879ae
720 */
721 static inline void
722 lmdb::dbi_set_dupsort(MDB_txn* const txn,
723 const MDB_dbi dbi,
724 MDB_cmp_func* const cmp = nullptr) {
725 const int rc = ::mdb_set_dupsort(txn, dbi, cmp);
726 if (rc != MDB_SUCCESS) {
727 error::raise("mdb_set_dupsort", rc);
728 }
729 }
730
731 /**
732 * @throws lmdb::error on failure
733 * @see http://symas.com/mdb/doc/group__mdb.html#ga697d82c7afe79f142207ad5adcdebfeb
734 */
735 static inline void
736 lmdb::dbi_set_relfunc(MDB_txn* const txn,
737 const MDB_dbi dbi,
738 MDB_rel_func* const rel) {
739 const int rc = ::mdb_set_relfunc(txn, dbi, rel);
740 if (rc != MDB_SUCCESS) {
741 error::raise("mdb_set_relfunc", rc);
742 }
743 }
744
745 /**
746 * @throws lmdb::error on failure
747 * @see http://symas.com/mdb/doc/group__mdb.html#ga7c34246308cee01724a1839a8f5cc594
748 */
749 static inline void
750 lmdb::dbi_set_relctx(MDB_txn* const txn,
751 const MDB_dbi dbi,
752 void* const ctx) {
753 const int rc = ::mdb_set_relctx(txn, dbi, ctx);
754 if (rc != MDB_SUCCESS) {
755 error::raise("mdb_set_relctx", rc);
756 }
757 }
758
759 /**
760 * @retval true if the key/value pair was retrieved
761 * @retval false if the key wasn't found
762 * @see http://symas.com/mdb/doc/group__mdb.html#ga8bf10cd91d3f3a83a34d04ce6b07992d
763 */
764 static inline bool
765 lmdb::dbi_get(MDB_txn* const txn,
766 const MDB_dbi dbi,
767 const MDB_val* const key,
768 MDB_val* const data) {
769 const int rc = ::mdb_get(txn, dbi, const_cast<MDB_val*>(key), data);
770 if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND) {
771 error::raise("mdb_get", rc);
772 }
773 return (rc == MDB_SUCCESS);
774 }
775
776 /**
777 * @retval true if the key/value pair was inserted
778 * @retval false if the key already existed
779 * @see http://symas.com/mdb/doc/group__mdb.html#ga4fa8573d9236d54687c61827ebf8cac0
780 */
781 static inline bool
782 lmdb::dbi_put(MDB_txn* const txn,
783 const MDB_dbi dbi,
784 const MDB_val* const key,
785 MDB_val* const data,
786 const unsigned int flags = 0) {
787 const int rc = ::mdb_put(txn, dbi, const_cast<MDB_val*>(key), data, flags);
788 if (rc != MDB_SUCCESS && rc != MDB_KEYEXIST) {
789 error::raise("mdb_put", rc);
790 }
791 return (rc == MDB_SUCCESS);
792 }
793
794 /**
795 * @retval true if the key/value pair was removed
796 * @retval false if the key wasn't found
797 * @see http://symas.com/mdb/doc/group__mdb.html#gab8182f9360ea69ac0afd4a4eaab1ddb0
798 */
799 static inline bool
800 lmdb::dbi_del(MDB_txn* const txn,
801 const MDB_dbi dbi,
802 const MDB_val* const key,
803 const MDB_val* const data = nullptr) {
804 const int rc = ::mdb_del(txn, dbi, const_cast<MDB_val*>(key), const_cast<MDB_val*>(data));
805 if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND) {
806 error::raise("mdb_del", rc);
807 }
808 return (rc == MDB_SUCCESS);
809 }
810
811 ////////////////////////////////////////////////////////////////////////////////
812 /* Procedural Interface: Cursors */
813
814 namespace lmdb {
815 static inline void cursor_open(MDB_txn* txn, MDB_dbi dbi, MDB_cursor** cursor);
816 static inline void cursor_close(MDB_cursor* cursor) noexcept;
817 static inline void cursor_renew(MDB_txn* txn, MDB_cursor* cursor);
818 static inline MDB_txn* cursor_txn(MDB_cursor* cursor) noexcept;
819 static inline MDB_dbi cursor_dbi(MDB_cursor* cursor) noexcept;
820 static inline bool cursor_get(MDB_cursor* cursor, MDB_val* key, MDB_val* data, MDB_cursor_op op);
821 static inline bool cursor_put(MDB_cursor* cursor, MDB_val* key, MDB_val* data, unsigned int flags);
822 static inline void cursor_del(MDB_cursor* cursor, unsigned int flags);
823 static inline void cursor_count(MDB_cursor* cursor, std::size_t& count);
824 }
825
826 /**
827 * @throws lmdb::error on failure
828 * @see http://symas.com/mdb/doc/group__mdb.html#ga9ff5d7bd42557fd5ee235dc1d62613aa
829 */
830 static inline void
831 lmdb::cursor_open(MDB_txn* const txn,
832 const MDB_dbi dbi,
833 MDB_cursor** const cursor) {
834 const int rc = ::mdb_cursor_open(txn, dbi, cursor);
835 if (rc != MDB_SUCCESS) {
836 error::raise("mdb_cursor_open", rc);
837 }
838 }
839
840 /**
841 * @see http://symas.com/mdb/doc/group__mdb.html#gad685f5d73c052715c7bd859cc4c05188
842 */
843 static inline void
844 lmdb::cursor_close(MDB_cursor* const cursor) noexcept {
845 ::mdb_cursor_close(cursor);
846 }
847
848 /**
849 * @throws lmdb::error on failure
850 * @see http://symas.com/mdb/doc/group__mdb.html#gac8b57befb68793070c85ea813df481af
851 */
852 static inline void
853 lmdb::cursor_renew(MDB_txn* const txn,
854 MDB_cursor* const cursor) {
855 const int rc = ::mdb_cursor_renew(txn, cursor);
856 if (rc != MDB_SUCCESS) {
857 error::raise("mdb_cursor_renew", rc);
858 }
859 }
860
861 /**
862 * @see http://symas.com/mdb/doc/group__mdb.html#ga7bf0d458f7f36b5232fcb368ebda79e0
863 */
864 static inline MDB_txn*
865 lmdb::cursor_txn(MDB_cursor* const cursor) noexcept {
866 return ::mdb_cursor_txn(cursor);
867 }
868
869 /**
870 * @see http://symas.com/mdb/doc/group__mdb.html#ga2f7092cf70ee816fb3d2c3267a732372
871 */
872 static inline MDB_dbi
873 lmdb::cursor_dbi(MDB_cursor* const cursor) noexcept {
874 return ::mdb_cursor_dbi(cursor);
875 }
876
877 /**
878 * @throws lmdb::error on failure
879 * @see http://symas.com/mdb/doc/group__mdb.html#ga48df35fb102536b32dfbb801a47b4cb0
880 */
881 static inline bool
882 lmdb::cursor_get(MDB_cursor* const cursor,
883 MDB_val* const key,
884 MDB_val* const data,
885 const MDB_cursor_op op) {
886 const int rc = ::mdb_cursor_get(cursor, key, data, op);
887 if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND) {
888 error::raise("mdb_cursor_get", rc);
889 }
890 return (rc == MDB_SUCCESS);
891 }
892
893 /**
894 * @throws lmdb::error on failure
895 * @see http://symas.com/mdb/doc/group__mdb.html#ga1f83ccb40011837ff37cc32be01ad91e
896 */
897 static inline bool
898 lmdb::cursor_put(MDB_cursor* const cursor,
899 MDB_val* const key,
900 MDB_val* const data,
901 const unsigned int flags = 0) {
902 const int rc = ::mdb_cursor_put(cursor, key, data, flags);
903 if (rc != MDB_SUCCESS && rc != MDB_KEYEXIST) {
904 error::raise("mdb_cursor_put", rc);
905 }
906 return (rc == MDB_SUCCESS);
907 }
908
909 /**
910 * @throws lmdb::error on failure
911 * @see http://symas.com/mdb/doc/group__mdb.html#ga26a52d3efcfd72e5bf6bd6960bf75f95
912 */
913 static inline void
914 lmdb::cursor_del(MDB_cursor* const cursor,
915 const unsigned int flags = 0) {
916 const int rc = ::mdb_cursor_del(cursor, flags);
917 if (rc != MDB_SUCCESS) {
918 error::raise("mdb_cursor_del", rc);
919 }
920 }
921
922 /**
923 * @throws lmdb::error on failure
924 * @see http://symas.com/mdb/doc/group__mdb.html#ga4041fd1e1862c6b7d5f10590b86ffbe2
925 */
926 static inline void
927 lmdb::cursor_count(MDB_cursor* const cursor,
928 std::size_t& count) {
929 const int rc = ::mdb_cursor_count(cursor, &count);
930 if (rc != MDB_SUCCESS) {
931 error::raise("mdb_cursor_count", rc);
932 }
933 }
934
935 ////////////////////////////////////////////////////////////////////////////////
936 /* Resource Interface: Environment */
937
938 namespace lmdb {
939 class env;
940 }
941
942 /**
943 * Resource class for `MDB_env*` handles.
944 *
945 * @note Instances of this class are movable, but not copyable.
946 * @see http://symas.com/mdb/doc/group__internal.html#structMDB__env
947 */
948 class lmdb::env {
949 protected:
950 MDB_env* _handle{nullptr};
951
952 public:
953 static constexpr unsigned int default_flags = 0;
954 static constexpr mode default_mode = 0644; /* -rw-r--r-- */
955
956 /**
957 * Creates a new LMDB environment.
958 *
959 * @param flags
960 * @throws lmdb::error on failure
961 */
962 static env create(const unsigned int flags = default_flags) {
963 MDB_env* handle{nullptr};
964 lmdb::env_create(&handle);
965 #ifdef LMDBXX_DEBUG
966 assert(handle != nullptr);
967 #endif
968 if (flags) {
969 try {
970 lmdb::env_set_flags(handle, flags);
971 }
972 catch (const lmdb::error&) {
973 lmdb::env_close(handle);
974 throw;
975 }
976 }
977 return env{handle};
978 }
979
980 /**
981 * Constructor.
982 *
983 * @param handle a valid `MDB_env*` handle
984 */
985 env(MDB_env* const handle) noexcept
986 : _handle{handle} {}
987
988 /**
989 * Move constructor.
990 */
991 env(env&& other) noexcept {
992 std::swap(_handle, other._handle);
993 }
994
995 /**
996 * Move assignment operator.
997 */
998 env& operator=(env&& other) noexcept {
999 if (this != &other) {
1000 std::swap(_handle, other._handle);
1001 }
1002 return *this;
1003 }
1004
1005 /**
1006 * Destructor.
1007 */
1008 ~env() noexcept {
1009 try { close(); } catch (...) {}
1010 }
1011
1012 /**
1013 * Returns the underlying `MDB_env*` handle.
1014 */
1015 operator MDB_env*() const noexcept {
1016 return _handle;
1017 }
1018
1019 /**
1020 * Returns the underlying `MDB_env*` handle.
1021 */
1022 MDB_env* handle() const noexcept {
1023 return _handle;
1024 }
1025
1026 /**
1027 * Flushes data buffers to disk.
1028 *
1029 * @param force
1030 * @throws lmdb::error on failure
1031 */
1032 void sync(const bool force = true) {
1033 lmdb::env_sync(handle(), force);
1034 }
1035
1036 /**
1037 * Closes this environment, releasing the memory map.
1038 *
1039 * @note this method is idempotent
1040 * @post `handle() == nullptr`
1041 */
1042 void close() noexcept {
1043 if (handle()) {
1044 lmdb::env_close(handle());
1045 _handle = nullptr;
1046 }
1047 }
1048
1049 int reader_check() {
1050 int dead;
1051 lmdb::reader_check(handle(), &dead);
1052 return dead;
1053 }
1054
1055 /**
1056 * Opens this environment.
1057 *
1058 * @param path
1059 * @param flags
1060 * @param mode
1061 * @throws lmdb::error on failure
1062 */
1063 env& open(const char* const path,
1064 const unsigned int flags = default_flags,
1065 const mode mode = default_mode) {
1066 lmdb::env_open(handle(), path, flags, mode);
1067 return *this;
1068 }
1069
1070 /**
1071 * @param flags
1072 * @param onoff
1073 * @throws lmdb::error on failure
1074 */
1075 env& set_flags(const unsigned int flags,
1076 const bool onoff = true) {
1077 lmdb::env_set_flags(handle(), flags, onoff);
1078 return *this;
1079 }
1080
1081 /**
1082 * @param size
1083 * @throws lmdb::error on failure
1084 */
1085 env& set_mapsize(const std::size_t size) {
1086 lmdb::env_set_mapsize(handle(), size);
1087 return *this;
1088 }
1089
1090 /**
1091 * @param count
1092 * @throws lmdb::error on failure
1093 */
1094 env& set_max_readers(const unsigned int count) {
1095 lmdb::env_set_max_readers(handle(), count);
1096 return *this;
1097 }
1098
1099 /**
1100 * @param count
1101 * @throws lmdb::error on failure
1102 */
1103 env& set_max_dbs(const MDB_dbi count) {
1104 lmdb::env_set_max_dbs(handle(), count);
1105 return *this;
1106 }
1107 };
1108
1109 ////////////////////////////////////////////////////////////////////////////////
1110 /* Resource Interface: Transactions */
1111
1112 namespace lmdb {
1113 class txn;
1114 }
1115
1116 /**
1117 * Resource class for `MDB_txn*` handles.
1118 *
1119 * @note Instances of this class are movable, but not copyable.
1120 * @see http://symas.com/mdb/doc/group__internal.html#structMDB__txn
1121 */
1122 class lmdb::txn {
1123 protected:
1124 MDB_txn* _handle{nullptr};
1125
1126 public:
1127 static constexpr unsigned int default_flags = 0;
1128
1129 /**
1130 * Creates a new LMDB transaction.
1131 *
1132 * @param env the environment handle
1133 * @param parent
1134 * @param flags
1135 * @throws lmdb::error on failure
1136 */
1137 static txn begin(MDB_env* const env,
1138 MDB_txn* const parent = nullptr,
1139 const unsigned int flags = default_flags) {
1140 MDB_txn* handle{nullptr};
1141 lmdb::txn_begin(env, parent, flags, &handle);
1142 #ifdef LMDBXX_DEBUG
1143 assert(handle != nullptr);
1144 #endif
1145 return txn{handle};
1146 }
1147
1148 /**
1149 * Constructor.
1150 *
1151 * @param handle a valid `MDB_txn*` handle
1152 */
1153 txn(MDB_txn* const handle) noexcept
1154 : _handle{handle} {}
1155
1156 /**
1157 * Move constructor.
1158 */
1159 txn(txn&& other) noexcept {
1160 std::swap(_handle, other._handle);
1161 }
1162
1163 /**
1164 * Move assignment operator.
1165 */
1166 txn& operator=(txn&& other) noexcept {
1167 if (this != &other) {
1168 std::swap(_handle, other._handle);
1169 }
1170 return *this;
1171 }
1172
1173 /**
1174 * Destructor.
1175 */
1176 ~txn() noexcept {
1177 if (_handle) {
1178 try { abort(); } catch (...) {}
1179 _handle = nullptr;
1180 }
1181 }
1182
1183 /**
1184 * Returns the underlying `MDB_txn*` handle.
1185 */
1186 operator MDB_txn*() const noexcept {
1187 return _handle;
1188 }
1189
1190 /**
1191 * Returns the underlying `MDB_txn*` handle.
1192 */
1193 MDB_txn* handle() const noexcept {
1194 return _handle;
1195 }
1196
1197 /**
1198 * Returns the transaction's `MDB_env*` handle.
1199 */
1200 MDB_env* env() const noexcept {
1201 return lmdb::txn_env(handle());
1202 }
1203
1204 /**
1205 * Commits this transaction.
1206 *
1207 * @throws lmdb::error on failure
1208 * @post `handle() == nullptr`
1209 */
1210 void commit() {
1211 auto h = _handle;
1212 _handle = nullptr;
1213 lmdb::txn_commit(h);
1214 }
1215
1216 /**
1217 * Aborts this transaction.
1218 *
1219 * @post `handle() == nullptr`
1220 */
1221 void abort() noexcept {
1222 auto h = _handle;
1223 _handle = nullptr;
1224 lmdb::txn_abort(h);
1225 }
1226
1227 /**
1228 * Resets this read-only transaction.
1229 */
1230 void reset() noexcept {
1231 lmdb::txn_reset(_handle);
1232 }
1233
1234 /**
1235 * Renews this read-only transaction.
1236 *
1237 * @throws lmdb::error on failure
1238 */
1239 void renew() {
1240 lmdb::txn_renew(_handle);
1241 }
1242 };
1243
1244 ////////////////////////////////////////////////////////////////////////////////
1245 /* Resource Interface: Databases */
1246
1247 namespace lmdb {
1248 class dbi;
1249 }
1250
1251 /**
1252 * Resource class for `MDB_dbi` handles.
1253 *
1254 * @see http://symas.com/mdb/doc/group__mdb.html#gadbe68a06c448dfb62da16443d251a78b
1255 */
1256 class lmdb::dbi {
1257 protected:
1258 MDB_dbi _handle{(std::numeric_limits<MDB_dbi>::max)()};
1259
1260 public:
1261 static constexpr unsigned int default_flags = 0;
1262 static constexpr unsigned int default_put_flags = 0;
1263
1264 /**
1265 * Opens a database handle.
1266 *
1267 * @param txn the transaction handle
1268 * @param name
1269 * @param flags
1270 * @throws lmdb::error on failure
1271 */
1272 static dbi
1273 open(MDB_txn* const txn,
1274 const char* const name = nullptr,
1275 const unsigned int flags = default_flags) {
1276 MDB_dbi handle{};
1277 lmdb::dbi_open(txn, name, flags, &handle);
1278 return dbi{handle};
1279 }
1280
1281 /**
1282 * Constructor.
1283 *
1284 * @note Creates an uninitialized dbi instance. You must move or move-assign a valid dbi instance onto it before you can use it.
1285 */
1286 dbi() noexcept
1287 : _handle{(std::numeric_limits<MDB_dbi>::max)()} {}
1288
1289 /**
1290 * Constructor.
1291 *
1292 * @param handle a valid `MDB_dbi` handle
1293 */
1294 dbi(const MDB_dbi handle) noexcept
1295 : _handle{handle} {}
1296
1297 /**
1298 * Destructor.
1299 */
1300 ~dbi() noexcept {
1301 if (_handle) {
1302 /* No need to call close() here. */
1303 }
1304 }
1305
1306 /**
1307 * Returns the underlying `MDB_dbi` handle.
1308 */
1309 operator MDB_dbi() const noexcept {
1310 return _handle;
1311 }
1312
1313 /**
1314 * Returns the underlying `MDB_dbi` handle.
1315 */
1316 MDB_dbi handle() const noexcept {
1317 return _handle;
1318 }
1319
1320 /**
1321 * Returns statistics for this database.
1322 *
1323 * @param txn a transaction handle
1324 * @throws lmdb::error on failure
1325 */
1326 MDB_stat stat(MDB_txn* const txn) const {
1327 MDB_stat result;
1328 lmdb::dbi_stat(txn, handle(), &result);
1329 return result;
1330 }
1331
1332 /**
1333 * Retrieves the flags for this database handle.
1334 *
1335 * @param txn a transaction handle
1336 * @throws lmdb::error on failure
1337 */
1338 unsigned int flags(MDB_txn* const txn) const {
1339 unsigned int result{};
1340 lmdb::dbi_flags(txn, handle(), &result);
1341 return result;
1342 }
1343
1344 /**
1345 * Returns the number of records in this database.
1346 *
1347 * @param txn a transaction handle
1348 * @throws lmdb::error on failure
1349 */
1350 std::size_t size(MDB_txn* const txn) const {
1351 return stat(txn).ms_entries;
1352 }
1353
1354 /**
1355 * @param txn a transaction handle
1356 * @param del
1357 * @throws lmdb::error on failure
1358 */
1359 void drop(MDB_txn* const txn,
1360 const bool del = false) {
1361 lmdb::dbi_drop(txn, handle(), del);
1362 }
1363
1364 /**
1365 * Sets a custom key comparison function for this database.
1366 *
1367 * @param txn a transaction handle
1368 * @param cmp the comparison function
1369 * @throws lmdb::error on failure
1370 */
1371 dbi& set_compare(MDB_txn* const txn,
1372 MDB_cmp_func* const cmp = nullptr) {
1373 lmdb::dbi_set_compare(txn, handle(), cmp);
1374 return *this;
1375 }
1376
1377 /**
1378 * Retrieves a key/value pair from this database.
1379 *
1380 * @param txn a transaction handle
1381 * @param key
1382 * @param data
1383 * @throws lmdb::error on failure
1384 */
1385
1386 bool get(MDB_txn* const txn,
1387 const std::string_view key,
1388 std::string_view& data) {
1389 const MDB_val keyV{key.size(), const_cast<char*>(key.data())};
1390 MDB_val dataV{data.size(), const_cast<char*>(data.data())};
1391 bool ret = lmdb::dbi_get(txn, handle(), &keyV, &dataV);
1392 if (ret) {
1393 data = std::string_view(static_cast<char*>(dataV.mv_data), dataV.mv_size);
1394 }
1395 return ret;
1396 }
1397
1398 /**
1399 * Stores a key/value pair into this database.
1400 *
1401 * @param txn a transaction handle
1402 * @param key
1403 * @param data
1404 * @param flags
1405 * @throws lmdb::error on failure
1406 */
1407 bool put(MDB_txn* const txn,
1408 const std::string_view key,
1409 std::string_view data,
1410 const unsigned int flags = default_put_flags) {
1411 const MDB_val keyV{key.size(), const_cast<char*>(key.data())};
1412 MDB_val dataV{data.size(), const_cast<char*>(data.data())};
1413 return lmdb::dbi_put(txn, handle(), &keyV, &dataV, flags);
1414 }
1415
1416 /**
1417 * Removes a key from this database.
1418 *
1419 * @param txn a transaction handle
1420 * @param key
1421 * @throws lmdb::error on failure
1422 */
1423 bool del(MDB_txn* const txn,
1424 const std::string_view key) {
1425 const MDB_val keyV{key.size(), const_cast<char*>(key.data())};
1426 return lmdb::dbi_del(txn, handle(), &keyV);
1427 }
1428
1429 /**
1430 * Removes a key/value pair from this database.
1431 *
1432 * @param txn a transaction handle
1433 * @param key
1434 * @param val
1435 * @throws lmdb::error on failure
1436 */
1437 bool del(MDB_txn* const txn,
1438 const std::string_view key,
1439 const std::string_view val) {
1440 const MDB_val keyV{key.size(), const_cast<char*>(key.data())};
1441 const MDB_val valV{val.size(), const_cast<char*>(val.data())};
1442 return lmdb::dbi_del(txn, handle(), &keyV, &valV);
1443 }
1444 };
1445
1446 ////////////////////////////////////////////////////////////////////////////////
1447 /* Resource Interface: Cursors */
1448
1449 namespace lmdb {
1450 class cursor;
1451 }
1452
1453 /**
1454 * Resource class for `MDB_cursor*` handles.
1455 *
1456 * @note Instances of this class are movable, but not copyable.
1457 * @see http://symas.com/mdb/doc/group__internal.html#structMDB__cursor
1458 */
1459 class lmdb::cursor {
1460 protected:
1461 MDB_cursor* _handle{nullptr};
1462
1463 public:
1464 static constexpr unsigned int default_flags = 0;
1465
1466 /**
1467 * Creates an LMDB cursor.
1468 *
1469 * @param txn the transaction handle
1470 * @param dbi the database handle
1471 * @throws lmdb::error on failure
1472 */
1473 static cursor
1474 open(MDB_txn* const txn,
1475 const MDB_dbi dbi) {
1476 MDB_cursor* handle{};
1477 lmdb::cursor_open(txn, dbi, &handle);
1478 #ifdef LMDBXX_DEBUG
1479 assert(handle != nullptr);
1480 #endif
1481 return cursor{handle};
1482 }
1483
1484 /**
1485 * Constructor.
1486 *
1487 * @param handle a valid `MDB_cursor*` handle
1488 */
1489 cursor(MDB_cursor* const handle) noexcept
1490 : _handle{handle} {}
1491
1492 /**
1493 * Move constructor.
1494 */
1495 cursor(cursor&& other) noexcept {
1496 std::swap(_handle, other._handle);
1497 }
1498
1499 /**
1500 * Move assignment operator.
1501 */
1502 cursor& operator=(cursor&& other) noexcept {
1503 if (this != &other) {
1504 std::swap(_handle, other._handle);
1505 }
1506 return *this;
1507 }
1508
1509 /**
1510 * Destructor.
1511 */
1512 ~cursor() noexcept {
1513 try { close(); } catch (...) {}
1514 }
1515
1516 /**
1517 * Returns the underlying `MDB_cursor*` handle.
1518 */
1519 operator MDB_cursor*() const noexcept {
1520 return _handle;
1521 }
1522
1523 /**
1524 * Returns the underlying `MDB_cursor*` handle.
1525 */
1526 MDB_cursor* handle() const noexcept {
1527 return _handle;
1528 }
1529
1530 /**
1531 * Closes this cursor.
1532 *
1533 * @note this method is idempotent
1534 * @post `handle() == nullptr`
1535 */
1536 void close() noexcept {
1537 if (_handle) {
1538 lmdb::cursor_close(_handle);
1539 _handle = nullptr;
1540 }
1541 }
1542
1543 /**
1544 * Renews this cursor.
1545 *
1546 * @param txn the transaction scope
1547 * @throws lmdb::error on failure
1548 */
1549 void renew(MDB_txn* const txn) {
1550 lmdb::cursor_renew(txn, handle());
1551 }
1552
1553 /**
1554 * Returns the cursor's transaction handle.
1555 */
1556 MDB_txn* txn() const noexcept {
1557 return lmdb::cursor_txn(handle());
1558 }
1559
1560 /**
1561 * Returns the cursor's database handle.
1562 */
1563 MDB_dbi dbi() const noexcept {
1564 return lmdb::cursor_dbi(handle());
1565 }
1566
1567 /**
1568 * Retrieves a key from the database.
1569 *
1570 * @param key
1571 * @param op
1572 * @throws lmdb::error on failure
1573 */
1574 bool get(std::string_view &key,
1575 const MDB_cursor_op op) {
1576 MDB_val keyV{key.size(), const_cast<char*>(key.data())};
1577 bool ret = lmdb::cursor_get(handle(), &keyV, nullptr, op);
1578 if (ret) {
1579 key = std::string_view(static_cast<char*>(keyV.mv_data), keyV.mv_size);
1580 }
1581 return ret;
1582 }
1583
1584 /**
1585 * Retrieves a key/value pair from the database.
1586 *
1587 * @param key
1588 * @param val
1589 * @param op
1590 * @throws lmdb::error on failure
1591 */
1592 bool get(std::string_view &key,
1593 std::string_view &val,
1594 const MDB_cursor_op op) {
1595 MDB_val keyV{key.size(), const_cast<char*>(key.data())};
1596 MDB_val valV{val.size(), const_cast<char*>(val.data())};
1597 bool ret = lmdb::cursor_get(handle(), &keyV, &valV, op);
1598 if (ret) {
1599 key = std::string_view(static_cast<char*>(keyV.mv_data), keyV.mv_size);
1600 val = std::string_view(static_cast<char*>(valV.mv_data), valV.mv_size);
1601 }
1602 return ret;
1603 }
1604
1605 /**
1606 * Stores key/data pairs into the database. The cursor is positioned at the new item, or on failure usually near it.
1607 *
1608 * See MDB docs for flag values.
1609 *
1610 * @param key
1611 * @param val
1612 * @param flags
1613 * @throws lmdb::error on failure
1614 */
1615 bool put(const std::string_view &key,
1616 const std::string_view &val,
1617 const unsigned int flags = 0) {
1618 MDB_val keyV{key.size(), const_cast<char*>(key.data())};
1619 MDB_val valV{val.size(), const_cast<char*>(val.data())};
1620 return lmdb::cursor_put(handle(), &keyV, &valV, flags);
1621 }
1622
1623 /**
1624 * Delete current key/data pair.
1625 *
1626 * @param flags Options for this operation. Values:
1627 * MDB_NODUPDATA - delete all of the data items for the current key. This flag may only be specified if the database was opened with MDB_DUPSORT.
1628 */
1629 void del(unsigned int flags = 0) {
1630 lmdb::cursor_del(handle(), flags);
1631 }
1632
1633 /**
1634 * Return count of duplicates for current key. This call is only valid on databases that support sorted duplicate data items MDB_DUPSORT.
1635 */
1636 size_t count() {
1637 std::size_t countp;
1638 lmdb::cursor_count(handle(), countp);
1639 return countp;
1640 }
1641 };
1642
1643 namespace lmdb {
1644 /**
1645 * Creates a std::string_view that points to the memory pointed to by v.
1646 *
1647 * @param v
1648 */
1649 template<typename T>
1650 static inline std::string_view ptr_to_sv(T* v) {
1651 return std::string_view(reinterpret_cast<char*>(v), sizeof(*v));
1652 }
1653
1654 /**
1655 * Creates a std::string_view that points to the memory occupied by v.
1656 *
1657 * @param v
1658 */
1659 template<typename T>
1660 static inline std::string_view to_sv(const T &v) {
1661 return std::string_view(reinterpret_cast<const char*>(std::addressof(v)), sizeof(v));
1662 }
1663
1664 /**
1665 * Takes a std::string_view and casts its pointer as a pointer to the parameterized type.
1666 *
1667 * @param v
1668 */
1669 template<typename T>
1670 static inline T* ptr_from_sv(std::string_view v) {
1671 if (v.size() != sizeof(T)) error::raise("from_sv", MDB_BAD_VALSIZE);
1672 return reinterpret_cast<T*>(const_cast<char*>(v.data()));
1673 }
1674
1675 /**
1676 * Takes a std::string_view and dereferences it, returning a value of the parameterized type.
1677 *
1678 * @param v
1679 */
1680 template<typename T>
1681 static inline T from_sv(std::string_view v) {
1682 if (v.size() != sizeof(T)) error::raise("from_sv", MDB_BAD_VALSIZE);
1683 T ret;
1684 std::memcpy(&ret, const_cast<char*>(v.data()), sizeof(T));
1685 return ret;
1686 }
1687 }
1688
1689 ////////////////////////////////////////////////////////////////////////////////
1690
1691 #endif /* LMDBXX_H */