btr0btr.c 82.2 KB
Newer Older
1 2 3 4 5 6 7
/******************************************************
The B-tree

(c) 1994-1996 Innobase Oy

Created 6/2/1994 Heikki Tuuri
*******************************************************/
8

9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include "btr0btr.h"

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

#include "fsp0fsp.h"
#include "page0page.h"
#include "btr0cur.h"
#include "btr0sea.h"
#include "btr0pcur.h"
#include "rem0cmp.h"
#include "lock0lock.h"
#include "ibuf0ibuf.h"
23
#include "trx0trx.h"
24

25
/*
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
Latching strategy of the InnoDB B-tree
--------------------------------------
A tree latch protects all non-leaf nodes of the tree. Each node of a tree
also has a latch of its own.

A B-tree operation normally first acquires an S-latch on the tree. It
searches down the tree and releases the tree latch when it has the
leaf node latch. To save CPU time we do not acquire any latch on
non-leaf nodes of the tree during a search, those pages are only bufferfixed.

If an operation needs to restructure the tree, it acquires an X-latch on
the tree before searching to a leaf node. If it needs, for example, to
split a leaf,
(1) InnoDB decides the split point in the leaf,
(2) allocates a new page,
(3) inserts the appropriate node pointer to the first non-leaf level,
(4) releases the tree X-latch,
(5) and then moves records from the leaf to the new allocated page.

45 46 47 48 49 50 51 52 53 54 55 56 57 58
Node pointers
-------------
Leaf pages of a B-tree contain the index records stored in the
tree. On levels n > 0 we store 'node pointers' to pages on level
n - 1. For each page there is exactly one node pointer stored:
thus the our tree is an ordinary B-tree, not a B-link tree.

A node pointer contains a prefix P of an index record. The prefix
is long enough so that it determines an index record uniquely.
The file page number of the child page is added as the last
field. To the child page we can store node pointers or index records
which are >= P in the alphabetical order, but < P1 if there is
a next node pointer on the level, and P1 is its prefix.

59
If a node pointer with a prefix P points to a non-leaf child,
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
then the leftmost record in the child must have the same
prefix P. If it points to a leaf node, the child is not required
to contain any record with a prefix equal to P. The leaf case
is decided this way to allow arbitrary deletions in a leaf node
without touching upper levels of the tree.

We have predefined a special minimum record which we
define as the smallest record in any alphabetical order.
A minimum record is denoted by setting a bit in the record
header. A minimum record acts as the prefix of a node pointer
which points to a leftmost node on any level of the tree.

File page allocation
--------------------
In the root node of a B-tree there are two file segment headers.
The leaf pages of a tree are allocated from one file segment, to
make them consecutive on disk if possible. From the other file segment
we allocate pages for the non-leaf levels of the tree.
*/

/****************************************************************
Returns the upper level node pointer to a page. It is assumed that
mtr holds an x-latch on the tree. */
static
rec_t*
btr_page_get_father_node_ptr(
/*=========================*/
				/* out: pointer to node pointer record */
88
	dict_index_t*	index,	/* in: index tree */
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	page_t*		page,	/* in: page: must contain at least one
				user record */
	mtr_t*		mtr);	/* in: mtr */
/*****************************************************************
Empties an index page. */
static
void
btr_page_empty(
/*===========*/
	page_t*	page,	/* in: page to be emptied */
	mtr_t*	mtr);	/* in: mtr */
/*****************************************************************
Returns TRUE if the insert fits on the appropriate half-page
with the chosen split_rec. */
static
ibool
btr_page_insert_fits(
/*=================*/
					/* out: TRUE if fits */
	btr_cur_t*	cursor,		/* in: cursor at which insert
					should be made */
	rec_t*		split_rec,	/* in: suggestion for first record
					on upper half-page, or NULL if
					tuple should be first */
unknown's avatar
unknown committed
113 114 115 116
	const ulint*	offsets,	/* in: rec_get_offsets(
					split_rec, cursor->index) */
	dtuple_t*	tuple,		/* in: tuple to insert */
	mem_heap_t*	heap);		/* in: temporary memory heap */
117 118 119

/******************************************************************
Gets the root node of a tree and x-latches it. */
120

121 122 123 124
page_t*
btr_root_get(
/*=========*/
				/* out: root page, x-latched */
125
	dict_index_t*	index,	/* in: index tree */
126 127 128 129 130
	mtr_t*		mtr)	/* in: mtr */
{
	ulint	space;
	ulint	root_page_no;
	page_t*	root;
131

132 133
	space = dict_index_get_space(index);
	root_page_no = dict_index_get_page(index);
134 135

	root = btr_page_get(space, root_page_no, RW_X_LATCH, mtr);
136
	ut_a((ibool)!!page_is_comp(root) == dict_table_is_comp(index->table));
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	return(root);
}

/*****************************************************************
Gets pointer to the previous user record in the tree. It is assumed that
the caller has appropriate latches on the page and its neighbor. */

rec_t*
btr_get_prev_user_rec(
/*==================*/
			/* out: previous user record, NULL if there is none */
	rec_t*	rec,	/* in: record on leaf level */
	mtr_t*	mtr)	/* in: mtr holding a latch on the page, and if
			needed, also to the previous page */
{
	page_t*	page;
	page_t*	prev_page;
	ulint	prev_page_no;
	ulint	space;

158
	if (!page_rec_is_infimum(rec)) {
159

160
		rec_t*	prev_rec = page_rec_get_prev(rec);
161

162
		if (!page_rec_is_infimum(prev_rec)) {
163 164 165 166

			return(prev_rec);
		}
	}
167

168
	page = buf_frame_align(rec);
169 170
	prev_page_no = btr_page_get_prev(page, mtr);
	space = buf_frame_get_space_id(page);
171

172 173 174
	if (prev_page_no != FIL_NULL) {

		prev_page = buf_page_get_with_no_latch(space, prev_page_no,
unknown's avatar
unknown committed
175
						       mtr);
176 177
		/* The caller must already have a latch to the brother */
		ut_ad((mtr_memo_contains(mtr, buf_block_align(prev_page),
unknown's avatar
unknown committed
178 179 180
					 MTR_MEMO_PAGE_S_FIX))
		      || (mtr_memo_contains(mtr, buf_block_align(prev_page),
					    MTR_MEMO_PAGE_X_FIX)));
unknown's avatar
unknown committed
181
		ut_a(page_is_comp(prev_page) == page_is_comp(page));
unknown's avatar
unknown committed
182 183
#ifdef UNIV_BTR_DEBUG
		ut_a(btr_page_get_next(prev_page, mtr)
unknown's avatar
unknown committed
184
		     == buf_frame_get_page_no(page));
unknown's avatar
unknown committed
185
#endif /* UNIV_BTR_DEBUG */
186

187
		return(page_rec_get_prev(page_get_supremum_rec(prev_page)));
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	}

	return(NULL);
}

/*****************************************************************
Gets pointer to the next user record in the tree. It is assumed that the
caller has appropriate latches on the page and its neighbor. */

rec_t*
btr_get_next_user_rec(
/*==================*/
			/* out: next user record, NULL if there is none */
	rec_t*	rec,	/* in: record on leaf level */
	mtr_t*	mtr)	/* in: mtr holding a latch on the page, and if
			needed, also to the next page */
{
	page_t*	page;
	page_t*	next_page;
	ulint	next_page_no;
	ulint	space;

210
	if (!page_rec_is_supremum(rec)) {
211

212
		rec_t*	next_rec = page_rec_get_next(rec);
213

214
		if (!page_rec_is_supremum(next_rec)) {
215 216 217 218

			return(next_rec);
		}
	}
219

220
	page = buf_frame_align(rec);
221 222
	next_page_no = btr_page_get_next(page, mtr);
	space = buf_frame_get_space_id(page);
223

224 225 226
	if (next_page_no != FIL_NULL) {

		next_page = buf_page_get_with_no_latch(space, next_page_no,
unknown's avatar
unknown committed
227
						       mtr);
228 229
		/* The caller must already have a latch to the brother */
		ut_ad((mtr_memo_contains(mtr, buf_block_align(next_page),
unknown's avatar
unknown committed
230 231 232
					 MTR_MEMO_PAGE_S_FIX))
		      || (mtr_memo_contains(mtr, buf_block_align(next_page),
					    MTR_MEMO_PAGE_X_FIX)));
unknown's avatar
unknown committed
233 234
#ifdef UNIV_BTR_DEBUG
		ut_a(btr_page_get_prev(next_page, mtr)
unknown's avatar
unknown committed
235
		     == buf_frame_get_page_no(page));
unknown's avatar
unknown committed
236
#endif /* UNIV_BTR_DEBUG */
237

unknown's avatar
unknown committed
238
		ut_a(page_is_comp(next_page) == page_is_comp(page));
239
		return(page_rec_get_next(page_get_infimum_rec(next_page)));
240 241 242 243 244 245
	}

	return(NULL);
}

/******************************************************************
246 247
Creates a new index page (not the root, and also not
used in page reorganization). */
248 249 250 251 252
static
void
btr_page_create(
/*============*/
	page_t*		page,	/* in: page to be created */
253
	dict_index_t*	index,	/* in: index */
254 255 256
	mtr_t*		mtr)	/* in: mtr */
{
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
257
				MTR_MEMO_PAGE_X_FIX));
258
	page_create(page, mtr, dict_table_is_comp(index->table));
unknown's avatar
unknown committed
259
	buf_block_align(page)->check_index_page_at_flush = TRUE;
260

261
	btr_page_set_index_id(page, index->id, mtr);
262 263 264 265 266 267 268 269 270 271
}

/******************************************************************
Allocates a new file page to be used in an ibuf tree. Takes the page from
the free list of the tree, which must contain pages! */
static
page_t*
btr_page_alloc_for_ibuf(
/*====================*/
				/* out: new allocated page, x-latched */
272
	dict_index_t*	index,	/* in: index tree */
273 274 275 276 277 278
	mtr_t*		mtr)	/* in: mtr */
{
	fil_addr_t	node_addr;
	page_t*		root;
	page_t*		new_page;

279
	root = btr_root_get(index, mtr);
280

281
	node_addr = flst_get_first(root + PAGE_HEADER
unknown's avatar
unknown committed
282
				   + PAGE_BTR_IBUF_FREE_LIST, mtr);
283 284
	ut_a(node_addr.page != FIL_NULL);

285
	new_page = buf_page_get(dict_index_get_space(index), node_addr.page,
unknown's avatar
unknown committed
286
				RW_X_LATCH, mtr);
287
#ifdef UNIV_SYNC_DEBUG
288
	buf_page_dbg_add_level(new_page, SYNC_TREE_NODE_NEW);
289
#endif /* UNIV_SYNC_DEBUG */
290 291

	flst_remove(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
unknown's avatar
unknown committed
292 293 294 295
		    new_page + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST_NODE,
		    mtr);
	ut_ad(flst_validate(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
			    mtr));
296 297 298 299 300 301 302

	return(new_page);
}

/******************************************************************
Allocates a new file page to be used in an index tree. NOTE: we assume
that the caller has made the reservation for free extents! */
303

304 305 306
page_t*
btr_page_alloc(
/*===========*/
307 308
					/* out: new allocated page, x-latched;
					NULL if out of space */
309
	dict_index_t*	index,		/* in: index */
310 311 312 313 314 315 316 317 318 319 320 321
	ulint		hint_page_no,	/* in: hint of a good page */
	byte		file_direction,	/* in: direction where a possible
					page split is made */
	ulint		level,		/* in: level where the page is placed
					in the tree */
	mtr_t*		mtr)		/* in: mtr */
{
	fseg_header_t*	seg_header;
	page_t*		root;
	page_t*		new_page;
	ulint		new_page_no;

322
	if (index->type & DICT_IBUF) {
323

324
		return(btr_page_alloc_for_ibuf(index, mtr));
325 326
	}

327
	root = btr_root_get(index, mtr);
328

329 330 331 332 333 334 335 336 337
	if (level == 0) {
		seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
	} else {
		seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;
	}

	/* Parameter TRUE below states that the caller has made the
	reservation for free extents, and thus we know that a page can
	be allocated: */
338

339
	new_page_no = fseg_alloc_free_page_general(seg_header, hint_page_no,
unknown's avatar
unknown committed
340
						   file_direction, TRUE, mtr);
341 342 343 344
	if (new_page_no == FIL_NULL) {

		return(NULL);
	}
345

346
	new_page = buf_page_get(dict_index_get_space(index), new_page_no,
unknown's avatar
unknown committed
347
				RW_X_LATCH, mtr);
348
#ifdef UNIV_SYNC_DEBUG
349
	buf_page_dbg_add_level(new_page, SYNC_TREE_NODE_NEW);
350
#endif /* UNIV_SYNC_DEBUG */
351

352
	return(new_page);
353
}
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

/******************************************************************
Gets the number of pages in a B-tree. */

ulint
btr_get_size(
/*=========*/
				/* out: number of pages */
	dict_index_t*	index,	/* in: index */
	ulint		flag)	/* in: BTR_N_LEAF_PAGES or BTR_TOTAL_SIZE */
{
	fseg_header_t*	seg_header;
	page_t*		root;
	ulint		n;
	ulint		dummy;
	mtr_t		mtr;

	mtr_start(&mtr);

373
	mtr_s_lock(dict_index_get_lock(index), &mtr);
374

375
	root = btr_root_get(index, &mtr);
376

377 378
	if (flag == BTR_N_LEAF_PAGES) {
		seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
379

380
		fseg_n_reserved_pages(seg_header, &n, &mtr);
381

382 383 384 385
	} else if (flag == BTR_TOTAL_SIZE) {
		seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;

		n = fseg_n_reserved_pages(seg_header, &dummy, &mtr);
386

387
		seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
388 389

		n += fseg_n_reserved_pages(seg_header, &dummy, &mtr);
390
	} else {
391
		ut_error;
392 393 394 395 396
	}

	mtr_commit(&mtr);

	return(n);
397
}
398 399 400 401 402 403 404 405

/******************************************************************
Frees a page used in an ibuf tree. Puts the page to the free list of the
ibuf tree. */
static
void
btr_page_free_for_ibuf(
/*===================*/
406
	dict_index_t*	index,	/* in: index tree */
407
	page_t*		page,	/* in: page to be freed, x-latched */
408 409 410 411 412
	mtr_t*		mtr)	/* in: mtr */
{
	page_t*		root;

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
413
				MTR_MEMO_PAGE_X_FIX));
414
	root = btr_root_get(index, mtr);
415

416
	flst_add_first(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
unknown's avatar
unknown committed
417
		       page + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST_NODE, mtr);
418

unknown's avatar
unknown committed
419
	ut_ad(flst_validate(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
unknown's avatar
unknown committed
420
			    mtr));
421 422 423
}

/******************************************************************
424 425 426 427
Frees a file page used in an index tree. Can be used also to (BLOB)
external storage pages, because the page level 0 can be given as an
argument. */

428
void
429 430
btr_page_free_low(
/*==============*/
431
	dict_index_t*	index,	/* in: index tree */
432
	page_t*		page,	/* in: page to be freed, x-latched */
433
	ulint		level,	/* in: page level */
434 435 436 437 438 439 440 441
	mtr_t*		mtr)	/* in: mtr */
{
	fseg_header_t*	seg_header;
	page_t*		root;
	ulint		space;
	ulint		page_no;

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
442
				MTR_MEMO_PAGE_X_FIX));
443 444 445 446
	/* The page gets invalid for optimistic searches: increment the frame
	modify clock */

	buf_frame_modify_clock_inc(page);
447

448
	if (index->type & DICT_IBUF) {
449

450
		btr_page_free_for_ibuf(index, page, mtr);
451 452 453 454

		return;
	}

455
	root = btr_root_get(index, mtr);
456

457 458 459 460 461 462 463 464
	if (level == 0) {
		seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
	} else {
		seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;
	}

	space = buf_frame_get_space_id(page);
	page_no = buf_frame_get_page_no(page);
465

466
	fseg_free_page(seg_header, space, page_no, mtr);
467
}
468

469 470 471 472 473 474 475
/******************************************************************
Frees a file page used in an index tree. NOTE: cannot free field external
storage pages because the page must contain info on its level. */

void
btr_page_free(
/*==========*/
476
	dict_index_t*	index,	/* in: index tree */
477
	page_t*		page,	/* in: page to be freed, x-latched */
478 479 480 481 482
	mtr_t*		mtr)	/* in: mtr */
{
	ulint		level;

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
483
				MTR_MEMO_PAGE_X_FIX));
484
	level = btr_page_get_level(page, mtr);
485

486
	btr_page_free_low(index, page, level, mtr);
487
}
488

489 490 491 492 493 494
/******************************************************************
Sets the child node file address in a node pointer. */
UNIV_INLINE
void
btr_node_ptr_set_child_page_no(
/*===========================*/
unknown's avatar
unknown committed
495 496 497 498
	rec_t*		rec,	/* in: node pointer record */
	const ulint*	offsets,/* in: array returned by rec_get_offsets() */
	ulint		page_no,/* in: child node address */
	mtr_t*		mtr)	/* in: mtr */
499 500 501 502
{
	byte*	field;
	ulint	len;

unknown's avatar
unknown committed
503
	ut_ad(rec_offs_validate(rec, NULL, offsets));
504
	ut_ad(0 < btr_page_get_level(buf_frame_align(rec), mtr));
unknown's avatar
unknown committed
505
	ut_ad(!rec_offs_comp(offsets) || rec_get_node_ptr_flag(rec));
506

507
	/* The child address is in the last field */
unknown's avatar
unknown committed
508
	field = rec_get_nth_field(rec, offsets,
unknown's avatar
unknown committed
509
				  rec_offs_n_fields(offsets) - 1, &len);
510 511

	ut_ad(len == 4);
512

513 514 515 516 517 518 519 520 521
	mlog_write_ulint(field, page_no, MLOG_4BYTES, mtr);
}

/****************************************************************
Returns the child page of a node pointer and x-latches it. */
static
page_t*
btr_node_ptr_get_child(
/*===================*/
unknown's avatar
unknown committed
522 523 524 525
				/* out: child page, x-latched */
	rec_t*		node_ptr,/* in: node pointer */
	const ulint*	offsets,/* in: array returned by rec_get_offsets() */
	mtr_t*		mtr)	/* in: mtr */
526 527 528 529
{
	ulint	page_no;
	ulint	space;
	page_t*	page;
unknown's avatar
unknown committed
530 531

	ut_ad(rec_offs_validate(node_ptr, NULL, offsets));
532
	space = buf_frame_get_space_id(node_ptr);
unknown's avatar
unknown committed
533
	page_no = btr_node_ptr_get_child_page_no(node_ptr, offsets);
534 535

	page = btr_page_get(space, page_no, RW_X_LATCH, mtr);
536

537 538 539 540 541 542 543 544 545 546 547 548
	return(page);
}

/****************************************************************
Returns the upper level node pointer to a page. It is assumed that mtr holds
an x-latch on the tree. */
static
rec_t*
btr_page_get_father_for_rec(
/*========================*/
				/* out: pointer to node pointer record,
				its page x-latched */
549
	dict_index_t*	index,	/* in: index tree */
550 551 552 553 554 555 556 557 558
	page_t*		page,	/* in: page: must contain at least one
				user record */
	rec_t*		user_rec,/* in: user_record on page */
	mtr_t*		mtr)	/* in: mtr */
{
	mem_heap_t*	heap;
	dtuple_t*	tuple;
	btr_cur_t	cursor;
	rec_t*		node_ptr;
559
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
560
	ulint*		offsets	= offsets_;
561
	*offsets_ = (sizeof offsets_) / sizeof *offsets_;
562

563
	ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index),
unknown's avatar
unknown committed
564
				MTR_MEMO_X_LOCK));
565
	ut_a(page_rec_is_user_rec(user_rec));
566

567
	ut_ad(dict_index_get_page(index) != buf_frame_get_page_no(page));
568 569 570

	heap = mem_heap_create(100);

571 572
	tuple = dict_index_build_node_ptr(index, user_rec, 0, heap,
					  btr_page_get_level(page, mtr));
573

unknown's avatar
unknown committed
574
	btr_cur_search_to_nth_level(index,
unknown's avatar
unknown committed
575 576 577
				    btr_page_get_level(page, mtr) + 1,
				    tuple, PAGE_CUR_LE,
				    BTR_CONT_MODIFY_TREE, &cursor, 0, mtr);
578 579

	node_ptr = btr_cur_get_rec(&cursor);
580
	offsets = rec_get_offsets(node_ptr, index, offsets,
unknown's avatar
unknown committed
581
				  ULINT_UNDEFINED, &heap);
582

unknown's avatar
unknown committed
583 584
	if (UNIV_UNLIKELY(btr_node_ptr_get_child_page_no(node_ptr, offsets)
			  != buf_frame_get_page_no(page))) {
585
		rec_t*	print_rec;
586
		fputs("InnoDB: Dump of the child page:\n", stderr);
unknown's avatar
unknown committed
587
		buf_page_print(buf_frame_align(page));
588
		fputs("InnoDB: Dump of the parent page:\n", stderr);
unknown's avatar
unknown committed
589 590
		buf_page_print(buf_frame_align(node_ptr));

591
		fputs("InnoDB: Corruption of an index tree: table ", stderr);
unknown's avatar
unknown committed
592
		ut_print_name(stderr, NULL, TRUE, index->table_name);
593
		fputs(", index ", stderr);
unknown's avatar
unknown committed
594
		ut_print_name(stderr, NULL, FALSE, index->name);
595
		fprintf(stderr, ",\n"
unknown's avatar
unknown committed
596
			"InnoDB: father ptr page no %lu, child page no %lu\n",
unknown's avatar
unknown committed
597 598
			(ulong)
			btr_node_ptr_get_child_page_no(node_ptr, offsets),
unknown's avatar
unknown committed
599
			(ulong) buf_frame_get_page_no(page));
600 601
		print_rec = page_rec_get_next(page_get_infimum_rec(page));
		offsets = rec_get_offsets(print_rec, index,
unknown's avatar
unknown committed
602
					  offsets, ULINT_UNDEFINED, &heap);
603
		page_rec_print(print_rec, offsets);
604
		offsets = rec_get_offsets(node_ptr, index, offsets,
unknown's avatar
unknown committed
605
					  ULINT_UNDEFINED, &heap);
unknown's avatar
unknown committed
606
		page_rec_print(node_ptr, offsets);
unknown's avatar
unknown committed
607

unknown's avatar
unknown committed
608 609 610 611 612 613 614 615
		fputs("InnoDB: You should dump + drop + reimport the table"
		      " to fix the\n"
		      "InnoDB: corruption. If the crash happens at "
		      "the database startup, see\n"
		      "InnoDB: http://dev.mysql.com/doc/refman/5.1/en/"
		      "forcing-recovery.html about\n"
		      "InnoDB: forcing recovery. "
		      "Then dump + drop + reimport.\n", stderr);
unknown's avatar
unknown committed
616 617
	}

unknown's avatar
unknown committed
618 619
	ut_a(btr_node_ptr_get_child_page_no(node_ptr, offsets)
	     == buf_frame_get_page_no(page));
620 621 622 623 624 625 626 627 628 629 630 631 632
	mem_heap_free(heap);

	return(node_ptr);
}

/****************************************************************
Returns the upper level node pointer to a page. It is assumed that
mtr holds an x-latch on the tree. */
static
rec_t*
btr_page_get_father_node_ptr(
/*=========================*/
				/* out: pointer to node pointer record */
633
	dict_index_t*	index,	/* in: index tree */
634 635 636 637
	page_t*		page,	/* in: page: must contain at least one
				user record */
	mtr_t*		mtr)	/* in: mtr */
{
638 639 640
	return(btr_page_get_father_for_rec(
		       index, page,
		       page_rec_get_next(page_get_infimum_rec(page)), mtr));
641 642 643 644 645 646 647 648 649 650 651 652 653
}

/****************************************************************
Creates the root node for a new index tree. */

ulint
btr_create(
/*=======*/
			/* out: page number of the created root, FIL_NULL if
			did not succeed */
	ulint	type,	/* in: type of the index */
	ulint	space,	/* in: space where created */
	dulint	index_id,/* in: index id */
654
	ulint	comp,	/* in: nonzero=compact page format */
655 656 657 658 659 660 661 662 663 664 665 666 667 668
	mtr_t*	mtr)	/* in: mini-transaction handle */
{
	ulint		page_no;
	buf_frame_t*	ibuf_hdr_frame;
	buf_frame_t*	frame;
	page_t*		page;

	/* Create the two new segments (one, in the case of an ibuf tree) for
	the index tree; the segment headers are put on the allocated root page
	(for an ibuf tree, not in the root, but on a separate ibuf header
	page) */

	if (type & DICT_IBUF) {
		/* Allocate first the ibuf header page */
669 670
		ibuf_hdr_frame = fseg_create(
			space, 0, IBUF_HEADER + IBUF_TREE_SEG_HEADER, mtr);
671

672
#ifdef UNIV_SYNC_DEBUG
673
		buf_page_dbg_add_level(ibuf_hdr_frame, SYNC_TREE_NODE_NEW);
674
#endif /* UNIV_SYNC_DEBUG */
675
		ut_ad(buf_frame_get_page_no(ibuf_hdr_frame)
unknown's avatar
unknown committed
676
		      == IBUF_HEADER_PAGE_NO);
677
		/* Allocate then the next page to the segment: it will be the
678
		tree root page */
679

unknown's avatar
unknown committed
680 681 682 683
		page_no = fseg_alloc_free_page(ibuf_hdr_frame + IBUF_HEADER
					       + IBUF_TREE_SEG_HEADER,
					       IBUF_TREE_ROOT_PAGE_NO,
					       FSP_UP, mtr);
684 685 686 687 688
		ut_ad(page_no == IBUF_TREE_ROOT_PAGE_NO);

		frame = buf_page_get(space, page_no, RW_X_LATCH, mtr);
	} else {
		frame = fseg_create(space, 0, PAGE_HEADER + PAGE_BTR_SEG_TOP,
unknown's avatar
unknown committed
689
				    mtr);
690
	}
691

692 693 694 695 696 697
	if (frame == NULL) {

		return(FIL_NULL);
	}

	page_no = buf_frame_get_page_no(frame);
698

699
#ifdef UNIV_SYNC_DEBUG
700
	buf_page_dbg_add_level(frame, SYNC_TREE_NODE_NEW);
701
#endif /* UNIV_SYNC_DEBUG */
702 703 704 705 706

	if (type & DICT_IBUF) {
		/* It is an insert buffer tree: initialize the free list */

		ut_ad(page_no == IBUF_TREE_ROOT_PAGE_NO);
707

708
		flst_init(frame + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST, mtr);
709
	} else {
710 711
		/* It is a non-ibuf tree: create a file segment for leaf
		pages */
712 713 714 715 716 717 718 719 720
		if (!fseg_create(space, page_no,
				 PAGE_HEADER + PAGE_BTR_SEG_LEAF, mtr)) {
			/* Not enough space for new segment, free root
			segment before return. */
			btr_free_root(space, page_no, mtr);

			return(FIL_NULL);
		}

721 722
		/* The fseg create acquires a second latch on the page,
		therefore we must declare it: */
723
#ifdef UNIV_SYNC_DEBUG
724
		buf_page_dbg_add_level(frame, SYNC_TREE_NODE_NEW);
725
#endif /* UNIV_SYNC_DEBUG */
726
	}
727

728
	/* Create a new index page on the the allocated segment page */
unknown's avatar
unknown committed
729
	page = page_create(frame, mtr, comp);
unknown's avatar
unknown committed
730
	buf_block_align(page)->check_index_page_at_flush = TRUE;
731 732 733 734 735 736

	/* Set the index id of the page */
	btr_page_set_index_id(page, index_id, mtr);

	/* Set the level of the new index page */
	btr_page_set_level(page, 0, mtr);
737

738 739 740 741 742 743 744
	/* Set the next node and previous node fields */
	btr_page_set_next(page, FIL_NULL, mtr);
	btr_page_set_prev(page, FIL_NULL, mtr);

	/* We reset the free bits for the page to allow creation of several
	trees in the same mtr, otherwise the latch on a bitmap page would
	prevent it because of the latching order */
745

746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
	ibuf_reset_free_bits_with_type(type, page);

	/* In the following assertion we test that two records of maximum
	allowed size fit on the root page: this fact is needed to ensure
	correctness of split algorithms */

	ut_ad(page_get_max_insert_size(page, 2) > 2 * BTR_PAGE_MAX_REC_SIZE);

	return(page_no);
}

/****************************************************************
Frees a B-tree except the root page, which MUST be freed after this
by calling btr_free_root. */

void
btr_free_but_not_root(
/*==================*/
	ulint	space,		/* in: space where created */
	ulint	root_page_no)	/* in: root page number */
{
	ibool	finished;
	page_t*	root;
	mtr_t	mtr;

771
leaf_loop:
772
	mtr_start(&mtr);
773 774

	root = btr_page_get(space, root_page_no, RW_X_LATCH, &mtr);
775 776 777 778

	/* NOTE: page hash indexes are dropped when a page is freed inside
	fsp0fsp. */

779 780
	finished = fseg_free_step(root + PAGE_HEADER + PAGE_BTR_SEG_LEAF,
				  &mtr);
781 782 783 784 785 786 787 788
	mtr_commit(&mtr);

	if (!finished) {

		goto leaf_loop;
	}
top_loop:
	mtr_start(&mtr);
789 790

	root = btr_page_get(space, root_page_no, RW_X_LATCH, &mtr);
791

792 793
	finished = fseg_free_step_not_header(
		root + PAGE_HEADER + PAGE_BTR_SEG_TOP, &mtr);
794 795 796 797 798
	mtr_commit(&mtr);

	if (!finished) {

		goto top_loop;
799
	}
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
}

/****************************************************************
Frees the B-tree root page. Other tree MUST already have been freed. */

void
btr_free_root(
/*==========*/
	ulint	space,		/* in: space where created */
	ulint	root_page_no,	/* in: root page number */
	mtr_t*	mtr)		/* in: a mini-transaction which has already
				been started */
{
	ibool	finished;
	page_t*	root;

	root = btr_page_get(space, root_page_no, RW_X_LATCH, mtr);

818 819
	btr_search_drop_page_hash_index(root);
top_loop:
820
	finished = fseg_free_step(root + PAGE_HEADER + PAGE_BTR_SEG_TOP, mtr);
821 822 823
	if (!finished) {

		goto top_loop;
824
	}
825 826 827 828
}

/*****************************************************************
Reorganizes an index page. */
unknown's avatar
unknown committed
829
static
830 831 832
void
btr_page_reorganize_low(
/*====================*/
unknown's avatar
unknown committed
833 834 835 836 837 838 839 840
	ibool		recovery,/* in: TRUE if called in recovery:
				locks should not be updated, i.e.,
				there cannot exist locks on the
				page, and a hash index should not be
				dropped: it cannot exist */
	page_t*		page,	/* in: page to be reorganized */
	dict_index_t*	index,	/* in: record descriptor */
	mtr_t*		mtr)	/* in: mtr */
841 842 843
{
	page_t*	new_page;
	ulint	log_mode;
unknown's avatar
unknown committed
844 845 846 847
	ulint	data_size1;
	ulint	data_size2;
	ulint	max_ins_size1;
	ulint	max_ins_size2;
848 849

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
850
				MTR_MEMO_PAGE_X_FIX));
851
	ut_ad(!!page_is_comp(page) == dict_table_is_comp(index->table));
unknown's avatar
unknown committed
852 853 854
	data_size1 = page_get_data_size(page);
	max_ins_size1 = page_get_max_insert_size_after_reorganize(page, 1);

855
	/* Write the log record */
856
	mlog_open_and_write_index(mtr, page, index, page_is_comp(page)
unknown's avatar
unknown committed
857 858
				  ? MLOG_COMP_PAGE_REORGANIZE
				  : MLOG_PAGE_REORGANIZE, 0);
859 860 861 862 863 864 865 866 867

	/* Turn logging off */
	log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);

	new_page = buf_frame_alloc();

	/* Copy the old page to temporary space */
	buf_frame_copy(new_page, page);

unknown's avatar
unknown committed
868 869 870
	if (!recovery) {
		btr_search_drop_page_hash_index(page);
	}
871 872 873 874

	/* Recreate the page: note that global data on page (possible
	segment headers, next page-field, etc.) is preserved intact */

875
	page_create(page, mtr, page_is_comp(page));
unknown's avatar
unknown committed
876
	buf_block_align(page)->check_index_page_at_flush = TRUE;
877

878 879 880 881
	/* Copy the records from the temporary space to the recreated page;
	do not copy the lock bits yet */

	page_copy_rec_list_end_no_locks(page, new_page,
unknown's avatar
unknown committed
882 883
					page_get_infimum_rec(new_page),
					index, mtr);
884 885
	/* Copy max trx id to recreated page */
	page_set_max_trx_id(page, page_get_max_trx_id(new_page));
886

unknown's avatar
unknown committed
887
	if (!recovery) {
888 889 890 891
		/* Update the record lock bitmaps */
		lock_move_reorganize_page(page, new_page);
	}

unknown's avatar
unknown committed
892 893 894 895 896 897
	data_size2 = page_get_data_size(page);
	max_ins_size2 = page_get_max_insert_size_after_reorganize(page, 1);

	if (data_size1 != data_size2 || max_ins_size1 != max_ins_size2) {
		buf_page_print(page);
		buf_page_print(new_page);
898
		fprintf(stderr,
unknown's avatar
unknown committed
899 900 901 902 903 904
			"InnoDB: Error: page old data size %lu"
			" new data size %lu\n"
			"InnoDB: Error: page old max ins size %lu"
			" new max ins size %lu\n"
			"InnoDB: Submit a detailed bug report"
			" to http://bugs.mysql.com\n",
905 906 907
			(unsigned long) data_size1, (unsigned long) data_size2,
			(unsigned long) max_ins_size1,
			(unsigned long) max_ins_size2);
unknown's avatar
unknown committed
908 909
	}

910 911 912 913 914 915 916 917 918 919 920 921
	buf_frame_free(new_page);

	/* Restore logging mode */
	mtr_set_log_mode(mtr, log_mode);
}

/*****************************************************************
Reorganizes an index page. */

void
btr_page_reorganize(
/*================*/
unknown's avatar
unknown committed
922 923 924
	page_t*		page,	/* in: page to be reorganized */
	dict_index_t*	index,	/* in: record descriptor */
	mtr_t*		mtr)	/* in: mtr */
925
{
unknown's avatar
unknown committed
926
	btr_page_reorganize_low(FALSE, page, index, mtr);
927 928 929 930 931 932 933 934
}

/***************************************************************
Parses a redo log record of reorganizing a page. */

byte*
btr_parse_page_reorganize(
/*======================*/
unknown's avatar
unknown committed
935 936 937 938 939 940 941
				/* out: end of log record or NULL */
	byte*		ptr,	/* in: buffer */
	byte*		end_ptr __attribute__((unused)),
				/* in: buffer end */
	dict_index_t*	index,	/* in: record descriptor */
	page_t*		page,	/* in: page or NULL */
	mtr_t*		mtr)	/* in: mtr or NULL */
942 943 944 945 946 947
{
	ut_ad(ptr && end_ptr);

	/* The record is empty, except for the record initial part */

	if (page) {
unknown's avatar
unknown committed
948
		btr_page_reorganize_low(TRUE, page, index, mtr);
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
	}

	return(ptr);
}

/*****************************************************************
Empties an index page. */
static
void
btr_page_empty(
/*===========*/
	page_t*	page,	/* in: page to be emptied */
	mtr_t*	mtr)	/* in: mtr */
{
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
964
				MTR_MEMO_PAGE_X_FIX));
965 966 967 968 969
	btr_search_drop_page_hash_index(page);

	/* Recreate the page: note that global data on page (possible
	segment headers, next page-field, etc.) is preserved intact */

unknown's avatar
unknown committed
970
	page_create(page, mtr, page_is_comp(page));
unknown's avatar
unknown committed
971
	buf_block_align(page)->check_index_page_at_flush = TRUE;
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
}

/*****************************************************************
Makes tree one level higher by splitting the root, and inserts
the tuple. It is assumed that mtr contains an x-latch on the tree.
NOTE that the operation of this function must always succeed,
we cannot reverse it: therefore enough free disk space must be
guaranteed to be available before this function is called. */

rec_t*
btr_root_raise_and_insert(
/*======================*/
				/* out: inserted record */
	btr_cur_t*	cursor,	/* in: cursor at which to insert: must be
				on the root page; when the function returns,
				the cursor is positioned on the predecessor
				of the inserted record */
	dtuple_t*	tuple,	/* in: tuple to insert */
	mtr_t*		mtr)	/* in: mtr */
{
992
	dict_index_t*	index;
993 994 995 996 997 998
	page_t*		root;
	page_t*		new_page;
	ulint		new_page_no;
	rec_t*		rec;
	mem_heap_t*	heap;
	dtuple_t*	node_ptr;
999
	ulint		level;
1000 1001
	rec_t*		node_ptr_rec;
	page_cur_t*	page_cursor;
1002

1003
	root = btr_cur_get_page(cursor);
1004
	index = btr_cur_get_index(cursor);
1005

1006 1007
	ut_ad(dict_index_get_page(index) == buf_frame_get_page_no(root));
	ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index),
unknown's avatar
unknown committed
1008
				MTR_MEMO_X_LOCK));
1009
	ut_ad(mtr_memo_contains(mtr, buf_block_align(root),
unknown's avatar
unknown committed
1010
				MTR_MEMO_PAGE_X_FIX));
1011 1012 1013 1014 1015
	btr_search_drop_page_hash_index(root);

	/* Allocate a new page to the tree. Root splitting is done by first
	moving the root records to the new page, emptying the root, putting
	a node pointer to the new page, and then splitting the new page. */
1016

1017
	new_page = btr_page_alloc(index, 0, FSP_NO_DIR,
1018 1019
				  btr_page_get_level(root, mtr), mtr);

1020
	btr_page_create(new_page, index, mtr);
1021 1022

	level = btr_page_get_level(root, mtr);
1023

1024 1025 1026
	/* Set the levels of the new index page and root page */
	btr_page_set_level(new_page, level, mtr);
	btr_page_set_level(root, level + 1, mtr);
1027

1028 1029 1030 1031 1032 1033 1034
	/* Set the next node and previous node fields of new page */
	btr_page_set_next(new_page, FIL_NULL, mtr);
	btr_page_set_prev(new_page, FIL_NULL, mtr);

	/* Move the records from root to the new page */

	page_move_rec_list_end(new_page, root, page_get_infimum_rec(root),
1035
			       index, mtr);
1036 1037 1038 1039
	/* If this is a pessimistic insert which is actually done to
	perform a pessimistic update then we have stored the lock
	information of the record to be inserted on the infimum of the
	root page: we cannot discard the lock structs on the root page */
1040

1041 1042 1043 1044 1045 1046 1047
	lock_update_root_raise(new_page, root);

	/* Create a memory heap where the node pointer is stored */
	heap = mem_heap_create(100);

	rec = page_rec_get_next(page_get_infimum_rec(new_page));
	new_page_no = buf_frame_get_page_no(new_page);
1048

1049 1050 1051
	/* Build the node pointer (= node key and page address) for the
	child */

1052 1053
	node_ptr = dict_index_build_node_ptr(index, rec, new_page_no, heap,
					     level);
1054
	/* Reorganize the root to get free space */
1055
	btr_page_reorganize(root, index, mtr);
1056 1057

	page_cursor = btr_cur_get_page_cur(cursor);
1058

1059 1060 1061 1062
	/* Insert node pointer to the root */

	page_cur_set_before_first(root, page_cursor);

unknown's avatar
unknown committed
1063
	node_ptr_rec = page_cur_tuple_insert(page_cursor, node_ptr,
1064
					     index, mtr);
1065 1066 1067 1068 1069 1070 1071

	ut_ad(node_ptr_rec);

	/* The node pointer must be marked as the predefined minimum record,
	as there is no lower alphabetical limit to records in the leftmost
	node of a level: */

1072
	btr_set_min_rec_mark(node_ptr_rec, page_is_comp(root), mtr);
1073

1074 1075 1076 1077 1078
	/* Free the memory heap */
	mem_heap_free(heap);

	/* We play safe and reset the free bits for the new page */

unknown's avatar
unknown committed
1079 1080 1081 1082
#if 0
	fprintf(stderr, "Root raise new page no %lu\n",
		buf_frame_get_page_no(new_page));
#endif
1083

1084
	ibuf_reset_free_bits(index, new_page);
1085
	/* Reposition the cursor to the child node */
1086
	page_cur_search(new_page, index, tuple,
unknown's avatar
unknown committed
1087
			PAGE_CUR_LE, page_cursor);
1088

1089 1090
	/* Split the child and insert tuple */
	return(btr_page_split_and_insert(cursor, tuple, mtr));
1091
}
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113

/*****************************************************************
Decides if the page should be split at the convergence point of inserts
converging to the left. */

ibool
btr_page_get_split_rec_to_left(
/*===========================*/
				/* out: TRUE if split recommended */
	btr_cur_t*	cursor,	/* in: cursor at which to insert */
	rec_t**		split_rec) /* out: if split recommended,
				the first record on upper half page,
				or NULL if tuple to be inserted should
				be first */
{
	page_t*	page;
	rec_t*	insert_point;
	rec_t*	infimum;

	page = btr_cur_get_page(cursor);
	insert_point = btr_cur_get_rec(cursor);

unknown's avatar
unknown committed
1114
	if (page_header_get_ptr(page, PAGE_LAST_INSERT)
unknown's avatar
unknown committed
1115
	    == page_rec_get_next(insert_point)) {
1116 1117

		infimum = page_get_infimum_rec(page);
1118

unknown's avatar
unknown committed
1119 1120 1121 1122 1123 1124
		/* If the convergence is in the middle of a page, include also
		the record immediately before the new insert to the upper
		page. Otherwise, we could repeatedly move from page to page
		lots of records smaller than the convergence point. */

		if (infimum != insert_point
unknown's avatar
unknown committed
1125
		    && page_rec_get_next(infimum) != insert_point) {
1126 1127 1128

			*split_rec = insert_point;
		} else {
1129 1130
			*split_rec = page_rec_get_next(insert_point);
		}
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157

		return(TRUE);
	}

	return(FALSE);
}

/*****************************************************************
Decides if the page should be split at the convergence point of inserts
converging to the right. */

ibool
btr_page_get_split_rec_to_right(
/*============================*/
				/* out: TRUE if split recommended */
	btr_cur_t*	cursor,	/* in: cursor at which to insert */
	rec_t**		split_rec) /* out: if split recommended,
				the first record on upper half page,
				or NULL if tuple to be inserted should
				be first */
{
	page_t*	page;
	rec_t*	insert_point;

	page = btr_cur_get_page(cursor);
	insert_point = btr_cur_get_rec(cursor);

unknown's avatar
unknown committed
1158 1159 1160 1161
	/* We use eager heuristics: if the new insert would be right after
	the previous insert on the same page, we assume that there is a
	pattern of sequential inserts here. */

1162
	if (UNIV_LIKELY(page_header_get_ptr(page, PAGE_LAST_INSERT)
unknown's avatar
unknown committed
1163
			== insert_point)) {
1164

1165 1166 1167 1168 1169
		rec_t*	next_rec;

		next_rec = page_rec_get_next(insert_point);

		if (page_rec_is_supremum(next_rec)) {
1170
split_at_new:
1171
			/* Split at the new record to insert */
1172
			*split_rec = NULL;
1173 1174 1175
		} else {
			rec_t*	next_next_rec = page_rec_get_next(next_rec);
			if (page_rec_is_supremum(next_next_rec)) {
1176

1177 1178
				goto split_at_new;
			}
unknown's avatar
unknown committed
1179 1180 1181 1182 1183 1184 1185

			/* If there are >= 2 user records up from the insert
			point, split all but 1 off. We want to keep one because
			then sequential inserts can use the adaptive hash
			index, as they can do the necessary checks of the right
			search position just by looking at the records on this
			page. */
1186

1187 1188
			*split_rec = next_next_rec;
		}
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208

		return(TRUE);
	}

	return(FALSE);
}

/*****************************************************************
Calculates a split record such that the tuple will certainly fit on
its half-page when the split is performed. We assume in this function
only that the cursor page has at least one user record. */
static
rec_t*
btr_page_get_sure_split_rec(
/*========================*/
					/* out: split record, or NULL if
					tuple will be the first record on
					upper half-page */
	btr_cur_t*	cursor,		/* in: cursor at which insert
					should be made */
1209
	dtuple_t*	tuple)		/* in: tuple to insert */
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
{
	page_t*	page;
	ulint	insert_size;
	ulint	free_space;
	ulint	total_data;
	ulint	total_n_recs;
	ulint	total_space;
	ulint	incl_data;
	rec_t*	ins_rec;
	rec_t*	rec;
	rec_t*	next_rec;
	ulint	n;
unknown's avatar
unknown committed
1222 1223 1224
	mem_heap_t* heap;
	ulint*	offsets;

1225
	page = btr_cur_get_page(cursor);
1226

unknown's avatar
unknown committed
1227
	insert_size = rec_get_converted_size(cursor->index, tuple);
1228
	free_space  = page_get_free_space_of_empty(page_is_comp(page));
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241

	/* free_space is now the free space of a created new page */

	total_data   = page_get_data_size(page) + insert_size;
	total_n_recs = page_get_n_recs(page) + 1;
	ut_ad(total_n_recs >= 2);
	total_space  = total_data + page_dir_calc_reserved_space(total_n_recs);

	n = 0;
	incl_data = 0;
	ins_rec = btr_cur_get_rec(cursor);
	rec = page_get_infimum_rec(page);

1242
	heap = NULL;
unknown's avatar
unknown committed
1243 1244
	offsets = NULL;

1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
	/* We start to include records to the left half, and when the
	space reserved by them exceeds half of total_space, then if
	the included records fit on the left page, they will be put there
	if something was left over also for the right page,
	otherwise the last included record will be the first on the right
	half page */

	for (;;) {
		/* Decide the next record to include */
		if (rec == ins_rec) {
			rec = NULL;	/* NULL denotes that tuple is
					now included */
		} else if (rec == NULL) {
			rec = page_rec_get_next(ins_rec);
		} else {
			rec = page_rec_get_next(rec);
		}

		if (rec == NULL) {
			/* Include tuple */
			incl_data += insert_size;
		} else {
1267
			offsets = rec_get_offsets(rec, cursor->index,
unknown's avatar
unknown committed
1268 1269
						  offsets, ULINT_UNDEFINED,
						  &heap);
unknown's avatar
unknown committed
1270
			incl_data += rec_offs_size(offsets);
1271 1272 1273
		}

		n++;
1274

1275
		if (incl_data + page_dir_calc_reserved_space(n)
unknown's avatar
unknown committed
1276
		    >= total_space / 2) {
1277

1278
			if (incl_data + page_dir_calc_reserved_space(n)
unknown's avatar
unknown committed
1279
			    <= free_space) {
1280 1281 1282
				/* The next record will be the first on
				the right half page if it is not the
				supremum record of page */
1283 1284

				if (rec == ins_rec) {
1285
					rec = NULL;
1286

1287
					goto func_exit;
1288 1289 1290 1291 1292
				} else if (rec == NULL) {
					next_rec = page_rec_get_next(ins_rec);
				} else {
					next_rec = page_rec_get_next(rec);
				}
1293 1294
				ut_ad(next_rec);
				if (!page_rec_is_supremum(next_rec)) {
1295
					rec = next_rec;
1296
				}
1297
			}
1298

1299
func_exit:
1300
			if (UNIV_LIKELY_NULL(heap)) {
1301 1302
				mem_heap_free(heap);
			}
1303 1304 1305
			return(rec);
		}
	}
1306
}
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320

/*****************************************************************
Returns TRUE if the insert fits on the appropriate half-page with the
chosen split_rec. */
static
ibool
btr_page_insert_fits(
/*=================*/
					/* out: TRUE if fits */
	btr_cur_t*	cursor,		/* in: cursor at which insert
					should be made */
	rec_t*		split_rec,	/* in: suggestion for first record
					on upper half-page, or NULL if
					tuple to be inserted should be first */
unknown's avatar
unknown committed
1321 1322 1323 1324
	const ulint*	offsets,	/* in: rec_get_offsets(
					split_rec, cursor->index) */
	dtuple_t*	tuple,		/* in: tuple to insert */
	mem_heap_t*	heap)		/* in: temporary memory heap */
1325 1326 1327 1328 1329 1330 1331 1332
{
	page_t*	page;
	ulint	insert_size;
	ulint	free_space;
	ulint	total_data;
	ulint	total_n_recs;
	rec_t*	rec;
	rec_t*	end_rec;
unknown's avatar
unknown committed
1333
	ulint*	offs;
1334

1335
	page = btr_cur_get_page(cursor);
unknown's avatar
unknown committed
1336 1337 1338

	ut_ad(!split_rec == !offsets);
	ut_ad(!offsets
unknown's avatar
unknown committed
1339
	      || !page_is_comp(page) == !rec_offs_comp(offsets));
unknown's avatar
unknown committed
1340
	ut_ad(!offsets
unknown's avatar
unknown committed
1341
	      || rec_offs_validate(split_rec, cursor->index, offsets));
unknown's avatar
unknown committed
1342 1343

	insert_size = rec_get_converted_size(cursor->index, tuple);
1344
	free_space  = page_get_free_space_of_empty(page_is_comp(page));
1345 1346 1347 1348 1349

	/* free_space is now the free space of a created new page */

	total_data   = page_get_data_size(page) + insert_size;
	total_n_recs = page_get_n_recs(page) + 1;
1350

1351 1352 1353
	/* We determine which records (from rec to end_rec, not including
	end_rec) will end up on the other half page from tuple when it is
	inserted. */
1354

1355 1356 1357 1358
	if (split_rec == NULL) {
		rec = page_rec_get_next(page_get_infimum_rec(page));
		end_rec = page_rec_get_next(btr_cur_get_rec(cursor));

unknown's avatar
unknown committed
1359
	} else if (cmp_dtuple_rec(tuple, split_rec, offsets) >= 0) {
1360 1361

		rec = page_rec_get_next(page_get_infimum_rec(page));
1362
		end_rec = split_rec;
1363 1364 1365 1366 1367 1368
	} else {
		rec = split_rec;
		end_rec = page_get_supremum_rec(page);
	}

	if (total_data + page_dir_calc_reserved_space(total_n_recs)
unknown's avatar
unknown committed
1369
	    <= free_space) {
1370 1371 1372 1373 1374 1375 1376

		/* Ok, there will be enough available space on the
		half page where the tuple is inserted */

		return(TRUE);
	}

unknown's avatar
unknown committed
1377 1378
	offs = NULL;

1379 1380 1381 1382
	while (rec != end_rec) {
		/* In this loop we calculate the amount of reserved
		space after rec is removed from page. */

1383
		offs = rec_get_offsets(rec, cursor->index, offs,
unknown's avatar
unknown committed
1384
				       ULINT_UNDEFINED, &heap);
unknown's avatar
unknown committed
1385 1386

		total_data -= rec_offs_size(offs);
1387 1388 1389
		total_n_recs--;

		if (total_data + page_dir_calc_reserved_space(total_n_recs)
unknown's avatar
unknown committed
1390
		    <= free_space) {
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401

			/* Ok, there will be enough available space on the
			half page where the tuple is inserted */

			return(TRUE);
		}

		rec = page_rec_get_next(rec);
	}

	return(FALSE);
1402
}
1403 1404 1405 1406 1407 1408 1409 1410

/***********************************************************
Inserts a data tuple to a tree on a non-leaf level. It is assumed
that mtr holds an x-latch on the tree. */

void
btr_insert_on_non_leaf_level(
/*=========================*/
1411
	dict_index_t*	index,	/* in: index */
1412 1413 1414 1415
	ulint		level,	/* in: level, must be > 0 */
	dtuple_t*	tuple,	/* in: the record to be inserted */
	mtr_t*		mtr)	/* in: mtr */
{
1416
	big_rec_t*	dummy_big_rec;
1417
	btr_cur_t	cursor;
1418 1419 1420 1421
	ulint		err;
	rec_t*		rec;

	ut_ad(level > 0);
1422

1423
	btr_cur_search_to_nth_level(index, level, tuple, PAGE_CUR_LE,
unknown's avatar
unknown committed
1424 1425
				    BTR_CONT_MODIFY_TREE,
				    &cursor, 0, mtr);
1426 1427

	err = btr_cur_pessimistic_insert(BTR_NO_LOCKING_FLAG
unknown's avatar
unknown committed
1428 1429 1430 1431
					 | BTR_KEEP_SYS_FLAG
					 | BTR_NO_UNDO_LOG_FLAG,
					 &cursor, tuple, &rec,
					 &dummy_big_rec, NULL, mtr);
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
	ut_a(err == DB_SUCCESS);
}

/******************************************************************
Attaches the halves of an index page on the appropriate level in an
index tree. */
static
void
btr_attach_half_pages(
/*==================*/
1442
	dict_index_t*	index,		/* in: the index tree */
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
	page_t*		page,		/* in: page to be split */
	rec_t*		split_rec,	/* in: first record on upper
					half page */
	page_t*		new_page,	/* in: the new half page */
	ulint		direction,	/* in: FSP_UP or FSP_DOWN */
	mtr_t*		mtr)		/* in: mtr */
{
	ulint		space;
	rec_t*		node_ptr;
	page_t*		prev_page;
	page_t*		next_page;
	ulint		prev_page_no;
	ulint		next_page_no;
	ulint		level;
	page_t*		lower_page;
	page_t*		upper_page;
	ulint		lower_page_no;
	ulint		upper_page_no;
	dtuple_t*	node_ptr_upper;
1462
	mem_heap_t*	heap;
1463 1464

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
1465
				MTR_MEMO_PAGE_X_FIX));
1466
	ut_ad(mtr_memo_contains(mtr, buf_block_align(new_page),
unknown's avatar
unknown committed
1467
				MTR_MEMO_PAGE_X_FIX));
unknown's avatar
unknown committed
1468 1469 1470
	ut_a(page_is_comp(page) == page_is_comp(new_page));

	/* Create a memory heap where the data tuple is stored */
1471
	heap = mem_heap_create(1024);
1472 1473 1474 1475 1476 1477 1478 1479 1480

	/* Based on split direction, decide upper and lower pages */
	if (direction == FSP_DOWN) {

		lower_page_no = buf_frame_get_page_no(new_page);
		upper_page_no = buf_frame_get_page_no(page);
		lower_page = new_page;
		upper_page = page;

1481 1482
		/* Look up the index for the node pointer to page */
		node_ptr = btr_page_get_father_node_ptr(index, page, mtr);
1483

1484
		/* Replace the address of the old child node (= page) with the
1485 1486
		address of the new lower half */

1487 1488 1489 1490 1491
		btr_node_ptr_set_child_page_no(node_ptr,
					       rec_get_offsets(
						       node_ptr, index,
						       NULL, ULINT_UNDEFINED,
						       &heap),
unknown's avatar
unknown committed
1492
					       lower_page_no, mtr);
unknown's avatar
unknown committed
1493
		mem_heap_empty(heap);
1494 1495 1496 1497 1498 1499
	} else {
		lower_page_no = buf_frame_get_page_no(page);
		upper_page_no = buf_frame_get_page_no(new_page);
		lower_page = page;
		upper_page = new_page;
	}
1500

1501 1502 1503 1504 1505 1506
	/* Get the level of the split pages */
	level = btr_page_get_level(page, mtr);

	/* Build the node pointer (= node key and page address) for the upper
	half */

1507 1508
	node_ptr_upper = dict_index_build_node_ptr(index, split_rec,
						   upper_page_no, heap, level);
1509 1510 1511 1512

	/* Insert it next to the pointer to the lower half. Note that this
	may generate recursion leading to a split on the higher level. */

1513
	btr_insert_on_non_leaf_level(index, level + 1, node_ptr_upper, mtr);
1514

1515 1516 1517 1518 1519 1520 1521 1522
	/* Free the memory heap */
	mem_heap_free(heap);

	/* Get the previous and next pages of page */

	prev_page_no = btr_page_get_prev(page, mtr);
	next_page_no = btr_page_get_next(page, mtr);
	space = buf_frame_get_space_id(page);
1523

1524
	/* Update page links of the level */
1525

1526 1527 1528
	if (prev_page_no != FIL_NULL) {

		prev_page = btr_page_get(space, prev_page_no, RW_X_LATCH, mtr);
unknown's avatar
unknown committed
1529
		ut_a(page_is_comp(prev_page) == page_is_comp(page));
unknown's avatar
unknown committed
1530 1531
#ifdef UNIV_BTR_DEBUG
		ut_a(btr_page_get_next(prev_page, mtr)
unknown's avatar
unknown committed
1532
		     == buf_frame_get_page_no(page));
unknown's avatar
unknown committed
1533
#endif /* UNIV_BTR_DEBUG */
1534 1535 1536 1537 1538 1539 1540

		btr_page_set_next(prev_page, lower_page_no, mtr);
	}

	if (next_page_no != FIL_NULL) {

		next_page = btr_page_get(space, next_page_no, RW_X_LATCH, mtr);
unknown's avatar
unknown committed
1541
		ut_a(page_is_comp(next_page) == page_is_comp(page));
1542 1543 1544

		btr_page_set_prev(next_page, upper_page_no, mtr);
	}
1545

1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
	btr_page_set_prev(lower_page, prev_page_no, mtr);
	btr_page_set_next(lower_page, upper_page_no, mtr);
	btr_page_set_level(lower_page, level, mtr);

	btr_page_set_prev(upper_page, lower_page_no, mtr);
	btr_page_set_next(upper_page, next_page_no, mtr);
	btr_page_set_level(upper_page, level, mtr);
}

/*****************************************************************
Splits an index page to halves and inserts the tuple. It is assumed
that mtr holds an x-latch to the index tree. NOTE: the tree x-latch
is released within this function! NOTE that the operation of this
function must always succeed, we cannot reverse it: therefore
enough free disk space must be guaranteed to be available before
this function is called. */

rec_t*
btr_page_split_and_insert(
/*======================*/
				/* out: inserted record; NOTE: the tree
				x-latch is released! NOTE: 2 free disk
				pages must be available! */
	btr_cur_t*	cursor,	/* in: cursor at which to insert; when the
				function returns, the cursor is positioned
				on the predecessor of the inserted record */
	dtuple_t*	tuple,	/* in: tuple to insert */
	mtr_t*		mtr)	/* in: mtr */
{
	page_t*		page;
	ulint		page_no;
	byte		direction;
	ulint		hint_page_no;
	page_t*		new_page;
	rec_t*		split_rec;
	page_t*		left_page;
	page_t*		right_page;
	page_t*		insert_page;
	page_cur_t*	page_cursor;
	rec_t*		first_rec;
unknown's avatar
unknown committed
1586
	byte*		buf = 0; /* remove warning */
1587 1588 1589 1590
	rec_t*		move_limit;
	ibool		insert_will_fit;
	ulint		n_iterations = 0;
	rec_t*		rec;
unknown's avatar
unknown committed
1591 1592 1593 1594 1595 1596
	mem_heap_t*	heap;
	ulint		n_uniq;
	ulint*		offsets;

	heap = mem_heap_create(1024);
	n_uniq = dict_index_get_n_unique_in_tree(cursor->index);
1597
func_start:
unknown's avatar
unknown committed
1598 1599
	mem_heap_empty(heap);
	offsets = NULL;
1600

1601
	ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(cursor->index),
unknown's avatar
unknown committed
1602
				MTR_MEMO_X_LOCK));
1603
#ifdef UNIV_SYNC_DEBUG
1604
	ut_ad(rw_lock_own(dict_index_get_lock(cursor->index), RW_LOCK_EX));
1605
#endif /* UNIV_SYNC_DEBUG */
1606 1607 1608 1609

	page = btr_cur_get_page(cursor);

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
1610
				MTR_MEMO_PAGE_X_FIX));
1611
	ut_ad(page_get_n_recs(page) >= 2);
1612

1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
	page_no = buf_frame_get_page_no(page);

	/* 1. Decide the split record; split_rec == NULL means that the
	tuple to be inserted should be the first record on the upper
	half-page */

	if (n_iterations > 0) {
		direction = FSP_UP;
		hint_page_no = page_no + 1;
		split_rec = btr_page_get_sure_split_rec(cursor, tuple);
1623

1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
	} else if (btr_page_get_split_rec_to_right(cursor, &split_rec)) {
		direction = FSP_UP;
		hint_page_no = page_no + 1;

	} else if (btr_page_get_split_rec_to_left(cursor, &split_rec)) {
		direction = FSP_DOWN;
		hint_page_no = page_no - 1;
	} else {
		direction = FSP_UP;
		hint_page_no = page_no + 1;
		split_rec = page_get_middle_rec(page);
	}

1637 1638
	/* 2. Allocate a new page to the index */
	new_page = btr_page_alloc(cursor->index, hint_page_no, direction,
unknown's avatar
unknown committed
1639
				  btr_page_get_level(page, mtr), mtr);
1640
	btr_page_create(new_page, cursor->index, mtr);
1641

1642 1643 1644 1645 1646 1647 1648 1649
	/* 3. Calculate the first record on the upper half-page, and the
	first record (move_limit) on original page which ends up on the
	upper half */

	if (split_rec != NULL) {
		first_rec = split_rec;
		move_limit = split_rec;
	} else {
unknown's avatar
unknown committed
1650
		buf = mem_alloc(rec_get_converted_size(cursor->index, tuple));
1651

unknown's avatar
unknown committed
1652
		first_rec = rec_convert_dtuple_to_rec(buf,
unknown's avatar
unknown committed
1653
						      cursor->index, tuple);
1654 1655
		move_limit = page_rec_get_next(btr_cur_get_rec(cursor));
	}
1656

1657 1658
	/* 4. Do first the modifications in the tree structure */

1659 1660
	btr_attach_half_pages(cursor->index, page, first_rec,
			      new_page, direction, mtr);
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670

	if (split_rec == NULL) {
		mem_free(buf);
	}

	/* If the split is made on the leaf level and the insert will fit
	on the appropriate half-page, we may release the tree x-latch.
	We can then move the records after releasing the tree latch,
	thus reducing the tree latch contention. */

unknown's avatar
unknown committed
1671
	if (split_rec) {
1672
		offsets = rec_get_offsets(split_rec, cursor->index, offsets,
unknown's avatar
unknown committed
1673
					  n_uniq, &heap);
unknown's avatar
unknown committed
1674 1675

		insert_will_fit = btr_page_insert_fits(cursor,
unknown's avatar
unknown committed
1676 1677
						       split_rec, offsets,
						       tuple, heap);
unknown's avatar
unknown committed
1678 1679
	} else {
		insert_will_fit = btr_page_insert_fits(cursor,
unknown's avatar
unknown committed
1680 1681
						       NULL, NULL,
						       tuple, heap);
unknown's avatar
unknown committed
1682
	}
1683

1684 1685
	if (insert_will_fit && (btr_page_get_level(page, mtr) == 0)) {

1686
		mtr_memo_release(mtr, dict_index_get_lock(cursor->index),
unknown's avatar
unknown committed
1687
				 MTR_MEMO_X_LOCK);
1688 1689 1690 1691
	}

	/* 5. Move then the records to the new page */
	if (direction == FSP_DOWN) {
unknown's avatar
unknown committed
1692
		/*		fputs("Split left\n", stderr); */
1693

unknown's avatar
unknown committed
1694
		page_move_rec_list_start(new_page, page, move_limit,
unknown's avatar
unknown committed
1695
					 cursor->index, mtr);
1696 1697 1698 1699 1700
		left_page = new_page;
		right_page = page;

		lock_update_split_left(right_page, left_page);
	} else {
unknown's avatar
unknown committed
1701
		/*		fputs("Split right\n", stderr); */
1702

unknown's avatar
unknown committed
1703
		page_move_rec_list_end(new_page, page, move_limit,
unknown's avatar
unknown committed
1704
				       cursor->index, mtr);
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
		left_page = page;
		right_page = new_page;

		lock_update_split_right(right_page, left_page);
	}

	/* 6. The split and the tree modification is now completed. Decide the
	page where the tuple should be inserted */

	if (split_rec == NULL) {
		insert_page = right_page;

	} else {
1718
		offsets = rec_get_offsets(first_rec, cursor->index,
unknown's avatar
unknown committed
1719
					  offsets, n_uniq, &heap);
unknown's avatar
unknown committed
1720 1721 1722 1723 1724 1725 1726

		if (cmp_dtuple_rec(tuple, first_rec, offsets) >= 0) {

			insert_page = right_page;
		} else {
			insert_page = left_page;
		}
1727 1728 1729 1730 1731
	}

	/* 7. Reposition the cursor for insert and try insertion */
	page_cursor = btr_cur_get_page_cur(cursor);

unknown's avatar
unknown committed
1732
	page_cur_search(insert_page, cursor->index, tuple,
unknown's avatar
unknown committed
1733
			PAGE_CUR_LE, page_cursor);
1734

unknown's avatar
unknown committed
1735
	rec = page_cur_tuple_insert(page_cursor, tuple, cursor->index, mtr);
1736 1737 1738 1739 1740 1741 1742 1743

	if (rec != NULL) {
		/* Insert fit on the page: update the free bits for the
		left and right pages in the same mtr */

		ibuf_update_free_bits_for_two_pages_low(cursor->index,
							left_page,
							right_page, mtr);
1744
		/* fprintf(stderr, "Split and insert done %lu %lu\n",
unknown's avatar
unknown committed
1745 1746
		buf_frame_get_page_no(left_page),
		buf_frame_get_page_no(right_page)); */
unknown's avatar
unknown committed
1747
		mem_heap_free(heap);
1748 1749
		return(rec);
	}
1750

1751 1752
	/* 8. If insert did not fit, try page reorganization */

unknown's avatar
unknown committed
1753
	btr_page_reorganize(insert_page, cursor->index, mtr);
1754

unknown's avatar
unknown committed
1755
	page_cur_search(insert_page, cursor->index, tuple,
unknown's avatar
unknown committed
1756
			PAGE_CUR_LE, page_cursor);
unknown's avatar
unknown committed
1757
	rec = page_cur_tuple_insert(page_cursor, tuple, cursor->index, mtr);
1758 1759 1760 1761

	if (rec == NULL) {
		/* The insert did not fit on the page: loop back to the
		start of the function for a new split */
1762

1763 1764 1765
		/* We play safe and reset the free bits for new_page */
		ibuf_reset_free_bits(cursor->index, new_page);

1766
		/* fprintf(stderr, "Split second round %lu\n",
unknown's avatar
unknown committed
1767
		buf_frame_get_page_no(page)); */
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
		n_iterations++;
		ut_ad(n_iterations < 2);
		ut_ad(!insert_will_fit);

		goto func_start;
	}

	/* Insert fit on the page: update the free bits for the
	left and right pages in the same mtr */

	ibuf_update_free_bits_for_two_pages_low(cursor->index, left_page,
unknown's avatar
unknown committed
1779 1780 1781 1782 1783 1784
						right_page, mtr);
#if 0
	fprintf(stderr, "Split and insert done %lu %lu\n",
		buf_frame_get_page_no(left_page),
		buf_frame_get_page_no(right_page));
#endif
1785

1786 1787
	ut_ad(page_validate(left_page, cursor->index));
	ut_ad(page_validate(right_page, cursor->index));
1788

unknown's avatar
unknown committed
1789
	mem_heap_free(heap);
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
	return(rec);
}

/*****************************************************************
Removes a page from the level list of pages. */
static
void
btr_level_list_remove(
/*==================*/
	page_t*		page,	/* in: page to remove */
	mtr_t*		mtr)	/* in: mtr */
1801
{
1802 1803 1804 1805 1806
	ulint	space;
	ulint	prev_page_no;
	page_t*	prev_page;
	ulint	next_page_no;
	page_t*	next_page;
1807

1808
	ut_ad(page && mtr);
1809
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
1810
				MTR_MEMO_PAGE_X_FIX));
1811 1812 1813 1814 1815
	/* Get the previous and next page numbers of page */

	prev_page_no = btr_page_get_prev(page, mtr);
	next_page_no = btr_page_get_next(page, mtr);
	space = buf_frame_get_space_id(page);
1816

1817
	/* Update page links of the level */
1818

1819 1820 1821
	if (prev_page_no != FIL_NULL) {

		prev_page = btr_page_get(space, prev_page_no, RW_X_LATCH, mtr);
unknown's avatar
unknown committed
1822
		ut_a(page_is_comp(prev_page) == page_is_comp(page));
unknown's avatar
unknown committed
1823 1824
#ifdef UNIV_BTR_DEBUG
		ut_a(btr_page_get_next(prev_page, mtr)
unknown's avatar
unknown committed
1825
		     == buf_frame_get_page_no(page));
unknown's avatar
unknown committed
1826
#endif /* UNIV_BTR_DEBUG */
1827 1828 1829 1830 1831 1832 1833

		btr_page_set_next(prev_page, next_page_no, mtr);
	}

	if (next_page_no != FIL_NULL) {

		next_page = btr_page_get(space, next_page_no, RW_X_LATCH, mtr);
unknown's avatar
unknown committed
1834
		ut_a(page_is_comp(next_page) == page_is_comp(page));
unknown's avatar
unknown committed
1835 1836
#ifdef UNIV_BTR_DEBUG
		ut_a(btr_page_get_prev(next_page, mtr)
unknown's avatar
unknown committed
1837
		     == buf_frame_get_page_no(page));
unknown's avatar
unknown committed
1838
#endif /* UNIV_BTR_DEBUG */
1839 1840 1841 1842

		btr_page_set_prev(next_page, prev_page_no, mtr);
	}
}
1843

1844 1845 1846 1847 1848 1849 1850 1851
/********************************************************************
Writes the redo log record for setting an index record as the predefined
minimum record. */
UNIV_INLINE
void
btr_set_min_rec_mark_log(
/*=====================*/
	rec_t*	rec,	/* in: record */
1852
	ulint	comp,	/* nonzero=compact record format */
1853 1854
	mtr_t*	mtr)	/* in: mtr */
{
1855 1856
	mlog_write_initial_log_record(
		rec, comp ? MLOG_COMP_REC_MIN_MARK : MLOG_REC_MIN_MARK, mtr);
1857 1858

	/* Write rec offset as a 2-byte ulint */
1859
	mlog_catenate_ulint(mtr, page_offset(rec), MLOG_2BYTES);
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
}

/********************************************************************
Parses the redo log record for setting an index record as the predefined
minimum record. */

byte*
btr_parse_set_min_rec_mark(
/*=======================*/
			/* out: end of log record or NULL */
	byte*	ptr,	/* in: buffer */
	byte*	end_ptr,/* in: buffer end */
1872
	ulint	comp,	/* in: nonzero=compact page format */
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
	page_t*	page,	/* in: page or NULL */
	mtr_t*	mtr)	/* in: mtr or NULL */
{
	rec_t*	rec;

	if (end_ptr < ptr + 2) {

		return(NULL);
	}

	if (page) {
1884 1885
		ut_a(!page_is_comp(page) == !comp);

1886 1887
		rec = page + mach_read_from_2(ptr);

unknown's avatar
unknown committed
1888
		btr_set_min_rec_mark(rec, comp, mtr);
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
	}

	return(ptr + 2);
}

/********************************************************************
Sets a record as the predefined minimum record. */

void
btr_set_min_rec_mark(
/*=================*/
	rec_t*	rec,	/* in: record */
1901
	ulint	comp,	/* in: nonzero=compact page format */
1902 1903 1904 1905
	mtr_t*	mtr)	/* in: mtr */
{
	ulint	info_bits;

unknown's avatar
unknown committed
1906
	info_bits = rec_get_info_bits(rec, comp);
1907

unknown's avatar
unknown committed
1908
	rec_set_info_bits(rec, comp, info_bits | REC_INFO_MIN_REC_FLAG);
1909

unknown's avatar
unknown committed
1910
	btr_set_min_rec_mark_log(rec, comp, mtr);
1911 1912 1913 1914 1915 1916 1917 1918
}

/*****************************************************************
Deletes on the upper level the node pointer to a page. */

void
btr_node_ptr_delete(
/*================*/
1919
	dict_index_t*	index,	/* in: index tree */
1920 1921 1922 1923 1924 1925 1926
	page_t*		page,	/* in: page whose node pointer is deleted */
	mtr_t*		mtr)	/* in: mtr */
{
	rec_t*		node_ptr;
	btr_cur_t	cursor;
	ibool		compressed;
	ulint		err;
1927

1928
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
1929
				MTR_MEMO_PAGE_X_FIX));
1930 1931
	/* Delete node pointer on father page */

1932
	node_ptr = btr_page_get_father_node_ptr(index, page, mtr);
1933

1934
	btr_cur_position(index, node_ptr, &cursor);
1935
	compressed = btr_cur_pessimistic_delete(&err, TRUE, &cursor, FALSE,
unknown's avatar
unknown committed
1936
						mtr);
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
	ut_a(err == DB_SUCCESS);

	if (!compressed) {
		btr_cur_compress_if_useful(&cursor, mtr);
	}
}

/*****************************************************************
If page is the only on its level, this function moves its records to the
father page, thus reducing the tree height. */
static
void
btr_lift_page_up(
/*=============*/
1951
	dict_index_t*	index,	/* in: index tree */
1952 1953 1954 1955 1956 1957
	page_t*		page,	/* in: page which is the only on its level;
				must not be empty: use
				btr_discard_only_page_on_level if the last
				record from the page should be removed */
	mtr_t*		mtr)	/* in: mtr */
{
unknown's avatar
unknown committed
1958
	page_t*		father_page;
unknown's avatar
unknown committed
1959 1960
	page_t*		iter_page;
	page_t*		pages[BTR_MAX_LEVELS];
unknown's avatar
unknown committed
1961
	ulint		page_level;
unknown's avatar
unknown committed
1962 1963 1964
	ulint		root_page_no;
	ulint		ancestors;
	ulint		i;
unknown's avatar
unknown committed
1965

1966 1967 1968
	ut_ad(btr_page_get_prev(page, mtr) == FIL_NULL);
	ut_ad(btr_page_get_next(page, mtr) == FIL_NULL);
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
1969
				MTR_MEMO_PAGE_X_FIX));
1970 1971
	father_page = buf_frame_align(
		btr_page_get_father_node_ptr(index, page, mtr));
1972

1973
	page_level = btr_page_get_level(page, mtr);
unknown's avatar
unknown committed
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
	root_page_no = dict_index_get_page(index);

	ancestors = 1;
	pages[0] = father_page;

	/* Store all ancestor pages so we can reset their levels later on.
	We have to do all the searches on the tree now because later on,
	after we've replaced the first level, the tree is in an inconsistent
	state and can not be searched. */
	iter_page = father_page;
	for (;;) {
		if (buf_block_get_page_no(buf_block_align(iter_page))
		    == root_page_no) {

			break;
		}

		ut_a(ancestors < BTR_MAX_LEVELS);

		iter_page = buf_frame_align(
			btr_page_get_father_node_ptr(index, iter_page, mtr));

		pages[ancestors++] = iter_page;
	}
1998 1999

	btr_search_drop_page_hash_index(page);
2000

2001 2002 2003 2004
	/* Make the father empty */
	btr_page_empty(father_page, mtr);

	/* Move records to the father */
2005
	page_copy_rec_list_end(father_page, page, page_get_infimum_rec(page),
unknown's avatar
unknown committed
2006
			       index, mtr);
2007 2008
	lock_update_copy_and_discard(father_page, page);

unknown's avatar
unknown committed
2009 2010 2011 2012 2013 2014 2015 2016 2017
	/* Go upward to root page, decreasing levels by one. */
	for (i = 0; i < ancestors; i++) {
		iter_page = pages[i];

		ut_ad(btr_page_get_level(iter_page, mtr) == (page_level + 1));

		btr_page_set_level(iter_page, page_level, mtr);
		page_level++;
	}
2018 2019

	/* Free the file page */
2020
	btr_page_free(index, page, mtr);
2021 2022

	/* We play safe and reset the free bits for the father */
unknown's avatar
unknown committed
2023 2024
	ibuf_reset_free_bits(index, father_page);
	ut_ad(page_validate(father_page, index));
2025
	ut_ad(btr_check_node_ptr(index, father_page, mtr));
2026
}
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047

/*****************************************************************
Tries to merge the page first to the left immediate brother if such a
brother exists, and the node pointers to the current page and to the brother
reside on the same page. If the left brother does not satisfy these
conditions, looks at the right brother. If the page is the only one on that
level lifts the records of the page to the father page, thus reducing the
tree height. It is assumed that mtr holds an x-latch on the tree and on the
page. If cursor is on the leaf level, mtr must also hold x-latches to the
brothers, if they exist. NOTE: it is assumed that the caller has reserved
enough free extents so that the compression will always succeed if done! */

void
btr_compress(
/*=========*/
	btr_cur_t*	cursor,	/* in: cursor on the page to merge or lift;
				the page must not be empty: in record delete
				use btr_discard_page if the page would become
				empty */
	mtr_t*		mtr)	/* in: mtr */
{
2048
	dict_index_t*	index;
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
	ulint		space;
	ulint		left_page_no;
	ulint		right_page_no;
	page_t*		merge_page;
	page_t*		father_page;
	ibool		is_left;
	page_t*		page;
	rec_t*		orig_pred;
	rec_t*		orig_succ;
	rec_t*		node_ptr;
	ulint		data_size;
	ulint		n_recs;
	ulint		max_ins_size;
	ulint		max_ins_size_reorg;
2063
	ulint		comp;
unknown's avatar
unknown committed
2064

2065
	page = btr_cur_get_page(cursor);
2066
	index = btr_cur_get_index(cursor);
2067
	comp = page_is_comp(page);
2068
	ut_a((ibool)!!comp == dict_table_is_comp(index->table));
2069

2070
	ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index),
unknown's avatar
unknown committed
2071
				MTR_MEMO_X_LOCK));
2072
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
2073
				MTR_MEMO_PAGE_X_FIX));
2074
	space = dict_index_get_space(index);
2075 2076 2077 2078

	left_page_no = btr_page_get_prev(page, mtr);
	right_page_no = btr_page_get_next(page, mtr);

unknown's avatar
unknown committed
2079 2080 2081 2082
#if 0
	fprintf(stderr, "Merge left page %lu right %lu \n",
		left_page_no, right_page_no);
#endif
2083

2084
	node_ptr = btr_page_get_father_node_ptr(index, page, mtr);
unknown's avatar
unknown committed
2085
	ut_ad(!comp || rec_get_status(node_ptr) == REC_STATUS_NODE_PTR);
2086
	father_page = buf_frame_align(node_ptr);
unknown's avatar
unknown committed
2087
	ut_a(comp == page_is_comp(father_page));
2088 2089 2090 2091

	/* Decide the page to which we try to merge and which will inherit
	the locks */

unknown's avatar
unknown committed
2092 2093 2094
	is_left = left_page_no != FIL_NULL;

	if (is_left) {
2095 2096

		merge_page = btr_page_get(space, left_page_no, RW_X_LATCH,
unknown's avatar
unknown committed
2097
					  mtr);
unknown's avatar
unknown committed
2098 2099
#ifdef UNIV_BTR_DEBUG
		ut_a(btr_page_get_next(merge_page, mtr)
unknown's avatar
unknown committed
2100
		     == buf_frame_get_page_no(page));
unknown's avatar
unknown committed
2101
#endif /* UNIV_BTR_DEBUG */
2102 2103 2104
	} else if (right_page_no != FIL_NULL) {

		merge_page = btr_page_get(space, right_page_no, RW_X_LATCH,
unknown's avatar
unknown committed
2105
					  mtr);
unknown's avatar
unknown committed
2106 2107
#ifdef UNIV_BTR_DEBUG
		ut_a(btr_page_get_prev(merge_page, mtr)
unknown's avatar
unknown committed
2108
		     == buf_frame_get_page_no(page));
unknown's avatar
unknown committed
2109
#endif /* UNIV_BTR_DEBUG */
2110 2111 2112
	} else {
		/* The page is the only one on the level, lift the records
		to the father */
2113
		btr_lift_page_up(index, page, mtr);
2114 2115 2116

		return;
	}
2117

2118 2119
	n_recs = page_get_n_recs(page);
	data_size = page_get_data_size(page);
2120
	ut_a(page_is_comp(merge_page) == comp);
2121

2122 2123
	max_ins_size_reorg = page_get_max_insert_size_after_reorganize(
		merge_page, n_recs);
2124 2125 2126 2127 2128 2129 2130
	if (data_size > max_ins_size_reorg) {

		/* No space for merge */

		return;
	}

2131
	ut_ad(page_validate(merge_page, index));
2132 2133

	max_ins_size = page_get_max_insert_size(merge_page, n_recs);
2134

2135 2136 2137 2138
	if (data_size > max_ins_size) {

		/* We have to reorganize merge_page */

2139
		btr_page_reorganize(merge_page, index, mtr);
2140

unknown's avatar
unknown committed
2141 2142
		max_ins_size = page_get_max_insert_size(merge_page, n_recs);

2143
		ut_ad(page_validate(merge_page, index));
2144
		ut_ad(page_get_max_insert_size(merge_page, n_recs)
unknown's avatar
unknown committed
2145
		      == max_ins_size_reorg);
2146 2147
	}

unknown's avatar
unknown committed
2148 2149 2150 2151 2152 2153 2154
	if (data_size > max_ins_size) {

		/* Add fault tolerance, though this should never happen */

		return;
	}

2155 2156 2157
	btr_search_drop_page_hash_index(page);

	/* Remove the page from the level list */
2158
	btr_level_list_remove(page, mtr);
2159 2160

	if (is_left) {
2161
		btr_node_ptr_delete(index, page, mtr);
2162
	} else {
2163
		mem_heap_t*	heap		= NULL;
2164
		ulint		offsets_[REC_OFFS_NORMAL_SIZE];
2165
		*offsets_ = (sizeof offsets_) / sizeof *offsets_;
2166
		/* Replace the address of the old child node (= page) with the
2167 2168
		address of the merge page to the right */

2169 2170 2171 2172 2173 2174
		btr_node_ptr_set_child_page_no(node_ptr,
					       rec_get_offsets(
						       node_ptr, index,
						       offsets_,
						       ULINT_UNDEFINED,
						       &heap),
unknown's avatar
unknown committed
2175
					       right_page_no, mtr);
2176
		if (UNIV_LIKELY_NULL(heap)) {
2177 2178
			mem_heap_free(heap);
		}
2179
		btr_node_ptr_delete(index, merge_page, mtr);
2180
	}
2181

2182 2183
	/* Move records to the merge page */
	if (is_left) {
2184 2185
		orig_pred = page_rec_get_prev(
			page_get_supremum_rec(merge_page));
2186
		page_copy_rec_list_start(merge_page, page,
unknown's avatar
unknown committed
2187
					 page_get_supremum_rec(page),
2188
					 index, mtr);
2189 2190 2191

		lock_update_merge_left(merge_page, orig_pred, page);
	} else {
2192 2193
		orig_succ = page_rec_get_next(
			page_get_infimum_rec(merge_page));
2194
		page_copy_rec_list_end(merge_page, page,
unknown's avatar
unknown committed
2195
				       page_get_infimum_rec(page),
2196
				       index, mtr);
2197 2198 2199 2200 2201

		lock_update_merge_right(orig_succ, page);
	}

	/* We have added new records to merge_page: update its free bits */
2202
	ibuf_update_free_bits_if_full(index, merge_page,
unknown's avatar
unknown committed
2203
				      UNIV_PAGE_SIZE, ULINT_UNDEFINED);
2204

2205
	ut_ad(page_validate(merge_page, index));
2206 2207

	/* Free the file page */
2208
	btr_page_free(index, page, mtr);
2209

2210
	ut_ad(btr_check_node_ptr(index, merge_page, mtr));
2211
}
2212 2213 2214 2215 2216 2217 2218

/*****************************************************************
Discards a page that is the only page on its level. */
static
void
btr_discard_only_page_on_level(
/*===========================*/
2219
	dict_index_t*	index,	/* in: index tree */
2220 2221 2222 2223 2224 2225
	page_t*		page,	/* in: page which is the only on its level */
	mtr_t*		mtr)	/* in: mtr */
{
	rec_t*	node_ptr;
	page_t*	father_page;
	ulint	page_level;
2226

2227 2228 2229
	ut_ad(btr_page_get_prev(page, mtr) == FIL_NULL);
	ut_ad(btr_page_get_next(page, mtr) == FIL_NULL);
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
2230
				MTR_MEMO_PAGE_X_FIX));
2231 2232
	btr_search_drop_page_hash_index(page);

2233
	node_ptr = btr_page_get_father_node_ptr(index, page, mtr);
2234 2235 2236 2237 2238 2239 2240 2241 2242
	father_page = buf_frame_align(node_ptr);

	page_level = btr_page_get_level(page, mtr);

	lock_update_discard(page_get_supremum_rec(father_page), page);

	btr_page_set_level(father_page, page_level, mtr);

	/* Free the file page */
2243
	btr_page_free(index, page, mtr);
2244

2245
	if (buf_frame_get_page_no(father_page) == dict_index_get_page(index)) {
2246 2247 2248 2249 2250
		/* The father is the root page */

		btr_page_empty(father_page, mtr);

		/* We play safe and reset the free bits for the father */
2251
		ibuf_reset_free_bits(index, father_page);
2252 2253 2254
	} else {
		ut_ad(page_get_n_recs(father_page) == 1);

2255
		btr_discard_only_page_on_level(index, father_page, mtr);
2256
	}
2257
}
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270

/*****************************************************************
Discards a page from a B-tree. This is used to remove the last record from
a B-tree page: the whole page must be removed at the same time. This cannot
be used for the root page, which is allowed to be empty. */

void
btr_discard_page(
/*=============*/
	btr_cur_t*	cursor,	/* in: cursor on the page to discard: not on
				the root page */
	mtr_t*		mtr)	/* in: mtr */
{
2271
	dict_index_t*	index;
2272 2273 2274 2275 2276 2277
	ulint		space;
	ulint		left_page_no;
	ulint		right_page_no;
	page_t*		merge_page;
	page_t*		page;
	rec_t*		node_ptr;
2278

2279
	page = btr_cur_get_page(cursor);
2280
	index = btr_cur_get_index(cursor);
2281

2282 2283
	ut_ad(dict_index_get_page(index) != buf_frame_get_page_no(page));
	ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index),
unknown's avatar
unknown committed
2284
				MTR_MEMO_X_LOCK));
2285
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
2286
				MTR_MEMO_PAGE_X_FIX));
2287
	space = dict_index_get_space(index);
2288

2289 2290 2291 2292 2293 2294 2295
	/* Decide the page which will inherit the locks */

	left_page_no = btr_page_get_prev(page, mtr);
	right_page_no = btr_page_get_next(page, mtr);

	if (left_page_no != FIL_NULL) {
		merge_page = btr_page_get(space, left_page_no, RW_X_LATCH,
unknown's avatar
unknown committed
2296
					  mtr);
unknown's avatar
unknown committed
2297 2298
#ifdef UNIV_BTR_DEBUG
		ut_a(btr_page_get_next(merge_page, mtr)
unknown's avatar
unknown committed
2299
		     == buf_frame_get_page_no(page));
unknown's avatar
unknown committed
2300
#endif /* UNIV_BTR_DEBUG */
2301 2302
	} else if (right_page_no != FIL_NULL) {
		merge_page = btr_page_get(space, right_page_no, RW_X_LATCH,
unknown's avatar
unknown committed
2303
					  mtr);
unknown's avatar
unknown committed
2304 2305
#ifdef UNIV_BTR_DEBUG
		ut_a(btr_page_get_prev(merge_page, mtr)
unknown's avatar
unknown committed
2306
		     == buf_frame_get_page_no(page));
unknown's avatar
unknown committed
2307
#endif /* UNIV_BTR_DEBUG */
2308
	} else {
2309
		btr_discard_only_page_on_level(index, page, mtr);
2310 2311 2312 2313

		return;
	}

unknown's avatar
unknown committed
2314
	ut_a(page_is_comp(merge_page) == page_is_comp(page));
2315
	btr_search_drop_page_hash_index(page);
2316

unknown's avatar
unknown committed
2317
	if (left_page_no == FIL_NULL && btr_page_get_level(page, mtr) > 0) {
2318 2319 2320 2321 2322 2323

		/* We have to mark the leftmost node pointer on the right
		side page as the predefined minimum record */

		node_ptr = page_rec_get_next(page_get_infimum_rec(merge_page));

2324
		ut_ad(page_rec_is_user_rec(node_ptr));
2325

2326
		btr_set_min_rec_mark(node_ptr, page_is_comp(merge_page), mtr);
2327 2328
	}

2329
	btr_node_ptr_delete(index, page, mtr);
2330 2331

	/* Remove the page from the level list */
2332
	btr_level_list_remove(page, mtr);
2333

unknown's avatar
unknown committed
2334
	if (left_page_no != FIL_NULL) {
2335 2336
		lock_update_discard(page_get_supremum_rec(merge_page), page);
	} else {
2337 2338 2339
		lock_update_discard(page_rec_get_next(
					    page_get_infimum_rec(merge_page)),
				    page);
2340 2341 2342
	}

	/* Free the file page */
2343
	btr_page_free(index, page, mtr);
2344

2345
	ut_ad(btr_check_node_ptr(index, merge_page, mtr));
2346
}
2347

2348
#ifdef UNIV_BTR_PRINT
2349 2350 2351 2352 2353 2354
/*****************************************************************
Prints size info of a B-tree. */

void
btr_print_size(
/*===========*/
2355
	dict_index_t*	index)	/* in: index tree */
2356 2357 2358 2359 2360
{
	page_t*		root;
	fseg_header_t*	seg;
	mtr_t		mtr;

2361
	if (index->type & DICT_IBUF) {
unknown's avatar
unknown committed
2362 2363
		fputs("Sorry, cannot print info of an ibuf tree:"
		      " use ibuf functions\n", stderr);
2364 2365 2366 2367 2368

		return;
	}

	mtr_start(&mtr);
2369

2370
	root = btr_root_get(index, &mtr);
2371 2372 2373

	seg = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;

2374
	fputs("INFO OF THE NON-LEAF PAGE SEGMENT\n", stderr);
2375 2376
	fseg_print(seg, &mtr);

2377
	if (!(index->type & DICT_UNIVERSAL)) {
2378 2379 2380

		seg = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;

2381
		fputs("INFO OF THE LEAF PAGE SEGMENT\n", stderr);
2382 2383 2384
		fseg_print(seg, &mtr);
	}

2385
	mtr_commit(&mtr);
2386 2387 2388 2389 2390 2391 2392 2393
}

/****************************************************************
Prints recursively index tree pages. */
static
void
btr_print_recursive(
/*================*/
2394
	dict_index_t*	index,	/* in: index tree */
2395 2396 2397
	page_t*		page,	/* in: index page */
	ulint		width,	/* in: print this many entries from start
				and end */
2398 2399
	mem_heap_t**	heap,	/* in/out: heap for rec_get_offsets() */
	ulint**		offsets,/* in/out: buffer for rec_get_offsets() */
2400 2401 2402 2403 2404 2405 2406 2407
	mtr_t*		mtr)	/* in: mtr */
{
	page_cur_t	cursor;
	ulint		n_recs;
	ulint		i	= 0;
	mtr_t		mtr2;
	rec_t*		node_ptr;
	page_t*		child;
unknown's avatar
unknown committed
2408

2409
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
2410
				MTR_MEMO_PAGE_X_FIX));
2411
	fprintf(stderr, "NODE ON LEVEL %lu page number %lu\n",
2412 2413 2414
		(ulong) btr_page_get_level(page, mtr),
		(ulong) buf_frame_get_page_no(page));

unknown's avatar
unknown committed
2415
	page_print(page, index, width, width);
2416

2417
	n_recs = page_get_n_recs(page);
2418

2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
	page_cur_set_before_first(page, &cursor);
	page_cur_move_to_next(&cursor);

	while (!page_cur_is_after_last(&cursor)) {

		if (0 == btr_page_get_level(page, mtr)) {

			/* If this is the leaf level, do nothing */

		} else if ((i <= width) || (i >= n_recs - width)) {

			mtr_start(&mtr2);

			node_ptr = page_cur_get_rec(&cursor);

2434
			*offsets = rec_get_offsets(node_ptr, index, *offsets,
unknown's avatar
unknown committed
2435
						   ULINT_UNDEFINED, heap);
unknown's avatar
unknown committed
2436
			child = btr_node_ptr_get_child(node_ptr,
unknown's avatar
unknown committed
2437
						       *offsets, &mtr2);
2438
			btr_print_recursive(index, child, width,
unknown's avatar
unknown committed
2439
					    heap, offsets, &mtr2);
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
			mtr_commit(&mtr2);
		}

		page_cur_move_to_next(&cursor);
		i++;
	}
}

/******************************************************************
Prints directories and other info of all nodes in the tree. */

void
2452 2453 2454
btr_print_index(
/*============*/
	dict_index_t*	index,	/* in: index */
2455 2456 2457
	ulint		width)	/* in: print this many entries from start
				and end */
{
unknown's avatar
unknown committed
2458 2459
	mtr_t		mtr;
	page_t*		root;
2460
	mem_heap_t*	heap	= NULL;
2461
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
2462
	ulint*		offsets	= offsets_;
2463
	*offsets_ = (sizeof offsets_) / sizeof *offsets_;
2464

2465
	fputs("--------------------------\n"
unknown's avatar
unknown committed
2466
	      "INDEX TREE PRINT\n", stderr);
2467 2468 2469

	mtr_start(&mtr);

2470
	root = btr_root_get(index, &mtr);
2471

2472
	btr_print_recursive(index, root, width, &heap, &offsets, &mtr);
2473
	if (UNIV_LIKELY_NULL(heap)) {
2474 2475
		mem_heap_free(heap);
	}
2476 2477 2478

	mtr_commit(&mtr);

2479
	btr_validate_index(index, NULL);
2480
}
2481
#endif /* UNIV_BTR_PRINT */
2482

2483
#ifdef UNIV_DEBUG
2484 2485 2486 2487 2488 2489 2490
/****************************************************************
Checks that the node pointer to a page is appropriate. */

ibool
btr_check_node_ptr(
/*===============*/
				/* out: TRUE */
2491
	dict_index_t*	index,	/* in: index tree */
2492 2493 2494 2495 2496 2497 2498 2499
	page_t*		page,	/* in: index page */
	mtr_t*		mtr)	/* in: mtr */
{
	mem_heap_t*	heap;
	rec_t*		node_ptr;
	dtuple_t*	node_ptr_tuple;

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
unknown's avatar
unknown committed
2500
				MTR_MEMO_PAGE_X_FIX));
2501
	if (dict_index_get_page(index) == buf_frame_get_page_no(page)) {
2502 2503 2504 2505

		return(TRUE);
	}

2506
	node_ptr = btr_page_get_father_node_ptr(index, page, mtr);
2507

2508 2509 2510 2511
	if (btr_page_get_level(page, mtr) == 0) {

		return(TRUE);
	}
2512

2513
	heap = mem_heap_create(256);
2514

2515 2516 2517
	node_ptr_tuple = dict_index_build_node_ptr(
		index, page_rec_get_next(page_get_infimum_rec(page)), 0, heap,
		btr_page_get_level(page, mtr));
2518

unknown's avatar
unknown committed
2519
	ut_a(!cmp_dtuple_rec(node_ptr_tuple, node_ptr,
2520
			     rec_get_offsets(node_ptr, index,
unknown's avatar
unknown committed
2521
					     NULL, ULINT_UNDEFINED, &heap)));
2522 2523 2524 2525 2526

	mem_heap_free(heap);

	return(TRUE);
}
2527
#endif /* UNIV_DEBUG */
2528

2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
/****************************************************************
Display identification information for a record. */
static
void
btr_index_rec_validate_report(
/*==========================*/
	page_t*		page,	/* in: index page */
	rec_t*		rec,	/* in: index record */
	dict_index_t*	index)	/* in: index */
{
	fputs("InnoDB: Record in ", stderr);
2540
	dict_index_name_print(stderr, NULL, index);
2541 2542 2543 2544
	fprintf(stderr, ", page %lu, at offset %lu\n",
		buf_frame_get_page_no(page), (ulint)(rec - page));
}

unknown's avatar
unknown committed
2545 2546 2547
/****************************************************************
Checks the size and number of fields in a record based on the definition of
the index. */
unknown's avatar
unknown committed
2548

unknown's avatar
unknown committed
2549 2550
ibool
btr_index_rec_validate(
2551
/*===================*/
unknown's avatar
unknown committed
2552 2553 2554 2555 2556 2557
					/* out: TRUE if ok */
	rec_t*		rec,		/* in: index record */
	dict_index_t*	index,		/* in: index */
	ibool		dump_on_error)	/* in: TRUE if the function
					should print hex dump of record
					and page on error */
unknown's avatar
unknown committed
2558
{
unknown's avatar
unknown committed
2559 2560 2561 2562
	ulint		len;
	ulint		n;
	ulint		i;
	page_t*		page;
2563
	mem_heap_t*	heap	= NULL;
2564
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
2565
	ulint*		offsets	= offsets_;
2566
	*offsets_ = (sizeof offsets_) / sizeof *offsets_;
unknown's avatar
unknown committed
2567 2568

	page = buf_frame_align(rec);
2569 2570

	if (UNIV_UNLIKELY(index->type & DICT_UNIVERSAL)) {
2571 2572 2573
		/* The insert buffer index tree can contain records from any
		other index: we cannot check the number of fields or
		their length */
unknown's avatar
unknown committed
2574

2575
		return(TRUE);
unknown's avatar
unknown committed
2576 2577
	}

2578
	if (UNIV_UNLIKELY((ibool)!!page_is_comp(page)
unknown's avatar
unknown committed
2579
			  != dict_table_is_comp(index->table))) {
2580 2581 2582
		btr_index_rec_validate_report(page, rec, index);
		fprintf(stderr, "InnoDB: compact flag=%lu, should be %lu\n",
			(ulong) !!page_is_comp(page),
2583 2584
			(ulong) dict_table_is_comp(index->table));

2585 2586 2587
		return(FALSE);
	}

unknown's avatar
unknown committed
2588 2589
	n = dict_index_get_n_fields(index);

2590
	if (!page_is_comp(page)
unknown's avatar
unknown committed
2591
	    && UNIV_UNLIKELY(rec_get_n_fields_old(rec) != n)) {
2592 2593
		btr_index_rec_validate_report(page, rec, index);
		fprintf(stderr, "InnoDB: has %lu fields, should have %lu\n",
unknown's avatar
unknown committed
2594
			(ulong) rec_get_n_fields_old(rec), (ulong) n);
unknown's avatar
unknown committed
2595

2596 2597
		if (dump_on_error) {
			buf_page_print(page);
unknown's avatar
unknown committed
2598

2599 2600 2601
			fputs("InnoDB: corrupt record ", stderr);
			rec_print_old(stderr, rec);
			putc('\n', stderr);
unknown's avatar
unknown committed
2602
		}
unknown's avatar
unknown committed
2603 2604 2605
		return(FALSE);
	}

2606
	offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, &heap);
unknown's avatar
unknown committed
2607

unknown's avatar
unknown committed
2608
	for (i = 0; i < n; i++) {
2609 2610
		ulint	fixed_size = dict_col_get_fixed_size(
			dict_index_get_nth_col(index, i));
unknown's avatar
unknown committed
2611

unknown's avatar
unknown committed
2612
		rec_get_nth_field(rec, offsets, i, &len);
2613

2614 2615 2616 2617 2618
		/* Note that if fixed_size != 0, it equals the
		length of a fixed-size column in the clustered index.
		A prefix index of the column is of fixed, but different
		length.  When fixed_size == 0, prefix_len is the maximum
		length of the prefix index column. */
2619

unknown's avatar
unknown committed
2620
		if ((dict_index_get_nth_field(index, i)->prefix_len == 0
unknown's avatar
unknown committed
2621 2622 2623 2624 2625 2626
		     && len != UNIV_SQL_NULL && fixed_size
		     && len != fixed_size)
		    || (dict_index_get_nth_field(index, i)->prefix_len > 0
			&& len != UNIV_SQL_NULL
			&& len
			> dict_index_get_nth_field(index, i)->prefix_len)) {
unknown's avatar
unknown committed
2627

2628
			btr_index_rec_validate_report(page, rec, index);
unknown's avatar
unknown committed
2629
			fprintf(stderr,
unknown's avatar
unknown committed
2630 2631
				"InnoDB: field %lu len is %lu,"
				" should be %lu\n",
2632
				(ulong) i, (ulong) len, (ulong) fixed_size);
unknown's avatar
unknown committed
2633

2634 2635 2636 2637 2638 2639 2640
			if (dump_on_error) {
				buf_page_print(page);

				fputs("InnoDB: corrupt record ", stderr);
				rec_print_new(stderr, rec, offsets);
				putc('\n', stderr);
			}
2641
			if (UNIV_LIKELY_NULL(heap)) {
unknown's avatar
unknown committed
2642
				mem_heap_free(heap);
unknown's avatar
unknown committed
2643
			}
unknown's avatar
unknown committed
2644 2645 2646 2647
			return(FALSE);
		}
	}

2648
	if (UNIV_LIKELY_NULL(heap)) {
2649 2650
		mem_heap_free(heap);
	}
2651
	return(TRUE);
unknown's avatar
unknown committed
2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
}

/****************************************************************
Checks the size and number of fields in records based on the definition of
the index. */
static
ibool
btr_index_page_validate(
/*====================*/
				/* out: TRUE if ok */
	page_t*		page,	/* in: index page */
	dict_index_t*	index)	/* in: index */
{
2665
	page_cur_t	cur;
unknown's avatar
unknown committed
2666
	ibool		ret	= TRUE;
2667

unknown's avatar
unknown committed
2668 2669 2670 2671 2672
	page_cur_set_before_first(page, &cur);
	page_cur_move_to_next(&cur);

	for (;;) {
		if (page_cur_is_after_last(&cur)) {
unknown's avatar
unknown committed
2673

unknown's avatar
unknown committed
2674 2675 2676
			break;
		}

2677
		if (!btr_index_rec_validate(cur.rec, index, TRUE)) {
unknown's avatar
unknown committed
2678

unknown's avatar
unknown committed
2679
			return(FALSE);
unknown's avatar
unknown committed
2680 2681 2682 2683 2684
		}

		page_cur_move_to_next(&cur);
	}

2685
	return(ret);
unknown's avatar
unknown committed
2686 2687
}

2688 2689 2690 2691 2692
/****************************************************************
Report an error on one page of an index tree. */
static
void
btr_validate_report1(
2693
/*=================*/
2694 2695 2696 2697 2698 2699 2700
				/* out: TRUE if ok */
	dict_index_t*	index,	/* in: index */
	ulint		level,	/* in: B-tree level */
	page_t*		page)	/* in: index page */
{
	fprintf(stderr, "InnoDB: Error in page %lu of ",
		buf_frame_get_page_no(page));
2701
	dict_index_name_print(stderr, NULL, index);
2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712
	if (level) {
		fprintf(stderr, ", index tree level %lu", level);
	}
	putc('\n', stderr);
}

/****************************************************************
Report an error on two pages of an index tree. */
static
void
btr_validate_report2(
2713
/*=================*/
2714 2715 2716 2717 2718 2719 2720 2721 2722
				/* out: TRUE if ok */
	dict_index_t*	index,	/* in: index */
	ulint		level,	/* in: B-tree level */
	page_t*		page1,	/* in: first index page */
	page_t*		page2)	/* in: second index page */
{
	fprintf(stderr, "InnoDB: Error in pages %lu and %lu of ",
		buf_frame_get_page_no(page1),
		buf_frame_get_page_no(page2));
2723
	dict_index_name_print(stderr, NULL, index);
2724 2725 2726 2727 2728 2729
	if (level) {
		fprintf(stderr, ", index tree level %lu", level);
	}
	putc('\n', stderr);
}

2730 2731 2732
/****************************************************************
Validates index tree level. */
static
unknown's avatar
unknown committed
2733
ibool
2734 2735
btr_validate_level(
/*===============*/
unknown's avatar
unknown committed
2736
				/* out: TRUE if ok */
2737
	dict_index_t*	index,	/* in: index tree */
2738
	trx_t*		trx,	/* in: transaction or NULL */
2739 2740 2741 2742
	ulint		level)	/* in: level number */
{
	ulint		space;
	page_t*		page;
unknown's avatar
unknown committed
2743
	page_t*		right_page = 0; /* remove warning */
2744 2745 2746 2747
	page_t*		father_page;
	page_t*		right_father_page;
	rec_t*		node_ptr;
	rec_t*		right_node_ptr;
unknown's avatar
unknown committed
2748
	rec_t*		rec;
2749 2750 2751 2752
	ulint		right_page_no;
	ulint		left_page_no;
	page_cur_t	cursor;
	dtuple_t*	node_ptr_tuple;
unknown's avatar
unknown committed
2753
	ibool		ret	= TRUE;
unknown's avatar
Merge  
unknown committed
2754
	mtr_t		mtr;
unknown's avatar
unknown committed
2755 2756 2757 2758
	mem_heap_t*	heap	= mem_heap_create(256);
	ulint*		offsets	= NULL;
	ulint*		offsets2= NULL;

2759 2760
	mtr_start(&mtr);

2761
	mtr_x_lock(dict_index_get_lock(index), &mtr);
2762

2763
	page = btr_root_get(index, &mtr);
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774

	space = buf_frame_get_space_id(page);

	while (level != btr_page_get_level(page, &mtr)) {

		ut_a(btr_page_get_level(page, &mtr) > 0);

		page_cur_set_before_first(page, &cursor);
		page_cur_move_to_next(&cursor);

		node_ptr = page_cur_get_rec(&cursor);
2775
		offsets = rec_get_offsets(node_ptr, index, offsets,
unknown's avatar
unknown committed
2776
					  ULINT_UNDEFINED, &heap);
unknown's avatar
unknown committed
2777
		page = btr_node_ptr_get_child(node_ptr, offsets, &mtr);
2778 2779
	}

unknown's avatar
unknown committed
2780 2781
	/* Now we are on the desired level. Loop through the pages on that
	level. */
2782
loop:
2783 2784 2785 2786 2787
	if (trx_is_interrupted(trx)) {
		mtr_commit(&mtr);
		mem_heap_free(heap);
		return(ret);
	}
unknown's avatar
unknown committed
2788 2789
	mem_heap_empty(heap);
	offsets = offsets2 = NULL;
2790
	mtr_x_lock(dict_index_get_lock(index), &mtr);
2791

unknown's avatar
unknown committed
2792 2793 2794
	/* Check ordering etc. of records */

	if (!page_validate(page, index)) {
2795
		btr_validate_report1(index, level, page);
2796

unknown's avatar
unknown committed
2797
		ret = FALSE;
unknown's avatar
unknown committed
2798 2799 2800
	} else if (level == 0) {
		/* We are on level 0. Check that the records have the right
		number of fields, and field lengths are right. */
unknown's avatar
unknown committed
2801 2802

		if (!btr_index_page_validate(page, index)) {
unknown's avatar
unknown committed
2803

unknown's avatar
unknown committed
2804 2805 2806
			ret = FALSE;
		}
	}
2807

2808 2809 2810 2811 2812 2813
	ut_a(btr_page_get_level(page, &mtr) == level);

	right_page_no = btr_page_get_next(page, &mtr);
	left_page_no = btr_page_get_prev(page, &mtr);

	ut_a((page_get_n_recs(page) > 0)
unknown's avatar
unknown committed
2814 2815
	     || ((level == 0)
		 && (buf_frame_get_page_no(page)
2816
		     == dict_index_get_page(index))));
2817 2818

	if (right_page_no != FIL_NULL) {
unknown's avatar
unknown committed
2819
		rec_t*	right_rec;
2820
		right_page = btr_page_get(space, right_page_no, RW_X_LATCH,
unknown's avatar
unknown committed
2821
					  &mtr);
unknown's avatar
unknown committed
2822
		if (UNIV_UNLIKELY(btr_page_get_prev(right_page, &mtr)
unknown's avatar
unknown committed
2823
				  != buf_frame_get_page_no(page))) {
unknown's avatar
unknown committed
2824 2825
			btr_validate_report2(index, level, page, right_page);
			fputs("InnoDB: broken FIL_PAGE_NEXT"
unknown's avatar
unknown committed
2826
			      " or FIL_PAGE_PREV links\n", stderr);
unknown's avatar
unknown committed
2827 2828 2829 2830 2831 2832 2833
			buf_page_print(page);
			buf_page_print(right_page);

			ret = FALSE;
		}

		if (UNIV_UNLIKELY(page_is_comp(right_page)
unknown's avatar
unknown committed
2834
				  != page_is_comp(page))) {
unknown's avatar
unknown committed
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844
			btr_validate_report2(index, level, page, right_page);
			fputs("InnoDB: 'compact' flag mismatch\n", stderr);
			buf_page_print(page);
			buf_page_print(right_page);

			ret = FALSE;

			goto node_ptr_fails;
		}

unknown's avatar
unknown committed
2845
		rec = page_rec_get_prev(page_get_supremum_rec(page));
2846 2847
		right_rec = page_rec_get_next(page_get_infimum_rec(
						      right_page));
2848
		offsets = rec_get_offsets(rec, index,
unknown's avatar
unknown committed
2849
					  offsets, ULINT_UNDEFINED, &heap);
2850
		offsets2 = rec_get_offsets(right_rec, index,
unknown's avatar
unknown committed
2851
					   offsets2, ULINT_UNDEFINED, &heap);
unknown's avatar
unknown committed
2852
		if (UNIV_UNLIKELY(cmp_rec_rec(rec, right_rec,
unknown's avatar
unknown committed
2853 2854
					      offsets, offsets2,
					      index) >= 0)) {
unknown's avatar
Merge  
unknown committed
2855

2856
			btr_validate_report2(index, level, page, right_page);
unknown's avatar
Merge  
unknown committed
2857

2858
			fputs("InnoDB: records in wrong order"
unknown's avatar
unknown committed
2859
			      " on adjacent pages\n", stderr);
unknown's avatar
Merge  
unknown committed
2860

unknown's avatar
unknown committed
2861 2862 2863
			buf_page_print(page);
			buf_page_print(right_page);

2864
			fputs("InnoDB: record ", stderr);
unknown's avatar
unknown committed
2865
			rec = page_rec_get_prev(page_get_supremum_rec(page));
2866
			rec_print(stderr, rec, index);
2867 2868
			putc('\n', stderr);
			fputs("InnoDB: record ", stderr);
2869 2870
			rec = page_rec_get_next(
				page_get_infimum_rec(right_page));
2871
			rec_print(stderr, rec, index);
2872
			putc('\n', stderr);
unknown's avatar
Merge  
unknown committed
2873

2874 2875
			ret = FALSE;
		}
2876
	}
2877

unknown's avatar
Merge  
unknown committed
2878
	if (level > 0 && left_page_no == FIL_NULL) {
2879 2880 2881
		ut_a(REC_INFO_MIN_REC_FLAG & rec_get_info_bits(
			     page_rec_get_next(page_get_infimum_rec(page)),
			     page_is_comp(page)));
2882 2883
	}

2884
	if (buf_frame_get_page_no(page) != dict_index_get_page(index)) {
2885 2886

		/* Check father node pointers */
2887

2888
		node_ptr = btr_page_get_father_node_ptr(index, page, &mtr);
unknown's avatar
unknown committed
2889
		father_page = buf_frame_align(node_ptr);
2890
		offsets	= rec_get_offsets(node_ptr, index,
unknown's avatar
unknown committed
2891
					  offsets, ULINT_UNDEFINED, &heap);
2892

unknown's avatar
unknown committed
2893 2894
		if (btr_node_ptr_get_child_page_no(node_ptr, offsets)
		    != buf_frame_get_page_no(page)
2895 2896 2897 2898
		    || node_ptr != btr_page_get_father_for_rec(
			    index, page,
			    page_rec_get_prev(page_get_supremum_rec(page)),
			    &mtr)) {
2899
			btr_validate_report1(index, level, page);
unknown's avatar
Merge  
unknown committed
2900

2901
			fputs("InnoDB: node pointer to the page is wrong\n",
unknown's avatar
unknown committed
2902
			      stderr);
unknown's avatar
Merge  
unknown committed
2903

unknown's avatar
unknown committed
2904 2905 2906
			buf_page_print(father_page);
			buf_page_print(page);

2907
			fputs("InnoDB: node ptr ", stderr);
2908
			rec_print_new(stderr, node_ptr, offsets);
unknown's avatar
Merge  
unknown committed
2909

2910
			fprintf(stderr, "\n"
unknown's avatar
Merge  
unknown committed
2911
				"InnoDB: node ptr child page n:o %lu\n",
unknown's avatar
unknown committed
2912 2913
				(unsigned long) btr_node_ptr_get_child_page_no
				(node_ptr, offsets));
unknown's avatar
Merge  
unknown committed
2914

2915
			fputs("InnoDB: record on page ", stderr);
2916 2917 2918 2919
			rec = btr_page_get_father_for_rec(
				index, page,
				page_rec_get_prev(page_get_supremum_rec(page)),
				&mtr);
2920
			rec_print(stderr, rec, index);
2921
			putc('\n', stderr);
2922
			ret = FALSE;
unknown's avatar
Merge  
unknown committed
2923

2924
			goto node_ptr_fails;
unknown's avatar
Merge  
unknown committed
2925
		}
2926 2927

		if (btr_page_get_level(page, &mtr) > 0) {
2928
			offsets	= rec_get_offsets(node_ptr, index,
unknown's avatar
unknown committed
2929 2930
						  offsets, ULINT_UNDEFINED,
						  &heap);
2931

2932 2933 2934 2935
			node_ptr_tuple = dict_index_build_node_ptr(
				index,
				page_rec_get_next(page_get_infimum_rec(page)),
				0, heap, btr_page_get_level(page, &mtr));
unknown's avatar
Merge  
unknown committed
2936

unknown's avatar
unknown committed
2937
			if (cmp_dtuple_rec(node_ptr_tuple, node_ptr,
unknown's avatar
unknown committed
2938
					   offsets)) {
2939 2940
				rec_t*	first_rec	= page_rec_get_next(
					page_get_infimum_rec(page));
unknown's avatar
Merge  
unknown committed
2941

2942
				btr_validate_report1(index, level, page);
unknown's avatar
unknown committed
2943 2944 2945

				buf_page_print(father_page);
				buf_page_print(page);
unknown's avatar
Merge  
unknown committed
2946

2947
				fputs("InnoDB: Error: node ptrs differ"
unknown's avatar
unknown committed
2948 2949
				      " on levels > 0\n"
				      "InnoDB: node ptr ", stderr);
2950
				rec_print_new(stderr, node_ptr, offsets);
2951
				fputs("InnoDB: first rec ", stderr);
2952
				rec_print(stderr, first_rec, index);
2953
				putc('\n', stderr);
2954
				ret = FALSE;
unknown's avatar
Merge  
unknown committed
2955

2956
				goto node_ptr_fails;
unknown's avatar
Merge  
unknown committed
2957
			}
2958 2959 2960
		}

		if (left_page_no == FIL_NULL) {
2961 2962
			ut_a(node_ptr == page_rec_get_next(
				     page_get_infimum_rec(father_page)));
2963 2964 2965 2966
			ut_a(btr_page_get_prev(father_page, &mtr) == FIL_NULL);
		}

		if (right_page_no == FIL_NULL) {
2967 2968
			ut_a(node_ptr == page_rec_get_prev(
				     page_get_supremum_rec(father_page)));
2969
			ut_a(btr_page_get_next(father_page, &mtr) == FIL_NULL);
unknown's avatar
unknown committed
2970
		} else {
2971 2972
			right_node_ptr = btr_page_get_father_node_ptr(
				index, right_page, &mtr);
unknown's avatar
unknown committed
2973 2974
			if (page_rec_get_next(node_ptr)
			    != page_get_supremum_rec(father_page)) {
2975

unknown's avatar
unknown committed
2976 2977
				if (right_node_ptr
				    != page_rec_get_next(node_ptr)) {
unknown's avatar
Merge  
unknown committed
2978
					ret = FALSE;
unknown's avatar
unknown committed
2979 2980 2981
					fputs("InnoDB: node pointer to"
					      " the right page is wrong\n",
					      stderr);
unknown's avatar
Merge  
unknown committed
2982

2983
					btr_validate_report1(index, level,
unknown's avatar
unknown committed
2984
							     page);
unknown's avatar
unknown committed
2985 2986 2987 2988

					buf_page_print(father_page);
					buf_page_print(page);
					buf_page_print(right_page);
unknown's avatar
Merge  
unknown committed
2989
				}
2990
			} else {
2991 2992
				right_father_page = buf_frame_align(
					right_node_ptr);
2993

2994 2995 2996
				if (right_node_ptr != page_rec_get_next(
					    page_get_infimum_rec(
						    right_father_page))) {
unknown's avatar
Merge  
unknown committed
2997
					ret = FALSE;
unknown's avatar
unknown committed
2998 2999 3000
					fputs("InnoDB: node pointer 2 to"
					      " the right page is wrong\n",
					      stderr);
unknown's avatar
Merge  
unknown committed
3001

3002
					btr_validate_report1(index, level,
unknown's avatar
unknown committed
3003
							     page);
unknown's avatar
unknown committed
3004 3005 3006 3007 3008

					buf_page_print(father_page);
					buf_page_print(right_father_page);
					buf_page_print(page);
					buf_page_print(right_page);
unknown's avatar
Merge  
unknown committed
3009 3010 3011
				}

				if (buf_frame_get_page_no(right_father_page)
unknown's avatar
unknown committed
3012
				    != btr_page_get_next(father_page, &mtr)) {
unknown's avatar
Merge  
unknown committed
3013 3014

					ret = FALSE;
unknown's avatar
unknown committed
3015 3016 3017
					fputs("InnoDB: node pointer 3 to"
					      " the right page is wrong\n",
					      stderr);
unknown's avatar
Merge  
unknown committed
3018

3019
					btr_validate_report1(index, level,
unknown's avatar
unknown committed
3020
							     page);
unknown's avatar
unknown committed
3021 3022 3023 3024 3025

					buf_page_print(father_page);
					buf_page_print(right_father_page);
					buf_page_print(page);
					buf_page_print(right_page);
unknown's avatar
Merge  
unknown committed
3026
				}
3027
			}
3028 3029 3030
		}
	}

unknown's avatar
Merge  
unknown committed
3031
node_ptr_fails:
unknown's avatar
unknown committed
3032 3033 3034
	/* Commit the mini-transaction to release the latch on 'page'.
	Re-acquire the latch on right_page, which will become 'page'
	on the next loop.  The page has already been checked. */
3035 3036 3037 3038
	mtr_commit(&mtr);

	if (right_page_no != FIL_NULL) {
		mtr_start(&mtr);
3039

3040 3041 3042 3043
		page = btr_page_get(space, right_page_no, RW_X_LATCH, &mtr);

		goto loop;
	}
unknown's avatar
unknown committed
3044

unknown's avatar
unknown committed
3045
	mem_heap_free(heap);
unknown's avatar
unknown committed
3046
	return(ret);
3047 3048 3049 3050 3051
}

/******************************************************************
Checks the consistency of an index tree. */

unknown's avatar
unknown committed
3052
ibool
3053 3054
btr_validate_index(
/*===============*/
unknown's avatar
unknown committed
3055
				/* out: TRUE if ok */
3056
	dict_index_t*	index,	/* in: index */
3057
	trx_t*		trx)	/* in: transaction or NULL */
3058 3059 3060 3061 3062 3063 3064
{
	mtr_t	mtr;
	page_t*	root;
	ulint	i;
	ulint	n;

	mtr_start(&mtr);
3065
	mtr_x_lock(dict_index_get_lock(index), &mtr);
3066

3067
	root = btr_root_get(index, &mtr);
3068 3069
	n = btr_page_get_level(root, &mtr);

3070
	for (i = 0; i <= n && !trx_is_interrupted(trx); i++) {
3071
		if (!btr_validate_level(index, trx, n - i)) {
unknown's avatar
unknown committed
3072 3073 3074 3075 3076

			mtr_commit(&mtr);

			return(FALSE);
		}
3077 3078 3079
	}

	mtr_commit(&mtr);
unknown's avatar
unknown committed
3080 3081

	return(TRUE);
3082
}