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 include/data0data.ic
21
SQL data field and tuple
23
Created 5/30/1994 Heikki Tuuri
24
*************************************************************************/
30
/** Dummy variable to catch access to uninitialized fields. In the
31
debug version, dtuple_create() will make all fields of dtuple_t point
33
extern byte data_error;
35
/*********************************************************************//**
36
Gets pointer to the type struct of SQL data field.
37
@return pointer to the type struct */
42
const dfield_t* field) /*!< in: SQL data field */
46
return((dtype_t*) &(field->type));
48
#endif /* UNIV_DEBUG */
50
/*********************************************************************//**
51
Sets the type struct of SQL data field. */
56
dfield_t* field, /*!< in: SQL data field */
57
dtype_t* type) /*!< in: pointer to data type struct */
65
/*********************************************************************//**
66
Gets pointer to the data in a field.
67
@return pointer to data */
72
const dfield_t* field) /*!< in: field */
75
ut_ad((field->len == UNIV_SQL_NULL)
76
|| (field->data != &data_error));
78
return((void*) field->data);
80
#endif /* UNIV_DEBUG */
82
/*********************************************************************//**
83
Gets length of field data.
84
@return length of data; UNIV_SQL_NULL if SQL null data */
89
const dfield_t* field) /*!< in: field */
92
ut_ad((field->len == UNIV_SQL_NULL)
93
|| (field->data != &data_error));
98
/*********************************************************************//**
99
Sets length in a field. */
104
dfield_t* field, /*!< in: field */
105
ulint len) /*!< in: length or UNIV_SQL_NULL */
108
#ifdef UNIV_VALGRIND_DEBUG
109
if (len != UNIV_SQL_NULL) UNIV_MEM_ASSERT_RW(field->data, len);
110
#endif /* UNIV_VALGRIND_DEBUG */
116
/*********************************************************************//**
117
Determines if a field is SQL NULL
118
@return nonzero if SQL null data */
123
const dfield_t* field) /*!< in: field */
127
return(field->len == UNIV_SQL_NULL);
130
/*********************************************************************//**
131
Determines if a field is externally stored
132
@return nonzero if externally stored */
137
const dfield_t* field) /*!< in: field */
141
return(UNIV_UNLIKELY(field->ext));
144
/*********************************************************************//**
145
Sets the "external storage" flag */
150
dfield_t* field) /*!< in/out: field */
157
/*********************************************************************//**
158
Sets pointer to the data and length in a field. */
163
dfield_t* field, /*!< in: field */
164
const void* data, /*!< in: data */
165
ulint len) /*!< in: length or UNIV_SQL_NULL */
169
#ifdef UNIV_VALGRIND_DEBUG
170
if (len != UNIV_SQL_NULL) UNIV_MEM_ASSERT_RW(data, len);
171
#endif /* UNIV_VALGRIND_DEBUG */
172
field->data = (void*) data;
177
/*********************************************************************//**
178
Sets a data field to SQL NULL. */
183
dfield_t* field) /*!< in/out: field */
185
dfield_set_data(field, NULL, UNIV_SQL_NULL);
188
/*********************************************************************//**
189
Copies the data and len fields. */
194
dfield_t* field1, /*!< out: field to copy to */
195
const dfield_t* field2) /*!< in: field to copy from */
197
ut_ad(field1 && field2);
199
field1->data = field2->data;
200
field1->len = field2->len;
201
field1->ext = field2->ext;
204
/*********************************************************************//**
205
Copies a data field to another. */
210
dfield_t* field1, /*!< out: field to copy to */
211
const dfield_t* field2) /*!< in: field to copy from */
216
/*********************************************************************//**
217
Copies the data pointed to by a data field. */
222
dfield_t* field, /*!< in/out: data field */
223
mem_heap_t* heap) /*!< in: memory heap where allocated */
225
if (!dfield_is_null(field)) {
226
UNIV_MEM_ASSERT_RW(field->data, field->len);
227
field->data = mem_heap_dup(heap, field->data, field->len);
231
/*********************************************************************//**
232
Tests if data length and content is equal for two dfields.
233
@return TRUE if equal */
236
dfield_datas_are_binary_equal(
237
/*==========================*/
238
const dfield_t* field1, /*!< in: field */
239
const dfield_t* field2) /*!< in: field */
245
return(len == field2->len
246
&& (len == UNIV_SQL_NULL
247
|| !memcmp(field1->data, field2->data, len)));
250
/*********************************************************************//**
251
Gets info bits in a data tuple.
255
dtuple_get_info_bits(
256
/*=================*/
257
const dtuple_t* tuple) /*!< in: tuple */
261
return(tuple->info_bits);
264
/*********************************************************************//**
265
Sets info bits in a data tuple. */
268
dtuple_set_info_bits(
269
/*=================*/
270
dtuple_t* tuple, /*!< in: tuple */
271
ulint info_bits) /*!< in: info bits */
275
tuple->info_bits = info_bits;
278
/*********************************************************************//**
279
Gets number of fields used in record comparisons.
280
@return number of fields used in comparisons in rem0cmp.* */
283
dtuple_get_n_fields_cmp(
284
/*====================*/
285
const dtuple_t* tuple) /*!< in: tuple */
289
return(tuple->n_fields_cmp);
292
/*********************************************************************//**
293
Sets number of fields used in record comparisons. */
296
dtuple_set_n_fields_cmp(
297
/*====================*/
298
dtuple_t* tuple, /*!< in: tuple */
299
ulint n_fields_cmp) /*!< in: number of fields used in
300
comparisons in rem0cmp.* */
303
ut_ad(n_fields_cmp <= tuple->n_fields);
305
tuple->n_fields_cmp = n_fields_cmp;
308
/*********************************************************************//**
309
Gets number of fields in a data tuple.
310
@return number of fields */
315
const dtuple_t* tuple) /*!< in: tuple */
319
return(tuple->n_fields);
323
/*********************************************************************//**
324
Gets nth field of a tuple.
328
dtuple_get_nth_field(
329
/*=================*/
330
const dtuple_t* tuple, /*!< in: tuple */
331
ulint n) /*!< in: index of field */
334
ut_ad(n < tuple->n_fields);
336
return((dfield_t*) tuple->fields + n);
338
#endif /* UNIV_DEBUG */
340
/**********************************************************//**
341
Creates a data tuple to a memory heap. The default value for number
342
of fields used in record comparisons for this tuple is n_fields.
343
@return own: created tuple */
348
mem_heap_t* heap, /*!< in: memory heap where the tuple
350
ulint n_fields) /*!< in: number of fields */
356
tuple = (dtuple_t*) mem_heap_alloc(heap, sizeof(dtuple_t)
357
+ n_fields * sizeof(dfield_t));
358
tuple->info_bits = 0;
359
tuple->n_fields = n_fields;
360
tuple->n_fields_cmp = n_fields;
361
tuple->fields = (dfield_t*) &tuple[1];
364
tuple->magic_n = DATA_TUPLE_MAGIC_N;
366
{ /* In the debug version, initialize fields to an error value */
369
for (i = 0; i < n_fields; i++) {
372
field = dtuple_get_nth_field(tuple, i);
374
dfield_set_len(field, UNIV_SQL_NULL);
375
field->data = &data_error;
376
dfield_get_type(field)->mtype = DATA_ERROR;
380
UNIV_MEM_INVALID(tuple->fields, n_fields * sizeof *tuple->fields);
385
/**********************************************************//**
386
Wrap data fields in a tuple. The default value for number
387
of fields used in record comparisons for this tuple is n_fields.
388
@return data tuple */
393
dtuple_t* tuple, /*!< in: storage for data tuple */
394
const dfield_t* fields, /*!< in: fields */
395
ulint n_fields) /*!< in: number of fields */
397
tuple->info_bits = 0;
398
tuple->n_fields = tuple->n_fields_cmp = n_fields;
399
tuple->fields = (dfield_t*) fields;
400
ut_d(tuple->magic_n = DATA_TUPLE_MAGIC_N);
405
/*********************************************************************//**
406
Copies a data tuple to another. This is a shallow copy; if a deep copy
407
is desired, dfield_dup() will have to be invoked on each field.
408
@return own: copy of tuple */
413
const dtuple_t* tuple, /*!< in: tuple to copy from */
414
mem_heap_t* heap) /*!< in: memory heap
415
where the tuple is created */
417
ulint n_fields = dtuple_get_n_fields(tuple);
418
dtuple_t* new_tuple = dtuple_create(heap, n_fields);
421
for (i = 0; i < n_fields; i++) {
422
dfield_copy(dtuple_get_nth_field(new_tuple, i),
423
dtuple_get_nth_field(tuple, i));
429
/**********************************************************//**
430
The following function returns the sum of data lengths of a tuple. The space
431
occupied by the field structs or the tuple struct is not counted. Neither
432
is possible space in externally stored parts of the field.
433
@return sum of data lengths */
436
dtuple_get_data_size(
437
/*=================*/
438
const dtuple_t* tuple, /*!< in: typed data tuple */
439
ulint comp) /*!< in: nonzero=ROW_FORMAT=COMPACT */
441
const dfield_t* field;
448
ut_ad(dtuple_check_typed(tuple));
449
ut_ad(tuple->magic_n == DATA_TUPLE_MAGIC_N);
451
n_fields = tuple->n_fields;
453
for (i = 0; i < n_fields; i++) {
454
field = dtuple_get_nth_field(tuple, i);
455
len = dfield_get_len(field);
457
if (len == UNIV_SQL_NULL) {
458
len = dtype_get_sql_null_size(dfield_get_type(field),
468
/*********************************************************************//**
469
Computes the number of externally stored fields in a data tuple.
470
@return number of externally stored fields */
475
const dtuple_t* tuple) /*!< in: tuple */
478
ulint n_fields = tuple->n_fields;
482
ut_ad(dtuple_check_typed(tuple));
483
ut_ad(tuple->magic_n == DATA_TUPLE_MAGIC_N);
485
for (i = 0; i < n_fields; i++) {
486
n_ext += dtuple_get_nth_field(tuple, i)->ext;
492
/*******************************************************************//**
493
Sets types of fields binary in a tuple. */
496
dtuple_set_types_binary(
497
/*====================*/
498
dtuple_t* tuple, /*!< in: data tuple */
499
ulint n) /*!< in: number of fields to set */
501
dtype_t* dfield_type;
504
for (i = 0; i < n; i++) {
505
dfield_type = dfield_get_type(dtuple_get_nth_field(tuple, i));
506
dtype_set(dfield_type, DATA_BINARY, 0, 0);
510
/************************************************************//**
511
Folds a prefix given as the number of fields of a tuple.
512
@return the folded value */
517
const dtuple_t* tuple, /*!< in: the tuple */
518
ulint n_fields,/*!< in: number of complete fields to fold */
519
ulint n_bytes,/*!< in: number of bytes to fold in an
520
incomplete last field */
521
index_id_t tree_id)/*!< in: index tree id */
523
const dfield_t* field;
530
ut_ad(tuple->magic_n == DATA_TUPLE_MAGIC_N);
531
ut_ad(dtuple_check_typed(tuple));
533
fold = ut_fold_ull(tree_id);
535
for (i = 0; i < n_fields; i++) {
536
field = dtuple_get_nth_field(tuple, i);
538
data = (const byte*) dfield_get_data(field);
539
len = dfield_get_len(field);
541
if (len != UNIV_SQL_NULL) {
542
fold = ut_fold_ulint_pair(fold,
543
ut_fold_binary(data, len));
548
field = dtuple_get_nth_field(tuple, i);
550
data = (const byte*) dfield_get_data(field);
551
len = dfield_get_len(field);
553
if (len != UNIV_SQL_NULL) {
558
fold = ut_fold_ulint_pair(fold,
559
ut_fold_binary(data, len));
566
/**********************************************************************//**
567
Writes an SQL null field full of zeros. */
572
byte* data, /*!< in: pointer to a buffer of size len */
573
ulint len) /*!< in: SQL null size in bytes */
575
memset(data, 0, len);
578
/**********************************************************************//**
579
Checks if a dtuple contains an SQL null value.
580
@return TRUE if some field is SQL null */
583
dtuple_contains_null(
584
/*=================*/
585
const dtuple_t* tuple) /*!< in: dtuple */
590
n = dtuple_get_n_fields(tuple);
592
for (i = 0; i < n; i++) {
593
if (dfield_is_null(dtuple_get_nth_field(tuple, i))) {
602
/**************************************************************//**
603
Frees the memory in a big rec vector. */
608
big_rec_t* vector) /*!< in, own: big rec vector; it is
609
freed in this function */
611
mem_heap_free(vector->heap);