-
Notifications
You must be signed in to change notification settings - Fork 3
/
olc_art.hpp
257 lines (188 loc) · 8.04 KB
/
olc_art.hpp
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
// Copyright 2019-2024 Laurynas Biveinis
#ifndef UNODB_DETAIL_OLC_ART_HPP
#define UNODB_DETAIL_OLC_ART_HPP
#include "global.hpp"
// IWYU pragma: no_include <__fwd/ostream.h>
#include <array>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <iosfwd> // IWYU pragma: keep
#include <optional>
#include <tuple>
#include "art_common.hpp"
#include "art_internal.hpp"
#include "assert.hpp"
#include "node_type.hpp"
#include "optimistic_lock.hpp"
#include "portability_arch.hpp"
#include "qsbr_ptr.hpp"
namespace unodb {
class olc_db;
namespace detail {
template <class, template <class> class, class, class, template <class> class,
template <class, class> class>
struct basic_art_policy; // IWYU pragma: keep
struct olc_node_header;
using olc_node_ptr = basic_node_ptr<olc_node_header>;
template <class>
class db_inode_qsbr_deleter; // IWYU pragma: keep
template <class, class>
class db_leaf_qsbr_deleter; // IWYU pragma: keep
template <class Header, class Db>
[[nodiscard]] auto make_db_leaf_ptr(art_key, value_view, Db &);
struct olc_impl_helpers;
template <class AtomicArray>
using non_atomic_array =
std::array<typename AtomicArray::value_type::value_type,
std::tuple_size<AtomicArray>::value>;
template <class T>
inline non_atomic_array<T> copy_atomic_to_nonatomic(T &atomic_array) noexcept {
non_atomic_array<T> result;
for (typename decltype(result)::size_type i = 0; i < result.size(); ++i) {
result[i] = atomic_array[i].load(std::memory_order_relaxed);
}
return result;
}
using olc_leaf_unique_ptr =
detail::basic_db_leaf_unique_ptr<detail::olc_node_header, olc_db>;
} // namespace detail
using qsbr_value_view = qsbr_ptr_span<const std::byte>;
// A concurrent Adaptive Radix Tree that is synchronized using optimistic lock
// coupling. At any time, at most two directly-related tree nodes can be
// write-locked by the insert algorithm and three by the delete algorithm. The
// lock used is optimistic lock (see optimistic_lock.hpp), where only writers
// lock and readers access nodes optimistically with node version checks. For
// deleted node reclamation, Quiescent State-Based Reclamation is used.
class olc_db final {
public:
using get_result = std::optional<qsbr_value_view>;
// Creation and destruction
olc_db() noexcept = default;
~olc_db() noexcept;
// Querying
[[nodiscard]] get_result get(key search_key) const noexcept;
[[nodiscard]] auto empty() const noexcept { return root == nullptr; }
// Modifying
// Cannot be called during stack unwinding with std::uncaught_exceptions() > 0
[[nodiscard]] bool insert(key insert_key, value_view v);
[[nodiscard]] bool remove(key remove_key);
// Only legal in single-threaded context, as destructor
void clear() noexcept;
// Stats
// Return current memory use by tree nodes in bytes
[[nodiscard]] auto get_current_memory_use() const noexcept {
return current_memory_use.load(std::memory_order_relaxed);
}
template <node_type NodeType>
[[nodiscard]] auto get_node_count() const noexcept {
return node_counts[as_i<NodeType>].load(std::memory_order_relaxed);
}
[[nodiscard]] auto get_node_counts() const noexcept {
return detail::copy_atomic_to_nonatomic(node_counts);
}
template <node_type NodeType>
[[nodiscard]] auto get_growing_inode_count() const noexcept {
return growing_inode_counts[internal_as_i<NodeType>].load(
std::memory_order_relaxed);
}
[[nodiscard]] auto get_growing_inode_counts() const noexcept {
return detail::copy_atomic_to_nonatomic(growing_inode_counts);
}
template <node_type NodeType>
[[nodiscard]] auto get_shrinking_inode_count() const noexcept {
return shrinking_inode_counts[internal_as_i<NodeType>].load(
std::memory_order_relaxed);
}
[[nodiscard]] auto get_shrinking_inode_counts() const noexcept {
return detail::copy_atomic_to_nonatomic(shrinking_inode_counts);
}
[[nodiscard]] auto get_key_prefix_splits() const noexcept {
return key_prefix_splits.load(std::memory_order_relaxed);
}
// Public utils
[[nodiscard]] static constexpr auto key_found(
const get_result &result) noexcept {
return static_cast<bool>(result);
}
// Debugging
[[gnu::cold]] UNODB_DETAIL_NOINLINE void dump(std::ostream &os) const;
olc_db(const olc_db &) noexcept = delete;
olc_db(olc_db &&) noexcept = delete;
olc_db &operator=(const olc_db &) noexcept = delete;
olc_db &operator=(olc_db &&) noexcept = delete;
private:
// If get_result is not present, the search was interrupted. Yes, this
// resolves to std::optional<std::optional<value_view>>, but IMHO both
// levels of std::optional are clear here
using try_get_result_type = std::optional<get_result>;
using try_update_result_type = std::optional<bool>;
[[nodiscard]] try_get_result_type try_get(detail::art_key k) const noexcept;
[[nodiscard]] try_update_result_type try_insert(
detail::art_key k, value_view v,
detail::olc_leaf_unique_ptr &cached_leaf);
[[nodiscard]] try_update_result_type try_remove(detail::art_key k);
void delete_root_subtree() noexcept;
void increase_memory_use(std::size_t delta) noexcept;
void decrease_memory_use(std::size_t delta) noexcept;
void increment_leaf_count(std::size_t leaf_size) noexcept {
increase_memory_use(leaf_size);
node_counts[as_i<node_type::LEAF>].fetch_add(1, std::memory_order_relaxed);
}
UNODB_DETAIL_DISABLE_MSVC_WARNING(4189)
void decrement_leaf_count(std::size_t leaf_size) noexcept {
decrease_memory_use(leaf_size);
const auto old_leaf_count UNODB_DETAIL_USED_IN_DEBUG =
node_counts[as_i<node_type::LEAF>].fetch_sub(1,
std::memory_order_relaxed);
UNODB_DETAIL_ASSERT(old_leaf_count > 0);
}
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
template <class INode>
constexpr void increment_inode_count() noexcept;
template <class INode>
constexpr void decrement_inode_count() noexcept;
template <node_type NodeType>
constexpr void account_growing_inode() noexcept;
template <node_type NodeType>
constexpr void account_shrinking_inode() noexcept;
alignas(
detail::hardware_destructive_interference_size) mutable optimistic_lock
root_pointer_lock;
in_critical_section<detail::olc_node_ptr> root{detail::olc_node_ptr{nullptr}};
static_assert(sizeof(root_pointer_lock) + sizeof(root) <=
detail::hardware_constructive_interference_size);
// Current logically allocated memory that is not scheduled to be reclaimed.
// The total memory currently allocated is this plus the QSBR deallocation
// backlog (qsbr::previous_interval_total_dealloc_size +
// qsbr::current_interval_total_dealloc_size).
alignas(detail::hardware_destructive_interference_size)
std::atomic<std::size_t> current_memory_use{0};
alignas(detail::hardware_destructive_interference_size)
std::atomic<std::uint64_t> key_prefix_splits{0};
template <class T>
using atomic_array = std::array<std::atomic<typename T::value_type>,
std::tuple_size<T>::value>;
alignas(detail::hardware_destructive_interference_size)
atomic_array<node_type_counter_array> node_counts{};
alignas(detail::hardware_destructive_interference_size)
atomic_array<inode_type_counter_array> growing_inode_counts{};
alignas(detail::hardware_destructive_interference_size)
atomic_array<inode_type_counter_array> shrinking_inode_counts{};
friend auto detail::make_db_leaf_ptr<detail::olc_node_header, olc_db>(
detail::art_key, value_view, olc_db &);
template <class, class>
friend class detail::basic_db_leaf_deleter;
template <class, class>
friend class detail::db_leaf_qsbr_deleter;
template <class>
friend class detail::db_inode_qsbr_deleter;
template <class, template <class> class, class, class, template <class> class,
template <class, class> class>
friend struct detail::basic_art_policy;
template <class, class>
friend class detail::basic_db_inode_deleter;
friend struct detail::olc_impl_helpers;
};
} // namespace unodb
#endif // UNODB_DETAIL_OLC_ART_HPP