1
/*****************************************************************************
3
Copyright (C) 1996, 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/dict0dict.ic
21
Data dictionary system
23
Created 1/8/1996 Heikki Tuuri
24
***********************************************************************/
26
#include "data0type.h"
27
#ifndef UNIV_HOTBACKUP
28
#include "dict0load.h"
29
#include "rem0types.h"
31
/*********************************************************************//**
32
Gets the minimum number of bytes per character.
33
@return minimum multi-byte char size, in bytes */
36
dict_col_get_mbminlen(
37
/*==================*/
38
const dict_col_t* col) /*!< in: column */
40
return(DATA_MBMINLEN(col->mbminmaxlen));
42
/*********************************************************************//**
43
Gets the maximum number of bytes per character.
44
@return maximum multi-byte char size, in bytes */
47
dict_col_get_mbmaxlen(
48
/*==================*/
49
const dict_col_t* col) /*!< in: column */
51
return(DATA_MBMAXLEN(col->mbminmaxlen));
53
/*********************************************************************//**
54
Sets the minimum and maximum number of bytes per character. */
57
dict_col_set_mbminmaxlen(
58
/*=====================*/
59
dict_col_t* col, /*!< in/out: column */
60
ulint mbminlen, /*!< in: minimum multi-byte
61
character size, in bytes */
62
ulint mbmaxlen) /*!< in: minimum multi-byte
63
character size, in bytes */
65
ut_ad(mbminlen < DATA_MBMAX);
66
ut_ad(mbmaxlen < DATA_MBMAX);
67
ut_ad(mbminlen <= mbmaxlen);
69
col->mbminmaxlen = DATA_MBMINMAXLEN(mbminlen, mbmaxlen);
71
/*********************************************************************//**
72
Gets the column data type. */
77
const dict_col_t* col, /*!< in: column */
78
dtype_t* type) /*!< out: data type */
82
type->mtype = col->mtype;
83
type->prtype = col->prtype;
85
type->mbminmaxlen = col->mbminmaxlen;
87
#endif /* !UNIV_HOTBACKUP */
90
/*********************************************************************//**
91
Assert that a column and a data type match.
95
dict_col_type_assert_equal(
96
/*=======================*/
97
const dict_col_t* col, /*!< in: column */
98
const dtype_t* type) /*!< in: data type */
103
ut_ad(col->mtype == type->mtype);
104
ut_ad(col->prtype == type->prtype);
105
ut_ad(col->len == type->len);
106
# ifndef UNIV_HOTBACKUP
107
ut_ad(col->mbminmaxlen == type->mbminmaxlen);
108
# endif /* !UNIV_HOTBACKUP */
112
#endif /* UNIV_DEBUG */
114
#ifndef UNIV_HOTBACKUP
115
/***********************************************************************//**
116
Returns the minimum size of the column.
117
@return minimum size */
120
dict_col_get_min_size(
121
/*==================*/
122
const dict_col_t* col) /*!< in: column */
124
return(dtype_get_min_size_low(col->mtype, col->prtype, col->len,
127
/***********************************************************************//**
128
Returns the maximum size of the column.
129
@return maximum size */
132
dict_col_get_max_size(
133
/*==================*/
134
const dict_col_t* col) /*!< in: column */
136
return(dtype_get_max_size_low(col->mtype, col->len));
138
#endif /* !UNIV_HOTBACKUP */
139
/***********************************************************************//**
140
Returns the size of a fixed size column, 0 if not a fixed size column.
141
@return fixed size, or 0 */
144
dict_col_get_fixed_size(
145
/*====================*/
146
const dict_col_t* col, /*!< in: column */
147
ulint comp) /*!< in: nonzero=ROW_FORMAT=COMPACT */
149
return(dtype_get_fixed_size_low(col->mtype, col->prtype, col->len,
150
col->mbminmaxlen, comp));
152
/***********************************************************************//**
153
Returns the ROW_FORMAT=REDUNDANT stored SQL NULL size of a column.
154
For fixed length types it is the fixed length of the type, otherwise 0.
155
@return SQL null storage size in ROW_FORMAT=REDUNDANT */
158
dict_col_get_sql_null_size(
159
/*=======================*/
160
const dict_col_t* col, /*!< in: column */
161
ulint comp) /*!< in: nonzero=ROW_FORMAT=COMPACT */
163
return(dict_col_get_fixed_size(col, comp));
166
/*********************************************************************//**
167
Gets the column number.
168
@return col->ind, table column position (starting from 0) */
173
const dict_col_t* col) /*!< in: column */
180
/*********************************************************************//**
181
Gets the column position in the clustered index. */
184
dict_col_get_clust_pos(
185
/*===================*/
186
const dict_col_t* col, /*!< in: table column */
187
const dict_index_t* clust_index) /*!< in: clustered index */
193
ut_ad(dict_index_is_clust(clust_index));
195
for (i = 0; i < clust_index->n_def; i++) {
196
const dict_field_t* field = &clust_index->fields[i];
198
if (!field->prefix_len && field->col == col) {
203
return(ULINT_UNDEFINED);
206
#ifndef UNIV_HOTBACKUP
208
/********************************************************************//**
209
Gets the first index on the table (the clustered index).
210
@return index, NULL if none exists */
213
dict_table_get_first_index(
214
/*=======================*/
215
const dict_table_t* table) /*!< in: table */
218
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
220
return(UT_LIST_GET_FIRST(((dict_table_t*) table)->indexes));
223
/********************************************************************//**
224
Gets the next index on the table.
225
@return index, NULL if none left */
228
dict_table_get_next_index(
229
/*======================*/
230
const dict_index_t* index) /*!< in: index */
233
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
235
return(UT_LIST_GET_NEXT(indexes, (dict_index_t*) index));
237
#endif /* UNIV_DEBUG */
238
#endif /* !UNIV_HOTBACKUP */
240
/********************************************************************//**
241
Check whether the index is the clustered index.
242
@return nonzero for clustered index, zero for other indexes */
247
const dict_index_t* index) /*!< in: index */
250
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
252
return(UNIV_UNLIKELY(index->type & DICT_CLUSTERED));
254
/********************************************************************//**
255
Check whether the index is unique.
256
@return nonzero for unique index, zero for other indexes */
259
dict_index_is_unique(
260
/*=================*/
261
const dict_index_t* index) /*!< in: index */
264
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
266
return(UNIV_UNLIKELY(index->type & DICT_UNIQUE));
269
/********************************************************************//**
270
Check whether the index is the insert buffer tree.
271
@return nonzero for insert buffer, zero for other indexes */
276
const dict_index_t* index) /*!< in: index */
279
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
281
return(UNIV_UNLIKELY(index->type & DICT_IBUF));
284
/********************************************************************//**
285
Check whether the index is a secondary index or the insert buffer tree.
286
@return nonzero for insert buffer, zero for other indexes */
289
dict_index_is_sec_or_ibuf(
290
/*======================*/
291
const dict_index_t* index) /*!< in: index */
296
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
300
return(UNIV_LIKELY(!(type & DICT_CLUSTERED) || (type & DICT_IBUF)));
303
/********************************************************************//**
304
Gets the number of user-defined columns in a table in the dictionary
306
@return number of user-defined (e.g., not ROW_ID) columns of a table */
309
dict_table_get_n_user_cols(
310
/*=======================*/
311
const dict_table_t* table) /*!< in: table */
314
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
316
return(table->n_cols - DATA_N_SYS_COLS);
319
/********************************************************************//**
320
Gets the number of system columns in a table in the dictionary cache.
321
@return number of system (e.g., ROW_ID) columns of a table */
324
dict_table_get_n_sys_cols(
325
/*======================*/
326
const dict_table_t* table __attribute__((unused))) /*!< in: table */
330
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
331
ut_ad(table->cached);
333
return(DATA_N_SYS_COLS);
336
/********************************************************************//**
337
Gets the number of all columns (also system) in a table in the dictionary
339
@return number of columns of a table */
342
dict_table_get_n_cols(
343
/*==================*/
344
const dict_table_t* table) /*!< in: table */
347
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
349
return(table->n_cols);
353
/********************************************************************//**
354
Gets the nth column of a table.
355
@return pointer to column object */
358
dict_table_get_nth_col(
359
/*===================*/
360
const dict_table_t* table, /*!< in: table */
361
ulint pos) /*!< in: position of column */
364
ut_ad(pos < table->n_def);
365
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
367
return((dict_col_t*) (table->cols) + pos);
370
/********************************************************************//**
371
Gets the given system column of a table.
372
@return pointer to column object */
375
dict_table_get_sys_col(
376
/*===================*/
377
const dict_table_t* table, /*!< in: table */
378
ulint sys) /*!< in: DATA_ROW_ID, ... */
383
ut_ad(sys < DATA_N_SYS_COLS);
384
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
386
col = dict_table_get_nth_col(table, table->n_cols
387
- DATA_N_SYS_COLS + sys);
388
ut_ad(col->mtype == DATA_SYS);
389
ut_ad(col->prtype == (sys | DATA_NOT_NULL));
393
#endif /* UNIV_DEBUG */
395
/********************************************************************//**
396
Gets the given system column number of a table.
397
@return column number */
400
dict_table_get_sys_col_no(
401
/*======================*/
402
const dict_table_t* table, /*!< in: table */
403
ulint sys) /*!< in: DATA_ROW_ID, ... */
406
ut_ad(sys < DATA_N_SYS_COLS);
407
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
409
return(table->n_cols - DATA_N_SYS_COLS + sys);
412
/********************************************************************//**
413
Check whether the table uses the compact page format.
414
@return TRUE if table uses the compact page format */
419
const dict_table_t* table) /*!< in: table */
423
#if DICT_TF_COMPACT != TRUE
427
return(UNIV_LIKELY(table->flags & DICT_TF_COMPACT));
430
/********************************************************************//**
431
Determine the file format of a table.
432
@return file format version */
435
dict_table_get_format(
436
/*==================*/
437
const dict_table_t* table) /*!< in: table */
441
return((table->flags & DICT_TF_FORMAT_MASK) >> DICT_TF_FORMAT_SHIFT);
444
/********************************************************************//**
445
Determine the file format of a table. */
448
dict_table_set_format(
449
/*==================*/
450
dict_table_t* table, /*!< in/out: table */
451
ulint format) /*!< in: file format version */
455
table->flags = (table->flags & ~DICT_TF_FORMAT_MASK)
456
| (format << DICT_TF_FORMAT_SHIFT);
459
/********************************************************************//**
460
Extract the compressed page size from table flags.
461
@return compressed page size, or 0 if not compressed */
464
dict_table_flags_to_zip_size(
465
/*=========================*/
466
ulint flags) /*!< in: flags */
468
ulint zip_size = flags & DICT_TF_ZSSIZE_MASK;
470
if (UNIV_UNLIKELY(zip_size)) {
471
zip_size = ((PAGE_ZIP_MIN_SIZE >> 1)
472
<< (zip_size >> DICT_TF_ZSSIZE_SHIFT));
474
ut_ad(zip_size <= UNIV_PAGE_SIZE);
480
/********************************************************************//**
481
Check whether the table uses the compressed compact page format.
482
@return compressed page size, or 0 if not compressed */
487
const dict_table_t* table) /*!< in: table */
491
return(dict_table_flags_to_zip_size(table->flags));
494
/*********************************************************************//**
495
Obtain exclusive locks on all index trees of the table. This is to prevent
496
accessing index trees while InnoDB is updating internal metadata for
497
operations such as truncate tables. */
500
dict_table_x_lock_indexes(
501
/*======================*/
502
dict_table_t* table) /*!< in: table */
507
ut_ad(mutex_own(&(dict_sys->mutex)));
509
/* Loop through each index of the table and lock them */
510
for (index = dict_table_get_first_index(table);
512
index = dict_table_get_next_index(index)) {
513
rw_lock_x_lock(dict_index_get_lock(index));
517
/*********************************************************************//**
518
Release the exclusive locks on all index tree. */
521
dict_table_x_unlock_indexes(
522
/*========================*/
523
dict_table_t* table) /*!< in: table */
528
ut_ad(mutex_own(&(dict_sys->mutex)));
530
for (index = dict_table_get_first_index(table);
532
index = dict_table_get_next_index(index)) {
533
rw_lock_x_unlock(dict_index_get_lock(index));
536
/********************************************************************//**
537
Gets the number of fields in the internal representation of an index,
538
including fields added by the dictionary system.
539
@return number of fields */
542
dict_index_get_n_fields(
543
/*====================*/
544
const dict_index_t* index) /*!< in: an internal
545
representation of index (in
546
the dictionary cache) */
549
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
551
return(index->n_fields);
554
/********************************************************************//**
555
Gets the number of fields in the internal representation of an index
556
that uniquely determine the position of an index entry in the index, if
557
we do not take multiversioning into account: in the B-tree use the value
558
returned by dict_index_get_n_unique_in_tree.
559
@return number of fields */
562
dict_index_get_n_unique(
563
/*====================*/
564
const dict_index_t* index) /*!< in: an internal representation
565
of index (in the dictionary cache) */
568
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
569
ut_ad(index->cached);
571
return(index->n_uniq);
574
/********************************************************************//**
575
Gets the number of fields in the internal representation of an index
576
which uniquely determine the position of an index entry in the index, if
577
we also take multiversioning into account.
578
@return number of fields */
581
dict_index_get_n_unique_in_tree(
582
/*============================*/
583
const dict_index_t* index) /*!< in: an internal representation
584
of index (in the dictionary cache) */
587
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
588
ut_ad(index->cached);
590
if (dict_index_is_clust(index)) {
592
return(dict_index_get_n_unique(index));
595
return(dict_index_get_n_fields(index));
598
/********************************************************************//**
599
Gets the number of user-defined ordering fields in the index. In the internal
600
representation of clustered indexes we add the row id to the ordering fields
601
to make a clustered index unique, but this function returns the number of
602
fields the user defined in the index as ordering fields.
603
@return number of fields */
606
dict_index_get_n_ordering_defined_by_user(
607
/*======================================*/
608
const dict_index_t* index) /*!< in: an internal representation
609
of index (in the dictionary cache) */
611
return(index->n_user_defined_cols);
615
/********************************************************************//**
616
Gets the nth field of an index.
617
@return pointer to field object */
620
dict_index_get_nth_field(
621
/*=====================*/
622
const dict_index_t* index, /*!< in: index */
623
ulint pos) /*!< in: position of field */
626
ut_ad(pos < index->n_def);
627
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
629
return((dict_field_t*) (index->fields) + pos);
631
#endif /* UNIV_DEBUG */
633
/********************************************************************//**
634
Returns the position of a system column in an index.
635
@return position, ULINT_UNDEFINED if not contained */
638
dict_index_get_sys_col_pos(
639
/*=======================*/
640
const dict_index_t* index, /*!< in: index */
641
ulint type) /*!< in: DATA_ROW_ID, ... */
644
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
645
ut_ad(!(index->type & DICT_UNIVERSAL));
647
if (dict_index_is_clust(index)) {
649
return(dict_col_get_clust_pos(
650
dict_table_get_sys_col(index->table, type),
654
return(dict_index_get_nth_col_pos(
655
index, dict_table_get_sys_col_no(index->table, type)));
658
/*********************************************************************//**
659
Gets the field column.
660
@return field->col, pointer to the table column */
665
const dict_field_t* field) /*!< in: index field */
672
/********************************************************************//**
673
Gets pointer to the nth column in an index.
677
dict_index_get_nth_col(
678
/*===================*/
679
const dict_index_t* index, /*!< in: index */
680
ulint pos) /*!< in: position of the field */
682
return(dict_field_get_col(dict_index_get_nth_field(index, pos)));
685
/********************************************************************//**
686
Gets the column number the nth field in an index.
687
@return column number */
690
dict_index_get_nth_col_no(
691
/*======================*/
692
const dict_index_t* index, /*!< in: index */
693
ulint pos) /*!< in: position of the field */
695
return(dict_col_get_no(dict_index_get_nth_col(index, pos)));
698
#ifndef UNIV_HOTBACKUP
699
/********************************************************************//**
700
Returns the minimum data size of an index record.
701
@return minimum data size in bytes */
704
dict_index_get_min_size(
705
/*====================*/
706
const dict_index_t* index) /*!< in: index */
708
ulint n = dict_index_get_n_fields(index);
712
size += dict_col_get_min_size(dict_index_get_nth_col(index,
719
/*********************************************************************//**
720
Gets the space id of the root of the index tree.
724
dict_index_get_space(
725
/*=================*/
726
const dict_index_t* index) /*!< in: index */
729
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
731
return(index->space);
734
/*********************************************************************//**
735
Sets the space id of the root of the index tree. */
738
dict_index_set_space(
739
/*=================*/
740
dict_index_t* index, /*!< in/out: index */
741
ulint space) /*!< in: space id */
744
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
746
index->space = space;
749
/*********************************************************************//**
750
Gets the page number of the root of the index tree.
751
@return page number */
756
const dict_index_t* index) /*!< in: index */
759
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
764
/*********************************************************************//**
765
Sets the page number of the root of index tree. */
770
dict_index_t* index, /*!< in/out: index */
771
ulint page) /*!< in: page number */
774
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
779
/*********************************************************************//**
780
Gets the read-write lock of the index tree.
781
@return read-write lock */
786
dict_index_t* index) /*!< in: index */
789
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
791
return(&(index->lock));
794
/********************************************************************//**
795
Returns free space reserved for future updates of records. This is
796
relevant only in the case of many consecutive inserts, as updates
797
which make the records bigger might fragment the index.
798
@return number of free bytes on page, reserved for updates */
801
dict_index_get_space_reserve(void)
802
/*==============================*/
804
return(UNIV_PAGE_SIZE / 16);
807
/**********************************************************************//**
808
Checks if a table is in the dictionary cache.
809
@return table, NULL if not found */
812
dict_table_check_if_in_cache_low(
813
/*=============================*/
814
const char* table_name) /*!< in: table name */
820
ut_ad(mutex_own(&(dict_sys->mutex)));
822
/* Look for the table name in the hash table */
823
table_fold = ut_fold_string(table_name);
825
HASH_SEARCH(name_hash, dict_sys->table_hash, table_fold,
826
dict_table_t*, table, ut_ad(table->cached),
827
!strcmp(table->name, table_name));
831
/**********************************************************************//**
832
Gets a table; loads it to the dictionary cache if necessary. A low-level
834
@return table, NULL if not found */
839
const char* table_name) /*!< in: table name */
844
ut_ad(mutex_own(&(dict_sys->mutex)));
846
table = dict_table_check_if_in_cache_low(table_name);
849
table = dict_load_table(table_name, TRUE);
852
ut_ad(!table || table->cached);
857
/**********************************************************************//**
858
Returns a table object based on table id.
859
@return table, NULL if does not exist */
862
dict_table_get_on_id_low(
863
/*=====================*/
864
table_id_t table_id) /*!< in: table id */
869
ut_ad(mutex_own(&(dict_sys->mutex)));
871
/* Look for the table name in the hash table */
872
fold = ut_fold_ull(table_id);
874
HASH_SEARCH(id_hash, dict_sys->table_id_hash, fold,
875
dict_table_t*, table, ut_ad(table->cached),
876
table->id == table_id);
878
table = dict_load_table_on_id(table_id);
881
ut_ad(!table || table->cached);
883
/* TODO: should get the type information from MySQL */
887
#endif /* !UNIV_HOTBACKUP */