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.h
21
SQL data field and tuple
23
Created 5/30/1994 Heikki Tuuri
24
*************************************************************************/
31
#include "data0types.h"
32
#include "data0type.h"
34
#include "dict0types.h"
36
/** Storage for overflow data in a big record, that is, a clustered
37
index record which needs external storage of data fields */
38
typedef struct big_rec_struct big_rec_t;
41
/*********************************************************************//**
42
Gets pointer to the type struct of SQL data field.
43
@return pointer to the type struct */
48
const dfield_t* field); /*!< in: SQL data field */
49
/*********************************************************************//**
50
Gets pointer to the data in a field.
51
@return pointer to data */
56
const dfield_t* field); /*!< in: field */
57
#else /* UNIV_DEBUG */
58
# define dfield_get_type(field) (&(field)->type)
59
# define dfield_get_data(field) ((field)->data)
60
#endif /* UNIV_DEBUG */
61
/*********************************************************************//**
62
Sets the type struct of SQL data field. */
67
dfield_t* field, /*!< in: SQL data field */
68
dtype_t* type); /*!< in: pointer to data type struct */
69
/*********************************************************************//**
70
Gets length of field data.
71
@return length of data; UNIV_SQL_NULL if SQL null data */
76
const dfield_t* field); /*!< in: field */
77
/*********************************************************************//**
78
Sets length in a field. */
83
dfield_t* field, /*!< in: field */
84
ulint len); /*!< in: length or UNIV_SQL_NULL */
85
/*********************************************************************//**
86
Determines if a field is SQL NULL
87
@return nonzero if SQL null data */
92
const dfield_t* field); /*!< in: field */
93
/*********************************************************************//**
94
Determines if a field is externally stored
95
@return nonzero if externally stored */
100
const dfield_t* field); /*!< in: field */
101
/*********************************************************************//**
102
Sets the "external storage" flag */
107
dfield_t* field); /*!< in/out: field */
108
/*********************************************************************//**
109
Sets pointer to the data and length in a field. */
114
dfield_t* field, /*!< in: field */
115
const void* data, /*!< in: data */
116
ulint len); /*!< in: length or UNIV_SQL_NULL */
117
/*********************************************************************//**
118
Sets a data field to SQL NULL. */
123
dfield_t* field); /*!< in/out: field */
124
/**********************************************************************//**
125
Writes an SQL null field full of zeros. */
130
byte* data, /*!< in: pointer to a buffer of size len */
131
ulint len); /*!< in: SQL null size in bytes */
132
/*********************************************************************//**
133
Copies the data and len fields. */
138
dfield_t* field1, /*!< out: field to copy to */
139
const dfield_t* field2);/*!< in: field to copy from */
140
/*********************************************************************//**
141
Copies a data field to another. */
146
dfield_t* field1, /*!< out: field to copy to */
147
const dfield_t* field2);/*!< in: field to copy from */
148
/*********************************************************************//**
149
Copies the data pointed to by a data field. */
154
dfield_t* field, /*!< in/out: data field */
155
mem_heap_t* heap); /*!< in: memory heap where allocated */
156
/*********************************************************************//**
157
Tests if data length and content is equal for two dfields.
158
@return TRUE if equal */
161
dfield_datas_are_binary_equal(
162
/*==========================*/
163
const dfield_t* field1, /*!< in: field */
164
const dfield_t* field2);/*!< in: field */
165
/*********************************************************************//**
166
Tests if dfield data length and content is equal to the given.
167
@return TRUE if equal */
170
dfield_data_is_binary_equal(
171
/*========================*/
172
const dfield_t* field, /*!< in: field */
173
ulint len, /*!< in: data length or UNIV_SQL_NULL */
174
const byte* data); /*!< in: data */
175
/*********************************************************************//**
176
Gets number of fields in a data tuple.
177
@return number of fields */
182
const dtuple_t* tuple); /*!< in: tuple */
184
/*********************************************************************//**
185
Gets nth field of a tuple.
189
dtuple_get_nth_field(
190
/*=================*/
191
const dtuple_t* tuple, /*!< in: tuple */
192
ulint n); /*!< in: index of field */
193
#else /* UNIV_DEBUG */
194
# define dtuple_get_nth_field(tuple, n) ((tuple)->fields + (n))
195
#endif /* UNIV_DEBUG */
196
/*********************************************************************//**
197
Gets info bits in a data tuple.
201
dtuple_get_info_bits(
202
/*=================*/
203
const dtuple_t* tuple); /*!< in: tuple */
204
/*********************************************************************//**
205
Sets info bits in a data tuple. */
208
dtuple_set_info_bits(
209
/*=================*/
210
dtuple_t* tuple, /*!< in: tuple */
211
ulint info_bits); /*!< in: info bits */
212
/*********************************************************************//**
213
Gets number of fields used in record comparisons.
214
@return number of fields used in comparisons in rem0cmp.* */
217
dtuple_get_n_fields_cmp(
218
/*====================*/
219
const dtuple_t* tuple); /*!< in: tuple */
220
/*********************************************************************//**
221
Gets number of fields used in record comparisons. */
224
dtuple_set_n_fields_cmp(
225
/*====================*/
226
dtuple_t* tuple, /*!< in: tuple */
227
ulint n_fields_cmp); /*!< in: number of fields used in
228
comparisons in rem0cmp.* */
229
/**********************************************************//**
230
Creates a data tuple to a memory heap. The default value for number
231
of fields used in record comparisons for this tuple is n_fields.
232
@return own: created tuple */
237
mem_heap_t* heap, /*!< in: memory heap where the tuple
239
ulint n_fields); /*!< in: number of fields */
241
/**********************************************************//**
242
Wrap data fields in a tuple. The default value for number
243
of fields used in record comparisons for this tuple is n_fields.
244
@return data tuple */
249
dtuple_t* tuple, /*!< in: storage for data tuple */
250
const dfield_t* fields, /*!< in: fields */
251
ulint n_fields); /*!< in: number of fields */
253
/*********************************************************************//**
254
Sets number of fields used in a tuple. Normally this is set in
255
dtuple_create, but if you want later to set it smaller, you can use this. */
260
dtuple_t* tuple, /*!< in: tuple */
261
ulint n_fields); /*!< in: number of fields */
262
/*********************************************************************//**
263
Copies a data tuple to another. This is a shallow copy; if a deep copy
264
is desired, dfield_dup() will have to be invoked on each field.
265
@return own: copy of tuple */
270
const dtuple_t* tuple, /*!< in: tuple to copy from */
271
mem_heap_t* heap); /*!< in: memory heap
272
where the tuple is created */
273
/**********************************************************//**
274
The following function returns the sum of data lengths of a tuple. The space
275
occupied by the field structs or the tuple struct is not counted.
276
@return sum of data lens */
279
dtuple_get_data_size(
280
/*=================*/
281
const dtuple_t* tuple, /*!< in: typed data tuple */
282
ulint comp); /*!< in: nonzero=ROW_FORMAT=COMPACT */
283
/*********************************************************************//**
284
Computes the number of externally stored fields in a data tuple.
285
@return number of fields */
290
const dtuple_t* tuple); /*!< in: tuple */
291
/************************************************************//**
292
Compare two data tuples, respecting the collation of character fields.
293
@return 1, 0 , -1 if tuple1 is greater, equal, less, respectively,
299
const dtuple_t* tuple1, /*!< in: tuple 1 */
300
const dtuple_t* tuple2);/*!< in: tuple 2 */
301
/************************************************************//**
302
Folds a prefix given as the number of fields of a tuple.
303
@return the folded value */
308
const dtuple_t* tuple, /*!< in: the tuple */
309
ulint n_fields,/*!< in: number of complete fields to fold */
310
ulint n_bytes,/*!< in: number of bytes to fold in an
311
incomplete last field */
312
index_id_t tree_id)/*!< in: index tree id */
313
__attribute__((pure));
314
/*******************************************************************//**
315
Sets types of fields binary in a tuple. */
318
dtuple_set_types_binary(
319
/*====================*/
320
dtuple_t* tuple, /*!< in: data tuple */
321
ulint n); /*!< in: number of fields to set */
322
/**********************************************************************//**
323
Checks if a dtuple contains an SQL null value.
324
@return TRUE if some field is SQL null */
327
dtuple_contains_null(
328
/*=================*/
329
const dtuple_t* tuple); /*!< in: dtuple */
330
/**********************************************************//**
331
Checks that a data field is typed. Asserts an error if not.
332
@return TRUE if ok */
337
const dfield_t* field); /*!< in: data field */
338
/**********************************************************//**
339
Checks that a data tuple is typed. Asserts an error if not.
340
@return TRUE if ok */
345
const dtuple_t* tuple); /*!< in: tuple */
346
/**********************************************************//**
347
Checks that a data tuple is typed.
348
@return TRUE if ok */
351
dtuple_check_typed_no_assert(
352
/*=========================*/
353
const dtuple_t* tuple); /*!< in: tuple */
355
/**********************************************************//**
356
Validates the consistency of a tuple which must be complete, i.e,
357
all fields must have been set.
358
@return TRUE if ok */
363
const dtuple_t* tuple); /*!< in: tuple */
364
#endif /* UNIV_DEBUG */
365
/*************************************************************//**
366
Pretty prints a dfield value according to its data type. */
371
const dfield_t* dfield);/*!< in: dfield */
372
/*************************************************************//**
373
Pretty prints a dfield value according to its data type. Also the hex string
374
is printed if a string contains non-printable characters. */
377
dfield_print_also_hex(
378
/*==================*/
379
const dfield_t* dfield); /*!< in: dfield */
380
/**********************************************************//**
381
The following function prints the contents of a tuple. */
386
FILE* f, /*!< in: output stream */
387
const dtuple_t* tuple); /*!< in: tuple */
388
/**************************************************************//**
389
Moves parts of long fields in entry to the big record vector so that
390
the size of tuple drops below the maximum record size allowed in the
391
database. Moves data only from those fields which are not necessary
392
to determine uniquely the insertion place of the tuple in the index.
393
@return own: created big record vector, NULL if we are not able to
394
shorten the entry enough, i.e., if there are too many fixed-length or
395
short fields in entry or the index is clustered */
398
dtuple_convert_big_rec(
399
/*===================*/
400
dict_index_t* index, /*!< in: index */
401
dtuple_t* entry, /*!< in/out: index entry */
402
ulint* n_ext); /*!< in/out: number of
403
externally stored columns */
404
/**************************************************************//**
405
Puts back to entry the data stored in vector. Note that to ensure the
406
fields in entry can accommodate the data, vector must have been created
407
from entry with dtuple_convert_big_rec. */
410
dtuple_convert_back_big_rec(
411
/*========================*/
412
dict_index_t* index, /*!< in: index */
413
dtuple_t* entry, /*!< in: entry whose data was put to vector */
414
big_rec_t* vector);/*!< in, own: big rec vector; it is
415
freed in this function */
416
/**************************************************************//**
417
Frees the memory in a big rec vector. */
422
big_rec_t* vector); /*!< in, own: big rec vector; it is
423
freed in this function */
425
/*######################################################################*/
427
/** Structure for an SQL data field */
428
struct dfield_struct{
429
void* data; /*!< pointer to data */
430
unsigned ext:1; /*!< TRUE=externally stored, FALSE=local */
431
unsigned len:32; /*!< data length; UNIV_SQL_NULL if SQL null */
432
dtype_t type; /*!< type of data */
435
/** Structure for an SQL data tuple of fields (logical record) */
436
struct dtuple_struct {
437
ulint info_bits; /*!< info bits of an index record:
438
the default is 0; this field is used
439
if an index record is built from
441
ulint n_fields; /*!< number of fields in dtuple */
442
ulint n_fields_cmp; /*!< number of fields which should
443
be used in comparison services
444
of rem0cmp.*; the index search
445
is performed by comparing only these
446
fields, others are ignored; the
447
default value in dtuple creation is
448
the same value as n_fields */
449
dfield_t* fields; /*!< fields */
450
UT_LIST_NODE_T(dtuple_t) tuple_list;
451
/*!< data tuples can be linked into a
452
list using this field */
454
ulint magic_n; /*!< magic number, used in
456
/** Value of dtuple_struct::magic_n */
457
# define DATA_TUPLE_MAGIC_N 65478679
458
#endif /* UNIV_DEBUG */
461
/** A slot for a field in a big rec vector */
462
typedef struct big_rec_field_struct big_rec_field_t;
463
/** A slot for a field in a big rec vector */
464
struct big_rec_field_struct {
465
ulint field_no; /*!< field number in record */
466
ulint len; /*!< stored data length, in bytes */
467
const void* data; /*!< stored data */
470
/** Storage format for overflow data in a big record, that is, a
471
clustered index record which needs external storage of data fields */
472
struct big_rec_struct {
473
mem_heap_t* heap; /*!< memory heap from which
475
ulint n_fields; /*!< number of stored fields */
476
big_rec_field_t*fields; /*!< stored fields */
480
#include "data0data.ic"