-
Notifications
You must be signed in to change notification settings - Fork 377
/
tuple.c
675 lines (600 loc) · 16.8 KB
/
tuple.c
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
/*
* 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 "tuple.h"
#include "trivia/util.h"
#include "memory.h"
#include "fiber.h"
#include "tt_uuid.h"
#include "small/quota.h"
#include "small/small.h"
#include "tuple_update.h"
#include "coll_cache.h"
static struct mempool tuple_iterator_pool;
static struct small_alloc runtime_alloc;
enum {
/** Lowest allowed slab_alloc_minimal */
OBJSIZE_MIN = 16,
};
static const double ALLOC_FACTOR = 1.05;
/**
* Last tuple returned by public C API
* \sa tuple_bless()
*/
struct tuple *box_tuple_last;
/**
* A format for standalone tuples allocated on runtime arena.
* \sa tuple_new().
*/
static struct tuple_format *tuple_format_runtime;
static void
runtime_tuple_delete(struct tuple_format *format, struct tuple *tuple);
/** A virtual method table for tuple_format_runtime */
static struct tuple_format_vtab tuple_format_runtime_vtab = {
runtime_tuple_delete,
};
struct tuple *
tuple_new(struct tuple_format *format, const char *data, const char *end)
{
assert(format->vtab.destroy == tuple_format_runtime_vtab.destroy);
mp_tuple_assert(data, end);
size_t data_len = end - data;
size_t meta_size = tuple_format_meta_size(format);
size_t total = sizeof(struct tuple) + meta_size + data_len;
struct tuple *tuple = (struct tuple *) smalloc(&runtime_alloc, total);
if (tuple == NULL) {
diag_set(OutOfMemory, (unsigned) total,
"malloc", "tuple");
return NULL;
}
tuple->refs = 0;
tuple->bsize = data_len;
tuple->format_id = tuple_format_id(format);
tuple_format_ref(format);
tuple->data_offset = sizeof(struct tuple) + meta_size;
char *raw = (char *) tuple + tuple->data_offset;
uint32_t *field_map = (uint32_t *) raw;
memcpy(raw, data, data_len);
if (tuple_init_field_map(format, field_map, raw)) {
runtime_tuple_delete(format, tuple);
return NULL;
}
say_debug("%s(%zu) = %p", __func__, data_len, tuple);
return tuple;
}
static void
runtime_tuple_delete(struct tuple_format *format, struct tuple *tuple)
{
assert(format->vtab.destroy == tuple_format_runtime_vtab.destroy);
say_debug("%s(%p)", __func__, tuple);
assert(tuple->refs == 0);
size_t total = sizeof(struct tuple) + tuple_format_meta_size(format) +
tuple->bsize;
tuple_format_unref(format);
smfree(&runtime_alloc, tuple, total);
}
int
tuple_validate_raw(struct tuple_format *format, const char *tuple)
{
if (format->field_count == 0)
return 0; /* Nothing to check */
/* Check to see if the tuple has a sufficient number of fields. */
uint32_t field_count = mp_decode_array(&tuple);
if (format->exact_field_count > 0 &&
format->exact_field_count != field_count) {
diag_set(ClientError, ER_EXACT_FIELD_COUNT,
(unsigned) field_count,
(unsigned) format->exact_field_count);
return -1;
}
if (unlikely(field_count < format->min_field_count)) {
diag_set(ClientError, ER_INDEX_FIELD_COUNT,
(unsigned) field_count,
(unsigned) format->min_field_count);
return -1;
}
/* Check field types */
struct tuple_field *field = &format->fields[0];
uint32_t i = 0;
uint32_t defined_field_count = MIN(field_count, format->field_count);
for (; i < defined_field_count; ++i, ++field) {
if (key_mp_type_validate(field->type, mp_typeof(*tuple),
ER_FIELD_TYPE, i + TUPLE_INDEX_BASE,
tuple_field_is_nullable(field)))
return -1;
mp_next(&tuple);
}
return 0;
}
/**
* Incremented on every snapshot and is used to distinguish tuples
* which were created after start of a snapshot (these tuples can
* be freed right away, since they are not used for snapshot) or
* before start of a snapshot (these tuples can be freed only
* after the snapshot has finished, otherwise it'll write bad data
* to the snapshot file).
*/
const char *
tuple_seek(struct tuple_iterator *it, uint32_t fieldno)
{
const char *field = tuple_field(it->tuple, fieldno);
if (likely(field != NULL)) {
it->pos = field;
it->fieldno = fieldno;
return tuple_next(it);
} else {
it->pos = it->end;
it->fieldno = tuple_field_count(it->tuple);
return NULL;
}
}
const char *
tuple_next(struct tuple_iterator *it)
{
if (it->pos < it->end) {
const char *field = it->pos;
mp_next(&it->pos);
assert(it->pos <= it->end);
it->fieldno++;
return field;
}
return NULL;
}
/**
* Optimized version of tuple_extract_key_raw() for sequential key defs
* @copydoc tuple_extract_key_raw()
*/
static char *
tuple_extract_key_sequential_raw(const char *data, const char *data_end,
const struct key_def *key_def,
uint32_t *key_size)
{
assert(key_def_is_sequential(key_def));
const char *field_start = data;
uint32_t bsize = mp_sizeof_array(key_def->part_count);
mp_decode_array(&field_start);
const char *field_end = field_start;
for (uint32_t i = 0; i < key_def->part_count; i++)
mp_next(&field_end);
bsize += field_end - field_start;
assert(!data_end || (field_end - field_start <= data_end - data));
(void) data_end;
char *key = (char *) region_alloc(&fiber()->gc, bsize);
if (key == NULL) {
diag_set(OutOfMemory, bsize, "region",
"tuple_extract_key_raw_sequential");
return NULL;
}
char *key_buf = mp_encode_array(key, key_def->part_count);
memcpy(key_buf, field_start, field_end - field_start);
if (key_size != NULL)
*key_size = bsize;
return key;
}
/**
* Optimized version of tuple_extract_key() for sequential key defs
* @copydoc tuple_extract_key()
*/
static inline char *
tuple_extract_key_sequential(const struct tuple *tuple,
const struct key_def *key_def,
uint32_t *key_size)
{
assert(key_def_is_sequential(key_def));
const char *data = tuple_data(tuple);
return tuple_extract_key_sequential_raw(data, NULL, key_def, key_size);
}
/**
* General-purpose implementation of tuple_extract_key()
* @copydoc tuple_extract_key()
*/
static char *
tuple_extract_key_slowpath(const struct tuple *tuple,
const struct key_def *key_def, uint32_t *key_size)
{
const char *data = tuple_data(tuple);
uint32_t part_count = key_def->part_count;
uint32_t bsize = mp_sizeof_array(part_count);
const struct tuple_format *format = tuple_format(tuple);
const uint32_t *field_map = tuple_field_map(tuple);
/* Calculate the key size. */
for (uint32_t i = 0; i < part_count; ++i) {
const char *field =
tuple_field_raw(format, data, field_map,
key_def->parts[i].fieldno);
const char *end = field;
/*
* Skip sequential part in order to minimize
* tuple_field_raw() calls.
*/
for (; i < key_def->part_count - 1; i++) {
if (key_def->parts[i].fieldno + 1 !=
key_def->parts[i + 1].fieldno) {
/* End of sequential part */
break;
}
mp_next(&end);
}
mp_next(&end);
bsize += end - field;
}
char *key = (char *) region_alloc(&fiber()->gc, bsize);
if (key == NULL) {
diag_set(OutOfMemory, bsize, "region", "tuple_extract_key");
return NULL;
}
char *key_buf = mp_encode_array(key, part_count);
for (uint32_t i = 0; i < part_count; ++i) {
const char *field =
tuple_field_raw(format, data, field_map,
key_def->parts[i].fieldno);
const char *end = field;
/*
* Skip sequential part in order to minimize
* tuple_field_raw() calls
*/
for (; i < key_def->part_count - 1; i++) {
if (key_def->parts[i].fieldno + 1 !=
key_def->parts[i + 1].fieldno) {
/* End of sequential part */
break;
}
mp_next(&end);
}
mp_next(&end);
bsize = end - field;
memcpy(key_buf, field, bsize);
key_buf += bsize;
}
if (key_size != NULL)
*key_size = key_buf - key;
return key;
}
/**
* General-purpose version of tuple_extract_key_raw()
* @copydoc tuple_extract_key_raw()
*/
static char *
tuple_extract_key_slowpath_raw(const char *data, const char *data_end,
const struct key_def *key_def,
uint32_t *key_size)
{
/* allocate buffer with maximal possible size */
char *key = (char *) region_alloc(&fiber()->gc, data_end - data);
if (key == NULL) {
diag_set(OutOfMemory, data_end - data, "region",
"tuple_extract_key_raw");
return NULL;
}
char *key_buf = mp_encode_array(key, key_def->part_count);
const char *field0 = data;
mp_decode_array(&field0);
const char *field0_end = field0;
mp_next(&field0_end);
const char *field = field0;
const char *field_end = field0_end;
uint32_t current_fieldno = 0;
for (uint32_t i = 0; i < key_def->part_count; i++) {
uint32_t fieldno = key_def->parts[i].fieldno;
for (; i < key_def->part_count - 1; i++) {
if (key_def->parts[i].fieldno + 1 !=
key_def->parts[i + 1].fieldno)
break;
}
if (fieldno < current_fieldno) {
/* Rewind. */
field = field0;
field_end = field0_end;
current_fieldno = 0;
}
while (current_fieldno < fieldno) {
/* search first field of key in tuple raw data */
field = field_end;
mp_next(&field_end);
current_fieldno++;
}
while (current_fieldno < key_def->parts[i].fieldno) {
/* search the last field in subsequence */
mp_next(&field_end);
current_fieldno++;
}
memcpy(key_buf, field, field_end - field);
key_buf += field_end - field;
assert(key_buf - key <= data_end - data);
}
if (key_size != NULL)
*key_size = (uint32_t)(key_buf - key);
return key;
}
/**
* Initialize tuple_extract_key() and tuple_extract_key_raw()
*/
void
tuple_extract_key_set(struct key_def *key_def)
{
if (key_def_is_sequential(key_def)) {
key_def->tuple_extract_key = tuple_extract_key_sequential;
key_def->tuple_extract_key_raw = tuple_extract_key_sequential_raw;
} else {
key_def->tuple_extract_key = tuple_extract_key_slowpath;
key_def->tuple_extract_key_raw = tuple_extract_key_slowpath_raw;
}
}
int
tuple_init(field_name_hash_f hash)
{
field_name_hash = hash;
/*
* Create a format for runtime tuples
*/
RLIST_HEAD(empty_list);
tuple_format_runtime = tuple_format_new(&tuple_format_runtime_vtab,
NULL, 0, 0, NULL, 0);
if (tuple_format_runtime == NULL)
return -1;
/* Make sure this one stays around. */
tuple_format_ref(tuple_format_runtime);
small_alloc_create(&runtime_alloc, &cord()->slabc, OBJSIZE_MIN,
ALLOC_FACTOR);
mempool_create(&tuple_iterator_pool, &cord()->slabc,
sizeof(struct tuple_iterator));
box_tuple_last = NULL;
if (coll_cache_init() != 0)
return -1;
return 0;
}
void
tuple_arena_create(struct slab_arena *arena, struct quota *quota,
uint64_t arena_max_size, uint32_t slab_size,
const char *arena_name)
{
/*
* Ensure that quota is a multiple of slab_size, to
* have accurate value of quota_used_ratio.
*/
size_t prealloc = small_align(arena_max_size, slab_size);
say_info("mapping %zu bytes for %s tuple arena...", prealloc,
arena_name);
if (slab_arena_create(arena, quota, prealloc, slab_size,
MAP_PRIVATE) != 0) {
if (errno == ENOMEM) {
panic("failed to preallocate %zu bytes: Cannot "\
"allocate memory, check option '%s_memory' in box.cfg(..)", prealloc,
arena_name);
} else {
panic_syserror("failed to preallocate %zu bytes for %s"\
" tuple arena", prealloc, arena_name);
}
}
}
void
tuple_arena_destroy(struct slab_arena *arena)
{
slab_arena_destroy(arena);
}
void
tuple_free(void)
{
/* Unref last tuple returned by public C API */
if (box_tuple_last != NULL) {
tuple_unref(box_tuple_last);
box_tuple_last = NULL;
}
mempool_destroy(&tuple_iterator_pool);
small_alloc_destroy(&runtime_alloc);
tuple_format_free();
coll_cache_destroy();
}
box_tuple_format_t *
box_tuple_format_default(void)
{
return tuple_format_runtime;
}
box_tuple_format_t *
box_tuple_format_new(struct key_def **keys, uint16_t key_count)
{
box_tuple_format_t *format =
tuple_format_new(&tuple_format_runtime_vtab,
keys, key_count, 0, NULL, 0);
if (format != NULL)
tuple_format_ref(format);
return format;
}
int
box_tuple_ref(box_tuple_t *tuple)
{
assert(tuple != NULL);
return tuple_ref(tuple);
}
void
box_tuple_unref(box_tuple_t *tuple)
{
assert(tuple != NULL);
return tuple_unref(tuple);
}
uint32_t
box_tuple_field_count(const box_tuple_t *tuple)
{
assert(tuple != NULL);
return tuple_field_count(tuple);
}
size_t
box_tuple_bsize(const box_tuple_t *tuple)
{
assert(tuple != NULL);
return tuple->bsize;
}
ssize_t
tuple_to_buf(const struct tuple *tuple, char *buf, size_t size)
{
uint32_t bsize;
const char *data = tuple_data_range(tuple, &bsize);
if (likely(bsize <= size)) {
memcpy(buf, data, bsize);
}
return bsize;
}
ssize_t
box_tuple_to_buf(const box_tuple_t *tuple, char *buf, size_t size)
{
assert(tuple != NULL);
return tuple_to_buf(tuple, buf, size);
}
box_tuple_format_t *
box_tuple_format(const box_tuple_t *tuple)
{
assert(tuple != NULL);
return tuple_format(tuple);
}
const char *
box_tuple_field(const box_tuple_t *tuple, uint32_t fieldno)
{
assert(tuple != NULL);
return tuple_field(tuple, fieldno);
}
typedef struct tuple_iterator box_tuple_iterator_t;
box_tuple_iterator_t *
box_tuple_iterator(box_tuple_t *tuple)
{
assert(tuple != NULL);
struct tuple_iterator *it = (struct tuple_iterator *)
mempool_alloc(&tuple_iterator_pool);
if (it == NULL) {
diag_set(OutOfMemory, tuple_iterator_pool.objsize,
"mempool", "new slab");
return NULL;
}
if (tuple_ref(tuple) != 0) {
mempool_free(&tuple_iterator_pool, it);
return NULL;
}
tuple_rewind(it, tuple);
return it;
}
void
box_tuple_iterator_free(box_tuple_iterator_t *it)
{
tuple_unref(it->tuple);
mempool_free(&tuple_iterator_pool, it);
}
uint32_t
box_tuple_position(box_tuple_iterator_t *it)
{
return it->fieldno;
}
void
box_tuple_rewind(box_tuple_iterator_t *it)
{
tuple_rewind(it, it->tuple);
}
const char *
box_tuple_seek(box_tuple_iterator_t *it, uint32_t fieldno)
{
return tuple_seek(it, fieldno);
}
const char *
box_tuple_next(box_tuple_iterator_t *it)
{
return tuple_next(it);
}
box_tuple_t *
box_tuple_update(const box_tuple_t *tuple, const char *expr,
const char *expr_end)
{
struct tuple_format *format = tuple_format_runtime;
uint32_t new_size = 0, bsize;
const char *old_data = tuple_data_range(tuple, &bsize);
struct region *region = &fiber()->gc;
size_t used = region_used(region);
const char *new_data =
tuple_update_execute(region_aligned_alloc_cb, region, expr,
expr_end, old_data, old_data + bsize,
&new_size, 1, NULL);
if (new_data == NULL) {
region_truncate(region, used);
return NULL;
}
struct tuple *ret = tuple_new(format, new_data, new_data + new_size);
region_truncate(region, used);
if (ret != NULL)
return tuple_bless(ret);
return NULL;
}
box_tuple_t *
box_tuple_upsert(const box_tuple_t *tuple, const char *expr,
const char *expr_end)
{
struct tuple_format *format = tuple_format_runtime;
uint32_t new_size = 0, bsize;
const char *old_data = tuple_data_range(tuple, &bsize);
struct region *region = &fiber()->gc;
size_t used = region_used(region);
const char *new_data =
tuple_upsert_execute(region_aligned_alloc_cb, region, expr,
expr_end, old_data, old_data + bsize,
&new_size, 1, false, NULL);
if (new_data == NULL) {
region_truncate(region, used);
return NULL;
}
struct tuple *ret = tuple_new(format, new_data, new_data + new_size);
region_truncate(region, used);
if (ret != NULL)
return tuple_bless(ret);
return NULL;
}
box_tuple_t *
box_tuple_new(box_tuple_format_t *format, const char *data, const char *end)
{
struct tuple *ret = tuple_new(format, data, end);
if (ret == NULL)
return NULL;
/* Can't fail on zero refs. */
return tuple_bless(ret);
}
int
tuple_snprint(char *buf, int size, const struct tuple *tuple)
{
int total = 0;
if (tuple == NULL) {
SNPRINT(total, snprintf, buf, size, "<NULL>");
return total;
}
SNPRINT(total, mp_snprint, buf, size, tuple_data(tuple));
return total;
}
const char *
tuple_str(const struct tuple *tuple)
{
char *buf = tt_static_buf();
if (tuple_snprint(buf, TT_STATIC_BUF_LEN, tuple) < 0)
return "<failed to format tuple>";
return buf;
}