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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/******************************************************
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 */
#define	DICT_UNIVERSAL 	4	/* index which can contain records from any
				other index */
#define	DICT_IBUF 	8	/* insert buffer tree */
				
/* Flags for ordering an index field: OR'ing of the flags allowed */
#define	DICT_DESCEND	1	/* in descending order (default ascending) */

/* Types for a table object */
#define DICT_TABLE_ORDINARY		1
#define	DICT_TABLE_CLUSTER_MEMBER	2
#define	DICT_TABLE_CLUSTER		3 /* this means that the table is
					  really a cluster definition */

/**************************************************************************
Creates a table memory object. */

dict_table_t*
dict_mem_table_create(
/*==================*/
				/* out, own: table object */
	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 */
	ulint	n_cols);	/* in: number of columns */
/**************************************************************************
Creates a cluster memory object. */

dict_cluster_t*
dict_mem_cluster_create(
/*====================*/
				/* out, own: cluster object (where the type
				dict_cluster_t == dict_table_t) */
	char*	name,		/* in: cluster name */
	ulint	space,		/* in: space where the clustered indexes
				of the member tables are placed */
	ulint	n_cols,		/* in: number of columns */
	ulint	mix_len);	/* in: length of the common key prefix in the
				cluster */
/**************************************************************************
Declares a non-published table as a member in a cluster. */

void
dict_mem_table_make_cluster_member(
/*===============================*/
	dict_table_t*	table,		/* in: non-published table */
	char*		cluster_name);	/* in: cluster name */
/**************************************************************************
Adds a column definition to a table. */

void
dict_mem_table_add_col(
/*===================*/
	dict_table_t*	table,	/* in: table */
	char*		name,	/* in: column name */
	ulint		mtype,	/* in: main datatype */
	ulint		prtype,	/* in: precise type */
	ulint		len,	/* in: length */
	ulint		prec);	/* in: precision */
/**************************************************************************
Creates an index memory object. */

dict_index_t*
dict_mem_index_create(
/*==================*/
				/* out, own: index object */
	char*	table_name,	/* in: table name */
	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 */
/**************************************************************************
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(
/*=====================*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
114 115 116 117 118 119 120
	dict_index_t*	index,		/* in: index */
	char*		name,		/* in: column name */
	ulint		order,		/* in: order criterion; 0 means an
					ascending order */
	ulint		prefix_len);	/* in: 0 or the column prefix length
					in a MySQL index like
					INDEX (textcol(25)) */
121 122 123 124 125 126 127 128
/**************************************************************************
Frees an index memory object. */

void
dict_mem_index_free(
/*================*/
	dict_index_t*	index);	/* in: index */
/**************************************************************************
129 130 131 132 133 134
Creates and initializes a foreign constraint memory object. */

dict_foreign_t*
dict_mem_foreign_create(void);
/*=========================*/
				/* out, own: foreign constraint struct */
135 136 137 138 139 140 141 142 143

/* Data structure for a column in a table */
struct dict_col_struct{
	hash_node_t	hash;	/* hash chain node */
	ulint		ind;	/* table column position (they are numbered
				starting from 0) */
	ulint		clust_pos;/* position of the column in the
				clustered index */
	ulint		ord_part;/* count of how many times this column
144
				appears in ordering fields of an index */
145 146 147 148 149 150 151
	char*		name;	/* name */
	dtype_t		type;	/* data type */
	dict_table_t*	table;	/* back pointer to table of this column */
	ulint		aux;	/* this is used as an auxiliary variable 
				in some of the functions below */
};

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
152 153
#define DICT_MAX_COL_PREFIX_LEN	512

154 155
/* Data structure for a field in an index */
struct dict_field_struct{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
156 157 158 159 160 161 162 163
	dict_col_t*	col;		/* pointer to the table column */
	char*		name;		/* name of the column */
	ulint		order;		/* flags for ordering this field:
					DICT_DESCEND, ... */
	ulint		prefix_len;	/* 0 or the length of the column
					prefix in a MySQL index of type, e.g.,
					INDEX (textcol(25)); must be smaller
					than DICT_MAX_COL_PREFIX_LEN */
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
};

/* Data structure for an index tree */
struct dict_tree_struct{
	ulint		type;	/* tree type */
	dulint		id;	/* id of the index stored in the tree, in the
				case of a mixed index, the id of the clustered
				index of the cluster table */
	ulint		space;	/* space of index tree */
	ulint		page;	/* index tree root page number */
	byte		pad[64];/* Padding to prevent other memory hotspots on
				the same memory cache line */
	rw_lock_t	lock;	/* read-write lock protecting the upper levels
				of the index tree */
	ulint		mem_fix;/* count of how many times this tree
				struct has been memoryfixed (by mini-
				transactions wanting to access the index
				tree) */
	UT_LIST_BASE_NODE_T(dict_index_t)
			tree_indexes; /* list of indexes stored in the
				index tree: if the tree is not of the
				mixed type there is only one index in
				the list; if the tree is of the mixed
				type, the first index in the list is the
				index of the cluster which owns the tree */
	ulint		magic_n;/* magic number */
190
};
191

192 193
#define	DICT_TREE_MAGIC_N	7545676

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
/* 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 */
	char*		name;	/* index name */
	char*		table_name; /* table name */
	dict_table_t*	table;	/* back pointer to table */
	ulint		space;	/* space where the index tree is placed */
	ulint		page_no;/* page number of the index tree root */
	ulint		trx_id_offset;/* position of the the trx id column
				in a clustered index record, if the fields
				before it are known to be of a fixed size,
				0 otherwise */
	ulint		n_user_defined_cols;
				/* number of columns the user defined to
				be in the index: in the internal
				representation we add more columns */
	ulint		n_uniq;	/* number of fields from the beginning
				which are enough to determine an index
				entry uniquely */
	ulint		n_def;	/* number of fields defined so far */
	ulint		n_fields;/* number of fields in the index */
	dict_field_t*	fields;	/* array of field descriptions */
	UT_LIST_NODE_T(dict_index_t)
			indexes;/* list of indexes of the table */
	dict_tree_t*	tree;	/* index tree struct */
	UT_LIST_NODE_T(dict_index_t)
			tree_indexes; /* list of indexes of the same index
				tree */
	ibool		cached;	/* TRUE if the index object is in the
				dictionary cache */
	btr_search_t*	search_info; /* info used in optimistic searches */
	/*----------------------*/
228
	ib_longlong*	stat_n_diff_key_vals;
229
				/* approximate number of different key values
230 231 232
				for this index, for each n-column prefix
				where n <= dict_get_n_unique(index); we
				periodically calculate new estimates */
233 234
	ulint		stat_index_size;
				/* approximate index size in database pages */
235 236 237
	ulint		stat_n_leaf_pages;
				/* approximate number of leaf pages in the
				index tree */
238 239 240
	ulint		magic_n;/* magic number */
};

241 242 243 244 245 246 247 248
/* 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 */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
249 250
	ulint		type;		/* 0 or DICT_FOREIGN_ON_DELETE_CASCADE
					or DICT_FOREIGN_ON_DELETE_SET_NULL */
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	char*		foreign_table_name;/* foreign table name */
	dict_table_t*	foreign_table;	/* table where the foreign key is */
	char**		foreign_col_names;/* names of the columns in the
					foreign key */
	char*		referenced_table_name;/* referenced table name */
	dict_table_t*	referenced_table;/* table where the referenced key
					is */
	char**		referenced_col_names;/* names of the referenced
					columns in the referenced table */
	ulint		n_fields;	/* 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 */ 
	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 */
};

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
280 281
/* 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 */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
282 283
#define DICT_FOREIGN_ON_DELETE_CASCADE	1
#define DICT_FOREIGN_ON_DELETE_SET_NULL	2
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
284 285 286 287 288
#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

289 290 291

#define	DICT_INDEX_MAGIC_N	76789786

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
/* Data structure for a database table */
struct dict_table_struct{
	dulint		id;	/* id of the table or cluster */
	ulint		type;	/* DICT_TABLE_ORDINARY, ... */
	mem_heap_t*	heap;	/* memory heap */
	char*		name;	/* table name */
	ulint		space;	/* space where the clustered index of the
				table is placed */
	hash_node_t	name_hash; /* hash chain node */
	hash_node_t	id_hash; /* hash chain node */
	ulint		n_def;	/* number of columns defined so far */
	ulint		n_cols;	/* number of columns */
	dict_col_t*	cols;	/* array of column descriptions */
	UT_LIST_BASE_NODE_T(dict_index_t)
			indexes; /* list of indexes of the table */
307 308 309 310 311 312 313
	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 */
314 315 316 317 318
	UT_LIST_NODE_T(dict_table_t)
			table_LRU; /* node of the LRU list of tables */
	ulint		mem_fix;/* count of how many times the table 
				and its indexes has been fixed in memory;
				currently NOT used */
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
319 320 321 322 323 324
	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 */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
325 326 327 328 329 330
	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! */
331 332
	ibool		cached;	/* TRUE if the table object has been added
				to the dictionary cache */
333 334 335 336 337 338 339
	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 */
340 341 342 343 344 345 346
	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 */
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
	UT_LIST_BASE_NODE_T(lock_t)
			locks; /* list of locks on the table */
	/*----------------------*/
	dulint		mix_id;	/* if the table is a member in a cluster,
				this is its mix id */
	ulint		mix_len;/* if the table is a cluster or a member
				this is the common key prefix lenght */
	ulint		mix_id_len;/* mix id length in a compressed form */
	byte		mix_id_buf[12];
				/* mix id of a mixed table written in
				a compressed form */
	char*		cluster_name; /* if the table is a member in a
				cluster, this is the name of the cluster */
	/*----------------------*/
	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
	ib_longlong	stat_n_rows;
372 373 374 375 376 377 378
				/* 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 */
379 380 381 382
	ibool           stat_initialized; /* TRUE if statistics have
				been calculated the first time
			        after database startup or table creation */
	ulint		stat_modified_counter;
383
				/* when a row is inserted, updated, or deleted,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
384 385 386 387 388 389 390 391 392 393
				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 */
394 395 396 397 398 399 400 401
	/*----------------------*/
	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) */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
402
	ib_longlong	autoinc;/* autoinc counter value to give to the
403
				next inserted row */	
404
	ulint		magic_n;/* magic number */
405
};
406
#define	DICT_TABLE_MAGIC_N	76333786
407 408 409 410 411 412
					
#ifndef UNIV_NONINL
#include "dict0mem.ic"
#endif

#endif