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
|
#include "cursor.h"
#include "cursor_p.h"
#include "exceptions.h"
namespace RethinkDB {
// for type completion, in order to forward declare with unique_ptr
Cursor::Cursor(Cursor&&) = default;
Cursor& Cursor::operator=(Cursor&&) = default;
CursorPrivate::CursorPrivate(uint64_t token_, Connection *conn_)
: single(false), no_more(false), index(0),
token(token_), conn(conn_)
{ }
CursorPrivate::CursorPrivate(uint64_t token_, Connection *conn_, Datum&& datum)
: single(true), no_more(true), index(0), buffer(Array{std::move(datum)}),
token(token_), conn(conn_)
{ }
Cursor::Cursor(CursorPrivate *dd) : d(dd) {}
Cursor::~Cursor() {
try {
if (d && d->conn) {
close();
}
} catch ( ... ) {}
}
Datum& Cursor::next(double wait) const {
if (!has_next(wait)) {
throw Error("next: No more data");
}
return d->buffer[d->index++];
}
Datum& Cursor::peek(double wait) const {
if (!has_next(wait)) {
throw Error("next: No more data");
}
return d->buffer[d->index];
}
void Cursor::each(std::function<void(Datum&&)> f, double wait) const {
while (has_next(wait)) {
f(std::move(d->buffer[d->index++]));
}
}
void CursorPrivate::convert_single() const {
if (index != 0) {
throw Error("Cursor: already consumed");
}
if (buffer.size() != 1) {
throw Error("Cursor: invalid response from server");
}
if (!buffer[0].is_array()) {
throw Error("Cursor: not an array");
}
buffer.swap(buffer[0].extract_array());
single = false;
}
void CursorPrivate::clear_and_read_all() const {
if (single) {
convert_single();
}
if (index != 0) {
buffer.erase(buffer.begin(), buffer.begin() + index);
index = 0;
}
while (!no_more) {
add_response(conn->d->wait_for_response(token, FOREVER));
}
}
Array&& Cursor::to_array() && {
d->clear_and_read_all();
return std::move(d->buffer);
}
Array Cursor::to_array() const & {
d->clear_and_read_all();
return d->buffer;
}
Datum Cursor::to_datum() const & {
if (d->single) {
if (d->index != 0) {
throw Error("to_datum: already consumed");
}
return d->buffer[0];
}
d->clear_and_read_all();
return d->buffer;
}
Datum Cursor::to_datum() && {
Datum ret((Nil()));
if (d->single) {
if (d->index != 0) {
throw Error("to_datum: already consumed");
}
ret = std::move(d->buffer[0]);
} else {
d->clear_and_read_all();
ret = std::move(d->buffer);
}
return ret;
}
void Cursor::close() const {
d->conn->stop_query(d->token);
d->no_more = true;
}
bool Cursor::has_next(double wait) const {
if (d->single) {
d->convert_single();
}
while (true) {
if (d->index >= d->buffer.size()) {
if (d->no_more) {
return false;
}
d->add_response(d->conn->d->wait_for_response(d->token, wait));
} else {
return true;
}
}
}
bool Cursor::is_single() const {
return d->single;
}
void CursorPrivate::add_results(Array&& results) const {
if (index >= buffer.size()) {
buffer = std::move(results);
index = 0;
} else {
for (auto& it : results) {
buffer.emplace_back(std::move(it));
}
}
}
void CursorPrivate::add_response(Response&& response) const {
using RT = Protocol::Response::ResponseType;
switch (response.type) {
case RT::SUCCESS_SEQUENCE:
add_results(std::move(response.result));
no_more = true;
break;
case RT::SUCCESS_PARTIAL:
conn->continue_query(token);
add_results(std::move(response.result));
break;
case RT::SUCCESS_ATOM:
add_results(std::move(response.result));
single = true;
no_more = true;
break;
case RT::SERVER_INFO:
add_results(std::move(response.result));
single = true;
no_more = true;
break;
case RT::WAIT_COMPLETE:
case RT::CLIENT_ERROR:
case RT::COMPILE_ERROR:
case RT::RUNTIME_ERROR:
no_more = true;
throw response.as_error();
}
}
Cursor::iterator Cursor::begin() {
return iterator(this);
}
Cursor::iterator Cursor::end() {
return iterator(nullptr);
}
Cursor::iterator::iterator(Cursor* cursor_) : cursor(cursor_) {}
Cursor::iterator& Cursor::iterator::operator++ () {
if (cursor == nullptr) {
throw Error("incrementing an exhausted Cursor iterator");
}
cursor->next();
return *this;
}
Datum& Cursor::iterator::operator* () {
if (cursor == nullptr) {
throw Error("reading from empty Cursor iterator");
}
return cursor->peek();
}
bool Cursor::iterator::operator!= (const Cursor::iterator& other) const {
if (cursor == other.cursor) {
return false;
}
return !((cursor == nullptr && !other.cursor->has_next()) ||
(other.cursor == nullptr && !cursor->has_next()));
}
}
|