1
/**********************************************************************
6
Created 1/8/1996 Heikki Tuuri
7
***********************************************************************/
12
#include "rem0types.h"
13
#include "data0type.h"
15
/*************************************************************************
16
Gets the column data type. */
21
const dict_col_t* col, /* in: column */
22
dtype_t* type) /* out: data type */
26
type->mtype = col->mtype;
27
type->prtype = col->prtype;
29
type->mbminlen = col->mbminlen;
30
type->mbmaxlen = col->mbmaxlen;
34
/*************************************************************************
35
Assert that a column and a data type match. */
38
dict_col_type_assert_equal(
39
/*=======================*/
41
const dict_col_t* col, /* in: column */
42
const dtype_t* type) /* in: data type */
47
ut_ad(col->mtype == type->mtype);
48
ut_ad(col->prtype == type->prtype);
49
ut_ad(col->len == type->len);
50
ut_ad(col->mbminlen == type->mbminlen);
51
ut_ad(col->mbmaxlen == type->mbmaxlen);
55
#endif /* UNIV_DEBUG */
57
/***************************************************************************
58
Returns the minimum size of the column. */
61
dict_col_get_min_size(
62
/*==================*/
63
/* out: minimum size */
64
const dict_col_t* col) /* in: column */
66
return(dtype_get_min_size_low(col->mtype, col->prtype, col->len,
67
col->mbminlen, col->mbmaxlen));
69
/***************************************************************************
70
Returns the maximum size of the column. */
73
dict_col_get_max_size(
74
/*==================*/
75
/* out: maximum size */
76
const dict_col_t* col) /* in: column */
78
return(dtype_get_max_size_low(col->mtype, col->len));
80
/***************************************************************************
81
Returns the size of a fixed size column, 0 if not a fixed size column. */
84
dict_col_get_fixed_size(
85
/*====================*/
86
/* out: fixed size, or 0 */
87
const dict_col_t* col) /* in: column */
89
return(dtype_get_fixed_size_low(col->mtype, col->prtype, col->len,
90
col->mbminlen, col->mbmaxlen));
92
/***************************************************************************
93
Returns the ROW_FORMAT=REDUNDANT stored SQL NULL size of a column.
94
For fixed length types it is the fixed length of the type, otherwise 0. */
97
dict_col_get_sql_null_size(
98
/*=======================*/
99
/* out: SQL null storage size
100
in ROW_FORMAT=REDUNDANT */
101
const dict_col_t* col) /* in: column */
103
return(dict_col_get_fixed_size(col));
106
/*************************************************************************
107
Gets the column number. */
112
const dict_col_t* col)
119
/*************************************************************************
120
Gets the column position in the clustered index. */
123
dict_col_get_clust_pos(
124
/*===================*/
125
const dict_col_t* col, /* in: table column */
126
const dict_index_t* clust_index) /* in: clustered index */
131
ut_ad(clust_index && clust_index->type & DICT_CLUSTERED);
133
for (i = 0; i < clust_index->n_def; i++) {
134
const dict_field_t* field = &clust_index->fields[i];
136
if (!field->prefix_len && field->col == col) {
141
return(ULINT_UNDEFINED);
144
/************************************************************************
145
Gets the first index on the table (the clustered index). */
148
dict_table_get_first_index(
149
/*=======================*/
150
/* out: index, NULL if none exists */
151
dict_table_t* table) /* in: table */
154
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
156
return(UT_LIST_GET_FIRST(table->indexes));
159
/************************************************************************
160
Gets the next index on the table. */
163
dict_table_get_next_index(
164
/*======================*/
165
/* out: index, NULL if none left */
166
dict_index_t* index) /* in: index */
169
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
171
return(UT_LIST_GET_NEXT(indexes, index));
174
/************************************************************************
175
Gets the number of user-defined columns in a table in the dictionary
179
dict_table_get_n_user_cols(
180
/*=======================*/
181
/* out: number of user-defined (e.g., not
182
ROW_ID) columns of a table */
183
dict_table_t* table) /* in: table */
186
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
188
return(table->n_cols - DATA_N_SYS_COLS);
191
/************************************************************************
192
Gets the number of system columns in a table in the dictionary cache. */
195
dict_table_get_n_sys_cols(
196
/*======================*/
197
/* out: number of system (e.g.,
198
ROW_ID) columns of a table */
199
dict_table_t* table __attribute__((unused))) /* in: table */
202
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
203
ut_ad(table->cached);
205
return(DATA_N_SYS_COLS);
208
/************************************************************************
209
Gets the number of all columns (also system) in a table in the dictionary
213
dict_table_get_n_cols(
214
/*==================*/
215
/* out: number of columns of a table */
216
dict_table_t* table) /* in: table */
219
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
221
return(table->n_cols);
224
/************************************************************************
225
Gets the nth column of a table. */
228
dict_table_get_nth_col(
229
/*===================*/
230
/* out: pointer to column object */
231
const dict_table_t* table, /* in: table */
232
ulint pos) /* in: position of column */
235
ut_ad(pos < table->n_def);
236
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
238
return((table->cols) + pos);
241
/************************************************************************
242
Gets the given system column of a table. */
245
dict_table_get_sys_col(
246
/*===================*/
247
/* out: pointer to column object */
248
const dict_table_t* table, /* in: table */
249
ulint sys) /* in: DATA_ROW_ID, ... */
251
const dict_col_t* col;
254
ut_ad(sys < DATA_N_SYS_COLS);
255
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
257
col = dict_table_get_nth_col(table, table->n_cols
258
- DATA_N_SYS_COLS + sys);
259
ut_ad(col->mtype == DATA_SYS);
260
ut_ad(col->prtype == (sys | DATA_NOT_NULL));
265
/************************************************************************
266
Gets the given system column number of a table. */
269
dict_table_get_sys_col_no(
270
/*======================*/
271
/* out: column number */
272
dict_table_t* table, /* in: table */
273
ulint sys) /* in: DATA_ROW_ID, ... */
276
ut_ad(sys < DATA_N_SYS_COLS);
277
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
279
return(table->n_cols - DATA_N_SYS_COLS + sys);
282
/************************************************************************
283
Check whether the table uses the compact page format. */
288
/* out: TRUE if table uses the
289
compact page format */
290
const dict_table_t* table) /* in: table */
294
#if DICT_TF_COMPACT != TRUE
298
return(UNIV_LIKELY(table->flags & DICT_TF_COMPACT));
301
/************************************************************************
302
Gets the number of fields in the internal representation of an index,
303
including fields added by the dictionary system. */
306
dict_index_get_n_fields(
307
/*====================*/
308
/* out: number of fields */
309
dict_index_t* index) /* in: an internal representation of index
310
(in the dictionary cache) */
313
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
315
return(index->n_fields);
318
/************************************************************************
319
Gets the number of fields in the internal representation of an index
320
that uniquely determine the position of an index entry in the index, if
321
we do not take multiversioning into account: in the B-tree use the value
322
returned by dict_index_get_n_unique_in_tree. */
325
dict_index_get_n_unique(
326
/*====================*/
327
/* out: number of fields */
328
dict_index_t* index) /* in: an internal representation of index
329
(in the dictionary cache) */
332
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
333
ut_ad(index->cached);
335
return(index->n_uniq);
338
/************************************************************************
339
Gets the number of fields in the internal representation of an index
340
which uniquely determine the position of an index entry in the index, if
341
we also take multiversioning into account. */
344
dict_index_get_n_unique_in_tree(
345
/*============================*/
346
/* out: number of fields */
347
dict_index_t* index) /* in: an internal representation of index
348
(in the dictionary cache) */
351
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
352
ut_ad(index->cached);
354
if (index->type & DICT_CLUSTERED) {
356
return(dict_index_get_n_unique(index));
359
return(dict_index_get_n_fields(index));
362
/************************************************************************
363
Gets the number of user-defined ordering fields in the index. In the internal
364
representation of clustered indexes we add the row id to the ordering fields
365
to make a clustered index unique, but this function returns the number of
366
fields the user defined in the index as ordering fields. */
369
dict_index_get_n_ordering_defined_by_user(
370
/*======================================*/
371
/* out: number of fields */
372
dict_index_t* index) /* in: an internal representation of index
373
(in the dictionary cache) */
375
return(index->n_user_defined_cols);
378
/************************************************************************
379
Gets the nth field of an index. */
382
dict_index_get_nth_field(
383
/*=====================*/
384
/* out: pointer to field object */
385
const dict_index_t* index, /* in: index */
386
ulint pos) /* in: position of field */
389
ut_ad(pos < index->n_def);
390
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
392
return((index->fields) + pos);
395
/************************************************************************
396
Returns the position of a system column in an index. */
399
dict_index_get_sys_col_pos(
400
/*=======================*/
401
/* out: position, ULINT_UNDEFINED if not
403
dict_index_t* index, /* in: index */
404
ulint type) /* in: DATA_ROW_ID, ... */
407
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
408
ut_ad(!(index->type & DICT_UNIVERSAL));
410
if (index->type & DICT_CLUSTERED) {
412
return(dict_col_get_clust_pos(
413
dict_table_get_sys_col(index->table, type),
417
return(dict_index_get_nth_col_pos(
418
index, dict_table_get_sys_col_no(index->table, type)));
421
/*************************************************************************
422
Gets the field column. */
427
const dict_field_t* field)
434
/************************************************************************
435
Gets pointer to the nth column in an index. */
438
dict_index_get_nth_col(
439
/*===================*/
441
const dict_index_t* index, /* in: index */
442
ulint pos) /* in: position of the field */
444
return(dict_field_get_col(dict_index_get_nth_field(index, pos)));
447
/************************************************************************
448
Gets the column number the nth field in an index. */
451
dict_index_get_nth_col_no(
452
/*======================*/
453
/* out: column number */
454
const dict_index_t* index, /* in: index */
455
ulint pos) /* in: position of the field */
457
return(dict_col_get_no(dict_index_get_nth_col(index, pos)));
460
/*************************************************************************
461
Gets the space id of the root of the index tree. */
464
dict_index_get_space(
465
/*=================*/
467
dict_index_t* index) /* in: index */
470
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
472
return(index->space);
475
/*************************************************************************
476
Sets the space id of the root of the index tree. */
479
dict_index_set_space(
480
/*=================*/
481
dict_index_t* index, /* in: index */
482
ulint space) /* in: space id */
485
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
487
index->space = space;
490
/*************************************************************************
491
Gets the page number of the root of the index tree. */
496
/* out: page number */
497
dict_index_t* index) /* in: index */
500
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
505
/*************************************************************************
506
Sets the page number of the root of index tree. */
511
dict_index_t* index, /* in: index */
512
ulint page) /* in: page number */
515
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
520
/*************************************************************************
521
Gets the type of the index tree. */
527
dict_index_t* index) /* in: index */
530
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
535
/*************************************************************************
536
Gets the read-write lock of the index tree. */
541
/* out: read-write lock */
542
dict_index_t* index) /* in: index */
545
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
547
return(&(index->lock));
550
/************************************************************************
551
Returns free space reserved for future updates of records. This is
552
relevant only in the case of many consecutive inserts, as updates
553
which make the records bigger might fragment the index. */
556
dict_index_get_space_reserve(void)
557
/*==============================*/
558
/* out: number of free bytes on page,
559
reserved for updates */
561
return(UNIV_PAGE_SIZE / 16);
564
/**************************************************************************
565
Checks if a table is in the dictionary cache. */
568
dict_table_check_if_in_cache_low(
569
/*=============================*/
570
/* out: table, NULL if not found */
571
const char* table_name) /* in: table name */
577
ut_ad(mutex_own(&(dict_sys->mutex)));
579
/* Look for the table name in the hash table */
580
table_fold = ut_fold_string(table_name);
582
HASH_SEARCH(name_hash, dict_sys->table_hash, table_fold, table,
583
ut_strcmp(table->name, table_name) == 0);
587
/**************************************************************************
588
Gets a table; loads it to the dictionary cache if necessary. A low-level
594
/* out: table, NULL if not found */
595
const char* table_name) /* in: table name */
600
ut_ad(mutex_own(&(dict_sys->mutex)));
602
table = dict_table_check_if_in_cache_low(table_name);
605
table = dict_load_table(table_name);
611
/**************************************************************************
612
Returns a table object based on table id. */
615
dict_table_get_on_id_low(
616
/*=====================*/
617
/* out: table, NULL if does not exist */
618
dulint table_id) /* in: table id */
623
ut_ad(mutex_own(&(dict_sys->mutex)));
625
/* Look for the table name in the hash table */
626
fold = ut_fold_dulint(table_id);
628
HASH_SEARCH(id_hash, dict_sys->table_id_hash, fold, table,
629
ut_dulint_cmp(table->id, table_id) == 0);
631
table = dict_load_table_on_id(table_id);
634
/* TODO: should get the type information from MySQL */
639
/**************************************************************************
640
Returns an index object. */
643
dict_table_get_index(
644
/*=================*/
645
/* out: index, NULL if does not exist */
646
dict_table_t* table, /* in: table */
647
const char* name) /* in: index name */
649
dict_index_t* index = NULL;
651
index = dict_table_get_first_index(table);
653
while (index != NULL) {
654
if (ut_strcmp(name, index->name) == 0) {
659
index = dict_table_get_next_index(index);