Tarantool.xs

filter: Statementstmt 85.2% 167 / 196 Branchbran 68.3% 67 / 98 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.5% 234 / 294 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« tp.h ↑ Summary
1 /* vim: set ft=c */
2 /*
3  
4 Copyright (C) 2011 Dmitry E. Oboukhov <unera@debian.org>
5 Copyright (C) 2011 Roman V. Nikolaev <rshadow@rambler.ru>
6  
7 This program is free software, you can redistribute it and/or
8 modify it under the terms of the Artistic License.
9  
10 */
11 #include "EXTERN.h"
12 #include "perl.h"
13 #include "XSUB.h"
14 #include "tp.h"
15 #include "msgpuck.h"
16  
17 extern void _mpack_item(SV *res, SV *o);
18 extern const char *_munpack_item(const char *p,
19 size_t len, SV **res, HV *ext, int utf);
20  
21 #define PREALLOC_SCALAR_SIZE 0
22  
23 272 inline static void hash_ssave(HV *h, const char *k, const char *v) {
24 272 hv_store( h, k, strlen(k), newSVpvn( v, strlen(v) ), 0 );
25 272 }
26  
27 12 inline static void hash_scsave(HV *h, const char *k, SV *sv) {
28 12 hv_store( h, k, strlen(k), sv, 0);
29 12 }
30  
31 464 inline static void hash_isave(HV *h, const char *k, uint32_t v) {
32 464 hv_store( h, k, strlen(k), newSViv( v ), 0 );
33 464 }
34  
35 39 static char * sv_resizer(struct tp *p, size_t req, size_t *size) {
36 39 SV *sv = p->obj;
37 39 STRLEN new_len = tp_size(p) + req;
38 39 ▶ char *new_str = SvGROW(sv, new_len);
Branch: 2 branches
BranchTrueFalseTotal
char *new_str = SvGROW(sv, new_len); 39 0 39
char *new_str = SvGROW(sv, new_len); 24 15 39
39 39 ▶ if (!new_str)
Branch: 1 branch
BranchTrueFalseTotal
if (!new_str) 0 39 39
40 0 croak("Cannot allocate memory");
41 // SvCUR_set(sv, new_len);
42 39 *size = new_len;
43 39 return new_str;
44 }
45  
46  
47 8 inline static void tp_av_tuple(struct tp *req, AV *tuple) {
48 int i;
49 8 tp_tuple(req);
50 20 ▶ for (i = 0; i <= av_len(tuple); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(tuple); i++) { 12 8 20
51 12 SV *field = *av_fetch(tuple, i, 0);
52 char *fd;
53 STRLEN fl;
54 12 fd = SvPV(field, fl);
55 12 tp_field(req, fd, fl);
56 }
57 8 }
58  
59  
60 100 inline static int fetch_tuples( HV * ret, struct tp * rep ) {
61 100 AV * tuples = newAV();
62 100 hv_store( ret, "tuples", 6, newRV_noinc( ( SV * ) tuples ), 0 );
63  
64 int code;
65 108 ▶ while ( (code = tp_next(rep)) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (code = tp_next(rep)) ) { 98 10 108
66  
67 98 ▶ if (code < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (code < 0) 90 8 98
68 90 return code;
69  
70 8 AV * t = newAV();
71 8 av_push( tuples, newRV_noinc( ( SV * ) t ) );
72  
73 26 ▶ while((code = tp_nextfield(rep))) {
Branch: 1 branch
BranchTrueFalseTotal
while((code = tp_nextfield(rep))) { 18 8 26
74 18 ▶ if (code < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (code < 0) 0 18 18
75 0 return code;
76 18 SV * f = newSVpvn(
77 tp_getfield(rep), tp_getfieldsize(rep)
78 );
79 18 av_push( t, f );
80 }
81 }
82 10 return 0;
83 }
84  
85  
86 #define ALLOC_RET_SV(__name, __ptr, __len, __size) \
87 SV *__name = newSVpvn("", 0); \
88 RETVAL = __name; \
89 if (__size) SvGROW(__name, __size); \
90 STRLEN __len; \
91 char *__ptr = SvPV(__name, __len);
92  
93 MODULE = DR::Tarantool PACKAGE = DR::Tarantool
94 PROTOTYPES: ENABLE
95  
96  
97  
98 SV * _pkt_select( req_id, ns, idx, offset, limit, keys )
99 unsigned req_id
100 unsigned ns
101 unsigned idx
102 unsigned offset
103 unsigned limit
104 AV * keys
105  
106  
107 CODE:
108 2 ALLOC_RET_SV(ret, b, len, PREALLOC_SCALAR_SIZE);
109 int k;
110  
111 struct tp req;
112 2 tp_init(&req, b, PREALLOC_SCALAR_SIZE, sv_resizer, ret);
113 2 tp_select(&req, ns, idx, offset, limit);
114  
115 4 ▶ for (k = 0; k <= av_len(keys); k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k <= av_len(keys); k++) { 3 1 4
116 3 SV *t = *av_fetch(keys, k, 0);
117 3 ▶ if (!SvROK(t) || (SvTYPE(SvRV(t)) != SVt_PVAV))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(t) || (SvTYPE(SvRV(t)) != SVt_PVAV)) 2 1 3
if (!SvROK(t) || (SvTYPE(SvRV(t)) != SVt_PVAV)) 0 2 2
118 1 croak("keys must be ARRAYREF of ARRAYREF");
119 2 AV *tuple = (AV *)SvRV(t);
120 2 tp_av_tuple(&req, (AV *)SvRV(t));
121 }
122 1 tp_reqid(&req, req_id);
123 1 SvCUR_set(ret, tp_used(&req));
124 OUTPUT:
125 RETVAL
126  
127  
128 SV * _pkt_ping( req_id )
129 unsigned req_id
130  
131 CODE:
132 1 ALLOC_RET_SV(ret, b, len, 0);
133  
134 struct tp req;
135 1 tp_init(&req, b, len, sv_resizer, ret);
136 1 tp_ping(&req);
137 1 tp_reqid(&req, req_id);
138 1 SvCUR_set(ret, tp_used(&req));
139  
140 OUTPUT:
141 RETVAL
142  
143  
144  
145 SV * _pkt_insert( req_id, ns, flags, tuple )
146 unsigned req_id
147 unsigned ns
148 unsigned flags
149 AV * tuple
150  
151 CODE:
152 1 ALLOC_RET_SV(ret, b, len, PREALLOC_SCALAR_SIZE);
153  
154 struct tp req;
155 1 tp_init(&req, b, PREALLOC_SCALAR_SIZE, sv_resizer, ret);
156 1 tp_insert(&req, ns, flags);
157 1 tp_av_tuple(&req, tuple);
158 1 tp_reqid(&req, req_id);
159  
160 1 SvCUR_set(ret, tp_used(&req));
161  
162 OUTPUT:
163 RETVAL
164  
165 SV * _pkt_delete( req_id, ns, flags, tuple )
166 unsigned req_id
167 unsigned ns
168 unsigned flags
169 AV *tuple
170  
171 CODE:
172 1 ALLOC_RET_SV(ret, b, len, PREALLOC_SCALAR_SIZE);
173  
174 struct tp req;
175 1 tp_init(&req, b, PREALLOC_SCALAR_SIZE, sv_resizer, ret);
176 1 tp_delete(&req, ns, flags);
177 1 tp_av_tuple(&req, tuple);
178 1 tp_reqid(&req, req_id);
179  
180 1 SvCUR_set(ret, tp_used(&req));
181  
182 OUTPUT:
183 RETVAL
184  
185  
186 SV * _pkt_call_lua( req_id, flags, proc, tuple )
187 unsigned req_id
188 unsigned flags
189 SV *proc
190 AV *tuple
191  
192 CODE:
193 STRLEN name_len;
194 2 char *name = SvPV(proc, name_len);
195  
196 2 ALLOC_RET_SV(ret, b, len, PREALLOC_SCALAR_SIZE);
197  
198 struct tp req;
199 2 tp_init(&req, b, PREALLOC_SCALAR_SIZE, sv_resizer, ret);
200 2 tp_call(&req, flags, name, name_len);
201 2 tp_av_tuple(&req, tuple);
202 2 tp_reqid(&req, req_id);
203  
204 2 SvCUR_set(ret, tp_used(&req));
205  
206 OUTPUT:
207 RETVAL
208  
209  
210 SV * _pkt_update( req_id, ns, flags, tuple, operations )
211 unsigned req_id
212 unsigned ns
213 unsigned flags
214 AV *tuple
215 AV *operations
216  
217 CODE:
218 2 ALLOC_RET_SV(ret, b, len, PREALLOC_SCALAR_SIZE);
219 struct tp req;
220 int i;
221 2 tp_init(&req, b, PREALLOC_SCALAR_SIZE, sv_resizer, ret);
222 2 tp_update(&req, ns, flags);
223 2 tp_reqid(&req, req_id);
224 2 tp_av_tuple(&req, tuple);
225 2 tp_updatebegin(&req);
226  
227  
228  
229 10 ▶ for (i = 0; i <= av_len( operations ); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len( operations ); i++) { 9 1 10
230 uint8_t opcode;
231  
232 9 SV *op = *av_fetch( operations, i, 0 );
233 9 ▶ if (!SvROK(op) || SvTYPE( SvRV(op) ) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(op) || SvTYPE( SvRV(op) ) != SVt_PVAV) 9 0 9
if (!SvROK(op) || SvTYPE( SvRV(op) ) != SVt_PVAV) 0 9 9
234 0 croak("Wrong update operation format");
235 9 AV *aop = (AV *)SvRV(op);
236  
237 9 int asize = av_len( aop ) + 1;
238 9 ▶ if ( asize < 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ( asize < 2 ) 0 9 9
239 0 croak("Too short operation argument list");
240  
241 9 unsigned fno = SvIV( *av_fetch( aop, 0, 0 ) );
242 STRLEN size;
243 9 char *opname = SvPV( *av_fetch( aop, 1, 0 ), size );
244  
245  
246 /* delete */
247 9 ▶ if ( strcmp(opname, "delete") == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strcmp(opname, "delete") == 0 ) { 1 8 9
248 1 tp_op(&req, fno, TP_OPDELETE, "", 0);
249 1 continue;
250 }
251  
252  
253 8 ▶ if (asize < 3)
Branch: 1 branch
BranchTrueFalseTotal
if (asize < 3) 0 8 8
254 0 croak("Too short operation argument list");
255  
256 /* assign */
257 8 ▶ if ( strcmp(opname, "set") == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strcmp(opname, "set") == 0 ) { 1 7 8
258  
259 char *data =
260 1 SvPV( *av_fetch( aop, 2, 0 ), size );
261 1 tp_op(&req, fno, TP_OPSET, data, size);
262 1 continue;
263 }
264  
265 /* insert */
266 7 ▶ if ( strcmp(opname, "insert") == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strcmp(opname, "insert") == 0 ) { 1 6 7
267 char *data =
268 1 SvPV( *av_fetch( aop, 2, 0 ), size );
269 1 tp_op(&req, fno, TP_OPINSERT, data, size);
270 1 continue;
271 }
272  
273  
274 /* arithmetic operations */
275 6 ▶ if ( strcmp(opname, "add") == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strcmp(opname, "add") == 0 ) { 1 5 6
276 1 opcode = TP_OPADD;
277 1 goto ARITH;
278 }
279 5 ▶ if ( strcmp(opname, "and") == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strcmp(opname, "and") == 0 ) { 1 4 5
280 1 opcode = TP_OPAND;
281 1 goto ARITH;
282 }
283 4 ▶ if ( strcmp(opname, "or") == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strcmp(opname, "or") == 0 ) { 1 3 4
284 1 opcode = TP_OPOR;
285 1 goto ARITH;
286 }
287 3 ▶ if ( strcmp(opname, "xor") == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strcmp(opname, "xor") == 0 ) { 1 2 3
288 1 opcode = TP_OPXOR;
289 1 goto ARITH;
290 }
291  
292  
293 /* substr */
294 2 ▶ if ( strcmp(opname, "substr") == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strcmp(opname, "substr") == 0 ) { 1 1 2
295 1 ▶ if (asize < 4)
Branch: 1 branch
BranchTrueFalseTotal
if (asize < 4) 0 1 1
296 0 croak("Too short argument "
297 "list for substr");
298 1 unsigned offset =
299 1 SvIV( *av_fetch( aop, 2, 0 ) );
300 1 unsigned length =
301 1 SvIV( *av_fetch( aop, 3, 0 ) );
302 char * data;
303 1 ▶ if ( asize > 4 && SvOK( *av_fetch( aop, 4, 0 ) ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( asize > 4 && SvOK( *av_fetch( aop, 4, 0 ) ) ) { 0 1 1
if ( asize > 4 && SvOK( *av_fetch( aop, 4, 0 ) ) ) { 0 0 0
304 0 data =
305 0 SvPV( *av_fetch( aop, 4, 0 ), size );
306 } else {
307 1 data = "";
308 1 size = 0;
309 }
310  
311 1 tp_opsplice(&req, fno, offset, length,
312 data, size);
313  
314 1 continue;
315 }
316  
317 /* unknown command */
318 1 croak("unknown update operation: `%s'", opname);
319  
320 8 ARITH: {
321 char *data =
322 4 SvPV( *av_fetch( aop, 2, 0 ), size );
323 4 ▶ if (sizeof(unsigned long long) < size)
Branch: 1 branch
BranchTrueFalseTotal
if (sizeof(unsigned long long) < size) 0 4 4
324 0 size = sizeof(unsigned long long);
325 4 tp_op(&req, fno, opcode, data, size);
326 4 continue;
327 }
328  
329 }
330  
331 1 SvCUR_set(ret, tp_used(&req));
332 OUTPUT:
333 RETVAL
334  
335  
336  
337 HV * _pkt_parse_response( response )
338 SV *response
339  
340 INIT:
341 150 RETVAL = newHV();
342 150 sv_2mortal((SV *)RETVAL);
343  
344 CODE:
345 /* asm("break"); */
346 150 ▶ if ( !SvOK(response) )
Branch: 1 branch
BranchTrueFalseTotal
if ( !SvOK(response) ) 1 149 150
347 1 croak( "response is undefined" );
348 STRLEN size;
349 149 char *data = SvPV( response, size );
350  
351 struct tp rep;
352 149 tp_init(&rep, data, size, NULL, 0);
353 // tp_use(&rep, size);
354  
355 149 ssize_t code = tp_reply(&rep);
356  
357 149 ▶ if (code == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (code == -1) { 33 116 149
358 33 hash_ssave(RETVAL, "status", "buffer");
359 33 hash_ssave(RETVAL, "errstr", "Input data too short");
360 116 ▶ } else if (code >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (code >= 0) { 116 0 116
361 116 uint32_t type = tp_replyop(&rep);
362 116 hash_isave(RETVAL, "code", tp_replycode(&rep) );
363 116 hash_isave(RETVAL, "req_id", tp_getreqid(&rep) );
364 116 hash_isave(RETVAL, "type", type );
365 116 hash_isave(RETVAL, "count", tp_replycount(&rep) );
366 116 ▶ if (code == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (code == 0) { 104 12 116
367 104 ▶ if (type != TP_PING)
Branch: 1 branch
BranchTrueFalseTotal
if (type != TP_PING) 100 4 104
368 100 code = fetch_tuples(RETVAL, &rep);
369 104 ▶ if (code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (code != 0) { 90 14 104
370 90 hash_ssave(RETVAL, "status", "buffer");
371 90 hash_ssave(RETVAL, "errstr",
372 "Broken response");
373 } else {
374 14 hash_ssave(RETVAL, "status", "ok");
375 }
376 } else {
377 12 hash_ssave(RETVAL, "status", "error");
378 12 size_t el = tp_replyerrorlen(&rep);
379 SV *err;
380 12 ▶ if (el) {
Branch: 1 branch
BranchTrueFalseTotal
if (el) { 12 0 12
381 12 char *s = tp_replyerror(&rep);
382 12 ▶ if (s[el - 1] == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (s[el - 1] == 0) 12 0 12
383 12 el--;
384 12 err = newSVpvn(s, el);
385 } else {
386 0 err = newSVpvn("", 0);
387 }
388  
389 12 hash_scsave(RETVAL, "errstr", err);
390 }
391 }
392 OUTPUT:
393 RETVAL
394  
395  
396  
397 unsigned TNT_PING()
398 CODE:
399 15 ▶ RETVAL = TP_PING;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = TP_PING; 10 5 15
400 OUTPUT:
401 RETVAL
402  
403  
404 unsigned TNT_CALL()
405 CODE:
406 3 ▶ RETVAL = TP_CALL;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = TP_CALL; 0 3 3
407 OUTPUT:
408 RETVAL
409  
410 unsigned TNT_INSERT()
411 CODE:
412 3 ▶ RETVAL = TP_INSERT;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = TP_INSERT; 0 3 3
413 OUTPUT:
414 RETVAL
415  
416 unsigned TNT_UPDATE()
417 CODE:
418 3 ▶ RETVAL = TP_UPDATE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = TP_UPDATE; 0 3 3
419 OUTPUT:
420 RETVAL
421  
422 unsigned TNT_DELETE()
423 CODE:
424 6 ▶ RETVAL = TP_DELETE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = TP_DELETE; 0 6 6
425 OUTPUT:
426 RETVAL
427  
428 unsigned TNT_SELECT()
429 CODE:
430 123 ▶ RETVAL = TP_SELECT;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = TP_SELECT; 116 7 123
431 OUTPUT:
432 RETVAL
433  
434  
435 unsigned TNT_FLAG_RETURN()
436 CODE:
437 1 ▶ RETVAL = TP_BOX_RETURN_TUPLE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = TP_BOX_RETURN_TUPLE; 0 1 1
438 OUTPUT:
439 RETVAL
440  
441 unsigned TNT_FLAG_ADD()
442 CODE:
443 1 ▶ RETVAL = TP_BOX_ADD;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = TP_BOX_ADD; 0 1 1
444 OUTPUT:
445 RETVAL
446  
447 unsigned TNT_FLAG_REPLACE()
448 CODE:
449 1 ▶ RETVAL = TP_BOX_REPLACE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = TP_BOX_REPLACE; 0 1 1
450 OUTPUT:
451 RETVAL
452  
453  
454  
455 SV * _msgpack(o)
456 SV *o
457 CODE:
458 0 SV *res = newSVpvn("", 0);
459 0 RETVAL = res;
460  
461 0 _mpack_item(res, o);
462 OUTPUT:
463 RETVAL
464  
465 SV * _msgunpack(str, utf)
466 SV *str;
467 SV *utf;
468 PROTOTYPE: $$
469 CODE:
470 0 SV *sv = 0;
471 size_t len;
472 0 const char *s = SvPV(str, len);
473 0 ▶ if (items > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) 0 0 0
474 0 _munpack_item(s, len, &sv, (HV *)ST(1), SvIV(utf));
475 else
476 0 _munpack_item(s, len, &sv, NULL, SvIV(utf));
477 0 RETVAL = sv;
478  
479 OUTPUT:
480 RETVAL
481  
482 size_t _msgcheck(str)
483 SV *str
484 PROTOTYPE: $
485 CODE:
486 int res;
487 size_t len;
488 0 ▶ if (SvOK(str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(str)) { 0 0 0
489 0 const char *p = SvPV(str, len);
490 0 ▶ if (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) { 0 0 0
491 0 const char *pe = p + len;
492 0 const char *begin = p;
493 0 ▶ if (mp_check(&p, pe) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp_check(&p, pe) == 0) { 0 0 0
494 0 RETVAL = p - begin;
495 } else {
496 0 RETVAL = 0;
497 }
498 } else {
499 0 RETVAL = 0;
500 }
501 } else {
502 0 RETVAL = 0;
503 }
504 OUTPUT:
505 RETVAL
506  
507  
508  
« tp.h ↑ Summary