tp.h

filter: Statementstmt 79.8% 270 / 338 Branchbran 62.3% 76 / 122 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.2% 346 / 460 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« msgpuck.h ↑ Summary Tarantool.xs »
1 #ifndef TP_H_INCLUDED
2 #define TP_H_INCLUDED
3  
4 /*
5 * TP - Tarantool Protocol library.
6 * (http://tarantool.org)
7 *
8 * protocol description:
9 * https://github.com/tarantool/tarantool/blob/master/doc/box-protocol.txt
10 * -------------------
11 *
12 * TP - a C library designed to create requests and process
13 * replies to or from a Tarantool server.
14 *
15 * The library is designed to be used by a C/C++ application which
16 * requires sophisticated memory control and/or performance.
17 *
18 * The library does not support network operations. All operations
19 * are done in a user supplied buffer and with help of
20 * a user-supplied allocator.
21 *
22 * The primary purpose of the library was to spare implementors
23 * of Tarantool drivers in other languages, such as Perl,
24 * Ruby, Python, etc, from the details of the binary protocol, and
25 * also to make it possible to avoid double-buffering by writing
26 * directly to/from language objects from/to a serialized binary
27 * packet stream. This paradigm makes data transfer from domain
28 * language types (such as strings, scalars, numbers, etc) to
29 * the network format direct, and, therefore, most efficient.
30 *
31 * As a side effect, the library is usable in any kind of
32 * networking environment: synchronous with buffered sockets, or
33 * asynchronous event-based, as well as with cooperative
34 * multitasking.
35 *
36 * Before using the library, please get acquainted with
37 * Tarnatool binary protocol, documented at
38 * https://github.com/tarantool/tarantool/blob/master/doc/box-protocol.txt
39 *
40 * BASIC REQUEST STRUCTURE
41 * -----------------------
42 *
43 * Any request in Tarantool consists of a 12-byte header,
44 * containing request type, id and length, and an optional tuple
45 * or tuples. Similarly, a response carries back the same request
46 * type and id, and then a tuple or tuples.
47 *
48 * Below is a step-by-step tutorial for creating requests
49 * and unpacking responses.
50 *
51 * TO ASSEMBLE A REQUEST
52 * ---------------------
53 *
54 * (1) initialize an instance of struct tp with tp_init().
55 * Provide tp_init() with a buffer and an (optional) allocator
56 * function.
57 *
58 * (2) construct requests by sequentially calling necessary
59 * operations, such as tp_insert(), tp_delete(), tp_update(),
60 * tp_call(). Note: these operations only append to the buffer
61 * a request header, a body of the request, which is usually
62 * a tuple, must be appended to the buffer with a separate call.
63 * Each next call of tp_*() API appends request data to
64 * the tail of the buffer. If the buffer becomes too small to
65 * contain the binary stream, the reallocation function is
66 * invoked to enlarge the buffer.
67 * A buffer can contain multiple requests: Tarantool
68 * handles them all asynchronously, sending responses
69 * back as soon as they are ready. The request id can be then
70 * used to associate responses with requests.
71 *
72 * For example:
73 *
74 * char buf[256];
75 * struct tp req;
76 * // initialize request buffer
77 * tp_init(&req, buf, sizeof(buf), NULL, NULL);
78 * // append INSERT packet header to the buffer
79 * // request flags are empty, request id is 0
80 * tp_insert(&req, 0, 0);
81 * // begin appending a tuple to the request
82 * tp_tuple(&req);
83 * // append one tuple field
84 * tp_sz(&req, "key");
85 * // one more tuple field
86 * tp_sz(&req, "value");
87 *
88 * (3) the buffer can be used right after all requests are
89 * appended to it. tp_used() can be used to get the current
90 * buffer size:
91 *
92 * write(1, buf, tp_used(&req)); // write the buffer to stdout
93 *
94 * (4) When no longer needed, the buffer must be freed manually.
95 *
96 * For additional examples, please read the documentation for
97 * buffer operations.
98 *
99 * PROCESSING A REPLY
100 * ------------------
101 *
102 * (1) tp_init() must be called with a pointer to a buffer which
103 * already stores or will eventually receive the server response.
104 * Functions tp_reqbuf() and tp_req() can be then used to examine
105 * if a network buffer contains a full reply or not.
106 *
107 * Following is an example of tp_req() usage (reading from stdin
108 * and parsing it until a response is completely read):
109 *
110 * struct tp rep;
111 * tp_init(&rep, NULL, 0, tp_realloc, NULL);
112 *
113 * while (1) {
114 * ssize_t to_read = tp_req(&rep);
115 * printf("to_read: %zu\n", to_read);
116 * if (to_read <= 0)
117 * break;
118 * ssize_t new_size = tp_ensure(&rep, to_read);
119 * printf("new_size: %zu\n", new_size);
120 * if (new_size == -1)
121 * return -1;
122 * int rc = fread(rep.p, to_read, 1, stdin);
123 * if (rc != 1)
124 * return 1;
125 * // discard processed data and make space available
126 * // for new input:
127 * tp_use(&rep, to_read);
128 * }
129 *
130 * (2) tp_reply() function can be used to find out if the request
131 * is executed successfully or not:
132 * server_code = tp_reply(&reply);
133 *
134 * if (server_code != 0) {
135 * printf("error: %-.*s\n", tp_replyerrorlen(&rep),
136 * tp_replyerror(&rep));
137 * }
138 *
139 * Note: the library itself doesn't contain #defines for server
140 * error codes. They are defined in
141 * https://github.com/tarantool/tarantool/blob/master/include/errcode.h
142 *
143 * A server failure can be either transient or persistent. For
144 * example, a failure to allocate memory is transient: as soon as
145 * some data is deleted, the request can be executed again, this
146 * time successfully. A constraint violation is a non-transient
147 * error: it persists regardless of how many times a request
148 * is re-executed. Server error codes can be analyzed to better
149 * handle an error.
150 *
151 * (3) The server usually responds to any kind of request with a
152 * tuple. Tuple data can be accessed via tp_next(), tp_nextfield(),
153 * tp_gettuple(), tp_getfield().
154 *
155 * See the docs for tp_reply() and tp_next()/tp_nextfield() for an
156 * example.
157 *
158 * API RETURN VALUE CONVENTION
159 * ---------------------------
160 *
161 * API functions return 0 on success, -1 on error.
162 * If a function appends data to struct tp, it returns the
163 * size appended on success, or -1 on error.
164 *
165 * SEE ALSO
166 * --------
167 *
168 * TP is used by Tarantool Perl driver:
169 * https://github.com/dr-co/dr-tarantool/blob/master/Tarantool.xs
170 */
171  
172 /*
173 * Copyright (c) 2012-2013 Tarantool AUTHORS
174 * (https://github.com/tarantool/tarantool/blob/master/AUTHORS)
175 *
176 * Redistribution and use in source and binary forms, with or
177 * without modification, are permitted provided that the following
178 * conditions are met:
179 *
180 * 1. Redistributions of source code must retain the above
181 * copyright notice, this list of conditions and the
182 * following disclaimer.
183 *
184 * 2. Redistributions in binary form must reproduce the above
185 * copyright notice, this list of conditions and the following
186 * disclaimer in the documentation and/or other materials
187 * provided with the distribution.
188 *
189 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
190 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
191 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
193 * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
194 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
195 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
196 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
197 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
198 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
199 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
200 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
201 * SUCH DAMAGE.
202 */
203 #ifdef __cplusplus
204 extern "C" {
205 #endif
206  
207 #include <stdlib.h>
208 #include <stdint.h>
209 #include <string.h>
210 #include <assert.h>
211  
212 #define tp_function_unused __attribute__((unused))
213 #define tp_packed __attribute__((packed))
214 #define tp_inline __attribute__((forceinline))
215 #define tp_noinline __attribute__((noinline))
216 #if defined(__GNUC__)
217 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
218 #define tp_hot __attribute__((hot))
219 #endif
220 #endif
221 #if !defined(tp_hot)
222 #define tp_hot
223 #endif
224  
225 #define tp_likely(expr) __builtin_expect(!! (expr), 1)
226 #define tp_unlikely(expr) __builtin_expect(!! (expr), 0)
227  
228 struct tp;
229  
230 /* Reallocation function, can be customized for own use */
231 typedef char *(*tp_reserve)(struct tp *p, size_t req, size_t *size);
232  
233 /* request types. */
234 #define TP_PING 65280
235 #define TP_INSERT 13
236 #define TP_SELECT 17
237 #define TP_UPDATE 19
238 #define TP_DELETE 21
239 #define TP_CALL 22
240  
241 /* requests flags */
242 #define TP_BOX_RETURN_TUPLE 1
243 #define TP_BOX_ADD 2
244 #define TP_BOX_REPLACE 4
245  
246 /* update operation codes */
247 #define TP_OPSET 0
248 #define TP_OPADD 1
249 #define TP_OPAND 2
250 #define TP_OPXOR 3
251 #define TP_OPOR 4
252 #define TP_OPSPLICE 5
253 #define TP_OPDELETE 6
254 #define TP_OPINSERT 7
255  
256 /* internal protocol headers */
257 struct tp_h {
258 uint32_t type, len, reqid;
259 } tp_packed;
260  
261 struct tp_hinsert {
262 uint32_t space, flags;
263 } tp_packed;
264  
265 struct tp_hdelete {
266 uint32_t space, flags;
267 } tp_packed;
268  
269 struct tp_hupdate {
270 uint32_t space, flags;
271 } tp_packed;
272  
273 struct tp_hcall {
274 uint32_t flags;
275 } tp_packed;
276  
277 struct tp_hselect {
278 uint32_t space, index;
279 uint32_t offset;
280 uint32_t limit;
281 uint32_t keyc;
282 } tp_packed;
283  
284 /*
285 * Main tp object - points either to a request buffer, or to
286 * a response.
287 *
288 * All fields except tp->p should not be accessed directly.
289 * Appropriate accessors should be used instead.
290 */
291 struct tp {
292 struct tp_h *h; /* current request header */
293 char *s, *p, *e; /* start, pos, end */
294 char *t, *f, *u; /* tuple, tuple field, update operation */
295 char *c; /* reply parsing position */
296 uint32_t tsz, fsz, tc; /* tuple size, field size, tuple count */
297 uint32_t code; /* reply server code */
298 uint32_t cnt; /* reply tuple count */
299 tp_reserve reserve; /* realloc function pointer */
300 void *obj; /* reallocation context pointer */
301 };
302  
303 /* Get the size of the allocated buffer */
304 static inline size_t
305 188 tp_size(struct tp *p) {
306 188 return p->e - p->s;
307 }
308  
309 /* Get the size of data in the buffer */
310 static inline size_t
311 46 tp_used(struct tp *p) {
312 46 return p->p - p->s;
313 }
314  
315 /* Get the size available for write */
316 static inline size_t
317 39 tp_unused(struct tp *p) {
318 39 return p->e - p->p;
319 }
320  
321 /* A common reallocation function, can be used
322 * for 'reserve' param in tp_init().
323 * Resizes the buffer twice the previous size using realloc().
324 *
325 * struct tp req;
326 * tp_init(&req, NULL, tp_realloc, NULL);
327 * tp_ping(&req); // will call the reallocator
328 *
329 * data must be manually freed when the buffer is no longer
330 * needed.
331 * (eg. free(p->s));
332 * if realloc will return NULL, then you must destroy previous memory.
333 * (eg.
334 * if (tp_realloc(p, ..) == NULL) {
335 * free(p->s)
336 * return NULL;
337 * }
338 */
339 tp_function_unused static char*
340 0 tp_realloc(struct tp *p, size_t required, size_t *size) {
341 0 size_t toalloc = tp_size(p) * 2;
342 0 ▶ if (tp_unlikely(toalloc < required))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(toalloc < required)) 0 0 0
343 0 toalloc = tp_size(p) + required;
344 0 *size = toalloc;
345 0 return realloc(p->s, toalloc);
346 }
347  
348  
349 /* Free function for use in a pair with tp_realloc */
350 static inline void
351 tp_free(struct tp *p) {
352 free(p->s);
353 }
354  
355 /* Get currently allocated buffer pointer */
356 static inline char*
357 tp_buf(struct tp *p) {
358 return p->s;
359 }
360  
361 /* Main initialization function.
362 *
363 * reserve - reallocation function, may be NULL
364 * obj - pointer to be passed to the reallocation function as
365 * context
366 * buf - current buffer, may be NULL
367 * size - current buffer size
368 *
369 * Either a buffer pointer or a reserve function must be
370 * provided.
371 */
372 static inline void
373 158 tp_init(struct tp *p, char *buf, size_t size,
374 tp_reserve reserve, void *obj) {
375 158 p->s = buf;
376 158 p->p = p->s;
377 158 p->e = p->s + size;
378 158 p->t = NULL;
379 158 p->f = NULL;
380 158 p->u = NULL;
381 158 p->c = NULL;
382 158 p->h = NULL;
383 158 p->tsz = 0;
384 158 p->fsz = 0;
385 158 p->cnt = 0;
386 158 p->code = 0;
387 158 p->reserve = reserve;
388 158 p->obj = obj;
389 158 }
390  
391 /* Ensure that buffer has enough space to fill size bytes, resize
392 * buffer if needed. */
393 static tp_noinline ssize_t
394 39 tp_ensure(struct tp *p, size_t size) {
395 39 ▶ if (tp_likely(tp_unused(p) >= size))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_likely(tp_unused(p) >= size)) 0 39 39
396 0 return 0;
397 39 ▶ if (tp_unlikely(p->reserve == NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(p->reserve == NULL)) 0 39 39
398 0 return -1;
399 size_t sz;
400 39 register char *np = p->reserve(p, size, &sz);
401 39 ▶ if (tp_unlikely(np == NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(np == NULL)) 0 39 39
402 0 return -1;
403 39 p->p = np + (p->p - p->s);
404 39 ▶ if (tp_likely(p->h))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_likely(p->h)) 37 2 39
405 37 p->h = (struct tp_h*)(np + (((char*)p->h) - p->s));
406 39 ▶ if (tp_likely(p->t))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_likely(p->t)) 23 16 39
407 23 p->t = np + (p->t - p->s);
408 39 ▶ if (tp_unlikely(p->f))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(p->f)) 0 39 39
409 0 p->f = (np + (p->f - p->s));
410 39 ▶ if (tp_unlikely(p->u))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(p->u)) 8 31 39
411 8 p->u = (np + (p->u - p->s));
412 39 p->s = np;
413 39 p->e = np + sz;
414 39 return sz;
415 }
416  
417 /* Mark size bytes as used.
418 * Can be used to tell the buffer that a chunk has been read
419 * from the network into it.
420 */
421 static inline ssize_t
422 7 tp_use(struct tp *p, size_t size) {
423 7 p->p += size;
424 7 return tp_used(p);
425 }
426  
427 /* Append data to the buffer.
428 * Mostly unnecessary, but can be used to add any raw
429 * iproto-format data to the buffer.
430 * Normally tp_tuple(), tp_field() and tp_sz() should be used
431 * instead.
432 */
433 static inline ssize_t
434 7 tp_append(struct tp *p, const void *data, size_t size) {
435 7 ▶ if (tp_unlikely(tp_ensure(p, size) == -1))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(tp_ensure(p, size) == -1)) 0 7 7
436 0 return -1;
437 7 memcpy(p->p, data, size);
438 7 return tp_use(p, size);
439 }
440  
441 /* Set the current request id.
442 *
443 * tp_ping(&req);
444 * tp_reqid(&req, 777);
445 */
446 static inline void
447 8 tp_reqid(struct tp *p, uint32_t reqid) {
448 assert(p->h != NULL);
449 8 p->h->reqid = reqid;
450 8 }
451  
452 /* Return the current request id */
453 static inline uint32_t
454 116 tp_getreqid(struct tp *p) {
455 assert(p->h != NULL);
456 116 return p->h->reqid;
457 }
458  
459 /* Get tuple count */
460 static inline uint32_t
461 tp_tuplecount(struct tp *p) {
462 assert(p->t != NULL);
463 return *(uint32_t*)(p->t);
464 }
465  
466 /* Write a tuple header */
467 static inline ssize_t
468 8 tp_tuple(struct tp *p) {
469 assert(p->h != NULL);
470 8 ▶ if (tp_unlikely(tp_ensure(p, sizeof(uint32_t)) == -1))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(tp_ensure(p, sizeof(uint32_t)) == -1)) 0 8 8
471 0 return -1;
472 8 *(uint32_t*)(p->t = p->p) = 0;
473 8 p->p += sizeof(uint32_t);
474 8 p->h->len += sizeof(uint32_t);
475 8 ▶ if (p->h->type == TP_SELECT) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->h->type == TP_SELECT) { 2 6 8
476 2 ((struct tp_hselect*)
477 2 ((char*)p->h + sizeof(struct tp_h)))->keyc++;
478 }
479 8 return tp_used(p);
480 }
481  
482 /* Leb128 calculation functions, internally used by the library */
483 static inline size_t
484 25 tp_leb128sizeof(uint32_t value) {
485 25 ▶ return ( tp_likely(value < (1 << 7))) ? 1 :
Branch: 1 branch
BranchTrueFalseTotal
return ( tp_likely(value < (1 << 7))) ? 1 : 0 25 25
486 0 ▶ ( tp_likely(value < (1 << 14))) ? 2 :
Branch: 1 branch
BranchTrueFalseTotal
( tp_likely(value < (1 << 14))) ? 2 : 0 0 0
487 0 ▶ (tp_unlikely(value < (1 << 21))) ? 3 :
Branch: 1 branch
BranchTrueFalseTotal
(tp_unlikely(value < (1 << 21))) ? 3 : 0 0 0
488 0 ▶ (tp_unlikely(value < (1 << 28))) ? 4 : 5;
Branch: 1 branch
BranchTrueFalseTotal
(tp_unlikely(value < (1 << 28))) ? 4 : 5; 0 0 0
489 }
490  
491 static tp_noinline void tp_hot
492 0 tp_leb128save_slowpath(struct tp *p, uint32_t value) {
493 0 ▶ if (tp_unlikely(value >= (1 << 21))) {
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(value >= (1 << 21))) { 0 0 0
494 0 ▶ if (tp_unlikely(value >= (1 << 28)))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(value >= (1 << 28))) 0 0 0
495 0 *(p->p++) = (value >> 28) | 0x80;
496 0 *(p->p++) = (value >> 21) | 0x80;
497 }
498 0 p->p[0] = ((value >> 14) | 0x80);
499 0 p->p[1] = ((value >> 7) | 0x80);
500 0 p->p[2] = value & 0x7F;
501 0 p->p += 3;
502 0 }
503  
504 static inline void tp_hot
505 25 tp_leb128save(struct tp *p, uint32_t value) {
506 25 ▶ if (tp_unlikely(value >= (1 << 14))) {
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(value >= (1 << 14))) { 0 25 25
507 0 tp_leb128save_slowpath(p, value);
508 0 return;
509 }
510 25 ▶ if (tp_likely(value >= (1 << 7)))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_likely(value >= (1 << 7))) 0 25 25
511 0 *(p->p++) = ((value >> 7) | 0x80);
512 25 *(p->p++) = ((value) & 0x7F);
513 }
514  
515 static tp_noinline int tp_hot
516 0 tp_leb128load_slowpath(struct tp *p, uint32_t *value) {
517 0 ▶ if (tp_likely(! (p->f[2] & 0x80))) {
Branch: 1 branch
BranchTrueFalseTotal
if (tp_likely(! (p->f[2] & 0x80))) { 0 0 0
518 0 *value = (p->f[0] & 0x7f) << 14 |
519 0 (p->f[1] & 0x7f) << 7 |
520 0 (p->f[2] & 0x7f);
521 0 p->f += 3;
522 } else
523 0 ▶ if (! (p->f[3] & 0x80)) {
Branch: 1 branch
BranchTrueFalseTotal
if (! (p->f[3] & 0x80)) { 0 0 0
524 0 *value = (p->f[0] & 0x7f) << 21 |
525 0 (p->f[1] & 0x7f) << 14 |
526 0 (p->f[2] & 0x7f) << 7 |
527 0 (p->f[3] & 0x7f);
528 0 p->f += 4;
529 } else
530 0 ▶ if (! (p->f[4] & 0x80)) {
Branch: 1 branch
BranchTrueFalseTotal
if (! (p->f[4] & 0x80)) { 0 0 0
531 0 *value = (p->f[0] & 0x7f) << 28 |
532 0 (p->f[1] & 0x7f) << 21 |
533 0 (p->f[2] & 0x7f) << 14 |
534 0 (p->f[3] & 0x7f) << 7 |
535 0 (p->f[4] & 0x7f);
536 0 p->f += 5;
537 } else
538 0 return -1;
539 0 return 0;
540 }
541  
542 static inline int tp_hot
543 18 tp_leb128load(struct tp *p, uint32_t *value) {
544 18 ▶ if (tp_likely(! (p->f[0] & 0x80))) {
Branch: 1 branch
BranchTrueFalseTotal
if (tp_likely(! (p->f[0] & 0x80))) { 18 0 18
545 18 *value = *(p->f++) & 0x7f;
546 } else
547 0 ▶ if (tp_likely(! (p->f[1] & 0x80))) {
Branch: 1 branch
BranchTrueFalseTotal
if (tp_likely(! (p->f[1] & 0x80))) { 0 0 0
548 0 *value = (p->f[0] & 0x7f) << 7 | (p->f[1] & 0x7f);
549 0 p->f += 2;
550 } else
551 0 return tp_leb128load_slowpath(p, value);
552 18 return 0;
553 }
554  
555 /* Write a tuple field
556 * Note: the tuple must be started prior to calling
557 * this function with tp_tuple() call.
558 */
559 static inline ssize_t
560 12 tp_field(struct tp *p, const char *data, size_t size) {
561 assert(p->h != NULL);
562 assert(p->t != NULL);
563 12 register int esz = tp_leb128sizeof(size);
564 12 ▶ if (tp_unlikely(tp_ensure(p, esz + size) == -1))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(tp_ensure(p, esz + size) == -1)) 0 12 12
565 0 return -1;
566 12 tp_leb128save(p, size);
567 12 memcpy(p->p, data, size);
568 12 p->p += size;
569 12 (*(uint32_t*)p->t)++;
570 12 p->h->len += esz + size;
571 12 return tp_used(p);
572 }
573  
574 /* Set the current request.
575 * Note: this is an internal helper function, not part of the
576 * tp.h API.
577 */
578 static inline void
579 9 tp_setreq(struct tp *p) {
580 9 p->h = (struct tp_h*)p->p;
581 9 p->t = NULL;
582 9 p->u = NULL;
583 9 }
584  
585 /* Set current request and append data to the buffer.
586 * Note: this is an internal helper function, not part of the
587 * tp.h API. tp_ping(), tp_update() and other functions
588 * which directly create a request header should be used
589 * instead.
590 */
591 static inline ssize_t
592 7 tp_appendreq(struct tp *p, void *h, size_t size) {
593 7 int isallocated = p->p != NULL;
594 7 tp_setreq(p);
595 7 ssize_t rc = tp_append(p, h, size);
596 7 ▶ if (tp_unlikely(rc == -1))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(rc == -1)) 0 7 7
597 0 return -1;
598 7 ▶ if (!isallocated)
Branch: 1 branch
BranchTrueFalseTotal
if (!isallocated) 0 7 7
599 0 p->h = (struct tp_h*)p->s;
600 7 return rc;
601 }
602  
603 /* Create a ping request.
604 *
605 * char buf[64];
606 * struct tp req;
607 * tp_init(&req, buf, sizeof(buf), NULL, NULL);
608 * tp_ping(&req);
609 */
610 static inline ssize_t
611 1 tp_ping(struct tp *p) {
612 1 struct tp_h h = { TP_PING, 0, 0 };
613 1 return tp_appendreq(p, &h, sizeof(h));
614 }
615  
616 /* Create an insert request.
617 *
618 * char buf[64];
619 * struct tp req;
620 * tp_init(&req, buf, sizeof(buf), NULL, NULL);
621 * tp_insert(&req, 0, TP_FRET);
622 * tp_tuple(&req);
623 * tp_sz(&req, "key");
624 * tp_sz(&req, "value");
625 */
626 static inline ssize_t
627 1 tp_insert(struct tp *p, uint32_t space, uint32_t flags) {
628 struct {
629 struct tp_h h;
630 struct tp_hinsert i;
631 } h;
632 1 h.h.type = TP_INSERT;
633 1 h.h.len = sizeof(struct tp_hinsert);
634 1 h.h.reqid = 0;
635 1 h.i.space = space;
636 1 h.i.flags = flags;
637 1 return tp_appendreq(p, &h, sizeof(h));
638 }
639  
640 /* Create a delete request.
641 *
642 * char buf[64];
643 * struct tp req;
644 * tp_init(&req, buf, sizeof(buf), NULL, NULL);
645 * tp_delete(&req, 0, 0);
646 * tp_tuple(&req);
647 * tp_sz(&req, "key");
648 */
649 static inline ssize_t
650 1 tp_delete(struct tp *p, uint32_t space, uint32_t flags) {
651 struct {
652 struct tp_h h;
653 struct tp_hdelete d;
654 } h;
655 1 h.h.type = TP_DELETE;
656 1 h.h.len = sizeof(struct tp_hdelete);
657 1 h.h.reqid = 0;
658 1 h.d.space = space;
659 1 h.d.flags = flags;
660 1 return tp_appendreq(p, &h, sizeof(h));
661 }
662  
663 /* Create a call request.
664 *
665 * char buf[64];
666 * struct tp req;
667 * tp_init(&req, buf, sizeof(buf), NULL, NULL);
668 *
669 * char proc[] = "hello_proc";
670 * tp_call(&req, 0, proc, sizeof(proc) - 1);
671 * tp_tuple(&req);
672 * tp_sz(&req, "arg1");
673 * tp_sz(&req, "arg2");
674 */
675 static inline ssize_t
676 2 tp_call(struct tp *p, uint32_t flags, const char *name, size_t name_len) {
677 struct {
678 struct tp_h h;
679 struct tp_hcall c;
680 } h;
681 2 size_t sz = tp_leb128sizeof(name_len);
682 2 h.h.type = TP_CALL;
683 2 h.h.len = sizeof(struct tp_hcall) + sz + name_len;
684 2 h.h.reqid = 0;
685 2 h.c.flags = flags;
686 2 ▶ if (tp_unlikely(tp_ensure(p, sizeof(h) + sz + name_len) == -1))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(tp_ensure(p, sizeof(h) + sz + name_len) == -1)) 0 2 2
687 0 return -1;
688 2 tp_setreq(p);
689 2 memcpy(p->p, &h, sizeof(h));
690 2 p->p += sizeof(h);
691 2 tp_leb128save(p, name_len);
692 2 memcpy(p->p, name, name_len);
693 2 p->p += name_len;
694 2 return tp_used(p);
695 }
696  
697 /* Append a select request.
698 *
699 * char buf[64];
700 * struct tp req;
701 * tp_init(&req, buf, sizeof(buf), NULL, NULL);
702 * tp_select(&req, 0, 0, 0, 100);
703 * tp_tuple(&req);
704 * tp_sz(&req, "key");
705 */
706 static inline ssize_t
707 2 tp_select(struct tp *p, uint32_t space, uint32_t index,
708 uint32_t offset, uint32_t limit) {
709 struct {
710 struct tp_h h;
711 struct tp_hselect s;
712 } h;
713 2 h.h.type = TP_SELECT;
714 2 h.h.len = sizeof(struct tp_hselect);
715 2 h.h.reqid = 0;
716 2 h.s.space = space;
717 2 h.s.index = index;
718 2 h.s.offset = offset;
719 2 h.s.limit = limit;
720 2 h.s.keyc = 0;
721 2 return tp_appendreq(p, &h, sizeof(h));
722 }
723  
724 /* Create an update request.
725 *
726 * char buf[64];
727 * struct tp req;
728 * tp_init(&req, buf, sizeof(buf), NULL, NULL);
729 * tp_update(&req, 0, 0);
730 * tp_tuple(&req);
731 * tp_sz(&req, "key");
732 * tp_updatebegin(&req);
733 * tp_op(&req, 1, TP_OPSET, "VALUE", 5);
734 */
735 static inline ssize_t
736 2 tp_update(struct tp *p, uint32_t space, uint32_t flags) {
737 struct {
738 struct tp_h h;
739 struct tp_hupdate u;
740 } h;
741 2 h.h.type = TP_UPDATE;
742 2 h.h.len = sizeof(struct tp_hupdate);
743 2 h.h.reqid = 0;
744 2 h.u.space = space;
745 2 h.u.flags = flags;
746 2 return tp_appendreq(p, &h, sizeof(h));
747 }
748  
749 /* Append the number of operations the update request
750 * is going to contain.
751 * Must be called right after appending the key which
752 * identifies the tuple which must be updated. Since
753 * the key can be multipart, tp_tuple() must be used to
754 * append it.
755 *
756 * In other words, this call sequence creates a proper
757 * UPDATE request:
758 * tp_init(...)
759 * tp_update()
760 * tp_tuple()
761 * tp_sz(), tp_sz(), ...
762 * tp_updatebegin()
763 * tp_op(), tp_op(), ...
764 */
765 static inline ssize_t
766 2 tp_updatebegin(struct tp *p) {
767 assert(p->h != NULL);
768 assert(p->h->type == TP_UPDATE);
769 2 ▶ if (tp_unlikely(tp_ensure(p, sizeof(uint32_t)) == -1))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(tp_ensure(p, sizeof(uint32_t)) == -1)) 0 2 2
770 0 return -1;
771 2 *(uint32_t*)(p->u = p->p) = 0;
772 2 p->p += sizeof(uint32_t);
773 2 p->h->len += sizeof(uint32_t);
774 2 return tp_used(p);
775 }
776  
777 /* Append a single UPDATE operation.
778 *
779 * May be called after tp_updatebegin().
780 * Can be used to create TP_OPSET, TP_OPADD, TP_OPAND,
781 * TP_OPXOR, TP_OPOR operations.
782 *
783 * See: tp_update() for example.
784 */
785 static inline ssize_t
786 8 tp_op(struct tp *p, uint32_t field, uint8_t op, const char *data,
787 size_t size) {
788 assert(p->h != NULL);
789 assert(p->u != NULL);
790 assert(p->h->type == TP_UPDATE);
791 8 size_t sz = 4 + 1 + tp_leb128sizeof(size) + size;
792 8 ▶ if (tp_unlikely(tp_ensure(p, sz)) == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(tp_ensure(p, sz)) == -1) 0 8 8
793 0 return -1;
794 /* field */
795 8 *(uint32_t*)(p->p) = field;
796 8 p->p += sizeof(uint32_t);
797 /* operation */
798 8 *(uint8_t*)(p->p) = op;
799 8 p->p += sizeof(uint8_t);
800 /* data */
801 8 tp_leb128save(p, size);
802 8 ▶ if (tp_likely(data))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_likely(data)) 7 1 8
803 7 memcpy(p->p, data, size);
804 8 p->p += size;
805 /* update offset and count */
806 8 p->h->len += sz;
807 8 (*(uint32_t*)p->u)++;
808 8 return tp_used(p);
809 }
810  
811 /* Append a SPLICE operation. This operation is unlike any other,
812 * since it takes three arguments instead of one.
813 */
814 static inline ssize_t
815 1 tp_opsplice(struct tp *p, uint32_t field, uint32_t offset,
816 uint32_t cut, const char *paste, size_t paste_len) {
817 1 uint32_t olen = tp_leb128sizeof(sizeof(offset)),
818 1 clen = tp_leb128sizeof(sizeof(cut)),
819 1 plen = tp_leb128sizeof(paste_len);
820 1 uint32_t sz = olen + sizeof(offset) + clen + sizeof(cut) +
821 1 plen + paste_len;
822 1 ssize_t rc = tp_op(p, field, TP_OPSPLICE, NULL, sz);
823 1 ▶ if (tp_unlikely(rc == -1))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(rc == -1)) 0 1 1
824 0 return -1;
825 1 p->p -= sz;
826 1 tp_leb128save(p, sizeof(offset));
827 1 memcpy(p->p, &offset, sizeof(offset));
828 1 p->p += sizeof(offset);
829 1 tp_leb128save(p, sizeof(cut));
830 1 memcpy(p->p, &cut, sizeof(cut));
831 1 p->p += sizeof(cut);
832 1 tp_leb128save(p, paste_len);
833 1 memcpy(p->p, paste, paste_len);
834 1 p->p += paste_len;
835 1 return rc;
836 }
837  
838 /* Append a '\0' terminated string as a tuple field. */
839 static inline ssize_t
840 tp_sz(struct tp *p, const char *sz) {
841 return tp_field(p, sz, strlen(sz));
842 }
843  
844 /*
845 * Returns the number of bytes which are required to fully
846 * store a reply in the buffer.
847 * The return value can be negative, which indicates that
848 * there is a complete reply in the buffer which is not parsed
849 * and discarded yet.
850 */
851 static inline ssize_t
852 149 tp_reqbuf(const char *buf, size_t size) {
853 149 ▶ if (tp_unlikely(size < sizeof(struct tp_h)))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(size < sizeof(struct tp_h))) 1 148 149
854 1 return sizeof(struct tp_h) - size;
855 148 register int sz =
856 148 ((struct tp_h*)buf)->len + sizeof(struct tp_h);
857 return (tp_likely(size < sz)) ?
858 148 sz - size : -(size - sz);
859 }
860  
861 /* Same as tp_reqbuf(), but works on the buffer in struct tp.
862 */
863 static inline ssize_t
864 149 tp_req(struct tp *p) {
865 149 return tp_reqbuf(p->s, tp_size(p));
866 }
867  
868 /* Get the size of a yet unprocessed reply data.
869 *
870 * This is not part of the API.
871 */
872 static inline size_t
873 542 tp_unfetched(struct tp *p) {
874 542 return p->p - p->c;
875 }
876  
877 /* Advance the reply processed pointer.
878 *
879 * This is not part of the API, tp_use() is a higher level
880 * function.
881 */
882 static inline void*
883 330 tp_fetch(struct tp *p, int inc) {
884 assert(tp_unfetched(p) >= inc);
885 330 register char *po = p->c;
886 330 p->c += inc;
887 330 return po;
888 }
889  
890 static inline int
891 330 tp_can_fetch(struct tp *p, int inc) {
892 330 ▶ if (tp_unfetched(p) >= inc)
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unfetched(p) >= inc) 330 0 330
893 330 return 1;
894 0 return 0;
895 }
896  
897  
898 /* Get the last server error.
899 */
900 static inline char*
901 12 tp_replyerror(struct tp *p) {
902 12 return p->c;
903 }
904  
905 /* Get the length of the last error message.
906 */
907 static inline int
908 12 tp_replyerrorlen(struct tp *p) {
909 12 return tp_unfetched(p);
910 }
911  
912 /* Get the tuple count in the response (there must be
913 * no error).
914 */
915 static inline uint32_t
916 116 tp_replycount(struct tp *p) {
917 116 return p->cnt;
918 }
919  
920 /* Get the current response return code.
921 */
922 static inline uint32_t
923 116 tp_replycode(struct tp *p) {
924 116 return p->code;
925 }
926  
927 /* Get the current response operation code. */
928 static inline uint32_t
929 216 tp_replyop(struct tp *p) {
930 216 return p->h->type;
931 }
932  
933 /*
934 * Initialize the buffer with a fully read server response.
935 * The response is parsed.
936 *
937 * struct tp rep;
938 * tp_init(&rep, reply_buf, reply_size, NULL, NULL);
939 *
940 * ssize_t server_code = tp_reply(&rep);
941 *
942 * printf("op: %d\n", tp_replyop(&rep));
943 * printf("count: %d\n", tp_replycount(&rep));
944 * printf("code: %zu\n", server_code);
945 *
946 * if (server_code != 0) {
947 * printf("error: %-.*s\n", tp_replyerrorlen(&rep),
948 * tp_replyerror(&rep));
949 * }
950 *
951 */
952 tp_function_unused static ssize_t
953 149 tp_reply(struct tp *p) {
954 149 ssize_t used = tp_req(p);
955 149 ▶ if (tp_unlikely(used > 0))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(used > 0)) 31 118 149
956 31 return -1;
957 /* this is end of packet in continious buffer */
958 118 p->p = p->e + used; /* end - used */
959 118 p->c = p->s;
960 118 ▶ if (!tp_can_fetch(p, sizeof(struct tp_h)))
Branch: 1 branch
BranchTrueFalseTotal
if (!tp_can_fetch(p, sizeof(struct tp_h))) 0 118 118
961 0 return -1;
962 118 p->h = tp_fetch(p, sizeof(struct tp_h));
963 118 p->t = p->f = p->u = NULL;
964 118 p->cnt = 0;
965 118 p->code = 0;
966 118 ▶ if (tp_unlikely(p->h->type == TP_PING))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(p->h->type == TP_PING)) 4 114 118
967 4 return 0;
968 114 ▶ if (tp_unlikely(p->h->type != TP_UPDATE &&
Branch: 8 branches
BranchTrueFalseTotal
if (tp_unlikely(p->h->type != TP_UPDATE && 110 4 114
if (tp_unlikely(p->h->type != TP_UPDATE && 104 6 110
if (tp_unlikely(p->h->type != TP_UPDATE && 104 10 114
if (tp_unlikely(p->h->type != TP_UPDATE && 102 2 104
if (tp_unlikely(p->h->type != TP_UPDATE && 102 12 114
if (tp_unlikely(p->h->type != TP_UPDATE && 8 94 102
if (tp_unlikely(p->h->type != TP_UPDATE && 8 106 114
if (tp_unlikely(p->h->type != TP_UPDATE && 2 6 8
969 p->h->type != TP_INSERT &&
970 p->h->type != TP_DELETE &&
971 p->h->type != TP_SELECT &&
972 p->h->type != TP_CALL))
973 2 return -1;
974 112 ▶ if (!tp_can_fetch(p, sizeof(uint32_t)))
Branch: 1 branch
BranchTrueFalseTotal
if (!tp_can_fetch(p, sizeof(uint32_t))) 0 112 112
975 0 return -1;
976 112 p->code = *(uint32_t*)tp_fetch(p, sizeof(uint32_t));
977 112 ▶ if (p->code != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p->code != 0) 12 100 112
978 12 return p->code;
979 /* BOX_QUIET */
980 100 ▶ if (tp_unlikely(tp_unfetched(p) == 0))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(tp_unfetched(p) == 0)) 0 100 100
981 0 return p->code;
982 100 ▶ if (!tp_can_fetch(p, sizeof(uint32_t)))
Branch: 1 branch
BranchTrueFalseTotal
if (!tp_can_fetch(p, sizeof(uint32_t))) 0 100 100
983 0 return -1;
984 100 p->cnt = *(uint32_t*)tp_fetch(p, sizeof(uint32_t));
985 100 return p->code;
986 }
987  
988 /* Example: iteration over returned tuples.
989 *
990 * while (tp_next(&rep)) {
991 * printf("tuple fields: %d\n", tp_tuplecount(&rep));
992 * printf("tuple size: %d\n", tp_tuplesize(&rep));
993 * printf("[");
994 * while (tp_nextfield(&rep)) {
995 * printf("%-.*s", tp_getfieldsize(rep), tp_getfield(&rep));
996 * if (tp_hasnextfield(&rep))
997 * printf(", ");
998 * }
999 * printf("]\n");
1000 * }
1001 */
1002  
1003 /* Rewind iteration to the first tuple. */
1004 static inline void
1005 tp_rewind(struct tp *p) {
1006 p->t = NULL;
1007 p->f = NULL;
1008 }
1009  
1010 /* Rewind iteration to the first tuple field of the current tuple. */
1011 static inline void
1012 tp_rewindfield(struct tp *p) {
1013 p->f = NULL;
1014 }
1015  
1016 /* Get the current tuple data, all fields. */
1017 static inline char*
1018 tp_gettuple(struct tp *p) {
1019 return p->t;
1020 }
1021  
1022 /* Get the current tuple size. */
1023 static inline uint32_t
1024 tp_tuplesize(struct tp *p) {
1025 return p->tsz;
1026 }
1027  
1028 /* Get the current field. */
1029 static inline char*
1030 18 tp_getfield(struct tp *p) {
1031 18 return p->f;
1032 }
1033  
1034 /* Get the current field size. */
1035 static inline uint32_t
1036 18 tp_getfieldsize(struct tp *p) {
1037 18 return p->fsz;
1038 }
1039  
1040 /* Get a pointer to the end of the current tuple. */
1041 static inline char*
1042 34 tp_tupleend(struct tp *p) {
1043 /* tuple_size + p->t + cardinaltiy_size +
1044 * fields_size */
1045 34 return p->t + 4 + p->tsz;
1046 }
1047  
1048 /* Check if the response has a tuple.
1049 * Automatically checked during tp_next() iteration. */
1050 static inline int
1051 100 tp_hasdata(struct tp *p) {
1052 100 ▶ return tp_replyop(p) != TP_PING && tp_unfetched(p) > 0;
Branch: 2 branches
BranchTrueFalseTotal
return tp_replyop(p) != TP_PING && tp_unfetched(p) > 0; 100 0 100
return tp_replyop(p) != TP_PING && tp_unfetched(p) > 0; 98 2 100
1053 }
1054  
1055 /* Check if there is a one more tuple. */
1056 static inline int
1057 8 tp_hasnext(struct tp *p) {
1058 assert(p->t != NULL);
1059 8 return (p->p - tp_tupleend(p)) >= 4;
1060 }
1061  
1062 /* Check if the current tuple has a one more field. */
1063 static inline int
1064 26 tp_hasnextfield(struct tp *p) {
1065 assert(p->t != NULL);
1066 26 register char *f = p->f + p->fsz;
1067 26 ▶ if (tp_unlikely(p->f == NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(p->f == NULL)) 8 18 26
1068 8 f = p->t + 4;
1069 26 return (tp_tupleend(p) - f) >= 1;
1070 }
1071  
1072 /* Skip to the next tuple.
1073 * Tuple can be accessed using:
1074 * tp_tuplecount(), tp_tuplesize(), tp_gettuple(). */
1075 static inline int
1076 108 tp_next(struct tp *p) {
1077 108 ▶ if (tp_unlikely(p->t == NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(p->t == NULL)) { 100 8 108
1078 100 ▶ if (tp_unlikely(! tp_hasdata(p)))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(! tp_hasdata(p))) 2 98 100
1079 2 return 0;
1080 98 p->t = p->c + 4;
1081 98 goto fetch;
1082 }
1083 8 ▶ if (tp_unlikely(! tp_hasnext(p)))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(! tp_hasnext(p))) 8 0 8
1084 8 return 0;
1085 0 p->t = tp_tupleend(p) + 4;
1086 98 fetch:
1087 98 p->tsz = *(uint32_t*)(p->t - 4);
1088 98 ▶ if (tp_unlikely((p->t + p->tsz) > p->e))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely((p->t + p->tsz) > p->e)) 90 8 98
1089 90 return -1;
1090 8 p->f = NULL;
1091 8 return 1;
1092 }
1093  
1094 /* Skip to the next field.
1095 * Data can be accessed using: tp_getfieldsize(), tp_getfield(). */
1096 static inline int
1097 26 tp_nextfield(struct tp *p) {
1098 assert(p->t != NULL);
1099 26 ▶ if (tp_unlikely(p->f == NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(p->f == NULL)) { 8 18 26
1100 8 ▶ if (tp_unlikely(! tp_hasnextfield(p)))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(! tp_hasnextfield(p))) 2 6 8
1101 2 return 0;
1102 6 p->f = p->t + 4;
1103 6 goto fetch;
1104 }
1105 18 ▶ if (tp_unlikely(! tp_hasnextfield(p)))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(! tp_hasnextfield(p))) 6 12 18
1106 6 return 0;
1107 12 p->f += p->fsz;
1108 18 fetch:;
1109 18 register int rc = tp_leb128load(p, &p->fsz);
1110 18 ▶ if (tp_unlikely(rc == -1))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely(rc == -1)) 0 18 18
1111 0 return -1;
1112 18 ▶ if (tp_unlikely((p->f + p->fsz) > p->e))
Branch: 1 branch
BranchTrueFalseTotal
if (tp_unlikely((p->f + p->fsz) > p->e)) 0 18 18
1113 0 return -1;
1114 18 return 1;
1115 }
1116  
1117 #ifdef __cplusplus
1118 } /* extern "C" */
1119 #endif
1120  
1121 #endif /* TP_H_INCLUDED */
« msgpuck.h ↑ Summary Tarantool.xs »