-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathmutation_fragment.cc
311 lines (269 loc) · 9.91 KB
/
mutation_fragment.cc
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
/*
* Copyright (C) 2016 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stack>
#include <boost/range/algorithm/heap_algorithm.hpp>
#include <seastar/util/defer.hh>
#include "mutation.hh"
#include "mutation_fragment.hh"
std::ostream&
operator<<(std::ostream& os, const clustering_row& row) {
return os << "{clustering_row: ck " << row._ck << " t " << row._t << " row_marker " << row._marker << " cells " << row._cells << "}";
}
std::ostream&
operator<<(std::ostream& os, const static_row& row) {
return os << "{static_row: "<< row._cells << "}";
}
std::ostream&
operator<<(std::ostream& os, const partition_start& ph) {
return os << "{partition_start: pk "<< ph._key << " partition_tombstone " << ph._partition_tombstone << "}";
}
std::ostream&
operator<<(std::ostream& os, const partition_end& eop) {
return os << "{partition_end}";
}
std::ostream& operator<<(std::ostream& out, partition_region r) {
switch (r) {
case partition_region::partition_start: out << "partition_start"; break;
case partition_region::static_row: out << "static_row"; break;
case partition_region::clustered: out << "clustered"; break;
case partition_region::partition_end: out << "partition_end"; break;
}
return out;
}
std::ostream& operator<<(std::ostream& out, position_in_partition_view pos) {
out << "{position: " << pos._type << ",";
if (pos._ck) {
out << *pos._ck;
} else {
out << "null";
}
return out << "," << pos._bound_weight << "}";
}
std::ostream& operator<<(std::ostream& out, const position_in_partition& pos) {
return out << static_cast<position_in_partition_view>(pos);
}
std::ostream& operator<<(std::ostream& out, const position_range& range) {
return out << "{" << range.start() << ", " << range.end() << "}";
}
mutation_fragment::mutation_fragment(static_row&& r)
: _kind(kind::static_row), _data(std::make_unique<data>())
{
new (&_data->_static_row) static_row(std::move(r));
}
mutation_fragment::mutation_fragment(clustering_row&& r)
: _kind(kind::clustering_row), _data(std::make_unique<data>())
{
new (&_data->_clustering_row) clustering_row(std::move(r));
}
mutation_fragment::mutation_fragment(range_tombstone&& r)
: _kind(kind::range_tombstone), _data(std::make_unique<data>())
{
new (&_data->_range_tombstone) range_tombstone(std::move(r));
}
mutation_fragment::mutation_fragment(partition_start&& r)
: _kind(kind::partition_start), _data(std::make_unique<data>())
{
new (&_data->_partition_start) partition_start(std::move(r));
}
mutation_fragment::mutation_fragment(partition_end&& r)
: _kind(kind::partition_end), _data(std::make_unique<data>())
{
new (&_data->_partition_end) partition_end(std::move(r));
}
void mutation_fragment::destroy_data() noexcept
{
switch (_kind) {
case kind::static_row:
_data->_static_row.~static_row();
break;
case kind::clustering_row:
_data->_clustering_row.~clustering_row();
break;
case kind::range_tombstone:
_data->_range_tombstone.~range_tombstone();
break;
case kind::partition_start:
_data->_partition_start.~partition_start();
break;
case kind::partition_end:
_data->_partition_end.~partition_end();
break;
}
}
namespace {
struct get_key_visitor {
const clustering_key_prefix& operator()(const clustering_row& cr) { return cr.key(); }
const clustering_key_prefix& operator()(const range_tombstone& rt) { return rt.start; }
template <typename T>
const clustering_key_prefix& operator()(const T&) { abort(); }
};
}
const clustering_key_prefix& mutation_fragment::key() const
{
assert(has_key());
return visit(get_key_visitor());
}
void mutation_fragment::apply(const schema& s, mutation_fragment&& mf)
{
assert(mergeable_with(mf));
_data->_size_in_bytes = stdx::nullopt;
switch (_kind) {
case mutation_fragment::kind::partition_start:
_data->_partition_start.partition_tombstone().apply(mf._data->_partition_start.partition_tombstone());
mf._data->_partition_start.~partition_start();
break;
case kind::static_row:
_data->_static_row.apply(s, std::move(mf._data->_static_row));
mf._data->_static_row.~static_row();
break;
case kind::clustering_row:
_data->_clustering_row.apply(s, std::move(mf._data->_clustering_row));
mf._data->_clustering_row.~clustering_row();
break;
case mutation_fragment::kind::partition_end:
// Nothing to do for this guy.
mf._data->_partition_end.~partition_end();
break;
default: abort();
}
mf._data.reset();
}
position_in_partition_view mutation_fragment::position() const
{
return visit([] (auto& mf) -> position_in_partition_view { return mf.position(); });
}
position_range mutation_fragment::range() const {
switch (_kind) {
case kind::static_row:
return position_range::for_static_row();
case kind::clustering_row:
return position_range(position_in_partition(position()), position_in_partition::after_key(key()));
case kind::partition_start:
return position_range(position_in_partition(position()), position_in_partition::for_static_row());
case kind::partition_end:
return position_range(position_in_partition(position()), position_in_partition::after_all_clustered_rows());
case kind::range_tombstone:
auto&& rt = as_range_tombstone();
return position_range(position_in_partition(rt.position()), position_in_partition(rt.end_position()));
}
abort();
}
std::ostream& operator<<(std::ostream& os, mutation_fragment::kind k)
{
switch (k) {
case mutation_fragment::kind::static_row: return os << "static row";
case mutation_fragment::kind::clustering_row: return os << "clustering row";
case mutation_fragment::kind::range_tombstone: return os << "range tombstone";
case mutation_fragment::kind::partition_start: return os << "partition start";
case mutation_fragment::kind::partition_end: return os << "partition end";
}
abort();
}
std::ostream& operator<<(std::ostream& os, const mutation_fragment& mf) {
os << "{mutation_fragment: " << mf._kind << " " << mf.position() << " ";
mf.visit([&os] (const auto& what) -> void {
os << what;
});
os << "}";
return os;
}
mutation_fragment_opt range_tombstone_stream::do_get_next()
{
auto& rt = *_list.tombstones().begin();
auto mf = mutation_fragment(std::move(rt));
_list.tombstones().erase(_list.begin());
current_deleter<range_tombstone>()(&rt);
return mf;
}
mutation_fragment_opt range_tombstone_stream::get_next(const rows_entry& re)
{
if (!_list.empty()) {
return !_cmp(re.position(), _list.begin()->position()) ? do_get_next() : mutation_fragment_opt();
}
return { };
}
mutation_fragment_opt range_tombstone_stream::get_next(const mutation_fragment& mf)
{
if (!_list.empty()) {
return !_cmp(mf.position(), _list.begin()->position()) ? do_get_next() : mutation_fragment_opt();
}
return { };
}
mutation_fragment_opt range_tombstone_stream::get_next(position_in_partition_view upper_bound)
{
if (!_list.empty()) {
return _cmp(_list.begin()->position(), upper_bound) ? do_get_next() : mutation_fragment_opt();
}
return { };
}
mutation_fragment_opt range_tombstone_stream::get_next()
{
if (!_list.empty()) {
return do_get_next();
}
return { };
}
void range_tombstone_stream::forward_to(position_in_partition_view pos) {
_list.erase_where([this, &pos] (const range_tombstone& rt) {
return !_cmp(pos, rt.end_position());
});
}
void range_tombstone_stream::apply(const range_tombstone_list& list, const query::clustering_range& range) {
for (const range_tombstone& rt : list.slice(_schema, range)) {
_list.apply(_schema, rt);
}
}
void range_tombstone_stream::reset() {
_list.clear();
}
bool range_tombstone_stream::empty() const {
return _list.empty();
}
position_range position_range::from_range(const query::clustering_range& range) {
auto bv_range = bound_view::from_range(range);
return {
position_in_partition(position_in_partition::range_tag_t(), bv_range.first),
position_in_partition(position_in_partition::range_tag_t(), bv_range.second)
};
}
position_range::position_range(const query::clustering_range& range)
: position_range(from_range(range))
{ }
position_range::position_range(query::clustering_range&& range)
: position_range(range) // FIXME: optimize
{ }
bool mutation_fragment::relevant_for_range(const schema& s, position_in_partition_view pos) const {
position_in_partition::less_compare cmp(s);
if (!cmp(position(), pos)) {
return true;
}
return relevant_for_range_assuming_after(s, pos);
}
bool mutation_fragment::relevant_for_range_assuming_after(const schema& s, position_in_partition_view pos) const {
position_in_partition::less_compare cmp(s);
// Range tombstones overlapping with the new range are let in
return is_range_tombstone() && cmp(pos, as_range_tombstone().end_position());
}
std::ostream& operator<<(std::ostream& out, const range_tombstone_stream& rtl) {
return out << rtl._list;
}
std::ostream& operator<<(std::ostream& out, const clustering_interval_set& set) {
return out << "{" << ::join(",\n ", set) << "}";
}