forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition_range_compat.hh
159 lines (141 loc) · 5 KB
/
partition_range_compat.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
/*
* Copyright 2016-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <vector>
#include "interval.hh"
#include "dht/ring_position.hh"
namespace compat {
using wrapping_partition_range = wrapping_interval<dht::ring_position>;
// unwraps a vector of wrapping ranges into a vector of nonwrapping ranges
// if the vector happens to be sorted by the left bound, it remains sorted
template <typename T, typename Comparator>
std::vector<interval<T>>
unwrap(std::vector<wrapping_interval<T>>&& v, Comparator&& cmp) {
std::vector<interval<T>> ret;
ret.reserve(v.size() + 1);
for (auto&& wr : v) {
if (wr.is_wrap_around(cmp)) {
auto&& p = std::move(wr).unwrap();
ret.insert(ret.begin(), interval<T>(std::move(p.first)));
ret.emplace_back(std::move(p.second));
} else {
ret.emplace_back(std::move(wr));
}
}
return ret;
}
// unwraps a vector of wrapping ranges into a vector of nonwrapping ranges
// if the vector happens to be sorted by the left bound, it remains sorted
template <typename T, typename Comparator>
std::vector<interval<T>>
unwrap(const std::vector<wrapping_interval<T>>& v, Comparator&& cmp) {
std::vector<interval<T>> ret;
ret.reserve(v.size() + 1);
for (auto&& wr : v) {
if (wr.is_wrap_around(cmp)) {
auto&& p = wr.unwrap();
ret.insert(ret.begin(), interval<T>(p.first));
ret.emplace_back(p.second);
} else {
ret.emplace_back(wr);
}
}
return ret;
}
template <typename T>
std::vector<wrapping_interval<T>>
wrap(const std::vector<interval<T>>& v) {
// re-wrap (-inf,x) ... (y, +inf) into (y, x):
if (v.size() >= 2 && !v.front().start() && !v.back().end()) {
auto ret = std::vector<wrapping_interval<T>>();
ret.reserve(v.size() - 1);
std::copy(v.begin() + 1, v.end() - 1, std::back_inserter(ret));
ret.emplace_back(v.back().start(), v.front().end());
return ret;
}
return boost::copy_range<std::vector<wrapping_interval<T>>>(v);
}
template <typename T>
std::vector<wrapping_interval<T>>
wrap(std::vector<interval<T>>&& v) {
// re-wrap (-inf,x) ... (y, +inf) into (y, x):
if (v.size() >= 2 && !v.front().start() && !v.back().end()) {
auto ret = std::vector<wrapping_interval<T>>();
ret.reserve(v.size() - 1);
std::move(v.begin() + 1, v.end() - 1, std::back_inserter(ret));
ret.emplace_back(std::move(v.back()).start(), std::move(v.front()).end());
return ret;
}
// want boost::adaptor::moved ...
return boost::copy_range<std::vector<wrapping_interval<T>>>(v);
}
inline
dht::token_range_vector
unwrap(const std::vector<wrapping_interval<dht::token>>& v) {
return unwrap(v, dht::token_comparator());
}
inline
dht::token_range_vector
unwrap(std::vector<wrapping_interval<dht::token>>&& v) {
return unwrap(std::move(v), dht::token_comparator());
}
class one_or_two_partition_ranges : public std::pair<dht::partition_range, std::optional<dht::partition_range>> {
using pair = std::pair<dht::partition_range, std::optional<dht::partition_range>>;
public:
explicit one_or_two_partition_ranges(dht::partition_range&& f)
: pair(std::move(f), std::nullopt) {
}
explicit one_or_two_partition_ranges(dht::partition_range&& f, dht::partition_range&& s)
: pair(std::move(f), std::move(s)) {
}
operator dht::partition_range_vector() const & {
auto ret = dht::partition_range_vector();
// not reserving, since ret.size() is likely to be 1
ret.push_back(first);
if (second) {
ret.push_back(*second);
}
return ret;
}
operator dht::partition_range_vector() && {
auto ret = dht::partition_range_vector();
// not reserving, since ret.size() is likely to be 1
ret.push_back(std::move(first));
if (second) {
ret.push_back(std::move(*second));
}
return ret;
}
};
inline
one_or_two_partition_ranges
unwrap(wrapping_partition_range pr, const schema& s) {
if (pr.is_wrap_around(dht::ring_position_comparator(s))) {
auto unw = std::move(pr).unwrap();
// Preserve ring order
return one_or_two_partition_ranges(
dht::partition_range(std::move(unw.second)),
dht::partition_range(std::move(unw.first)));
} else {
return one_or_two_partition_ranges(dht::partition_range(std::move(pr)));
}
}
// Unwraps `range` and calls `func` with its components, with an unwrapped
// range type, as a parameter (once or twice)
template <typename T, typename Comparator, typename Func>
void
unwrap_into(wrapping_interval<T>&& range, const Comparator& cmp, Func&& func) {
if (range.is_wrap_around(cmp)) {
auto&& unw = range.unwrap();
// Preserve ring order
func(interval<T>(std::move(unw.second)));
func(interval<T>(std::move(unw.first)));
} else {
func(interval<T>(std::move(range)));
}
}
}