forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery-result-reader.hh
164 lines (141 loc) · 5.04 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
/*
* Copyright 2015 Cloudius Systems
*/
#pragma once
#include "query-request.hh"
#include "query-result.hh"
#include "utils/data_input.hh"
// Refer to query-result.hh for the query result format
namespace query {
class result_atomic_cell_view {
api::timestamp_type _timestamp;
expiry_opt _expiry;
bytes_view _value;
public:
result_atomic_cell_view(api::timestamp_type timestamp, expiry_opt expiry, bytes_view value)
: _timestamp(timestamp), _expiry(expiry), _value(value) { }
api::timestamp_type timestamp() const {
return _timestamp;
}
expiry_opt expiry() const {
return _expiry;
}
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 {
bytes_view _v;
const partition_slice& _slice;
public:
result_row_view(bytes_view v, const partition_slice& slice) : _v(v), _slice(slice) {}
class iterator_type {
data_input _in;
const partition_slice& _slice;
public:
iterator_type(bytes_view v, const partition_slice& slice)
: _in(v)
, _slice(slice)
{ }
std::experimental::optional<result_atomic_cell_view> next_atomic_cell() {
auto present = _in.read<int8_t>();
if (!present) {
return {};
}
api::timestamp_type timestamp = api::missing_timestamp;
expiry_opt expiry_;
if (_slice.options.contains<partition_slice::option::send_timestamp_and_expiry>()) {
timestamp = _in.read <api::timestamp_type> ();
auto expiry_rep = _in.read<gc_clock::rep>();
if (expiry_rep != std::numeric_limits<gc_clock::rep>::max()) {
expiry_ = gc_clock::time_point(gc_clock::duration(expiry_rep));
}
}
auto value = _in.read_view_to_blob<uint32_t>();
return {result_atomic_cell_view(timestamp, expiry_, value)};
}
std::experimental::optional<collection_mutation::view> next_collection_cell() {
auto present = _in.read<int8_t>();
if (!present) {
return {};
}
return collection_mutation::view{_in.read_view_to_blob<uint32_t>()};
};
void skip(const column_definition& def) {
if (def.is_atomic()) {
next_atomic_cell();
} else {
next_collection_cell();
}
}
};
iterator_type iterator() const {
return iterator_type(_v, _slice);
}
bool empty() const {
return _v.empty();
}
};
// 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 {
bytes_view _v;
public:
result_view(bytes_view v) : _v(v) {}
template <typename ResultVisitor>
void consume(const partition_slice& slice, ResultVisitor&& visitor) {
data_input in(_v);
while (in.has_next()) {
auto row_count = in.read<uint32_t>();
if (slice.options.contains<partition_slice::option::send_partition_key>()) {
auto key = partition_key::from_bytes(to_bytes(in.read_view_to_blob<uint32_t>()));
visitor.accept_new_partition(key, row_count);
} else {
visitor.accept_new_partition(row_count);
}
bytes_view static_row_view;
if (!slice.static_columns.empty()) {
static_row_view = in.read_view_to_blob<uint32_t>();
}
result_row_view static_row(static_row_view, slice);
while (row_count--) {
if (slice.options.contains<partition_slice::option::send_clustering_key>()) {
auto key = clustering_key::from_bytes(to_bytes(in.read_view_to_blob<uint32_t>()));
result_row_view row(in.read_view_to_blob<uint32_t>(), slice);
visitor.accept_new_row(key, static_row, row);
} else {
result_row_view row(in.read_view_to_blob<uint32_t>(), slice);
visitor.accept_new_row(static_row, row);
}
}
visitor.accept_partition_end(static_row);
}
}
};
}