This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Cursor.xs
316 lines (254 loc) · 8.07 KB
/
Cursor.xs
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright 2009 10gen, Inc.
*
* Licensed 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.
*/
#include "perl_mongo.h"
#include "mongo_link.h"
static int
cursor_free (pTHX_ SV *sv, MAGIC *mg)
{
mongo_cursor *cursor;
PERL_UNUSED_ARG(sv);
cursor = (mongo_cursor *)mg->mg_ptr;
if (cursor) {
if (cursor->buf.start) {
Safefree(cursor->buf.start);
}
Safefree(cursor);
}
mg->mg_ptr = NULL;
return 0;
}
static int
cursor_clone (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
{
mongo_cursor *cursor, *new_cursor;
size_t buflen;
PERL_UNUSED_ARG (params);
cursor = (mongo_cursor *)mg->mg_ptr;
Newx(new_cursor, 1, mongo_cursor);
Copy(cursor, new_cursor, 1, mongo_cursor);
buflen = cursor->buf.end - cursor->buf.start;
Newx(new_cursor->buf.start, buflen, char);
Copy(cursor->buf.start, new_cursor->buf.start, buflen, char);
new_cursor->buf.end = new_cursor->buf.start + buflen;
new_cursor->buf.pos =
new_cursor->buf.start + (cursor->buf.pos - cursor->buf.start);
mg->mg_ptr = (char *)new_cursor;
return 0;
}
MGVTBL cursor_vtbl = {
NULL,
NULL,
NULL,
NULL,
cursor_free,
#if MGf_COPY
NULL,
#endif
#if MGf_DUP
cursor_clone,
#endif
#if MGf_LOCAL
NULL,
#endif
};
static mongo_cursor* get_cursor(SV *self);
static int has_next(SV *self, mongo_cursor *cursor);
static void kill_cursor(SV *self);
static mongo_cursor* get_cursor(SV *self) {
perl_mongo_call_method(self, "_do_query", G_DISCARD, 0);
return (mongo_cursor*)perl_mongo_get_ptr_from_instance(self, &cursor_vtbl);
}
static int has_next(SV *self, mongo_cursor *cursor) {
SV *link, *limit, *ns, *request_id, *response_to;
mongo_msg_header header;
buffer buf;
int size, heard;
limit = perl_mongo_call_reader (self, "_limit");
if ((SvIV(limit) > 0 && cursor->at >= SvIV(limit)) ||
cursor->num == 0 ||
(cursor->at == cursor->num && cursor->cursor_id == 0)) {
SvREFCNT_dec(limit);
return 0;
}
else if (cursor->at < cursor->num) {
SvREFCNT_dec(limit);
return 1;
}
link = perl_mongo_call_reader (self, "_client");
ns = perl_mongo_call_reader (self, "_ns");
// we have to go and check with the db
size = 34+strlen(SvPV_nolen(ns));
Newx(buf.start, size, char);
buf.pos = buf.start;
buf.end = buf.start + size;
response_to = perl_mongo_call_reader(self, "_request_id");
request_id = get_sv("MongoDB::Cursor::_request_id", GV_ADD);
CREATE_RESPONSE_HEADER(buf, SvPV_nolen(ns), SvIV(response_to), OP_GET_MORE);
// change this cursor's request id so we can match the response
perl_mongo_call_method(self, "_request_id", G_DISCARD, 1, request_id);
SvREFCNT_dec(response_to);
perl_mongo_serialize_int(&buf, SvIV(limit));
perl_mongo_serialize_long(&buf, cursor->cursor_id);
perl_mongo_serialize_size(buf.start, &buf);
SvREFCNT_dec(limit);
SvREFCNT_dec(ns);
// fails if we're out of elems
if(mongo_link_say(link, &buf) == -1) {
SvREFCNT_dec(link);
Safefree(buf.start);
die("can't get db response, not connected");
return 0;
}
Safefree(buf.start);
// if we have cursor->at == cursor->num && recv fails,
// we're probably just out of results
// mongo_link_hear returns 1 on success, 0 on failure
heard = mongo_link_hear(self);
SvREFCNT_dec(link);
return heard > 0;
}
static void kill_cursor(SV *self) {
mongo_cursor *cursor = (mongo_cursor*)perl_mongo_get_ptr_from_instance(self, &cursor_vtbl);
SV *link = perl_mongo_call_reader (self, "_client");
SV *request_id_sv = perl_mongo_call_reader (self, "_request_id");
char quickbuf[128];
buffer buf;
mongo_msg_header header;
// we allocate a cursor even if no results are returned, but the database will
// throw an assertion if we try to kill a non-existant cursor non-cursors have
// ids of 0
if (cursor->cursor_id == 0) {
SvREFCNT_dec(link);
SvREFCNT_dec(request_id_sv);
return;
}
buf.pos = quickbuf;
buf.start = buf.pos;
buf.end = buf.start + 128;
// std header
CREATE_MSG_HEADER(SvIV(request_id_sv), 0, OP_KILL_CURSORS);
SvREFCNT_dec(request_id_sv);
APPEND_HEADER(buf, 0);
// # of cursors
perl_mongo_serialize_int(&buf, 1);
// cursor ids
perl_mongo_serialize_long(&buf, cursor->cursor_id);
perl_mongo_serialize_size(buf.start, &buf);
mongo_link_say(link, &buf);
SvREFCNT_dec(link);
}
MODULE = MongoDB::Cursor PACKAGE = MongoDB::Cursor
PROTOTYPES: DISABLE
void
_init (self)
SV *self
PREINIT:
mongo_cursor *cursor;
CODE:
Newxz(cursor, 1, mongo_cursor);
// attach a mongo_cursor* to the MongoDB::Cursor
perl_mongo_attach_ptr_to_instance(self, cursor, &cursor_vtbl);
bool
has_next (self)
SV *self
PREINIT:
mongo_cursor *cursor;
CODE:
cursor = get_cursor(self);
RETVAL = has_next(self, cursor);
OUTPUT:
RETVAL
SV *
next (self)
SV *self
PREINIT:
mongo_cursor *cursor;
SV *dt_type_sv;
CODE:
cursor = get_cursor(self);
if (has_next(self, cursor)) {
dt_type_sv = perl_mongo_call_reader( self, "_dt_type" );
if ( SvOK( dt_type_sv ) ) {
char *dt_type = SvPV( dt_type_sv, SvLEN( dt_type_sv ) );
RETVAL = perl_mongo_bson_to_sv(&cursor->buf, dt_type);
} else {
// dt type is undef
RETVAL = perl_mongo_bson_to_sv(&cursor->buf, NULL);
}
cursor->at++;
if (cursor->num == 1 &&
hv_exists((HV*)SvRV(RETVAL), "$err", strlen("$err"))) {
SV **err = 0, **code = 0;
err = hv_fetch((HV*)SvRV(RETVAL), "$err", strlen("$err"), 0);
code = hv_fetch((HV*)SvRV(RETVAL), "code", strlen("code"), 0);
if (code && SvIOK(*code) &&
(SvIV(*code) == 10107 || SvIV(*code) == 13435 || SvIV(*code) == 13436)) {
SV *conn = perl_mongo_call_method (self, "_client", 0, 0);
set_disconnected(conn);
}
croak("query error: %s", SvPV_nolen(*err));
}
} else {
RETVAL = newSV(0);
}
OUTPUT:
RETVAL
SV *
reset (self)
SV *self
PREINIT:
mongo_cursor *cursor;
CODE:
cursor = (mongo_cursor*)perl_mongo_get_ptr_from_instance(self, &cursor_vtbl);
cursor->buf.pos = cursor->buf.start;
cursor->at = 0;
cursor->num = 0;
perl_mongo_call_method (self, "started_iterating", G_DISCARD, 1, &PL_sv_no);
RETVAL = SvREFCNT_inc(self);
OUTPUT:
RETVAL
SV *
info (self)
SV *self
PREINIT:
mongo_cursor *cursor;
HV *hv;
CODE:
cursor = (mongo_cursor*)perl_mongo_get_ptr_from_instance(self, &cursor_vtbl);
hv = newHV();
hv_store(hv, "flag", strlen("flag"), newSViv(cursor->flag), 0);
hv_store(hv, "cursor_id", strlen("cursor_id"),
newSViv(cursor->cursor_id), 0);
hv_store(hv, "start", strlen("start"), newSViv(cursor->start), 0);
hv_store(hv, "at", strlen("at"), newSViv(cursor->at), 0);
hv_store(hv, "num", strlen("num"), newSViv(cursor->num), 0);
RETVAL = newRV_noinc((SV*)hv);
OUTPUT:
RETVAL
void
DESTROY (self)
SV *self
PREINIT:
mongo_link *link;
SV *link_sv;
CODE:
link_sv = perl_mongo_call_reader(self, "_client");
link = (mongo_link*)perl_mongo_get_ptr_from_instance(link_sv, &connection_vtbl);
// check if cursor is connected
if (link->master && link->master->connected) {
kill_cursor(self);
}
SvREFCNT_dec(link_sv);