forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
built_indexes_virtual_reader.hh
252 lines (233 loc) · 10.7 KB
/
built_indexes_virtual_reader.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
/*
* Copyright (C) 2018-present 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 "database.hh"
#include "db/system_keyspace.hh"
#include "flat_mutation_reader.hh"
#include "mutation_fragment.hh"
#include "mutation_reader.hh"
#include "query-request.hh"
#include "schema_fwd.hh"
#include "secondary_index_manager.hh"
#include "tracing/tracing.hh"
#include "view_info.hh"
#include <memory>
namespace db::index {
class built_indexes_virtual_reader {
database& _db;
struct built_indexes_reader : flat_mutation_reader::impl {
database& _db;
schema_ptr _schema;
query::partition_slice _view_names_slice;
flat_mutation_reader _underlying;
sstring _current_keyspace;
// Convert a key holding an index name (e.g., xyz) to a key holding
// the backing materialized-view name (e.g., xyz_index).
static clustering_key_prefix index_name_to_view_name(
const clustering_key_prefix& c,
const schema& built_views_schema,
const schema& built_indexes_schema)
{
sstring index_name = value_cast<sstring>(utf8_type->deserialize(*c.begin(built_indexes_schema)));
sstring view_name = ::secondary_index::index_table_name(index_name);
return clustering_key_prefix::from_single_value(built_views_schema, utf8_type->decompose(view_name));
}
// Convert a clustering_range holding a range of index names (e.g.,
// abc..xyz) to a range of backing view names (abc_index..xyz_index).
static query::clustering_range index_name_range_to_view_name_range(
const query::clustering_range& range,
const schema& built_views_schema,
const schema& built_indexes_schema)
{
bool singular = range.is_singular();
auto start = range.start();
auto end = range.end();
if (start) {
*start = query::clustering_range::bound(
index_name_to_view_name(start->value(), built_views_schema, built_indexes_schema),
start->is_inclusive());
}
if (end) {
*end = query::clustering_range::bound(
index_name_to_view_name(end->value(), built_views_schema, built_indexes_schema),
end->is_inclusive());
}
return query::clustering_range(std::move(start), std::move(end), singular);
}
// The index list reader is created with a slice of index names
// (e.g., xyz) but the underlying reader needs a slice of view names,
// e.g., xyz_index, so we need to add the _index suffix with
// index_table_name().
static query::partition_slice index_slice_to_view_slice(
const query::partition_slice& slice,
const schema& built_views_schema,
const schema& built_indexes_schema)
{
// Most of the slice - such as options and which columns to read -
// should be copied unchanged. Just the clustering row ranges need
// changing.
// Fix _row_ranges (used for WHERE specifying a clustering range):
query::partition_slice ret = slice;
for (query::clustering_range &range : ret._row_ranges) {
range = index_name_range_to_view_name_range(range, built_views_schema, built_indexes_schema);
}
// Fix _specific_ranges (used for continuing on paging).
auto& specific_ranges = ret.get_specific_ranges();
if (specific_ranges) {
partition_key pk = specific_ranges->pk();
std::vector<query::clustering_range> ranges;
for (const query::clustering_range &range : specific_ranges->ranges()) {
ranges.push_back(index_name_range_to_view_name_range(range, built_views_schema, built_indexes_schema));
}
ret.clear_ranges();
ret.set_range(built_indexes_schema, pk, ranges);
}
return ret;
}
built_indexes_reader(
database& db,
schema_ptr schema,
reader_permit permit,
column_family& built_views,
const dht::partition_range& range,
const query::partition_slice& slice,
const io_priority_class& pc,
tracing::trace_state_ptr trace_state,
streamed_mutation::forwarding fwd,
mutation_reader::forwarding fwd_mr)
: flat_mutation_reader::impl(schema, permit)
, _db(db)
, _schema(std::move(schema))
, _view_names_slice(index_slice_to_view_slice(slice, *built_views.schema(), *_schema))
, _underlying(built_views.make_reader(
built_views.schema(),
std::move(permit),
range,
_view_names_slice,
pc,
std::move(trace_state),
fwd,
fwd_mr)) {
}
bool is_index(const clustering_row& built_view_row) const {
auto view_name = value_cast<sstring>(utf8_type->deserialize(*built_view_row.key().begin(*_underlying.schema())));
try {
auto s = _db.find_schema(_current_keyspace, view_name);
return s->is_view() && _db.find_column_family(s->view_info()->base_id()).get_index_manager().is_index(view_ptr(s));
} catch (const no_such_column_family&) {
return false;
}
}
virtual future<> fill_buffer() override {
return _underlying.fill_buffer().then([this] {
_end_of_stream = _underlying.is_end_of_stream();
while (!_underlying.is_buffer_empty()) {
auto mf = _underlying.pop_mutation_fragment();
if (mf.is_partition_start()) {
_current_keyspace = value_cast<sstring>(utf8_type->deserialize(*mf.as_partition_start().key().key().begin(*_underlying.schema())));
} else if (mf.is_clustering_row()) {
if (is_index(mf.as_clustering_row())) {
// mf's clustering key is the materialized view's
// name, xyz_index, while we need the index name
// xyz. So modify mf:
mf.mutate_as_clustering_row(*_underlying.schema(),
[&] (clustering_row& cr) mutable {
sstring view_name = value_cast<sstring>(utf8_type->deserialize(*cr.key().begin(*_underlying.schema())));
sstring index_name = ::secondary_index::index_name_from_table_name(view_name);
cr.key() = clustering_key_prefix::from_single_value(*_underlying.schema(), utf8_type->decompose(index_name));
});
} else {
continue;
}
}
push_mutation_fragment(std::move(mf));
}
});
}
virtual future<> next_partition() override {
_end_of_stream = false;
clear_buffer_to_next_partition();
if (is_buffer_empty()) {
return _underlying.next_partition();
}
return make_ready_future<>();
}
virtual future<> fast_forward_to(const dht::partition_range& pr) override {
clear_buffer();
_end_of_stream = false;
return _underlying.fast_forward_to(pr);
}
virtual future<> fast_forward_to(position_range range) override {
forward_buffer_to(range.start());
_end_of_stream = false;
// range contains index names (e.g., xyz) but the underlying table
// contains view names (e.g., xyz_index) so we need to add the
// _index suffix with index_table_name():
position_in_partition start = range.start();
position_in_partition end = range.end();
if (start.get_clustering_key_prefix()) {
clustering_key_prefix ck = index_name_to_view_name(
*start.get_clustering_key_prefix(),
*_underlying.schema(), *_schema);
start = position_in_partition(start.region(),
start.get_bound_weight(),
std::move(ck));
}
if (end.get_clustering_key_prefix()) {
clustering_key_prefix ck = index_name_to_view_name(
*end.get_clustering_key_prefix(),
*_underlying.schema(), *_schema);
start = position_in_partition(end.region(),
end.get_bound_weight(),
std::move(ck));
}
range = position_range(std::move(start), std::move(end));
return _underlying.fast_forward_to(std::move(range));
}
virtual future<> close() noexcept override {
return _underlying.close();
}
};
public:
built_indexes_virtual_reader(database& db)
: _db(db) {
}
flat_mutation_reader operator()(
schema_ptr s,
reader_permit permit,
const dht::partition_range& range,
const query::partition_slice& slice,
const io_priority_class& pc,
tracing::trace_state_ptr trace_state,
streamed_mutation::forwarding fwd,
mutation_reader::forwarding fwd_mr) {
return make_flat_mutation_reader<built_indexes_reader>(
_db,
std::move(s),
std::move(permit),
_db.find_column_family(s->ks_name(), system_keyspace::v3::BUILT_VIEWS),
range,
slice,
pc,
std::move(trace_state),
fwd,
fwd_mr);
}
};
}