-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mdbx.h
5327 lines (4915 loc) · 240 KB
/
mdbx.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
/**
_libmdbx_ is an extremely fast, compact, powerful, embedded,
transactional [key-value
store](https://en.wikipedia.org/wiki/Key-value_database) database, with
[permissive license](./LICENSE). _MDBX_ has a specific set of properties and
capabilities, focused on creating unique lightweight solutions with
extraordinary performance.
_libmdbx_ is superior to [LMDB](https://bit.ly/26ts7tL) in terms of features
and reliability, not inferior in performance. In comparison to LMDB, _libmdbx_
makes many things just work perfectly, not silently and catastrophically
break down. _libmdbx_ supports Linux, Windows, MacOS, OSX, iOS, Android,
FreeBSD, DragonFly, Solaris, OpenSolaris, OpenIndiana, NetBSD, OpenBSD and other
systems compliant with POSIX.1-2008.
_The Future will (be) [Positive](https://www.ptsecurity.com). Всё будет хорошо._
\section copyright LICENSE & COPYRIGHT
\authors Copyright (c) 2015-2022, Leonid Yuriev <leo@yuriev.ru>
and other _libmdbx_ authors: please see [AUTHORS](./AUTHORS) file.
\copyright Redistribution and use in source and binary forms, with or without
modification, are permitted only as authorized by the OpenLDAP Public License.
A copy of this license is available in the file LICENSE in the
top-level directory of the distribution or, alternatively, at
<http://www.OpenLDAP.org/license.html>.
---
This code is derived from "LMDB engine" written by
Howard Chu (Symas Corporation), which itself derived from btree.c
written by Martin Hedenfalk.
---
Portions Copyright 2011-2015 Howard Chu, Symas Corp. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted only as authorized by the OpenLDAP
Public License.
A copy of this license is available in the file LICENSE in the
top-level directory of the distribution or, alternatively, at
<http://www.OpenLDAP.org/license.html>.
---
Portions Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*******************************************************************************/
#pragma once
#ifndef LIBMDBX_H
#define LIBMDBX_H
#ifdef _MSC_VER
#pragma warning(push, 1)
#pragma warning(disable : 4548) /* expression before comma has no effect; \
expected expression with side - effect */
#pragma warning(disable : 4530) /* C++ exception handler used, but unwind \
* semantics are not enabled. Specify /EHsc */
#pragma warning(disable : 4577) /* 'noexcept' used with no exception handling \
* mode specified; termination on exception is \
* not guaranteed. Specify /EHsc */
#endif /* _MSC_VER (warnings) */
/* *INDENT-OFF* */
/* clang-format off */
/**
\file mdbx.h
\brief The libmdbx C API header file
\defgroup c_api C API
@{
\defgroup c_err Error handling
\defgroup c_opening Opening & Closing
\defgroup c_transactions Transactions
\defgroup c_dbi Databases
\defgroup c_crud Create/Read/Update/Delete (see Quick Reference in details)
\details
\anchor c_crud_hints
# Quick Reference for Insert/Update/Delete operations
Historically, libmdbx inherits the API basis from LMDB, where it is often
difficult to select flags/options and functions for the desired operation.
So it is recommend using this hints.
## Databases with UNIQUE keys
In databases created without the \ref MDBX_DUPSORT option, keys are always
unique. Thus always a single value corresponds to the each key, and so there
are only a few cases of changing data.
| Case | Flags to use | Result |
|---------------------------------------------|---------------------|------------------------|
| _INSERTING_|||
|Key is absent → Insertion |\ref MDBX_NOOVERWRITE|Insertion |
|Key exist → Error since key present |\ref MDBX_NOOVERWRITE|Error \ref MDBX_KEYEXIST and return Present value|
| _UPSERTING_|||
|Key is absent → Insertion |\ref MDBX_UPSERT |Insertion |
|Key exist → Update |\ref MDBX_UPSERT |Update |
| _UPDATING_|||
|Key is absent → Error since no such key |\ref MDBX_CURRENT |Error \ref MDBX_NOTFOUND|
|Key exist → Update |\ref MDBX_CURRENT |Update value |
| _DELETING_|||
|Key is absent → Error since no such key |\ref mdbx_del() or \ref mdbx_replace()|Error \ref MDBX_NOTFOUND|
|Key exist → Delete by key |\ref mdbx_del() with the parameter `data = NULL`|Deletion|
|Key exist → Delete by key with with data matching check|\ref mdbx_del() with the parameter `data` filled with the value which should be match for deletion|Deletion or \ref MDBX_NOTFOUND if the value does not match|
|Delete at the current cursor position |\ref mdbx_cursor_del() with \ref MDBX_CURRENT flag|Deletion|
|Extract (read & delete) value by the key |\ref mdbx_replace() with zero flag and parameter `new_data = NULL`|Returning a deleted value|
## Databases with NON-UNIQUE keys
In databases created with the \ref MDBX_DUPSORT (Sorted Duplicates) option, keys
may be non unique. Such non-unique keys in a key-value database may be treated
as a duplicates or as like a multiple values corresponds to keys.
| Case | Flags to use | Result |
|---------------------------------------------|---------------------|------------------------|
| _INSERTING_|||
|Key is absent → Insertion |\ref MDBX_NOOVERWRITE|Insertion|
|Key exist → Needn't to add new values |\ref MDBX_NOOVERWRITE|Error \ref MDBX_KEYEXIST with returning the first value from those already present|
| _UPSERTING_|||
|Key is absent → Insertion |\ref MDBX_UPSERT |Insertion|
|Key exist → Wanna to add new values |\ref MDBX_UPSERT |Add one more value to the key|
|Key exist → Replace all values with a new one|\ref MDBX_UPSERT + \ref MDBX_ALLDUPS|Overwrite by single new value|
| _UPDATING_|||
|Key is absent → Error since no such key |\ref MDBX_CURRENT |Error \ref MDBX_NOTFOUND|
|Key exist, Single value → Update |\ref MDBX_CURRENT |Update single value |
|Key exist, Multiple values → Replace all values with a new one|\ref MDBX_CURRENT + \ref MDBX_ALLDUPS|Overwrite by single new value|
|Key exist, Multiple values → Error since it is unclear which of the values should be updated|\ref mdbx_put() with \ref MDBX_CURRENT|Error \ref MDBX_EMULTIVAL|
|Key exist, Multiple values → Update particular entry of multi-value|\ref mdbx_replace() with \ref MDBX_CURRENT + \ref MDBX_NOOVERWRITE and the parameter `old_value` filled with the value that wanna to update|Update one multi-value entry|
|Key exist, Multiple values → Update the current entry of multi-value|\ref mdbx_cursor_put() with \ref MDBX_CURRENT|Update one multi-value entry|
| _DELETING_|||
|Key is absent → Error since no such key |\ref mdbx_del() or \ref mdbx_replace()|Error \ref MDBX_NOTFOUND|
|Key exist → Delete all values corresponds given key|\ref mdbx_del() with the parameter `data = NULL`|Deletion|
|Key exist → Delete particular value corresponds given key|\ref mdbx_del() with the parameter `data` filled with the value that wanna to delete, or \ref mdbx_replace() with \ref MDBX_CURRENT + \ref MDBX_NOOVERWRITE and the `old_value` parameter filled with the value that wanna to delete and `new_data = NULL`| Deletion or \ref MDBX_NOTFOUND if no such key-value pair|
|Delete one value at the current cursor position|\ref mdbx_cursor_del() with \ref MDBX_CURRENT flag|Deletion only the current entry|
|Delete all values of key at the current cursor position|\ref mdbx_cursor_del() with with \ref MDBX_ALLDUPS flag|Deletion all duplicates of key (all multi-values) at the current cursor position|
\defgroup c_cursors Cursors
\defgroup c_statinfo Statistics & Information
\defgroup c_settings Settings
\defgroup c_debug Logging and runtime debug
\defgroup c_rqest Range query estimation
\defgroup c_extra Extra operations
*/
/* *INDENT-ON* */
/* clang-format on */
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#if !defined(NDEBUG) && !defined(assert)
#include <assert.h>
#endif /* NDEBUG */
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <winnt.h>
#ifndef __mode_t_defined
typedef unsigned short mdbx_mode_t;
#else
typedef mode_t mdbx_mode_t;
#endif /* __mode_t_defined */
typedef HANDLE mdbx_filehandle_t;
typedef DWORD mdbx_pid_t;
typedef DWORD mdbx_tid_t;
#else /* Windows */
#include <errno.h> /* for error codes */
#include <pthread.h> /* for pthread_t */
#include <sys/types.h> /* for pid_t */
#include <sys/uio.h> /* for struct iovec */
#define HAVE_STRUCT_IOVEC 1
typedef int mdbx_filehandle_t;
typedef pid_t mdbx_pid_t;
typedef pthread_t mdbx_tid_t;
typedef mode_t mdbx_mode_t;
#endif /* !Windows */
#ifdef _MSC_VER
#pragma warning(pop)
#endif
/** @} close c_api
* \defgroup api_macros Common Macros
* @{ */
/*----------------------------------------------------------------------------*/
#ifndef __has_attribute
#define __has_attribute(x) (0)
#endif /* __has_attribute */
#ifndef __has_cpp_attribute
#define __has_cpp_attribute(x) 0
#endif /* __has_cpp_attribute */
#ifndef __has_feature
#define __has_feature(x) (0)
#endif /* __has_feature */
#ifndef __has_extension
#define __has_extension(x) (0)
#endif /* __has_extension */
#ifndef __has_builtin
#define __has_builtin(x) (0)
#endif /* __has_builtin */
/** Many functions have no effects except the return value and their
* return value depends only on the parameters and/or global variables.
* Such a function can be subject to common subexpression elimination
* and loop optimization just as an arithmetic operator would be.
* These functions should be declared with the attribute pure. */
#if (defined(__GNUC__) || __has_attribute(__pure__)) && \
(!defined(__clang__) /* https://bugs.llvm.org/show_bug.cgi?id=43275 */ \
|| !defined(__cplusplus) || !__has_feature(cxx_exceptions))
#define MDBX_PURE_FUNCTION __attribute__((__pure__))
#elif defined(_MSC_VER) && !defined(__clang__) && _MSC_VER >= 1920
#define MDBX_PURE_FUNCTION
#elif defined(__cplusplus) && __has_cpp_attribute(gnu::pure) && \
(!defined(__clang__) || !__has_feature(cxx_exceptions))
#define MDBX_PURE_FUNCTION [[gnu::pure]]
#else
#define MDBX_PURE_FUNCTION
#endif /* MDBX_PURE_FUNCTION */
/** Like \ref MDBX_PURE_FUNCTION with addition `noexcept` restriction
* that is compatible to CLANG and proposed [[pure]]. */
#if defined(__GNUC__) || \
(__has_attribute(__pure__) && __has_attribute(__nothrow__))
#define MDBX_NOTHROW_PURE_FUNCTION __attribute__((__pure__, __nothrow__))
#elif defined(_MSC_VER) && !defined(__clang__) && _MSC_VER >= 1920
#if __has_cpp_attribute(pure)
#define MDBX_NOTHROW_PURE_FUNCTION [[pure]]
#else
#define MDBX_NOTHROW_PURE_FUNCTION
#endif
#elif defined(__cplusplus) && __has_cpp_attribute(gnu::pure)
#if __has_cpp_attribute(gnu::nothrow)
#define MDBX_NOTHROW_PURE_FUNCTION [[gnu::pure, gnu::nothrow]]
#else
#define MDBX_NOTHROW_PURE_FUNCTION [[gnu::pure]]
#endif
#elif defined(__cplusplus) && __has_cpp_attribute(pure)
#define MDBX_NOTHROW_PURE_FUNCTION [[pure]]
#else
#define MDBX_NOTHROW_PURE_FUNCTION
#endif /* MDBX_NOTHROW_PURE_FUNCTION */
/** Many functions do not examine any values except their arguments,
* and have no effects except the return value. Basically this is just
* slightly more strict class than the PURE attribute, since function
* is not allowed to read global memory.
*
* Note that a function that has pointer arguments and examines the
* data pointed to must not be declared const. Likewise, a function
* that calls a non-const function usually must not be const.
* It does not make sense for a const function to return void. */
#if (defined(__GNUC__) || __has_attribute(__pure__)) && \
(!defined(__clang__) /* https://bugs.llvm.org/show_bug.cgi?id=43275 */ \
|| !defined(__cplusplus) || !__has_feature(cxx_exceptions))
#define MDBX_CONST_FUNCTION __attribute__((__const__))
#elif defined(_MSC_VER) && !defined(__clang__) && _MSC_VER >= 1920
#define MDBX_CONST_FUNCTION MDBX_PURE_FUNCTION
#elif defined(__cplusplus) && __has_cpp_attribute(gnu::const) && \
(!defined(__clang__) || !__has_feature(cxx_exceptions))
#define MDBX_CONST_FUNCTION [[gnu::const]]
#else
#define MDBX_CONST_FUNCTION MDBX_PURE_FUNCTION
#endif /* MDBX_CONST_FUNCTION */
/** Like \ref MDBX_CONST_FUNCTION with addition `noexcept` restriction
* that is compatible to CLANG and future [[const]]. */
#if defined(__GNUC__) || \
(__has_attribute(__const__) && __has_attribute(__nothrow__))
#define MDBX_NOTHROW_CONST_FUNCTION __attribute__((__const__, __nothrow__))
#elif defined(_MSC_VER) && !defined(__clang__) && _MSC_VER >= 1920
#define MDBX_NOTHROW_CONST_FUNCTION MDBX_NOTHROW_PURE_FUNCTION
#elif defined(__cplusplus) && __has_cpp_attribute(gnu::const)
#if __has_cpp_attribute(gnu::nothrow)
#define MDBX_NOTHROW_PURE_FUNCTION [[gnu::const, gnu::nothrow]]
#else
#define MDBX_NOTHROW_PURE_FUNCTION [[gnu::const]]
#endif
#elif defined(__cplusplus) && __has_cpp_attribute(const)
#define MDBX_NOTHROW_CONST_FUNCTION [[const]]
#else
#define MDBX_NOTHROW_CONST_FUNCTION MDBX_NOTHROW_PURE_FUNCTION
#endif /* MDBX_NOTHROW_CONST_FUNCTION */
#ifndef MDBX_DEPRECATED /* may be predefined to avoid warnings "deprecated" */
#ifdef __deprecated
#define MDBX_DEPRECATED __deprecated
#elif defined(__GNUC__) || __has_attribute(__deprecated__)
#define MDBX_DEPRECATED __attribute__((__deprecated__))
#elif defined(_MSC_VER)
#define MDBX_DEPRECATED __declspec(deprecated)
#else
#define MDBX_DEPRECATED
#endif
#endif /* MDBX_DEPRECATED */
#ifndef __dll_export
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) || \
defined(__MINGW__) || defined(__MINGW32__) || defined(__MINGW64__)
#if defined(__GNUC__) || __has_attribute(__dllexport__)
#define __dll_export __attribute__((__dllexport__))
#elif defined(_MSC_VER)
#define __dll_export __declspec(dllexport)
#else
#define __dll_export
#endif
#elif defined(__GNUC__) || __has_attribute(__visibility__)
#define __dll_export __attribute__((__visibility__("default")))
#else
#define __dll_export
#endif
#endif /* __dll_export */
#ifndef __dll_import
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) || \
defined(__MINGW__) || defined(__MINGW32__) || defined(__MINGW64__)
#if defined(__GNUC__) || __has_attribute(__dllimport__)
#define __dll_import __attribute__((__dllimport__))
#elif defined(_MSC_VER)
#define __dll_import __declspec(dllimport)
#else
#define __dll_import
#endif
#else
#define __dll_import
#endif
#endif /* __dll_import */
/** \brief Auxiliary macro for robustly define the both inline version of API
* function and non-inline fallback dll-exported version for applications linked
* with old version of libmdbx, with a strictly ODR-common implementation. */
#if defined(LIBMDBX_INTERNALS) && !defined(LIBMDBX_NO_EXPORTS_LEGACY_API)
#define LIBMDBX_INLINE_API(TYPE, NAME, ARGS) \
/* proto of exported which uses common impl */ LIBMDBX_API TYPE NAME ARGS; \
/* definition of common impl */ static __inline TYPE __inline_##NAME ARGS
#else
#define LIBMDBX_INLINE_API(TYPE, NAME, ARGS) static __inline TYPE NAME ARGS
#endif /* LIBMDBX_INLINE_API */
/** \brief Converts a macro argument into a string constant. */
#ifndef MDBX_STRINGIFY
#define MDBX_STRINGIFY_HELPER(x) #x
#define MDBX_STRINGIFY(x) MDBX_STRINGIFY_HELPER(x)
#endif /* MDBX_STRINGIFY */
/*----------------------------------------------------------------------------*/
#ifndef __cplusplus
#ifndef bool
#define bool _Bool
#endif
#ifndef true
#define true (1)
#endif
#ifndef false
#define false (0)
#endif
#endif /* bool without __cplusplus */
#if !defined(DOXYGEN) && (!defined(__cpp_noexcept_function_type) || \
__cpp_noexcept_function_type < 201510L)
#define MDBX_CXX17_NOEXCEPT
#else
#define MDBX_CXX17_NOEXCEPT noexcept
#endif /* MDBX_CXX17_NOEXCEPT */
/* Workaround for old compilers without properly support for constexpr. */
#if !defined(__cplusplus)
#define MDBX_CXX01_CONSTEXPR __inline
#define MDBX_CXX01_CONSTEXPR_VAR const
#elif !defined(DOXYGEN) && \
((__cplusplus < 201103L && defined(__cpp_constexpr) && \
__cpp_constexpr < 200704L) || \
(defined(__LCC__) && __LCC__ < 124) || \
(defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ < 407) && \
!defined(__clang__) && !defined(__LCC__)) || \
(defined(_MSC_VER) && _MSC_VER < 1910) || \
(defined(__clang__) && __clang_major__ < 4))
#define MDBX_CXX01_CONSTEXPR inline
#define MDBX_CXX01_CONSTEXPR_VAR const
#else
#define MDBX_CXX01_CONSTEXPR constexpr
#define MDBX_CXX01_CONSTEXPR_VAR constexpr
#endif /* MDBX_CXX01_CONSTEXPR */
#if !defined(__cplusplus)
#define MDBX_CXX11_CONSTEXPR __inline
#define MDBX_CXX11_CONSTEXPR_VAR const
#elif !defined(DOXYGEN) && \
(!defined(__cpp_constexpr) || __cpp_constexpr < 201304L || \
(defined(__LCC__) && __LCC__ < 124) || \
(defined(__GNUC__) && __GNUC__ < 6 && !defined(__clang__) && \
!defined(__LCC__)) || \
(defined(_MSC_VER) && _MSC_VER < 1910) || \
(defined(__clang__) && __clang_major__ < 5))
#define MDBX_CXX11_CONSTEXPR inline
#define MDBX_CXX11_CONSTEXPR_VAR const
#else
#define MDBX_CXX11_CONSTEXPR constexpr
#define MDBX_CXX11_CONSTEXPR_VAR constexpr
#endif /* MDBX_CXX11_CONSTEXPR */
#if !defined(__cplusplus)
#define MDBX_CXX14_CONSTEXPR __inline
#define MDBX_CXX14_CONSTEXPR_VAR const
#elif defined(DOXYGEN) || \
defined(__cpp_constexpr) && __cpp_constexpr >= 201304L && \
((defined(_MSC_VER) && _MSC_VER >= 1910) || \
(defined(__clang__) && __clang_major__ > 4) || \
(defined(__GNUC__) && __GNUC__ > 6) || \
(!defined(__GNUC__) && !defined(__clang__) && !defined(_MSC_VER)))
#define MDBX_CXX14_CONSTEXPR constexpr
#define MDBX_CXX14_CONSTEXPR_VAR constexpr
#else
#define MDBX_CXX14_CONSTEXPR inline
#define MDBX_CXX14_CONSTEXPR_VAR const
#endif /* MDBX_CXX14_CONSTEXPR */
#if defined(__noreturn)
#define MDBX_NORETURN __noreturn
#elif defined(_Noreturn)
#define MDBX_NORETURN _Noreturn
#elif defined(__GNUC__) || __has_attribute(__noreturn__)
#define MDBX_NORETURN __attribute__((__noreturn__))
#elif defined(_MSC_VER) && !defined(__clang__)
#define MDBX_NORETURN __declspec(noreturn)
#else
#define MDBX_NORETURN
#endif /* MDBX_NORETURN */
#ifndef MDBX_PRINTF_ARGS
#if defined(__GNUC__) || __has_attribute(__format__)
#if defined(__MINGW__) || defined(__MINGW32__) || defined(__MINGW64__)
#define MDBX_PRINTF_ARGS(format_index, first_arg) \
__attribute__((__format__(__gnu_printf__, format_index, first_arg)))
#else
#define MDBX_PRINTF_ARGS(format_index, first_arg) \
__attribute__((__format__(__printf__, format_index, first_arg)))
#endif /* MinGW */
#else
#define MDBX_PRINTF_ARGS(format_index, first_arg)
#endif
#endif /* MDBX_PRINTF_ARGS */
#if defined(DOXYGEN) || \
(defined(__cplusplus) && __cplusplus >= 201603 && \
__has_cpp_attribute(maybe_unused) && \
__has_cpp_attribute(maybe_unused) >= 201603) || \
(!defined(__cplusplus) && defined(__STDC_VERSION__) && \
__STDC_VERSION__ > 202005L)
#define MDBX_MAYBE_UNUSED [[maybe_unused]]
#elif defined(__GNUC__) || __has_attribute(__unused__)
#define MDBX_MAYBE_UNUSED __attribute__((__unused__))
#else
#define MDBX_MAYBE_UNUSED
#endif /* MDBX_MAYBE_UNUSED */
#if __has_attribute(no_sanitize)
#define MDBX_NOSANITIZE_ENUM __attribute((__no_sanitize__("enum")))
#else
#define MDBX_NOSANITIZE_ENUM
#endif /* MDBX_NOSANITIZE_ENUM */
/* Oh, below are some songs and dances since:
* - C++ requires explicit definition of the necessary operators.
* - the proper implementation of DEFINE_ENUM_FLAG_OPERATORS for C++ required
* the constexpr feature which is broken in most old compilers;
* - DEFINE_ENUM_FLAG_OPERATORS may be defined broken as in the Windows SDK. */
#ifndef DEFINE_ENUM_FLAG_OPERATORS
#ifdef __cplusplus
#if !defined(__cpp_constexpr) || __cpp_constexpr < 200704L || \
(defined(__LCC__) && __LCC__ < 124) || \
(defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ < 407) && \
!defined(__clang__) && !defined(__LCC__)) || \
(defined(_MSC_VER) && _MSC_VER < 1910) || \
(defined(__clang__) && __clang_major__ < 4)
/* The constexpr feature is not available or (may be) broken */
#define CONSTEXPR_ENUM_FLAGS_OPERATIONS 0
#else
/* C always allows these operators for enums */
#define CONSTEXPR_ENUM_FLAGS_OPERATIONS 1
#endif /* __cpp_constexpr */
/// Define operator overloads to enable bit operations on enum values that are
/// used to define flags (based on Microsoft's DEFINE_ENUM_FLAG_OPERATORS).
#define DEFINE_ENUM_FLAG_OPERATORS(ENUM) \
extern "C++" { \
MDBX_NOSANITIZE_ENUM MDBX_CXX01_CONSTEXPR ENUM operator|(ENUM a, ENUM b) { \
return ENUM(unsigned(a) | unsigned(b)); \
} \
MDBX_NOSANITIZE_ENUM MDBX_CXX14_CONSTEXPR ENUM &operator|=(ENUM &a, \
ENUM b) { \
return a = a | b; \
} \
MDBX_NOSANITIZE_ENUM MDBX_CXX01_CONSTEXPR ENUM operator&(ENUM a, ENUM b) { \
return ENUM(unsigned(a) & unsigned(b)); \
} \
MDBX_NOSANITIZE_ENUM MDBX_CXX01_CONSTEXPR ENUM operator&(ENUM a, \
unsigned b) { \
return ENUM(unsigned(a) & b); \
} \
MDBX_NOSANITIZE_ENUM MDBX_CXX01_CONSTEXPR ENUM operator&(unsigned a, \
ENUM b) { \
return ENUM(a & unsigned(b)); \
} \
MDBX_NOSANITIZE_ENUM MDBX_CXX14_CONSTEXPR ENUM &operator&=(ENUM &a, \
ENUM b) { \
return a = a & b; \
} \
MDBX_NOSANITIZE_ENUM MDBX_CXX14_CONSTEXPR ENUM &operator&=(ENUM &a, \
unsigned b) { \
return a = a & b; \
} \
MDBX_CXX01_CONSTEXPR unsigned operator~(ENUM a) { return ~unsigned(a); } \
MDBX_NOSANITIZE_ENUM MDBX_CXX01_CONSTEXPR ENUM operator^(ENUM a, ENUM b) { \
return ENUM(unsigned(a) ^ unsigned(b)); \
} \
MDBX_NOSANITIZE_ENUM MDBX_CXX14_CONSTEXPR ENUM &operator^=(ENUM &a, \
ENUM b) { \
return a = a ^ b; \
} \
}
#else /* __cplusplus */
/* nope for C since it always allows these operators for enums */
#define DEFINE_ENUM_FLAG_OPERATORS(ENUM)
#define CONSTEXPR_ENUM_FLAGS_OPERATIONS 1
#endif /* !__cplusplus */
#elif !defined(CONSTEXPR_ENUM_FLAGS_OPERATIONS)
#ifdef __cplusplus
/* DEFINE_ENUM_FLAG_OPERATORS may be defined broken as in the Windows SDK */
#define CONSTEXPR_ENUM_FLAGS_OPERATIONS 0
#else
/* C always allows these operators for enums */
#define CONSTEXPR_ENUM_FLAGS_OPERATIONS 1
#endif
#endif /* DEFINE_ENUM_FLAG_OPERATORS */
/** @} end of Common Macros */
/*----------------------------------------------------------------------------*/
/** \addtogroup c_api
* @{ */
#ifdef __cplusplus
extern "C" {
#endif
/* MDBX version 0.11.x */
#define MDBX_VERSION_MAJOR 0
#define MDBX_VERSION_MINOR 11
#ifndef LIBMDBX_API
#if defined(LIBMDBX_EXPORTS)
#define LIBMDBX_API __dll_export
#elif defined(LIBMDBX_IMPORTS)
#define LIBMDBX_API __dll_import
#else
#define LIBMDBX_API
#endif
#endif /* LIBMDBX_API */
#ifdef __cplusplus
#if defined(__clang__) || __has_attribute(type_visibility)
#define LIBMDBX_API_TYPE LIBMDBX_API __attribute__((type_visibility("default")))
#else
#define LIBMDBX_API_TYPE LIBMDBX_API
#endif
#else
#define LIBMDBX_API_TYPE
#endif /* LIBMDBX_API_TYPE */
#if defined(LIBMDBX_IMPORTS)
#define LIBMDBX_VERINFO_API __dll_import
#else
#define LIBMDBX_VERINFO_API __dll_export
#endif /* LIBMDBX_VERINFO_API */
/** \brief libmdbx version information */
extern LIBMDBX_VERINFO_API const struct MDBX_version_info {
uint8_t major; /**< Major version number */
uint8_t minor; /**< Minor version number */
uint16_t release; /**< Release number of Major.Minor */
uint32_t revision; /**< Revision number of Release */
struct {
const char *datetime; /**< committer date, strict ISO-8601 format */
const char *tree; /**< commit hash (hexadecimal digits) */
const char *commit; /**< tree hash, i.e. digest of the source code */
const char *describe; /**< git-describe string */
} git; /**< source information from git */
const char *sourcery; /**< sourcery anchor for pinning */
} /** \brief libmdbx version information */ mdbx_version;
/** \brief libmdbx build information
* \attention Some strings could be NULL in case no corresponding information
* was provided at build time (i.e. flags). */
extern LIBMDBX_VERINFO_API const struct MDBX_build_info {
const char *datetime; /**< build timestamp (ISO-8601 or __DATE__ __TIME__) */
const char *target; /**< cpu/arch-system-config triplet */
const char *options; /**< mdbx-related options */
const char *compiler; /**< compiler */
const char *flags; /**< CFLAGS and CXXFLAGS */
} /** \brief libmdbx build information */ mdbx_build;
#if (defined(_WIN32) || defined(_WIN64)) && !MDBX_BUILD_SHARED_LIBRARY
/* MDBX internally uses global and thread local storage destructors to
* automatically (de)initialization, releasing reader lock table slots
* and so on.
*
* If MDBX builded as a DLL this is done out-of-the-box by DllEntry() function,
* which called automatically by Windows core with passing corresponding reason
* argument.
*
* Otherwise, if MDBX was builded not as a DLL, some black magic
* may be required depending of Windows version:
*
* - Modern Windows versions, including Windows Vista and later, provides
* support for "TLS Directory" (e.g .CRT$XL[A-Z] sections in executable
* or dll file). In this case, MDBX capable of doing all automatically,
* therefore you DON'T NEED to call mdbx_module_handler()
* so the MDBX_MANUAL_MODULE_HANDLER defined as 0.
*
* - Obsolete versions of Windows, prior to Windows Vista, REQUIRES calling
* mdbx_module_handler() manually from corresponding DllMain() or WinMain()
* of your DLL or application,
* so the MDBX_MANUAL_MODULE_HANDLER defined as 1.
*
* Therefore, building MDBX as a DLL is recommended for all version of Windows.
* So, if you doubt, just build MDBX as the separate DLL and don't care about
* the MDBX_MANUAL_MODULE_HANDLER. */
#ifndef _WIN32_WINNT
#error Non-dll build libmdbx requires target Windows version \
to be explicitly defined via _WIN32_WINNT for properly \
handling thread local storage destructors.
#endif /* _WIN32_WINNT */
#if _WIN32_WINNT >= 0x0600 /* Windows Vista */
/* As described above mdbx_module_handler() is NOT needed for Windows Vista
* and later. */
#define MDBX_MANUAL_MODULE_HANDLER 0
#else
/* As described above mdbx_module_handler() IS REQUIRED for Windows versions
* prior to Windows Vista. */
#define MDBX_MANUAL_MODULE_HANDLER 1
void LIBMDBX_API NTAPI mdbx_module_handler(PVOID module, DWORD reason,
PVOID reserved);
#endif
#endif /* Windows && !DLL && MDBX_MANUAL_MODULE_HANDLER */
/* OPACITY STRUCTURES *********************************************************/
/** \brief Opaque structure for a database environment.
* \details An environment supports multiple key-value sub-databases (aka
* key-value spaces or tables), all residing in the same shared-memory map.
* \see mdbx_env_create() \see mdbx_env_close() */
#ifndef __cplusplus
typedef struct MDBX_env MDBX_env;
#else
struct MDBX_env;
#endif
/** \brief Opaque structure for a transaction handle.
* \ingroup c_transactions
* \details All database operations require a transaction handle. Transactions
* may be read-only or read-write.
* \see mdbx_txn_begin() \see mdbx_txn_commit() \see mdbx_txn_abort() */
#ifndef __cplusplus
typedef struct MDBX_txn MDBX_txn;
#else
struct MDBX_txn;
#endif
/** \brief A handle for an individual database (key-value spaces) in the
* environment.
* \ingroup c_dbi
* \details Zero handle is used internally (hidden Garbage Collection subDB).
* So, any valid DBI-handle great than 0 and less than or equal
* \ref MDBX_MAX_DBI.
* \see mdbx_dbi_open() \see mdbx_dbi_close() */
typedef uint32_t MDBX_dbi;
/** \brief Opaque structure for navigating through a database
* \ingroup c_cursors
* \see mdbx_cursor_create() \see mdbx_cursor_bind() \see mdbx_cursor_close()
*/
#ifndef __cplusplus
typedef struct MDBX_cursor MDBX_cursor;
#else
struct MDBX_cursor;
#endif
/** \brief Generic structure used for passing keys and data in and out of the
* database.
* \anchor MDBX_val \see mdbx::slice \see mdbx::buffer
*
* \details Values returned from the database are valid only until a subsequent
* update operation, or the end of the transaction. Do not modify or
* free them, they commonly point into the database itself.
*
* Key sizes must be between 0 and \ref mdbx_env_get_maxkeysize() inclusive.
* The same applies to data sizes in databases with the \ref MDBX_DUPSORT flag.
* Other data items can in theory be from 0 to \ref MDBX_MAXDATASIZE bytes long.
*
* \note The notable difference between MDBX and LMDB is that MDBX support zero
* length keys. */
#ifndef HAVE_STRUCT_IOVEC
struct iovec {
void *iov_base; /**< pointer to some data */
size_t iov_len; /**< the length of data in bytes */
};
#define HAVE_STRUCT_IOVEC
#endif /* HAVE_STRUCT_IOVEC */
#if defined(__sun) || defined(__SVR4) || defined(__svr4__)
/* The `iov_len` is signed on Sun/Solaris.
* So define custom MDBX_val to avoid a lot of warnings. */
struct MDBX_val {
void *iov_base; /**< pointer to some data */
size_t iov_len; /**< the length of data in bytes */
};
#ifndef __cplusplus
typedef struct MDBX_val MDBX_val;
#endif
#else /* SunOS */
typedef struct iovec MDBX_val;
#endif /* ! SunOS */
enum MDBX_constants {
/** The hard limit for DBI handles */
MDBX_MAX_DBI = UINT32_C(32765),
/** The maximum size of a data item. */
MDBX_MAXDATASIZE = UINT32_C(0x7fff0000),
/** The minimal database page size in bytes. */
MDBX_MIN_PAGESIZE = 256,
/** The maximal database page size in bytes. */
MDBX_MAX_PAGESIZE = 65536,
};
/* THE FILES *******************************************************************
* At the file system level, the environment corresponds to a pair of files. */
/** \brief The name of the lock file in the environment */
#define MDBX_LOCKNAME "/mdbx.lck"
/** \brief The name of the data file in the environment */
#define MDBX_DATANAME "/mdbx.dat"
/** \brief The suffix of the lock file when \ref MDBX_NOSUBDIR is used */
#define MDBX_LOCK_SUFFIX "-lck"
/* DEBUG & LOGGING ************************************************************/
/** \addtogroup c_debug
* \note Most of debug feature enabled only when libmdbx builded with
* \ref MDBX_DEBUG build option. @{ */
/** Log level
* \note Levels detailed than (great than) \ref MDBX_LOG_NOTICE
* requires build libmdbx with \ref MDBX_DEBUG option. */
enum MDBX_log_level_t {
/** Critical conditions, i.e. assertion failures.
* \note libmdbx always produces such messages regardless
* of \ref MDBX_DEBUG build option. */
MDBX_LOG_FATAL = 0,
/** Enables logging for error conditions
* and \ref MDBX_LOG_FATAL.
* \note libmdbx always produces such messages regardless
* of \ref MDBX_DEBUG build option. */
MDBX_LOG_ERROR = 1,
/** Enables logging for warning conditions
* and \ref MDBX_LOG_ERROR ... \ref MDBX_LOG_FATAL.
* \note libmdbx always produces such messages regardless
* of \ref MDBX_DEBUG build option. */
MDBX_LOG_WARN = 2,
/** Enables logging for normal but significant condition
* and \ref MDBX_LOG_WARN ... \ref MDBX_LOG_FATAL.
* \note libmdbx always produces such messages regardless
* of \ref MDBX_DEBUG build option. */
MDBX_LOG_NOTICE = 3,
/** Enables logging for verbose informational
* and \ref MDBX_LOG_NOTICE ... \ref MDBX_LOG_FATAL.
* \note Requires build libmdbx with \ref MDBX_DEBUG option. */
MDBX_LOG_VERBOSE = 4,
/** Enables logging for debug-level messages
* and \ref MDBX_LOG_VERBOSE ... \ref MDBX_LOG_FATAL.
* \note Requires build libmdbx with \ref MDBX_DEBUG option. */
MDBX_LOG_DEBUG = 5,
/** Enables logging for trace debug-level messages
* and \ref MDBX_LOG_DEBUG ... \ref MDBX_LOG_FATAL.
* \note Requires build libmdbx with \ref MDBX_DEBUG option. */
MDBX_LOG_TRACE = 6,
/** Enables extra debug-level messages (dump pgno lists)
* and all other log-messages.
* \note Requires build libmdbx with \ref MDBX_DEBUG option. */
MDBX_LOG_EXTRA = 7,
#ifdef ENABLE_UBSAN
MDBX_LOG_MAX = 7 /* avoid UBSAN false-positive trap by a tests */,
#endif /* ENABLE_UBSAN */
/** for \ref mdbx_setup_debug() only: Don't change current settings */
MDBX_LOG_DONTCHANGE = -1
};
#ifndef __cplusplus
typedef enum MDBX_log_level_t MDBX_log_level_t;
#endif
/** \brief Runtime debug flags
*
* \details `MDBX_DBG_DUMP` and `MDBX_DBG_LEGACY_MULTIOPEN` always have an
* effect, but `MDBX_DBG_ASSERT`, `MDBX_DBG_AUDIT` and `MDBX_DBG_JITTER` only if
* libmdbx builded with \ref MDBX_DEBUG. */
enum MDBX_debug_flags_t {
MDBX_DBG_NONE = 0,
/** Enable assertion checks.
* \note Always enabled for builds with `MDBX_FORCE_ASSERTIONS` option,
* otherwise requires build with \ref MDBX_DEBUG > 0 */
MDBX_DBG_ASSERT = 1,
/** Enable pages usage audit at commit transactions.
* \note Requires build with \ref MDBX_DEBUG > 0 */
MDBX_DBG_AUDIT = 2,
/** Enable small random delays in critical points.
* \note Requires build with \ref MDBX_DEBUG > 0 */
MDBX_DBG_JITTER = 4,
/** Include or not meta-pages in coredump files.
* \note May affect performance in \ref MDBX_WRITEMAP mode */
MDBX_DBG_DUMP = 8,
/** Allow multi-opening environment(s) */
MDBX_DBG_LEGACY_MULTIOPEN = 16,
/** Allow read and write transactions overlapping for the same thread. */
MDBX_DBG_LEGACY_OVERLAP = 32,
/** Don't auto-upgrade format signature.
* \note However a new write transactions will use and store
* the last signature regardless this flag */
MDBX_DBG_DONT_UPGRADE = 64,
#ifdef ENABLE_UBSAN
MDBX_DBG_MAX = ((unsigned)MDBX_LOG_MAX) << 16 |
127 /* avoid UBSAN false-positive trap by a tests */,
#endif /* ENABLE_UBSAN */
/** for mdbx_setup_debug() only: Don't change current settings */
MDBX_DBG_DONTCHANGE = -1
};
#ifndef __cplusplus
typedef enum MDBX_debug_flags_t MDBX_debug_flags_t;
#else
DEFINE_ENUM_FLAG_OPERATORS(MDBX_debug_flags_t)
#endif
/** \brief A debug-logger callback function,
* called before printing the message and aborting.
* \see mdbx_setup_debug()
*
* \param [in] env An environment handle returned by \ref mdbx_env_create().
* \param [in] msg The assertion message, not including newline. */
typedef void MDBX_debug_func(MDBX_log_level_t loglevel, const char *function,
int line, const char *fmt,
va_list args) MDBX_CXX17_NOEXCEPT;
/** \brief The "don't change `logger`" value for mdbx_setup_debug() */
#define MDBX_LOGGER_DONTCHANGE ((MDBX_debug_func *)(intptr_t)-1)
/** \brief Setup global log-level, debug options and debug logger.
* \returns The previously `debug_flags` in the 0-15 bits
* and `log_level` in the 16-31 bits. */
LIBMDBX_API int mdbx_setup_debug(MDBX_log_level_t log_level,
MDBX_debug_flags_t debug_flags,
MDBX_debug_func *logger);
/** \brief A callback function for most MDBX assert() failures,
* called before printing the message and aborting.
* \see mdbx_env_set_assert()
*
* \param [in] env An environment handle returned by mdbx_env_create().
* \param [in] msg The assertion message, not including newline. */
typedef void MDBX_assert_func(const MDBX_env *env, const char *msg,
const char *function,
unsigned line) MDBX_CXX17_NOEXCEPT;
/** \brief Set or reset the assert() callback of the environment.
*
* Does nothing if libmdbx was built with MDBX_DEBUG=0 or with NDEBUG,
* and will return `MDBX_ENOSYS` in such case.
*
* \param [in] env An environment handle returned by mdbx_env_create().
* \param [in] func An MDBX_assert_func function, or 0.
*
* \returns A non-zero error value on failure and 0 on success. */
LIBMDBX_API int mdbx_env_set_assert(MDBX_env *env, MDBX_assert_func *func);
/** \brief Dump given MDBX_val to the buffer
*
* Dumps it as string if value is printable (all bytes in the range 0x20..0x7E),
* otherwise made hexadecimal dump. Requires at least 4 byte length buffer.
*
* \returns One of:
* - NULL if given buffer size less than 4 bytes;
* - pointer to constant string if given value NULL or empty;
* - otherwise pointer to given buffer. */
LIBMDBX_API const char *mdbx_dump_val(const MDBX_val *key, char *const buf,
const size_t bufsize);
/** \brief Panics with message and causes abnormal process termination. */
LIBMDBX_API void mdbx_panic(const char *fmt, ...) MDBX_PRINTF_ARGS(1, 2);
/** @} end of logging & debug */
/** \brief Environment flags
* \ingroup c_opening
* \anchor env_flags
* \see mdbx_env_open() \see mdbx_env_set_flags() */
enum MDBX_env_flags_t {
MDBX_ENV_DEFAULTS = 0,
/** No environment directory.
*
* By default, MDBX creates its environment in a directory whose pathname is
* given in path, and creates its data and lock files under that directory.
* With this option, path is used as-is for the database main data file.
* The database lock file is the path with "-lck" appended.
*
* - with `MDBX_NOSUBDIR` = in a filesystem we have the pair of MDBX-files
* which names derived from given pathname by appending predefined suffixes.
*
* - without `MDBX_NOSUBDIR` = in a filesystem we have the MDBX-directory with
* given pathname, within that a pair of MDBX-files with predefined names.
*
* This flag affects only at new environment creating by \ref mdbx_env_open(),
* otherwise at opening an existing environment libmdbx will choice this
* automatically. */
MDBX_NOSUBDIR = UINT32_C(0x4000),
/** Read only mode.
*
* Open the environment in read-only mode. No write operations will be
* allowed. MDBX will still modify the lock file - except on read-only
* filesystems, where MDBX does not use locks.
*
* - with `MDBX_RDONLY` = open environment in read-only mode.
* MDBX supports pure read-only mode (i.e. without opening LCK-file) only
* when environment directory and/or both files are not writable (and the
* LCK-file may be missing). In such case allowing file(s) to be placed
* on a network read-only share.
*
* - without `MDBX_RDONLY` = open environment in read-write mode.
*
* This flag affects only at environment opening but can't be changed after.
*/