dict0mem.h 14.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/******************************************************
Data dictionary memory object creation

(c) 1996 Innobase Oy

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

#ifndef dict0mem_h
#define dict0mem_h

#include "univ.i"
#include "dict0types.h"
#include "data0type.h"
#include "data0data.h"
#include "mem0mem.h"
#include "rem0types.h"
#include "btr0types.h"
#include "ut0mem.h"
#include "ut0lst.h"
#include "ut0rnd.h"
#include "ut0byte.h"
#include "sync0rw.h"
#include "lock0types.h"
#include "hash0hash.h"
#include "que0types.h"

/* Type flags of an index: OR'ing of the flags is allowed to define a
combination of types */
#define DICT_CLUSTERED	1	/* clustered index */
#define DICT_UNIQUE	2	/* unique index */
32
#define	DICT_UNIVERSAL	4	/* index which can contain records from any
33
				other index */
34
#define	DICT_IBUF	8	/* insert buffer tree */
35 36 37

/* Types for a table object */
#define DICT_TABLE_ORDINARY		1
unknown's avatar
unknown committed
38
#if 0 /* not implemented */
39 40 41
#define	DICT_TABLE_CLUSTER_MEMBER	2
#define	DICT_TABLE_CLUSTER		3 /* this means that the table is
					  really a cluster definition */
unknown's avatar
unknown committed
42
#endif
43

44 45 46
/* Table flags */
#define DICT_TF_COMPACT			1	/* compact page format */

47 48 49 50 51 52
/**************************************************************************
Creates a table memory object. */

dict_table_t*
dict_mem_table_create(
/*==================*/
53 54 55 56 57 58
					/* out, own: table object */
	const char*	name,		/* in: table name */
	ulint		space,		/* in: space where the clustered index
					of the table is placed; this parameter
					is ignored if the table is made
					a member of a cluster */
unknown's avatar
unknown committed
59
	ulint		n_cols,		/* in: number of columns */
60
	ulint		flags);		/* in: table flags */
unknown's avatar
unknown committed
61 62
/********************************************************************
Free a table memory object. */
63 64

void
unknown's avatar
unknown committed
65 66 67
dict_mem_table_free(
/*================*/
	dict_table_t*	table);		/* in: table */
68 69 70 71 72 73 74
/**************************************************************************
Adds a column definition to a table. */

void
dict_mem_table_add_col(
/*===================*/
	dict_table_t*	table,	/* in: table */
75
	const char*	name,	/* in: column name */
76 77
	ulint		mtype,	/* in: main datatype */
	ulint		prtype,	/* in: precise type */
78
	ulint		len);	/* in: precision */
79 80 81 82 83 84
/**************************************************************************
Creates an index memory object. */

dict_index_t*
dict_mem_index_create(
/*==================*/
85 86 87 88 89 90 91 92 93
					/* out, own: index object */
	const char*	table_name,	/* in: table name */
	const char*	index_name,	/* in: index name */
	ulint		space,		/* in: space where the index tree is
					placed, ignored if the index is of
					the clustered type */
	ulint		type,		/* in: DICT_UNIQUE,
					DICT_CLUSTERED, ... ORed */
	ulint		n_fields);	/* in: number of fields */
94 95 96 97 98 99 100 101
/**************************************************************************
Adds a field definition to an index. NOTE: does not take a copy
of the column name if the field is a column. The memory occupied
by the column name may be released only after publishing the index. */

void
dict_mem_index_add_field(
/*=====================*/
unknown's avatar
unknown committed
102
	dict_index_t*	index,		/* in: index */
103
	const char*	name,		/* in: column name */
unknown's avatar
unknown committed
104 105 106
	ulint		prefix_len);	/* in: 0 or the column prefix length
					in a MySQL index like
					INDEX (textcol(25)) */
107 108 109 110 111 112 113 114
/**************************************************************************
Frees an index memory object. */

void
dict_mem_index_free(
/*================*/
	dict_index_t*	index);	/* in: index */
/**************************************************************************
115 116 117 118 119 120
Creates and initializes a foreign constraint memory object. */

dict_foreign_t*
dict_mem_foreign_create(void);
/*=========================*/
				/* out, own: foreign constraint struct */
121 122 123

/* Data structure for a column in a table */
struct dict_col_struct{
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	/*----------------------*/
	/* The following are copied from dtype_t,
	so that all bit-fields can be packed tightly. */
	unsigned	mtype:8;	/* main data type */
	unsigned	prtype:24;	/* precise type; MySQL data
					type, charset code, flags to
					indicate nullability,
					signedness, whether this is a
					binary string, whether this is
					a true VARCHAR where MySQL
					uses 2 bytes to store the length */

	/* the remaining fields do not affect alphabetical ordering: */

	unsigned	len:16;		/* length; for MySQL data this
					is field->pack_length(),
					except that for a >= 5.0.3
					type true VARCHAR this is the
					maximum byte length of the
					string data (in addition to
					the string, MySQL uses 1 or 2
					bytes to store the string length) */

	unsigned	mbminlen:2;	/* minimum length of a
					character, in bytes */
	unsigned	mbmaxlen:3;	/* maximum length of a
					character, in bytes */
	/*----------------------*/
	/* End of definitions copied from dtype_t */

	unsigned	ind:10;		/* table column position
					(starting from 0) */
	unsigned	ord_part:1;	/* nonzero if this column
					appears in the ordering fields
					of an index */
159 160
};

161 162 163 164
/* DICT_MAX_INDEX_COL_LEN is measured in bytes and is the max index column
length + 1. Starting from 4.1.6, we set it to < 3 * 256, so that one can
create a column prefix index on 255 characters of a TEXT field also in the
UTF-8 charset. In that charset, a character may take at most 3 bytes. */
unknown's avatar
unknown committed
165

166
#define DICT_MAX_INDEX_COL_LEN		768
unknown's avatar
unknown committed
167

168 169
/* Data structure for a field in an index */
struct dict_field_struct{
unknown's avatar
unknown committed
170
	dict_col_t*	col;		/* pointer to the table column */
171
	const char*	name;		/* name of the column */
172
	unsigned	prefix_len:10;	/* 0 or the length of the column
unknown's avatar
unknown committed
173 174 175
					prefix in bytes in a MySQL index of
					type, e.g., INDEX (textcol(25));
					must be smaller than
176
					DICT_MAX_INDEX_COL_LEN; NOTE that
unknown's avatar
unknown committed
177 178
					in the UTF-8 charset, MySQL sets this
					to 3 * the prefix len in UTF-8 chars */
179
	unsigned	fixed_len:10;	/* 0 or the fixed length of the
unknown's avatar
unknown committed
180
					column if smaller than
181
					DICT_MAX_INDEX_COL_LEN */
182 183 184 185 186 187 188
};

/* Data structure for an index */
struct dict_index_struct{
	dulint		id;	/* id of the index */
	mem_heap_t*	heap;	/* memory heap */
	ulint		type;	/* index type */
189 190
	const char*	name;	/* index name */
	const char*	table_name; /* table name */
191
	dict_table_t*	table;	/* back pointer to table */
192 193 194 195
	unsigned	space:32;
				/* space where the index tree is placed */
	unsigned	page:32;/* index tree root page number */
	unsigned	trx_id_offset:10;/* position of the the trx id column
196 197 198
				in a clustered index record, if the fields
				before it are known to be of a fixed size,
				0 otherwise */
199
	unsigned	n_user_defined_cols:10;
200 201 202
				/* number of columns the user defined to
				be in the index: in the internal
				representation we add more columns */
203
	unsigned	n_uniq:10;/* number of fields from the beginning
204 205
				which are enough to determine an index
				entry uniquely */
206 207 208 209 210
	unsigned	n_def:10;/* number of fields defined so far */
	unsigned	n_fields:10;/* number of fields in the index */
	unsigned	n_nullable:10;/* number of nullable fields */
	unsigned	cached:1;/* TRUE if the index object is in the
				dictionary cache */
211 212 213 214 215
	dict_field_t*	fields;	/* array of field descriptions */
	UT_LIST_NODE_T(dict_index_t)
			indexes;/* list of indexes of the table */
	btr_search_t*	search_info; /* info used in optimistic searches */
	/*----------------------*/
216
	ib_longlong*	stat_n_diff_key_vals;
217
				/* approximate number of different key values
218 219 220
				for this index, for each n-column prefix
				where n <= dict_get_n_unique(index); we
				periodically calculate new estimates */
221 222
	ulint		stat_index_size;
				/* approximate index size in database pages */
223 224 225
	ulint		stat_n_leaf_pages;
				/* approximate number of leaf pages in the
				index tree */
226 227 228
	rw_lock_t	lock;	/* read-write lock protecting the upper levels
				of the index tree */
#ifdef UNIV_DEBUG
229
	ulint		magic_n;/* magic number */
230 231
# define DICT_INDEX_MAGIC_N	76789786
#endif
232 233
};

234 235 236 237 238 239 240 241
/* Data structure for a foreign key constraint; an example:
FOREIGN KEY (A, B) REFERENCES TABLE2 (C, D) */

struct dict_foreign_struct{
	mem_heap_t*	heap;		/* this object is allocated from
					this memory heap */
	char*		id;		/* id of the constraint as a
					null-terminated string */
242 243 244 245 246 247 248
	unsigned	n_fields:10;	/* number of indexes' first fields
					for which the the foreign key
					constraint is defined: we allow the
					indexes to contain more fields than
					mentioned in the constraint, as long
					as the first fields are as mentioned */
	unsigned	type:6;		/* 0 or DICT_FOREIGN_ON_DELETE_CASCADE
unknown's avatar
unknown committed
249
					or DICT_FOREIGN_ON_DELETE_SET_NULL */
250 251
	char*		foreign_table_name;/* foreign table name */
	dict_table_t*	foreign_table;	/* table where the foreign key is */
252
	const char**	foreign_col_names;/* names of the columns in the
253 254 255 256
					foreign key */
	char*		referenced_table_name;/* referenced table name */
	dict_table_t*	referenced_table;/* table where the referenced key
					is */
257
	const char**	referenced_col_names;/* names of the referenced
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
					columns in the referenced table */
	dict_index_t*	foreign_index;	/* foreign index; we require that
					both tables contain explicitly defined
					indexes for the constraint: InnoDB
					does not generate new indexes
					implicitly */
	dict_index_t*	referenced_index;/* referenced index */
	UT_LIST_NODE_T(dict_foreign_t)
			foreign_list;	/* list node for foreign keys of the
					table */
	UT_LIST_NODE_T(dict_foreign_t)
			referenced_list;/* list node for referenced keys of the
					table */
};

unknown's avatar
unknown committed
273 274
/* The flags for ON_UPDATE and ON_DELETE can be ORed; the default is that
a foreign key constraint is enforced, therefore RESTRICT just means no flag */
unknown's avatar
unknown committed
275 276
#define DICT_FOREIGN_ON_DELETE_CASCADE	1
#define DICT_FOREIGN_ON_DELETE_SET_NULL	2
unknown's avatar
unknown committed
277 278 279 280 281
#define DICT_FOREIGN_ON_UPDATE_CASCADE	4
#define DICT_FOREIGN_ON_UPDATE_SET_NULL	8
#define DICT_FOREIGN_ON_DELETE_NO_ACTION 16
#define DICT_FOREIGN_ON_UPDATE_NO_ACTION 32

282

283 284
/* Data structure for a database table */
struct dict_table_struct{
unknown's avatar
unknown committed
285
	dulint		id;	/* id of the table */
286
	mem_heap_t*	heap;	/* memory heap */
287
	const char*	name;	/* table name */
unknown's avatar
unknown committed
288 289 290 291 292 293
	const char*	dir_path_of_temp_table;/* NULL or the directory path
				where a TEMPORARY table that was explicitly
				created by a user should be placed if
				innodb_file_per_table is defined in my.cnf;
				in Unix this is usually /tmp/..., in Windows
				\temp\... */
294 295
	unsigned	space:32;
				/* space where the clustered index of the
296
				table is placed */
297 298
	unsigned	ibd_file_missing:1;
				/* TRUE if this is in a single-table
unknown's avatar
unknown committed
299 300 301
				tablespace and the .ibd file is missing; then
				we must return in ha_innodb.cc an error if the
				user tries to query such an orphaned table */
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	unsigned	tablespace_discarded:1;
				/* this flag is set TRUE when the user
				calls DISCARD TABLESPACE on this
				table, and reset to FALSE in IMPORT
				TABLESPACE */
	unsigned	cached:1;/* TRUE if the table object has been added
				to the dictionary cache */
	unsigned	flags:8;/* DICT_TF_COMPACT, ... */
	unsigned	n_def:10;/* number of columns defined so far */
	unsigned	n_cols:10;/* number of columns */
	dict_col_t*	cols;	/* array of column descriptions */
	const char*	col_names;
				/* n_def column names packed in an
				"name1\0name2\0...nameN\0" array. until
				n_def reaches n_cols, this is allocated with
				ut_malloc, and the final size array is
				allocated through the table's heap. */
319 320 321 322
	hash_node_t	name_hash; /* hash chain node */
	hash_node_t	id_hash; /* hash chain node */
	UT_LIST_BASE_NODE_T(dict_index_t)
			indexes; /* list of indexes of the table */
323 324 325 326 327 328 329
	UT_LIST_BASE_NODE_T(dict_foreign_t)
			foreign_list;/* list of foreign key constraints
				in the table; these refer to columns
				in other tables */
	UT_LIST_BASE_NODE_T(dict_foreign_t)
			referenced_list;/* list of foreign key constraints
				which refer to this table */
330 331
	UT_LIST_NODE_T(dict_table_t)
			table_LRU; /* node of the LRU list of tables */
unknown's avatar
unknown committed
332 333 334 335 336 337
	ulint		n_mysql_handles_opened;
				/* count of how many handles MySQL has opened
				to this table; dropping of the table is
				NOT allowed until this count gets to zero;
				MySQL does NOT itself check the number of
				open handles at drop */
unknown's avatar
unknown committed
338 339 340 341 342 343
	ulint		n_foreign_key_checks_running;
				/* count of how many foreign key check
				operations are currently being performed
				on the table: we cannot drop the table while
				there are foreign key checks running on
				it! */
344 345 346 347 348 349 350
	lock_t*		auto_inc_lock;/* a buffer for an auto-inc lock
				for this table: we allocate the memory here
				so that individual transactions can get it
				and release it without a need to allocate
				space from the lock heap of the trx:
				otherwise the lock heap would grow rapidly
				if we do a large insert from a select */
351 352 353 354 355 356 357
	dulint		query_cache_inv_trx_id;
				/* transactions whose trx id < than this
				number are not allowed to store to the MySQL
				query cache or retrieve from it; when a trx
				with undo logs commits, it sets this to the
				value of the trx id counter for the tables it
				had an IX lock on */
358 359
	UT_LIST_BASE_NODE_T(lock_t)
			locks; /* list of locks on the table */
360
#ifdef UNIV_DEBUG
361 362 363 364 365 366 367 368 369 370
	/*----------------------*/
	ibool		does_not_fit_in_memory;
				/* this field is used to specify in simulations
				tables which are so big that disk should be
				accessed: disk access is simulated by
				putting the thread to sleep for a while;
				NOTE that this flag is not stored to the data
				dictionary on disk, and the database will
				forget about value TRUE if it has to reload
				the table definition from disk */
371
#endif /* UNIV_DEBUG */
372
	/*----------------------*/
373 374 375 376 377 378 379
	unsigned	big_rows:1;
				/* flag: TRUE if the maximum length of
				a single row exceeds BIG_ROW_SIZE;
				initialized in dict_table_add_to_cache() */
	unsigned	stat_initialized:1; /* TRUE if statistics have
				been calculated the first time
				after database startup or table creation */
380
	ib_longlong	stat_n_rows;
381 382 383 384 385 386 387
				/* approximate number of rows in the table;
				we periodically calculate new estimates */
	ulint		stat_clustered_index_size;
				/* approximate clustered index size in
				database pages */
	ulint		stat_sum_of_other_index_sizes;
				/* other indexes in database pages */
388
	ulint		stat_modified_counter;
389
				/* when a row is inserted, updated, or deleted,
unknown's avatar
unknown committed
390 391 392 393 394 395 396 397 398 399
				we add 1 to this number; we calculate new
				estimates for the stat_... values for the
				table and the indexes at an interval of 2 GB
				or when about 1 / 16 of table has been
				modified; also when the estimate operation is
				called for MySQL SHOW TABLE STATUS; the
				counter is reset to zero at statistics
				calculation; this counter is not protected by
				any latch, because this is only used for
				heuristics */
400 401 402 403 404 405 406 407
	/*----------------------*/
	mutex_t		autoinc_mutex;
				/* mutex protecting the autoincrement
				counter */
	ibool		autoinc_inited;
				/* TRUE if the autoinc counter has been
				inited; MySQL gets the init value by executing
				SELECT MAX(auto inc column) */
unknown's avatar
unknown committed
408
	ib_longlong	autoinc;/* autoinc counter value to give to the
409
				next inserted row */
410
#ifdef UNIV_DEBUG
411
	ulint		magic_n;/* magic number */
412 413
# define DICT_TABLE_MAGIC_N	76333786
#endif /* UNIV_DEBUG */
414
};
415

416 417 418 419 420
#ifndef UNIV_NONINL
#include "dict0mem.ic"
#endif

#endif