dict0dict.h 42.1 KB
Newer Older
vasil's avatar
vasil committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*****************************************************************************

Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

*****************************************************************************/

19 20
/**************************************************//**
@file include/dict0dict.h
osku's avatar
osku committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
Data dictionary system

Created 1/8/1996 Heikki Tuuri
*******************************************************/

#ifndef dict0dict_h
#define dict0dict_h

#include "univ.i"
#include "dict0types.h"
#include "dict0mem.h"
#include "data0type.h"
#include "data0data.h"
#include "mem0mem.h"
#include "rem0types.h"
#include "ut0mem.h"
#include "ut0lst.h"
#include "hash0hash.h"
#include "ut0rnd.h"
#include "ut0byte.h"
#include "trx0types.h"

43
#ifndef UNIV_HOTBACKUP
44 45
# include "sync0sync.h"
# include "sync0rw.h"
46
/******************************************************************//**
osku's avatar
osku committed
47
Makes all characters in a NUL-terminated UTF-8 string lower case. */
48
UNIV_INTERN
osku's avatar
osku committed
49 50 51
void
dict_casedn_str(
/*============*/
52
	char*	a);	/*!< in/out: string to put in lower case */
53
/********************************************************************//**
54 55
Get the database name length in a table name.
@return	database name length */
56
UNIV_INTERN
osku's avatar
osku committed
57 58 59
ulint
dict_get_db_name_len(
/*=================*/
60
	const char*	name);	/*!< in: table name in the form
osku's avatar
osku committed
61
				dbname '/' tablename */
62
/********************************************************************//**
63 64
Return the end of table name where we have removed dbname and '/'.
@return	table name */
osku's avatar
osku committed
65 66

const char*
67 68
dict_remove_db_name(
/*================*/
69
	const char*	name);	/*!< in: table name in the form
70
				dbname '/' tablename */
71
/**********************************************************************//**
72 73
Returns a table object based on table id.
@return	table, NULL if does not exist */
74
UNIV_INTERN
75 76 77
dict_table_t*
dict_table_get_on_id(
/*=================*/
78 79
        dulint  table_id,       /*!< in: table id */
        trx_t*  trx);           /*!< in: transaction handle */
80
/********************************************************************//**
osku's avatar
osku committed
81
Decrements the count of open MySQL handles to a table. */
82
UNIV_INTERN
osku's avatar
osku committed
83 84 85
void
dict_table_decrement_handle_count(
/*==============================*/
86 87
	dict_table_t*	table,		/*!< in/out: table */
	ibool		dict_locked);	/*!< in: TRUE=data dictionary locked */
88
/**********************************************************************//**
osku's avatar
osku committed
89
Inits the data dictionary module. */
90
UNIV_INTERN
osku's avatar
osku committed
91 92 93
void
dict_init(void);
/*===========*/
94
/********************************************************************//**
osku's avatar
osku committed
95 96 97 98 99
Gets the space id of every table of the data dictionary and makes a linear
list and a hash table of them to the data dictionary cache. This function
can be called at database startup if we did not need to do a crash recovery.
In crash recovery we must scan the space id's from the .ibd files in MySQL
database directories. */
100
UNIV_INTERN
osku's avatar
osku committed
101 102 103
void
dict_load_space_id_list(void);
/*=========================*/
104
/*********************************************************************//**
osku's avatar
osku committed
105 106
Gets the column data type. */
UNIV_INLINE
107 108 109
void
dict_col_copy_type(
/*===============*/
110 111
	const dict_col_t*	col,	/*!< in: column */
	dtype_t*		type);	/*!< out: data type */
112
#endif /* !UNIV_HOTBACKUP */
113
#ifdef UNIV_DEBUG
114
/*********************************************************************//**
115 116
Assert that a column and a data type match.
@return	TRUE */
117 118 119 120
UNIV_INLINE
ibool
dict_col_type_assert_equal(
/*=======================*/
121 122
	const dict_col_t*	col,	/*!< in: column */
	const dtype_t*		type);	/*!< in: data type */
123
#endif /* UNIV_DEBUG */
124
#ifndef UNIV_HOTBACKUP
125
/***********************************************************************//**
126 127
Returns the minimum size of the column.
@return	minimum size */
128 129 130 131
UNIV_INLINE
ulint
dict_col_get_min_size(
/*==================*/
132
	const dict_col_t*	col);	/*!< in: column */
133
/***********************************************************************//**
134 135
Returns the maximum size of the column.
@return	maximum size */
136 137 138 139
UNIV_INLINE
ulint
dict_col_get_max_size(
/*==================*/
140
	const dict_col_t*	col);	/*!< in: column */
141
/***********************************************************************//**
142 143
Returns the size of a fixed size column, 0 if not a fixed size column.
@return	fixed size, or 0 */
144 145 146 147
UNIV_INLINE
ulint
dict_col_get_fixed_size(
/*====================*/
148 149
	const dict_col_t*	col,	/*!< in: column */
	ulint			comp);	/*!< in: nonzero=ROW_FORMAT=COMPACT  */
150
/***********************************************************************//**
151
Returns the ROW_FORMAT=REDUNDANT stored SQL NULL size of a column.
152 153
For fixed length types it is the fixed length of the type, otherwise 0.
@return	SQL null storage size in ROW_FORMAT=REDUNDANT */
154 155 156 157
UNIV_INLINE
ulint
dict_col_get_sql_null_size(
/*=======================*/
158 159
	const dict_col_t*	col,	/*!< in: column */
	ulint			comp);	/*!< in: nonzero=ROW_FORMAT=COMPACT  */
160

161
/*********************************************************************//**
162 163
Gets the column number.
@return	col->ind, table column position (starting from 0) */
osku's avatar
osku committed
164 165 166 167
UNIV_INLINE
ulint
dict_col_get_no(
/*============*/
168
	const dict_col_t*	col);	/*!< in: column */
169
/*********************************************************************//**
osku's avatar
osku committed
170 171 172 173 174
Gets the column position in the clustered index. */
UNIV_INLINE
ulint
dict_col_get_clust_pos(
/*===================*/
175 176
	const dict_col_t*	col,		/*!< in: table column */
	const dict_index_t*	clust_index);	/*!< in: clustered index */
177
/****************************************************************//**
178
If the given column name is reserved for InnoDB system columns, return
179 180
TRUE.
@return	TRUE if name is reserved */
181
UNIV_INTERN
182 183 184
ibool
dict_col_name_is_reserved(
/*======================*/
185
	const char*	name);	/*!< in: column name */
186
/********************************************************************//**
187
Acquire the autoinc lock. */
188
UNIV_INTERN
189 190 191
void
dict_table_autoinc_lock(
/*====================*/
192
	dict_table_t*	table);	/*!< in/out: table */
193
/********************************************************************//**
194
Unconditionally set the autoinc counter. */
195
UNIV_INTERN
osku's avatar
osku committed
196 197 198
void
dict_table_autoinc_initialize(
/*==========================*/
199 200
	dict_table_t*	table,	/*!< in/out: table */
	ib_uint64_t	value);	/*!< in: next value to assign to a row */
201
/********************************************************************//**
osku's avatar
osku committed
202
Reads the next autoinc value (== autoinc counter value), 0 if not yet
203 204
initialized.
@return	value for a new row, or 0 */
205
UNIV_INTERN
206
ib_uint64_t
osku's avatar
osku committed
207 208
dict_table_autoinc_read(
/*====================*/
209
	const dict_table_t*	table);	/*!< in: table */
210
/********************************************************************//**
sunny's avatar
sunny committed
211 212
Updates the autoinc counter if the value supplied is greater than the
current value. */
213
UNIV_INTERN
osku's avatar
osku committed
214
void
215 216
dict_table_autoinc_update_if_greater(
/*=================================*/
osku's avatar
osku committed
217

218 219
	dict_table_t*	table,	/*!< in/out: table */
	ib_uint64_t	value);	/*!< in: value which was assigned to a row */
220
/********************************************************************//**
221
Release the autoinc lock. */
222
UNIV_INTERN
223 224 225
void
dict_table_autoinc_unlock(
/*======================*/
226
	dict_table_t*	table);	/*!< in/out: table */
227
#endif /* !UNIV_HOTBACKUP */
228
/**********************************************************************//**
229
Adds system columns to a table object. */
230
UNIV_INTERN
231 232 233
void
dict_table_add_system_columns(
/*==========================*/
234 235
	dict_table_t*	table,	/*!< in/out: table */
	mem_heap_t*	heap);	/*!< in: temporary heap */
236
#ifndef UNIV_HOTBACKUP
237
/**********************************************************************//**
osku's avatar
osku committed
238
Adds a table object to the dictionary cache. */
239
UNIV_INTERN
osku's avatar
osku committed
240 241 242
void
dict_table_add_to_cache(
/*====================*/
243 244
	dict_table_t*	table,	/*!< in: table */
	mem_heap_t*	heap);	/*!< in: temporary heap */
245
/**********************************************************************//**
osku's avatar
osku committed
246
Removes a table object from the dictionary cache. */
247
UNIV_INTERN
osku's avatar
osku committed
248 249 250
void
dict_table_remove_from_cache(
/*=========================*/
251
	dict_table_t*	table);	/*!< in, own: table */
252
/**********************************************************************//**
253 254
Renames a table object.
@return	TRUE if success */
255
UNIV_INTERN
osku's avatar
osku committed
256 257 258
ibool
dict_table_rename_in_cache(
/*=======================*/
259 260 261
	dict_table_t*	table,		/*!< in/out: table */
	const char*	new_name,	/*!< in: new name */
	ibool		rename_also_foreigns);/*!< in: in ALTER TABLE we want
osku's avatar
osku committed
262 263
					to preserve the original table name
					in constraints which reference it */
264
/**********************************************************************//**
265
Removes an index from the dictionary cache. */
266
UNIV_INTERN
267 268 269
void
dict_index_remove_from_cache(
/*=========================*/
270 271
	dict_table_t*	table,	/*!< in/out: table */
	dict_index_t*	index);	/*!< in, own: index */
272
/**********************************************************************//**
osku's avatar
osku committed
273 274
Change the id of a table object in the dictionary cache. This is used in
DISCARD TABLESPACE. */
275
UNIV_INTERN
osku's avatar
osku committed
276 277 278
void
dict_table_change_id_in_cache(
/*==========================*/
279 280
	dict_table_t*	table,	/*!< in/out: table object already in cache */
	dulint		new_id);/*!< in: new id to set */
281
/**********************************************************************//**
osku's avatar
osku committed
282 283 284
Adds a foreign key constraint object to the dictionary cache. May free
the object if there already is an object with the same identifier in.
At least one of foreign table or referenced table must already be in
285 286
the dictionary cache!
@return	DB_SUCCESS or error code */
287
UNIV_INTERN
osku's avatar
osku committed
288 289 290
ulint
dict_foreign_add_to_cache(
/*======================*/
291 292
	dict_foreign_t*	foreign,	/*!< in, own: foreign key constraint */
	ibool		check_charsets);/*!< in: TRUE=check charset
293
					compatibility */
294
/*********************************************************************//**
295
Check if the index is referenced by a foreign key, if TRUE return the
296
matching instance NULL otherwise.
297 298
@return pointer to foreign key struct if index is defined for foreign
key, otherwise NULL */
299
UNIV_INTERN
300 301 302
dict_foreign_t*
dict_table_get_referenced_constraint(
/*=================================*/
303 304
	dict_table_t*	table,	/*!< in: InnoDB table */
	dict_index_t*	index);	/*!< in: InnoDB index */
305
/*********************************************************************//**
306 307
Checks if a table is referenced by foreign keys.
@return	TRUE if table is referenced by a foreign key */
308
UNIV_INTERN
osku's avatar
osku committed
309
ibool
310 311
dict_table_is_referenced_by_foreign_key(
/*====================================*/
312
	const dict_table_t*	table);	/*!< in: InnoDB table */
313
/**********************************************************************//**
314 315
Replace the index in the foreign key list that matches this index's
definition with an equivalent index. */
316
UNIV_INTERN
317 318 319
void
dict_table_replace_index_in_foreign_list(
/*=====================================*/
320 321
	dict_table_t*	table,  /*!< in/out: table */
	dict_index_t*	index);	/*!< in: index to be replaced */
322
/*********************************************************************//**
323 324
Checks if a index is defined for a foreign key constraint. Index is a part
of a foreign key constraint if the index is referenced by foreign key
325
or index is a foreign key index
326 327
@return pointer to foreign key struct if index is defined for foreign
key, otherwise NULL */
328
UNIV_INTERN
329 330 331
dict_foreign_t*
dict_table_get_foreign_constraint(
/*==============================*/
332 333
	dict_table_t*	table,	/*!< in: InnoDB table */
	dict_index_t*	index);	/*!< in: InnoDB index */
334
/*********************************************************************//**
osku's avatar
osku committed
335 336 337 338 339
Scans a table create SQL string and adds to the data dictionary
the foreign key constraints declared in the string. This function
should be called after the indexes for a table have been created.
Each foreign key constraint must be accompanied with indexes in
bot participating tables. The indexes are allowed to contain more
340 341
fields than mentioned in the constraint.
@return	error code or DB_SUCCESS */
342
UNIV_INTERN
osku's avatar
osku committed
343 344 345
ulint
dict_create_foreign_constraints(
/*============================*/
346 347
	trx_t*		trx,		/*!< in: transaction */
	const char*	sql_string,	/*!< in: table create statement where
osku's avatar
osku committed
348 349 350 351 352 353 354
					foreign keys are declared like:
					FOREIGN KEY (a, b) REFERENCES
					table2(c, d), table2 can be written
					also with the database
					name before it: test.table2; the
					default database id the database of
					parameter name */
355
	const char*	name,		/*!< in: table full name in the
osku's avatar
osku committed
356 357
					normalized form
					database_name/table_name */
358
	ibool		reject_fks);	/*!< in: if TRUE, fail with error
osku's avatar
osku committed
359 360
					code DB_CANNOT_ADD_CONSTRAINT if
					any foreign keys are found. */
361
/**********************************************************************//**
362
Parses the CONSTRAINT id's to be dropped in an ALTER TABLE statement.
363 364
@return DB_SUCCESS or DB_CANNOT_DROP_CONSTRAINT if syntax error or the
constraint id does not match */
365
UNIV_INTERN
osku's avatar
osku committed
366 367 368
ulint
dict_foreign_parse_drop_constraints(
/*================================*/
369
	mem_heap_t*	heap,			/*!< in: heap from which we can
osku's avatar
osku committed
370
						allocate memory */
371 372 373
	trx_t*		trx,			/*!< in: transaction */
	dict_table_t*	table,			/*!< in: table */
	ulint*		n,			/*!< out: number of constraints
osku's avatar
osku committed
374
						to drop */
375
	const char***	constraints_to_drop);	/*!< out: id's of the
osku's avatar
osku committed
376
						constraints to drop */
377
/**********************************************************************//**
378 379 380
Returns a table object and optionally increment its MySQL open handle count.
NOTE! This is a high-level function to be used mainly from outside the
'dict' directory. Inside this directory dict_table_get_low is usually the
381 382
appropriate function.
@return	table, NULL if does not exist */
383
UNIV_INTERN
osku's avatar
osku committed
384 385 386
dict_table_t*
dict_table_get(
/*===========*/
387
	const char*	table_name,	/*!< in: table name */
388
	ibool		inc_mysql_count);
389
					/*!< in: whether to increment the open
390
					handle count on the table */
391
/**********************************************************************//**
392 393
Returns a index object, based on table and index id, and memoryfixes it.
@return	index, NULL if does not exist */
394
UNIV_INTERN
395 396
dict_index_t*
dict_index_get_on_id_low(
osku's avatar
osku committed
397
/*=====================*/
398 399
	dict_table_t*	table,		/*!< in: table */
	dulint		index_id);	/*!< in: index id */
400
/**********************************************************************//**
401 402
Checks if a table is in the dictionary cache.
@return	table, NULL if not found */
403

osku's avatar
osku committed
404 405 406
UNIV_INLINE
dict_table_t*
dict_table_check_if_in_cache_low(
407
/*=============================*/
408
	const char*	table_name);	/*!< in: table name */
409
/**********************************************************************//**
osku's avatar
osku committed
410
Gets a table; loads it to the dictionary cache if necessary. A low-level
411 412
function.
@return	table, NULL if not found */
osku's avatar
osku committed
413 414 415 416
UNIV_INLINE
dict_table_t*
dict_table_get_low(
/*===============*/
417
	const char*	table_name);	/*!< in: table name */
418
/**********************************************************************//**
419 420
Returns a table object based on table id.
@return	table, NULL if does not exist */
421 422 423 424
UNIV_INLINE
dict_table_t*
dict_table_get_on_id_low(
/*=====================*/
425
	dulint	table_id);	/*!< in: table id */
426
/**********************************************************************//**
427
Find an index that is equivalent to the one passed in and is not marked
428 429
for deletion.
@return	index equivalent to foreign->foreign_index, or NULL */
430 431 432 433
UNIV_INTERN
dict_index_t*
dict_foreign_find_equiv_index(
/*==========================*/
434
	dict_foreign_t*	foreign);/*!< in: foreign key */
435
/**********************************************************************//**
436
Returns an index object by matching on the name and column names and
437 438
if more than one index matches return the index with the max id
@return	matching index, NULL if not found */
439
UNIV_INTERN
osku's avatar
osku committed
440
dict_index_t*
441
dict_table_get_index_by_max_id(
osku's avatar
osku committed
442
/*===========================*/
443 444 445 446
	dict_table_t*	table,	/*!< in: table */
	const char*	name,	/*!< in: the index name to find */
	const char**	columns,/*!< in: array of column names */
	ulint		n_cols);/*!< in: number of columns */
447
/**********************************************************************//**
448
Returns a column's name.
449 450 451
@return column name. NOTE: not guaranteed to stay valid if table is
modified in any way (columns added, etc.). */
UNIV_INTERN
452 453 454
const char*
dict_table_get_col_name(
/*====================*/
455 456
	const dict_table_t*	table,	/*!< in: table */
	ulint			col_nr);/*!< in: column number */
457

458
/**********************************************************************//**
osku's avatar
osku committed
459
Prints a table definition. */
460
UNIV_INTERN
osku's avatar
osku committed
461 462 463
void
dict_table_print(
/*=============*/
464
	dict_table_t*	table);	/*!< in: table */
465
/**********************************************************************//**
osku's avatar
osku committed
466
Prints a table data. */
467
UNIV_INTERN
osku's avatar
osku committed
468 469 470
void
dict_table_print_low(
/*=================*/
471
	dict_table_t*	table);	/*!< in: table */
472
/**********************************************************************//**
osku's avatar
osku committed
473
Prints a table data when we know the table name. */
474
UNIV_INTERN
osku's avatar
osku committed
475 476 477
void
dict_table_print_by_name(
/*=====================*/
478
	const char*	name);	/*!< in: table name */
479
/**********************************************************************//**
osku's avatar
osku committed
480
Outputs info on foreign keys of a table. */
481
UNIV_INTERN
osku's avatar
osku committed
482 483 484
void
dict_print_info_on_foreign_keys(
/*============================*/
485
	ibool		create_table_format, /*!< in: if TRUE then print in
osku's avatar
osku committed
486 487 488
				a format suitable to be inserted into
				a CREATE TABLE, otherwise in the format
				of SHOW TABLE STATUS */
489 490 491
	FILE*		file,	/*!< in: file where to print */
	trx_t*		trx,	/*!< in: transaction */
	dict_table_t*	table);	/*!< in: table */
492
/**********************************************************************//**
osku's avatar
osku committed
493 494
Outputs info on a foreign key of a table in a format suitable for
CREATE TABLE. */
495
UNIV_INTERN
osku's avatar
osku committed
496 497 498
void
dict_print_info_on_foreign_key_in_create_format(
/*============================================*/
499 500 501 502
	FILE*		file,		/*!< in: file where to print */
	trx_t*		trx,		/*!< in: transaction */
	dict_foreign_t*	foreign,	/*!< in: foreign key constraint */
	ibool		add_newline);	/*!< in: whether to add a newline */
503
/********************************************************************//**
osku's avatar
osku committed
504
Displays the names of the index and the table. */
505
UNIV_INTERN
osku's avatar
osku committed
506 507 508
void
dict_index_name_print(
/*==================*/
509 510 511
	FILE*			file,	/*!< in: output stream */
	trx_t*			trx,	/*!< in: transaction */
	const dict_index_t*	index);	/*!< in: index to print */
512
#ifdef UNIV_DEBUG
513
/********************************************************************//**
514 515
Gets the first index on the table (the clustered index).
@return	index, NULL if none exists */
osku's avatar
osku committed
516 517 518 519
UNIV_INLINE
dict_index_t*
dict_table_get_first_index(
/*=======================*/
520
	const dict_table_t*	table);	/*!< in: table */
521
/********************************************************************//**
522 523
Gets the next index on the table.
@return	index, NULL if none left */
osku's avatar
osku committed
524 525 526 527
UNIV_INLINE
dict_index_t*
dict_table_get_next_index(
/*======================*/
528
	const dict_index_t*	index);	/*!< in: index */
529 530 531 532
#else /* UNIV_DEBUG */
# define dict_table_get_first_index(table) UT_LIST_GET_FIRST((table)->indexes)
# define dict_table_get_next_index(index) UT_LIST_GET_NEXT(indexes, index)
#endif /* UNIV_DEBUG */
533
#endif /* !UNIV_HOTBACKUP */
534
/********************************************************************//**
535 536
Check whether the index is the clustered index.
@return	nonzero for clustered index, zero for other indexes */
537 538 539 540
UNIV_INLINE
ulint
dict_index_is_clust(
/*================*/
541
	const dict_index_t*	index)	/*!< in: index */
542
	__attribute__((pure));
543
/********************************************************************//**
544 545
Check whether the index is unique.
@return	nonzero for unique index, zero for other indexes */
546 547 548 549
UNIV_INLINE
ulint
dict_index_is_unique(
/*=================*/
550
	const dict_index_t*	index)	/*!< in: index */
551
	__attribute__((pure));
552
/********************************************************************//**
553 554
Check whether the index is the insert buffer tree.
@return	nonzero for insert buffer, zero for other indexes */
555 556 557 558
UNIV_INLINE
ulint
dict_index_is_ibuf(
/*===============*/
559
	const dict_index_t*	index)	/*!< in: index */
560
	__attribute__((pure));
561
/********************************************************************//**
562 563
Check whether the index is a secondary index or the insert buffer tree.
@return	nonzero for insert buffer, zero for other indexes */
564 565 566 567
UNIV_INLINE
ulint
dict_index_is_sec_or_ibuf(
/*======================*/
568
	const dict_index_t*	index)	/*!< in: index */
569
	__attribute__((pure));
570

571
/********************************************************************//**
osku's avatar
osku committed
572
Gets the number of user-defined columns in a table in the dictionary
573 574
cache.
@return	number of user-defined (e.g., not ROW_ID) columns of a table */
osku's avatar
osku committed
575 576 577 578
UNIV_INLINE
ulint
dict_table_get_n_user_cols(
/*=======================*/
579
	const dict_table_t*	table);	/*!< in: table */
580
/********************************************************************//**
581 582
Gets the number of system columns in a table in the dictionary cache.
@return	number of system (e.g., ROW_ID) columns of a table */
osku's avatar
osku committed
583 584 585 586
UNIV_INLINE
ulint
dict_table_get_n_sys_cols(
/*======================*/
587
	const dict_table_t*	table);	/*!< in: table */
588
/********************************************************************//**
osku's avatar
osku committed
589
Gets the number of all columns (also system) in a table in the dictionary
590 591
cache.
@return	number of columns of a table */
osku's avatar
osku committed
592 593 594 595
UNIV_INLINE
ulint
dict_table_get_n_cols(
/*==================*/
596
	const dict_table_t*	table);	/*!< in: table */
597
#ifdef UNIV_DEBUG
598
/********************************************************************//**
599 600
Gets the nth column of a table.
@return	pointer to column object */
osku's avatar
osku committed
601
UNIV_INLINE
602
dict_col_t*
osku's avatar
osku committed
603 604
dict_table_get_nth_col(
/*===================*/
605 606
	const dict_table_t*	table,	/*!< in: table */
	ulint			pos);	/*!< in: position of column */
607
/********************************************************************//**
608 609
Gets the given system column of a table.
@return	pointer to column object */
610 611 612 613
UNIV_INLINE
dict_col_t*
dict_table_get_sys_col(
/*===================*/
614 615
	const dict_table_t*	table,	/*!< in: table */
	ulint			sys);	/*!< in: DATA_ROW_ID, ... */
616 617 618 619 620 621
#else /* UNIV_DEBUG */
#define dict_table_get_nth_col(table, pos) \
((table)->cols + (pos))
#define dict_table_get_sys_col(table, sys) \
((table)->cols + (table)->n_cols + (sys) - DATA_N_SYS_COLS)
#endif /* UNIV_DEBUG */
622
/********************************************************************//**
623 624
Gets the given system column number of a table.
@return	column number */
osku's avatar
osku committed
625 626 627 628
UNIV_INLINE
ulint
dict_table_get_sys_col_no(
/*======================*/
629 630
	const dict_table_t*	table,	/*!< in: table */
	ulint			sys);	/*!< in: DATA_ROW_ID, ... */
631
#ifndef UNIV_HOTBACKUP
632
/********************************************************************//**
633 634
Returns the minimum data size of an index record.
@return	minimum data size in bytes */
635 636 637 638
UNIV_INLINE
ulint
dict_index_get_min_size(
/*====================*/
639
	const dict_index_t*	index);	/*!< in: index */
640
#endif /* !UNIV_HOTBACKUP */
641
/********************************************************************//**
642 643
Check whether the table uses the compact page format.
@return	TRUE if table uses the compact page format */
644 645 646 647
UNIV_INLINE
ibool
dict_table_is_comp(
/*===============*/
648
	const dict_table_t*	table);	/*!< in: table */
649
/********************************************************************//**
650 651
Determine the file format of a table.
@return	file format version */
652 653 654 655
UNIV_INLINE
ulint
dict_table_get_format(
/*==================*/
656
	const dict_table_t*	table);	/*!< in: table */
657
/********************************************************************//**
658 659 660 661 662
Set the file format of a table. */
UNIV_INLINE
void
dict_table_set_format(
/*==================*/
663 664
	dict_table_t*	table,	/*!< in/out: table */
	ulint		format);/*!< in: file format version */
665
/********************************************************************//**
666 667
Extract the compressed page size from table flags.
@return	compressed page size, or 0 if not compressed */
668 669 670 671
UNIV_INLINE
ulint
dict_table_flags_to_zip_size(
/*=========================*/
672
	ulint	flags)	/*!< in: flags */
673
	__attribute__((const));
674
/********************************************************************//**
675 676
Check whether the table uses the compressed compact page format.
@return	compressed page size, or 0 if not compressed */
677
UNIV_INLINE
678 679 680
ulint
dict_table_zip_size(
/*================*/
681
	const dict_table_t*	table);	/*!< in: table */
682
/********************************************************************//**
osku's avatar
osku committed
683
Checks if a column is in the ordering columns of the clustered index of a
684 685
table. Column prefixes are treated like whole columns.
@return	TRUE if the column, or its prefix, is in the clustered key */
686
UNIV_INTERN
osku's avatar
osku committed
687 688 689
ibool
dict_table_col_in_clustered_key(
/*============================*/
690 691
	const dict_table_t*	table,	/*!< in: table */
	ulint			n);	/*!< in: column number */
692
#ifndef UNIV_HOTBACKUP
693
/*******************************************************************//**
694 695 696
Copies types of columns contained in table to tuple and sets all
fields of the tuple to the SQL NULL value.  This function should
be called right after dtuple_create(). */
697
UNIV_INTERN
osku's avatar
osku committed
698 699 700
void
dict_table_copy_types(
/*==================*/
701 702
	dtuple_t*		tuple,	/*!< in/out: data tuple */
	const dict_table_t*	table);	/*!< in: table */
703
/**********************************************************************//**
osku's avatar
osku committed
704 705
Looks for an index with the given id. NOTE that we do not reserve
the dictionary mutex: this function is for emergency purposes like
706 707
printing info of a corrupt database page!
@return	index or NULL if not found from cache */
708
UNIV_INTERN
osku's avatar
osku committed
709 710 711
dict_index_t*
dict_index_find_on_id_low(
/*======================*/
712
	dulint	id);	/*!< in: index id */
713
/**********************************************************************//**
714
Adds an index to the dictionary cache.
715
@return	DB_SUCCESS, DB_TOO_BIG_RECORD, or DB_CORRUPTION */
716
UNIV_INTERN
717
ulint
osku's avatar
osku committed
718 719
dict_index_add_to_cache(
/*====================*/
720 721
	dict_table_t*	table,	/*!< in: table on which the index is */
	dict_index_t*	index,	/*!< in, own: index; NOTE! The index memory
osku's avatar
osku committed
722
				object is freed in this function! */
723 724
	ulint		page_no,/*!< in: root page number of the index */
	ibool		strict);/*!< in: TRUE=refuse to create the index
725 726
				if records could be too big to fit in
				an B-tree page */
727
/**********************************************************************//**
728
Removes an index from the dictionary cache. */
729
UNIV_INTERN
730 731 732
void
dict_index_remove_from_cache(
/*=========================*/
733 734
	dict_table_t*	table,	/*!< in/out: table */
	dict_index_t*	index);	/*!< in, own: index */
735
#endif /* !UNIV_HOTBACKUP */
736
/********************************************************************//**
osku's avatar
osku committed
737
Gets the number of fields in the internal representation of an index,
738 739
including fields added by the dictionary system.
@return	number of fields */
osku's avatar
osku committed
740 741 742 743
UNIV_INLINE
ulint
dict_index_get_n_fields(
/*====================*/
744
	const dict_index_t*	index);	/*!< in: an internal
745 746
					representation of index (in
					the dictionary cache) */
747
/********************************************************************//**
osku's avatar
osku committed
748 749 750
Gets the number of fields in the internal representation of an index
that uniquely determine the position of an index entry in the index, if
we do not take multiversioning into account: in the B-tree use the value
751 752
returned by dict_index_get_n_unique_in_tree.
@return	number of fields */
osku's avatar
osku committed
753 754 755 756
UNIV_INLINE
ulint
dict_index_get_n_unique(
/*====================*/
757
	const dict_index_t*	index);	/*!< in: an internal representation
758
					of index (in the dictionary cache) */
759
/********************************************************************//**
osku's avatar
osku committed
760 761
Gets the number of fields in the internal representation of an index
which uniquely determine the position of an index entry in the index, if
762 763
we also take multiversioning into account.
@return	number of fields */
osku's avatar
osku committed
764 765 766 767
UNIV_INLINE
ulint
dict_index_get_n_unique_in_tree(
/*============================*/
768
	const dict_index_t*	index);	/*!< in: an internal representation
769
					of index (in the dictionary cache) */
770
/********************************************************************//**
osku's avatar
osku committed
771 772 773
Gets the number of user-defined ordering fields in the index. In the internal
representation we add the row id to the ordering fields to make all indexes
unique, but this function returns the number of fields the user defined
774 775
in the index as ordering fields.
@return	number of fields */
osku's avatar
osku committed
776 777 778 779
UNIV_INLINE
ulint
dict_index_get_n_ordering_defined_by_user(
/*======================================*/
780
	const dict_index_t*	index);	/*!< in: an internal representation
781
					of index (in the dictionary cache) */
782
#ifdef UNIV_DEBUG
783
/********************************************************************//**
784 785
Gets the nth field of an index.
@return	pointer to field object */
osku's avatar
osku committed
786 787 788 789
UNIV_INLINE
dict_field_t*
dict_index_get_nth_field(
/*=====================*/
790 791
	const dict_index_t*	index,	/*!< in: index */
	ulint			pos);	/*!< in: position of field */
792 793 794
#else /* UNIV_DEBUG */
# define dict_index_get_nth_field(index, pos) ((index)->fields + (pos))
#endif /* UNIV_DEBUG */
795
/********************************************************************//**
796 797
Gets pointer to the nth column in an index.
@return	column */
osku's avatar
osku committed
798
UNIV_INLINE
799 800 801
const dict_col_t*
dict_index_get_nth_col(
/*===================*/
802 803
	const dict_index_t*	index,	/*!< in: index */
	ulint			pos);	/*!< in: position of the field */
804
/********************************************************************//**
805 806
Gets the column number of the nth field in an index.
@return	column number */
osku's avatar
osku committed
807 808 809 810
UNIV_INLINE
ulint
dict_index_get_nth_col_no(
/*======================*/
811 812
	const dict_index_t*	index,	/*!< in: index */
	ulint			pos);	/*!< in: position of the field */
813
/********************************************************************//**
814
Looks for column n in an index.
815 816
@return position in internal representation of the index;
ULINT_UNDEFINED if not contained */
817
UNIV_INTERN
osku's avatar
osku committed
818 819 820
ulint
dict_index_get_nth_col_pos(
/*=======================*/
821 822
	const dict_index_t*	index,	/*!< in: index */
	ulint			n);	/*!< in: column number */
823
/********************************************************************//**
824 825
Returns TRUE if the index contains a column or a prefix of that column.
@return	TRUE if contains the column or its prefix */
826
UNIV_INTERN
osku's avatar
osku committed
827 828 829
ibool
dict_index_contains_col_or_prefix(
/*==============================*/
830 831
	const dict_index_t*	index,	/*!< in: index */
	ulint			n);	/*!< in: column number */
832
/********************************************************************//**
osku's avatar
osku committed
833 834 835
Looks for a matching field in an index. The column has to be the same. The
column in index must be complete, or must contain a prefix longer than the
column in index2. That is, we must be able to construct the prefix in index2
836
from the prefix in index.
837 838
@return position in internal representation of the index;
ULINT_UNDEFINED if not contained */
839
UNIV_INTERN
osku's avatar
osku committed
840 841 842
ulint
dict_index_get_nth_field_pos(
/*=========================*/
843 844 845
	const dict_index_t*	index,	/*!< in: index from which to search */
	const dict_index_t*	index2,	/*!< in: index */
	ulint			n);	/*!< in: field number in index2 */
846
/********************************************************************//**
847 848
Looks for column n position in the clustered index.
@return	position in internal representation of the clustered index */
849
UNIV_INTERN
osku's avatar
osku committed
850 851 852
ulint
dict_table_get_nth_col_pos(
/*=======================*/
853 854
	const dict_table_t*	table,	/*!< in: table */
	ulint			n);	/*!< in: column number */
855
/********************************************************************//**
856 857
Returns the position of a system column in an index.
@return	position, ULINT_UNDEFINED if not contained */
osku's avatar
osku committed
858 859 860 861
UNIV_INLINE
ulint
dict_index_get_sys_col_pos(
/*=======================*/
862 863
	const dict_index_t*	index,	/*!< in: index */
	ulint			type);	/*!< in: DATA_ROW_ID, ... */
864
/*******************************************************************//**
osku's avatar
osku committed
865
Adds a column to index. */
866
UNIV_INTERN
osku's avatar
osku committed
867 868 869
void
dict_index_add_col(
/*===============*/
870 871 872 873
	dict_index_t*		index,		/*!< in/out: index */
	const dict_table_t*	table,		/*!< in: table */
	dict_col_t*		col,		/*!< in: column */
	ulint			prefix_len);	/*!< in: column prefix length */
874
#ifndef UNIV_HOTBACKUP
875
/*******************************************************************//**
osku's avatar
osku committed
876
Copies types of fields contained in index to tuple. */
877
UNIV_INTERN
osku's avatar
osku committed
878 879 880
void
dict_index_copy_types(
/*==================*/
881 882 883
	dtuple_t*		tuple,		/*!< in/out: data tuple */
	const dict_index_t*	index,		/*!< in: index */
	ulint			n_fields);	/*!< in: number of
884
						field types to copy */
885
#endif /* !UNIV_HOTBACKUP */
886
/*********************************************************************//**
887 888
Gets the field column.
@return	field->col, pointer to the table column */
osku's avatar
osku committed
889
UNIV_INLINE
890
const dict_col_t*
osku's avatar
osku committed
891 892
dict_field_get_col(
/*===============*/
893
	const dict_field_t*	field);	/*!< in: index field */
894
#ifndef UNIV_HOTBACKUP
895
/**********************************************************************//**
896
Returns an index object if it is found in the dictionary cache.
897 898
Assumes that dict_sys->mutex is already being held.
@return	index, NULL if not found */
899
UNIV_INTERN
900 901 902
dict_index_t*
dict_index_get_if_in_cache_low(
/*===========================*/
903
	dulint	index_id);	/*!< in: index id */
904
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
905
/**********************************************************************//**
906 907
Returns an index object if it is found in the dictionary cache.
@return	index, NULL if not found */
908
UNIV_INTERN
osku's avatar
osku committed
909 910 911
dict_index_t*
dict_index_get_if_in_cache(
/*=======================*/
912
	dulint	index_id);	/*!< in: index id */
913 914
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
#ifdef UNIV_DEBUG
915
/**********************************************************************//**
osku's avatar
osku committed
916
Checks that a tuple has n_fields_cmp value in a sensible range, so that
917 918
no comparison can occur with the page number field in a node pointer.
@return	TRUE if ok */
919
UNIV_INTERN
osku's avatar
osku committed
920
ibool
921 922
dict_index_check_search_tuple(
/*==========================*/
923 924
	const dict_index_t*	index,	/*!< in: index tree */
	const dtuple_t*		tuple);	/*!< in: tuple used in a search */
925
/**********************************************************************//**
926
Check for duplicate index entries in a table [using the index name] */
927
UNIV_INTERN
928 929 930
void
dict_table_check_for_dup_indexes(
/*=============================*/
931
	const dict_table_t*	table,	/*!< in: Check for dup indexes
932
					in this table */
933 934
	ibool			tmp_ok);/*!< in: TRUE=allow temporary
					index names */
935
#endif /* UNIV_DEBUG */
936
/**********************************************************************//**
937 938
Builds a node pointer out of a physical record and a page number.
@return	own: node pointer */
939
UNIV_INTERN
osku's avatar
osku committed
940
dtuple_t*
941 942
dict_index_build_node_ptr(
/*======================*/
943 944
	const dict_index_t*	index,	/*!< in: index */
	const rec_t*		rec,	/*!< in: record for which to build node
945
					pointer */
946
	ulint			page_no,/*!< in: page number to put in node
947
					pointer */
948
	mem_heap_t*		heap,	/*!< in: memory heap where pointer
949
					created */
950
	ulint			level);	/*!< in: level of rec in tree:
951
					0 means leaf level */
952
/**********************************************************************//**
osku's avatar
osku committed
953
Copies an initial segment of a physical record, long enough to specify an
954 955
index entry uniquely.
@return	pointer to the prefix record */
956
UNIV_INTERN
osku's avatar
osku committed
957
rec_t*
958 959
dict_index_copy_rec_order_prefix(
/*=============================*/
960 961
	const dict_index_t*	index,	/*!< in: index */
	const rec_t*		rec,	/*!< in: record for which to
962
					copy prefix */
963 964
	ulint*			n_fields,/*!< out: number of fields copied */
	byte**			buf,	/*!< in/out: memory buffer for the
965
					copied prefix, or NULL */
966
	ulint*			buf_size);/*!< in/out: buffer size */
967
/**********************************************************************//**
968 969
Builds a typed data tuple out of a physical record.
@return	own: data tuple */
970
UNIV_INTERN
osku's avatar
osku committed
971
dtuple_t*
972 973
dict_index_build_data_tuple(
/*========================*/
974 975 976 977
	dict_index_t*	index,	/*!< in: index */
	rec_t*		rec,	/*!< in: record for which to build data tuple */
	ulint		n_fields,/*!< in: number of data fields */
	mem_heap_t*	heap);	/*!< in: memory heap where tuple created */
978
/*********************************************************************//**
979 980
Gets the space id of the root of the index tree.
@return	space id */
osku's avatar
osku committed
981 982
UNIV_INLINE
ulint
983 984
dict_index_get_space(
/*=================*/
985
	const dict_index_t*	index);	/*!< in: index */
986
/*********************************************************************//**
osku's avatar
osku committed
987 988 989
Sets the space id of the root of the index tree. */
UNIV_INLINE
void
990 991
dict_index_set_space(
/*=================*/
992 993
	dict_index_t*	index,	/*!< in/out: index */
	ulint		space);	/*!< in: space id */
994
/*********************************************************************//**
995 996
Gets the page number of the root of the index tree.
@return	page number */
osku's avatar
osku committed
997 998
UNIV_INLINE
ulint
999 1000
dict_index_get_page(
/*================*/
1001
	const dict_index_t*	tree);	/*!< in: index */
1002
/*********************************************************************//**
osku's avatar
osku committed
1003 1004 1005
Sets the page number of the root of index tree. */
UNIV_INLINE
void
1006 1007
dict_index_set_page(
/*================*/
1008 1009
	dict_index_t*	index,	/*!< in/out: index */
	ulint		page);	/*!< in: page number */
1010
/*********************************************************************//**
1011 1012
Gets the read-write lock of the index tree.
@return	read-write lock */
osku's avatar
osku committed
1013 1014
UNIV_INLINE
rw_lock_t*
1015 1016
dict_index_get_lock(
/*================*/
1017
	dict_index_t*	index);	/*!< in: index */
1018
/********************************************************************//**
osku's avatar
osku committed
1019 1020
Returns free space reserved for future updates of records. This is
relevant only in the case of many consecutive inserts, as updates
1021 1022
which make the records bigger might fragment the index.
@return	number of free bytes on page, reserved for updates */
osku's avatar
osku committed
1023 1024
UNIV_INLINE
ulint
1025 1026
dict_index_get_space_reserve(void);
/*==============================*/
1027
/*********************************************************************//**
osku's avatar
osku committed
1028
Calculates the minimum record length in an index. */
1029
UNIV_INTERN
osku's avatar
osku committed
1030 1031 1032
ulint
dict_index_calc_min_rec_len(
/*========================*/
1033
	const dict_index_t*	index);	/*!< in: index */
1034
/*********************************************************************//**
osku's avatar
osku committed
1035 1036
Calculates new estimates for table and index statistics. The statistics
are used in query optimization. */
1037
UNIV_INTERN
osku's avatar
osku committed
1038 1039 1040
void
dict_update_statistics_low(
/*=======================*/
1041 1042
	dict_table_t*	table,		/*!< in/out: table */
	ibool		has_dict_mutex);/*!< in: TRUE if the caller has the
1043
					dictionary mutex */
1044
/*********************************************************************//**
osku's avatar
osku committed
1045 1046
Calculates new estimates for table and index statistics. The statistics
are used in query optimization. */
1047
UNIV_INTERN
osku's avatar
osku committed
1048 1049 1050
void
dict_update_statistics(
/*===================*/
1051
	dict_table_t*	table);	/*!< in/out: table */
1052
/********************************************************************//**
osku's avatar
osku committed
1053
Reserves the dictionary system mutex for MySQL. */
1054
UNIV_INTERN
osku's avatar
osku committed
1055 1056 1057
void
dict_mutex_enter_for_mysql(void);
/*============================*/
1058
/********************************************************************//**
osku's avatar
osku committed
1059
Releases the dictionary system mutex for MySQL. */
1060
UNIV_INTERN
osku's avatar
osku committed
1061 1062 1063
void
dict_mutex_exit_for_mysql(void);
/*===========================*/
1064
/********************************************************************//**
1065 1066
Checks if the database name in two table names is the same.
@return	TRUE if same db name */
1067
UNIV_INTERN
osku's avatar
osku committed
1068 1069 1070
ibool
dict_tables_have_same_db(
/*=====================*/
1071
	const char*	name1,	/*!< in: table name in the form
osku's avatar
osku committed
1072
				dbname '/' tablename */
1073
	const char*	name2);	/*!< in: table name in the form
osku's avatar
osku committed
1074
				dbname '/' tablename */
1075
/*********************************************************************//**
1076
Removes an index from the cache */
1077
UNIV_INTERN
1078 1079 1080
void
dict_index_remove_from_cache(
/*=========================*/
1081 1082
	dict_table_t*	table,	/*!< in/out: table */
	dict_index_t*	index);	/*!< in, own: index */
1083
/**********************************************************************//**
1084 1085
Get index by name
@return	index, NULL if does not exist */
1086
UNIV_INTERN
1087 1088 1089
dict_index_t*
dict_table_get_index_on_name(
/*=========================*/
1090 1091
	dict_table_t*	table,	/*!< in: table */
	const char*	name);	/*!< in: name of the index to find */
1092
/**********************************************************************//**
1093
In case there is more than one index with the same name return the index
1094 1095
with the min(id).
@return	index, NULL if does not exist */
1096
UNIV_INTERN
1097 1098 1099
dict_index_t*
dict_table_get_index_on_name_and_min_id(
/*====================================*/
1100 1101
	dict_table_t*	table,	/*!< in: table */
	const char*	name);	/*!< in: name of the index to find */
osku's avatar
osku committed
1102 1103 1104 1105 1106
/* Buffers for storing detailed information about the latest foreign key
and unique key errors */
extern FILE*	dict_foreign_err_file;
extern mutex_t	dict_foreign_err_mutex; /* mutex protecting the buffers */

1107 1108 1109
/** the dictionary system */
extern dict_sys_t*	dict_sys;
/** the data dictionary rw-latch protecting dict_sys */
osku's avatar
osku committed
1110 1111 1112 1113
extern rw_lock_t	dict_operation_lock;

/* Dictionary system struct */
struct dict_sys_struct{
1114
	mutex_t		mutex;		/*!< mutex protecting the data
osku's avatar
osku committed
1115 1116 1117 1118 1119 1120
					dictionary; protects also the
					disk-based dictionary system tables;
					this mutex serializes CREATE TABLE
					and DROP TABLE, as well as reading
					the dictionary data for a table from
					system tables */
1121
	dulint		row_id;		/*!< the next row id to assign;
osku's avatar
osku committed
1122 1123 1124 1125 1126
					NOTE that at a checkpoint this
					must be written to the dict system
					header and flushed to a file; in
					recovery this must be derived from
					the log records */
1127
	hash_table_t*	table_hash;	/*!< hash table of the tables, based
osku's avatar
osku committed
1128
					on name */
1129
	hash_table_t*	table_id_hash;	/*!< hash table of the tables, based
osku's avatar
osku committed
1130 1131
					on id */
	UT_LIST_BASE_NODE_T(dict_table_t)
1132 1133
			table_LRU;	/*!< LRU list of tables */
	ulint		size;		/*!< varying space in bytes occupied
osku's avatar
osku committed
1134 1135
					by the data dictionary table and
					index objects */
1136 1137 1138 1139
	dict_table_t*	sys_tables;	/*!< SYS_TABLES table */
	dict_table_t*	sys_columns;	/*!< SYS_COLUMNS table */
	dict_table_t*	sys_indexes;	/*!< SYS_INDEXES table */
	dict_table_t*	sys_fields;	/*!< SYS_FIELDS table */
osku's avatar
osku committed
1140
};
1141
#endif /* !UNIV_HOTBACKUP */
osku's avatar
osku committed
1142

1143
/** dummy index for ROW_FORMAT=REDUNDANT supremum and infimum records */
1144
extern dict_index_t*	dict_ind_redundant;
1145
/** dummy index for ROW_FORMAT=COMPACT supremum and infimum records */
1146 1147
extern dict_index_t*	dict_ind_compact;

1148
/**********************************************************************//**
1149 1150 1151 1152 1153 1154
Inits dict_ind_redundant and dict_ind_compact. */
UNIV_INTERN
void
dict_ind_init(void);
/*===============*/

1155 1156 1157 1158 1159 1160 1161
/**********************************************************************//**
Closes the data dictionary module. */
UNIV_INTERN
void
dict_close(void);
/*============*/

osku's avatar
osku committed
1162 1163 1164 1165 1166
#ifndef UNIV_NONINL
#include "dict0dict.ic"
#endif

#endif