Debian Codegraph @

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++11 wrapper for LMDB.
8 *
9 * @author Arto Bendiken <[email protected]>
10 * @see https://sourceforge.net/projects/lmdbxx/
11 */
12
13 #ifndef __cplusplus
14 #error "<lmdb++.h> requires a C++ compiler"
15 #endif
16
17 #if __cplusplus < 201103L
18 #if !defined(_MSC_VER) || _MSC_VER < 1900
19 #error "<lmdb++.h> requires a C++11 compiler (CXXFLAGS='-std=c++11')"
20 #endif // _MSC_VER check
21 #endif
22
23 ////////////////////////////////////////////////////////////////////////////////
24
25 #include <lmdb.h> /* for MDB_*, mdb_*() */
26
27 #ifdef LMDBXX_DEBUG
28 #include <cassert> /* for assert() */
29 #endif
30 #include <cstddef> /* for std::size_t */
31 #include <cstdio> /* for std::snprintf() */
32 #include <cstring> /* for std::strlen() */
33 #include <stdexcept> /* for std::runtime_error */
34 #include <string> /* for std::string */
35 #include <type_traits> /* for std::is_pod<> */
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 // TODO: mdb_reader_check()
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 ////////////////////////////////////////////////////////////////////////////////
521 /* Procedural Interface: Transactions */
522
523 namespace lmdb {
524 static inline void txn_begin(
525 MDB_env* env, MDB_txn* parent, unsigned int flags, MDB_txn** txn);
526 static inline MDB_env* txn_env(MDB_txn* txn) noexcept;
527 #ifdef LMDBXX_TXN_ID
528 static inline std::size_t txn_id(MDB_txn* txn) noexcept;
529 #endif
530 static inline void txn_commit(MDB_txn* txn);
531 static inline void txn_abort(MDB_txn* txn) noexcept;
532 static inline void txn_reset(MDB_txn* txn) noexcept;
533 static inline void txn_renew(MDB_txn* txn);
534 }
535
536 /**
537 * @throws lmdb::error on failure
538 * @see http://symas.com/mdb/doc/group__mdb.html#gad7ea55da06b77513609efebd44b26920
539 */
540 static inline void
541 lmdb::txn_begin(MDB_env* const env,
542 MDB_txn* const parent,
543 const unsigned int flags,
544 MDB_txn** txn) {
545 const int rc = ::mdb_txn_begin(env, parent, flags, txn);
546 if (rc != MDB_SUCCESS) {
547 error::raise("mdb_txn_begin", rc);
548 }
549 }
550
551 /**
552 * @see http://symas.com/mdb/doc/group__mdb.html#gaeb17735b8aaa2938a78a45cab85c06a0
553 */
554 static inline MDB_env*
555 lmdb::txn_env(MDB_txn* const txn) noexcept {
556 return ::mdb_txn_env(txn);
557 }
558
559 #ifdef LMDBXX_TXN_ID
560 /**
561 * @note Only available in HEAD, not yet in any 0.9.x release (as of 0.9.16).
562 */
563 static inline std::size_t
564 lmdb::txn_id(MDB_txn* const txn) noexcept {
565 return ::mdb_txn_id(txn);
566 }
567 #endif
568
569 /**
570 * @throws lmdb::error on failure
571 * @see http://symas.com/mdb/doc/group__mdb.html#ga846fbd6f46105617ac9f4d76476f6597
572 */
573 static inline void
574 lmdb::txn_commit(MDB_txn* const txn) {
575 const int rc = ::mdb_txn_commit(txn);
576 if (rc != MDB_SUCCESS) {
577 error::raise("mdb_txn_commit", rc);
578 }
579 }
580
581 /**
582 * @see http://symas.com/mdb/doc/group__mdb.html#ga73a5938ae4c3239ee11efa07eb22b882
583 */
584 static inline void
585 lmdb::txn_abort(MDB_txn* const txn) noexcept {
586 ::mdb_txn_abort(txn);
587 }
588
589 /**
590 * @see http://symas.com/mdb/doc/group__mdb.html#ga02b06706f8a66249769503c4e88c56cd
591 */
592 static inline void
593 lmdb::txn_reset(MDB_txn* const txn) noexcept {
594 ::mdb_txn_reset(txn);
595 }
596
597 /**
598 * @throws lmdb::error on failure
599 * @see http://symas.com/mdb/doc/group__mdb.html#ga6c6f917959517ede1c504cf7c720ce6d
600 */
601 static inline void
602 lmdb::txn_renew(MDB_txn* const txn) {
603 const int rc = ::mdb_txn_renew(txn);
604 if (rc != MDB_SUCCESS) {
605 error::raise("mdb_txn_renew", rc);
606 }
607 }
608
609 ////////////////////////////////////////////////////////////////////////////////
610 /* Procedural Interface: Databases */
611
612 namespace lmdb {
613 static inline void dbi_open(
614 MDB_txn* txn, const char* name, unsigned int flags, MDB_dbi* dbi);
615 static inline void dbi_stat(MDB_txn* txn, MDB_dbi dbi, MDB_stat* stat);
616 static inline void dbi_flags(MDB_txn* txn, MDB_dbi dbi, unsigned int* flags);
617 static inline void dbi_close(MDB_env* env, MDB_dbi dbi) noexcept;
618 static inline void dbi_drop(MDB_txn* txn, MDB_dbi dbi, bool del);
619 static inline void dbi_set_compare(MDB_txn* txn, MDB_dbi dbi, MDB_cmp_func* cmp);
620 static inline void dbi_set_dupsort(MDB_txn* txn, MDB_dbi dbi, MDB_cmp_func* cmp);
621 static inline void dbi_set_relfunc(MDB_txn* txn, MDB_dbi dbi, MDB_rel_func* rel);
622 static inline void dbi_set_relctx(MDB_txn* txn, MDB_dbi dbi, void* ctx);
623 static inline bool dbi_get(MDB_txn* txn, MDB_dbi dbi, const MDB_val* key, MDB_val* data);
624 static inline bool dbi_put(MDB_txn* txn, MDB_dbi dbi, const MDB_val* key, MDB_val* data, unsigned int flags);
625 static inline bool dbi_del(MDB_txn* txn, MDB_dbi dbi, const MDB_val* key, const MDB_val* data);
626 // TODO: mdb_cmp()
627 // TODO: mdb_dcmp()
628 }
629
630 /**
631 * @throws lmdb::error on failure
632 * @see http://symas.com/mdb/doc/group__mdb.html#gac08cad5b096925642ca359a6d6f0562a
633 */
634 static inline void
635 lmdb::dbi_open(MDB_txn* const txn,
636 const char* const name,
637 const unsigned int flags,
638 MDB_dbi* const dbi) {
639 const int rc = ::mdb_dbi_open(txn, name, flags, dbi);
640 if (rc != MDB_SUCCESS) {
641 error::raise("mdb_dbi_open", rc);
642 }
643 }
644
645 /**
646 * @throws lmdb::error on failure
647 * @see http://symas.com/mdb/doc/group__mdb.html#gae6c1069febe94299769dbdd032fadef6
648 */
649 static inline void
650 lmdb::dbi_stat(MDB_txn* const txn,
651 const MDB_dbi dbi,
652 MDB_stat* const result) {
653 const int rc = ::mdb_stat(txn, dbi, result);
654 if (rc != MDB_SUCCESS) {
655 error::raise("mdb_stat", rc);
656 }
657 }
658
659 /**
660 * @throws lmdb::error on failure
661 * @see http://symas.com/mdb/doc/group__mdb.html#ga95ba4cb721035478a8705e57b91ae4d4
662 */
663 static inline void
664 lmdb::dbi_flags(MDB_txn* const txn,
665 const MDB_dbi dbi,
666 unsigned int* const flags) {
667 const int rc = ::mdb_dbi_flags(txn, dbi, flags);
668 if (rc != MDB_SUCCESS) {
669 error::raise("mdb_dbi_flags", rc);
670 }
671 }
672
673 /**
674 * @see http://symas.com/mdb/doc/group__mdb.html#ga52dd98d0c542378370cd6b712ff961b5
675 */
676 static inline void
677 lmdb::dbi_close(MDB_env* const env,
678 const MDB_dbi dbi) noexcept {
679 ::mdb_dbi_close(env, dbi);
680 }
681
682 /**
683 * @see http://symas.com/mdb/doc/group__mdb.html#gab966fab3840fc54a6571dfb32b00f2db
684 */
685 static inline void
686 lmdb::dbi_drop(MDB_txn* const txn,
687 const MDB_dbi dbi,
688 const bool del = false) {
689 const int rc = ::mdb_drop(txn, dbi, del ? 1 : 0);
690 if (rc != MDB_SUCCESS) {
691 error::raise("mdb_drop", rc);
692 }
693 }
694
695 /**
696 * @throws lmdb::error on failure
697 * @see http://symas.com/mdb/doc/group__mdb.html#ga68e47ffcf72eceec553c72b1784ee0fe
698 */
699 static inline void
700 lmdb::dbi_set_compare(MDB_txn* const txn,
701 const MDB_dbi dbi,
702 MDB_cmp_func* const cmp = nullptr) {
703 const int rc = ::mdb_set_compare(txn, dbi, cmp);
704 if (rc != MDB_SUCCESS) {
705 error::raise("mdb_set_compare", rc);
706 }
707 }
708
709 /**
710 * @throws lmdb::error on failure
711 * @see http://symas.com/mdb/doc/group__mdb.html#gacef4ec3dab0bbd9bc978b73c19c879ae
712 */
713 static inline void
714 lmdb::dbi_set_dupsort(MDB_txn* const txn,
715 const MDB_dbi dbi,
716 MDB_cmp_func* const cmp = nullptr) {
717 const int rc = ::mdb_set_dupsort(txn, dbi, cmp);
718 if (rc != MDB_SUCCESS) {
719 error::raise("mdb_set_dupsort", rc);
720 }
721 }
722
723 /**
724 * @throws lmdb::error on failure
725 * @see http://symas.com/mdb/doc/group__mdb.html#ga697d82c7afe79f142207ad5adcdebfeb
726 */
727 static inline void
728 lmdb::dbi_set_relfunc(MDB_txn* const txn,
729 const MDB_dbi dbi,
730 MDB_rel_func* const rel) {
731 const int rc = ::mdb_set_relfunc(txn, dbi, rel);
732 if (rc != MDB_SUCCESS) {
733 error::raise("mdb_set_relfunc", rc);
734 }
735 }
736
737 /**
738 * @throws lmdb::error on failure
739 * @see http://symas.com/mdb/doc/group__mdb.html#ga7c34246308cee01724a1839a8f5cc594
740 */
741 static inline void
742 lmdb::dbi_set_relctx(MDB_txn* const txn,
743 const MDB_dbi dbi,
744 void* const ctx) {
745 const int rc = ::mdb_set_relctx(txn, dbi, ctx);
746 if (rc != MDB_SUCCESS) {
747 error::raise("mdb_set_relctx", rc);
748 }
749 }
750
751 /**
752 * @retval true if the key/value pair was retrieved
753 * @retval false if the key wasn't found
754 * @see http://symas.com/mdb/doc/group__mdb.html#ga8bf10cd91d3f3a83a34d04ce6b07992d
755 */
756 static inline bool
757 lmdb::dbi_get(MDB_txn* const txn,
758 const MDB_dbi dbi,
759 const MDB_val* const key,
760 MDB_val* const data) {
761 const int rc = ::mdb_get(txn, dbi, const_cast<MDB_val*>(key), data);
762 if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND) {
763 error::raise("mdb_get", rc);
764 }
765 return (rc == MDB_SUCCESS);
766 }
767
768 /**
769 * @retval true if the key/value pair was inserted
770 * @retval false if the key already existed
771 * @see http://symas.com/mdb/doc/group__mdb.html#ga4fa8573d9236d54687c61827ebf8cac0
772 */
773 static inline bool
774 lmdb::dbi_put(MDB_txn* const txn,
775 const MDB_dbi dbi,
776 const MDB_val* const key,
777 MDB_val* const data,
778 const unsigned int flags = 0) {
779 const int rc = ::mdb_put(txn, dbi, const_cast<MDB_val*>(key), data, flags);
780 if (rc != MDB_SUCCESS && rc != MDB_KEYEXIST) {
781 error::raise("mdb_put", rc);
782 }
783 return (rc == MDB_SUCCESS);
784 }
785
786 /**
787 * @retval true if the key/value pair was removed
788 * @retval false if the key wasn't found
789 * @see http://symas.com/mdb/doc/group__mdb.html#gab8182f9360ea69ac0afd4a4eaab1ddb0
790 */
791 static inline bool
792 lmdb::dbi_del(MDB_txn* const txn,
793 const MDB_dbi dbi,
794 const MDB_val* const key,
795 const MDB_val* const data = nullptr) {
796 const int rc = ::mdb_del(txn, dbi, const_cast<MDB_val*>(key), const_cast<MDB_val*>(data));
797 if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND) {
798 error::raise("mdb_del", rc);
799 }
800 return (rc == MDB_SUCCESS);
801 }
802
803 ////////////////////////////////////////////////////////////////////////////////
804 /* Procedural Interface: Cursors */
805
806 namespace lmdb {
807 static inline void cursor_open(MDB_txn* txn, MDB_dbi dbi, MDB_cursor** cursor);
808 static inline void cursor_close(MDB_cursor* cursor) noexcept;
809 static inline void cursor_renew(MDB_txn* txn, MDB_cursor* cursor);
810 static inline MDB_txn* cursor_txn(MDB_cursor* cursor) noexcept;
811 static inline MDB_dbi cursor_dbi(MDB_cursor* cursor) noexcept;
812 static inline bool cursor_get(MDB_cursor* cursor, MDB_val* key, MDB_val* data, MDB_cursor_op op);
813 static inline void cursor_put(MDB_cursor* cursor, MDB_val* key, MDB_val* data, unsigned int flags);
814 static inline void cursor_del(MDB_cursor* cursor, unsigned int flags);
815 static inline void cursor_count(MDB_cursor* cursor, std::size_t& count);
816 }
817
818 /**
819 * @throws lmdb::error on failure
820 * @see http://symas.com/mdb/doc/group__mdb.html#ga9ff5d7bd42557fd5ee235dc1d62613aa
821 */
822 static inline void
823 lmdb::cursor_open(MDB_txn* const txn,
824 const MDB_dbi dbi,
825 MDB_cursor** const cursor) {
826 const int rc = ::mdb_cursor_open(txn, dbi, cursor);
827 if (rc != MDB_SUCCESS) {
828 error::raise("mdb_cursor_open", rc);
829 }
830 }
831
832 /**
833 * @see http://symas.com/mdb/doc/group__mdb.html#gad685f5d73c052715c7bd859cc4c05188
834 */
835 static inline void
836 lmdb::cursor_close(MDB_cursor* const cursor) noexcept {
837 ::mdb_cursor_close(cursor);
838 }
839
840 /**
841 * @throws lmdb::error on failure
842 * @see http://symas.com/mdb/doc/group__mdb.html#gac8b57befb68793070c85ea813df481af
843 */
844 static inline void
845 lmdb::cursor_renew(MDB_txn* const txn,
846 MDB_cursor* const cursor) {
847 const int rc = ::mdb_cursor_renew(txn, cursor);
848 if (rc != MDB_SUCCESS) {
849 error::raise("mdb_cursor_renew", rc);
850 }
851 }
852
853 /**
854 * @see http://symas.com/mdb/doc/group__mdb.html#ga7bf0d458f7f36b5232fcb368ebda79e0
855 */
856 static inline MDB_txn*
857 lmdb::cursor_txn(MDB_cursor* const cursor) noexcept {
858 return ::mdb_cursor_txn(cursor);
859 }
860
861 /**
862 * @see http://symas.com/mdb/doc/group__mdb.html#ga2f7092cf70ee816fb3d2c3267a732372
863 */
864 static inline MDB_dbi
865 lmdb::cursor_dbi(MDB_cursor* const cursor) noexcept {
866 return ::mdb_cursor_dbi(cursor);
867 }
868
869 /**
870 * @throws lmdb::error on failure
871 * @see http://symas.com/mdb/doc/group__mdb.html#ga48df35fb102536b32dfbb801a47b4cb0
872 */
873 static inline bool
874 lmdb::cursor_get(MDB_cursor* const cursor,
875 MDB_val* const key,
876 MDB_val* const data,
877 const MDB_cursor_op op) {
878 const int rc = ::mdb_cursor_get(cursor, key, data, op);
879 if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND) {
880 error::raise("mdb_cursor_get", rc);
881 }
882 return (rc == MDB_SUCCESS);
883 }
884
885 /**
886 * @throws lmdb::error on failure
887 * @see http://symas.com/mdb/doc/group__mdb.html#ga1f83ccb40011837ff37cc32be01ad91e
888 */
889 static inline void
890 lmdb::cursor_put(MDB_cursor* const cursor,
891 MDB_val* const key,
892 MDB_val* const data,
893 const unsigned int flags = 0) {
894 const int rc = ::mdb_cursor_put(cursor, key, data, flags);
895 if (rc != MDB_SUCCESS) {
896 error::raise("mdb_cursor_put", rc);
897 }
898 }
899
900 /**
901 * @throws lmdb::error on failure
902 * @see http://symas.com/mdb/doc/group__mdb.html#ga26a52d3efcfd72e5bf6bd6960bf75f95
903 */
904 static inline void
905 lmdb::cursor_del(MDB_cursor* const cursor,
906 const unsigned int flags = 0) {
907 const int rc = ::mdb_cursor_del(cursor, flags);
908 if (rc != MDB_SUCCESS) {
909 error::raise("mdb_cursor_del", rc);
910 }
911 }
912
913 /**
914 * @throws lmdb::error on failure
915 * @see http://symas.com/mdb/doc/group__mdb.html#ga4041fd1e1862c6b7d5f10590b86ffbe2
916 */
917 static inline void
918 lmdb::cursor_count(MDB_cursor* const cursor,
919 std::size_t& count) {
920 const int rc = ::mdb_cursor_count(cursor, &count);
921 if (rc != MDB_SUCCESS) {
922 error::raise("mdb_cursor_count", rc);
923 }
924 }
925
926 ////////////////////////////////////////////////////////////////////////////////
927 /* Resource Interface: Values */
928
929 namespace lmdb {
930 class val;
931 }
932
933 /**
934 * Wrapper class for `MDB_val` structures.
935 *
936 * @note Instances of this class are movable and copyable both.
937 * @see http://symas.com/mdb/doc/group__mdb.html#structMDB__val
938 */
939 class lmdb::val {
940 protected:
941 MDB_val _val;
942
943 public:
944 /**
945 * Default constructor.
946 */
947 val() noexcept = default;
948
949 /**
950 * Constructor.
951 */
952 val(const std::string& data) noexcept
953 : val{data.data(), data.size()} {}
954
955 /**
956 * Constructor.
957 */
958 val(const char* const data) noexcept
959 : val{data, std::strlen(data)} {}
960
961 /**
962 * Constructor.
963 */
964 val(const void* const data,
965 const std::size_t size) noexcept
966 : _val{size, const_cast<void*>(data)} {}
967
968 /**
969 * Move constructor.
970 */
971 val(val&& other) noexcept = default;
972
973 /**
974 * Move assignment operator.
975 */
976 val& operator=(val&& other) noexcept = default;
977
978 /**
979 * Destructor.
980 */
981 ~val() noexcept = default;
982
983 /**
984 * Returns an `MDB_val*` pointer.
985 */
986 operator MDB_val*() noexcept {
987 return &_val;
988 }
989
990 /**
991 * Returns an `MDB_val*` pointer.
992 */
993 operator const MDB_val*() const noexcept {
994 return &_val;
995 }
996
997 /**
998 * Determines whether this value is empty.
999 */
1000 bool empty() const noexcept {
1001 return size() == 0;
1002 }
1003
1004 /**
1005 * Returns the size of the data.
1006 */
1007 std::size_t size() const noexcept {
1008 return _val.mv_size;
1009 }
1010
1011 /**
1012 * Returns a pointer to the data.
1013 */
1014 template<typename T>
1015 T* data() noexcept {
1016 return reinterpret_cast<T*>(_val.mv_data);
1017 }
1018
1019 /**
1020 * Returns a pointer to the data.
1021 */
1022 template<typename T>
1023 const T* data() const noexcept {
1024 return reinterpret_cast<T*>(_val.mv_data);
1025 }
1026
1027 /**
1028 * Returns a pointer to the data.
1029 */
1030 char* data() noexcept {
1031 return reinterpret_cast<char*>(_val.mv_data);
1032 }
1033
1034 /**
1035 * Returns a pointer to the data.
1036 */
1037 const char* data() const noexcept {
1038 return reinterpret_cast<char*>(_val.mv_data);
1039 }
1040
1041 /**
1042 * Assigns the value.
1043 */
1044 template<typename T>
1045 val& assign(const T* const data,
1046 const std::size_t size) noexcept {
1047 _val.mv_size = size;
1048 _val.mv_data = const_cast<void*>(reinterpret_cast<const void*>(data));
1049 return *this;
1050 }
1051
1052 /**
1053 * Assigns the value.
1054 */
1055 val& assign(const char* const data) noexcept {
1056 return assign(data, std::strlen(data));
1057 }
1058
1059 /**
1060 * Assigns the value.
1061 */
1062 val& assign(const std::string& data) noexcept {
1063 return assign(data.data(), data.size());
1064 }
1065 };
1066
1067 #if !(defined(__COVERITY__) || defined(_MSC_VER))
1068 static_assert(std::is_pod<lmdb::val>::value, "lmdb::val must be a POD type");
1069 static_assert(sizeof(lmdb::val) == sizeof(MDB_val), "sizeof(lmdb::val) != sizeof(MDB_val)");
1070 #endif
1071
1072 ////////////////////////////////////////////////////////////////////////////////
1073 /* Resource Interface: Environment */
1074
1075 namespace lmdb {
1076 class env;
1077 }
1078
1079 /**
1080 * Resource class for `MDB_env*` handles.
1081 *
1082 * @note Instances of this class are movable, but not copyable.
1083 * @see http://symas.com/mdb/doc/group__internal.html#structMDB__env
1084 */
1085 class lmdb::env {
1086 protected:
1087 MDB_env* _handle{nullptr};
1088
1089 public:
1090 static constexpr unsigned int default_flags = 0;
1091 static constexpr mode default_mode = 0644; /* -rw-r--r-- */
1092
1093 /**
1094 * Creates a new LMDB environment.
1095 *
1096 * @param flags
1097 * @throws lmdb::error on failure
1098 */
1099 static env create(const unsigned int flags = default_flags) {
1100 MDB_env* handle{nullptr};
1101 lmdb::env_create(&handle);
1102 #ifdef LMDBXX_DEBUG
1103 assert(handle != nullptr);
1104 #endif
1105 if (flags) {
1106 try {
1107 lmdb::env_set_flags(handle, flags);
1108 }
1109 catch (const lmdb::error&) {
1110 lmdb::env_close(handle);
1111 throw;
1112 }
1113 }
1114 return env{handle};
1115 }
1116
1117 /**
1118 * Constructor.
1119 *
1120 * @param handle a valid `MDB_env*` handle
1121 */
1122 env(MDB_env* const handle) noexcept
1123 : _handle{handle} {}
1124
1125 /**
1126 * Move constructor.
1127 */
1128 env(env&& other) noexcept {
1129 std::swap(_handle, other._handle);
1130 }
1131
1132 /**
1133 * Move assignment operator.
1134 */
1135 env& operator=(env&& other) noexcept {
1136 if (this != &other) {
1137 std::swap(_handle, other._handle);
1138 }
1139 return *this;
1140 }
1141
1142 /**
1143 * Destructor.
1144 */
1145 ~env() noexcept {
1146 try { close(); } catch (...) {}
1147 }
1148
1149 /**
1150 * Returns the underlying `MDB_env*` handle.
1151 */
1152 operator MDB_env*() const noexcept {
1153 return _handle;
1154 }
1155
1156 /**
1157 * Returns the underlying `MDB_env*` handle.
1158 */
1159 MDB_env* handle() const noexcept {
1160 return _handle;
1161 }
1162
1163 /**
1164 * Flushes data buffers to disk.
1165 *
1166 * @param force
1167 * @throws lmdb::error on failure
1168 */
1169 void sync(const bool force = true) {
1170 lmdb::env_sync(handle(), force);
1171 }
1172
1173 /**
1174 * Closes this environment, releasing the memory map.
1175 *
1176 * @note this method is idempotent
1177 * @post `handle() == nullptr`
1178 */
1179 void close() noexcept {
1180 if (handle()) {
1181 lmdb::env_close(handle());
1182 _handle = nullptr;
1183 }
1184 }
1185
1186 /**
1187 * Opens this environment.
1188 *
1189 * @param path
1190 * @param flags
1191 * @param mode
1192 * @throws lmdb::error on failure
1193 */
1194 env& open(const char* const path,
1195 const unsigned int flags = default_flags,
1196 const mode mode = default_mode) {
1197 lmdb::env_open(handle(), path, flags, mode);
1198 return *this;
1199 }
1200
1201 /**
1202 * @param flags
1203 * @param onoff
1204 * @throws lmdb::error on failure
1205 */
1206 env& set_flags(const unsigned int flags,
1207 const bool onoff = true) {
1208 lmdb::env_set_flags(handle(), flags, onoff);
1209 return *this;
1210 }
1211
1212 /**
1213 * @param size
1214 * @throws lmdb::error on failure
1215 */
1216 env& set_mapsize(const std::size_t size) {
1217 lmdb::env_set_mapsize(handle(), size);
1218 return *this;
1219 }
1220
1221 /**
1222 * @param count
1223 * @throws lmdb::error on failure
1224 */
1225 env& set_max_readers(const unsigned int count) {
1226 lmdb::env_set_max_readers(handle(), count);
1227 return *this;
1228 }
1229
1230 /**
1231 * @param count
1232 * @throws lmdb::error on failure
1233 */
1234 env& set_max_dbs(const MDB_dbi count) {
1235 lmdb::env_set_max_dbs(handle(), count);
1236 return *this;
1237 }
1238 };
1239
1240 ////////////////////////////////////////////////////////////////////////////////
1241 /* Resource Interface: Transactions */
1242
1243 namespace lmdb {
1244 class txn;
1245 }
1246
1247 /**
1248 * Resource class for `MDB_txn*` handles.
1249 *
1250 * @note Instances of this class are movable, but not copyable.
1251 * @see http://symas.com/mdb/doc/group__internal.html#structMDB__txn
1252 */
1253 class lmdb::txn {
1254 protected:
1255 MDB_txn* _handle{nullptr};
1256
1257 public:
1258 static constexpr unsigned int default_flags = 0;
1259
1260 /**
1261 * Creates a new LMDB transaction.
1262 *
1263 * @param env the environment handle
1264 * @param parent
1265 * @param flags
1266 * @throws lmdb::error on failure
1267 */
1268 static txn begin(MDB_env* const env,
1269 MDB_txn* const parent = nullptr,
1270 const unsigned int flags = default_flags) {
1271 MDB_txn* handle{nullptr};
1272 lmdb::txn_begin(env, parent, flags, &handle);
1273 #ifdef LMDBXX_DEBUG
1274 assert(handle != nullptr);
1275 #endif
1276 return txn{handle};
1277 }
1278
1279 /**
1280 * Constructor.
1281 *
1282 * @param handle a valid `MDB_txn*` handle
1283 */
1284 txn(MDB_txn* const handle) noexcept
1285 : _handle{handle} {}
1286
1287 /**
1288 * Move constructor.
1289 */
1290 txn(txn&& other) noexcept {
1291 std::swap(_handle, other._handle);
1292 }
1293
1294 /**
1295 * Move assignment operator.
1296 */
1297 txn& operator=(txn&& other) noexcept {
1298 if (this != &other) {
1299 std::swap(_handle, other._handle);
1300 }
1301 return *this;
1302 }
1303
1304 /**
1305 * Destructor.
1306 */
1307 ~txn() noexcept {
1308 if (_handle) {
1309 try { abort(); } catch (...) {}
1310 _handle = nullptr;
1311 }
1312 }
1313
1314 /**
1315 * Returns the underlying `MDB_txn*` handle.
1316 */
1317 operator MDB_txn*() const noexcept {
1318 return _handle;
1319 }
1320
1321 /**
1322 * Returns the underlying `MDB_txn*` handle.
1323 */
1324 MDB_txn* handle() const noexcept {
1325 return _handle;
1326 }
1327
1328 /**
1329 * Returns the transaction's `MDB_env*` handle.
1330 */
1331 MDB_env* env() const noexcept {
1332 return lmdb::txn_env(handle());
1333 }
1334
1335 /**
1336 * Commits this transaction.
1337 *
1338 * @throws lmdb::error on failure
1339 * @post `handle() == nullptr`
1340 */
1341 void commit() {
1342 lmdb::txn_commit(_handle);
1343 _handle = nullptr;
1344 }
1345
1346 /**
1347 * Aborts this transaction.
1348 *
1349 * @post `handle() == nullptr`
1350 */
1351 void abort() noexcept {
1352 lmdb::txn_abort(_handle);
1353 _handle = nullptr;
1354 }
1355
1356 /**
1357 * Resets this read-only transaction.
1358 */
1359 void reset() noexcept {
1360 lmdb::txn_reset(_handle);
1361 }
1362
1363 /**
1364 * Renews this read-only transaction.
1365 *
1366 * @throws lmdb::error on failure
1367 */
1368 void renew() {
1369 lmdb::txn_renew(_handle);
1370 }
1371 };
1372
1373 ////////////////////////////////////////////////////////////////////////////////
1374 /* Resource Interface: Databases */
1375
1376 namespace lmdb {
1377 class dbi;
1378 }
1379
1380 /**
1381 * Resource class for `MDB_dbi` handles.
1382 *
1383 * @note Instances of this class are movable, but not copyable.
1384 * @see http://symas.com/mdb/doc/group__mdb.html#gadbe68a06c448dfb62da16443d251a78b
1385 */
1386 class lmdb::dbi {
1387 protected:
1388 MDB_dbi _handle{0};
1389
1390 public:
1391 static constexpr unsigned int default_flags = 0;
1392 static constexpr unsigned int default_put_flags = 0;
1393
1394 /**
1395 * Opens a database handle.
1396 *
1397 * @param txn the transaction handle
1398 * @param name
1399 * @param flags
1400 * @throws lmdb::error on failure
1401 */
1402 static dbi
1403 open(MDB_txn* const txn,
1404 const char* const name = nullptr,
1405 const unsigned int flags = default_flags) {
1406 MDB_dbi handle{};
1407 lmdb::dbi_open(txn, name, flags, &handle);
1408 return dbi{handle};
1409 }
1410
1411 /**
1412 * Constructor.
1413 *
1414 * @param handle a valid `MDB_dbi` handle
1415 */
1416 dbi(const MDB_dbi handle) noexcept
1417 : _handle{handle} {}
1418
1419 /**
1420 * Move constructor.
1421 */
1422 dbi(dbi&& other) noexcept {
1423 std::swap(_handle, other._handle);
1424 }
1425
1426 /**
1427 * Move assignment operator.
1428 */
1429 dbi& operator=(dbi&& other) noexcept {
1430 if (this != &other) {
1431 std::swap(_handle, other._handle);
1432 }
1433 return *this;
1434 }
1435
1436 /**
1437 * Destructor.
1438 */
1439 ~dbi() noexcept {
1440 if (_handle) {
1441 /* No need to call close() here. */
1442 }
1443 }
1444
1445 /**
1446 * Returns the underlying `MDB_dbi` handle.
1447 */
1448 operator MDB_dbi() const noexcept {
1449 return _handle;
1450 }
1451
1452 /**
1453 * Returns the underlying `MDB_dbi` handle.
1454 */
1455 MDB_dbi handle() const noexcept {
1456 return _handle;
1457 }
1458
1459 /**
1460 * Returns statistics for this database.
1461 *
1462 * @param txn a transaction handle
1463 * @throws lmdb::error on failure
1464 */
1465 MDB_stat stat(MDB_txn* const txn) const {
1466 MDB_stat result;
1467 lmdb::dbi_stat(txn, handle(), &result);
1468 return result;
1469 }
1470
1471 /**
1472 * Retrieves the flags for this database handle.
1473 *
1474 * @param txn a transaction handle
1475 * @throws lmdb::error on failure
1476 */
1477 unsigned int flags(MDB_txn* const txn) const {
1478 unsigned int result{};
1479 lmdb::dbi_flags(txn, handle(), &result);
1480 return result;
1481 }
1482
1483 /**
1484 * Returns the number of records in this database.
1485 *
1486 * @param txn a transaction handle
1487 * @throws lmdb::error on failure
1488 */
1489 std::size_t size(MDB_txn* const txn) const {
1490 return stat(txn).ms_entries;
1491 }
1492
1493 /**
1494 * @param txn a transaction handle
1495 * @param del
1496 * @throws lmdb::error on failure
1497 */
1498 void drop(MDB_txn* const txn,
1499 const bool del = false) {
1500 lmdb::dbi_drop(txn, handle(), del);
1501 }
1502
1503 /**
1504 * Sets a custom key comparison function for this database.
1505 *
1506 * @param txn a transaction handle
1507 * @param cmp the comparison function
1508 * @throws lmdb::error on failure
1509 */
1510 dbi& set_compare(MDB_txn* const txn,
1511 MDB_cmp_func* const cmp = nullptr) {
1512 lmdb::dbi_set_compare(txn, handle(), cmp);
1513 return *this;
1514 }
1515
1516 /**
1517 * Retrieves a key/value pair from this database.
1518 *
1519 * @param txn a transaction handle
1520 * @param key
1521 * @param data
1522 * @throws lmdb::error on failure
1523 */
1524 bool get(MDB_txn* const txn,
1525 const val& key,
1526 val& data) {
1527 return lmdb::dbi_get(txn, handle(), key, data);
1528 }
1529
1530 /**
1531 * Retrieves a key from this database.
1532 *
1533 * @param txn a transaction handle
1534 * @param key
1535 * @throws lmdb::error on failure
1536 */
1537 template<typename K>
1538 bool get(MDB_txn* const txn,
1539 const K& key) const {
1540 const lmdb::val k{&key, sizeof(K)};
1541 lmdb::val v{};
1542 return lmdb::dbi_get(txn, handle(), k, v);
1543 }
1544
1545 /**
1546 * Retrieves a key/value pair from this database.
1547 *
1548 * @param txn a transaction handle
1549 * @param key
1550 * @param val
1551 * @throws lmdb::error on failure
1552 */
1553 template<typename K, typename V>
1554 bool get(MDB_txn* const txn,
1555 const K& key,
1556 V& val) const {
1557 const lmdb::val k{&key, sizeof(K)};
1558 lmdb::val v{};
1559 const bool result = lmdb::dbi_get(txn, handle(), k, v);
1560 if (result) {
1561 val = *v.data<const V>();
1562 }
1563 return result;
1564 }
1565
1566 /**
1567 * Retrieves a key/value pair from this database.
1568 *
1569 * @param txn a transaction handle
1570 * @param key a NUL-terminated string key
1571 * @param val
1572 * @throws lmdb::error on failure
1573 */
1574 template<typename V>
1575 bool get(MDB_txn* const txn,
1576 const char* const key,
1577 V& val) const {
1578 const lmdb::val k{key, std::strlen(key)};
1579 lmdb::val v{};
1580 const bool result = lmdb::dbi_get(txn, handle(), k, v);
1581 if (result) {
1582 val = *v.data<const V>();
1583 }
1584 return result;
1585 }
1586
1587 /**
1588 * Stores a key/value pair into this database.
1589 *
1590 * @param txn a transaction handle
1591 * @param key
1592 * @param data
1593 * @param flags
1594 * @throws lmdb::error on failure
1595 */
1596 bool put(MDB_txn* const txn,
1597 const val& key,
1598 val& data,
1599 const unsigned int flags = default_put_flags) {
1600 return lmdb::dbi_put(txn, handle(), key, data, flags);
1601 }
1602
1603 /**
1604 * Stores a key into this database.
1605 *
1606 * @param txn a transaction handle
1607 * @param key
1608 * @param flags
1609 * @throws lmdb::error on failure
1610 */
1611 template<typename K>
1612 bool put(MDB_txn* const txn,
1613 const K& key,
1614 const unsigned int flags = default_put_flags) {
1615 const lmdb::val k{&key, sizeof(K)};
1616 lmdb::val v{};
1617 return lmdb::dbi_put(txn, handle(), k, v, flags);
1618 }
1619
1620 /**
1621 * Stores a key/value pair into this database.
1622 *
1623 * @param txn a transaction handle
1624 * @param key
1625 * @param val
1626 * @param flags
1627 * @throws lmdb::error on failure
1628 */
1629 template<typename K, typename V>
1630 bool put(MDB_txn* const txn,
1631 const K& key,
1632 const V& val,
1633 const unsigned int flags = default_put_flags) {
1634 const lmdb::val k{&key, sizeof(K)};
1635 lmdb::val v{&val, sizeof(V)};
1636 return lmdb::dbi_put(txn, handle(), k, v, flags);
1637 }
1638
1639 /**
1640 * Stores a key/value pair into this database.
1641 *
1642 * @param txn a transaction handle
1643 * @param key a NUL-terminated string key
1644 * @param val
1645 * @param flags
1646 * @throws lmdb::error on failure
1647 */
1648 template<typename V>
1649 bool put(MDB_txn* const txn,
1650 const char* const key,
1651 const V& val,
1652 const unsigned int flags = default_put_flags) {
1653 const lmdb::val k{key, std::strlen(key)};
1654 lmdb::val v{&val, sizeof(V)};
1655 return lmdb::dbi_put(txn, handle(), k, v, flags);
1656 }
1657
1658 /**
1659 * Stores a key/value pair into this database.
1660 *
1661 * @param txn a transaction handle
1662 * @param key a NUL-terminated string key
1663 * @param val a NUL-terminated string key
1664 * @param flags
1665 * @throws lmdb::error on failure
1666 */
1667 bool put(MDB_txn* const txn,
1668 const char* const key,
1669 const char* const val,
1670 const unsigned int flags = default_put_flags) {
1671 const lmdb::val k{key, std::strlen(key)};
1672 lmdb::val v{val, std::strlen(val)};
1673 return lmdb::dbi_put(txn, handle(), k, v, flags);
1674 }
1675
1676 /**
1677 * Removes a key/value pair from this database.
1678 *
1679 * @param txn a transaction handle
1680 * @param key
1681 * @throws lmdb::error on failure
1682 */
1683 bool del(MDB_txn* const txn,
1684 const val& key) {
1685 return lmdb::dbi_del(txn, handle(), key);
1686 }
1687
1688 /**
1689 * Removes a key/value pair from this database.
1690 *
1691 * @param txn a transaction handle
1692 * @param key
1693 * @throws lmdb::error on failure
1694 */
1695 template<typename K>
1696 bool del(MDB_txn* const txn,
1697 const K& key) {
1698 const lmdb::val k{&key, sizeof(K)};
1699 return lmdb::dbi_del(txn, handle(), k);
1700 }
1701 };
1702
1703 ////////////////////////////////////////////////////////////////////////////////
1704 /* Resource Interface: Cursors */
1705
1706 namespace lmdb {
1707 class cursor;
1708 }
1709
1710 /**
1711 * Resource class for `MDB_cursor*` handles.
1712 *
1713 * @note Instances of this class are movable, but not copyable.
1714 * @see http://symas.com/mdb/doc/group__internal.html#structMDB__cursor
1715 */
1716 class lmdb::cursor {
1717 protected:
1718 MDB_cursor* _handle{nullptr};
1719
1720 public:
1721 static constexpr unsigned int default_flags = 0;
1722
1723 /**
1724 * Creates an LMDB cursor.
1725 *
1726 * @param txn the transaction handle
1727 * @param dbi the database handle
1728 * @throws lmdb::error on failure
1729 */
1730 static cursor
1731 open(MDB_txn* const txn,
1732 const MDB_dbi dbi) {
1733 MDB_cursor* handle{};
1734 lmdb::cursor_open(txn, dbi, &handle);
1735 #ifdef LMDBXX_DEBUG
1736 assert(handle != nullptr);
1737 #endif
1738 return cursor{handle};
1739 }
1740
1741 /**
1742 * Constructor.
1743 *
1744 * @param handle a valid `MDB_cursor*` handle
1745 */
1746 cursor(MDB_cursor* const handle) noexcept
1747 : _handle{handle} {}
1748
1749 /**
1750 * Move constructor.
1751 */
1752 cursor(cursor&& other) noexcept {
1753 std::swap(_handle, other._handle);
1754 }
1755
1756 /**
1757 * Move assignment operator.
1758 */
1759 cursor& operator=(cursor&& other) noexcept {
1760 if (this != &other) {
1761 std::swap(_handle, other._handle);
1762 }
1763 return *this;
1764 }
1765
1766 /**
1767 * Destructor.
1768 */
1769 ~cursor() noexcept {
1770 try { close(); } catch (...) {}
1771 }
1772
1773 /**
1774 * Returns the underlying `MDB_cursor*` handle.
1775 */
1776 operator MDB_cursor*() const noexcept {
1777 return _handle;
1778 }
1779
1780 /**
1781 * Returns the underlying `MDB_cursor*` handle.
1782 */
1783 MDB_cursor* handle() const noexcept {
1784 return _handle;
1785 }
1786
1787 /**
1788 * Closes this cursor.
1789 *
1790 * @note this method is idempotent
1791 * @post `handle() == nullptr`
1792 */
1793 void close() noexcept {
1794 if (_handle) {
1795 lmdb::cursor_close(_handle);
1796 _handle = nullptr;
1797 }
1798 }
1799
1800 /**
1801 * Renews this cursor.
1802 *
1803 * @param txn the transaction scope
1804 * @throws lmdb::error on failure
1805 */
1806 void renew(MDB_txn* const txn) {
1807 lmdb::cursor_renew(txn, handle());
1808 }
1809
1810 /**
1811 * Returns the cursor's transaction handle.
1812 */
1813 MDB_txn* txn() const noexcept {
1814 return lmdb::cursor_txn(handle());
1815 }
1816
1817 /**
1818 * Returns the cursor's database handle.
1819 */
1820 MDB_dbi dbi() const noexcept {
1821 return lmdb::cursor_dbi(handle());
1822 }
1823
1824 /**
1825 * Retrieves a key from the database.
1826 *
1827 * @param key
1828 * @param op
1829 * @throws lmdb::error on failure
1830 */
1831 bool get(MDB_val* const key,
1832 const MDB_cursor_op op) {
1833 return get(key, nullptr, op);
1834 }
1835
1836 /**
1837 * Retrieves a key from the database.
1838 *
1839 * @param key
1840 * @param op
1841 * @throws lmdb::error on failure
1842 */
1843 bool get(lmdb::val& key,
1844 const MDB_cursor_op op) {
1845 return get(key, nullptr, op);
1846 }
1847
1848 /**
1849 * Retrieves a key/value pair from the database.
1850 *
1851 * @param key
1852 * @param val (may be `nullptr`)
1853 * @param op
1854 * @throws lmdb::error on failure
1855 */
1856 bool get(MDB_val* const key,
1857 MDB_val* const val,
1858 const MDB_cursor_op op) {
1859 return lmdb::cursor_get(handle(), key, val, op);
1860 }
1861
1862 /**
1863 * Retrieves a key/value pair from the database.
1864 *
1865 * @param key
1866 * @param val
1867 * @param op
1868 * @throws lmdb::error on failure
1869 */
1870 bool get(lmdb::val& key,
1871 lmdb::val& val,
1872 const MDB_cursor_op op) {
1873 return lmdb::cursor_get(handle(), key, val, op);
1874 }
1875
1876 /**
1877 * Retrieves a key/value pair from the database.
1878 *
1879 * @param key
1880 * @param val
1881 * @param op
1882 * @throws lmdb::error on failure
1883 */
1884 bool get(std::string& key,
1885 std::string& val,
1886 const MDB_cursor_op op) {
1887 lmdb::val k{}, v{};
1888 const bool found = get(k, v, op);
1889 if (found) {
1890 key.assign(k.data(), k.size());
1891 val.assign(v.data(), v.size());
1892 }
1893 return found;
1894 }
1895
1896 /**
1897 * Positions this cursor at the given key.
1898 *
1899 * @param key
1900 * @param op
1901 * @throws lmdb::error on failure
1902 */
1903 template<typename K>
1904 bool find(const K& key,
1905 const MDB_cursor_op op = MDB_SET) {
1906 lmdb::val k{&key, sizeof(K)};
1907 return get(k, nullptr, op);
1908 }
1909 };
1910
1911 ////////////////////////////////////////////////////////////////////////////////
1912
1913 #endif /* LMDBXX_H */