forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery-result-reader.hh
203 lines (175 loc) · 6.05 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
/*
* Copyright (C) 2015 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 <boost/range/adaptor/transformed.hpp>
#include "query-request.hh"
#include "query-result.hh"
#include "utils/data_input.hh"
#include "digest_algorithm.hh"
#include "idl/uuid.dist.hh"
#include "idl/keys.dist.hh"
#include "idl/query.dist.hh"
#include "serializer_impl.hh"
#include "serialization_visitors.hh"
#include "idl/query.dist.impl.hh"
#include "idl/keys.dist.impl.hh"
#include "idl/uuid.dist.impl.hh"
namespace query {
class result_atomic_cell_view {
api::timestamp_type _timestamp;
expiry_opt _expiry;
ttl_opt _ttl;
bytes_view _value;
public:
result_atomic_cell_view(api::timestamp_type timestamp, expiry_opt expiry, ttl_opt ttl, bytes_view value)
: _timestamp(timestamp), _expiry(expiry), _ttl(ttl), _value(value) { }
api::timestamp_type timestamp() const {
return _timestamp;
}
expiry_opt expiry() const {
return _expiry;
}
ttl_opt ttl() const {
return _ttl;
}
bytes_view value() const {
return _value;
}
};
// 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_vec = std::vector<std::experimental::optional<ser::qr_cell_view>>;
cells_vec _cells;
cells_vec::iterator _i;
bytes _tmp_value;
public:
iterator_type(ser::qr_row_view v)
: _cells(v.cells())
, _i(_cells.begin())
{ }
std::experimental::optional<result_atomic_cell_view> next_atomic_cell() {
auto cell_opt = *_i++;
if (!cell_opt) {
return {};
}
ser::qr_cell_view v = *cell_opt;
api::timestamp_type timestamp = v.timestamp().value_or(api::missing_timestamp);
expiry_opt expiry = v.expiry();
ttl_opt ttl = v.ttl();
_tmp_value = v.value();
return {result_atomic_cell_view(timestamp, expiry, ttl, _tmp_value)};
}
std::experimental::optional<bytes_view> next_collection_cell() {
auto cell_opt = *_i++;
if (!cell_opt) {
return {};
}
ser::qr_cell_view v = *cell_opt;
_tmp_value = v.value();
return {bytes_view(_tmp_value)};
};
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
uint32_t row_count) {}
void accept_new_partition(uint32_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) {}
};
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) {}
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) {
do_with(res, [&] (result_view v) {
v.consume(slice, visitor);
});
}
template <typename ResultVisitor>
void consume(const partition_slice& slice, ResultVisitor&& visitor) {
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, uint32_t> count_partitions_and_rows() {
auto&& ps = _v.partitions();
auto rows = boost::accumulate(ps | boost::adaptors::transformed([] (auto& p) {
return std::max(p.rows().size(), size_t(1));
}), uint32_t(0));
return std::make_tuple(ps.size(), rows);
}
};
}