/
tuple.h
792 lines (703 loc) · 20.8 KB
/
tuple.h
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
#ifndef TARANTOOL_BOX_TUPLE_H_INCLUDED
#define TARANTOOL_BOX_TUPLE_H_INCLUDED
/*
* Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "trivia/util.h"
#include "tuple_format.h"
#if defined(__cplusplus)
extern "C" {
#endif /* defined(__cplusplus) */
/** \cond public */
typedef struct tuple_format box_tuple_format_t;
/**
* Tuple Format.
*
* Each Tuple has associated format (class). Default format is used to
* create tuples which are not attach to any particular space.
*/
box_tuple_format_t *
box_tuple_format_default(void);
/**
* Tuple
*/
typedef struct tuple box_tuple_t;
/**
* Allocate and initialize a new tuple from a raw MsgPack Array data.
*
* \param format tuple format.
* Use box_tuple_format_default() to create space-independent tuple.
* \param data tuple data in MsgPack Array format ([field1, field2, ...]).
* \param end the end of \a data
* \retval NULL on out of memory
* \retval tuple otherwise
* \pre data, end is valid MsgPack Array
* \sa \code box.tuple.new(data) \endcode
*/
box_tuple_t *
box_tuple_new(box_tuple_format_t *format, const char *data, const char *end);
/**
* Increase the reference counter of tuple.
*
* Tuples are reference counted. All functions that return tuples guarantee
* that the last returned tuple is refcounted internally until the next
* call to API function that yields or returns another tuple.
*
* You should increase the reference counter before taking tuples for long
* processing in your code. Such tuples will not be garbage collected even
* if another fiber remove they from space. After processing please
* decrement the reference counter using box_tuple_unref(), otherwise the
* tuple will leak.
*
* \param tuple a tuple
* \retval -1 on error (check box_error_last())
* \retval 0 on success
* \sa box_tuple_unref()
*/
int
box_tuple_ref(box_tuple_t *tuple);
/**
* Decrease the reference counter of tuple.
*
* \param tuple a tuple
* \sa box_tuple_ref()
*/
void
box_tuple_unref(box_tuple_t *tuple);
/**
* Return the number of fields in tuple (the size of MsgPack Array).
* \param tuple a tuple
*/
uint32_t
box_tuple_field_count(const box_tuple_t *tuple);
/**
* Return the number of bytes used to store internal tuple data (MsgPack Array).
* \param tuple a tuple
*/
size_t
box_tuple_bsize(const box_tuple_t *tuple);
/**
* Dump raw MsgPack data to the memory byffer \a buf of size \a size.
*
* Store tuple fields in the memory buffer.
* \retval -1 on error.
* \retval number of bytes written on success.
* Upon successful return, the function returns the number of bytes written.
* If buffer size is not enough then the return value is the number of bytes
* which would have been written if enough space had been available.
*/
ssize_t
box_tuple_to_buf(const box_tuple_t *tuple, char *buf, size_t size);
/**
* Return the associated format.
* \param tuple tuple
* \return tuple_format
*/
box_tuple_format_t *
box_tuple_format(const box_tuple_t *tuple);
/**
* Return the raw tuple field in MsgPack format.
*
* The buffer is valid until next call to box_tuple_* functions.
*
* \param tuple a tuple
* \param field_id zero-based index in MsgPack array.
* \retval NULL if i >= box_tuple_field_count(tuple)
* \retval msgpack otherwise
*/
const char *
box_tuple_field(const box_tuple_t *tuple, uint32_t field_id);
/**
* Tuple iterator
*/
typedef struct tuple_iterator box_tuple_iterator_t;
/**
* Allocate and initialize a new tuple iterator. The tuple iterator
* allow to iterate over fields at root level of MsgPack array.
*
* Example:
* \code
* box_tuple_iterator *it = box_tuple_iterator(tuple);
* if (it == NULL) {
* // error handling using box_error_last()
* }
* const char *field;
* while (field = box_tuple_next(it)) {
* // process raw MsgPack data
* }
*
* // rewind iterator to first position
* box_tuple_rewind(it);
* assert(box_tuple_position(it) == 0);
*
* // rewind iterator to first position
* field = box_tuple_seek(it, 3);
* assert(box_tuple_position(it) == 4);
*
* box_iterator_free(it);
* \endcode
*
* \post box_tuple_position(it) == 0
*/
box_tuple_iterator_t *
box_tuple_iterator(box_tuple_t *tuple);
/**
* Destroy and free tuple iterator
*/
void
box_tuple_iterator_free(box_tuple_iterator_t *it);
/**
* Return zero-based next position in iterator.
* That is, this function return the field id of field that will be
* returned by the next call to box_tuple_next(it). Returned value is zero
* after initialization or rewind and box_tuple_field_count(tuple)
* after the end of iteration.
*
* \param it tuple iterator
* \returns position.
*/
uint32_t
box_tuple_position(box_tuple_iterator_t *it);
/**
* Rewind iterator to the initial position.
*
* \param it tuple iterator
* \post box_tuple_position(it) == 0
*/
void
box_tuple_rewind(box_tuple_iterator_t *it);
/**
* Seek the tuple iterator.
*
* The returned buffer is valid until next call to box_tuple_* API.
* Requested field_no returned by next call to box_tuple_next(it).
*
* \param it tuple iterator
* \param field_no field no - zero-based position in MsgPack array.
* \post box_tuple_position(it) == field_no if returned value is not NULL
* \post box_tuple_position(it) == box_tuple_field_count(tuple) if returned
* value is NULL.
*/
const char *
box_tuple_seek(box_tuple_iterator_t *it, uint32_t field_no);
/**
* Return the next tuple field from tuple iterator.
* The returned buffer is valid until next call to box_tuple_* API.
*
* \param it tuple iterator.
* \retval NULL if there are no more fields.
* \retval MsgPack otherwise
* \pre box_tuple_position(it) is zerod-based id of returned field
* \post box_tuple_position(it) == box_tuple_field_count(tuple) if returned
* value is NULL.
*/
const char *
box_tuple_next(box_tuple_iterator_t *it);
box_tuple_t *
box_tuple_update(const box_tuple_t *tuple, const char *expr, const
char *expr_end);
box_tuple_t *
box_tuple_upsert(const box_tuple_t *tuple, const char *expr, const
char *expr_end);
char *
box_tuple_extract_key(const box_tuple_t *tuple, uint32_t space_id,
uint32_t index_id, uint32_t *key_size);
/** \endcond public */
/**
* @brief Compare two tuple fields using using field type definition
* @param field_a field
* @param field_b field
* @param field_type field type definition
* @retval 0 if field_a == field_b
* @retval <0 if field_a < field_b
* @retval >0 if field_a > field_b
*/
int
tuple_compare_field(const char *field_a, const char *field_b,
enum field_type type);
#if defined(__cplusplus)
} /* extern "C" */
#include "key_def.h" /* for enum field_type */
#include "tuple_update.h"
#include "errinj.h"
enum { TUPLE_REF_MAX = UINT16_MAX };
/** Common quota for tuples and indexes */
extern struct quota memtx_quota;
/** Tuple allocator */
extern struct small_alloc memtx_alloc;
/** Tuple slab arena */
extern struct slab_arena memtx_arena;
/**
* An atom of Tarantool storage. Represents MsgPack Array.
*/
struct PACKED tuple
{
/*
* sic: the header of the tuple is used
* to store a free list pointer in smfree_delayed.
* Please don't change it without understanding
* how smfree_delayed and snapshotting COW works.
*/
/** snapshot generation version */
uint32_t version;
/** reference counter */
uint16_t refs;
/** format identifier */
uint16_t format_id;
/** length of the variable part of the tuple */
uint32_t bsize;
/**
* Fields can have variable length, and thus are packed
* into a contiguous byte array. Each field is prefixed
* with BER-packed field length.
*/
char data[0];
};
/**
* Create a new tuple from a sequence of MsgPack encoded fields.
* tuple->refs is 0.
*
* Throws an exception if tuple format is incorrect.
*
* \sa box_tuple_new()
*/
struct tuple *
tuple_new(struct tuple_format *format, const char *data, const char *end);
/**
* Allocate a tuple
* It's similar to tuple_new, but does not set tuple data and thus does not
* initialize field offsets.
*
* After tuple_alloc and filling tuple data the tuple_init_field_map must be
* called!
*
* @param size tuple->bsize
*/
struct tuple *
tuple_alloc(struct tuple_format *format, size_t size);
/**
* Fill field map of tuple by the data in it
* Used after tuple_alloc call and filling tuple data.
* Throws an error if tuple data does not match the format.
*/
void
tuple_init_field_map(struct tuple_format *format, struct tuple *tuple);
/**
* Free the tuple.
* @pre tuple->refs == 0
*/
void
tuple_delete(struct tuple *tuple);
/**
* Check tuple data correspondence to space format;
* throw proper exception if smth wrong.
*/
void
tuple_validate(struct tuple_format *format, struct tuple *tuple);
/**
* Check tuple data correspondence to space format;
* throw proper exception if smth wrong.
* data argument expected to be a proper msgpack array
*/
void
tuple_validate_raw(struct tuple_format *format, const char *data);
/**
* Throw and exception about tuple reference counter overflow.
*/
void
tuple_ref_exception();
/**
* Increment tuple reference counter.
* Throws if overflow detected.
*
* @pre tuple->refs + count >= 0
*/
inline void
tuple_ref(struct tuple *tuple)
{
if (tuple->refs + 1 > TUPLE_REF_MAX)
tuple_ref_exception();
tuple->refs++;
}
/**
* Decrement tuple reference counter. If it has reached zero, free the tuple.
*
* @pre tuple->refs + count >= 0
*/
inline void
tuple_unref(struct tuple *tuple)
{
assert(tuple->refs - 1 >= 0);
tuple->refs--;
if (tuple->refs == 0)
tuple_delete(tuple);
}
/** Make tuple references exception-friendly in absence of @finally. */
struct TupleRef {
struct tuple *tuple;
TupleRef(struct tuple *arg) :tuple(arg) { tuple_ref(tuple); }
~TupleRef() { tuple_unref(tuple); }
TupleRef(const TupleRef &) = delete;
void operator=(const TupleRef&) = delete;
};
/** Make tuple references exception-friendly in absence of @finally. */
struct TupleRefNil {
struct tuple *tuple;
TupleRefNil (struct tuple *arg) :tuple(arg)
{ if (tuple) tuple_ref(tuple); }
~TupleRefNil() { if (tuple) tuple_unref(tuple); }
TupleRefNil(const TupleRefNil&) = delete;
void operator=(const TupleRefNil&) = delete;
};
/**
* @brief Return a tuple format instance
* @param tuple tuple
* @return tuple format instance
*/
static inline struct tuple_format *
tuple_format(const struct tuple *tuple)
{
struct tuple_format *format = tuple_format_by_id(tuple->format_id);
assert(tuple_format_id(format) == tuple->format_id);
return format;
}
/**
* @brief Return the number of fields in tuple
* @param tuple
* @return the number of fields in tuple
*/
inline uint32_t
tuple_field_count(const struct tuple *tuple)
{
const char *data = tuple->data;
return mp_decode_array(&data);
}
/**
* Get a field by id from an non-indexed tuple.
* Returns a pointer to BER-length prefixed field.
*
* @returns field data if field exists or NULL
*/
inline const char *
tuple_field_raw(const char *data, uint32_t bsize, uint32_t i)
{
const char *pos = data;
uint32_t size = mp_decode_array(&pos);
if (unlikely(i >= size))
return NULL;
for (uint32_t k = 0; k < i; k++) {
mp_next(&pos);
}
(void)bsize;
assert(pos <= data + bsize);
return pos;
}
/**
* Get a field from tuple by index.
* Returns a pointer to BER-length prefixed field.
*
* @pre field < tuple->field_count.
* @returns field data if field exists or NULL
*/
inline const char *
tuple_field_old(const struct tuple_format *format,
const struct tuple *tuple, uint32_t i)
{
if (likely(i < format->field_count)) {
/* Indexed field */
if (i == 0) {
const char *pos = tuple->data;
mp_decode_array(&pos);
return pos;
}
if (format->fields[i].offset_slot != INT32_MAX) {
uint32_t *field_map = (uint32_t *) tuple;
int32_t slot = format->fields[i].offset_slot;
return tuple->data + field_map[slot];
}
}
ERROR_INJECT(ERRINJ_TUPLE_FIELD, return NULL);
return tuple_field_raw(tuple->data, tuple->bsize, i);
}
/**
* @brief Return field data of the field
* @param tuple tuple
* @param field_no field number
* @param field pointer where the start of field data will be stored,
* or NULL if field is out of range
* @param len pointer where the len of the field will be stored
*/
static inline const char *
tuple_field(const struct tuple *tuple, uint32_t i)
{
return tuple_field_old(tuple_format(tuple), tuple, i);
}
/**
* A convenience shortcut for data dictionary - get a tuple field
* as uint32_t.
*/
inline uint32_t
tuple_field_u32(struct tuple *tuple, uint32_t i)
{
const char *field = tuple_field(tuple, i);
if (field == NULL)
tnt_raise(ClientError, ER_NO_SUCH_FIELD, i);
if (mp_typeof(*field) != MP_UINT)
tnt_raise(ClientError, ER_FIELD_TYPE, i + INDEX_OFFSET,
field_type_strs[FIELD_TYPE_UNSIGNED]);
uint64_t val = mp_decode_uint(&field);
if (val > UINT32_MAX)
tnt_raise(ClientError, ER_FIELD_TYPE, i + INDEX_OFFSET,
field_type_strs[FIELD_TYPE_UNSIGNED]);
return (uint32_t) val;
}
/**
* A convenience shortcut for data dictionary - get a tuple field
* as a NUL-terminated string - returns a string of up to 256 bytes.
*/
const char *
tuple_field_cstr(struct tuple *tuple, uint32_t i);
/** Helper method for the above function. */
const char *
tuple_field_to_cstr(const char *field, uint32_t len);
/**
* @brief Tuple Interator
*/
struct tuple_iterator {
/** @cond false **/
/* State */
struct tuple *tuple;
/** Always points to the beginning of the next field. */
const char *pos;
/** @endcond **/
/** field no of the next field. */
int fieldno;
};
/**
* @brief Initialize an iterator over tuple fields
*
* A workflow example:
* @code
* struct tuple_iterator it;
* tuple_rewind(&it, tuple);
* const char *field;
* uint32_t len;
* while ((field = tuple_next(&it, &len)))
* lua_pushlstring(L, field, len);
*
* @endcode
*
* @param[out] it tuple iterator
* @param[in] tuple tuple
*/
inline void
tuple_rewind(struct tuple_iterator *it, struct tuple *tuple)
{
it->tuple = tuple;
it->pos = tuple->data;
(void) mp_decode_array(&it->pos); /* Skip array header */
it->fieldno = 0;
}
/**
* @brief Position the iterator at a given field no.
*
* @retval field if the iterator has the requested field
* @retval NULL otherwise (iteration is out of range)
*/
const char *
tuple_seek(struct tuple_iterator *it, uint32_t field_no);
/**
* @brief Iterate to the next field
* @param it tuple iterator
* @return next field or NULL if the iteration is out of range
*/
const char *
tuple_next(struct tuple_iterator *it);
/**
* A convenience shortcut for the data dictionary - get next field
* from iterator as uint32_t or raise an error if there is
* no next field.
*/
inline uint32_t
tuple_next_u32(struct tuple_iterator *it)
{
uint32_t fieldno = it->fieldno;
const char *field = tuple_next(it);
if (field == NULL)
tnt_raise(ClientError, ER_NO_SUCH_FIELD, it->fieldno);
if (mp_typeof(*field) != MP_UINT)
tnt_raise(ClientError, ER_FIELD_TYPE, fieldno + INDEX_OFFSET,
field_type_strs[FIELD_TYPE_UNSIGNED]);
uint32_t val = mp_decode_uint(&field);
if (val > UINT32_MAX)
tnt_raise(ClientError, ER_FIELD_TYPE, fieldno + INDEX_OFFSET,
field_type_strs[FIELD_TYPE_UNSIGNED]);
return (uint32_t) val;
}
/**
* A convenience shortcut for the data dictionary - get next field
* from iterator as a C string or raise an error if there is no
* next field.
*/
const char *
tuple_next_cstr(struct tuple_iterator *it);
/**
* Extract key from tuple by given key definition and return
* buffer allocated on box_txn_alloc with this key.
* @param tuple - tuple from which need to extract key
* @param key_def - definition of key that need to extract
* @param key_size - here will be size of extracted key
*/
char *
tuple_extract_key(const struct tuple *tuple, const struct key_def *key_def,
uint32_t *key_size);
/**
* Extract key from raw msgpuck by given key definition and return
* buffer allocated on box_txn_alloc with this key.
* @param data - msgpuck data from which need to extract key
* @param data_end - pointer at the end of data
* @param key_def - definition of key that need to extract
* @param key_size - here will be size of extracted key
*/
char *
tuple_extract_key_raw(const char *data, const char *data_end,
const struct key_def *key_def, uint32_t *key_size);
struct tuple *
tuple_update(struct tuple_format *new_format,
tuple_update_alloc_func f, void *alloc_ctx,
const struct tuple *old_tuple,
const char *expr, const char *expr_end, int field_base,
uint64_t *column_mask);
struct tuple *
tuple_upsert(struct tuple_format *new_format,
void *(*region_alloc)(void *, size_t), void *alloc_ctx,
const struct tuple *old_tuple,
const char *expr, const char *expr_end, int field_base);
/**
* @brief Compare two tuples using field by field using key definition
* @param tuple_a tuple
* @param tuple_b tuple
* @param key_def key definition
* @retval 0 if key_fields(tuple_a) == key_fields(tuple_b)
* @retval <0 if key_fields(tuple_a) < key_fields(tuple_b)
* @retval >0 if key_fields(tuple_a) > key_fields(tuple_b)
*/
int
tuple_compare_default(const struct tuple *tuple_a, const struct tuple *tuple_b,
const struct key_def *key_def);
/**
* @brief Compare a tuple with a key field by field using key definition
* @param tuple_a tuple
* @param key BER-encoded key
* @param part_count number of parts in \a key
* @param key_def key definition
* @retval 0 if key_fields(tuple_a) == parts(key)
* @retval <0 if key_fields(tuple_a) < parts(key)
* @retval >0 if key_fields(tuple_a) > parts(key)
*/
int
tuple_compare_with_key_default(const struct tuple *tuple_a, const char *key,
uint32_t part_count, const struct key_def *key_def);
inline int
tuple_compare_with_key(const struct tuple *tuple, const char *key,
uint32_t part_count, const struct key_def *key_def)
{
return key_def->tuple_compare_with_key(tuple, key, part_count, key_def);
}
inline int
tuple_compare(const struct tuple *tuple_a, const struct tuple *tuple_b,
const struct key_def *key_def)
{
return key_def->tuple_compare(tuple_a, tuple_b, key_def);
}
/** These functions are implemented in tuple_convert.cc. */
/* Store tuple in the output buffer in iproto format. */
int
tuple_to_obuf(struct tuple *tuple, struct obuf *buf);
/**
* \copydoc box_tuple_to_buf()
*/
ssize_t
tuple_to_buf(const struct tuple *tuple, char *buf, size_t size);
/** Initialize tuple library */
void
tuple_init(float alloc_arena_max_size, uint32_t slab_alloc_minimal,
uint32_t slab_alloc_maximal, float alloc_factor);
/** Cleanup tuple library */
void
tuple_free();
void
tuple_begin_snapshot();
void
tuple_end_snapshot();
extern struct tuple *box_tuple_last;
/**
* Convert internal `struct tuple` to public `box_tuple_t`.
* \post \a tuple ref counted until the next call.
* \post tuple_ref() doesn't fail at least once
* \sa tuple_ref
* \throw ER_TUPLE_REF_OVERFLOW
*/
static inline box_tuple_t *
tuple_bless(struct tuple *tuple)
{
assert(tuple != NULL);
/* Ensure tuple can be referenced at least once after return */
if (tuple->refs + 2 > TUPLE_REF_MAX)
tuple_ref_exception();
tuple->refs++;
/* Remove previous tuple */
if (likely(box_tuple_last != NULL))
tuple_unref(box_tuple_last); /* do not throw */
/* Remember current tuple */
box_tuple_last = tuple;
return tuple;
}
static inline void
mp_tuple_assert(const char *tuple, const char *tuple_end)
{
assert(mp_typeof(*tuple) == MP_ARRAY);
#ifndef NDEBUG
mp_next(&tuple);
#endif
assert(tuple == tuple_end);
(void) tuple;
(void) tuple_end;
}
static inline uint32_t
box_tuple_field_u32(box_tuple_t *tuple, uint32_t field_no, uint32_t deflt)
{
const char *field = box_tuple_field(tuple, field_no);
if (field != NULL && mp_typeof(*field) == MP_UINT)
return mp_decode_uint(&field);
return deflt;
}
#endif /* defined(__cplusplus) */
#endif /* TARANTOOL_BOX_TUPLE_H_INCLUDED */