forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery-result-reader.hh
212 lines (181 loc) · 6.13 KB
/
query-result-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
/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include "utils/assert.hh"
#include "query-result.hh"
#include "full_position.hh"
#include "idl/query.dist.hh"
#include "idl/query.dist.impl.hh"
namespace query {
using result_bytes_view = ser::buffer_view<bytes_ostream::fragment_iterator>;
class result_atomic_cell_view {
ser::qr_cell_view _view;
public:
result_atomic_cell_view(ser::qr_cell_view view)
: _view(view) { }
api::timestamp_type timestamp() const {
return _view.timestamp().value_or(api::missing_timestamp);
}
expiry_opt expiry() const {
return _view.expiry();
}
ttl_opt ttl() const {
return _view.ttl();
}
result_bytes_view value() const {
return _view.value().view();
}
};
// Contains cells in the same order as requested by partition_slice.
// Contains only live cells.
class result_row_view {
ser::qr_row_view _v;
public:
result_row_view(ser::qr_row_view v) : _v(v) {}
class iterator_type {
using cells_deserializer = ser::vector_deserializer<std::optional<ser::qr_cell_view>>;
cells_deserializer _cells;
cells_deserializer::iterator _i;
public:
iterator_type(const ser::qr_row_view& v)
: _cells(v.cells())
, _i(_cells.begin())
{ }
std::optional<result_atomic_cell_view> next_atomic_cell() {
auto cell_opt = *_i++;
if (!cell_opt) {
return {};
}
return {result_atomic_cell_view(*cell_opt)};
}
std::optional<result_bytes_view> next_collection_cell() {
auto cell_opt = *_i++;
if (!cell_opt) {
return {};
}
ser::qr_cell_view v = *cell_opt;
return {v.value().view()};
};
void skip(const column_definition& def) {
++_i;
}
};
iterator_type iterator() const {
return iterator_type(_v);
}
};
// Describes expectations about the ResultVisitor concept.
//
// Interaction flow:
// -> accept_new_partition()
// -> accept_new_row()
// -> accept_new_row()
// -> accept_partition_end()
// -> accept_new_partition()
// -> accept_new_row()
// -> accept_new_row()
// -> accept_new_row()
// -> accept_partition_end()
// ...
//
struct result_visitor {
void accept_new_partition(
const partition_key& key, // FIXME: use view for the key
uint64_t row_count) {}
void accept_new_partition(uint64_t row_count) {}
void accept_new_row(
const clustering_key& key, // FIXME: use view for the key
const result_row_view& static_row,
const result_row_view& row) {}
void accept_new_row(const result_row_view& static_row, const result_row_view& row) {}
void accept_partition_end(const result_row_view& static_row) {}
};
template<typename Visitor>
concept ResultVisitor = requires(Visitor visitor, const partition_key& pkey,
uint64_t row_count, const clustering_key& ckey,
const result_row_view& static_row, const result_row_view& row)
{
visitor.accept_new_partition(pkey, row_count);
visitor.accept_new_partition(row_count);
visitor.accept_new_row(ckey, static_row, row);
visitor.accept_new_row(static_row, row);
visitor.accept_partition_end(static_row);
};
class result_view {
ser::query_result_view _v;
friend class result_merger;
public:
result_view(const bytes_ostream& v) : _v(ser::query_result_view{ser::as_input_stream(v)}) {}
result_view(ser::query_result_view v) : _v(v) {}
explicit result_view(const query::result& res) : result_view(res.buf()) { }
template <typename Func>
static auto do_with(const query::result& res, Func&& func) {
result_view view(res.buf());
return func(view);
}
template <typename ResultVisitor>
static void consume(const query::result& res, const partition_slice& slice, ResultVisitor&& visitor) {
result_view(res).consume(slice, visitor);
}
template <typename Visitor>
requires ResultVisitor<Visitor>
void consume(const partition_slice& slice, Visitor&& visitor) const {
for (auto&& p : _v.partitions()) {
auto rows = p.rows();
auto row_count = rows.size();
if (slice.options.contains<partition_slice::option::send_partition_key>()) {
auto key = *p.key();
visitor.accept_new_partition(key, row_count);
} else {
visitor.accept_new_partition(row_count);
}
result_row_view static_row(p.static_row());
for (auto&& row : rows) {
result_row_view view(row.cells());
if (slice.options.contains<partition_slice::option::send_clustering_key>()) {
visitor.accept_new_row(*row.key(), static_row, view);
} else {
visitor.accept_new_row(static_row, view);
}
}
visitor.accept_partition_end(static_row);
}
}
std::tuple<uint32_t, uint64_t> count_partitions_and_rows() const {
auto ps = _v.partitions();
uint64_t rows = 0;
for (auto p : ps) {
rows += std::max(p.rows().size(), size_t(1));
}
return std::make_tuple(ps.size(), rows);
}
full_position calculate_last_position() const {
auto ps = _v.partitions();
SCYLLA_ASSERT(!ps.empty());
auto pit = ps.begin();
auto pnext = pit;
while (++pnext != ps.end()) {
pit = pnext;
}
auto p = *pit;
auto rs = p.rows();
auto pos = position_in_partition::for_partition_start();
if (!rs.empty()) {
auto rit = rs.begin();
auto rnext = rit;
while (++rnext != rs.end()) {
rit = rnext;
}
const auto& key_opt = (*rit).key();
if (key_opt) {
pos = position_in_partition::for_key(*key_opt);
}
}
return { p.key().value(), std::move(pos) };
}
};
}