This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
mdbx.h++
4719 lines (4017 loc) · 174 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
/// \file mdbx.h++
/// \brief The libmdbx C++ API header file (preliminary).
///
/// \author Copyright (c) 2020-2021, Leonid Yuriev <leo@yuriev.ru>.
/// \copyright SPDX-License-Identifier: Apache-2.0
///
/// Tested with:
/// - LCC >= 1.23 (http://www.mcst.ru/lcc),
/// - GNU C++ >= 4.8,
/// - clang >= 4.0,
/// - MSVC >= 19.0 (Visual Studio 2015),
/// but 19.2x could hang due optimizer bug.
///
#pragma once
#if (!defined(__cplusplus) || __cplusplus < 201103L) && \
!(defined( \
_MSC_VER) /* MSVC is mad and don't define __cplusplus properly */ \
&& _MSC_VER == 1900)
#error "C++11 or better is required"
#endif
#if (defined(_WIN32) || defined(_WIN64)) && MDBX_WITHOUT_MSVC_CRT
#error \
"CRT is required for C++ API, the MDBX_WITHOUT_MSVC_CRT option must be disabled"
#endif /* Windows */
#ifndef __has_include
#define __has_include(header) (0)
#endif /* __has_include */
#if __has_include(<version>)
#include <version>
#endif /* <version> */
/* Disable min/max macros from C' headers */
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <algorithm> // for std::min/max
#include <cassert> // for assert()
#include <cstring> // for std::strlen, str:memcmp
#include <exception> // for std::exception_ptr
#include <ostream> // for std::ostream
#include <sstream> // for std::ostringstream
#include <stdexcept> // for std::invalid_argument
#include <string> // for std::string
#include <type_traits> // for std::is_pod<>, etc.
#include <vector> // for std::vector<> as template args
// Unused for now
// #if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
// #include <bit>
// #endif
#if defined(__cpp_lib_memory_resource) && __cpp_lib_memory_resource >= 201603L
#include <memory_resource>
#endif
#if defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L
#include <string_view>
#endif
#if defined(__cpp_lib_filesystem) && __cpp_lib_filesystem >= 201703L
#include <filesystem>
#endif
#include "mdbx.h"
#if defined(DOXYGEN) || \
defined(__cpp_constexpr) && __cpp_constexpr >= 201603L && \
((defined(_MSC_VER) && _MSC_VER >= 1915) || \
(defined(__clang__) && __clang_major__ > 5) || \
(defined(__GNUC__) && __GNUC__ > 7) || \
(!defined(__GNUC__) && !defined(__clang__) && !defined(_MSC_VER)))
#define MDBX_CXX17_CONSTEXPR constexpr
#else
#define MDBX_CXX17_CONSTEXPR inline
#endif /* MDBX_CXX17_CONSTEXPR */
#if defined(DOXYGEN) || defined(__cpp_lib_is_constant_evaluated) && \
__cpp_lib_is_constant_evaluated >= 201811L && \
defined(__cpp_lib_constexpr_string) && \
__cpp_lib_constexpr_string >= 201907L
#define MDBX_CXX20_CONSTEXPR constexpr
#else
#define MDBX_CXX20_CONSTEXPR inline
#endif /* MDBX_CXX20_CONSTEXPR */
#if defined(CONSTEXPR_ASSERT)
#define MDBX_CONSTEXPR_ASSERT(expr) CONSTEXPR_ASSERT(expr)
#elif defined NDEBUG
#define MDBX_CONSTEXPR_ASSERT(expr) void(0)
#else
#define MDBX_CONSTEXPR_ASSERT(expr) \
((expr) ? void(0) : [] { assert(!#expr); }())
#endif /* MDBX_CONSTEXPR_ASSERT */
#ifndef MDBX_LIKELY
#if defined(DOXYGEN) || \
(defined(__GNUC__) || __has_builtin(__builtin_expect)) && \
!defined(__COVERITY__)
#define MDBX_LIKELY(cond) __builtin_expect(!!(cond), 1)
#else
#define MDBX_LIKELY(x) (x)
#endif
#endif /* MDBX_LIKELY */
#ifndef MDBX_UNLIKELY
#if defined(DOXYGEN) || \
(defined(__GNUC__) || __has_builtin(__builtin_expect)) && \
!defined(__COVERITY__)
#define MDBX_UNLIKELY(cond) __builtin_expect(!!(cond), 0)
#else
#define MDBX_UNLIKELY(x) (x)
#endif
#endif /* MDBX_UNLIKELY */
#if defined(DOXYGEN) || \
(__has_cpp_attribute(fallthrough) && \
(!defined(__clang__) || __clang__ > 4)) || \
__cplusplus >= 201703L
#define MDBX_CXX17_FALLTHROUGH [[fallthrough]]
#else
#define MDBX_CXX17_FALLTHROUGH
#endif /* MDBX_CXX17_FALLTHROUGH */
#if defined(DOXYGEN) || __has_cpp_attribute(likely)
#define MDBX_CXX20_LIKELY [[likely]]
#else
#define MDBX_CXX20_LIKELY
#endif /* MDBX_CXX20_LIKELY */
#ifndef MDBX_CXX20_UNLIKELY
#if defined(DOXYGEN) || __has_cpp_attribute(unlikely)
#define MDBX_CXX20_UNLIKELY [[unlikely]]
#else
#define MDBX_CXX20_UNLIKELY
#endif
#endif /* MDBX_CXX20_UNLIKELY */
#ifdef _MSC_VER
#pragma warning(push, 4)
#pragma warning(disable : 4251) /* 'std::FOO' needs to have dll-interface to \
be used by clients of 'mdbx::BAR' */
#pragma warning(disable : 4275) /* non dll-interface 'std::FOO' used as \
base for dll-interface 'mdbx::BAR' */
/* MSVC is mad and can generate this warning for its own intermediate
* automatically generated code, which becomes unreachable after some kinds of
* optimization (copy elision, etc). */
#pragma warning(disable : 4702) /* unreachable code */
#endif /* _MSC_VER (warnings) */
//------------------------------------------------------------------------------
/// \defgroup cxx_api C++ API
/// @{
namespace mdbx {
// Functions whose signature depends on the `mdbx::byte` type
// must be strictly defined as inline!
#if defined(DOXYGEN) || (defined(__cpp_char8_t) && __cpp_char8_t >= 201811)
// Wanna using a non-aliasing type to release more power of an optimizer.
using byte = char8_t;
#else
// Wanna not using std::byte since it doesn't add features,
// but add inconvenient restrictions.
using byte = unsigned char;
#endif /* __cpp_char8_t >= 201811*/
/// \copydoc MDBX_version_info
using version_info = ::MDBX_version_info;
/// \brief Returns libmdbx version information.
MDBX_CXX11_CONSTEXPR const version_info &get_version() noexcept;
/// \copydoc MDBX_build_info
using build_info = ::MDBX_build_info;
/// \brief Returns libmdbx build information.
MDBX_CXX11_CONSTEXPR const build_info &get_build() noexcept;
/// \brief constexpr-compatible strlen().
static MDBX_CXX17_CONSTEXPR size_t strlen(const char *c_str) noexcept;
struct slice;
class env;
class env_managed;
class txn;
class txn_managed;
class cursor;
class cursor_managed;
/// \brief Legacy default allocator
/// but it is recommended to use \ref polymorphic_allocator.
using legacy_allocator = ::std::string::allocator_type;
#if defined(DOXYGEN) || \
defined(__cpp_lib_memory_resource) && __cpp_lib_memory_resource >= 201603L
/// \brief Default polymorphic allocator for modern code.
using polymorphic_allocator = ::std::pmr::string::allocator_type;
#endif /* __cpp_lib_memory_resource >= 201603L */
/// \brief Default singe-byte string.
template <class ALLOCATOR = legacy_allocator>
using string = ::std::basic_string<char, ::std::char_traits<char>, ALLOCATOR>;
using filehandle = ::mdbx_filehandle_t;
#if defined(DOXYGEN) || \
(defined(__cpp_lib_filesystem) && __cpp_lib_filesystem >= 201703L && \
(!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || \
__MAC_OS_X_VERSION_MIN_REQUIRED >= 101500))
#define MDBX_STD_FILESYSTEM_PATH
using path = ::std::filesystem::path;
#elif defined(_WIN32) || defined(_WIN64)
using path = ::std::wstring;
#else
using path = ::std::string;
#endif
/// \brief Transfers C++ exceptions thru C callbacks.
/// \details Implements saving exceptions before returning
/// from an C++'s environment to the intermediate C code and re-throwing after
/// returning from C to the C++'s environment.
class LIBMDBX_API_TYPE exception_thunk {
::std::exception_ptr captured_;
public:
exception_thunk() noexcept = default;
exception_thunk(const exception_thunk &) = delete;
exception_thunk(exception_thunk &&) = delete;
exception_thunk &operator=(const exception_thunk &) = delete;
exception_thunk &operator=(exception_thunk &&) = delete;
inline bool is_clean() const noexcept;
inline void capture() noexcept;
inline void rethrow_captured() const;
};
/// \brief Implements error information and throwing corresponding exceptions.
class LIBMDBX_API_TYPE error {
MDBX_error_t code_;
inline error &operator=(MDBX_error_t error_code) noexcept;
public:
MDBX_CXX11_CONSTEXPR error(MDBX_error_t error_code) noexcept;
error(const error &) = default;
error(error &&) = default;
error &operator=(const error &) = default;
error &operator=(error &&) = default;
MDBX_CXX11_CONSTEXPR friend bool operator==(const error &a,
const error &b) noexcept;
MDBX_CXX11_CONSTEXPR friend bool operator!=(const error &a,
const error &b) noexcept;
MDBX_CXX11_CONSTEXPR bool is_success() const noexcept;
MDBX_CXX11_CONSTEXPR bool is_result_true() const noexcept;
MDBX_CXX11_CONSTEXPR bool is_result_false() const noexcept;
MDBX_CXX11_CONSTEXPR bool is_failure() const noexcept;
/// \brief Returns error code.
MDBX_CXX11_CONSTEXPR MDBX_error_t code() const noexcept;
/// \brief Returns message for MDBX's errors only and "SYSTEM" for others.
const char *what() const noexcept;
/// \brief Returns message for any errors.
::std::string message() const;
/// \brief Returns true for MDBX's errors.
MDBX_CXX11_CONSTEXPR bool is_mdbx_error() const noexcept;
/// \brief Panics on unrecoverable errors inside destructors etc.
[[noreturn]] void panic(const char *context_where_when,
const char *func_who_what) const noexcept;
[[noreturn]] void throw_exception() const;
[[noreturn]] static inline void throw_exception(int error_code);
inline void throw_on_failure() const;
inline void success_or_throw() const;
inline void success_or_throw(const exception_thunk &) const;
inline void panic_on_failure(const char *context_where,
const char *func_who) const noexcept;
inline void success_or_panic(const char *context_where,
const char *func_who) const noexcept;
static inline void throw_on_nullptr(const void *ptr, MDBX_error_t error_code);
static inline void success_or_throw(MDBX_error_t error_code);
static void success_or_throw(int error_code) {
success_or_throw(static_cast<MDBX_error_t>(error_code));
}
static inline void throw_on_failure(int error_code);
static inline bool boolean_or_throw(int error_code);
static inline void success_or_throw(int error_code, const exception_thunk &);
static inline void panic_on_failure(int error_code, const char *context_where,
const char *func_who) noexcept;
static inline void success_or_panic(int error_code, const char *context_where,
const char *func_who) noexcept;
};
/// \brief Base class for all libmdbx's exceptions that are corresponds
/// to libmdbx errors.
///
/// \see MDBX_error_t
class LIBMDBX_API_TYPE exception : public ::std::runtime_error {
using base = ::std::runtime_error;
::mdbx::error error_;
public:
exception(const ::mdbx::error &) noexcept;
exception(const exception &) = default;
exception(exception &&) = default;
exception &operator=(const exception &) = default;
exception &operator=(exception &&) = default;
virtual ~exception() noexcept;
const mdbx::error error() const noexcept { return error_; }
};
/// \brief Fatal exception that lead termination anyway
/// in dangerous unrecoverable cases.
class LIBMDBX_API_TYPE fatal : public exception {
using base = exception;
public:
fatal(const ::mdbx::error &) noexcept;
fatal(const exception &src) noexcept : fatal(src.error()) {}
fatal(exception &&src) noexcept : fatal(src.error()) {}
fatal(const fatal &src) noexcept : fatal(src.error()) {}
fatal(fatal &&src) noexcept : fatal(src.error()) {}
fatal &operator=(fatal &&) = default;
fatal &operator=(const fatal &) = default;
virtual ~fatal() noexcept;
};
#define MDBX_DECLARE_EXCEPTION(NAME) \
struct LIBMDBX_API_TYPE NAME : public exception { \
NAME(const ::mdbx::error &); \
virtual ~NAME() noexcept; \
}
MDBX_DECLARE_EXCEPTION(bad_map_id);
MDBX_DECLARE_EXCEPTION(bad_transaction);
MDBX_DECLARE_EXCEPTION(bad_value_size);
MDBX_DECLARE_EXCEPTION(db_corrupted);
MDBX_DECLARE_EXCEPTION(db_full);
MDBX_DECLARE_EXCEPTION(db_invalid);
MDBX_DECLARE_EXCEPTION(db_too_large);
MDBX_DECLARE_EXCEPTION(db_unable_extend);
MDBX_DECLARE_EXCEPTION(db_version_mismatch);
MDBX_DECLARE_EXCEPTION(db_wanna_write_for_recovery);
MDBX_DECLARE_EXCEPTION(incompatible_operation);
MDBX_DECLARE_EXCEPTION(internal_page_full);
MDBX_DECLARE_EXCEPTION(internal_problem);
MDBX_DECLARE_EXCEPTION(key_exists);
MDBX_DECLARE_EXCEPTION(key_mismatch);
MDBX_DECLARE_EXCEPTION(max_maps_reached);
MDBX_DECLARE_EXCEPTION(max_readers_reached);
MDBX_DECLARE_EXCEPTION(multivalue);
MDBX_DECLARE_EXCEPTION(no_data);
MDBX_DECLARE_EXCEPTION(not_found);
MDBX_DECLARE_EXCEPTION(operation_not_permitted);
MDBX_DECLARE_EXCEPTION(permission_denied_or_not_writeable);
MDBX_DECLARE_EXCEPTION(reader_slot_busy);
MDBX_DECLARE_EXCEPTION(remote_media);
MDBX_DECLARE_EXCEPTION(something_busy);
MDBX_DECLARE_EXCEPTION(thread_mismatch);
MDBX_DECLARE_EXCEPTION(transaction_full);
MDBX_DECLARE_EXCEPTION(transaction_overlapping);
#undef MDBX_DECLARE_EXCEPTION
[[noreturn]] LIBMDBX_API void throw_too_small_target_buffer();
[[noreturn]] LIBMDBX_API void throw_max_length_exceeded();
[[noreturn]] LIBMDBX_API void throw_out_range();
MDBX_CXX14_CONSTEXPR size_t check_length(size_t bytes);
//------------------------------------------------------------------------------
/// \brief References a data located outside the slice.
///
/// The `slice` is similar in many ways to `std::string_view`, but it
/// implements specific capabilities and manipulates with bytes but
/// not a characters.
///
/// \copydetails MDBX_val
struct LIBMDBX_API_TYPE slice : public ::MDBX_val {
/// \todo slice& operator<<(slice&, ...) for reading
/// \todo key-to-value (parse/unpack) functions
/// \todo template<class X> key(X); for decoding keys while reading
enum { max_length = MDBX_MAXDATASIZE };
/// \brief Create an empty slice.
MDBX_CXX11_CONSTEXPR slice() noexcept;
/// \brief Create a slice that refers to [0,bytes-1] of memory bytes pointed
/// by ptr.
MDBX_CXX14_CONSTEXPR slice(const void *ptr, size_t bytes);
/// \brief Create a slice that refers to [begin,end] of memory bytes.
MDBX_CXX14_CONSTEXPR slice(const void *begin, const void *end);
/// \brief Create a slice that refers to text[0,strlen(text)-1].
template <size_t SIZE>
MDBX_CXX14_CONSTEXPR slice(const char (&text)[SIZE]) noexcept
: slice(text, SIZE - 1) {
MDBX_CONSTEXPR_ASSERT(SIZE > 0 && text[SIZE - 1] == '\0');
}
/// \brief Create a slice that refers to c_str[0,strlen(c_str)-1].
explicit MDBX_CXX17_CONSTEXPR slice(const char *c_str);
/// \brief Create a slice that refers to the contents of "str".
/* 'explicit' to avoid reference to the temporary std::string instance */
template <class C, class T, class A>
explicit MDBX_CXX20_CONSTEXPR slice(const ::std::basic_string<C, T, A> &str)
: slice(str.data(), str.length() * sizeof(C)) {}
MDBX_CXX14_CONSTEXPR slice(const MDBX_val &src);
MDBX_CXX11_CONSTEXPR slice(const slice &) noexcept = default;
#if defined(DOXYGEN) || \
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
/// \brief Create a slice that refers to the same contents as "sv"
template <class C, class T>
explicit MDBX_CXX14_CONSTEXPR slice(const ::std::basic_string_view<C, T> &sv)
: slice(sv.data(), sv.data() + sv.length()) {}
#endif /* __cpp_lib_string_view >= 201606L */
inline slice(MDBX_val &&src);
inline slice(slice &&src) noexcept;
#if defined(DOXYGEN) || \
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
template <class C, class T>
slice(::std::basic_string_view<C, T> &&sv) : slice(sv) {
sv = {};
}
#endif /* __cpp_lib_string_view >= 201606L */
template <size_t SIZE>
static MDBX_CXX14_CONSTEXPR slice wrap(const char (&text)[SIZE]) {
return slice(text);
}
template <typename POD>
MDBX_CXX14_CONSTEXPR static slice wrap(const POD &pod) {
static_assert(::std::is_standard_layout<POD>::value &&
!std::is_pointer<POD>::value,
"Must be a standard layout type!");
return slice(&pod, sizeof(pod));
}
inline slice &assign(const void *ptr, size_t bytes);
inline slice &assign(const slice &src) noexcept;
inline slice &assign(const ::MDBX_val &src);
inline slice &assign(slice &&src) noexcept;
inline slice &assign(::MDBX_val &&src);
inline slice &assign(const void *begin, const void *end);
template <class C, class T, class A>
slice &assign(const ::std::basic_string<C, T, A> &str) {
return assign(str.data(), str.length() * sizeof(C));
}
inline slice &assign(const char *c_str);
#if defined(DOXYGEN) || \
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
template <class C, class T>
slice &assign(const ::std::basic_string_view<C, T> &view) {
return assign(view.begin(), view.end());
}
template <class C, class T>
slice &assign(::std::basic_string_view<C, T> &&view) {
assign(view);
view = {};
return *this;
}
#endif /* __cpp_lib_string_view >= 201606L */
slice &operator=(const slice &) noexcept = default;
inline slice &operator=(slice &&src) noexcept;
inline slice &operator=(::MDBX_val &&src);
#if defined(DOXYGEN) || \
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
template <class C, class T>
slice &operator=(const ::std::basic_string_view<C, T> &view) {
return assign(view);
}
template <class C, class T>
slice &operator=(::std::basic_string_view<C, T> &&view) {
return assign(view);
}
#endif /* __cpp_lib_string_view >= 201606L */
template <class C = char, class T = ::std::char_traits<C>,
class A = legacy_allocator>
MDBX_CXX20_CONSTEXPR ::std::basic_string<C, T, A>
string(const A &allocator = A()) const {
static_assert(sizeof(C) == 1, "Must be single byte characters");
return ::std::basic_string<C, T, A>(char_ptr(), length(), allocator);
}
template <class C, class T, class A>
MDBX_CXX20_CONSTEXPR operator ::std::basic_string<C, T, A>() const {
return this->string<C, T, A>();
}
/// \brief Fills the buffer by hexadecimal data dump of slice content.
/// \throws std::length_error if given buffer is too small.
char *to_hex(char *dest, size_t dest_size, bool uppercase = false,
unsigned wrap_width = 0) const;
/// \brief Returns the buffer size in bytes needed for hexadecimal data dump
/// of slice content.
MDBX_CXX11_CONSTEXPR size_t
to_hex_bytes(unsigned wrap_width = 0) const noexcept {
const size_t bytes = length() << 1;
return wrap_width ? bytes + bytes / wrap_width : bytes;
}
/// \brief Fills the buffer with data converted from hexadecimal dump
/// from slice content.
/// \throws std::length_error if given buffer is too small.
byte *from_hex(byte *dest, size_t dest_size,
bool ignore_spaces = false) const;
/// \brief Returns the buffer size in bytes needed for conversion
/// hexadecimal dump from slice content to data.
MDBX_CXX11_CONSTEXPR size_t from_hex_bytes() const noexcept {
return length() >> 1;
}
/// \brief Fills the buffer by [Base58](https://en.wikipedia.org/wiki/Base58)
/// data dump of slice content.
/// \throws std::length_error if given buffer is too small.
char *to_base58(char *dest, size_t dest_size, unsigned wrap_width = 0) const;
/// \brief Returns the buffer size in bytes needed for
/// [Base58](https://en.wikipedia.org/wiki/Base58) data dump of slice content.
MDBX_CXX11_CONSTEXPR size_t
to_base58_bytes(unsigned wrap_width = 0) const noexcept {
const size_t bytes = length() / 8 * 11 + (length() % 8 * 43 + 31) / 32;
return wrap_width ? bytes + bytes / wrap_width : bytes;
}
/// \brief Fills the buffer with data converted from
/// [Base58](https://en.wikipedia.org/wiki/Base58) dump from slice content.
/// \throws std::length_error if given buffer is too small.
byte *from_base58(byte *dest, size_t dest_size,
bool ignore_spaces = false) const;
/// \brief Returns the buffer size in bytes needed for conversion
/// [Base58](https://en.wikipedia.org/wiki/Base58) dump to data.
MDBX_CXX11_CONSTEXPR size_t from_base58_bytes() const noexcept {
return length() / 11 * 8 + length() % 11 * 32 / 43;
}
/// \brief Fills the buffer by [Base64](https://en.wikipedia.org/wiki/Base64)
/// data dump.
/// \throws std::length_error if given buffer is too small.
char *to_base64(char *dest, size_t dest_size, unsigned wrap_width = 0) const;
/// \brief Returns the buffer size in bytes needed for
/// [Base64](https://en.wikipedia.org/wiki/Base64) data dump.
MDBX_CXX11_CONSTEXPR size_t
to_base64_bytes(unsigned wrap_width = 0) const noexcept {
const size_t bytes = (length() + 2) / 3 * 4;
return wrap_width ? bytes + bytes / wrap_width : bytes;
}
/// \brief Fills the buffer with data converted from
/// [Base64](https://en.wikipedia.org/wiki/Base64) dump.
/// \throws std::length_error if given buffer is too small.
byte *from_base64(byte *dest, size_t dest_size,
bool ignore_spaces = false) const;
/// \brief Returns the buffer size in bytes needed for conversion
/// [Base64](https://en.wikipedia.org/wiki/Base64) dump to data.
MDBX_CXX11_CONSTEXPR size_t from_base64_bytes() const noexcept {
return (length() + 3) / 4 * 3;
}
/// \brief Returns a string with a hexadecimal dump of the slice content.
template <class ALLOCATOR = legacy_allocator>
inline ::mdbx::string<ALLOCATOR>
hex_encode(bool uppercase = false,
const ALLOCATOR &allocator = ALLOCATOR()) const;
/// \brief Decodes hexadecimal dump from the slice content into returned data
/// string.
template <class ALLOCATOR = legacy_allocator>
inline ::mdbx::string<ALLOCATOR>
hex_decode(const ALLOCATOR &allocator = ALLOCATOR()) const;
/// \brief Returns a string with a
/// [Base58](https://en.wikipedia.org/wiki/Base58) dump of the slice content.
template <class ALLOCATOR = legacy_allocator>
inline ::mdbx::string<ALLOCATOR>
base58_encode(const ALLOCATOR &allocator = ALLOCATOR()) const;
/// \brief Decodes [Base58](https://en.wikipedia.org/wiki/Base58) dump
/// from the slice content into returned data string.
template <class ALLOCATOR = legacy_allocator>
inline ::mdbx::string<ALLOCATOR>
base58_decode(const ALLOCATOR &allocator = ALLOCATOR()) const;
/// \brief Returns a string with a
/// [Base64](https://en.wikipedia.org/wiki/Base64) dump of the slice content.
template <class ALLOCATOR = legacy_allocator>
inline ::mdbx::string<ALLOCATOR>
base64_encode(const ALLOCATOR &allocator = ALLOCATOR()) const;
/// \brief Decodes [Base64](https://en.wikipedia.org/wiki/Base64) dump
/// from the slice content into returned data string.
template <class ALLOCATOR = legacy_allocator>
inline ::mdbx::string<ALLOCATOR>
base64_decode(const ALLOCATOR &allocator = ALLOCATOR()) const;
/// \brief Checks whether the content of the slice is printable.
/// \param [in] disable_utf8 By default if `disable_utf8` is `false` function
/// checks that content bytes are printable ASCII-7 characters or a valid UTF8
/// sequences. Otherwise, if if `disable_utf8` is `true` function checks that
/// content bytes are printable extended 8-bit ASCII codes.
MDBX_NOTHROW_PURE_FUNCTION bool
is_printable(bool disable_utf8 = false) const noexcept;
/// \brief Checks whether the content of the slice is a hexadecimal dump.
/// \param [in] ignore_spaces If `true` function will skips spaces surrounding
/// (before, between and after) a encoded bytes. However, spaces should not
/// break a pair of characters encoding a single byte.
MDBX_NOTHROW_PURE_FUNCTION bool
is_hex(bool ignore_spaces = false) const noexcept;
MDBX_NOTHROW_PURE_FUNCTION bool
/// \brief Checks whether the content of the slice is a
/// [Base58](https://en.wikipedia.org/wiki/Base58) dump.
/// \param [in] ignore_spaces If `true` function will skips spaces surrounding
/// (before, between and after) a encoded bytes. However, spaces should not
/// break a code group of characters.
is_base58(bool ignore_spaces = false) const noexcept;
MDBX_NOTHROW_PURE_FUNCTION bool
/// \brief Checks whether the content of the slice is a
/// [Base64](https://en.wikipedia.org/wiki/Base64) dump.
/// \param [in] ignore_spaces If `true` function will skips spaces surrounding
/// (before, between and after) a encoded bytes. However, spaces should not
/// break a code group of characters.
is_base64(bool ignore_spaces = false) const noexcept;
#if defined(DOXYGEN) || \
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
/// \brief Return a string_view that references the same data as this slice.
template <class C, class T>
MDBX_CXX11_CONSTEXPR explicit
operator ::std::basic_string_view<C, T>() const noexcept {
static_assert(sizeof(C) == 1, "Must be single byte characters");
return ::std::basic_string_view<C, T>(char_ptr(), length());
}
/// \brief Return a string_view that references the same data as this slice.
template <class C = char, class T = ::std::char_traits<C>>
MDBX_CXX11_CONSTEXPR ::std::basic_string_view<C, T>
string_view() const noexcept {
static_assert(sizeof(C) == 1, "Must be single byte characters");
return ::std::basic_string_view<C, T>(char_ptr(), length());
}
#endif /* __cpp_lib_string_view >= 201606L */
inline void swap(slice &other) noexcept;
#if defined(DOXYGEN) || \
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
template <class C, class T>
void swap(::std::basic_string_view<C, T> &view) noexcept {
static_assert(sizeof(C) == 1, "Must be single byte characters");
const auto temp = ::std::basic_string_view<C, T>(*this);
*this = view;
view = temp;
}
#endif /* __cpp_lib_string_view >= 201606L */
/// \brief Returns casted to pointer to byte an address of data.
MDBX_CXX11_CONSTEXPR const byte *byte_ptr() const noexcept;
/// \brief Returns casted to pointer to char an address of data.
MDBX_CXX11_CONSTEXPR const char *char_ptr() const noexcept;
/// \brief Return a pointer to the beginning of the referenced data.
MDBX_CXX11_CONSTEXPR const void *data() const noexcept;
/// \brief Returns the number of bytes.
MDBX_CXX11_CONSTEXPR size_t length() const noexcept;
/// \brief Checks whether the slice is empty.
MDBX_CXX11_CONSTEXPR bool empty() const noexcept;
/// \brief Checks whether the slice data pointer is nullptr.
MDBX_CXX11_CONSTEXPR bool is_null() const noexcept;
/// \brief Returns the number of bytes.
MDBX_CXX11_CONSTEXPR size_t size() const noexcept;
/// \brief Returns true if slice is not empty.
MDBX_CXX11_CONSTEXPR operator bool() const noexcept;
/// \brief Depletes content of slice and make it invalid.
inline void invalidate() noexcept;
/// \brief Makes the slice empty and referencing to nothing.
inline void clear() noexcept;
/// \brief Drops the first "n" bytes from this slice.
/// \pre REQUIRES: `n <= size()`
inline void remove_prefix(size_t n) noexcept;
/// \brief Drops the last "n" bytes from this slice.
/// \pre REQUIRES: `n <= size()`
inline void remove_suffix(size_t n) noexcept;
/// \brief Drops the first "n" bytes from this slice.
/// \throws std::out_of_range if `n > size()`
inline void safe_remove_prefix(size_t n);
/// \brief Drops the last "n" bytes from this slice.
/// \throws std::out_of_range if `n > size()`
inline void safe_remove_suffix(size_t n);
/// \brief Checks if the data starts with the given prefix.
MDBX_NOTHROW_PURE_FUNCTION inline bool
starts_with(const slice &prefix) const noexcept;
/// \brief Checks if the data ends with the given suffix.
MDBX_NOTHROW_PURE_FUNCTION inline bool
ends_with(const slice &suffix) const noexcept;
/// \brief Returns the nth byte in the referenced data.
/// \pre REQUIRES: `n < size()`
inline byte operator[](size_t n) const noexcept;
/// \brief Returns the nth byte in the referenced data with bounds checking.
/// \throws std::out_of_range if `n >= size()`
inline byte at(size_t n) const;
/// \brief Returns the first "n" bytes of the slice.
/// \pre REQUIRES: `n <= size()`
inline slice head(size_t n) const noexcept;
/// \brief Returns the last "n" bytes of the slice.
/// \pre REQUIRES: `n <= size()`
inline slice tail(size_t n) const noexcept;
/// \brief Returns the middle "n" bytes of the slice.
/// \pre REQUIRES: `from + n <= size()`
inline slice middle(size_t from, size_t n) const noexcept;
/// \brief Returns the first "n" bytes of the slice.
/// \throws std::out_of_range if `n >= size()`
inline slice safe_head(size_t n) const;
/// \brief Returns the last "n" bytes of the slice.
/// \throws std::out_of_range if `n >= size()`
inline slice safe_tail(size_t n) const;
/// \brief Returns the middle "n" bytes of the slice.
/// \throws std::out_of_range if `from + n >= size()`
inline slice safe_middle(size_t from, size_t n) const;
/// \brief Returns the hash value of referenced data.
/// \attention Function implementation and returned hash values may changed
/// version to version, and in future the t1ha3 will be used here. Therefore
/// values obtained from this function shouldn't be persisted anywhere.
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX14_CONSTEXPR size_t
hash_value() const noexcept;
/// \brief Three-way fast non-lexicographically length-based comparison.
/// \return value:
/// == 0 if "a" == "b",
/// < 0 if "a" shorter than "b",
/// > 0 if "a" longer than "b",
/// < 0 if "a" length-equal and lexicographically less than "b",
/// > 0 if "a" length-equal and lexicographically great than "b".
MDBX_NOTHROW_PURE_FUNCTION static inline intptr_t
compare_fast(const slice &a, const slice &b) noexcept;
/// \brief Three-way lexicographically comparison.
/// \return value:
/// < 0 if "a" < "b",
/// == 0 if "a" == "b",
/// > 0 if "a" > "b".
MDBX_NOTHROW_PURE_FUNCTION static inline intptr_t
compare_lexicographically(const slice &a, const slice &b) noexcept;
friend inline bool operator==(const slice &a, const slice &b) noexcept;
friend inline bool operator<(const slice &a, const slice &b) noexcept;
friend inline bool operator>(const slice &a, const slice &b) noexcept;
friend inline bool operator<=(const slice &a, const slice &b) noexcept;
friend inline bool operator>=(const slice &a, const slice &b) noexcept;
friend inline bool operator!=(const slice &a, const slice &b) noexcept;
/// \brief Checks the slice is not refers to null address or has zero length.
MDBX_CXX11_CONSTEXPR bool is_valid() const noexcept {
return !(iov_base == nullptr && iov_len != 0);
}
/// \brief Build an invalid slice which non-zero length and refers to null
/// address.
inline static slice invalid() noexcept { return slice(size_t(-1)); }
protected:
MDBX_CXX11_CONSTEXPR slice(size_t invalid_length) noexcept
: ::MDBX_val({nullptr, invalid_length}) {}
};
//------------------------------------------------------------------------------
/// \brief The chunk of data stored inside the buffer or located outside it.
template <class ALLOCATOR = legacy_allocator> class buffer {
friend class txn;
/* FIXME: replace std::string with custom silo.
* The std::string<char> does not guarantee any alignment for allocated
* buffer. For instance short values may be stored within internal inplace
* buffer, which might odd address. Moreover, allocator for the `char` type
* may return unaligned/odd address. This may UB for placing a 32-bit and
* 64-bit values.
* So seems the std::string<> should be replaced with ad hoc solution. */
using silo = ::mdbx::string<ALLOCATOR>;
silo silo_;
::mdbx::slice slice_;
void insulate() {
assert(is_reference());
silo_.assign(slice_.char_ptr(), slice_.length());
slice_.iov_base = const_cast<char *>(silo_.data());
}
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR const byte *
silo_begin() const noexcept {
return static_cast<const byte *>(static_cast<const void *>(silo_.data()));
}
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR const byte *
silo_end() const noexcept {
return silo_begin() + silo_.capacity();
}
struct data_preserver : public exception_thunk {
buffer data;
data_preserver(ALLOCATOR &allocator) : data(allocator) {}
static int callback(void *context, MDBX_val *target, const void *src,
size_t bytes) noexcept;
MDBX_CXX11_CONSTEXPR operator MDBX_preserve_func() const noexcept {
return callback;
}
MDBX_CXX11_CONSTEXPR operator const buffer &() const noexcept {
return data;
}
MDBX_CXX11_CONSTEXPR operator buffer &() noexcept { return data; }
};
public:
/// \todo buffer& operator<<(buffer&, ...) for writing
/// \todo buffer& operator>>(buffer&, ...) for reading (delegated to slice)
/// \todo template<class X> key(X) for encoding keys while writing
using allocator_type = ALLOCATOR;
enum : size_t {
max_length = MDBX_MAXDATASIZE,
default_shrink_threshold = 1024
};
/// \brief Returns the associated allocator.
MDBX_CXX20_CONSTEXPR allocator_type get_allocator() const {
return silo_.get_allocator();
}
/// \brief Checks whether data chunk stored inside the buffer, otherwise
/// buffer just refers to data located outside the buffer.
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR bool
is_freestanding() const noexcept {
return size_t(byte_ptr() - silo_begin()) < silo_.capacity();
}
/// \brief Checks whether the buffer just refers to data located outside
/// the buffer, rather than stores it.
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR bool
is_reference() const noexcept {
return !is_freestanding();
}
/// \brief Returns the number of bytes that can be held in currently allocated
/// storage.
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR size_t
capacity() const noexcept {
return is_freestanding() ? silo_.capacity() : 0;
}
/// \brief Returns the number of bytes that available in currently allocated
/// storage ahead the currently beginning of data.
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR size_t
headroom() const noexcept {
return is_freestanding() ? slice_.byte_ptr() - silo_begin() : 0;
}
/// \brief Returns the number of bytes that available in currently allocated
/// storage after the currently data end.
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR size_t
tailroom() const noexcept {
return is_freestanding() ? capacity() - headroom() - slice_.length() : 0;
}
/// \brief Returns casted to const pointer to byte an address of data.
MDBX_CXX11_CONSTEXPR const byte *byte_ptr() const noexcept {
return slice_.byte_ptr();
}
/// \brief Returns casted to pointer to byte an address of data.
/// \pre REQUIRES: The buffer should store data chunk, but not referenced to
/// an external one.
MDBX_CXX11_CONSTEXPR byte *byte_ptr() noexcept {
assert(is_freestanding());
return const_cast<byte *>(slice_.byte_ptr());
}
/// \brief Returns casted to const pointer to char an address of data.
MDBX_CXX11_CONSTEXPR const char *char_ptr() const noexcept {
return slice_.char_ptr();
}
/// \brief Returns casted to pointer to char an address of data.
/// \pre REQUIRES: The buffer should store data chunk, but not referenced to
/// an external one.
MDBX_CXX11_CONSTEXPR char *char_ptr() noexcept {
assert(is_freestanding());
return const_cast<char *>(slice_.char_ptr());
}
/// \brief Return a const pointer to the beginning of the referenced data.
MDBX_CXX11_CONSTEXPR const void *data() const noexcept {
return slice_.data();
}
/// \brief Return a pointer to the beginning of the referenced data.
/// \pre REQUIRES: The buffer should store data chunk, but not referenced to
/// an external one.
MDBX_CXX11_CONSTEXPR void *data() noexcept {
assert(is_freestanding());
return const_cast<void *>(slice_.data());
}
/// \brief Returns the number of bytes.
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR size_t
length() const noexcept {
return MDBX_CONSTEXPR_ASSERT(is_reference() ||
slice_.length() + headroom() ==
silo_.length()),
slice_.length();
}
void make_freestanding() {
if (is_reference())
insulate();
}
buffer(const ::mdbx::slice &src, bool make_reference,
const allocator_type &allocator = allocator_type())
: silo_(allocator), slice_(src) {
if (!make_reference)
insulate();
}
buffer(const buffer &src, bool make_reference,
const allocator_type &allocator = allocator_type())
: buffer(src.slice_, make_reference, allocator) {}
buffer(const void *ptr, size_t bytes, bool make_reference,
const allocator_type &allocator = allocator_type())
: buffer(::mdbx::slice(ptr, bytes), make_reference, allocator) {}
template <class C, class T, class A>
buffer(const ::std::basic_string<C, T, A> &str, bool make_reference,
const allocator_type &allocator = allocator_type())
: buffer(::mdbx::slice(str), make_reference, allocator) {}
buffer(const char *c_str, bool make_reference,
const allocator_type &allocator = allocator_type())
: buffer(::mdbx::slice(c_str), make_reference, allocator) {}
#if defined(DOXYGEN) || \
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
template <class C, class T>
buffer(const ::std::basic_string_view<C, T> &view, bool make_reference,
const allocator_type &allocator = allocator_type())
: buffer(::mdbx::slice(view), make_reference, allocator) {}
#endif /* __cpp_lib_string_view >= 201606L */
MDBX_CXX20_CONSTEXPR
buffer(const ::mdbx::slice &src,
const allocator_type &allocator = allocator_type())
: silo_(src.char_ptr(), src.length(), allocator), slice_(silo_) {}
MDBX_CXX20_CONSTEXPR
buffer(const buffer &src, const allocator_type &allocator = allocator_type())
: buffer(src.slice_, allocator) {}
MDBX_CXX20_CONSTEXPR
buffer(const void *ptr, size_t bytes,
const allocator_type &allocator = allocator_type())
: buffer(::mdbx::slice(ptr, bytes), allocator) {}
template <class C, class T, class A>
MDBX_CXX20_CONSTEXPR
buffer(const ::std::basic_string<C, T, A> &str,