forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring_position.hh
516 lines (428 loc) · 18.6 KB
/
ring_position.hh
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
/*
* Modified by ScyllaDB
* Copyright (C) 2023-present ScyllaDB
*/
/*
* SPDX-License-Identifier: (AGPL-3.0-or-later and Apache-2.0)
*/
#pragma once
#include "keys.hh"
#include "dht/token.hh"
#include "dht/decorated_key.hh"
namespace dht {
//
// Represents position in the ring of partitions, where partitions are ordered
// according to decorated_key ordering (first by token, then by key value).
// Intended to be used for defining partition ranges.
//
// The 'key' part is optional. When it's absent, this object represents a position
// which is either before or after all keys sharing given token. That's determined
// by relation_to_keys().
//
// For example for the following data:
//
// tokens: | t1 | t2 |
// +----+----+----+
// keys: | k1 | k2 | k3 |
//
// The ordering is:
//
// ring_position(t1, token_bound::start) < ring_position(k1)
// ring_position(k1) < ring_position(k2)
// ring_position(k1) == decorated_key(k1)
// ring_position(k2) == decorated_key(k2)
// ring_position(k2) < ring_position(t1, token_bound::end)
// ring_position(k2) < ring_position(k3)
// ring_position(t1, token_bound::end) < ring_position(t2, token_bound::start)
//
// Maps to org.apache.cassandra.db.RowPosition and its derivatives in Origin.
//
class ring_position {
public:
enum class token_bound : int8_t { start = -1, end = 1 };
private:
friend class ring_position_comparator;
friend class ring_position_ext;
dht::token _token;
token_bound _token_bound{}; // valid when !_key
std::optional<partition_key> _key;
public:
static ring_position min() noexcept {
return { minimum_token(), token_bound::start };
}
static ring_position max() noexcept {
return { maximum_token(), token_bound::end };
}
bool is_min() const noexcept {
return _token.is_minimum();
}
bool is_max() const noexcept {
return _token.is_maximum();
}
static ring_position starting_at(dht::token token) {
return { std::move(token), token_bound::start };
}
static ring_position ending_at(dht::token token) {
return { std::move(token), token_bound::end };
}
ring_position(dht::token token, token_bound bound)
: _token(std::move(token))
, _token_bound(bound)
{ }
ring_position(dht::token token, partition_key key)
: _token(std::move(token))
, _key(std::make_optional(std::move(key)))
{ }
ring_position(dht::token token, token_bound bound, std::optional<partition_key> key)
: _token(std::move(token))
, _token_bound(bound)
, _key(std::move(key))
{ }
ring_position(const dht::decorated_key& dk)
: _token(dk._token)
, _key(std::make_optional(dk._key))
{ }
ring_position(dht::decorated_key&& dk)
: _token(std::move(dk._token))
, _key(std::make_optional(std::move(dk._key)))
{ }
const dht::token& token() const noexcept {
return _token;
}
// Valid when !has_key()
token_bound bound() const {
return _token_bound;
}
// Returns -1 if smaller than keys with the same token, +1 if greater.
int relation_to_keys() const {
return _key ? 0 : static_cast<int>(_token_bound);
}
const std::optional<partition_key>& key() const {
return _key;
}
bool has_key() const {
return bool(_key);
}
// Call only when has_key()
dht::decorated_key as_decorated_key() const {
return { _token, *_key };
}
bool equal(const schema&, const ring_position&) const;
// Trichotomic comparator defining a total ordering on ring_position objects
std::strong_ordering tri_compare(const schema&, const ring_position&) const;
// "less" comparator corresponding to tri_compare()
bool less_compare(const schema&, const ring_position&) const;
};
// Non-owning version of ring_position and ring_position_ext.
//
// Unlike ring_position, it can express positions which are right after and right before the keys.
// ring_position still can not because it is sent between nodes and such a position
// would not be (yet) properly interpreted by old nodes. That's why any ring_position
// can be converted to ring_position_view, but not the other way.
//
// It is possible to express a partition_range using a pair of two ring_position_views v1 and v2,
// where v1 = ring_position_view::for_range_start(r) and v2 = ring_position_view::for_range_end(r).
// Such range includes all keys k such that v1 <= k < v2, with order defined by ring_position_comparator.
//
class ring_position_view {
friend std::strong_ordering ring_position_tri_compare(const schema& s, ring_position_view lh, ring_position_view rh);
friend class ring_position_comparator;
friend class ring_position_comparator_for_sstables;
friend class ring_position_ext;
// Order is lexicographical on (_token, _key) tuples, where _key part may be missing, and
// _weight affecting order between tuples if one is a prefix of the other (including being equal).
// A positive weight puts the position after all strictly prefixed by it, while a non-positive
// weight puts it before them. If tuples are equal, the order is further determined by _weight.
//
// For example {_token=t1, _key=nullptr, _weight=1} is ordered after {_token=t1, _key=k1, _weight=0},
// but {_token=t1, _key=nullptr, _weight=-1} is ordered before it.
//
const dht::token* _token; // always not nullptr
const partition_key* _key; // Can be nullptr
int8_t _weight;
private:
ring_position_view() noexcept : _token(nullptr), _key(nullptr), _weight(0) { }
explicit operator bool() const noexcept { return bool(_token); }
public:
using token_bound = ring_position::token_bound;
struct after_key_tag {};
using after_key = bool_class<after_key_tag>;
static ring_position_view min() noexcept {
static auto min_token = minimum_token();
return { min_token, nullptr, -1 };
}
static ring_position_view max() noexcept {
static auto max_token = maximum_token();
return { max_token, nullptr, 1 };
}
bool is_min() const noexcept {
return _token->is_minimum();
}
bool is_max() const noexcept {
return _token->is_maximum();
}
static ring_position_view for_range_start(const partition_range& r) {
return r.start() ? ring_position_view(r.start()->value(), after_key(!r.start()->is_inclusive())) : min();
}
static ring_position_view for_range_end(const partition_range& r) {
return r.end() ? ring_position_view(r.end()->value(), after_key(r.end()->is_inclusive())) : max();
}
static ring_position_view for_after_key(const dht::decorated_key& dk) {
return ring_position_view(dk, after_key::yes);
}
static ring_position_view for_after_key(dht::ring_position_view view) {
return ring_position_view(after_key_tag(), view);
}
static ring_position_view starting_at(const dht::token& t) {
return ring_position_view(t, token_bound::start);
}
static ring_position_view ending_at(const dht::token& t) {
return ring_position_view(t, token_bound::end);
}
ring_position_view(const dht::ring_position& pos, after_key after = after_key::no)
: _token(&pos.token())
, _key(pos.has_key() ? &*pos.key() : nullptr)
, _weight(pos.has_key() ? bool(after) : pos.relation_to_keys())
{ }
ring_position_view(const ring_position_view& pos) = default;
ring_position_view& operator=(const ring_position_view& other) = default;
ring_position_view(after_key_tag, const ring_position_view& v)
: _token(v._token)
, _key(v._key)
, _weight(v._key ? 1 : v._weight)
{ }
ring_position_view(const dht::decorated_key& key, after_key after_key = after_key::no)
: _token(&key.token())
, _key(&key.key())
, _weight(bool(after_key))
{ }
ring_position_view(const dht::token& token, const partition_key* key, int8_t weight)
: _token(&token)
, _key(key)
, _weight(weight)
{ }
explicit ring_position_view(const dht::token& token, token_bound bound = token_bound::start)
: _token(&token)
, _key(nullptr)
, _weight(static_cast<std::underlying_type_t<token_bound>>(bound))
{ }
const dht::token& token() const noexcept { return *_token; }
const partition_key* key() const { return _key; }
// Only when key() == nullptr
token_bound get_token_bound() const { return token_bound(_weight); }
// Only when key() != nullptr
after_key is_after_key() const { return after_key(_weight == 1); }
friend fmt::formatter<ring_position_view>;
friend class optimized_optional<ring_position_view>;
};
using ring_position_view_opt = optimized_optional<ring_position_view>;
//
// Represents position in the ring of partitions, where partitions are ordered
// according to decorated_key ordering (first by token, then by key value).
// Intended to be used for defining partition ranges.
//
// Unlike ring_position, it can express positions which are right after and right before the keys.
// ring_position still can not because it is sent between nodes and such a position
// would not be (yet) properly interpreted by old nodes. That's why any ring_position
// can be converted to ring_position_ext, but not the other way.
//
// It is possible to express a partition_range using a pair of two ring_position_exts v1 and v2,
// where v1 = ring_position_ext::for_range_start(r) and v2 = ring_position_ext::for_range_end(r).
// Such range includes all keys k such that v1 <= k < v2, with order defined by ring_position_comparator.
//
class ring_position_ext {
// Order is lexicographical on (_token, _key) tuples, where _key part may be missing, and
// _weight affecting order between tuples if one is a prefix of the other (including being equal).
// A positive weight puts the position after all strictly prefixed by it, while a non-positive
// weight puts it before them. If tuples are equal, the order is further determined by _weight.
//
// For example {_token=t1, _key=nullptr, _weight=1} is ordered after {_token=t1, _key=k1, _weight=0},
// but {_token=t1, _key=nullptr, _weight=-1} is ordered before it.
//
dht::token _token;
std::optional<partition_key> _key;
int8_t _weight;
public:
using token_bound = ring_position::token_bound;
struct after_key_tag {};
using after_key = bool_class<after_key_tag>;
static ring_position_ext min() noexcept {
return { minimum_token(), std::nullopt, -1 };
}
static ring_position_ext max() noexcept {
return { maximum_token(), std::nullopt, 1 };
}
bool is_min() const noexcept {
return _token.is_minimum();
}
bool is_max() const noexcept {
return _token.is_maximum();
}
static ring_position_ext for_range_start(const partition_range& r) {
return r.start() ? ring_position_ext(r.start()->value(), after_key(!r.start()->is_inclusive())) : min();
}
static ring_position_ext for_range_end(const partition_range& r) {
return r.end() ? ring_position_ext(r.end()->value(), after_key(r.end()->is_inclusive())) : max();
}
static ring_position_ext for_after_key(const dht::decorated_key& dk) {
return ring_position_ext(dk, after_key::yes);
}
static ring_position_ext for_after_key(dht::ring_position_ext view) {
return ring_position_ext(after_key_tag(), view);
}
static ring_position_ext starting_at(const dht::token& t) {
return ring_position_ext(t, token_bound::start);
}
static ring_position_ext ending_at(const dht::token& t) {
return ring_position_ext(t, token_bound::end);
}
ring_position_ext(const dht::ring_position& pos, after_key after = after_key::no)
: _token(pos.token())
, _key(pos.key())
, _weight(pos.has_key() ? bool(after) : pos.relation_to_keys())
{ }
ring_position_ext(const ring_position_ext& pos) = default;
ring_position_ext& operator=(const ring_position_ext& other) = default;
ring_position_ext(ring_position_view v)
: _token(*v._token)
, _key(v._key ? std::make_optional(*v._key) : std::nullopt)
, _weight(v._weight)
{ }
ring_position_ext(after_key_tag, const ring_position_ext& v)
: _token(v._token)
, _key(v._key)
, _weight(v._key ? 1 : v._weight)
{ }
ring_position_ext(const dht::decorated_key& key, after_key after_key = after_key::no)
: _token(key.token())
, _key(key.key())
, _weight(bool(after_key))
{ }
ring_position_ext(dht::token token, std::optional<partition_key> key, int8_t weight) noexcept
: _token(std::move(token))
, _key(std::move(key))
, _weight(weight)
{ }
ring_position_ext(ring_position&& pos) noexcept
: _token(std::move(pos._token))
, _key(std::move(pos._key))
, _weight(pos.relation_to_keys())
{ }
explicit ring_position_ext(const dht::token& token, token_bound bound = token_bound::start)
: _token(token)
, _key(std::nullopt)
, _weight(static_cast<std::underlying_type_t<token_bound>>(bound))
{ }
const dht::token& token() const noexcept { return _token; }
const std::optional<partition_key>& key() const { return _key; }
int8_t weight() const { return _weight; }
// Only when key() == std::nullopt
token_bound get_token_bound() const { return token_bound(_weight); }
// Only when key() != std::nullopt
after_key is_after_key() const { return after_key(_weight == 1); }
operator ring_position_view() const { return { _token, _key ? &*_key : nullptr, _weight }; }
};
std::strong_ordering ring_position_tri_compare(const schema& s, ring_position_view lh, ring_position_view rh);
template <typename T>
requires std::is_convertible<T, ring_position_view>::value
ring_position_view ring_position_view_to_compare(const T& val) {
return val;
}
// Trichotomic comparator for ring order
struct ring_position_comparator {
const schema& s;
ring_position_comparator(const schema& s_) : s(s_) {}
std::strong_ordering operator()(ring_position_view lh, ring_position_view rh) const {
return ring_position_tri_compare(s, lh, rh);
}
template <typename T>
std::strong_ordering operator()(const T& lh, ring_position_view rh) const {
return ring_position_tri_compare(s, ring_position_view_to_compare(lh), rh);
}
template <typename T>
std::strong_ordering operator()(ring_position_view lh, const T& rh) const {
return ring_position_tri_compare(s, lh, ring_position_view_to_compare(rh));
}
template <typename T1, typename T2>
std::strong_ordering operator()(const T1& lh, const T2& rh) const {
return ring_position_tri_compare(s, ring_position_view_to_compare(lh), ring_position_view_to_compare(rh));
}
};
struct ring_position_comparator_for_sstables {
const schema& s;
ring_position_comparator_for_sstables(const schema& s_) : s(s_) {}
std::strong_ordering operator()(ring_position_view, sstables::decorated_key_view) const;
std::strong_ordering operator()(sstables::decorated_key_view, ring_position_view) const;
};
// "less" comparator giving the same order as ring_position_comparator
struct ring_position_less_comparator {
ring_position_comparator tri;
ring_position_less_comparator(const schema& s) : tri(s) {}
template<typename T, typename U>
bool operator()(const T& lh, const U& rh) const {
return tri(lh, rh) < 0;
}
};
// Wraps ring_position or ring_position_view so either is compatible with old-style C++: default
// constructor, stateless comparators, yada yada.
// The motivations for supporting both types are to make containers self-sufficient by not relying
// on callers to keep ring position alive, allow lookup on containers that don't support different
// key types, and also avoiding unnecessary copies.
class compatible_ring_position_or_view {
schema_ptr _schema;
lw_shared_ptr<dht::ring_position> _rp;
dht::ring_position_view_opt _rpv; // optional only for default ctor, nothing more
public:
compatible_ring_position_or_view() = default;
explicit compatible_ring_position_or_view(schema_ptr s, dht::ring_position rp)
: _schema(std::move(s)), _rp(make_lw_shared<dht::ring_position>(std::move(rp))), _rpv(dht::ring_position_view(*_rp)) {
}
explicit compatible_ring_position_or_view(const schema& s, dht::ring_position_view rpv)
: _schema(s.shared_from_this()), _rpv(rpv) {
}
const dht::ring_position_view& position() const {
return *_rpv;
}
std::strong_ordering operator<=>(const compatible_ring_position_or_view& other) const {
return dht::ring_position_tri_compare(*_schema, position(), other.position());
}
bool operator==(const compatible_ring_position_or_view& other) const {
return *this <=> other == 0;
}
};
class partition_ranges_view {
const dht::partition_range* _data = nullptr;
size_t _size = 0;
public:
partition_ranges_view() = default;
partition_ranges_view(const dht::partition_range& range) : _data(&range), _size(1) {}
partition_ranges_view(const dht::partition_range_vector& ranges) : _data(ranges.data()), _size(ranges.size()) {}
bool empty() const { return _size == 0; }
size_t size() const { return _size; }
const dht::partition_range& front() const { return *_data; }
const dht::partition_range& back() const { return *(_data + _size - 1); }
const dht::partition_range* begin() const { return _data; }
const dht::partition_range* end() const { return _data + _size; }
};
} // namespace dht
template<>
struct fmt::formatter<dht::ring_position_view> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
auto format(const dht::ring_position_view&, fmt::format_context& ctx) const -> decltype(ctx.out());
};
template<>
struct fmt::formatter<dht::ring_position_ext> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
auto format(const dht::ring_position_ext& pos, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "{}", (dht::ring_position_view)pos);
}
};
template<>
struct fmt::formatter<dht::ring_position> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
auto format(const dht::ring_position& pos, fmt::format_context& ctx) const -> decltype(ctx.out());
};
template <> struct fmt::formatter<dht::partition_ranges_view> : fmt::formatter<string_view> {
auto format(const dht::partition_ranges_view&, fmt::format_context& ctx) const
-> decltype(ctx.out());
};