forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_set.hh
282 lines (225 loc) · 8.86 KB
/
result_set.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright (C) 2015-present ScyllaDB
*
* Modified by 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/>.
*/
#pragma once
#include <vector>
#include "utils/chunked_vector.hh"
#include "enum_set.hh"
#include "service/pager/paging_state.hh"
#include "query-result-reader.hh"
#include "result_generator.hh"
namespace cql3 {
class metadata {
public:
enum class flag : uint8_t {
GLOBAL_TABLES_SPEC = 0,
HAS_MORE_PAGES = 1,
NO_METADATA = 2,
};
using flag_enum = super_enum<flag,
flag::GLOBAL_TABLES_SPEC,
flag::HAS_MORE_PAGES,
flag::NO_METADATA>;
using flag_enum_set = enum_set<flag_enum>;
struct column_info {
// Please note that columnCount can actually be smaller than names, even if names is not null. This is
// used to include columns in the resultSet that we need to do post-query re-orderings
// (SelectStatement.orderResults) but that shouldn't be sent to the user as they haven't been requested
// (CASSANDRA-4911). So the serialization code will exclude any columns in name whose index is >= columnCount.
std::vector<lw_shared_ptr<column_specification>> _names;
uint32_t _column_count;
column_info(std::vector<lw_shared_ptr<column_specification>> names, uint32_t column_count)
: _names(std::move(names))
, _column_count(column_count)
{ }
explicit column_info(std::vector<lw_shared_ptr<column_specification>> names)
: _names(std::move(names))
, _column_count(_names.size())
{ }
};
private:
flag_enum_set _flags;
private:
lw_shared_ptr<column_info> _column_info;
lw_shared_ptr<const service::pager::paging_state> _paging_state;
public:
metadata(std::vector<lw_shared_ptr<column_specification>> names_);
metadata(flag_enum_set flags, std::vector<lw_shared_ptr<column_specification>> names_, uint32_t column_count,
lw_shared_ptr<const service::pager::paging_state> paging_state);
// The maximum number of values that the ResultSet can hold. This can be bigger than columnCount due to CASSANDRA-4911
uint32_t value_count() const;
void add_non_serialized_column(lw_shared_ptr<column_specification> name);
private:
bool all_in_same_cf() const;
public:
void set_paging_state(lw_shared_ptr<const service::pager::paging_state> paging_state);
void maybe_set_paging_state(lw_shared_ptr<const service::pager::paging_state> paging_state);
void set_skip_metadata();
flag_enum_set flags() const;
uint32_t column_count() const { return _column_info->_column_count; }
lw_shared_ptr<const service::pager::paging_state> paging_state() const;
const std::vector<lw_shared_ptr<column_specification>>& get_names() const {
return _column_info->_names;
}
};
::shared_ptr<const cql3::metadata> make_empty_metadata();
class prepared_metadata {
public:
enum class flag : uint32_t {
GLOBAL_TABLES_SPEC = 0,
// Denotes whether the prepared statement at hand is an LWT statement.
//
// Use the last available bit in the flags since we don't want to clash
// with C* in case they add some other flag in one the next versions of binary protocol.
LWT = 31
};
using flag_enum = super_enum<flag,
flag::GLOBAL_TABLES_SPEC,
flag::LWT>;
using flag_enum_set = enum_set<flag_enum>;
static constexpr flag_enum_set::mask_type LWT_FLAG_MASK = flag_enum_set::mask_for<flag::LWT>();
private:
flag_enum_set _flags;
std::vector<lw_shared_ptr<column_specification>> _names;
std::vector<uint16_t> _partition_key_bind_indices;
public:
prepared_metadata(const std::vector<lw_shared_ptr<column_specification>>& names,
const std::vector<uint16_t>& partition_key_bind_indices,
bool is_conditional);
flag_enum_set flags() const;
const std::vector<lw_shared_ptr<column_specification>>& names() const;
const std::vector<uint16_t>& partition_key_bind_indices() const;
};
template<typename Visitor>
concept ResultVisitor = requires(Visitor& visitor) {
visitor.start_row();
visitor.accept_value(std::optional<query::result_bytes_view>());
visitor.end_row();
};
class result_set {
::shared_ptr<metadata> _metadata;
utils::chunked_vector<std::vector<bytes_opt>> _rows;
friend class result;
public:
result_set(std::vector<lw_shared_ptr<column_specification>> metadata_);
result_set(::shared_ptr<metadata> metadata);
result_set(result_set&& other) = default;
result_set(const result_set& other) = delete;
size_t size() const;
bool empty() const;
void add_row(std::vector<bytes_opt> row);
void add_column_value(bytes_opt value);
void reverse();
void trim(size_t limit);
template<typename RowComparator>
void sort(const RowComparator& cmp) {
std::sort(_rows.begin(), _rows.end(), std::ref(cmp));
}
metadata& get_metadata();
const metadata& get_metadata() const;
// Returns a range of rows. A row is a range of bytes_opt.
const utils::chunked_vector<std::vector<bytes_opt>>& rows() const;
template<typename Visitor>
requires ResultVisitor<Visitor>
void visit(Visitor&& visitor) const {
auto column_count = get_metadata().column_count();
for (auto& row : _rows) {
visitor.start_row();
for (auto i = 0u; i < column_count; i++) {
auto& cell = row[i];
visitor.accept_value(cell ? std::optional<query::result_bytes_view>(*cell) : std::optional<query::result_bytes_view>());
}
visitor.end_row();
}
}
class builder;
};
class result_set::builder {
result_set _result;
std::vector<bytes_opt> _current_row;
public:
explicit builder(shared_ptr<metadata> mtd)
: _result(std::move(mtd)) { }
void start_row() { }
void accept_value(std::optional<query::result_bytes_view> value) {
if (!value) {
_current_row.emplace_back();
return;
}
_current_row.emplace_back(value->linearize());
}
void end_row() {
_result.add_row(std::exchange(_current_row, { }));
}
result_set get_result_set() && { return std::move(_result); }
};
class result {
mutable std::unique_ptr<cql3::result_set> _result_set;
result_generator _result_generator;
shared_ptr<const cql3::metadata> _metadata;
public:
explicit result(std::unique_ptr<cql3::result_set> rs)
: _result_set(std::move(rs))
, _metadata(_result_set->_metadata)
{ }
explicit result(result_generator generator, shared_ptr<const metadata> m)
: _result_generator(std::move(generator))
, _metadata(std::move(m))
{ }
const cql3::metadata& get_metadata() const { return *_metadata; }
const cql3::result_set& result_set() const {
if (_result_set) {
return *_result_set;
}
auto builder = result_set::builder(make_shared<cql3::metadata>(*_metadata));
_result_generator.visit(builder);
auto tmp_rs = std::make_unique<cql3::result_set>(std::move(builder).get_result_set());
_result_set.swap(tmp_rs);
return *_result_set;
}
template<typename Visitor>
requires ResultVisitor<Visitor>
void visit(Visitor&& visitor) const {
if (_result_set) {
_result_set->visit(std::forward<Visitor>(visitor));
} else {
_result_generator.visit(std::forward<Visitor>(visitor));
}
}
};
}