dict0load.c 32.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/******************************************************
Loads to the memory cache database object definitions
from dictionary tables

(c) 1996 Innobase Oy

Created 4/24/1996 Heikki Tuuri
*******************************************************/

#include "dict0load.h"
11
#ifndef UNIV_HOTBACKUP
monty@mysql.com's avatar
monty@mysql.com committed
12
#include "mysql_version.h"
13
#endif /* !UNIV_HOTBACKUP */
14 15 16 17 18 19 20 21 22 23 24

#ifdef UNIV_NONINL
#include "dict0load.ic"
#endif

#include "btr0pcur.h"
#include "btr0btr.h"
#include "page0page.h"
#include "mach0data.h"
#include "dict0dict.h"
#include "dict0boot.h"
25
#include "rem0cmp.h"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
26
#include "srv0start.h"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
27
#include "srv0srv.h"
28

29 30 31 32 33 34
/************************************************************************
Finds the first table name in the given database. */

char*
dict_get_first_table_name_in_db(
/*============================*/
35 36 37
				/* out, own: table name, NULL if
				does not exist; the caller must
				free the memory in the string! */
38
	const char*	name)	/* in: database name which ends in '/' */
39 40 41 42 43 44 45 46 47 48 49
{
	dict_table_t*	sys_tables;
	btr_pcur_t	pcur;
	dict_index_t*	sys_index;
	dtuple_t*	tuple;
	mem_heap_t*	heap;
	dfield_t*	dfield;
	rec_t*		rec;
	byte*		field;
	ulint		len;
	mtr_t		mtr;
50

51
#ifdef UNIV_SYNC_DEBUG
52
	ut_ad(mutex_own(&(dict_sys->mutex)));
53
#endif /* UNIV_SYNC_DEBUG */
54 55

	heap = mem_heap_create(1000);
56

57 58
	mtr_start(&mtr);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
59
	sys_tables = dict_table_get_low("SYS_TABLES");
60
	sys_index = UT_LIST_GET_FIRST(sys_tables->indexes);
61
	ut_a(!dict_table_is_comp(sys_tables));
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

	tuple = dtuple_create(heap, 1);
	dfield = dtuple_get_nth_field(tuple, 0);

	dfield_set_data(dfield, name, ut_strlen(name));
	dict_index_copy_types(tuple, sys_index, 1);

	btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
					BTR_SEARCH_LEAF, &pcur, &mtr);
loop:
	rec = btr_pcur_get_rec(&pcur);

	if (!btr_pcur_is_on_user_rec(&pcur, &mtr)) {
		/* Not found */

		btr_pcur_close(&pcur);
		mtr_commit(&mtr);
		mem_heap_free(heap);
80

81
		return(NULL);
82
	}
83

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
84
	field = rec_get_nth_field_old(rec, 0, &len);
85 86

	if (len < strlen(name)
87
		|| ut_memcmp(name, field, strlen(name)) != 0) {
88 89 90 91 92
		/* Not found */

		btr_pcur_close(&pcur);
		mtr_commit(&mtr);
		mem_heap_free(heap);
93

94 95 96
		return(NULL);
	}

97
	if (!rec_get_deleted_flag(rec, 0)) {
98 99 100

		/* We found one */

101 102
		char*	table_name = mem_strdupl((char*) field, len);

103 104 105
		btr_pcur_close(&pcur);
		mtr_commit(&mtr);
		mem_heap_free(heap);
106

107 108
		return(table_name);
	}
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	btr_pcur_move_to_next_user_rec(&pcur, &mtr);

	goto loop;
}

/************************************************************************
Prints to the standard output information on all tables found in the data
dictionary system table. */

void
dict_print(void)
/*============*/
{
	dict_table_t*	sys_tables;
	dict_index_t*	sys_index;
	dict_table_t*	table;
	btr_pcur_t	pcur;
	rec_t*		rec;
	byte*		field;
	ulint		len;
	mtr_t		mtr;
131

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
132 133 134 135 136 137 138
	/* Enlarge the fatal semaphore wait timeout during the InnoDB table
	monitor printout */

	mutex_enter(&kernel_mutex);
	srv_fatal_semaphore_wait_threshold += 7200; /* 2 hours */
	mutex_exit(&kernel_mutex);

139 140 141 142
	mutex_enter(&(dict_sys->mutex));

	mtr_start(&mtr);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
143
	sys_tables = dict_table_get_low("SYS_TABLES");
144 145 146 147 148 149 150 151 152 153 154 155 156 157
	sys_index = UT_LIST_GET_FIRST(sys_tables->indexes);

	btr_pcur_open_at_index_side(TRUE, sys_index, BTR_SEARCH_LEAF, &pcur,
								TRUE, &mtr);
loop:
	btr_pcur_move_to_next_user_rec(&pcur, &mtr);

	rec = btr_pcur_get_rec(&pcur);

	if (!btr_pcur_is_on_user_rec(&pcur, &mtr)) {
		/* end of index */

		btr_pcur_close(&pcur);
		mtr_commit(&mtr);
158

159 160
		mutex_exit(&(dict_sys->mutex));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
161 162 163 164 165 166
		/* Restore the fatal semaphore wait timeout */

		mutex_enter(&kernel_mutex);
		srv_fatal_semaphore_wait_threshold -= 7200; /* 2 hours */
		mutex_exit(&kernel_mutex);

167
		return;
168
	}
169

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
170
	field = rec_get_nth_field_old(rec, 0, &len);
171

172
	if (!rec_get_deleted_flag(rec, 0)) {
173 174 175

		/* We found one */

176
		char*	table_name = mem_strdupl((char*) field, len);
177

178 179 180
		btr_pcur_store_position(&pcur, &mtr);

		mtr_commit(&mtr);
181

182
		table = dict_table_get_low(table_name);
183
		mem_free(table_name);
184 185

		if (table == NULL) {
186
			fputs("InnoDB: Failed to load table ", stderr);
187
			ut_print_namel(stderr, NULL, (char*) field, len);
188
			putc('\n', stderr);
189
		} else {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
190 191 192 193 194 195
			/* The table definition was corrupt if there
			is no index */

			if (dict_table_get_first_index(table)) {
				dict_update_statistics_low(table, TRUE);
			}
196 197 198 199 200 201 202 203 204 205 206 207

			dict_table_print_low(table);
		}

		mtr_start(&mtr);

		btr_pcur_restore_position(BTR_SEARCH_LEAF, &pcur, &mtr);
	}

	goto loop;
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
208 209 210 211
/************************************************************************
In a crash recovery we already have all the tablespace objects created.
This function compares the space id information in the InnoDB data dictionary
to what we already read with fil_load_single_table_tablespaces().
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
212 213 214 215

In a normal startup, we create the tablespace objects for every table in
InnoDB's data dictionary, if the corresponding .ibd file exists.
We also scan the biggest space id, and store it to fil_system. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
216 217

void
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
218 219
dict_check_tablespaces_and_store_max_id(
/*====================================*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
220 221 222 223 224 225 226 227 228 229 230
	ibool	in_crash_recovery)	/* in: are we doing a crash recovery */
{
	dict_table_t*	sys_tables;
	dict_index_t*	sys_index;
	btr_pcur_t	pcur;
	rec_t*		rec;
	byte*		field;
	ulint		len;
	ulint		space_id;
	ulint		max_space_id	= 0;
	mtr_t		mtr;
231

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
232 233 234 235
	mutex_enter(&(dict_sys->mutex));

	mtr_start(&mtr);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
236
	sys_tables = dict_table_get_low("SYS_TABLES");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
237
	sys_index = UT_LIST_GET_FIRST(sys_tables->indexes);
238
	ut_a(!dict_table_is_comp(sys_tables));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
239 240 241 242 243 244 245 246 247 248 249 250 251

	btr_pcur_open_at_index_side(TRUE, sys_index, BTR_SEARCH_LEAF, &pcur,
								TRUE, &mtr);
loop:
	btr_pcur_move_to_next_user_rec(&pcur, &mtr);

	rec = btr_pcur_get_rec(&pcur);

	if (!btr_pcur_is_on_user_rec(&pcur, &mtr)) {
		/* end of index */

		btr_pcur_close(&pcur);
		mtr_commit(&mtr);
252

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
253 254 255 256
		/* We must make the tablespace cache aware of the biggest
		known space id */

		/* printf("Biggest space id in data dictionary %lu\n",
257
		   max_space_id); */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
258 259 260 261 262
		fil_set_max_space_id_if_bigger(max_space_id);

		mutex_exit(&(dict_sys->mutex));

		return;
263
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
264

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
265
	field = rec_get_nth_field_old(rec, 0, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
266

267
	if (!rec_get_deleted_flag(rec, 0)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
268 269 270

		/* We found one */

271
		char*	name = mem_strdupl((char*) field, len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
272

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
273
		field = rec_get_nth_field_old(rec, 9, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
274
		ut_a(len == 4);
275

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
276 277 278 279 280
		space_id = mach_read_from_4(field);

		btr_pcur_store_position(&pcur, &mtr);

		mtr_commit(&mtr);
281

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
282 283 284
		if (space_id != 0 && in_crash_recovery) {
			/* Check that the tablespace (the .ibd file) really
			exists; print a warning to the .err log if not */
285

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
286
			fil_space_for_table_exists_in_mem(space_id, name,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
287
							FALSE, TRUE, TRUE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
288
		}
289

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
290 291 292 293 294 295 296 297
		if (space_id != 0 && !in_crash_recovery) {
			/* It is a normal database startup: create the space
			object and check that the .ibd file exists. */

			fil_open_single_table_tablespace(FALSE, space_id,
									name);
		}

298 299
		mem_free(name);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
300 301 302 303 304 305 306 307 308 309 310 311
		if (space_id > max_space_id) {
			max_space_id = space_id;
		}

		mtr_start(&mtr);

		btr_pcur_restore_position(BTR_SEARCH_LEAF, &pcur, &mtr);
	}

	goto loop;
}

312
/************************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
313 314 315 316 317 318 319
Loads definitions for table columns. */
static
void
dict_load_columns(
/*==============*/
	dict_table_t*	table,	/* in: table */
	mem_heap_t*	heap)	/* in: memory heap for temporary storage */
320
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
321
	dict_table_t*	sys_columns;
322
	dict_index_t*	sys_index;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
323
	btr_pcur_t	pcur;
324 325 326 327 328
	dtuple_t*	tuple;
	dfield_t*	dfield;
	rec_t*		rec;
	byte*		field;
	ulint		len;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
329 330 331 332 333 334 335
	byte*		buf;
	char*		name;
	ulint		mtype;
	ulint		prtype;
	ulint		col_len;
	ulint		prec;
	ulint		i;
336
	mtr_t		mtr;
337

338
#ifdef UNIV_SYNC_DEBUG
339
	ut_ad(mutex_own(&(dict_sys->mutex)));
340
#endif /* UNIV_SYNC_DEBUG */
341 342 343

	mtr_start(&mtr);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
344
	sys_columns = dict_table_get_low("SYS_COLUMNS");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
345
	sys_index = UT_LIST_GET_FIRST(sys_columns->indexes);
346
	ut_a(!dict_table_is_comp(sys_columns));
347 348 349 350

	tuple = dtuple_create(heap, 1);
	dfield = dtuple_get_nth_field(tuple, 0);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
351 352 353 354
	buf = mem_heap_alloc(heap, 8);
	mach_write_to_8(buf, table->id);

	dfield_set_data(dfield, buf, 8);
355 356 357
	dict_index_copy_types(tuple, sys_index, 1);

	btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
358
						BTR_SEARCH_LEAF, &pcur, &mtr);
359
	for (i = 0; i < table->n_cols - DATA_N_SYS_COLS; i++) {
360

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
361
		rec = btr_pcur_get_rec(&pcur);
362

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
363
		ut_a(btr_pcur_is_on_user_rec(&pcur, &mtr));
364

365
		ut_a(!rec_get_deleted_flag(rec, 0));
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
366 367

		field = rec_get_nth_field_old(rec, 0, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
368 369
		ut_ad(len == 8);
		ut_a(ut_dulint_cmp(table->id, mach_read_from_8(field)) == 0);
370

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
371
		field = rec_get_nth_field_old(rec, 1, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
372 373
		ut_ad(len == 4);
		ut_a(i == mach_read_from_4(field));
374

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
375
		ut_a(0 == ut_strcmp("NAME",
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
376
			dict_field_get_col(
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
377
			dict_index_get_nth_field(sys_index, 4))->name));
378

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
379
		field = rec_get_nth_field_old(rec, 4, &len);
380
		name = mem_heap_strdupl(heap, (char*) field, len);
381

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
382
		field = rec_get_nth_field_old(rec, 5, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
383
		mtype = mach_read_from_4(field);
384

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
385
		field = rec_get_nth_field_old(rec, 6, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
386
		prtype = mach_read_from_4(field);
387

388 389 390 391 392 393 394 395 396 397 398 399 400
		if (dtype_get_charset_coll(prtype) == 0
				&& dtype_is_string_type(mtype)) {
			/* The table was created with < 4.1.2. */

			if (dtype_is_binary_string_type(mtype, prtype)) {
				/* Use the binary collation for
				string columns of binary type. */

				prtype = dtype_form_prtype(prtype,
					DATA_MYSQL_BINARY_CHARSET_COLL);
			} else {
				/* Use the default charset for
				other than binary columns. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
401

402
				prtype = dtype_form_prtype(prtype,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
403
					data_mysql_default_charset_coll);
404
			}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
405 406
		}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
407
		field = rec_get_nth_field_old(rec, 7, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
408
		col_len = mach_read_from_4(field);
409

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
410
		ut_a(0 == ut_strcmp("PREC",
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
411
			dict_field_get_col(
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
412
			dict_index_get_nth_field(sys_index, 8))->name));
413

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
414
		field = rec_get_nth_field_old(rec, 8, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
415
		prec = mach_read_from_4(field);
416

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
417 418 419
		dict_mem_table_add_col(table, name, mtype, prtype, col_len,
									prec);
		btr_pcur_move_to_next_user_rec(&pcur, &mtr);
420
	}
421

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
422 423
	btr_pcur_close(&pcur);
	mtr_commit(&mtr);
424 425
}

426 427 428 429 430
/************************************************************************
Report that an index field or index for a table has been delete marked. */
static
void
dict_load_report_deleted_index(
431
/*===========================*/
432 433
	const char*	name,	/* in: table name */
	ulint		field)	/* in: index field, or ULINT_UNDEFINED */
434
{
435 436
	fprintf(stderr, "InnoDB: Error: data dictionary entry"
		" for table %s is corrupt!\n", name);
437 438 439
	if (field != ULINT_UNDEFINED) {
		fprintf(stderr,
			"InnoDB: Index field %lu is delete marked.\n", field);
440
	} else {
441 442 443 444
		fputs("InnoDB: An index is delete marked.\n", stderr);
	}
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
445 446 447 448 449 450 451 452 453
/************************************************************************
Loads definitions for index fields. */
static
void
dict_load_fields(
/*=============*/
	dict_table_t*	table,	/* in: table */
	dict_index_t*	index,	/* in: index whose fields to load */
	mem_heap_t*	heap)	/* in: memory heap for temporary storage */
454
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
455 456
	dict_table_t*	sys_fields;
	dict_index_t*	sys_index;
457 458 459
	btr_pcur_t	pcur;
	dtuple_t*	tuple;
	dfield_t*	dfield;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
460 461
	ulint		pos_and_prefix_len;
	ulint		prefix_len;
462 463
	rec_t*		rec;
	byte*		field;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
464 465 466
	ulint		len;
	byte*		buf;
	ulint		i;
467
	mtr_t		mtr;
468

469
#ifdef UNIV_SYNC_DEBUG
470
	ut_ad(mutex_own(&(dict_sys->mutex)));
471
#endif /* UNIV_SYNC_DEBUG */
472

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
473
	UT_NOT_USED(table);
474

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
475
	mtr_start(&mtr);
476

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
477
	sys_fields = dict_table_get_low("SYS_FIELDS");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
478
	sys_index = UT_LIST_GET_FIRST(sys_fields->indexes);
479
	ut_a(!dict_table_is_comp(sys_fields));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
480 481

	tuple = dtuple_create(heap, 1);
482 483
	dfield = dtuple_get_nth_field(tuple, 0);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
484 485
	buf = mem_heap_alloc(heap, 8);
	mach_write_to_8(buf, index->id);
486

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
487 488
	dfield_set_data(dfield, buf, 8);
	dict_index_copy_types(tuple, sys_index, 1);
489

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
490
	btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
491
						BTR_SEARCH_LEAF, &pcur, &mtr);
492
	for (i = 0; i < index->n_fields; i++) {
493

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
494
		rec = btr_pcur_get_rec(&pcur);
495 496

		ut_a(btr_pcur_is_on_user_rec(&pcur, &mtr));
497
		if (rec_get_deleted_flag(rec, 0)) {
498
			dict_load_report_deleted_index(table->name, i);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
499
		}
500

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
501
		field = rec_get_nth_field_old(rec, 0, &len);
502
		ut_ad(len == 8);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
503
		ut_a(ut_memcmp(buf, field, len) == 0);
504

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
505
		field = rec_get_nth_field_old(rec, 1, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
506 507 508 509 510 511 512 513 514 515 516 517
		ut_a(len == 4);

		/* The next field stores the field position in the index
		and a possible column prefix length if the index field
		does not contain the whole column. The storage format is
		like this: if there is at least one prefix field in the index,
		then the HIGH 2 bytes contain the field number (== i) and the
		low 2 bytes the prefix length for the field. Otherwise the
		field number (== i) is contained in the 2 LOW bytes. */

		pos_and_prefix_len = mach_read_from_4(field);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
518
		ut_a((pos_and_prefix_len & 0xFFFFUL) == i
519
			|| (pos_and_prefix_len & 0xFFFF0000UL) == (i << 16));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
520 521

		if ((i == 0 && pos_and_prefix_len > 0)
522
			|| (pos_and_prefix_len & 0xFFFF0000UL) > 0) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
523

524
			prefix_len = pos_and_prefix_len & 0xFFFFUL;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
525
		} else {
526
			prefix_len = 0;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
527
		}
528

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
529
		ut_a(0 == ut_strcmp("COL_NAME",
530
			dict_field_get_col(
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
531
			dict_index_get_nth_field(sys_index, 4))->name));
532

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
533
		field = rec_get_nth_field_old(rec, 4, &len);
534

535
		dict_mem_index_add_field(index,
536
					 mem_heap_strdupl(heap, (char*) field, len), prefix_len);
537 538

		btr_pcur_move_to_next_user_rec(&pcur, &mtr);
539
	}
540 541 542 543 544 545

	btr_pcur_close(&pcur);
	mtr_commit(&mtr);
}

/************************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
546 547
Loads definitions for table indexes. Adds them to the data dictionary
cache. */
548
static
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
549
ibool
550 551
dict_load_indexes(
/*==============*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
552 553
				/* out: TRUE if ok, FALSE if corruption
				of dictionary table */
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
	dict_table_t*	table,	/* in: table */
	mem_heap_t*	heap)	/* in: memory heap for temporary storage */
{
	dict_table_t*	sys_indexes;
	dict_index_t*	sys_index;
	dict_index_t*	index;
	btr_pcur_t	pcur;
	dtuple_t*	tuple;
	dfield_t*	dfield;
	rec_t*		rec;
	byte*		field;
	ulint		len;
	ulint		name_len;
	char*		name_buf;
	ulint		type;
	ulint		space;
	ulint		page_no;
	ulint		n_fields;
	byte*		buf;
	ibool		is_sys_table;
	dulint		id;
	mtr_t		mtr;
576

577
#ifdef UNIV_SYNC_DEBUG
578
	ut_ad(mutex_own(&(dict_sys->mutex)));
579
#endif /* UNIV_SYNC_DEBUG */
580 581

	if ((ut_dulint_get_high(table->id) == 0)
582
		&& (ut_dulint_get_low(table->id) < DICT_HDR_FIRST_ID)) {
583 584 585 586
		is_sys_table = TRUE;
	} else {
		is_sys_table = FALSE;
	}
587

588 589
	mtr_start(&mtr);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
590
	sys_indexes = dict_table_get_low("SYS_INDEXES");
591
	sys_index = UT_LIST_GET_FIRST(sys_indexes->indexes);
592
	ut_a(!dict_table_is_comp(sys_indexes));
593 594 595 596 597 598 599 600 601 602 603 604

	tuple = dtuple_create(heap, 1);
	dfield = dtuple_get_nth_field(tuple, 0);

	buf = mem_heap_alloc(heap, 8);
	mach_write_to_8(buf, table->id);

	dfield_set_data(dfield, buf, 8);
	dict_index_copy_types(tuple, sys_index, 1);

	btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
						BTR_SEARCH_LEAF, &pcur, &mtr);
605
	for (;;) {
606 607 608 609 610 611
		if (!btr_pcur_is_on_user_rec(&pcur, &mtr)) {

			break;
		}

		rec = btr_pcur_get_rec(&pcur);
612

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
613
		field = rec_get_nth_field_old(rec, 0, &len);
614 615 616 617 618 619
		ut_ad(len == 8);

		if (ut_memcmp(buf, field, len) != 0) {
			break;
		}

620
		if (rec_get_deleted_flag(rec, 0)) {
621 622
			dict_load_report_deleted_index(table->name,
				ULINT_UNDEFINED);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
623 624 625 626 627 628

			btr_pcur_close(&pcur);
			mtr_commit(&mtr);

			return(FALSE);
		}
629

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
630
		field = rec_get_nth_field_old(rec, 1, &len);
631 632 633
		ut_ad(len == 8);
		id = mach_read_from_8(field);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
634
		ut_a(0 == ut_strcmp("NAME",
635
			dict_field_get_col(
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
636 637 638
			dict_index_get_nth_field(sys_index, 4))->name));

		field = rec_get_nth_field_old(rec, 4, &name_len);
639
		name_buf = mem_heap_strdupl(heap, (char*) field, name_len);
640

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
641
		field = rec_get_nth_field_old(rec, 5, &len);
642 643
		n_fields = mach_read_from_4(field);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
644
		field = rec_get_nth_field_old(rec, 6, &len);
645 646
		type = mach_read_from_4(field);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
647
		field = rec_get_nth_field_old(rec, 7, &len);
648 649
		space = mach_read_from_4(field);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
650
		ut_a(0 == ut_strcmp("PAGE_NO",
651
			dict_field_get_col(
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
652
			dict_index_get_nth_field(sys_index, 8))->name));
653

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
654
		field = rec_get_nth_field_old(rec, 8, &len);
655 656
		page_no = mach_read_from_4(field);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
657 658
		if (page_no == FIL_NULL) {

659 660 661 662
			fprintf(stderr,
		"InnoDB: Error: trying to load index %s for table %s\n"
		"InnoDB: but the index tree has been freed!\n",
				name_buf, table->name);
663

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
664 665 666 667 668 669 670
			btr_pcur_close(&pcur);
			mtr_commit(&mtr);

			return(FALSE);
		}

		if ((type & DICT_CLUSTERED) == 0
671
			&& NULL == dict_table_get_first_index(table)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
672

673 674 675 676
			fprintf(stderr,
		"InnoDB: Error: trying to load index %s for table %s\n"
		"InnoDB: but the first index is not clustered!\n",
				name_buf, table->name);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
677 678 679 680 681 682

			btr_pcur_close(&pcur);
			mtr_commit(&mtr);

			return(FALSE);
		}
683

684
		if (is_sys_table
685 686 687 688
			&& ((type & DICT_CLUSTERED)
			|| ((table == dict_sys->sys_tables)
				&& (name_len == (sizeof "ID_IND") - 1)
				&& (0 == ut_memcmp(name_buf, "ID_IND",
689 690
							name_len))))) {

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
691 692
			/* The index was created in memory already at booting
			of the database server */
693
		} else {
694
			index = dict_mem_index_create(table->name, name_buf,
695 696
						space, type, n_fields);
			index->id = id;
697

698
			dict_load_fields(table, index, heap);
699
			dict_index_add_to_cache(table, index, page_no);
700 701 702
		}

		btr_pcur_move_to_next_user_rec(&pcur, &mtr);
703
	}
704 705 706

	btr_pcur_close(&pcur);
	mtr_commit(&mtr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
707 708

	return(TRUE);
709 710 711
}

/************************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
712 713 714 715 716 717 718 719 720
Loads a table definition and also all its index definitions, and also
the cluster definition if the table is a member in a cluster. Also loads
all foreign key constraints where the foreign key is in the table or where
a foreign key references columns in this table. Adds all these to the data
dictionary cache. */

dict_table_t*
dict_load_table(
/*============*/
721 722 723 724 725 726 727
				/* out: table, NULL if does not exist;
				if the table is stored in an .ibd file,
				but the file does not exist,
				then we set the ibd_file_missing flag TRUE
				in the table object we return */
	const char*	name)	/* in: table name in the
				databasename/tablename format */
728
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
729
	ibool		ibd_file_missing	= FALSE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
730 731
	dict_table_t*	table;
	dict_table_t*	sys_tables;
732
	btr_pcur_t	pcur;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
733
	dict_index_t*	sys_index;
734
	dtuple_t*	tuple;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
735
	mem_heap_t*	heap;
736 737 738 739
	dfield_t*	dfield;
	rec_t*		rec;
	byte*		field;
	ulint		len;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
740 741
	ulint		space;
	ulint		n_cols;
742
	ulint		flags;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
743
	ulint		err;
744
	mtr_t		mtr;
745

746
#ifdef UNIV_SYNC_DEBUG
747
	ut_ad(mutex_own(&(dict_sys->mutex)));
748
#endif /* UNIV_SYNC_DEBUG */
749

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
750
	heap = mem_heap_create(1000);
751

752 753
	mtr_start(&mtr);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
754
	sys_tables = dict_table_get_low("SYS_TABLES");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
755
	sys_index = UT_LIST_GET_FIRST(sys_tables->indexes);
756
	ut_a(!dict_table_is_comp(sys_tables));
757 758 759 760

	tuple = dtuple_create(heap, 1);
	dfield = dtuple_get_nth_field(tuple, 0);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
761
	dfield_set_data(dfield, name, ut_strlen(name));
762 763 764
	dict_index_copy_types(tuple, sys_index, 1);

	btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
765 766
					BTR_SEARCH_LEAF, &pcur, &mtr);
	rec = btr_pcur_get_rec(&pcur);
767

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
768
	if (!btr_pcur_is_on_user_rec(&pcur, &mtr)
769
			|| rec_get_deleted_flag(rec, 0)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
770
		/* Not found */
771
	err_exit:
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
772 773 774
		btr_pcur_close(&pcur);
		mtr_commit(&mtr);
		mem_heap_free(heap);
775

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
776
		return(NULL);
777
	}
778

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
779
	field = rec_get_nth_field_old(rec, 0, &len);
780

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
781 782
	/* Check if the table name in record is the searched one */
	if (len != ut_strlen(name) || ut_memcmp(name, field, len) != 0) {
783

784
		goto err_exit;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
785
	}
786

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
787
	ut_a(0 == ut_strcmp("SPACE",
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
788
		dict_field_get_col(
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
789
		dict_index_get_nth_field(sys_index, 9))->name));
790

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
791
	field = rec_get_nth_field_old(rec, 9, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
792 793
	space = mach_read_from_4(field);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
794 795 796
	/* Check if the tablespace exists and has the right name */
	if (space != 0) {
		if (fil_space_for_table_exists_in_mem(space, name, FALSE,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
797
							FALSE, FALSE)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
798 799 800
			/* Ok; (if we did a crash recovery then the tablespace
			can already be in the memory cache) */
		} else {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
801 802 803 804 805 806 807 808 809
			/* In >= 4.1.9, InnoDB scans the data dictionary also
			at a normal mysqld startup. It is an error if the
			space object does not exist in memory. */

			ut_print_timestamp(stderr);
			fprintf(stderr,
"  InnoDB: error: space object of table %s,\n"
"InnoDB: space id %lu did not exist in memory. Retrying an open.\n",
							name, (ulong)space);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
810
			/* Try to open the tablespace */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
811 812
			if (!fil_open_single_table_tablespace(TRUE,
							space, name)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
813 814 815 816 817 818 819 820
				/* We failed to find a sensible tablespace
				file */

				ibd_file_missing = TRUE;
			}
		}
	}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
821
	ut_a(0 == ut_strcmp("N_COLS",
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
822
		dict_field_get_col(
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
823
		dict_index_get_nth_field(sys_index, 4))->name));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
824

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
825
	field = rec_get_nth_field_old(rec, 4, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
826 827
	n_cols = mach_read_from_4(field);

828 829
	flags = 0;

830
	/* The high-order bit of N_COLS is the "compact format" flag. */
831 832 833 834 835 836
	if (n_cols & 0x80000000UL) {
		flags |= DICT_TF_COMPACT;
	}

	table = dict_mem_table_create(name, space, n_cols & ~0x80000000UL,
		flags);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
837

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
838 839
	table->ibd_file_missing = ibd_file_missing;

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
840
	ut_a(0 == ut_strcmp("ID",
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
841
		dict_field_get_col(
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
842
		dict_index_get_nth_field(sys_index, 3))->name));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
843

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
844
	field = rec_get_nth_field_old(rec, 3, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
845 846
	table->id = mach_read_from_8(field);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
847
	field = rec_get_nth_field_old(rec, 5, &len);
848 849 850 851 852 853
	if (UNIV_UNLIKELY(mach_read_from_4(field) != DICT_TABLE_ORDINARY)) {
		ut_print_timestamp(stderr);
		fprintf(stderr,
			"  InnoDB: table %s: unknown table type %lu\n",
			name, (ulong) mach_read_from_4(field));
		goto err_exit;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
854
	}
855

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
856 857
	btr_pcur_close(&pcur);
	mtr_commit(&mtr);
858

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
859 860 861
	dict_load_columns(table, heap);

	dict_table_add_to_cache(table);
862

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
863
	dict_load_indexes(table, heap);
864

865
	err = dict_load_foreigns(table->name, TRUE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
866 867
/*
	if (err != DB_SUCCESS) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
868

869 870 871 872
		mutex_enter(&dict_foreign_err_mutex);

		ut_print_timestamp(stderr);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
873 874 875 876 877
		fprintf(stderr,
"  InnoDB: Error: could not make a foreign key definition to match\n"
"InnoDB: the foreign key table or the referenced table!\n"
"InnoDB: The data dictionary of InnoDB is corrupt. You may need to drop\n"
"InnoDB: and recreate the foreign key table or the referenced table.\n"
878
"InnoDB: Submit a detailed bug report to http://bugs.mysql.com\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
879
"InnoDB: Latest foreign key error printout:\n%s\n", dict_foreign_err_buf);
880

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
881 882 883
		mutex_exit(&dict_foreign_err_mutex);
	}
*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
884 885 886 887 888 889 890 891 892 893 894 895
	mem_heap_free(heap);

	return(table);
}

/***************************************************************************
Loads a table object based on the table id. */

dict_table_t*
dict_load_table_on_id(
/*==================*/
				/* out: table; NULL if table does not exist */
896
	dulint	table_id)	/* in: table id */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
897 898 899
{
	byte		id_buf[8];
	btr_pcur_t	pcur;
900
	mem_heap_t*	heap;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
901 902 903 904 905 906
	dtuple_t*	tuple;
	dfield_t*	dfield;
	dict_index_t*	sys_table_ids;
	dict_table_t*	sys_tables;
	rec_t*		rec;
	byte*		field;
907
	ulint		len;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
908 909
	dict_table_t*	table;
	mtr_t		mtr;
910

911
#ifdef UNIV_SYNC_DEBUG
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
912
	ut_ad(mutex_own(&(dict_sys->mutex)));
913
#endif /* UNIV_SYNC_DEBUG */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
914 915 916 917 918

	/* NOTE that the operation of this function is protected by
	the dictionary mutex, and therefore no deadlocks can occur
	with other dictionary operations. */

919
	mtr_start(&mtr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
920
	/*---------------------------------------------------*/
921
	/* Get the secondary index based on ID for table SYS_TABLES */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
922 923 924
	sys_tables = dict_sys->sys_tables;
	sys_table_ids = dict_table_get_next_index(
				dict_table_get_first_index(sys_tables));
925
	ut_a(!dict_table_is_comp(sys_tables));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
926 927 928 929 930 931 932
	heap = mem_heap_create(256);

	tuple  = dtuple_create(heap, 1);
	dfield = dtuple_get_nth_field(tuple, 0);

	/* Write the table id in byte format to id_buf */
	mach_write_to_8(id_buf, table_id);
933

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
934 935 936 937 938 939
	dfield_set_data(dfield, id_buf, 8);
	dict_index_copy_types(tuple, sys_table_ids, 1);

	btr_pcur_open_on_user_rec(sys_table_ids, tuple, PAGE_CUR_GE,
						BTR_SEARCH_LEAF, &pcur, &mtr);
	rec = btr_pcur_get_rec(&pcur);
940

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
941
	if (!btr_pcur_is_on_user_rec(&pcur, &mtr)
942
			|| rec_get_deleted_flag(rec, 0)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
943 944 945 946 947
		/* Not found */

		btr_pcur_close(&pcur);
		mtr_commit(&mtr);
		mem_heap_free(heap);
948

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
949 950 951 952 953 954 955 956
		return(NULL);
	}

	/*---------------------------------------------------*/
	/* Now we have the record in the secondary index containing the
	table ID and NAME */

	rec = btr_pcur_get_rec(&pcur);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
957
	field = rec_get_nth_field_old(rec, 0, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
958
	ut_ad(len == 8);
959

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
960 961 962 963 964 965
	/* Check if the table id in record is the one searched for */
	if (ut_dulint_cmp(table_id, mach_read_from_8(field)) != 0) {

		btr_pcur_close(&pcur);
		mtr_commit(&mtr);
		mem_heap_free(heap);
966

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
967 968
		return(NULL);
	}
969

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
970
	/* Now we get the table name from the record */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
971
	field = rec_get_nth_field_old(rec, 1, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
972
	/* Load the table definition to memory */
973
	table = dict_load_table(mem_heap_strdupl(heap, (char*) field, len));
974

975 976
	btr_pcur_close(&pcur);
	mtr_commit(&mtr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
	mem_heap_free(heap);

	return(table);
}

/************************************************************************
This function is called when the database is booted. Loads system table
index definitions except for the clustered index which is added to the
dictionary cache at booting before calling this function. */

void
dict_load_sys_table(
/*================*/
	dict_table_t*	table)	/* in: system table */
{
	mem_heap_t*	heap;

994
#ifdef UNIV_SYNC_DEBUG
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
995
	ut_ad(mutex_own(&(dict_sys->mutex)));
996
#endif /* UNIV_SYNC_DEBUG */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
997 998 999 1000

	heap = mem_heap_create(1000);

	dict_load_indexes(table, heap);
1001

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1002
	mem_heap_free(heap);
1003 1004
}

1005 1006 1007 1008 1009 1010
/************************************************************************
Loads foreign key constraint col names (also for the referenced table). */
static
void
dict_load_foreign_cols(
/*===================*/
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1011
	const char*	id,	/* in: foreign constraint id as a null-
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
				terminated string */
	dict_foreign_t*	foreign)/* in: foreign constraint object */
{
	dict_table_t*	sys_foreign_cols;
	dict_index_t*	sys_index;
	btr_pcur_t	pcur;
	dtuple_t*	tuple;
	dfield_t*	dfield;
	rec_t*		rec;
	byte*		field;
	ulint		len;
	ulint		i;
	mtr_t		mtr;
1025

1026
#ifdef UNIV_SYNC_DEBUG
1027
	ut_ad(mutex_own(&(dict_sys->mutex)));
1028
#endif /* UNIV_SYNC_DEBUG */
1029 1030 1031 1032 1033 1034 1035 1036

	foreign->foreign_col_names = mem_heap_alloc(foreign->heap,
					foreign->n_fields * sizeof(void*));

	foreign->referenced_col_names = mem_heap_alloc(foreign->heap,
					foreign->n_fields * sizeof(void*));
	mtr_start(&mtr);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1037
	sys_foreign_cols = dict_table_get_low("SYS_FOREIGN_COLS");
1038
	sys_index = UT_LIST_GET_FIRST(sys_foreign_cols->indexes);
1039
	ut_a(!dict_table_is_comp(sys_foreign_cols));
1040 1041 1042 1043 1044 1045 1046 1047 1048

	tuple = dtuple_create(foreign->heap, 1);
	dfield = dtuple_get_nth_field(tuple, 0);

	dfield_set_data(dfield, id, ut_strlen(id));
	dict_index_copy_types(tuple, sys_index, 1);

	btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
						BTR_SEARCH_LEAF, &pcur, &mtr);
1049
	for (i = 0; i < foreign->n_fields; i++) {
1050 1051 1052 1053

		rec = btr_pcur_get_rec(&pcur);

		ut_a(btr_pcur_is_on_user_rec(&pcur, &mtr));
1054
		ut_a(!rec_get_deleted_flag(rec, 0));
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1055 1056

		field = rec_get_nth_field_old(rec, 0, &len);
1057 1058 1059
		ut_a(len == ut_strlen(id));
		ut_a(ut_memcmp(id, field, len) == 0);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1060
		field = rec_get_nth_field_old(rec, 1, &len);
1061 1062 1063
		ut_a(len == 4);
		ut_a(i == mach_read_from_4(field));

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1064
		field = rec_get_nth_field_old(rec, 4, &len);
1065
		foreign->foreign_col_names[i] =
1066
			mem_heap_strdupl(foreign->heap, (char*) field, len);
1067

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1068
		field = rec_get_nth_field_old(rec, 5, &len);
1069
		foreign->referenced_col_names[i] =
1070
		  mem_heap_strdupl(foreign->heap, (char*) field, len);
1071 1072

		btr_pcur_move_to_next_user_rec(&pcur, &mtr);
1073
	}
1074 1075 1076 1077 1078

	btr_pcur_close(&pcur);
	mtr_commit(&mtr);
}

1079
/***************************************************************************
1080 1081 1082 1083 1084
Loads a foreign key constraint to the dictionary cache. */
static
ulint
dict_load_foreign(
/*==============*/
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1085
				/* out: DB_SUCCESS or error code */
1086
	const char*	id,	/* in: foreign constraint id as a
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1087
				null-terminated string */
1088 1089
	ibool		check_charsets)/* in: TRUE=check charset compatibility */
{
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
	dict_foreign_t*	foreign;
	dict_table_t*	sys_foreign;
	btr_pcur_t	pcur;
	dict_index_t*	sys_index;
	dtuple_t*	tuple;
	mem_heap_t*	heap2;
	dfield_t*	dfield;
	rec_t*		rec;
	byte*		field;
	ulint		len;
	mtr_t		mtr;
1101

1102
#ifdef UNIV_SYNC_DEBUG
1103
	ut_ad(mutex_own(&(dict_sys->mutex)));
1104
#endif /* UNIV_SYNC_DEBUG */
1105

1106
	heap2 = mem_heap_create(1000);
1107

1108 1109
	mtr_start(&mtr);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1110
	sys_foreign = dict_table_get_low("SYS_FOREIGN");
1111
	sys_index = UT_LIST_GET_FIRST(sys_foreign->indexes);
1112
	ut_a(!dict_table_is_comp(sys_foreign));
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

	tuple = dtuple_create(heap2, 1);
	dfield = dtuple_get_nth_field(tuple, 0);

	dfield_set_data(dfield, id, ut_strlen(id));
	dict_index_copy_types(tuple, sys_index, 1);

	btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
					BTR_SEARCH_LEAF, &pcur, &mtr);
	rec = btr_pcur_get_rec(&pcur);

	if (!btr_pcur_is_on_user_rec(&pcur, &mtr)
1125
			|| rec_get_deleted_flag(rec, 0)) {
1126 1127
		/* Not found */

1128 1129 1130
		fprintf(stderr,
			"InnoDB: Error A: cannot load foreign constraint %s\n",
			id);
1131 1132 1133 1134

		btr_pcur_close(&pcur);
		mtr_commit(&mtr);
		mem_heap_free(heap2);
1135

1136
		return(DB_ERROR);
1137
	}
1138

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1139
	field = rec_get_nth_field_old(rec, 0, &len);
1140 1141 1142 1143

	/* Check if the id in record is the searched one */
	if (len != ut_strlen(id) || ut_memcmp(id, field, len) != 0) {

1144 1145 1146
		fprintf(stderr,
			"InnoDB: Error B: cannot load foreign constraint %s\n",
			id);
1147 1148 1149 1150

		btr_pcur_close(&pcur);
		mtr_commit(&mtr);
		mem_heap_free(heap2);
1151

1152 1153 1154 1155 1156 1157 1158
		return(DB_ERROR);
	}

	/* Read the table names and the number of columns associated
	with the constraint */

	mem_heap_free(heap2);
1159

1160 1161
	foreign = dict_mem_foreign_create();

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1162 1163
	foreign->n_fields =
		mach_read_from_4(rec_get_nth_field_old(rec, 5, &len));
1164 1165

	ut_a(len == 4);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1166 1167

	/* We store the type to the bits 24-31 of n_fields */
1168

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1169
	foreign->type = foreign->n_fields >> 24;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1170
	foreign->n_fields = foreign->n_fields & 0xFFFFFFUL;
1171

1172
	foreign->id = mem_heap_strdup(foreign->heap, id);
1173

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1174
	field = rec_get_nth_field_old(rec, 3, &len);
1175
	foreign->foreign_table_name =
1176
		mem_heap_strdupl(foreign->heap, (char*) field, len);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1177 1178

	field = rec_get_nth_field_old(rec, 4, &len);
1179
	foreign->referenced_table_name =
1180
		mem_heap_strdupl(foreign->heap, (char*) field, len);
1181 1182 1183 1184 1185 1186

	btr_pcur_close(&pcur);
	mtr_commit(&mtr);

	dict_load_foreign_cols(id, foreign);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1187 1188 1189 1190 1191 1192
	/* If the foreign table is not yet in the dictionary cache, we
	have to load it so that we are able to make type comparisons
	in the next function call. */

	dict_table_get_low(foreign->foreign_table_name);

1193 1194 1195
	/* Note that there may already be a foreign constraint object in
	the dictionary cache for this constraint: then the following
	call only sets the pointers in it to point to the appropriate table
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1196 1197 1198 1199
	and index objects and frees the newly created object foreign.
	Adding to the cache should always succeed since we are not creating
	a new foreign key constraint but loading one from the data
	dictionary. */
1200

1201
	return(dict_foreign_add_to_cache(foreign, check_charsets));
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
}

/***************************************************************************
Loads foreign key constraints where the table is either the foreign key
holder or where the table is referenced by a foreign key. Adds these
constraints to the data dictionary. Note that we know that the dictionary
cache already contains all constraints where the other relevant table is
already in the dictionary cache. */

ulint
dict_load_foreigns(
/*===============*/
1214
					/* out: DB_SUCCESS or error code */
1215
	const char*	table_name,	/* in: table name */
1216 1217
	ibool		check_charsets)	/* in: TRUE=check charset
					compatibility */
1218 1219
{
	btr_pcur_t	pcur;
1220
	mem_heap_t*	heap;
1221 1222
	dtuple_t*	tuple;
	dfield_t*	dfield;
1223 1224
	dict_index_t*	sec_index;
	dict_table_t*	sys_foreign;
1225 1226
	rec_t*		rec;
	byte*		field;
1227
	ulint		len;
1228 1229 1230
	char*		id ;
	ulint		err;
	mtr_t		mtr;
1231

1232
#ifdef UNIV_SYNC_DEBUG
1233
	ut_ad(mutex_own(&(dict_sys->mutex)));
1234
#endif /* UNIV_SYNC_DEBUG */
1235

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1236
	sys_foreign = dict_table_get_low("SYS_FOREIGN");
1237 1238 1239 1240 1241 1242

	if (sys_foreign == NULL) {
		/* No foreign keys defined yet in this database */

		fprintf(stderr,
	"InnoDB: Error: no foreign key system tables in the database\n");
1243

1244 1245
		return(DB_ERROR);
	}
1246

1247 1248
	ut_a(!dict_table_is_comp(sys_foreign));
	mtr_start(&mtr);
1249 1250

	/* Get the secondary index based on FOR_NAME from table
1251
	SYS_FOREIGN */
1252 1253 1254 1255

	sec_index = dict_table_get_next_index(
				dict_table_get_first_index(sys_foreign));
start_load:
1256 1257 1258 1259 1260
	heap = mem_heap_create(256);

	tuple  = dtuple_create(heap, 1);
	dfield = dtuple_get_nth_field(tuple, 0);

1261 1262
	dfield_set_data(dfield, table_name, ut_strlen(table_name));
	dict_index_copy_types(tuple, sec_index, 1);
1263

1264
	btr_pcur_open_on_user_rec(sec_index, tuple, PAGE_CUR_GE,
1265
						BTR_SEARCH_LEAF, &pcur, &mtr);
1266
loop:
1267
	rec = btr_pcur_get_rec(&pcur);
1268

1269 1270
	if (!btr_pcur_is_on_user_rec(&pcur, &mtr)) {
		/* End of index */
1271

1272
		goto load_next_index;
1273 1274
	}

1275 1276
	/* Now we have the record in the secondary index containing a table
	name and a foreign constraint ID */
1277 1278

	rec = btr_pcur_get_rec(&pcur);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1279
	field = rec_get_nth_field_old(rec, 0, &len);
1280

1281 1282 1283
	/* Check if the table name in the record is the one searched for; the
	following call does the comparison in the latin1_swedish_ci
	charset-collation, in a case-insensitive way. */
1284

1285 1286 1287
	if (0 != cmp_data_data(dfield_get_type(dfield),
			dfield_get_data(dfield), dfield_get_len(dfield),
			field, len)) {
1288

1289
		goto load_next_index;
1290
	}
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300

	/* Since table names in SYS_FOREIGN are stored in a case-insensitive
	order, we have to check that the table name matches also in a binary
	string comparison. On Unix, MySQL allows table names that only differ
	in character case. */

	if (0 != ut_memcmp(field, table_name, len)) {

		goto next_rec;
	}
1301 1302

	if (rec_get_deleted_flag(rec, 0)) {
1303 1304 1305 1306 1307

		goto next_rec;
	}

	/* Now we get a foreign key constraint id */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1308
	field = rec_get_nth_field_old(rec, 1, &len);
1309
	id = mem_heap_strdupl(heap, (char*) field, len);
1310

1311
	btr_pcur_store_position(&pcur, &mtr);
1312

1313 1314 1315
	mtr_commit(&mtr);

	/* Load the foreign constraint definition to the dictionary cache */
1316 1317

	err = dict_load_foreign(id, check_charsets);
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334

	if (err != DB_SUCCESS) {
		btr_pcur_close(&pcur);
		mem_heap_free(heap);

		return(err);
	}

	mtr_start(&mtr);

	btr_pcur_restore_position(BTR_SEARCH_LEAF, &pcur, &mtr);
next_rec:
	btr_pcur_move_to_next_user_rec(&pcur, &mtr);

	goto loop;

load_next_index:
1335 1336 1337
	btr_pcur_close(&pcur);
	mtr_commit(&mtr);
	mem_heap_free(heap);
1338

1339
	sec_index = dict_table_get_next_index(sec_index);
1340

1341 1342
	if (sec_index != NULL) {

1343
		mtr_start(&mtr);
1344 1345 1346 1347 1348

		goto start_load;
	}

	return(DB_SUCCESS);
1349
}