forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutation_reader.cc
228 lines (202 loc) · 7.16 KB
/
mutation_reader.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
/*
* Copyright 2015 Cloudius Systems
*/
/*
* 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 <boost/range/algorithm/heap_algorithm.hpp>
#include <boost/range/algorithm/reverse.hpp>
#include "mutation_reader.hh"
#include "core/future-util.hh"
namespace stdx = std::experimental;
template<typename T>
inline
std::experimental::optional<T>
move_and_disengage(std::experimental::optional<T>& opt) {
auto t = std::move(opt);
opt = std::experimental::nullopt;
return t;
}
// Combines multiple mutation_readers into one.
class combined_reader final : public mutation_reader::impl {
std::vector<mutation_reader> _readers;
struct mutation_and_reader {
mutation m;
mutation_reader* read;
};
std::vector<mutation_and_reader> _ptables;
// comparison function for std::make_heap()/std::push_heap()
static bool heap_compare(const mutation_and_reader& a, const mutation_and_reader& b) {
auto&& s = a.m.schema();
// order of comparison is inverted, because heaps produce greatest value first
return b.m.decorated_key().less_compare(*s, a.m.decorated_key());
}
mutation_opt _current;
bool _inited = false;
private:
// Produces next mutation or disengaged optional if there are no more.
//
// Entry conditions:
// - either _ptables is empty or_ptables.back() is the next item to be consumed.
// - the _ptables heap is in invalid state (if not empty), waiting for pop_back or push_heap.
future<mutation_opt> next() {
if (_ptables.empty()) {
return make_ready_future<mutation_opt>(move_and_disengage(_current));
};
auto& candidate = _ptables.back();
mutation& m = candidate.m;
if (_current && !_current->decorated_key().equal(*m.schema(), m.decorated_key())) {
// key has changed, so emit accumulated mutation
return make_ready_future<mutation_opt>(move_and_disengage(_current));
}
apply(_current, std::move(m));
return (*candidate.read)().then([this] (mutation_opt&& more) {
// Restore heap to valid state
if (!more) {
_ptables.pop_back();
} else {
_ptables.back().m = std::move(*more);
boost::range::push_heap(_ptables, &heap_compare);
}
boost::range::pop_heap(_ptables, &heap_compare);
return next();
});
}
public:
combined_reader(std::vector<mutation_reader> readers)
: _readers(std::move(readers))
{ }
virtual future<mutation_opt> operator()() override {
if (!_inited) {
return parallel_for_each(_readers, [this] (mutation_reader& reader) {
return reader().then([this, &reader](mutation_opt&& m) {
if (m) {
_ptables.push_back({std::move(*m), &reader});
}
});
}).then([this] {
boost::range::make_heap(_ptables, &heap_compare);
boost::range::pop_heap(_ptables, &heap_compare);
_inited = true;
return next();
});
}
return next();
}
};
mutation_reader
make_combined_reader(std::vector<mutation_reader> readers) {
return make_mutation_reader<combined_reader>(std::move(readers));
}
class joining_reader final : public mutation_reader::impl {
std::vector<mutation_reader> _readers;
std::vector<mutation_reader>::iterator _current;
public:
joining_reader(std::vector<mutation_reader> readers)
: _readers(std::move(readers))
, _current(_readers.begin()) {
}
joining_reader(joining_reader&&) = default;
virtual future<mutation_opt> operator()() override {
if (_current == _readers.end()) {
return make_ready_future<mutation_opt>(stdx::nullopt);
}
return (*_current)().then([this] (mutation_opt m) {
if (!m) {
++_current;
return operator()();
} else {
return make_ready_future<mutation_opt>(std::move(m));
}
});
}
};
mutation_reader
make_combined_reader(mutation_reader&& a, mutation_reader&& b) {
std::vector<mutation_reader> v;
v.reserve(2);
v.push_back(std::move(a));
v.push_back(std::move(b));
return make_combined_reader(std::move(v));
}
mutation_reader
make_joining_reader(std::vector<mutation_reader> readers) {
return make_mutation_reader<joining_reader>(std::move(readers));
}
class lazy_reader final : public mutation_reader::impl {
std::function<mutation_reader ()> _make_reader;
stdx::optional<mutation_reader> _reader;
public:
lazy_reader(std::function<mutation_reader ()> make_reader)
: _make_reader(std::move(make_reader)) {
}
virtual future<mutation_opt> operator()() override {
if (!_reader) {
_reader = _make_reader();
}
return (*_reader)();
}
};
mutation_reader
make_lazy_reader(std::function<mutation_reader ()> make_reader) {
return make_mutation_reader<lazy_reader>(std::move(make_reader));
}
class reader_returning final : public mutation_reader::impl {
mutation _m;
bool _done = false;
public:
reader_returning(mutation m) : _m(std::move(m)) {
}
virtual future<mutation_opt> operator()() override {
if (_done) {
return make_ready_future<mutation_opt>();
} else {
_done = true;
return make_ready_future<mutation_opt>(std::move(_m));
}
}
};
mutation_reader make_reader_returning(mutation m) {
return make_mutation_reader<reader_returning>(std::move(m));
}
class reader_returning_many final : public mutation_reader::impl {
std::vector<mutation> _m;
bool _done = false;
public:
reader_returning_many(std::vector<mutation> m) : _m(std::move(m)) {
boost::range::reverse(_m);
}
virtual future<mutation_opt> operator()() override {
if (_m.empty()) {
return make_ready_future<mutation_opt>();
}
auto m = std::move(_m.back());
_m.pop_back();
return make_ready_future<mutation_opt>(std::move(m));
}
};
mutation_reader make_reader_returning_many(std::vector<mutation> mutations) {
return make_mutation_reader<reader_returning_many>(std::move(mutations));
}
class empty_reader final : public mutation_reader::impl {
public:
virtual future<mutation_opt> operator()() override {
return make_ready_future<mutation_opt>();
}
};
mutation_reader make_empty_reader() {
return make_mutation_reader<empty_reader>();
}