-
Notifications
You must be signed in to change notification settings - Fork 4
/
olc_art.cpp
1252 lines (961 loc) · 40.1 KB
/
olc_art.cpp
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
// Copyright 2019-2024 Laurynas Biveinis
#include "art_internal.hpp"
#include "global.hpp"
#include "olc_art.hpp"
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <memory>
#include <optional>
#include <type_traits>
#include <utility>
#include "art_common.hpp"
#include "art_internal_impl.hpp"
#include "assert.hpp"
#include "node_type.hpp"
#include "optimistic_lock.hpp"
#include "qsbr.hpp"
#include "qsbr_ptr.hpp"
namespace unodb::detail {
struct [[nodiscard]] olc_node_header {
[[nodiscard]] constexpr optimistic_lock &lock() const noexcept {
return m_lock;
}
#ifndef NDEBUG
static void check_on_dealloc(const void *ptr) noexcept {
static_cast<const olc_node_header *>(ptr)->m_lock.check_on_dealloc();
}
#endif
private:
mutable optimistic_lock m_lock;
};
static_assert(std::is_standard_layout_v<olc_node_header>);
template <class Header, class Db>
class db_leaf_qsbr_deleter {
public:
using leaf_type = basic_leaf<Header>;
static_assert(std::is_trivially_destructible_v<leaf_type>);
constexpr explicit db_leaf_qsbr_deleter(
Db &db_ UNODB_DETAIL_LIFETIMEBOUND) noexcept
: db_instance{db_} {}
void operator()(leaf_type *to_delete) const {
const auto leaf_size = to_delete->get_size();
this_thread().on_next_epoch_deallocate(to_delete, leaf_size
#ifndef NDEBUG
,
olc_node_header::check_on_dealloc
#endif
);
db_instance.decrement_leaf_count(leaf_size);
}
~db_leaf_qsbr_deleter() = default;
db_leaf_qsbr_deleter(const db_leaf_qsbr_deleter<Header, Db> &) = default;
db_leaf_qsbr_deleter<Header, Db> &operator=(
const db_leaf_qsbr_deleter<Header, Db> &) = delete;
db_leaf_qsbr_deleter(db_leaf_qsbr_deleter<Header, Db> &&) = delete;
db_leaf_qsbr_deleter<Header, Db> &operator=(
db_leaf_qsbr_deleter<Header, Db> &&) = delete;
private:
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
Db &db_instance;
};
} // namespace unodb::detail
namespace {
template <class INode>
using db_inode_qsbr_deleter_parent =
unodb::detail::basic_db_inode_deleter<INode, unodb::olc_db>;
} // namespace
namespace unodb::detail {
template <class INode>
class db_inode_qsbr_deleter : public db_inode_qsbr_deleter_parent<INode> {
public:
using db_inode_qsbr_deleter_parent<INode>::db_inode_qsbr_deleter_parent;
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
void operator()(INode *inode_ptr) {
static_assert(std::is_trivially_destructible_v<INode>);
this_thread().on_next_epoch_deallocate(inode_ptr, sizeof(INode)
#ifndef NDEBUG
,
olc_node_header::check_on_dealloc
#endif
);
this->get_db().template decrement_inode_count<INode>();
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
};
} // namespace unodb::detail
namespace {
class olc_inode;
class olc_inode_4;
class olc_inode_16;
class olc_inode_48;
class olc_inode_256;
using olc_inode_defs =
unodb::detail::basic_inode_def<olc_inode, olc_inode_4, olc_inode_16,
olc_inode_48, olc_inode_256>;
using olc_art_policy =
unodb::detail::basic_art_policy<unodb::olc_db, unodb::in_critical_section,
unodb::detail::olc_node_ptr, olc_inode_defs,
unodb::detail::db_inode_qsbr_deleter,
unodb::detail::db_leaf_qsbr_deleter>;
using olc_db_leaf_unique_ptr = olc_art_policy::db_leaf_unique_ptr;
using leaf = olc_art_policy::leaf_type;
using olc_inode_base = unodb::detail::basic_inode_impl<olc_art_policy>;
class olc_inode : public olc_inode_base {};
[[nodiscard]] auto &node_ptr_lock(
const unodb::detail::olc_node_ptr
&node UNODB_DETAIL_LIFETIMEBOUND) noexcept {
return node.ptr<unodb::detail::olc_node_header *>()->lock();
}
#ifndef NDEBUG
[[nodiscard]] auto &node_ptr_lock(
const leaf *const node UNODB_DETAIL_LIFETIMEBOUND) noexcept {
return node->lock();
}
#endif
template <class INode>
[[nodiscard]] constexpr auto &lock(
const INode &inode UNODB_DETAIL_LIFETIMEBOUND) noexcept {
return inode.lock();
}
template <class T>
[[nodiscard]] T &obsolete(T &t UNODB_DETAIL_LIFETIMEBOUND,
unodb::optimistic_lock::write_guard &guard) noexcept {
UNODB_DETAIL_ASSERT(guard.guards(lock(t)));
// My first attempt was to pass guard by value and let it destruct at the end
// of this scope, but due to copy elision (?) the destructor got called way
// too late, after the owner node was destructed.
guard.unlock_and_obsolete();
return t;
}
[[nodiscard]] inline auto obsolete_child_by_index(
std::uint8_t child UNODB_DETAIL_LIFETIMEBOUND,
unodb::optimistic_lock::write_guard &guard) noexcept {
guard.unlock_and_obsolete();
return child;
}
} // namespace
namespace unodb {
template <class INode>
constexpr void olc_db::increment_inode_count() noexcept {
static_assert(olc_inode_defs::is_inode<INode>());
node_counts[as_i<INode::type>].fetch_add(1, std::memory_order_relaxed);
increase_memory_use(sizeof(INode));
}
} // namespace unodb
namespace unodb::detail {
// Wrap olc_inode_add in a struct so that the latter and not the former could be
// declared as friend of olc_db, avoiding the need to forward declare the likes
// of olc_db_leaf_unique_ptr.
struct olc_impl_helpers {
// GCC 10 diagnoses parameters that are present only in uninstantiated if
// constexpr branch, such as node_in_parent for olc_inode_256.
UNODB_DETAIL_DISABLE_GCC_10_WARNING("-Wunused-parameter")
template <class INode>
[[nodiscard]] static std::optional<in_critical_section<olc_node_ptr> *>
add_or_choose_subtree(
INode &inode, std::byte key_byte, art_key k, value_view v,
olc_db &db_instance, tree_depth depth,
optimistic_lock::read_critical_section &node_critical_section,
in_critical_section<olc_node_ptr> *node_in_parent,
optimistic_lock::read_critical_section &parent_critical_section,
olc_db_leaf_unique_ptr &cached_leaf);
UNODB_DETAIL_RESTORE_GCC_10_WARNINGS()
template <class INode>
[[nodiscard]] static std::optional<bool> remove_or_choose_subtree(
INode &inode, std::byte key_byte, detail::art_key k, olc_db &db_instance,
optimistic_lock::read_critical_section &parent_critical_section,
optimistic_lock::read_critical_section &node_critical_section,
in_critical_section<olc_node_ptr> *node_in_parent,
in_critical_section<olc_node_ptr> **child_in_parent,
optimistic_lock::read_critical_section *child_critical_section,
node_type *child_type, olc_node_ptr *child);
olc_impl_helpers() = delete;
};
} // namespace unodb::detail
namespace {
class [[nodiscard]] olc_inode_4 final
: public unodb::detail::basic_inode_4<olc_art_policy> {
using parent_class = basic_inode_4<olc_art_policy>;
public:
using parent_class::parent_class;
void init(db &db_instance, olc_inode_16 &source_node,
unodb::optimistic_lock::write_guard &source_node_guard,
std::uint8_t child_to_delete,
unodb::optimistic_lock::write_guard &child_guard);
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
void init(unodb::detail::art_key k1, unodb::detail::art_key shifted_k2,
unodb::detail::tree_depth depth, leaf *child1,
olc_db_leaf_unique_ptr &&child2) noexcept {
UNODB_DETAIL_ASSERT(node_ptr_lock(child1).is_write_locked());
parent_class::init(k1, shifted_k2, depth, child1, std::move(child2));
}
void init(unodb::detail::olc_node_ptr source_node, unsigned len,
unodb::detail::tree_depth depth, olc_db_leaf_unique_ptr &&child1) {
UNODB_DETAIL_ASSERT(node_ptr_lock(source_node).is_write_locked());
parent_class::init(source_node, len, depth, std::move(child1));
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
template <typename... Args>
[[nodiscard]] auto add_or_choose_subtree(Args &&...args) {
return unodb::detail::olc_impl_helpers::add_or_choose_subtree(
*this, std::forward<Args>(args)...);
}
template <typename... Args>
[[nodiscard]] auto remove_or_choose_subtree(Args &&...args) {
return unodb::detail::olc_impl_helpers::remove_or_choose_subtree(
*this, std::forward<Args>(args)...);
}
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
void remove(std::uint8_t child_index, unodb::olc_db &db_instance) noexcept {
UNODB_DETAIL_ASSERT(::lock(*this).is_write_locked());
basic_inode_4::remove(child_index, db_instance);
}
[[nodiscard]] auto leave_last_child(std::uint8_t child_to_delete,
unodb::olc_db &db_instance) noexcept {
UNODB_DETAIL_ASSERT(::lock(*this).is_obsoleted_by_this_thread());
UNODB_DETAIL_ASSERT(node_ptr_lock(children[child_to_delete].load())
.is_obsoleted_by_this_thread());
return basic_inode_4::leave_last_child(child_to_delete, db_instance);
}
// cppcheck-suppress duplInheritedMember
[[gnu::cold]] UNODB_DETAIL_NOINLINE void dump(std::ostream &os) const {
os << ", ";
::lock(*this).dump(os);
basic_inode_4::dump(os);
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
};
// 48 (or 56) == sizeof(inode_4)
#ifndef _MSC_VER
#ifdef NDEBUG
static_assert(sizeof(olc_inode_4) == 48 + 8);
#else
static_assert(sizeof(olc_inode_4) == 48 + 24);
#endif
#else // #ifndef _MSC_VER
#ifdef NDEBUG
static_assert(sizeof(olc_inode_4) == 56 + 8);
#else
static_assert(sizeof(olc_inode_4) == 56 + 24);
#endif
#endif // #ifndef _MSC_VER
class [[nodiscard]] olc_inode_16 final
: public unodb::detail::basic_inode_16<olc_art_policy> {
using parent_class = basic_inode_16<olc_art_policy>;
public:
using parent_class::parent_class;
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
void init(db &db_instance, olc_inode_4 &source_node,
unodb::optimistic_lock::write_guard &source_node_guard,
olc_db_leaf_unique_ptr &&child,
unodb::detail::tree_depth depth) noexcept {
UNODB_DETAIL_ASSERT(source_node_guard.guards(::lock(source_node)));
parent_class::init(db_instance, obsolete(source_node, source_node_guard),
std::move(child), depth);
UNODB_DETAIL_ASSERT_INACTIVE(source_node_guard);
}
void init(db &db_instance, olc_inode_48 &source_node,
unodb::optimistic_lock::write_guard &source_node_guard,
std::uint8_t child_to_delete,
unodb::optimistic_lock::write_guard &child_guard) noexcept;
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
template <typename... Args>
[[nodiscard]] auto add_or_choose_subtree(Args &&...args) {
return unodb::detail::olc_impl_helpers::add_or_choose_subtree(
*this, std::forward<Args>(args)...);
}
template <typename... Args>
[[nodiscard]] auto remove_or_choose_subtree(Args &&...args) {
return unodb::detail::olc_impl_helpers::remove_or_choose_subtree(
*this, std::forward<Args>(args)...);
}
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
void remove(std::uint8_t child_index, unodb::olc_db &db_instance) noexcept {
UNODB_DETAIL_ASSERT(::lock(*this).is_write_locked());
basic_inode_16::remove(child_index, db_instance);
}
// cppcheck-suppress duplInheritedMember
[[nodiscard]] find_result find_child(std::byte key_byte) noexcept {
#ifdef UNODB_DETAIL_THREAD_SANITIZER
const auto children_count_ = this->get_children_count();
for (unsigned i = 0; i < children_count_; ++i)
if (keys.byte_array[i] == key_byte)
return std::make_pair(i, &children[i]);
return parent_class::child_not_found;
#else
return basic_inode_16::find_child(key_byte);
#endif
}
// cppcheck-suppress duplInheritedMember
[[gnu::cold]] UNODB_DETAIL_NOINLINE void dump(std::ostream &os) const {
os << ", ";
::lock(*this).dump(os);
basic_inode_16::dump(os);
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
};
// 160 == sizeof(inode_16)
#ifdef NDEBUG
static_assert(sizeof(olc_inode_16) == 160 + 16);
#else // #ifdef NDEBUG
static_assert(sizeof(olc_inode_16) == 160 + 32);
#endif // #ifdef NDEBUG
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
void olc_inode_4::init(db &db_instance, olc_inode_16 &source_node,
unodb::optimistic_lock::write_guard &source_node_guard,
std::uint8_t child_to_delete,
unodb::optimistic_lock::write_guard &child_guard) {
UNODB_DETAIL_ASSERT(source_node_guard.guards(::lock(source_node)));
UNODB_DETAIL_ASSERT(child_guard.active());
parent_class::init(db_instance, obsolete(source_node, source_node_guard),
obsolete_child_by_index(child_to_delete, child_guard));
UNODB_DETAIL_ASSERT_INACTIVE(source_node_guard);
UNODB_DETAIL_ASSERT_INACTIVE(child_guard);
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
class [[nodiscard]] olc_inode_48 final
: public unodb::detail::basic_inode_48<olc_art_policy> {
using parent_class = basic_inode_48<olc_art_policy>;
public:
using parent_class::parent_class;
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
void init(db &db_instance, olc_inode_16 &source_node,
unodb::optimistic_lock::write_guard &source_node_guard,
olc_db_leaf_unique_ptr &&child,
unodb::detail::tree_depth depth) noexcept {
UNODB_DETAIL_ASSERT(source_node_guard.guards(::lock(source_node)));
parent_class::init(db_instance, obsolete(source_node, source_node_guard),
std::move(child), depth);
UNODB_DETAIL_ASSERT_INACTIVE(source_node_guard);
}
void init(db &db_instance, olc_inode_256 &source_node,
unodb::optimistic_lock::write_guard &source_node_guard,
std::uint8_t child_to_delete,
unodb::optimistic_lock::write_guard &child_guard) noexcept;
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
template <typename... Args>
[[nodiscard]] auto add_or_choose_subtree(Args &&...args) {
return unodb::detail::olc_impl_helpers::add_or_choose_subtree(
*this, std::forward<Args>(args)...);
}
template <typename... Args>
[[nodiscard]] auto remove_or_choose_subtree(Args &&...args) {
return unodb::detail::olc_impl_helpers::remove_or_choose_subtree(
*this, std::forward<Args>(args)...);
}
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
void remove(std::uint8_t child_index, unodb::olc_db &db_instance) noexcept {
UNODB_DETAIL_ASSERT(::lock(*this).is_write_locked());
basic_inode_48::remove(child_index, db_instance);
}
// cppcheck-suppress duplInheritedMember
[[gnu::cold]] UNODB_DETAIL_NOINLINE void dump(std::ostream &os) const {
os << ", ";
::lock(*this).dump(os);
basic_inode_48::dump(os);
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
};
// sizeof(inode_48) == 672 on AVX2, 656 otherwise
#ifdef NDEBUG
// AVX2 too. Padding?
static_assert(sizeof(olc_inode_48) == 656 + 16);
#else // #ifdef NDEBUG
#if defined(UNODB_DETAIL_AVX2)
static_assert(sizeof(olc_inode_48) == 672 + 32);
#else
static_assert(sizeof(olc_inode_48) == 656 + 32);
#endif
#endif // #ifdef NDEBUG
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
void olc_inode_16::init(
db &db_instance, olc_inode_48 &source_node,
unodb::optimistic_lock::write_guard &source_node_guard,
std::uint8_t child_to_delete,
unodb::optimistic_lock::write_guard &child_guard) noexcept {
UNODB_DETAIL_ASSERT(source_node_guard.guards(::lock(source_node)));
UNODB_DETAIL_ASSERT(child_guard.active());
parent_class::init(db_instance, obsolete(source_node, source_node_guard),
obsolete_child_by_index(child_to_delete, child_guard));
UNODB_DETAIL_ASSERT_INACTIVE(source_node_guard);
UNODB_DETAIL_ASSERT_INACTIVE(child_guard);
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
class [[nodiscard]] olc_inode_256 final
: public unodb::detail::basic_inode_256<olc_art_policy> {
using parent_class = basic_inode_256<olc_art_policy>;
public:
using parent_class::parent_class;
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
void init(db &db_instance, olc_inode_48 &source_node,
unodb::optimistic_lock::write_guard &source_node_guard,
olc_db_leaf_unique_ptr &&child,
unodb::detail::tree_depth depth) noexcept {
UNODB_DETAIL_ASSERT(source_node_guard.guards(::lock(source_node)));
parent_class::init(db_instance, obsolete(source_node, source_node_guard),
std::move(child), depth);
UNODB_DETAIL_ASSERT_INACTIVE(source_node_guard);
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
template <typename... Args>
[[nodiscard]] auto add_or_choose_subtree(Args &&...args) {
return unodb::detail::olc_impl_helpers::add_or_choose_subtree(
*this, std::forward<Args>(args)...);
}
template <typename... Args>
[[nodiscard]] auto remove_or_choose_subtree(Args &&...args) {
return unodb::detail::olc_impl_helpers::remove_or_choose_subtree(
*this, std::forward<Args>(args)...);
}
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
void remove(std::uint8_t child_index, unodb::olc_db &db_instance) noexcept {
UNODB_DETAIL_ASSERT(::lock(*this).is_write_locked());
basic_inode_256::remove(child_index, db_instance);
}
// cppcheck-suppress duplInheritedMember
[[gnu::cold]] UNODB_DETAIL_NOINLINE void dump(std::ostream &os) const {
os << ", ";
::lock(*this).dump(os);
basic_inode_256::dump(os);
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
};
// 2064 == sizeof(inode_256)
#ifdef NDEBUG
static_assert(sizeof(olc_inode_256) == 2064 + 8);
#else
static_assert(sizeof(olc_inode_256) == 2064 + 24);
#endif
UNODB_DETAIL_DISABLE_MSVC_WARNING(26434)
void olc_inode_48::init(
db &db_instance, olc_inode_256 &source_node,
unodb::optimistic_lock::write_guard &source_node_guard,
std::uint8_t child_to_delete,
unodb::optimistic_lock::write_guard &child_guard) noexcept {
UNODB_DETAIL_ASSERT(source_node_guard.guards(::lock(source_node)));
UNODB_DETAIL_ASSERT(child_guard.active());
parent_class::init(db_instance, obsolete(source_node, source_node_guard),
obsolete_child_by_index(child_to_delete, child_guard));
UNODB_DETAIL_ASSERT_INACTIVE(source_node_guard);
UNODB_DETAIL_ASSERT_INACTIVE(child_guard);
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
void create_leaf_if_needed(olc_db_leaf_unique_ptr &cached_leaf,
unodb::detail::art_key k, unodb::value_view v,
unodb::olc_db &db_instance) {
if (UNODB_DETAIL_LIKELY(cached_leaf == nullptr)) {
UNODB_DETAIL_ASSERT(&cached_leaf.get_deleter().get_db() == &db_instance);
// Do not assign because we do not need to assign the deleter
// NOLINTNEXTLINE(misc-uniqueptr-reset-release)
cached_leaf.reset(
olc_art_policy::make_db_leaf_ptr(k, v, db_instance).release());
}
}
} // namespace
namespace unodb::detail {
UNODB_DETAIL_DISABLE_MSVC_WARNING(26460)
template <class INode>
[[nodiscard]] std::optional<in_critical_section<olc_node_ptr> *>
olc_impl_helpers::add_or_choose_subtree(
INode &inode, std::byte key_byte, art_key k, value_view v,
olc_db &db_instance, tree_depth depth,
optimistic_lock::read_critical_section &node_critical_section,
in_critical_section<olc_node_ptr> *node_in_parent,
optimistic_lock::read_critical_section &parent_critical_section,
olc_db_leaf_unique_ptr &cached_leaf) {
auto *const child_in_parent = inode.find_child(key_byte).second;
if (child_in_parent == nullptr) {
create_leaf_if_needed(cached_leaf, k, v, db_instance);
const auto children_count = inode.get_children_count();
if constexpr (!std::is_same_v<INode, olc_inode_256>) {
if (UNODB_DETAIL_UNLIKELY(children_count == INode::capacity)) {
auto larger_node{
INode::larger_derived_type::create(db_instance, inode)};
{
const optimistic_lock::write_guard write_unlock_on_exit{
std::move(parent_critical_section)};
if (UNODB_DETAIL_UNLIKELY(write_unlock_on_exit.must_restart()))
return {}; // LCOV_EXCL_LINE
optimistic_lock::write_guard node_write_guard{
std::move(node_critical_section)};
if (UNODB_DETAIL_UNLIKELY(node_write_guard.must_restart())) return {};
larger_node->init(db_instance, inode, node_write_guard,
std::move(cached_leaf), depth);
*node_in_parent = detail::olc_node_ptr{
larger_node.release(), INode::larger_derived_type::type};
UNODB_DETAIL_ASSERT_INACTIVE(node_write_guard);
}
db_instance.account_growing_inode<INode::larger_derived_type::type>();
return child_in_parent;
}
}
const optimistic_lock::write_guard write_unlock_on_exit{
std::move(node_critical_section)};
if (UNODB_DETAIL_UNLIKELY(write_unlock_on_exit.must_restart())) return {};
if (UNODB_DETAIL_UNLIKELY(!parent_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
inode.add_to_nonfull(std::move(cached_leaf), depth, children_count);
}
return child_in_parent;
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
template <class INode>
[[nodiscard]] std::optional<bool> olc_impl_helpers::remove_or_choose_subtree(
INode &inode, std::byte key_byte, detail::art_key k, olc_db &db_instance,
optimistic_lock::read_critical_section &parent_critical_section,
optimistic_lock::read_critical_section &node_critical_section,
in_critical_section<olc_node_ptr> *node_in_parent,
in_critical_section<olc_node_ptr> **child_in_parent,
optimistic_lock::read_critical_section *child_critical_section,
node_type *child_type, olc_node_ptr *child) {
const auto [child_i, found_child]{inode.find_child(key_byte)};
if (found_child == nullptr) {
if (UNODB_DETAIL_UNLIKELY(!parent_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
if (UNODB_DETAIL_UNLIKELY(!node_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
return false;
}
*child = found_child->load();
if (UNODB_DETAIL_UNLIKELY(!node_critical_section.check())) return {};
auto &child_lock{node_ptr_lock(*child)};
*child_critical_section = child_lock.try_read_lock();
if (UNODB_DETAIL_UNLIKELY(child_critical_section->must_restart())) return {};
*child_type = child->type();
if (*child_type != node_type::LEAF) {
*child_in_parent = found_child;
if (UNODB_DETAIL_UNLIKELY(!parent_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
return true;
}
const auto *const leaf{child->ptr<::leaf *>()};
if (!leaf->matches(k)) {
if (UNODB_DETAIL_UNLIKELY(!parent_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
if (UNODB_DETAIL_UNLIKELY(!node_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
if (UNODB_DETAIL_UNLIKELY(!child_critical_section->try_read_unlock()))
return {}; // LCOV_EXCL_LINE
return false;
}
const auto is_node_min_size{inode.is_min_size()};
if (UNODB_DETAIL_LIKELY(!is_node_min_size)) {
if (UNODB_DETAIL_UNLIKELY(!parent_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
const optimistic_lock::write_guard node_guard{
std::move(node_critical_section)};
if (UNODB_DETAIL_UNLIKELY(node_guard.must_restart())) return {};
optimistic_lock::write_guard child_guard{
std::move(*child_critical_section)};
if (UNODB_DETAIL_UNLIKELY(child_guard.must_restart())) return {};
child_guard.unlock_and_obsolete();
inode.remove(child_i, db_instance);
*child_in_parent = nullptr;
return true;
}
UNODB_DETAIL_ASSERT(is_node_min_size);
if constexpr (std::is_same_v<INode, olc_inode_4>) {
const optimistic_lock::write_guard parent_guard{
std::move(parent_critical_section)};
if (UNODB_DETAIL_UNLIKELY(parent_guard.must_restart())) return {};
optimistic_lock::write_guard node_guard{std::move(node_critical_section)};
if (UNODB_DETAIL_UNLIKELY(node_guard.must_restart())) return {};
optimistic_lock::write_guard child_guard{
std::move(*child_critical_section)};
if (UNODB_DETAIL_UNLIKELY(child_guard.must_restart())) return {};
auto current_node{
olc_art_policy::make_db_inode_reclaimable_ptr(&inode, db_instance)};
node_guard.unlock_and_obsolete();
child_guard.unlock_and_obsolete();
*node_in_parent = current_node->leave_last_child(child_i, db_instance);
UNODB_DETAIL_ASSERT_INACTIVE(node_guard);
UNODB_DETAIL_ASSERT_INACTIVE(child_guard);
*child_in_parent = nullptr;
} else {
auto smaller_node{INode::smaller_derived_type::create(db_instance, inode)};
const optimistic_lock::write_guard parent_guard{
std::move(parent_critical_section)};
if (UNODB_DETAIL_UNLIKELY(parent_guard.must_restart())) return {};
optimistic_lock::write_guard node_guard{std::move(node_critical_section)};
if (UNODB_DETAIL_UNLIKELY(node_guard.must_restart())) return {};
optimistic_lock::write_guard child_guard{
std::move(*child_critical_section)};
if (UNODB_DETAIL_UNLIKELY(child_guard.must_restart())) return {};
smaller_node->init(db_instance, inode, node_guard, child_i, child_guard);
*node_in_parent = detail::olc_node_ptr{smaller_node.release(),
INode::smaller_derived_type::type};
UNODB_DETAIL_ASSERT_INACTIVE(node_guard);
UNODB_DETAIL_ASSERT_INACTIVE(child_guard);
*child_in_parent = nullptr;
}
db_instance.template account_shrinking_inode<INode::type>();
return true;
}
} // namespace unodb::detail
namespace unodb {
UNODB_DETAIL_DISABLE_MSVC_WARNING(4189)
template <class INode>
constexpr void olc_db::decrement_inode_count() noexcept {
static_assert(olc_inode_defs::is_inode<INode>());
const auto old_inode_count UNODB_DETAIL_USED_IN_DEBUG =
node_counts[as_i<INode::type>].fetch_sub(1, std::memory_order_relaxed);
UNODB_DETAIL_ASSERT(old_inode_count > 0);
decrease_memory_use(sizeof(INode));
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
template <node_type NodeType>
constexpr void olc_db::account_growing_inode() noexcept {
static_assert(NodeType != node_type::LEAF);
// NOLINTNEXTLINE(google-readability-casting)
growing_inode_counts[internal_as_i<NodeType>].fetch_add(
1, std::memory_order_relaxed);
}
template <node_type NodeType>
constexpr void olc_db::account_shrinking_inode() noexcept {
static_assert(NodeType != node_type::LEAF);
shrinking_inode_counts[internal_as_i<NodeType>].fetch_add(
1, std::memory_order_relaxed);
}
olc_db::~olc_db() noexcept {
UNODB_DETAIL_ASSERT(
qsbr_state::single_thread_mode(qsbr::instance().get_state()));
delete_root_subtree();
}
olc_db::get_result olc_db::get(key search_key) const noexcept {
try_get_result_type result;
const detail::art_key bin_comparable_key{search_key};
while (true) {
result = try_get(bin_comparable_key);
if (result) break;
// TODO(laurynas): upgrade to write locks to prevent starving after a
// certain number of failures?
}
return *result;
}
olc_db::try_get_result_type olc_db::try_get(detail::art_key k) const noexcept {
auto parent_critical_section = root_pointer_lock.try_read_lock();
if (UNODB_DETAIL_UNLIKELY(parent_critical_section.must_restart())) {
// LCOV_EXCL_START
spin_wait_loop_body();
return {};
// LCOV_EXCL_STOP
}
auto node{root.load()};
if (UNODB_DETAIL_UNLIKELY(node == nullptr)) {
if (UNODB_DETAIL_UNLIKELY(!parent_critical_section.try_read_unlock())) {
// LCOV_EXCL_START
spin_wait_loop_body();
return {};
// LCOV_EXCL_STOP
}
return std::make_optional<get_result>(std::nullopt);
}
auto remaining_key{k};
if (UNODB_DETAIL_UNLIKELY(!parent_critical_section.check())) {
// LCOV_EXCL_START
spin_wait_loop_body();
return {};
// LCOV_EXCL_STOP
}
while (true) {
auto node_critical_section = node_ptr_lock(node).try_read_lock();
if (UNODB_DETAIL_UNLIKELY(node_critical_section.must_restart())) return {};
if (UNODB_DETAIL_UNLIKELY(!parent_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
const auto node_type = node.type();
if (node_type == node_type::LEAF) {
const auto *const leaf{node.ptr<::leaf *>()};
if (leaf->matches(k)) {
const auto val_view{leaf->get_value_view()};
if (UNODB_DETAIL_UNLIKELY(!node_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
return qsbr_ptr_span<const std::byte>{val_view};
}
if (UNODB_DETAIL_UNLIKELY(!node_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
return std::make_optional<get_result>(std::nullopt);
}
auto *const inode{node.ptr<olc_inode *>()};
const auto &key_prefix{inode->get_key_prefix()};
const auto key_prefix_length{key_prefix.length()};
const auto shared_key_prefix_length{
key_prefix.get_shared_length(remaining_key)};
if (shared_key_prefix_length < key_prefix_length) {
if (UNODB_DETAIL_UNLIKELY(!node_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
return std::make_optional<get_result>(std::nullopt);
}
UNODB_DETAIL_ASSERT(shared_key_prefix_length == key_prefix_length);
remaining_key.shift_right(key_prefix_length);
const auto *const child_in_parent{
inode->find_child(node_type, remaining_key[0]).second};
if (child_in_parent == nullptr) {
if (UNODB_DETAIL_UNLIKELY(!node_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
return std::make_optional<get_result>(std::nullopt);
}
const auto child = child_in_parent->load();
parent_critical_section = std::move(node_critical_section);
node = child;
remaining_key.shift_right(1);
if (UNODB_DETAIL_UNLIKELY(!parent_critical_section.check())) return {};
}
}
bool olc_db::insert(key insert_key, value_view v) {
const auto bin_comparable_key = detail::art_key{insert_key};
try_update_result_type result;
olc_db_leaf_unique_ptr cached_leaf{
nullptr,
detail::basic_db_leaf_deleter<detail::olc_node_header, olc_db>{*this}};
while (true) {
result = try_insert(bin_comparable_key, v, cached_leaf);
if (result) break;
}
return *result;
}
olc_db::try_update_result_type olc_db::try_insert(
detail::art_key k, value_view v, olc_db_leaf_unique_ptr &cached_leaf) {
auto parent_critical_section = root_pointer_lock.try_read_lock();
if (UNODB_DETAIL_UNLIKELY(parent_critical_section.must_restart())) {
// LCOV_EXCL_START
spin_wait_loop_body();
return {};
// LCOV_EXCL_STOP
}
auto node{root.load()};
if (UNODB_DETAIL_UNLIKELY(node == nullptr)) {
create_leaf_if_needed(cached_leaf, k, v, *this);
const optimistic_lock::write_guard write_unlock_on_exit{
std::move(parent_critical_section)};
if (UNODB_DETAIL_UNLIKELY(write_unlock_on_exit.must_restart())) {
// Do not call spin_wait_loop_body here - creating the leaf took some time
return {}; // LCOV_EXCL_LINE
}
root = detail::olc_node_ptr{cached_leaf.release(), node_type::LEAF};
return true;
}
auto *node_in_parent{&root};
detail::tree_depth depth{};
auto remaining_key{k};
if (UNODB_DETAIL_UNLIKELY(!parent_critical_section.check())) {
// LCOV_EXCL_START
spin_wait_loop_body();
return {};
// LCOV_EXCL_STOP
}
while (true) {
auto node_critical_section = node_ptr_lock(node).try_read_lock();
if (UNODB_DETAIL_UNLIKELY(node_critical_section.must_restart())) return {};
const auto node_type = node.type();
if (node_type == node_type::LEAF) {
auto *const leaf{node.ptr<::leaf *>()};
const auto existing_key{leaf->get_key()};
if (UNODB_DETAIL_UNLIKELY(k == existing_key)) {
if (UNODB_DETAIL_UNLIKELY(!parent_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
if (UNODB_DETAIL_UNLIKELY(!node_critical_section.try_read_unlock()))
return {}; // LCOV_EXCL_LINE
if (UNODB_DETAIL_UNLIKELY(cached_leaf != nullptr)) {
cached_leaf.reset(); // LCOV_EXCL_LINE
}
return false;
}
create_leaf_if_needed(cached_leaf, k, v, *this);
auto new_node{
olc_inode_4::create(*this, existing_key, remaining_key, depth)};
{
const optimistic_lock::write_guard parent_guard{
std::move(parent_critical_section)};