1
/*****************************************************************************
3
Copyright (C) 1994, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
17
*****************************************************************************/
19
/********************************************************************//**
20
@file data/data0data.c
1
/************************************************************************
21
2
SQL data field and tuple
4
(c) 1994-1996 Innobase Oy
23
6
Created 5/30/1994 Heikki Tuuri
24
7
*************************************************************************/
38
20
#include "btr0cur.h"
41
#endif /* !UNIV_HOTBACKUP */
44
/** Dummy variable to catch access to uninitialized fields. In the
45
debug version, dtuple_create() will make all fields of dtuple_t point
25
/* data pointers of tuple fields are initialized to point here
47
27
UNIV_INTERN byte data_error;
49
# ifndef UNIV_DEBUG_VALGRIND
50
/** this is used to fool the compiler in dtuple_validate */
29
/* this is used to fool the compiler in dtuple_validate */
51
30
UNIV_INTERN ulint data_dummy;
52
# endif /* !UNIV_DEBUG_VALGRIND */
53
31
#endif /* UNIV_DEBUG */
55
#ifndef UNIV_HOTBACKUP
56
/*********************************************************************//**
57
Tests if dfield data length and content is equal to the given.
58
@return TRUE if equal */
33
/*************************************************************************
34
Tests if dfield data length and content is equal to the given. */
61
37
dfield_data_is_binary_equal(
62
38
/*========================*/
63
const dfield_t* field, /*!< in: field */
64
ulint len, /*!< in: data length or UNIV_SQL_NULL */
65
const byte* data) /*!< in: data */
39
/* out: TRUE if equal */
40
const dfield_t* field, /* in: field */
41
ulint len, /* in: data length or UNIV_SQL_NULL */
42
const byte* data) /* in: data */
67
44
if (len != dfield_get_len(field)) {
85
/************************************************************//**
86
Compare two data tuples, respecting the collation of character fields.
87
@return 1, 0 , -1 if tuple1 is greater, equal, less, respectively,
62
/****************************************************************
63
Compare two data tuples, respecting the collation of character fields. */
93
const dtuple_t* tuple1, /*!< in: tuple 1 */
94
const dtuple_t* tuple2) /*!< in: tuple 2 */
68
/* out: 1, 0 , -1 if tuple1 is greater, equal,
69
less, respectively, than tuple2 */
70
const dtuple_t* tuple1, /* in: tuple 1 */
71
const dtuple_t* tuple2) /* in: tuple 2 */
127
/*********************************************************************//**
104
/*************************************************************************
128
105
Sets number of fields used in a tuple. Normally this is set in
129
106
dtuple_create, but if you want later to set it smaller, you can use this. */
132
109
dtuple_set_n_fields(
133
110
/*================*/
134
dtuple_t* tuple, /*!< in: tuple */
135
ulint n_fields) /*!< in: number of fields */
111
dtuple_t* tuple, /* in: tuple */
112
ulint n_fields) /* in: number of fields */
140
117
tuple->n_fields_cmp = n_fields;
143
/**********************************************************//**
144
Checks that a data field is typed.
145
@return TRUE if ok */
120
/**************************************************************
121
Checks that a data field is typed. */
148
124
dfield_check_typed_no_assert(
149
125
/*=========================*/
150
const dfield_t* field) /*!< in: data field */
126
/* out: TRUE if ok */
127
const dfield_t* field) /* in: data field */
152
129
if (dfield_get_type(field)->mtype > DATA_MYSQL
153
130
|| dfield_get_type(field)->mtype < DATA_VARCHAR) {
165
/**********************************************************//**
166
Checks that a data tuple is typed.
167
@return TRUE if ok */
142
/**************************************************************
143
Checks that a data tuple is typed. */
170
146
dtuple_check_typed_no_assert(
171
147
/*=========================*/
172
const dtuple_t* tuple) /*!< in: tuple */
148
/* out: TRUE if ok */
149
const dtuple_t* tuple) /* in: tuple */
174
151
const dfield_t* field;
200
#endif /* !UNIV_HOTBACKUP */
203
/**********************************************************//**
204
Checks that a data field is typed. Asserts an error if not.
205
@return TRUE if ok */
178
/**************************************************************
179
Checks that a data field is typed. Asserts an error if not. */
208
182
dfield_check_typed(
209
183
/*===============*/
210
const dfield_t* field) /*!< in: data field */
184
/* out: TRUE if ok */
185
const dfield_t* field) /* in: data field */
212
187
if (dfield_get_type(field)->mtype > DATA_MYSQL
213
188
|| dfield_get_type(field)->mtype < DATA_VARCHAR) {
226
/**********************************************************//**
227
Checks that a data tuple is typed. Asserts an error if not.
228
@return TRUE if ok */
201
/**************************************************************
202
Checks that a data tuple is typed. Asserts an error if not. */
231
205
dtuple_check_typed(
232
206
/*===============*/
233
const dtuple_t* tuple) /*!< in: tuple */
207
/* out: TRUE if ok */
208
const dtuple_t* tuple) /* in: tuple */
235
210
const dfield_t* field;
248
/**********************************************************//**
224
/**************************************************************
249
225
Validates the consistency of a tuple which must be complete, i.e,
250
all fields must have been set.
251
@return TRUE if ok */
226
all fields must have been set. */
256
const dtuple_t* tuple) /*!< in: tuple */
231
/* out: TRUE if ok */
232
const dtuple_t* tuple) /* in: tuple */
258
234
const dfield_t* field;
263
241
ut_ad(tuple->magic_n == DATA_TUPLE_MAGIC_N);
275
253
if (!dfield_is_null(field)) {
277
const byte* data = dfield_get_data(field);
278
#ifndef UNIV_DEBUG_VALGRIND
255
data = dfield_get_data(field);
256
UNIV_MEM_ASSERT_RW(data, len);
281
258
for (j = 0; j < len; j++) {
298
272
#endif /* UNIV_DEBUG */
300
#ifndef UNIV_HOTBACKUP
301
/*************************************************************//**
274
/*****************************************************************
302
275
Pretty prints a dfield value according to its data type. */
307
const dfield_t* dfield) /*!< in: dfield */
280
const dfield_t* dfield) /* in: dfield */
311
ulint len = dfield_get_len(dfield);
312
const byte *data = static_cast<const byte *>(dfield_get_data(dfield));
286
len = dfield_get_len(dfield);
287
data = dfield_get_data(dfield);
314
289
if (dfield_is_null(dfield)) {
315
290
fputs("NULL", stderr);
341
/*************************************************************//**
316
/*****************************************************************
342
317
Pretty prints a dfield value according to its data type. Also the hex string
343
318
is printed if a string contains non-printable characters. */
346
321
dfield_print_also_hex(
347
322
/*==================*/
348
const dfield_t* dfield) /*!< in: dfield */
323
const dfield_t* dfield) /* in: dfield */
350
325
const byte* data;
408
381
val = mach_read_from_4(data);
410
383
if (!(prtype & DATA_UNSIGNED)) {
411
val &= ~UNSIGNED_MASK;
412
385
fprintf(stderr, "%ld", (long) val);
414
387
fprintf(stderr, "%lu", (ulong) val);
419
392
id = mach_read_from_6(data);
420
fprintf(stderr, "%llu", (ullint) id);
393
fprintf(stderr, "{%lu %lu}",
394
ut_dulint_get_high(id),
395
ut_dulint_get_low(id));
424
399
id = mach_read_from_7(data);
425
fprintf(stderr, "%llu", (ullint) id);
400
fprintf(stderr, "{%lu %lu}",
401
ut_dulint_get_high(id),
402
ut_dulint_get_low(id));
428
405
id = mach_read_from_8(data);
429
fprintf(stderr, "%llu", (ullint) id);
406
fprintf(stderr, "{%lu %lu}",
407
ut_dulint_get_high(id),
408
ut_dulint_get_low(id));
438
417
case DATA_TRX_ID:
439
418
id = mach_read_from_6(data);
441
fprintf(stderr, "trx_id " TRX_ID_FMT, id);
420
fprintf(stderr, "trx_id " TRX_ID_FMT,
421
TRX_ID_PREP_PRINTF(id));
444
424
case DATA_ROLL_PTR:
445
425
id = mach_read_from_7(data);
447
fprintf(stderr, "roll_ptr " TRX_ID_FMT, id);
427
fprintf(stderr, "roll_ptr {%lu %lu}",
428
ut_dulint_get_high(id), ut_dulint_get_low(id));
450
431
case DATA_ROW_ID:
451
432
id = mach_read_from_6(data);
453
fprintf(stderr, "row_id " TRX_ID_FMT, id);
434
fprintf(stderr, "row_id {%lu %lu}",
435
ut_dulint_get_high(id), ut_dulint_get_low(id));
457
id = mach_ull_read_compressed(data);
439
id = mach_dulint_read_compressed(data);
459
fprintf(stderr, "mix_id " TRX_ID_FMT, id);
441
fprintf(stderr, "mix_id {%lu %lu}",
442
ut_dulint_get_high(id), ut_dulint_get_low(id));
505
/*************************************************************//**
488
/*****************************************************************
506
489
Print a dfield value using ut_print_buf. */
509
492
dfield_print_raw(
510
493
/*=============*/
511
FILE* f, /*!< in: output stream */
512
const dfield_t* dfield) /*!< in: dfield */
494
FILE* f, /* in: output stream */
495
const dfield_t* dfield) /* in: dfield */
514
497
ulint len = dfield_get_len(dfield);
515
498
if (!dfield_is_null(dfield)) {
528
/**********************************************************//**
511
/**************************************************************
529
512
The following function prints the contents of a tuple. */
534
FILE* f, /*!< in: output stream */
535
const dtuple_t* tuple) /*!< in: tuple */
517
FILE* f, /* in: output stream */
518
const dtuple_t* tuple) /* in: tuple */
547
530
dfield_print_raw(f, dtuple_get_nth_field(tuple, i));
553
536
ut_ad(dtuple_validate(tuple));
556
/**************************************************************//**
539
/******************************************************************
557
540
Moves parts of long fields in entry to the big record vector so that
558
541
the size of tuple drops below the maximum record size allowed in the
559
542
database. Moves data only from those fields which are not necessary
560
to determine uniquely the insertion place of the tuple in the index.
561
@return own: created big record vector, NULL if we are not able to
562
shorten the entry enough, i.e., if there are too many fixed-length or
563
short fields in entry or the index is clustered */
543
to determine uniquely the insertion place of the tuple in the index. */
566
546
dtuple_convert_big_rec(
567
547
/*===================*/
568
dict_index_t* index, /*!< in: index */
569
dtuple_t* entry, /*!< in/out: index entry */
570
ulint* n_ext) /*!< in/out: number of
548
/* out, own: created big record vector,
549
NULL if we are not able to shorten
550
the entry enough, i.e., if there are
551
too many fixed-length or short fields
552
in entry or the index is clustered */
553
dict_index_t* index, /* in: index */
554
dtuple_t* entry, /* in/out: index entry */
555
ulint* n_ext) /* in/out: number of
571
556
externally stored columns */
573
558
mem_heap_t* heap;
607
592
heap = mem_heap_create(size + dtuple_get_n_fields(entry)
608
593
* sizeof(big_rec_field_t) + 1000);
610
vector = static_cast<big_rec_t *>(mem_heap_alloc(heap, sizeof(big_rec_t)));
595
vector = mem_heap_alloc(heap, sizeof(big_rec_t));
612
597
vector->heap = heap;
613
vector->fields = static_cast<big_rec_field_t *>(mem_heap_alloc(heap, dtuple_get_n_fields(entry)
614
* sizeof(big_rec_field_t)));
598
vector->fields = mem_heap_alloc(heap, dtuple_get_n_fields(entry)
599
* sizeof(big_rec_field_t));
616
601
/* Decide which fields to shorten: the algorithm is to look for
617
602
a variable-length field that yields the biggest savings when
659
/* In DYNAMIC and COMPRESSED format, store
660
locally any non-BLOB columns whose maximum
661
length does not exceed 256 bytes. This is
662
because there is no room for the "external
663
storage" flag when the maximum length is 255
664
bytes or less. This restriction trivially
665
holds in REDUNDANT and COMPACT format, because
666
there we always store locally columns whose
667
length is up to local_len == 788 bytes.
668
@see rec_init_offsets_comp_ordinary */
669
if (ifield->col->mtype != DATA_BLOB
670
&& ifield->col->len < 256) {
674
/* In DYNAMIC and COMPRESSED format, store
675
locally any non-BLOB columns whose maximum
676
length does not exceed 256 bytes. This is
677
because there is no room for the "external
678
storage" flag when the maximum length is 255
679
bytes or less. This restriction trivially
680
holds in REDUNDANT and COMPACT format, because
681
there we always store locally columns whose
682
length is up to local_len == 788 bytes.
683
@see rec_init_offsets_comp_ordinary */
684
if (ifield->col->mtype != DATA_BLOB
685
&& ifield->col->len < 256) {
689
/* In DYNAMIC and COMPRESSED format, store
690
locally any non-BLOB columns whose maximum
691
length does not exceed 256 bytes. This is
692
because there is no room for the "external
693
storage" flag when the maximum length is 255
694
bytes or less. This restriction trivially
695
holds in REDUNDANT and COMPACT format, because
696
there we always store locally columns whose
697
length is up to local_len == 788 bytes.
698
@see rec_init_offsets_comp_ordinary */
699
if (ifield->col->mtype != DATA_BLOB
700
&& ifield->col->len < 256) {
705
645
longest = savings;
732
672
b->data = (char*) dfield_get_data(dfield) + local_prefix_len;
734
674
/* Allocate the locally stored part of the column. */
735
data = static_cast<unsigned char *>(mem_heap_alloc(heap, local_len));
675
data = mem_heap_alloc(heap, local_len);
737
677
/* Copy the local prefix. */
738
678
memcpy(data, dfield_get_data(dfield), local_prefix_len);
762
/**************************************************************//**
702
/******************************************************************
763
703
Puts back to entry the data stored in vector. Note that to ensure the
764
704
fields in entry can accommodate the data, vector must have been created
765
705
from entry with dtuple_convert_big_rec. */
768
708
dtuple_convert_back_big_rec(
769
709
/*========================*/
770
dict_index_t* /*index __attribute__((unused))*/, /*!< in: index */
771
dtuple_t* entry, /*!< in: entry whose data was put to vector */
772
big_rec_t* vector) /*!< in, own: big rec vector; it is
710
dict_index_t* index __attribute__((unused)), /* in: index */
711
dtuple_t* entry, /* in: entry whose data was put to vector */
712
big_rec_t* vector) /* in, own: big rec vector; it is
773
713
freed in this function */
775
715
big_rec_field_t* b = vector->fields;