-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathlmdb++.h
1913 lines (1723 loc) · 46.5 KB
/
lmdb++.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This is free and unencumbered software released into the public domain. */
#ifndef LMDBXX_H
#define LMDBXX_H
/**
* <lmdb++.h> - C++11 wrapper for LMDB.
*
* @author Arto Bendiken <[email protected]>
* @see https://sourceforge.net/projects/lmdbxx/
*/
#ifndef __cplusplus
#error "<lmdb++.h> requires a C++ compiler"
#endif
#if __cplusplus < 201103L
#if !defined(_MSC_VER) || _MSC_VER < 1900
#error "<lmdb++.h> requires a C++11 compiler (CXXFLAGS='-std=c++11')"
#endif // _MSC_VER check
#endif
////////////////////////////////////////////////////////////////////////////////
#include <lmdb.h> /* for MDB_*, mdb_*() */
#ifdef LMDBXX_DEBUG
#include <cassert> /* for assert() */
#endif
#include <cstddef> /* for std::size_t */
#include <cstdio> /* for std::snprintf() */
#include <cstring> /* for std::strlen() */
#include <stdexcept> /* for std::runtime_error */
#include <string> /* for std::string */
#include <type_traits> /* for std::is_pod<> */
namespace lmdb {
using mode = mdb_mode_t;
}
////////////////////////////////////////////////////////////////////////////////
/* Error Handling */
namespace lmdb {
class error;
class logic_error;
class fatal_error;
class runtime_error;
class key_exist_error;
class not_found_error;
class corrupted_error;
class panic_error;
class version_mismatch_error;
class map_full_error;
class bad_dbi_error;
}
/**
* Base class for LMDB exception conditions.
*
* @see http://symas.com/mdb/doc/group__errors.html
*/
class lmdb::error : public std::runtime_error {
protected:
const int _code;
public:
/**
* Throws an error based on the given LMDB return code.
*/
[[noreturn]] static inline void raise(const char* origin, int rc);
/**
* Constructor.
*/
error(const char* const origin,
const int rc) noexcept
: runtime_error{origin},
_code{rc} {}
/**
* Returns the underlying LMDB error code.
*/
int code() const noexcept {
return _code;
}
/**
* Returns the origin of the LMDB error.
*/
const char* origin() const noexcept {
return runtime_error::what();
}
/**
* Returns the underlying LMDB error code.
*/
virtual const char* what() const noexcept {
static thread_local char buffer[1024];
std::snprintf(buffer, sizeof(buffer),
"%s: %s", origin(), ::mdb_strerror(code()));
return buffer;
}
};
/**
* Base class for logic error conditions.
*/
class lmdb::logic_error : public lmdb::error {
public:
using error::error;
};
/**
* Base class for fatal error conditions.
*/
class lmdb::fatal_error : public lmdb::error {
public:
using error::error;
};
/**
* Base class for runtime error conditions.
*/
class lmdb::runtime_error : public lmdb::error {
public:
using error::error;
};
/**
* Exception class for `MDB_KEYEXIST` errors.
*
* @see http://symas.com/mdb/doc/group__errors.html#ga05dc5bbcc7da81a7345bd8676e8e0e3b
*/
class lmdb::key_exist_error final : public lmdb::runtime_error {
public:
using runtime_error::runtime_error;
};
/**
* Exception class for `MDB_NOTFOUND` errors.
*
* @see http://symas.com/mdb/doc/group__errors.html#gabeb52e4c4be21b329e31c4add1b71926
*/
class lmdb::not_found_error final : public lmdb::runtime_error {
public:
using runtime_error::runtime_error;
};
/**
* Exception class for `MDB_CORRUPTED` errors.
*
* @see http://symas.com/mdb/doc/group__errors.html#gaf8148bf1b85f58e264e57194bafb03ef
*/
class lmdb::corrupted_error final : public lmdb::fatal_error {
public:
using fatal_error::fatal_error;
};
/**
* Exception class for `MDB_PANIC` errors.
*
* @see http://symas.com/mdb/doc/group__errors.html#gae37b9aedcb3767faba3de8c1cf6d3473
*/
class lmdb::panic_error final : public lmdb::fatal_error {
public:
using fatal_error::fatal_error;
};
/**
* Exception class for `MDB_VERSION_MISMATCH` errors.
*
* @see http://symas.com/mdb/doc/group__errors.html#ga909b2db047fa90fb0d37a78f86a6f99b
*/
class lmdb::version_mismatch_error final : public lmdb::fatal_error {
public:
using fatal_error::fatal_error;
};
/**
* Exception class for `MDB_MAP_FULL` errors.
*
* @see http://symas.com/mdb/doc/group__errors.html#ga0a83370402a060c9175100d4bbfb9f25
*/
class lmdb::map_full_error final : public lmdb::runtime_error {
public:
using runtime_error::runtime_error;
};
/**
* Exception class for `MDB_BAD_DBI` errors.
*
* @since 0.9.14 (2014/09/20)
* @see http://symas.com/mdb/doc/group__errors.html#gab4c82e050391b60a18a5df08d22a7083
*/
class lmdb::bad_dbi_error final : public lmdb::runtime_error {
public:
using runtime_error::runtime_error;
};
inline void
lmdb::error::raise(const char* const origin,
const int rc) {
switch (rc) {
case MDB_KEYEXIST: throw key_exist_error{origin, rc};
case MDB_NOTFOUND: throw not_found_error{origin, rc};
case MDB_CORRUPTED: throw corrupted_error{origin, rc};
case MDB_PANIC: throw panic_error{origin, rc};
case MDB_VERSION_MISMATCH: throw version_mismatch_error{origin, rc};
case MDB_MAP_FULL: throw map_full_error{origin, rc};
#ifdef MDB_BAD_DBI
case MDB_BAD_DBI: throw bad_dbi_error{origin, rc};
#endif
default: throw lmdb::runtime_error{origin, rc};
}
}
////////////////////////////////////////////////////////////////////////////////
/* Procedural Interface: Metadata */
namespace lmdb {
// TODO: mdb_version()
// TODO: mdb_strerror()
}
////////////////////////////////////////////////////////////////////////////////
/* Procedural Interface: Environment */
namespace lmdb {
static inline void env_create(MDB_env** env);
static inline void env_open(MDB_env* env,
const char* path, unsigned int flags, mode mode);
#if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 14)
static inline void env_copy(MDB_env* env, const char* path, unsigned int flags);
static inline void env_copy_fd(MDB_env* env, mdb_filehandle_t fd, unsigned int flags);
#else
static inline void env_copy(MDB_env* env, const char* path);
static inline void env_copy_fd(MDB_env* env, mdb_filehandle_t fd);
#endif
static inline void env_stat(MDB_env* env, MDB_stat* stat);
static inline void env_info(MDB_env* env, MDB_envinfo* stat);
static inline void env_sync(MDB_env* env, bool force);
static inline void env_close(MDB_env* env) noexcept;
static inline void env_set_flags(MDB_env* env, unsigned int flags, bool onoff);
static inline void env_get_flags(MDB_env* env, unsigned int* flags);
static inline void env_get_path(MDB_env* env, const char** path);
static inline void env_get_fd(MDB_env* env, mdb_filehandle_t* fd);
static inline void env_set_mapsize(MDB_env* env, std::size_t size);
static inline void env_set_max_readers(MDB_env* env, unsigned int count);
static inline void env_get_max_readers(MDB_env* env, unsigned int* count);
static inline void env_set_max_dbs(MDB_env* env, MDB_dbi count);
static inline unsigned int env_get_max_keysize(MDB_env* env);
#if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 11)
static inline void env_set_userctx(MDB_env* env, void* ctx);
static inline void* env_get_userctx(MDB_env* env);
#endif
// TODO: mdb_env_set_assert()
// TODO: mdb_reader_list()
// TODO: mdb_reader_check()
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#gaad6be3d8dcd4ea01f8df436f41d158d4
*/
static inline void
lmdb::env_create(MDB_env** env) {
const int rc = ::mdb_env_create(env);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_create", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga32a193c6bf4d7d5c5d579e71f22e9340
*/
static inline void
lmdb::env_open(MDB_env* const env,
const char* const path,
const unsigned int flags,
const mode mode) {
const int rc = ::mdb_env_open(env, path, flags, mode);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_open", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga3bf50d7793b36aaddf6b481a44e24244
* @see http://symas.com/mdb/doc/group__mdb.html#ga5d51d6130325f7353db0955dbedbc378
*/
static inline void
lmdb::env_copy(MDB_env* const env,
#if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 14)
const char* const path,
const unsigned int flags = 0) {
const int rc = ::mdb_env_copy2(env, path, flags);
#else
const char* const path) {
const int rc = ::mdb_env_copy(env, path);
#endif
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_copy2", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga5040d0de1f14000fa01fc0b522ff1f86
* @see http://symas.com/mdb/doc/group__mdb.html#ga470b0bcc64ac417de5de5930f20b1a28
*/
static inline void
lmdb::env_copy_fd(MDB_env* const env,
#if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 14)
const mdb_filehandle_t fd,
const unsigned int flags = 0) {
const int rc = ::mdb_env_copyfd2(env, fd, flags);
#else
const mdb_filehandle_t fd) {
const int rc = ::mdb_env_copyfd(env, fd);
#endif
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_copyfd2", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#gaf881dca452050efbd434cd16e4bae255
*/
static inline void
lmdb::env_stat(MDB_env* const env,
MDB_stat* const stat) {
const int rc = ::mdb_env_stat(env, stat);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_stat", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga18769362c7e7d6cf91889a028a5c5947
*/
static inline void
lmdb::env_info(MDB_env* const env,
MDB_envinfo* const stat) {
const int rc = ::mdb_env_info(env, stat);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_info", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga85e61f05aa68b520cc6c3b981dba5037
*/
static inline void
lmdb::env_sync(MDB_env* const env,
const bool force = true) {
const int rc = ::mdb_env_sync(env, force);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_sync", rc);
}
}
/**
* @see http://symas.com/mdb/doc/group__mdb.html#ga4366c43ada8874588b6a62fbda2d1e95
*/
static inline void
lmdb::env_close(MDB_env* const env) noexcept {
::mdb_env_close(env);
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga83f66cf02bfd42119451e9468dc58445
*/
static inline void
lmdb::env_set_flags(MDB_env* const env,
const unsigned int flags,
const bool onoff = true) {
const int rc = ::mdb_env_set_flags(env, flags, onoff ? 1 : 0);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_set_flags", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga2733aefc6f50beb49dd0c6eb19b067d9
*/
static inline void
lmdb::env_get_flags(MDB_env* const env,
unsigned int* const flags) {
const int rc = ::mdb_env_get_flags(env, flags);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_get_flags", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#gac699fdd8c4f8013577cb933fb6a757fe
*/
static inline void
lmdb::env_get_path(MDB_env* const env,
const char** path) {
const int rc = ::mdb_env_get_path(env, path);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_get_path", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#gaf1570e7c0e5a5d860fef1032cec7d5f2
*/
static inline void
lmdb::env_get_fd(MDB_env* const env,
mdb_filehandle_t* const fd) {
const int rc = ::mdb_env_get_fd(env, fd);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_get_fd", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#gaa2506ec8dab3d969b0e609cd82e619e5
*/
static inline void
lmdb::env_set_mapsize(MDB_env* const env,
const std::size_t size) {
const int rc = ::mdb_env_set_mapsize(env, size);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_set_mapsize", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#gae687966c24b790630be2a41573fe40e2
*/
static inline void
lmdb::env_set_max_readers(MDB_env* const env,
const unsigned int count) {
const int rc = ::mdb_env_set_maxreaders(env, count);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_set_maxreaders", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga70e143cf11760d869f754c9c9956e6cc
*/
static inline void
lmdb::env_get_max_readers(MDB_env* const env,
unsigned int* const count) {
const int rc = ::mdb_env_get_maxreaders(env, count);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_get_maxreaders", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#gaa2fc2f1f37cb1115e733b62cab2fcdbc
*/
static inline void
lmdb::env_set_max_dbs(MDB_env* const env,
const MDB_dbi count) {
const int rc = ::mdb_env_set_maxdbs(env, count);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_set_maxdbs", rc);
}
}
/**
* @see http://symas.com/mdb/doc/group__mdb.html#gaaf0be004f33828bf2fb09d77eb3cef94
*/
static inline unsigned int
lmdb::env_get_max_keysize(MDB_env* const env) {
const int rc = ::mdb_env_get_maxkeysize(env);
#ifdef LMDBXX_DEBUG
assert(rc >= 0);
#endif
return static_cast<unsigned int>(rc);
}
#if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 11)
/**
* @throws lmdb::error on failure
* @since 0.9.11 (2014/01/15)
* @see http://symas.com/mdb/doc/group__mdb.html#gaf2fe09eb9c96eeb915a76bf713eecc46
*/
static inline void
lmdb::env_set_userctx(MDB_env* const env,
void* const ctx) {
const int rc = ::mdb_env_set_userctx(env, ctx);
if (rc != MDB_SUCCESS) {
error::raise("mdb_env_set_userctx", rc);
}
}
#endif
#if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 11)
/**
* @since 0.9.11 (2014/01/15)
* @see http://symas.com/mdb/doc/group__mdb.html#ga45df6a4fb150cda2316b5ae224ba52f1
*/
static inline void*
lmdb::env_get_userctx(MDB_env* const env) {
return ::mdb_env_get_userctx(env);
}
#endif
////////////////////////////////////////////////////////////////////////////////
/* Procedural Interface: Transactions */
namespace lmdb {
static inline void txn_begin(
MDB_env* env, MDB_txn* parent, unsigned int flags, MDB_txn** txn);
static inline MDB_env* txn_env(MDB_txn* txn) noexcept;
#ifdef LMDBXX_TXN_ID
static inline std::size_t txn_id(MDB_txn* txn) noexcept;
#endif
static inline void txn_commit(MDB_txn* txn);
static inline void txn_abort(MDB_txn* txn) noexcept;
static inline void txn_reset(MDB_txn* txn) noexcept;
static inline void txn_renew(MDB_txn* txn);
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#gad7ea55da06b77513609efebd44b26920
*/
static inline void
lmdb::txn_begin(MDB_env* const env,
MDB_txn* const parent,
const unsigned int flags,
MDB_txn** txn) {
const int rc = ::mdb_txn_begin(env, parent, flags, txn);
if (rc != MDB_SUCCESS) {
error::raise("mdb_txn_begin", rc);
}
}
/**
* @see http://symas.com/mdb/doc/group__mdb.html#gaeb17735b8aaa2938a78a45cab85c06a0
*/
static inline MDB_env*
lmdb::txn_env(MDB_txn* const txn) noexcept {
return ::mdb_txn_env(txn);
}
#ifdef LMDBXX_TXN_ID
/**
* @note Only available in HEAD, not yet in any 0.9.x release (as of 0.9.16).
*/
static inline std::size_t
lmdb::txn_id(MDB_txn* const txn) noexcept {
return ::mdb_txn_id(txn);
}
#endif
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga846fbd6f46105617ac9f4d76476f6597
*/
static inline void
lmdb::txn_commit(MDB_txn* const txn) {
const int rc = ::mdb_txn_commit(txn);
if (rc != MDB_SUCCESS) {
error::raise("mdb_txn_commit", rc);
}
}
/**
* @see http://symas.com/mdb/doc/group__mdb.html#ga73a5938ae4c3239ee11efa07eb22b882
*/
static inline void
lmdb::txn_abort(MDB_txn* const txn) noexcept {
::mdb_txn_abort(txn);
}
/**
* @see http://symas.com/mdb/doc/group__mdb.html#ga02b06706f8a66249769503c4e88c56cd
*/
static inline void
lmdb::txn_reset(MDB_txn* const txn) noexcept {
::mdb_txn_reset(txn);
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga6c6f917959517ede1c504cf7c720ce6d
*/
static inline void
lmdb::txn_renew(MDB_txn* const txn) {
const int rc = ::mdb_txn_renew(txn);
if (rc != MDB_SUCCESS) {
error::raise("mdb_txn_renew", rc);
}
}
////////////////////////////////////////////////////////////////////////////////
/* Procedural Interface: Databases */
namespace lmdb {
static inline void dbi_open(
MDB_txn* txn, const char* name, unsigned int flags, MDB_dbi* dbi);
static inline void dbi_stat(MDB_txn* txn, MDB_dbi dbi, MDB_stat* stat);
static inline void dbi_flags(MDB_txn* txn, MDB_dbi dbi, unsigned int* flags);
static inline void dbi_close(MDB_env* env, MDB_dbi dbi) noexcept;
static inline void dbi_drop(MDB_txn* txn, MDB_dbi dbi, bool del);
static inline void dbi_set_compare(MDB_txn* txn, MDB_dbi dbi, MDB_cmp_func* cmp);
static inline void dbi_set_dupsort(MDB_txn* txn, MDB_dbi dbi, MDB_cmp_func* cmp);
static inline void dbi_set_relfunc(MDB_txn* txn, MDB_dbi dbi, MDB_rel_func* rel);
static inline void dbi_set_relctx(MDB_txn* txn, MDB_dbi dbi, void* ctx);
static inline bool dbi_get(MDB_txn* txn, MDB_dbi dbi, const MDB_val* key, MDB_val* data);
static inline bool dbi_put(MDB_txn* txn, MDB_dbi dbi, const MDB_val* key, MDB_val* data, unsigned int flags);
static inline bool dbi_del(MDB_txn* txn, MDB_dbi dbi, const MDB_val* key, const MDB_val* data);
// TODO: mdb_cmp()
// TODO: mdb_dcmp()
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#gac08cad5b096925642ca359a6d6f0562a
*/
static inline void
lmdb::dbi_open(MDB_txn* const txn,
const char* const name,
const unsigned int flags,
MDB_dbi* const dbi) {
const int rc = ::mdb_dbi_open(txn, name, flags, dbi);
if (rc != MDB_SUCCESS) {
error::raise("mdb_dbi_open", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#gae6c1069febe94299769dbdd032fadef6
*/
static inline void
lmdb::dbi_stat(MDB_txn* const txn,
const MDB_dbi dbi,
MDB_stat* const result) {
const int rc = ::mdb_stat(txn, dbi, result);
if (rc != MDB_SUCCESS) {
error::raise("mdb_stat", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga95ba4cb721035478a8705e57b91ae4d4
*/
static inline void
lmdb::dbi_flags(MDB_txn* const txn,
const MDB_dbi dbi,
unsigned int* const flags) {
const int rc = ::mdb_dbi_flags(txn, dbi, flags);
if (rc != MDB_SUCCESS) {
error::raise("mdb_dbi_flags", rc);
}
}
/**
* @see http://symas.com/mdb/doc/group__mdb.html#ga52dd98d0c542378370cd6b712ff961b5
*/
static inline void
lmdb::dbi_close(MDB_env* const env,
const MDB_dbi dbi) noexcept {
::mdb_dbi_close(env, dbi);
}
/**
* @see http://symas.com/mdb/doc/group__mdb.html#gab966fab3840fc54a6571dfb32b00f2db
*/
static inline void
lmdb::dbi_drop(MDB_txn* const txn,
const MDB_dbi dbi,
const bool del = false) {
const int rc = ::mdb_drop(txn, dbi, del ? 1 : 0);
if (rc != MDB_SUCCESS) {
error::raise("mdb_drop", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga68e47ffcf72eceec553c72b1784ee0fe
*/
static inline void
lmdb::dbi_set_compare(MDB_txn* const txn,
const MDB_dbi dbi,
MDB_cmp_func* const cmp = nullptr) {
const int rc = ::mdb_set_compare(txn, dbi, cmp);
if (rc != MDB_SUCCESS) {
error::raise("mdb_set_compare", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#gacef4ec3dab0bbd9bc978b73c19c879ae
*/
static inline void
lmdb::dbi_set_dupsort(MDB_txn* const txn,
const MDB_dbi dbi,
MDB_cmp_func* const cmp = nullptr) {
const int rc = ::mdb_set_dupsort(txn, dbi, cmp);
if (rc != MDB_SUCCESS) {
error::raise("mdb_set_dupsort", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga697d82c7afe79f142207ad5adcdebfeb
*/
static inline void
lmdb::dbi_set_relfunc(MDB_txn* const txn,
const MDB_dbi dbi,
MDB_rel_func* const rel) {
const int rc = ::mdb_set_relfunc(txn, dbi, rel);
if (rc != MDB_SUCCESS) {
error::raise("mdb_set_relfunc", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga7c34246308cee01724a1839a8f5cc594
*/
static inline void
lmdb::dbi_set_relctx(MDB_txn* const txn,
const MDB_dbi dbi,
void* const ctx) {
const int rc = ::mdb_set_relctx(txn, dbi, ctx);
if (rc != MDB_SUCCESS) {
error::raise("mdb_set_relctx", rc);
}
}
/**
* @retval true if the key/value pair was retrieved
* @retval false if the key wasn't found
* @see http://symas.com/mdb/doc/group__mdb.html#ga8bf10cd91d3f3a83a34d04ce6b07992d
*/
static inline bool
lmdb::dbi_get(MDB_txn* const txn,
const MDB_dbi dbi,
const MDB_val* const key,
MDB_val* const data) {
const int rc = ::mdb_get(txn, dbi, const_cast<MDB_val*>(key), data);
if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND) {
error::raise("mdb_get", rc);
}
return (rc == MDB_SUCCESS);
}
/**
* @retval true if the key/value pair was inserted
* @retval false if the key already existed
* @see http://symas.com/mdb/doc/group__mdb.html#ga4fa8573d9236d54687c61827ebf8cac0
*/
static inline bool
lmdb::dbi_put(MDB_txn* const txn,
const MDB_dbi dbi,
const MDB_val* const key,
MDB_val* const data,
const unsigned int flags = 0) {
const int rc = ::mdb_put(txn, dbi, const_cast<MDB_val*>(key), data, flags);
if (rc != MDB_SUCCESS && rc != MDB_KEYEXIST) {
error::raise("mdb_put", rc);
}
return (rc == MDB_SUCCESS);
}
/**
* @retval true if the key/value pair was removed
* @retval false if the key wasn't found
* @see http://symas.com/mdb/doc/group__mdb.html#gab8182f9360ea69ac0afd4a4eaab1ddb0
*/
static inline bool
lmdb::dbi_del(MDB_txn* const txn,
const MDB_dbi dbi,
const MDB_val* const key,
const MDB_val* const data = nullptr) {
const int rc = ::mdb_del(txn, dbi, const_cast<MDB_val*>(key), const_cast<MDB_val*>(data));
if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND) {
error::raise("mdb_del", rc);
}
return (rc == MDB_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
/* Procedural Interface: Cursors */
namespace lmdb {
static inline void cursor_open(MDB_txn* txn, MDB_dbi dbi, MDB_cursor** cursor);
static inline void cursor_close(MDB_cursor* cursor) noexcept;
static inline void cursor_renew(MDB_txn* txn, MDB_cursor* cursor);
static inline MDB_txn* cursor_txn(MDB_cursor* cursor) noexcept;
static inline MDB_dbi cursor_dbi(MDB_cursor* cursor) noexcept;
static inline bool cursor_get(MDB_cursor* cursor, MDB_val* key, MDB_val* data, MDB_cursor_op op);
static inline void cursor_put(MDB_cursor* cursor, MDB_val* key, MDB_val* data, unsigned int flags);
static inline void cursor_del(MDB_cursor* cursor, unsigned int flags);
static inline void cursor_count(MDB_cursor* cursor, std::size_t& count);
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga9ff5d7bd42557fd5ee235dc1d62613aa
*/
static inline void
lmdb::cursor_open(MDB_txn* const txn,
const MDB_dbi dbi,
MDB_cursor** const cursor) {
const int rc = ::mdb_cursor_open(txn, dbi, cursor);
if (rc != MDB_SUCCESS) {
error::raise("mdb_cursor_open", rc);
}
}
/**
* @see http://symas.com/mdb/doc/group__mdb.html#gad685f5d73c052715c7bd859cc4c05188
*/
static inline void
lmdb::cursor_close(MDB_cursor* const cursor) noexcept {
::mdb_cursor_close(cursor);
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#gac8b57befb68793070c85ea813df481af
*/
static inline void
lmdb::cursor_renew(MDB_txn* const txn,
MDB_cursor* const cursor) {
const int rc = ::mdb_cursor_renew(txn, cursor);
if (rc != MDB_SUCCESS) {
error::raise("mdb_cursor_renew", rc);
}
}
/**
* @see http://symas.com/mdb/doc/group__mdb.html#ga7bf0d458f7f36b5232fcb368ebda79e0
*/
static inline MDB_txn*
lmdb::cursor_txn(MDB_cursor* const cursor) noexcept {
return ::mdb_cursor_txn(cursor);
}
/**
* @see http://symas.com/mdb/doc/group__mdb.html#ga2f7092cf70ee816fb3d2c3267a732372
*/
static inline MDB_dbi
lmdb::cursor_dbi(MDB_cursor* const cursor) noexcept {
return ::mdb_cursor_dbi(cursor);
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga48df35fb102536b32dfbb801a47b4cb0
*/
static inline bool
lmdb::cursor_get(MDB_cursor* const cursor,
MDB_val* const key,
MDB_val* const data,
const MDB_cursor_op op) {
const int rc = ::mdb_cursor_get(cursor, key, data, op);
if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND) {
error::raise("mdb_cursor_get", rc);
}
return (rc == MDB_SUCCESS);
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga1f83ccb40011837ff37cc32be01ad91e
*/
static inline void
lmdb::cursor_put(MDB_cursor* const cursor,
MDB_val* const key,
MDB_val* const data,
const unsigned int flags = 0) {
const int rc = ::mdb_cursor_put(cursor, key, data, flags);
if (rc != MDB_SUCCESS) {
error::raise("mdb_cursor_put", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga26a52d3efcfd72e5bf6bd6960bf75f95
*/
static inline void
lmdb::cursor_del(MDB_cursor* const cursor,
const unsigned int flags = 0) {
const int rc = ::mdb_cursor_del(cursor, flags);
if (rc != MDB_SUCCESS) {
error::raise("mdb_cursor_del", rc);
}
}
/**
* @throws lmdb::error on failure
* @see http://symas.com/mdb/doc/group__mdb.html#ga4041fd1e1862c6b7d5f10590b86ffbe2
*/
static inline void
lmdb::cursor_count(MDB_cursor* const cursor,
std::size_t& count) {
const int rc = ::mdb_cursor_count(cursor, &count);
if (rc != MDB_SUCCESS) {
error::raise("mdb_cursor_count", rc);
}
}
////////////////////////////////////////////////////////////////////////////////
/* Resource Interface: Values */
namespace lmdb {
class val;
}
/**
* Wrapper class for `MDB_val` structures.
*
* @note Instances of this class are movable and copyable both.
* @see http://symas.com/mdb/doc/group__mdb.html#structMDB__val
*/
class lmdb::val {
protected:
MDB_val _val;
public:
/**
* Default constructor.
*/
val() noexcept = default;
/**
* Constructor.
*/
val(const std::string& data) noexcept
: val{data.data(), data.size()} {}
/**
* Constructor.
*/
val(const char* const data) noexcept
: val{data, std::strlen(data)} {}
/**
* Constructor.
*/
val(const void* const data,
const std::size_t size) noexcept
: _val{size, const_cast<void*>(data)} {}
/**
* Move constructor.
*/
val(val&& other) noexcept = default;
/**
* Move assignment operator.
*/
val& operator=(val&& other) noexcept = default;
/**
* Destructor.
*/
~val() noexcept = default;
/**
* Returns an `MDB_val*` pointer.
*/
operator MDB_val*() noexcept {
return &_val;
}
/**
* Returns an `MDB_val*` pointer.
*/
operator const MDB_val*() const noexcept {
return &_val;
}
/**
* Determines whether this value is empty.
*/
bool empty() const noexcept {