btr0btr.c 84.4 KB
Newer Older
osku's avatar
osku committed
1 2 3 4 5 6 7
/******************************************************
The B-tree

(c) 1994-1996 Innobase Oy

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

osku's avatar
osku committed
9 10 11 12 13 14 15 16
#include "btr0btr.h"

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

#include "fsp0fsp.h"
#include "page0page.h"
marko's avatar
marko committed
17
#include "page0zip.h"
osku's avatar
osku committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include "btr0cur.h"
#include "btr0sea.h"
#include "btr0pcur.h"
#include "rem0cmp.h"
#include "lock0lock.h"
#include "ibuf0ibuf.h"
#include "trx0trx.h"

/*
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.

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.

60
If a node pointer with a prefix P points to a non-leaf child,
osku's avatar
osku committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
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.
*/

/******************************************************************
Creates a new index page to the tree (not the root, and also not
used in page reorganization). */
static
void
btr_page_create(
/*============*/
	page_t*		page,	/* in: page to be created */
	dict_tree_t*	tree,	/* in: index tree */
	mtr_t*		mtr);	/* in: mtr */
/****************************************************************
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 */
	dict_tree_t*	tree,	/* in: index tree */
	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(
/*===========*/
marko's avatar
marko committed
109 110
	page_t*		page,	/* in: page to be emptied */
	page_zip_des_t*	page_zip,/* out: compressed page, or NULL */
111 112
	mtr_t*		mtr,	/* in: mtr */
	dict_index_t*	index);	/* in: the index of the page */
osku's avatar
osku committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
/*****************************************************************
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 */
	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 */

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

page_t*
btr_root_get(
/*=========*/
				/* out: root page, x-latched */
	dict_tree_t*	tree,	/* in: index tree */
	mtr_t*		mtr)	/* in: mtr */
{
	ulint	space;
	ulint	root_page_no;
	page_t*	root;
144

osku's avatar
osku committed
145 146 147 148 149 150
	space = dict_tree_get_space(tree);
	root_page_no = dict_tree_get_page(tree);

	root = btr_page_get(space, root_page_no, RW_X_LATCH, mtr);
	ut_a((ibool)!!page_is_comp(root) ==
			UT_LIST_GET_FIRST(tree->tree_indexes)->table->comp);
151

osku's avatar
osku committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	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;

	if (!page_rec_is_infimum(rec)) {

		rec_t*	prev_rec = page_rec_get_prev(rec);

		if (!page_rec_is_infimum(prev_rec)) {

			return(prev_rec);
		}
	}
181

osku's avatar
osku committed
182 183 184
	page = buf_frame_align(rec);
	prev_page_no = btr_page_get_prev(page, mtr);
	space = buf_frame_get_space_id(page);
185

osku's avatar
osku committed
186 187 188 189 190 191
	if (prev_page_no != FIL_NULL) {

		prev_page = buf_page_get_with_no_latch(space, prev_page_no,
									mtr);
		/* The caller must already have a latch to the brother */
		ut_ad((mtr_memo_contains(mtr, buf_block_align(prev_page),
192 193 194
						MTR_MEMO_PAGE_S_FIX))
			|| (mtr_memo_contains(mtr, buf_block_align(prev_page),
					MTR_MEMO_PAGE_X_FIX)));
osku's avatar
osku committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
		ut_a(page_is_comp(prev_page) == page_is_comp(page));

		return(page_rec_get_prev(page_get_supremum_rec(prev_page)));
	}

	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;

	if (!page_rec_is_supremum(rec)) {

		rec_t*	next_rec = page_rec_get_next(rec);

		if (!page_rec_is_supremum(next_rec)) {

			return(next_rec);
		}
	}
229

osku's avatar
osku committed
230 231 232
	page = buf_frame_align(rec);
	next_page_no = btr_page_get_next(page, mtr);
	space = buf_frame_get_space_id(page);
233

osku's avatar
osku committed
234 235 236 237 238 239
	if (next_page_no != FIL_NULL) {

		next_page = buf_page_get_with_no_latch(space, next_page_no,
									mtr);
		/* The caller must already have a latch to the brother */
		ut_ad((mtr_memo_contains(mtr, buf_block_align(next_page),
240 241 242
						MTR_MEMO_PAGE_S_FIX))
			|| (mtr_memo_contains(mtr, buf_block_align(next_page),
						MTR_MEMO_PAGE_X_FIX)));
osku's avatar
osku committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

		ut_a(page_is_comp(next_page) == page_is_comp(page));
		return(page_rec_get_next(page_get_infimum_rec(next_page)));
	}

	return(NULL);
}

/******************************************************************
Creates a new index page to the tree (not the root, and also not used in
page reorganization). */
static
void
btr_page_create(
/*============*/
	page_t*		page,	/* in: page to be created */
	dict_tree_t*	tree,	/* in: index tree */
	mtr_t*		mtr)	/* in: mtr */
{
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
263
							MTR_MEMO_PAGE_X_FIX));
marko's avatar
marko committed
264
	page_create(page, NULL, mtr,
265
			UT_LIST_GET_FIRST(tree->tree_indexes));
osku's avatar
osku committed
266
	buf_block_align(page)->check_index_page_at_flush = TRUE;
267

marko's avatar
marko committed
268
	btr_page_set_index_id(page, NULL, tree->id, mtr);
osku's avatar
osku committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
}

/******************************************************************
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 */
	dict_tree_t*	tree,	/* in: index tree */
	mtr_t*		mtr)	/* in: mtr */
{
	fil_addr_t	node_addr;
	page_t*		root;
	page_t*		new_page;

	root = btr_root_get(tree, mtr);
287

osku's avatar
osku committed
288 289 290 291 292 293 294 295 296 297 298
	node_addr = flst_get_first(root + PAGE_HEADER
					+ PAGE_BTR_IBUF_FREE_LIST, mtr);
	ut_a(node_addr.page != FIL_NULL);

	new_page = buf_page_get(dict_tree_get_space(tree), node_addr.page,
							RW_X_LATCH, mtr);
#ifdef UNIV_SYNC_DEBUG
	buf_page_dbg_add_level(new_page, SYNC_TREE_NODE_NEW);
#endif /* UNIV_SYNC_DEBUG */

	flst_remove(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
299
		new_page + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST_NODE,
osku's avatar
osku committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
									mtr);
	ut_ad(flst_validate(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST, mtr));

	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! */

page_t*
btr_page_alloc(
/*===========*/
					/* out: new allocated page, x-latched;
					NULL if out of space */
	dict_tree_t*	tree,		/* in: index tree */
	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;

	if (tree->type & DICT_IBUF) {

		return(btr_page_alloc_for_ibuf(tree, mtr));
	}

	root = btr_root_get(tree, mtr);
334

osku's avatar
osku committed
335 336 337 338 339 340 341 342 343
	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: */
344

osku's avatar
osku committed
345 346 347 348 349 350 351 352 353 354 355 356
	new_page_no = fseg_alloc_free_page_general(seg_header, hint_page_no,
						file_direction, TRUE, mtr);
	if (new_page_no == FIL_NULL) {

		return(NULL);
	}

	new_page = buf_page_get(dict_tree_get_space(tree), new_page_no,
							RW_X_LATCH, mtr);
#ifdef UNIV_SYNC_DEBUG
	buf_page_dbg_add_level(new_page, SYNC_TREE_NODE_NEW);
#endif /* UNIV_SYNC_DEBUG */
357

osku's avatar
osku committed
358
	return(new_page);
359
}
osku's avatar
osku committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

/******************************************************************
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);

	mtr_s_lock(dict_tree_get_lock(index->tree), &mtr);

	root = btr_root_get(index->tree, &mtr);
382

osku's avatar
osku committed
383 384
	if (flag == BTR_N_LEAF_PAGES) {
		seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
385

osku's avatar
osku committed
386
		fseg_n_reserved_pages(seg_header, &n, &mtr);
387

osku's avatar
osku committed
388 389 390 391
	} else if (flag == BTR_TOTAL_SIZE) {
		seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;

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

osku's avatar
osku committed
393
		seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
394 395

		n += fseg_n_reserved_pages(seg_header, &dummy, &mtr);
osku's avatar
osku committed
396 397 398 399 400 401 402
	} else {
		ut_error;
	}

	mtr_commit(&mtr);

	return(n);
403
}
osku's avatar
osku committed
404 405 406 407 408 409 410 411 412

/******************************************************************
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(
/*===================*/
	dict_tree_t*	tree,	/* in: index tree */
413
	page_t*		page,	/* in: page to be freed, x-latched */
osku's avatar
osku committed
414 415 416 417 418
	mtr_t*		mtr)	/* in: mtr */
{
	page_t*		root;

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
419
							MTR_MEMO_PAGE_X_FIX));
osku's avatar
osku committed
420
	root = btr_root_get(tree, mtr);
421

osku's avatar
osku committed
422
	flst_add_first(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
423
		page + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST_NODE, mtr);
osku's avatar
osku committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437

	ut_ad(flst_validate(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
									mtr));
}

/******************************************************************
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. */

void
btr_page_free_low(
/*==============*/
	dict_tree_t*	tree,	/* in: index tree */
438
	page_t*		page,	/* in: page to be freed, x-latched */
439 440
	ulint		space,	/* in: space */
	ulint		page_no,/* in: page number */
osku's avatar
osku committed
441 442 443 444 445 446 447
	ulint		level,	/* in: page level */
	mtr_t*		mtr)	/* in: mtr */
{
	fseg_header_t*	seg_header;
	page_t*		root;

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
448
							MTR_MEMO_PAGE_X_FIX));
osku's avatar
osku committed
449 450 451 452
	/* The page gets invalid for optimistic searches: increment the frame
	modify clock */

	buf_frame_modify_clock_inc(page);
453

osku's avatar
osku committed
454 455 456 457 458 459 460 461
	if (tree->type & DICT_IBUF) {

		btr_page_free_for_ibuf(tree, page, mtr);

		return;
	}

	root = btr_root_get(tree, mtr);
462

osku's avatar
osku committed
463 464 465 466 467 468 469
	if (level == 0) {
		seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
	} else {
		seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;
	}

	fseg_free_page(seg_header, space, page_no, mtr);
470
}
osku's avatar
osku committed
471 472 473 474 475 476 477 478 479

/******************************************************************
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(
/*==========*/
	dict_tree_t*	tree,	/* in: index tree */
480
	page_t*		page,	/* in: page to be freed, x-latched */
osku's avatar
osku committed
481 482 483
	mtr_t*		mtr)	/* in: mtr */
{
	ulint		level;
484 485
	ulint		space;
	ulint		page_no;
osku's avatar
osku committed
486 487

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
488
							MTR_MEMO_PAGE_X_FIX));
osku's avatar
osku committed
489
	level = btr_page_get_level(page, mtr);
490

491 492
	space = buf_frame_get_space_id(page);
	page_no = buf_frame_get_page_no(page);
493

494
	btr_page_free_low(tree, page, space, page_no, level, mtr);
495
}
osku's avatar
osku committed
496 497 498 499 500 501 502 503

/******************************************************************
Sets the child node file address in a node pointer. */
UNIV_INLINE
void
btr_node_ptr_set_child_page_no(
/*===========================*/
	rec_t*		rec,	/* in: node pointer record */
504 505
	page_zip_des_t*	page_zip,/* in/out: compressed page whose uncompressed
				part will be updated, or NULL */
osku's avatar
osku committed
506 507 508 509 510 511 512 513 514 515 516
	const ulint*	offsets,/* in: array returned by rec_get_offsets() */
	ulint		page_no,/* in: child node address */
	mtr_t*		mtr)	/* in: mtr */
{
	byte*	field;
	ulint	len;

	ut_ad(rec_offs_validate(rec, NULL, offsets));
	ut_ad(0 < btr_page_get_level(buf_frame_align(rec), mtr));
	ut_ad(!rec_offs_comp(offsets) || rec_get_node_ptr_flag(rec));

517
	/* The child address is in the last field */
osku's avatar
osku committed
518 519 520
	field = rec_get_nth_field(rec, offsets,
					rec_offs_n_fields(offsets) - 1, &len);

521
	ut_ad(len == REC_NODE_PTR_SIZE);
522

marko's avatar
marko committed
523
	if (UNIV_LIKELY_NULL(page_zip)) {
524 525 526 527
		page_zip_write_node_ptr(page_zip, rec,
				rec_offs_data_size(offsets), page_no, mtr);
	} else {
		mlog_write_ulint(field, page_no, MLOG_4BYTES, mtr);
marko's avatar
marko committed
528
	}
osku's avatar
osku committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
}

/****************************************************************
Returns the child page of a node pointer and x-latches it. */
static
page_t*
btr_node_ptr_get_child(
/*===================*/
				/* 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 */
{
	ulint	page_no;
	ulint	space;
	page_t*	page;

	ut_ad(rec_offs_validate(node_ptr, NULL, offsets));
	space = buf_frame_get_space_id(node_ptr);
	page_no = btr_node_ptr_get_child_page_no(node_ptr, offsets);

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

osku's avatar
osku committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
	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 */
	dict_tree_t*	tree,	/* in: index tree */
	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;
	dict_index_t*	index;
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
	ulint*		offsets	= offsets_;
	*offsets_ = (sizeof offsets_) / sizeof *offsets_;

	ut_ad(mtr_memo_contains(mtr, dict_tree_get_lock(tree),
							MTR_MEMO_X_LOCK));
	ut_a(page_rec_is_user_rec(user_rec));
582

osku's avatar
osku committed
583 584 585 586 587
	ut_ad(dict_tree_get_page(tree) != buf_frame_get_page_no(page));

	heap = mem_heap_create(100);

	tuple = dict_tree_build_node_ptr(tree, user_rec, 0, heap,
588
					btr_page_get_level(page, mtr));
osku's avatar
osku committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	index = UT_LIST_GET_FIRST(tree->tree_indexes);

	/* In the following, we choose just any index from the tree as the
	first parameter for btr_cur_search_to_nth_level. */

	btr_cur_search_to_nth_level(index,
				btr_page_get_level(page, mtr) + 1,
				tuple, PAGE_CUR_LE,
				BTR_CONT_MODIFY_TREE, &cursor, 0, mtr);

	node_ptr = btr_cur_get_rec(&cursor);
	offsets = rec_get_offsets(node_ptr, index, offsets,
						ULINT_UNDEFINED, &heap);

	if (btr_node_ptr_get_child_page_no(node_ptr, offsets) !=
604
						buf_frame_get_page_no(page)) {
osku's avatar
osku committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
		rec_t*	print_rec;
		fputs("InnoDB: Dump of the child page:\n", stderr);
		buf_page_print(buf_frame_align(page));
		fputs("InnoDB: Dump of the parent page:\n", stderr);
		buf_page_print(buf_frame_align(node_ptr));

		fputs("InnoDB: Corruption of an index tree: table ", stderr);
		ut_print_name(stderr, NULL, index->table_name);
		fputs(", index ", stderr);
		ut_print_name(stderr, NULL, index->name);
		fprintf(stderr, ",\n"
"InnoDB: father ptr page no %lu, child page no %lu\n",
			(ulong)
			btr_node_ptr_get_child_page_no(node_ptr, offsets),
			(ulong) buf_frame_get_page_no(page));
		print_rec = page_rec_get_next(page_get_infimum_rec(page));
		offsets = rec_get_offsets(print_rec, index,
				offsets, ULINT_UNDEFINED, &heap);
		page_rec_print(print_rec, offsets);
		offsets = rec_get_offsets(node_ptr, index, offsets,
					ULINT_UNDEFINED, &heap);
		page_rec_print(node_ptr, offsets);

		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/mysql/en/Forcing_recovery.html about\n"
"InnoDB: forcing recovery. Then dump + drop + reimport.\n", stderr);
	}

	ut_a(btr_node_ptr_get_child_page_no(node_ptr, offsets) ==
						buf_frame_get_page_no(page));
	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 */
	dict_tree_t*	tree,	/* in: index tree */
	page_t*		page,	/* in: page: must contain at least one
				user record */
	mtr_t*		mtr)	/* in: mtr */
{
	return(btr_page_get_father_for_rec(tree, page,
		page_rec_get_next(page_get_infimum_rec(page)), mtr));
}

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

ulint
btr_create(
/*=======*/
665 666 667 668 669 670 671
				/* 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 */
	dict_index_t*	index,	/* in: index */
	mtr_t*		mtr)	/* in: mini-transaction handle */
osku's avatar
osku committed
672 673 674 675 676
{
	ulint		page_no;
	buf_frame_t*	ibuf_hdr_frame;
	buf_frame_t*	frame;
	page_t*		page;
marko's avatar
marko committed
677
	page_zip_des_t*	page_zip;
osku's avatar
osku committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692

	/* 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 */
		ibuf_hdr_frame = fseg_create(space, 0,
				IBUF_HEADER + IBUF_TREE_SEG_HEADER, mtr);

#ifdef UNIV_SYNC_DEBUG
		buf_page_dbg_add_level(ibuf_hdr_frame, SYNC_TREE_NODE_NEW);
#endif /* UNIV_SYNC_DEBUG */
		ut_ad(buf_frame_get_page_no(ibuf_hdr_frame)
693
						== IBUF_HEADER_PAGE_NO);
osku's avatar
osku committed
694
		/* Allocate then the next page to the segment: it will be the
695
		tree root page */
osku's avatar
osku committed
696

697
		page_no = fseg_alloc_free_page(
osku's avatar
osku committed
698
				ibuf_hdr_frame + IBUF_HEADER
699
				+ IBUF_TREE_SEG_HEADER, IBUF_TREE_ROOT_PAGE_NO,
osku's avatar
osku committed
700 701 702 703 704 705 706 707
				FSP_UP, mtr);
		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,
									mtr);
	}
708

osku's avatar
osku committed
709 710 711 712 713 714
	if (frame == NULL) {

		return(FIL_NULL);
	}

	page_no = buf_frame_get_page_no(frame);
715

osku's avatar
osku committed
716 717 718 719 720 721 722 723
#ifdef UNIV_SYNC_DEBUG
	buf_page_dbg_add_level(frame, SYNC_TREE_NODE_NEW);
#endif /* UNIV_SYNC_DEBUG */

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

		ut_ad(page_no == IBUF_TREE_ROOT_PAGE_NO);
724

osku's avatar
osku committed
725
		flst_init(frame + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST, mtr);
726
	} else {
osku's avatar
osku committed
727 728 729 730 731 732 733 734 735 736
		/* It is a non-ibuf tree: create a file segment for leaf
		pages */
		fseg_create(space, page_no, PAGE_HEADER + PAGE_BTR_SEG_LEAF,
									mtr);
		/* The fseg create acquires a second latch on the page,
		therefore we must declare it: */
#ifdef UNIV_SYNC_DEBUG
		buf_page_dbg_add_level(frame, SYNC_TREE_NODE_NEW);
#endif /* UNIV_SYNC_DEBUG */
	}
737

osku's avatar
osku committed
738
	/* Create a new index page on the the allocated segment page */
739
	page = page_create(frame, NULL, mtr, index);
osku's avatar
osku committed
740 741 742
	buf_block_align(page)->check_index_page_at_flush = TRUE;

	/* Set the index id of the page */
marko's avatar
marko committed
743
	btr_page_set_index_id(page, NULL, index_id, mtr);
osku's avatar
osku committed
744 745

	/* Set the level of the new index page */
marko's avatar
marko committed
746
	btr_page_set_level(page, NULL, 0, mtr);
747

osku's avatar
osku committed
748
	/* Set the next node and previous node fields */
marko's avatar
marko committed
749 750
	btr_page_set_next(page, NULL, FIL_NULL, mtr);
	btr_page_set_prev(page, NULL, FIL_NULL, mtr);
osku's avatar
osku committed
751 752 753 754

	/* 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 */
755

osku's avatar
osku committed
756 757 758 759 760 761 762 763
	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);

marko's avatar
marko committed
764 765
	page_zip = buf_block_get_page_zip(buf_block_align(page));
	if (UNIV_LIKELY_NULL(page_zip)) {
766 767
		if (UNIV_UNLIKELY(page_zip_compress(
				page_zip, page, index, mtr))) {
marko's avatar
marko committed
768 769 770 771 772
			/* An empty page should always be compressible */
			ut_error;
		}
	}

osku's avatar
osku committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
	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;

790
leaf_loop:
osku's avatar
osku committed
791
	mtr_start(&mtr);
792 793

	root = btr_page_get(space, root_page_no, RW_X_LATCH, &mtr);
osku's avatar
osku committed
794 795 796 797 798 799 800 801 802 803 804 805 806 807

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

	finished = fseg_free_step(
				root + PAGE_HEADER + PAGE_BTR_SEG_LEAF, &mtr);
	mtr_commit(&mtr);

	if (!finished) {

		goto leaf_loop;
	}
top_loop:
	mtr_start(&mtr);
808 809

	root = btr_page_get(space, root_page_no, RW_X_LATCH, &mtr);
osku's avatar
osku committed
810 811 812 813 814 815 816 817

	finished = fseg_free_step_not_header(
				root + PAGE_HEADER + PAGE_BTR_SEG_TOP, &mtr);
	mtr_commit(&mtr);

	if (!finished) {

		goto top_loop;
818
	}
osku's avatar
osku committed
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
}

/****************************************************************
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);

837 838
	btr_search_drop_page_hash_index(root);
top_loop:
osku's avatar
osku committed
839 840 841 842 843
	finished = fseg_free_step(
				root + PAGE_HEADER + PAGE_BTR_SEG_TOP, mtr);
	if (!finished) {

		goto top_loop;
844
	}
osku's avatar
osku committed
845 846 847 848 849
}

/*****************************************************************
Reorganizes an index page. */
static
850
ibool
osku's avatar
osku committed
851 852 853 854 855 856 857
btr_page_reorganize_low(
/*====================*/
	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 */
marko's avatar
marko committed
858 859
	page_t*		page,	/* in/out: page to be reorganized */
	page_zip_des_t*	page_zip,/* in/out: compressed page, or NULL */
osku's avatar
osku committed
860 861 862 863 864 865 866 867 868
	dict_index_t*	index,	/* in: record descriptor */
	mtr_t*		mtr)	/* in: mtr */
{
	page_t*	new_page;
	ulint	log_mode;
	ulint	data_size1;
	ulint	data_size2;
	ulint	max_ins_size1;
	ulint	max_ins_size2;
869
	ibool	success = FALSE;
osku's avatar
osku committed
870 871

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
872
							MTR_MEMO_PAGE_X_FIX));
marko's avatar
marko committed
873
	ut_ad((ibool) !!page_is_comp(page) == index->table->comp);
osku's avatar
osku committed
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
	data_size1 = page_get_data_size(page);
	max_ins_size1 = page_get_max_insert_size_after_reorganize(page, 1);

	/* Write the log record */
	mlog_open_and_write_index(mtr, page, index, page_is_comp(page)
			? MLOG_COMP_PAGE_REORGANIZE
			: MLOG_PAGE_REORGANIZE, 0);

	/* 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);

marko's avatar
marko committed
890
	if (UNIV_LIKELY(!recovery)) {
osku's avatar
osku committed
891 892 893 894 895 896
		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 */

897
	page_create(page, NULL, mtr, index);
osku's avatar
osku committed
898
	buf_block_align(page)->check_index_page_at_flush = TRUE;
899

osku's avatar
osku committed
900 901 902
	/* Copy the records from the temporary space to the recreated page;
	do not copy the lock bits yet */

marko's avatar
marko committed
903
	page_copy_rec_list_end_no_locks(page,
osku's avatar
osku committed
904 905
				page_get_infimum_rec(new_page), index, mtr);
	/* Copy max trx id to recreated page */
marko's avatar
marko committed
906
	page_set_max_trx_id(page, NULL, page_get_max_trx_id(new_page));
907

marko's avatar
marko committed
908
	if (UNIV_LIKELY_NULL(page_zip)) {
909 910
		if (UNIV_UNLIKELY(!page_zip_compress(
				page_zip, page, index, mtr))) {
marko's avatar
marko committed
911

912 913 914 915
			/* Restore the old page and exit. */
			buf_frame_copy(page, new_page);

			goto func_exit;
marko's avatar
marko committed
916 917 918 919
		}
	}

	if (UNIV_LIKELY(!recovery)) {
osku's avatar
osku committed
920 921 922 923 924 925 926
		/* Update the record lock bitmaps */
		lock_move_reorganize_page(page, new_page);
	}

	data_size2 = page_get_data_size(page);
	max_ins_size2 = page_get_max_insert_size_after_reorganize(page, 1);

marko's avatar
marko committed
927 928
	if (UNIV_UNLIKELY(data_size1 != data_size2)
			|| UNIV_UNLIKELY(max_ins_size1 != max_ins_size2)) {
osku's avatar
osku committed
929 930
		buf_page_print(page);
		buf_page_print(new_page);
931
		fprintf(stderr,
osku's avatar
osku committed
932 933 934 935 936 937
"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",
			(unsigned long) data_size1, (unsigned long) data_size2,
			(unsigned long) max_ins_size1,
			(unsigned long) max_ins_size2);
938 939
	} else {
		success = TRUE;
osku's avatar
osku committed
940 941
	}

942
func_exit:
osku's avatar
osku committed
943 944 945 946
	buf_frame_free(new_page);

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

	return(success);
osku's avatar
osku committed
949 950 951 952 953
}

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

954
ibool
osku's avatar
osku committed
955 956
btr_page_reorganize(
/*================*/
957
				/* out: TRUE on success, FALSE on failure */
osku's avatar
osku committed
958 959 960 961
	page_t*		page,	/* in: page to be reorganized */
	dict_index_t*	index,	/* in: record descriptor */
	mtr_t*		mtr)	/* in: mtr */
{
962
	return(btr_page_reorganize_low(FALSE, page,
marko's avatar
marko committed
963
				buf_block_get_page_zip(buf_block_align(page)),
964
				index, mtr));
osku's avatar
osku committed
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
}

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

byte*
btr_parse_page_reorganize(
/*======================*/
				/* 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 */
{
	ut_ad(ptr && end_ptr);

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

marko's avatar
marko committed
985 986 987 988
	if (UNIV_LIKELY(page != NULL)) {
		page_zip_des_t*	page_zip = buf_block_get_page_zip(
						buf_block_align(page));
		btr_page_reorganize_low(TRUE, page, page_zip, index, mtr);
osku's avatar
osku committed
989 990 991 992 993 994 995 996 997 998 999
	}

	return(ptr);
}

/*****************************************************************
Empties an index page. */
static
void
btr_page_empty(
/*===========*/
marko's avatar
marko committed
1000 1001
	page_t*		page,	/* in: page to be emptied */
	page_zip_des_t*	page_zip,/* out: compressed page, or NULL */
1002 1003
	mtr_t*		mtr,	/* in: mtr */
	dict_index_t*	index)	/* in: index of the page */
osku's avatar
osku committed
1004 1005
{
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
1006
							MTR_MEMO_PAGE_X_FIX));
marko's avatar
marko committed
1007 1008
	ut_ad(!page_zip || page_zip_validate(page_zip, page));

osku's avatar
osku committed
1009 1010 1011 1012 1013
	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 */

1014
	page_create(page, page_zip, mtr, index);
osku's avatar
osku committed
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	buf_block_align(page)->check_index_page_at_flush = TRUE;
}

/*****************************************************************
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 */
1034 1035
	const ulint*	ext,	/* in: array of extern field numbers */
	ulint		n_ext,	/* in: number of elements in vec */
osku's avatar
osku committed
1036 1037 1038 1039 1040 1041 1042 1043 1044
	mtr_t*		mtr)	/* in: mtr */
{
	dict_tree_t*	tree;
	page_t*		root;
	page_t*		new_page;
	ulint		new_page_no;
	rec_t*		rec;
	mem_heap_t*	heap;
	dtuple_t*	node_ptr;
1045
	ulint		level;
osku's avatar
osku committed
1046 1047
	rec_t*		node_ptr_rec;
	page_cur_t*	page_cursor;
1048 1049
	page_zip_des_t*	root_page_zip;
	page_zip_des_t*	new_page_zip;
1050

osku's avatar
osku committed
1051 1052 1053 1054 1055 1056 1057
	root = btr_cur_get_page(cursor);
	tree = btr_cur_get_tree(cursor);

	ut_ad(dict_tree_get_page(tree) == buf_frame_get_page_no(root));
	ut_ad(mtr_memo_contains(mtr, dict_tree_get_lock(tree),
							MTR_MEMO_X_LOCK));
	ut_ad(mtr_memo_contains(mtr, buf_block_align(root),
1058
							MTR_MEMO_PAGE_X_FIX));
osku's avatar
osku committed
1059 1060 1061 1062 1063
	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. */
1064

osku's avatar
osku committed
1065
	new_page = btr_page_alloc(tree, 0, FSP_NO_DIR,
1066
				btr_page_get_level(root, mtr), mtr);
osku's avatar
osku committed
1067 1068 1069

	btr_page_create(new_page, tree, mtr);

1070
	root_page_zip = buf_block_get_page_zip(buf_block_align(root));
osku's avatar
osku committed
1071
	level = btr_page_get_level(root, mtr);
1072

osku's avatar
osku committed
1073
	/* Set the levels of the new index page and root page */
marko's avatar
marko committed
1074
	btr_page_set_level(new_page, NULL, level, mtr);
1075
	btr_page_set_level(root, root_page_zip, level + 1, mtr);
1076

osku's avatar
osku committed
1077
	/* Set the next node and previous node fields of new page */
marko's avatar
marko committed
1078 1079
	btr_page_set_next(new_page, NULL, FIL_NULL, mtr);
	btr_page_set_prev(new_page, NULL, FIL_NULL, mtr);
osku's avatar
osku committed
1080 1081 1082

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

1083
	new_page_zip = buf_block_get_page_zip(buf_block_align(new_page));
marko's avatar
marko committed
1084

1085 1086
	page_move_rec_list_end(new_page, new_page_zip,
				page_get_infimum_rec(root), root_page_zip,
marko's avatar
marko committed
1087 1088
				cursor->index, mtr);

osku's avatar
osku committed
1089 1090 1091 1092
	/* 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 */
1093

osku's avatar
osku committed
1094 1095 1096 1097 1098 1099 1100
	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);
1101

osku's avatar
osku committed
1102 1103 1104 1105
	/* Build the node pointer (= node key and page address) for the
	child */

	node_ptr = dict_tree_build_node_ptr(tree, rec, new_page_no, heap,
1106
								level);
osku's avatar
osku committed
1107
	/* Reorganize the root to get free space */
1108 1109 1110 1111 1112
	if (!btr_page_reorganize_low(FALSE, root, root_page_zip,
					cursor->index, mtr)) {
		/* The page should be empty at this point.
		Thus, the operation should succeed. */
		ut_error;
1113
	}
osku's avatar
osku committed
1114 1115

	page_cursor = btr_cur_get_page_cur(cursor);
1116

osku's avatar
osku committed
1117 1118 1119 1120
	/* Insert node pointer to the root */

	page_cur_set_before_first(root, page_cursor);

marko's avatar
marko committed
1121
	node_ptr_rec = page_cur_tuple_insert(page_cursor, NULL,
1122
					node_ptr, cursor->index, NULL, 0, mtr);
osku's avatar
osku committed
1123 1124 1125 1126 1127 1128 1129

	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: */

1130
	btr_set_min_rec_mark(node_ptr_rec, mtr);
1131

1132
	if (UNIV_LIKELY_NULL(root_page_zip)
1133
		&& !UNIV_UNLIKELY(page_zip_compress(root_page_zip, root,
1134
							cursor->index, mtr))) {
marko's avatar
marko committed
1135 1136 1137 1138 1139 1140
		/* The root page should only contain the
		node pointer to new_page at this point.
		Thus, the data should fit. */
		ut_error;
	}

osku's avatar
osku committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
	/* Free the memory heap */
	mem_heap_free(heap);

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

/*	fprintf(stderr, "Root raise new page no %lu\n",
					buf_frame_get_page_no(new_page)); */

	ibuf_reset_free_bits(UT_LIST_GET_FIRST(tree->tree_indexes),
								new_page);
	/* Reposition the cursor to the child node */
	page_cur_search(new_page, cursor->index, tuple,
				PAGE_CUR_LE, page_cursor);
1154

osku's avatar
osku committed
1155
	/* Split the child and insert tuple */
1156
	return(btr_page_split_and_insert(cursor, tuple, ext, n_ext, mtr));
1157
}
osku's avatar
osku committed
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

/*****************************************************************
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);

	if (page_header_get_ptr(page, PAGE_LAST_INSERT)
1181 1182 1183
		== page_rec_get_next(insert_point)) {

		infimum = page_get_infimum_rec(page);
osku's avatar
osku committed
1184 1185 1186 1187 1188 1189 1190

		/* 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
1191
			&& page_rec_get_next(infimum) != insert_point) {
osku's avatar
osku committed
1192 1193 1194

			*split_rec = insert_point;
		} else {
1195 1196
			*split_rec = page_rec_get_next(insert_point);
		}
osku's avatar
osku committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237

		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);

	/* 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. */

	if (UNIV_LIKELY(page_header_get_ptr(page, PAGE_LAST_INSERT)
				== insert_point)) {

		rec_t*	next_rec;

		next_rec = page_rec_get_next(insert_point);

		if (page_rec_is_supremum(next_rec)) {
split_at_new:
			/* Split at the new record to insert */
1238
			*split_rec = NULL;
osku's avatar
osku committed
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
		} else {
			rec_t*	next_next_rec = page_rec_get_next(next_rec);
			if (page_rec_is_supremum(next_next_rec)) {

				goto split_at_new;
			}

			/* 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. */
1252

osku's avatar
osku committed
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
			*split_rec = next_next_rec;
		}

		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 */
1275
	dtuple_t*	tuple)		/* in: tuple to insert */
osku's avatar
osku committed
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
{
	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;
	mem_heap_t* heap;
	ulint*	offsets;

	page = btr_cur_get_page(cursor);
1292

osku's avatar
osku committed
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
	insert_size = rec_get_converted_size(cursor->index, tuple);
	free_space  = page_get_free_space_of_empty(page_is_comp(page));

	/* 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);

	heap = NULL;
	offsets = NULL;

	/* 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 {
			offsets = rec_get_offsets(rec, cursor->index,
					offsets, ULINT_UNDEFINED, &heap);
			incl_data += rec_offs_size(offsets);
		}

		n++;
1339

osku's avatar
osku committed
1340
		if (incl_data + page_dir_calc_reserved_space(n)
1341
			>= total_space / 2) {
osku's avatar
osku committed
1342

1343 1344 1345 1346 1347
			if (incl_data + page_dir_calc_reserved_space(n)
				<= free_space) {
				/* The next record will be the first on
				the right half page if it is not the
				supremum record of page */
osku's avatar
osku committed
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361

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

					goto func_exit;
				} else if (rec == NULL) {
					next_rec = page_rec_get_next(ins_rec);
				} else {
					next_rec = page_rec_get_next(rec);
				}
				ut_ad(next_rec);
				if (!page_rec_is_supremum(next_rec)) {
					rec = next_rec;
				}
1362
			}
osku's avatar
osku committed
1363 1364 1365 1366 1367 1368 1369 1370

func_exit:
			if (UNIV_LIKELY_NULL(heap)) {
				mem_heap_free(heap);
			}
			return(rec);
		}
	}
1371
}
osku's avatar
osku committed
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398

/*****************************************************************
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 */
	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 */
{
	page_t*	page;
	ulint	insert_size;
	ulint	free_space;
	ulint	total_data;
	ulint	total_n_recs;
	rec_t*	rec;
	rec_t*	end_rec;
	ulint*	offs;
1399

osku's avatar
osku committed
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
	page = btr_cur_get_page(cursor);

	ut_ad(!split_rec == !offsets);
	ut_ad(!offsets
		|| !page_is_comp(page) == !rec_offs_comp(offsets));
	ut_ad(!offsets
		|| rec_offs_validate(split_rec, cursor->index, offsets));

	insert_size = rec_get_converted_size(cursor->index, tuple);
	free_space  = page_get_free_space_of_empty(page_is_comp(page));

	/* 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;
1415

osku's avatar
osku committed
1416 1417 1418
	/* 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. */
1419

osku's avatar
osku committed
1420 1421 1422 1423 1424 1425 1426
	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));

	} else if (cmp_dtuple_rec(tuple, split_rec, offsets) >= 0) {

		rec = page_rec_get_next(page_get_infimum_rec(page));
1427
		end_rec = split_rec;
osku's avatar
osku committed
1428 1429 1430 1431 1432 1433
	} else {
		rec = split_rec;
		end_rec = page_get_supremum_rec(page);
	}

	if (total_data + page_dir_calc_reserved_space(total_n_recs)
1434
		<= free_space) {
osku's avatar
osku committed
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454

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

		return(TRUE);
	}

	offs = NULL;

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

		offs = rec_get_offsets(rec, cursor->index, offs,
						ULINT_UNDEFINED, &heap);

		total_data -= rec_offs_size(offs);
		total_n_recs--;

		if (total_data + page_dir_calc_reserved_space(total_n_recs)
1455
			<= free_space) {
osku's avatar
osku committed
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466

			/* 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);
1467
}
osku's avatar
osku committed
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481

/***********************************************************
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(
/*=========================*/
	dict_tree_t*	tree,	/* in: tree */
	ulint		level,	/* in: level, must be > 0 */
	dtuple_t*	tuple,	/* in: the record to be inserted */
	mtr_t*		mtr)	/* in: mtr */
{
	big_rec_t*	dummy_big_rec;
1482
	btr_cur_t	cursor;
osku's avatar
osku committed
1483 1484 1485 1486
	ulint		err;
	rec_t*		rec;

	ut_ad(level > 0);
1487

osku's avatar
osku committed
1488 1489 1490 1491
	/* In the following, choose just any index from the tree as the
	first parameter for btr_cur_search_to_nth_level. */

	btr_cur_search_to_nth_level(UT_LIST_GET_FIRST(tree->tree_indexes),
1492 1493
		level, tuple, PAGE_CUR_LE, BTR_CONT_MODIFY_TREE,
		&cursor, 0, mtr);
osku's avatar
osku committed
1494 1495

	err = btr_cur_pessimistic_insert(BTR_NO_LOCKING_FLAG
1496 1497
		| BTR_KEEP_SYS_FLAG | BTR_NO_UNDO_LOG_FLAG,
		&cursor, tuple, &rec, &dummy_big_rec, NULL, 0, NULL, mtr);
osku's avatar
osku committed
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
	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(
/*==================*/
	dict_tree_t*	tree,		/* in: the index tree */
marko's avatar
marko committed
1509
	page_t*		page,		/* in/out: page to be split */
1510 1511 1512
	page_zip_des_t*	page_zip,	/* in/out: compressed page whose
					uncompressed part will be updated,
					or NULL */
osku's avatar
osku committed
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
	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;
marko's avatar
marko committed
1530 1531
	page_zip_des_t*	lower_page_zip;
	page_zip_des_t*	upper_page_zip;
osku's avatar
osku committed
1532
	dtuple_t*	node_ptr_upper;
1533
	mem_heap_t*	heap;
osku's avatar
osku committed
1534 1535

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
1536
							MTR_MEMO_PAGE_X_FIX));
osku's avatar
osku committed
1537
	ut_ad(mtr_memo_contains(mtr, buf_block_align(new_page),
1538
							MTR_MEMO_PAGE_X_FIX));
osku's avatar
osku committed
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
	ut_a(page_is_comp(page) == page_is_comp(new_page));

	/* Create a memory heap where the data tuple is stored */
	heap = mem_heap_create(1024);

	/* 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;

		/* Look from the tree for the node pointer to page */
		node_ptr = btr_page_get_father_node_ptr(tree, page, mtr);

1555
		/* Replace the address of the old child node (= page) with the
osku's avatar
osku committed
1556 1557
		address of the new lower half */

marko's avatar
marko committed
1558
		btr_node_ptr_set_child_page_no(node_ptr, page_zip,
osku's avatar
osku committed
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
			rec_get_offsets(node_ptr,
					UT_LIST_GET_FIRST(tree->tree_indexes),
					NULL, ULINT_UNDEFINED, &heap),
			lower_page_no, mtr);
		mem_heap_empty(heap);
	} 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;
	}
1570

marko's avatar
marko committed
1571 1572 1573
	lower_page_zip = buf_block_get_page_zip(buf_block_align(lower_page));
	upper_page_zip = buf_block_get_page_zip(buf_block_align(upper_page));

osku's avatar
osku committed
1574 1575 1576 1577 1578 1579 1580
	/* 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 */

	node_ptr_upper = dict_tree_build_node_ptr(tree, split_rec,
1581
		upper_page_no, heap, level);
osku's avatar
osku committed
1582 1583 1584 1585 1586

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

	btr_insert_on_non_leaf_level(tree, level + 1, node_ptr_upper, mtr);
1587

osku's avatar
osku committed
1588 1589 1590 1591 1592 1593 1594 1595
	/* 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);
1596

osku's avatar
osku committed
1597
	/* Update page links of the level */
1598

osku's avatar
osku committed
1599 1600 1601 1602 1603
	if (prev_page_no != FIL_NULL) {

		prev_page = btr_page_get(space, prev_page_no, RW_X_LATCH, mtr);
		ut_a(page_is_comp(prev_page) == page_is_comp(page));

marko's avatar
marko committed
1604 1605 1606
		btr_page_set_next(prev_page, buf_block_get_page_zip(
					buf_block_align(prev_page)),
					lower_page_no, mtr);
osku's avatar
osku committed
1607 1608 1609 1610 1611 1612 1613
	}

	if (next_page_no != FIL_NULL) {

		next_page = btr_page_get(space, next_page_no, RW_X_LATCH, mtr);
		ut_a(page_is_comp(next_page) == page_is_comp(page));

marko's avatar
marko committed
1614 1615 1616
		btr_page_set_prev(next_page, buf_block_get_page_zip(
					buf_block_align(next_page)),
					upper_page_no, mtr);
osku's avatar
osku committed
1617
	}
1618

marko's avatar
marko committed
1619 1620 1621
	btr_page_set_prev(lower_page, lower_page_zip, prev_page_no, mtr);
	btr_page_set_next(lower_page, lower_page_zip, upper_page_no, mtr);
	btr_page_set_level(lower_page, lower_page_zip, level, mtr);
osku's avatar
osku committed
1622

marko's avatar
marko committed
1623 1624 1625
	btr_page_set_prev(upper_page, upper_page_zip, lower_page_no, mtr);
	btr_page_set_next(upper_page, upper_page_zip, next_page_no, mtr);
	btr_page_set_level(upper_page, upper_page_zip, level, mtr);
osku's avatar
osku committed
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
}

/*****************************************************************
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 */
1646 1647
	const ulint*	ext,	/* in: array of extern field numbers */
	ulint		n_ext,	/* in: number of elements in vec */
osku's avatar
osku committed
1648 1649 1650
	mtr_t*		mtr)	/* in: mtr */
{
	dict_tree_t*	tree;
1651
	page_t*		page;
marko's avatar
marko committed
1652
	page_zip_des_t*	page_zip;
1653
	ulint		page_no;
osku's avatar
osku committed
1654
	byte		direction;
1655 1656
	ulint		hint_page_no;
	page_t*		new_page;
osku's avatar
osku committed
1657 1658 1659 1660
	rec_t*		split_rec;
	page_t*		left_page;
	page_t*		right_page;
	page_t*		insert_page;
1661
	page_zip_des_t*	insert_page_zip;
osku's avatar
osku committed
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
	page_cur_t*	page_cursor;
	rec_t*		first_rec;
	byte*		buf = 0; /* remove warning */
	rec_t*		move_limit;
	ibool		insert_will_fit;
	ulint		n_iterations = 0;
	rec_t*		rec;
	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);
func_start:
	mem_heap_empty(heap);
	offsets = NULL;
	tree = btr_cur_get_tree(cursor);
1679

osku's avatar
osku committed
1680 1681 1682 1683 1684 1685
	ut_ad(mtr_memo_contains(mtr, dict_tree_get_lock(tree),
							MTR_MEMO_X_LOCK));
#ifdef UNIV_SYNC_DEBUG
	ut_ad(rw_lock_own(dict_tree_get_lock(tree), RW_LOCK_EX));
#endif /* UNIV_SYNC_DEBUG */

1686
	page = btr_cur_get_page(cursor);
marko's avatar
marko committed
1687
	page_zip = buf_block_get_page_zip(buf_block_align(page));
osku's avatar
osku committed
1688

1689
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
1690
							MTR_MEMO_PAGE_X_FIX));
1691
	ut_ad(page_get_n_recs(page) >= 2);
1692

1693
	page_no = buf_frame_get_page_no(page);
osku's avatar
osku committed
1694 1695 1696 1697 1698 1699 1700

	/* 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;
1701
		hint_page_no = page_no + 1;
osku's avatar
osku committed
1702
		split_rec = btr_page_get_sure_split_rec(cursor, tuple);
1703

osku's avatar
osku committed
1704 1705
	} else if (btr_page_get_split_rec_to_right(cursor, &split_rec)) {
		direction = FSP_UP;
1706
		hint_page_no = page_no + 1;
osku's avatar
osku committed
1707 1708 1709

	} else if (btr_page_get_split_rec_to_left(cursor, &split_rec)) {
		direction = FSP_DOWN;
1710
		hint_page_no = page_no - 1;
osku's avatar
osku committed
1711 1712
	} else {
		direction = FSP_UP;
1713 1714
		hint_page_no = page_no + 1;
		split_rec = page_get_middle_rec(page);
osku's avatar
osku committed
1715 1716 1717
	}

	/* 2. Allocate a new page to the tree */
1718 1719 1720
	new_page = btr_page_alloc(tree, hint_page_no, direction,
					btr_page_get_level(page, mtr), mtr);
	btr_page_create(new_page, tree, mtr);
1721

osku's avatar
osku committed
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
	/* 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 {
		buf = mem_alloc(rec_get_converted_size(cursor->index, tuple));

		first_rec = rec_convert_dtuple_to_rec(buf,
							cursor->index, tuple);
		move_limit = page_rec_get_next(btr_cur_get_rec(cursor));
	}
1736

osku's avatar
osku committed
1737 1738
	/* 4. Do first the modifications in the tree structure */

1739
	btr_attach_half_pages(tree, page, page_zip, first_rec,
marko's avatar
marko committed
1740
			new_page, direction, mtr);
osku's avatar
osku committed
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753

	/* 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. */

	if (split_rec) {
		offsets = rec_get_offsets(split_rec, cursor->index, offsets,
							n_uniq, &heap);

		insert_will_fit = btr_page_insert_fits(cursor,
					split_rec, offsets, tuple, heap);
	} else {
marko's avatar
marko committed
1754
		mem_free(buf);
osku's avatar
osku committed
1755 1756 1757
		insert_will_fit = btr_page_insert_fits(cursor,
					NULL, NULL, tuple, heap);
	}
1758

1759
	if (insert_will_fit && (btr_page_get_level(page, mtr) == 0)) {
osku's avatar
osku committed
1760 1761 1762 1763 1764 1765

		mtr_memo_release(mtr, dict_tree_get_lock(tree),
							MTR_MEMO_X_LOCK);
	}

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

1769 1770
		page_move_rec_list_start(new_page, buf_block_get_page_zip(
					buf_block_align(new_page)),
1771
					move_limit, page_zip,
1772
					cursor->index, mtr);
osku's avatar
osku committed
1773

1774 1775
		left_page = new_page;
		right_page = page;
osku's avatar
osku committed
1776 1777 1778 1779 1780

		lock_update_split_left(right_page, left_page);
	} else {
/*		fputs("Split right\n", stderr); */

1781 1782
		page_move_rec_list_end(new_page, buf_block_get_page_zip(
					buf_block_align(new_page)),
1783
					move_limit, page_zip,
1784 1785 1786 1787
					cursor->index, mtr);
		left_page = page;
		right_page = new_page;

osku's avatar
osku committed
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
		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 {
		offsets = rec_get_offsets(first_rec, cursor->index,
						offsets, n_uniq, &heap);

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

			insert_page = right_page;
		} else {
			insert_page = left_page;
		}
	}

1809 1810
	insert_page_zip = buf_block_get_page_zip(buf_block_align(insert_page));

osku's avatar
osku committed
1811 1812 1813 1814 1815 1816
	/* 7. Reposition the cursor for insert and try insertion */
	page_cursor = btr_cur_get_page_cur(cursor);

	page_cur_search(insert_page, cursor->index, tuple,
						PAGE_CUR_LE, page_cursor);

1817
	rec = page_cur_tuple_insert(page_cursor, insert_page_zip,
1818
					tuple, cursor->index, ext, n_ext, mtr);
marko's avatar
marko committed
1819

1820 1821
	ut_ad(!insert_page_zip
		|| page_zip_validate(insert_page_zip, insert_page));
osku's avatar
osku committed
1822

marko's avatar
marko committed
1823
	if (UNIV_LIKELY(rec != NULL)) {
osku's avatar
osku committed
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
		/* 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);
		/* fprintf(stderr, "Split and insert done %lu %lu\n",
				buf_frame_get_page_no(left_page),
				buf_frame_get_page_no(right_page)); */
		mem_heap_free(heap);
		return(rec);
	}
1836

osku's avatar
osku committed
1837 1838
	/* 8. If insert did not fit, try page reorganization */

1839 1840 1841 1842 1843
	if (UNIV_UNLIKELY(!btr_page_reorganize(
				insert_page, cursor->index, mtr))) {

		goto insert_failed;
	}
osku's avatar
osku committed
1844 1845 1846

	page_cur_search(insert_page, cursor->index, tuple,
						PAGE_CUR_LE, page_cursor);
1847
	rec = page_cur_tuple_insert(page_cursor, insert_page_zip,
1848
					tuple, cursor->index, ext, n_ext, mtr);
marko's avatar
marko committed
1849 1850

	if (UNIV_UNLIKELY(rec == NULL)) {
osku's avatar
osku committed
1851 1852
		/* The insert did not fit on the page: loop back to the
		start of the function for a new split */
1853
insert_failed:
1854 1855
		/* We play safe and reset the free bits for new_page */
		ibuf_reset_free_bits(cursor->index, new_page);
osku's avatar
osku committed
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890

		/* fprintf(stderr, "Split second round %lu\n",
					buf_frame_get_page_no(page)); */
		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,
							right_page, mtr);
	/* fprintf(stderr, "Split and insert done %lu %lu\n",
				buf_frame_get_page_no(left_page),
				buf_frame_get_page_no(right_page)); */

	ut_ad(page_validate(left_page, UT_LIST_GET_FIRST(tree->tree_indexes)));
	ut_ad(page_validate(right_page, UT_LIST_GET_FIRST(tree->tree_indexes)));

	mem_heap_free(heap);
	return(rec);
}

/*****************************************************************
Removes a page from the level list of pages. */
static
void
btr_level_list_remove(
/*==================*/
	dict_tree_t*	tree __attribute__((unused)), /* in: index tree */
	page_t*		page,	/* in: page to remove */
	mtr_t*		mtr)	/* in: mtr */
1891
{
osku's avatar
osku committed
1892 1893 1894 1895 1896
	ulint	space;
	ulint	prev_page_no;
	page_t*	prev_page;
	ulint	next_page_no;
	page_t*	next_page;
1897

osku's avatar
osku committed
1898 1899
	ut_ad(tree && page && mtr);
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
1900
							MTR_MEMO_PAGE_X_FIX));
osku's avatar
osku committed
1901 1902 1903 1904 1905
	/* 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);
1906

osku's avatar
osku committed
1907
	/* Update page links of the level */
1908

osku's avatar
osku committed
1909 1910 1911 1912 1913
	if (prev_page_no != FIL_NULL) {

		prev_page = btr_page_get(space, prev_page_no, RW_X_LATCH, mtr);
		ut_a(page_is_comp(prev_page) == page_is_comp(page));

marko's avatar
marko committed
1914 1915 1916
		btr_page_set_next(prev_page, buf_block_get_page_zip(
				buf_block_align(prev_page)),
				next_page_no, mtr);
osku's avatar
osku committed
1917 1918 1919 1920 1921 1922 1923
	}

	if (next_page_no != FIL_NULL) {

		next_page = btr_page_get(space, next_page_no, RW_X_LATCH, mtr);
		ut_a(page_is_comp(next_page) == page_is_comp(page));

marko's avatar
marko committed
1924 1925 1926
		btr_page_set_prev(next_page, buf_block_get_page_zip(
				buf_block_align(next_page)),
				prev_page_no, mtr);
osku's avatar
osku committed
1927 1928
	}
}
1929

osku's avatar
osku committed
1930 1931 1932 1933 1934 1935 1936 1937
/********************************************************************
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 */
marko's avatar
marko committed
1938
	byte	type,	/* in: MLOG_COMP_REC_MIN_MARK or MLOG_REC_MIN_MARK */
osku's avatar
osku committed
1939 1940
	mtr_t*	mtr)	/* in: mtr */
{
marko's avatar
marko committed
1941
	mlog_write_initial_log_record(rec, type, mtr);
osku's avatar
osku committed
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973

	/* Write rec offset as a 2-byte ulint */
	mlog_catenate_ulint(mtr, ut_align_offset(rec, UNIV_PAGE_SIZE),
								MLOG_2BYTES);
}

/********************************************************************
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 */
	ulint	comp,	/* in: nonzero=compact page format */
	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) {
		ut_a(!page_is_comp(page) == !comp);

		rec = page + mach_read_from_2(ptr);

1974
		btr_set_min_rec_mark(rec, mtr);
osku's avatar
osku committed
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
	}

	return(ptr + 2);
}

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

void
btr_set_min_rec_mark(
/*=================*/
1986 1987
	rec_t*	rec,	/* in: record */
	mtr_t*	mtr)	/* in: mtr */
osku's avatar
osku committed
1988 1989 1990
{
	ulint	info_bits;

marko's avatar
marko committed
1991 1992
	if (UNIV_LIKELY(page_rec_is_comp(rec))) {
		info_bits = rec_get_info_bits(rec, TRUE);
osku's avatar
osku committed
1993

1994
		rec_set_info_bits_new(rec, info_bits | REC_INFO_MIN_REC_FLAG);
marko's avatar
marko committed
1995 1996 1997 1998 1999 2000 2001 2002 2003

		btr_set_min_rec_mark_log(rec, MLOG_COMP_REC_MIN_MARK, mtr);
	} else {
		info_bits = rec_get_info_bits(rec, FALSE);

		rec_set_info_bits_old(rec, info_bits | REC_INFO_MIN_REC_FLAG);

		btr_set_min_rec_mark_log(rec, MLOG_REC_MIN_MARK, mtr);
	}
osku's avatar
osku committed
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
}

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

void
btr_node_ptr_delete(
/*================*/
	dict_tree_t*	tree,	/* in: index tree */
	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;
2020

osku's avatar
osku committed
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
							MTR_MEMO_PAGE_X_FIX));
	/* Delete node pointer on father page */

	node_ptr = btr_page_get_father_node_ptr(tree, page, mtr);

	btr_cur_position(UT_LIST_GET_FIRST(tree->tree_indexes), node_ptr,
								&cursor);
	compressed = btr_cur_pessimistic_delete(&err, TRUE, &cursor, FALSE,
									mtr);
	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
marko's avatar
marko committed
2042
ibool
osku's avatar
osku committed
2043 2044
btr_lift_page_up(
/*=============*/
marko's avatar
marko committed
2045
				/* out: TRUE on success */
osku's avatar
osku committed
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
	dict_tree_t*	tree,	/* in: index tree */
	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 */
{
	page_t*		father_page;
	ulint		page_level;
	dict_index_t*	index;
marko's avatar
marko committed
2056
	page_zip_des_t*	father_page_zip;
osku's avatar
osku committed
2057 2058 2059 2060

	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),
2061
							MTR_MEMO_PAGE_X_FIX));
osku's avatar
osku committed
2062 2063
	father_page = buf_frame_align(
			btr_page_get_father_node_ptr(tree, page, mtr));
marko's avatar
marko committed
2064 2065
	father_page_zip = buf_block_get_page_zip(buf_block_align(father_page));

osku's avatar
osku committed
2066 2067 2068 2069
	page_level = btr_page_get_level(page, mtr);
	index = UT_LIST_GET_FIRST(tree->tree_indexes);

	btr_search_drop_page_hash_index(page);
2070

osku's avatar
osku committed
2071
	/* Make the father empty */
2072
	btr_page_empty(father_page, NULL, mtr, index);
osku's avatar
osku committed
2073 2074

	/* Move records to the father */
marko's avatar
marko committed
2075 2076 2077 2078
	if (!page_copy_rec_list_end(father_page, NULL,
				page_get_infimum_rec(page), index, mtr)) {
		ut_error;
	}
osku's avatar
osku committed
2079

marko's avatar
marko committed
2080
	btr_page_set_level(father_page, NULL, page_level, mtr);
osku's avatar
osku committed
2081

marko's avatar
marko committed
2082 2083
	if (UNIV_LIKELY_NULL(father_page_zip)) {
		if (UNIV_UNLIKELY(!page_zip_compress(
2084
				father_page_zip, father_page, index, mtr))) {
marko's avatar
marko committed
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
			/* Restore the old page from temporary space */
			if (UNIV_UNLIKELY(!page_zip_decompress(
					father_page_zip, father_page, mtr))) {
				ut_error; /* probably memory corruption */
			}

			return(FALSE);
		}
	}

	lock_update_copy_and_discard(father_page, page);

osku's avatar
osku committed
2097
	/* Free the file page */
2098
	btr_page_free(tree, page, mtr);
osku's avatar
osku committed
2099 2100 2101 2102 2103

	/* We play safe and reset the free bits for the father */
	ibuf_reset_free_bits(index, father_page);
	ut_ad(page_validate(father_page, index));
	ut_ad(btr_check_node_ptr(tree, father_page, mtr));
marko's avatar
marko committed
2104 2105

	return(TRUE);
2106
}
osku's avatar
osku committed
2107 2108 2109 2110 2111 2112 2113 2114 2115

/*****************************************************************
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
marko's avatar
marko committed
2116
brothers, if they exist. */
osku's avatar
osku committed
2117

marko's avatar
marko committed
2118
ibool
osku's avatar
osku committed
2119 2120
btr_compress(
/*=========*/
marko's avatar
marko committed
2121
				/* out: TRUE on success */
osku's avatar
osku committed
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
	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 */
{
	dict_tree_t*	tree;
	ulint		space;
	ulint		left_page_no;
	ulint		right_page_no;
	page_t*		merge_page;
	ibool		is_left;
	page_t*		page;
marko's avatar
marko committed
2135
	page_zip_des_t*	page_zip;
osku's avatar
osku committed
2136 2137 2138 2139 2140 2141 2142 2143
	rec_t*		node_ptr;
	ulint		data_size;
	ulint		n_recs;
	ulint		max_ins_size;
	ulint		max_ins_size_reorg;
	ulint		level;

	page = btr_cur_get_page(cursor);
marko's avatar
marko committed
2144
	page_zip = buf_block_get_page_zip(buf_block_align(page));
osku's avatar
osku committed
2145
	tree = btr_cur_get_tree(cursor);
marko's avatar
marko committed
2146
	ut_a((ibool)!!page_is_comp(page) == cursor->index->table->comp);
osku's avatar
osku committed
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161

	ut_ad(mtr_memo_contains(mtr, dict_tree_get_lock(tree),
							MTR_MEMO_X_LOCK));
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
							MTR_MEMO_PAGE_X_FIX));
	level = btr_page_get_level(page, mtr);
	space = dict_tree_get_space(tree);

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

/*	fprintf(stderr, "Merge left page %lu right %lu \n", left_page_no,
							right_page_no); */

	node_ptr = btr_page_get_father_node_ptr(tree, page, mtr);
marko's avatar
marko committed
2162 2163
	ut_ad(!page_is_comp(page)
		|| rec_get_status(node_ptr) == REC_STATUS_NODE_PTR);
marko's avatar
marko committed
2164
	ut_a(page_is_comp(page) == page_rec_is_comp(node_ptr));
osku's avatar
osku committed
2165 2166 2167 2168

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

marko's avatar
marko committed
2169 2170 2171
	is_left = left_page_no != FIL_NULL;

	if (is_left) {
osku's avatar
osku committed
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181

		merge_page = btr_page_get(space, left_page_no, RW_X_LATCH,
									mtr);
	} else if (right_page_no != FIL_NULL) {

		merge_page = btr_page_get(space, right_page_no, RW_X_LATCH,
									mtr);
	} else {
		/* The page is the only one on the level, lift the records
		to the father */
marko's avatar
marko committed
2182
		return(btr_lift_page_up(tree, page, mtr));
osku's avatar
osku committed
2183
	}
2184

osku's avatar
osku committed
2185 2186
	n_recs = page_get_n_recs(page);
	data_size = page_get_data_size(page);
marko's avatar
marko committed
2187
	ut_a(page_is_comp(merge_page) == page_is_comp(page));
osku's avatar
osku committed
2188 2189 2190 2191 2192 2193 2194

	max_ins_size_reorg = page_get_max_insert_size_after_reorganize(
							merge_page, n_recs);
	if (data_size > max_ins_size_reorg) {

		/* No space for merge */

marko's avatar
marko committed
2195
		return(FALSE);
osku's avatar
osku committed
2196 2197 2198 2199 2200
	}

	ut_ad(page_validate(merge_page, cursor->index));

	max_ins_size = page_get_max_insert_size(merge_page, n_recs);
2201

marko's avatar
marko committed
2202
	if (UNIV_UNLIKELY(data_size > max_ins_size)) {
osku's avatar
osku committed
2203 2204 2205

		/* We have to reorganize merge_page */

2206 2207 2208 2209 2210
		if (UNIV_UNLIKELY(!btr_page_reorganize(
				merge_page, cursor->index, mtr))) {

			return(FALSE);
		}
osku's avatar
osku committed
2211 2212 2213 2214 2215 2216 2217

		max_ins_size = page_get_max_insert_size(merge_page, n_recs);

		ut_ad(page_validate(merge_page, cursor->index));
		ut_ad(page_get_max_insert_size(merge_page, n_recs)
							== max_ins_size_reorg);

marko's avatar
marko committed
2218
		if (UNIV_UNLIKELY(data_size > max_ins_size)) {
osku's avatar
osku committed
2219

marko's avatar
marko committed
2220 2221
			/* Add fault tolerance, though this should
			never happen */
osku's avatar
osku committed
2222

marko's avatar
marko committed
2223 2224
			return(FALSE);
		}
osku's avatar
osku committed
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237
	}

	btr_search_drop_page_hash_index(page);

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

	if (is_left) {
		btr_node_ptr_delete(tree, page, mtr);
	} else {
		mem_heap_t*	heap		= NULL;
		ulint		offsets_[REC_OFFS_NORMAL_SIZE];
		*offsets_ = (sizeof offsets_) / sizeof *offsets_;
2238
		/* Replace the address of the old child node (= page) with the
osku's avatar
osku committed
2239 2240
		address of the merge page to the right */

2241
		btr_node_ptr_set_child_page_no(node_ptr, page_zip,
osku's avatar
osku committed
2242 2243 2244 2245 2246 2247 2248 2249
				rec_get_offsets(node_ptr, cursor->index,
				offsets_, ULINT_UNDEFINED, &heap),
				right_page_no, mtr);
		if (UNIV_LIKELY_NULL(heap)) {
			mem_heap_free(heap);
		}
		btr_node_ptr_delete(tree, merge_page, mtr);
	}
marko's avatar
marko committed
2250

osku's avatar
osku committed
2251 2252
	/* Move records to the merge page */
	if (is_left) {
marko's avatar
marko committed
2253
		rec_t*	orig_pred = page_rec_get_prev(
osku's avatar
osku committed
2254
					page_get_supremum_rec(merge_page));
marko's avatar
marko committed
2255 2256 2257 2258 2259 2260 2261
		if (UNIV_UNLIKELY(!page_copy_rec_list_start(
				merge_page, buf_block_get_page_zip(
					buf_block_align(merge_page)),
				page_get_supremum_rec(page),
				cursor->index, mtr))) {
			return(FALSE);
		}
osku's avatar
osku committed
2262 2263 2264

		lock_update_merge_left(merge_page, orig_pred, page);
	} else {
marko's avatar
marko committed
2265
		rec_t*	orig_succ = page_rec_get_next(
osku's avatar
osku committed
2266
					page_get_infimum_rec(merge_page));
marko's avatar
marko committed
2267 2268 2269 2270 2271 2272 2273
		if (UNIV_UNLIKELY(!page_copy_rec_list_end(
				merge_page, buf_block_get_page_zip(
					buf_block_align(merge_page)),
				page_get_infimum_rec(page),
				cursor->index, mtr))) {
			return(FALSE);
		}
osku's avatar
osku committed
2274 2275 2276 2277 2278 2279 2280

		lock_update_merge_right(orig_succ, page);
	}

	/* We have added new records to merge_page: update its free bits */
	ibuf_update_free_bits_if_full(cursor->index, merge_page,
					UNIV_PAGE_SIZE, ULINT_UNDEFINED);
2281

osku's avatar
osku committed
2282 2283 2284
	ut_ad(page_validate(merge_page, cursor->index));

	/* Free the file page */
2285
	btr_page_free(tree, page, mtr);
osku's avatar
osku committed
2286 2287

	ut_ad(btr_check_node_ptr(tree, merge_page, mtr));
marko's avatar
marko committed
2288
	return(TRUE);
2289
}
osku's avatar
osku committed
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302

/*****************************************************************
Discards a page that is the only page on its level. */
static
void
btr_discard_only_page_on_level(
/*===========================*/
	dict_tree_t*	tree,	/* in: index tree */
	page_t*		page,	/* in: page which is the only on its level */
	mtr_t*		mtr)	/* in: mtr */
{
	page_t*	father_page;
	ulint	page_level;
2303

osku's avatar
osku committed
2304 2305 2306 2307 2308 2309
	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),
							MTR_MEMO_PAGE_X_FIX));
	btr_search_drop_page_hash_index(page);

marko's avatar
marko committed
2310 2311
	father_page = buf_frame_align(
			btr_page_get_father_node_ptr(tree, page, mtr));
osku's avatar
osku committed
2312 2313 2314 2315 2316

	page_level = btr_page_get_level(page, mtr);

	lock_update_discard(page_get_supremum_rec(father_page), page);

marko's avatar
marko committed
2317 2318 2319
	btr_page_set_level(father_page, buf_block_get_page_zip(
				buf_block_align(father_page)),
				page_level, mtr);
osku's avatar
osku committed
2320 2321

	/* Free the file page */
2322
	btr_page_free(tree, page, mtr);
osku's avatar
osku committed
2323

marko's avatar
marko committed
2324
	if (UNIV_LIKELY(buf_frame_get_page_no(father_page)
marko's avatar
marko committed
2325
				== dict_tree_get_page(tree))) {
osku's avatar
osku committed
2326 2327
		/* The father is the root page */

2328 2329
		dict_index_t*	index = UT_LIST_GET_FIRST(tree->tree_indexes);

marko's avatar
marko committed
2330 2331
		btr_page_empty(father_page,
			buf_block_get_page_zip(buf_block_align(father_page)),
2332
			mtr, index);
osku's avatar
osku committed
2333 2334

		/* We play safe and reset the free bits for the father */
2335
		ibuf_reset_free_bits(index, father_page);
osku's avatar
osku committed
2336 2337 2338 2339 2340
	} else {
		ut_ad(page_get_n_recs(father_page) == 1);

		btr_discard_only_page_on_level(tree, father_page, mtr);
	}
2341
}
osku's avatar
osku committed
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361

/*****************************************************************
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 */
{
	dict_tree_t*	tree;
	ulint		space;
	ulint		left_page_no;
	ulint		right_page_no;
	page_t*		merge_page;
	page_t*		page;
	rec_t*		node_ptr;
2362

osku's avatar
osku committed
2363 2364 2365 2366 2367 2368 2369 2370 2371
	page = btr_cur_get_page(cursor);
	tree = btr_cur_get_tree(cursor);

	ut_ad(dict_tree_get_page(tree) != buf_frame_get_page_no(page));
	ut_ad(mtr_memo_contains(mtr, dict_tree_get_lock(tree),
							MTR_MEMO_X_LOCK));
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
							MTR_MEMO_PAGE_X_FIX));
	space = dict_tree_get_space(tree);
2372

osku's avatar
osku committed
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
	/* 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,
									mtr);
	} else if (right_page_no != FIL_NULL) {
		merge_page = btr_page_get(space, right_page_no, RW_X_LATCH,
									mtr);
	} else {
		btr_discard_only_page_on_level(tree, page, mtr);

		return;
	}

	ut_a(page_is_comp(merge_page) == page_is_comp(page));
	btr_search_drop_page_hash_index(page);
2392

osku's avatar
osku committed
2393 2394 2395 2396 2397 2398 2399 2400
	if (left_page_no == FIL_NULL && btr_page_get_level(page, mtr) > 0) {

		/* 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));

		ut_ad(page_rec_is_user_rec(node_ptr));

2401
		btr_set_min_rec_mark(node_ptr, mtr);
2402 2403
	}

osku's avatar
osku committed
2404 2405 2406 2407 2408
	btr_node_ptr_delete(tree, page, mtr);

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

marko's avatar
marko committed
2409
	if (left_page_no != FIL_NULL) {
osku's avatar
osku committed
2410 2411 2412
		lock_update_discard(page_get_supremum_rec(merge_page), page);
	} else {
		lock_update_discard(page_rec_get_next(
2413
				page_get_infimum_rec(merge_page)), page);
osku's avatar
osku committed
2414 2415 2416
	}

	/* Free the file page */
2417
	btr_page_free(tree, page, mtr);
osku's avatar
osku committed
2418 2419

	ut_ad(btr_check_node_ptr(tree, merge_page, mtr));
2420
}
osku's avatar
osku committed
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443

#ifdef UNIV_BTR_PRINT
/*****************************************************************
Prints size info of a B-tree. */

void
btr_print_size(
/*===========*/
	dict_tree_t*	tree)	/* in: index tree */
{
	page_t*		root;
	fseg_header_t*	seg;
	mtr_t		mtr;

	if (tree->type & DICT_IBUF) {
		fputs(
	"Sorry, cannot print info of an ibuf tree: use ibuf functions\n",
			stderr);

		return;
	}

	mtr_start(&mtr);
2444

osku's avatar
osku committed
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
	root = btr_root_get(tree, &mtr);

	seg = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;

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

	if (!(tree->type & DICT_UNIVERSAL)) {

		seg = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;

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

2460
	mtr_commit(&mtr);
osku's avatar
osku committed
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
}

/****************************************************************
Prints recursively index tree pages. */
static
void
btr_print_recursive(
/*================*/
	dict_tree_t*	tree,	/* in: index tree */
	page_t*		page,	/* in: index page */
	ulint		width,	/* in: print this many entries from start
				and end */
	mem_heap_t**	heap,	/* in/out: heap for rec_get_offsets() */
	ulint**		offsets,/* in/out: buffer for rec_get_offsets() */
	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;
	dict_index_t*	index;

	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
							MTR_MEMO_PAGE_X_FIX));
	fprintf(stderr, "NODE ON LEVEL %lu page number %lu\n",
2488 2489 2490
		(ulong) btr_page_get_level(page, mtr),
		(ulong) buf_frame_get_page_no(page));

osku's avatar
osku committed
2491 2492
	index = UT_LIST_GET_FIRST(tree->tree_indexes);
	page_print(page, index, width, width);
2493

osku's avatar
osku committed
2494
	n_recs = page_get_n_recs(page);
2495

osku's avatar
osku committed
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559
	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);

			*offsets = rec_get_offsets(node_ptr, index, *offsets,
					ULINT_UNDEFINED, heap);
			child = btr_node_ptr_get_child(node_ptr,
					*offsets, &mtr2);
			btr_print_recursive(tree, child, width,
					heap, offsets, &mtr2);
			mtr_commit(&mtr2);
		}

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

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

void
btr_print_tree(
/*===========*/
	dict_tree_t*	tree,	/* in: tree */
	ulint		width)	/* in: print this many entries from start
				and end */
{
	mtr_t		mtr;
	page_t*		root;
	mem_heap_t*	heap	= NULL;
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
	ulint*		offsets	= offsets_;
	*offsets_ = (sizeof offsets_) / sizeof *offsets_;

	fputs("--------------------------\n"
		"INDEX TREE PRINT\n", stderr);

	mtr_start(&mtr);

	root = btr_root_get(tree, &mtr);

	btr_print_recursive(tree, root, width, &heap, &offsets, &mtr);
	if (UNIV_LIKELY_NULL(heap)) {
		mem_heap_free(heap);
	}

	mtr_commit(&mtr);

	btr_validate_tree(tree, NULL);
}
#endif /* UNIV_BTR_PRINT */

2560
#ifdef UNIV_DEBUG
osku's avatar
osku committed
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583
/****************************************************************
Checks that the node pointer to a page is appropriate. */

ibool
btr_check_node_ptr(
/*===============*/
				/* out: TRUE */
	dict_tree_t*	tree,	/* in: index tree */
	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),
							MTR_MEMO_PAGE_X_FIX));
	if (dict_tree_get_page(tree) == buf_frame_get_page_no(page)) {

		return(TRUE);
	}

	node_ptr = btr_page_get_father_node_ptr(tree, page, mtr);
2584

osku's avatar
osku committed
2585 2586 2587 2588
	if (btr_page_get_level(page, mtr) == 0) {

		return(TRUE);
	}
2589

osku's avatar
osku committed
2590
	heap = mem_heap_create(256);
2591

osku's avatar
osku committed
2592 2593 2594 2595
	node_ptr_tuple = dict_tree_build_node_ptr(
				tree,
				page_rec_get_next(page_get_infimum_rec(page)),
				0, heap, btr_page_get_level(page, mtr));
2596

osku's avatar
osku committed
2597 2598 2599 2600 2601 2602 2603 2604 2605
	ut_a(cmp_dtuple_rec(node_ptr_tuple, node_ptr,
			rec_get_offsets(node_ptr,
			dict_tree_find_index(tree, node_ptr),
			NULL, ULINT_UNDEFINED, &heap)) == 0);

	mem_heap_free(heap);

	return(TRUE);
}
2606
#endif /* UNIV_DEBUG */
osku's avatar
osku committed
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629

/****************************************************************
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);
	dict_index_name_print(stderr, NULL, index);
	fprintf(stderr, ", page %lu, at offset %lu\n",
		buf_frame_get_page_no(page), (ulint)(rec - page));
}

/****************************************************************
Checks the size and number of fields in a record based on the definition of
the index. */

ibool
btr_index_rec_validate(
2630
/*===================*/
osku's avatar
osku committed
2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649
					/* 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 */
{
	ulint		len;
	ulint		n;
	ulint		i;
	page_t*		page;
	mem_heap_t*	heap	= NULL;
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
	ulint*		offsets	= offsets_;
	*offsets_ = (sizeof offsets_) / sizeof *offsets_;

	page = buf_frame_align(rec);

	if (UNIV_UNLIKELY(index->type & DICT_UNIVERSAL)) {
2650 2651 2652
		/* The insert buffer index tree can contain records from any
		other index: we cannot check the number of fields or
		their length */
osku's avatar
osku committed
2653

2654
		return(TRUE);
osku's avatar
osku committed
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
	}

	if (UNIV_UNLIKELY((ibool)!!page_is_comp(page) != index->table->comp)) {
		btr_index_rec_validate_report(page, rec, index);
		fprintf(stderr, "InnoDB: compact flag=%lu, should be %lu\n",
			(ulong) !!page_is_comp(page),
			(ulong) index->table->comp);
		return(FALSE);
	}

	n = dict_index_get_n_fields(index);

	if (!page_is_comp(page)
			&& UNIV_UNLIKELY(rec_get_n_fields_old(rec) != n)) {
		btr_index_rec_validate_report(page, rec, index);
		fprintf(stderr, "InnoDB: has %lu fields, should have %lu\n",
			(ulong) rec_get_n_fields_old(rec), (ulong) n);

		if (dump_on_error) {
			buf_page_print(page);

			fputs("InnoDB: corrupt record ", stderr);
			rec_print_old(stderr, rec);
			putc('\n', stderr);
		}
		return(FALSE);
	}

	offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, &heap);

	for (i = 0; i < n; i++) {
		dtype_t*	type = dict_index_get_nth_type(index, i);
		ulint		fixed_size = dtype_get_fixed_size(type);

		rec_get_nth_field(rec, offsets, i, &len);

		/* Note that prefix indexes are not fixed size even when
		their type is CHAR. */

		if ((dict_index_get_nth_field(index, i)->prefix_len == 0
2695 2696 2697 2698 2699 2700
				&& 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)) {
osku's avatar
osku committed
2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723

			btr_index_rec_validate_report(page, rec, index);
			fprintf(stderr,
"InnoDB: field %lu len is %lu, should be %lu\n",
				(ulong) i, (ulong) len, (ulong) dtype_get_fixed_size(type));

			if (dump_on_error) {
				buf_page_print(page);

				fputs("InnoDB: corrupt record ", stderr);
				rec_print_new(stderr, rec, offsets);
				putc('\n', stderr);
			}
			if (UNIV_LIKELY_NULL(heap)) {
				mem_heap_free(heap);
			}
			return(FALSE);
		}
	}

	if (UNIV_LIKELY_NULL(heap)) {
		mem_heap_free(heap);
	}
2724
	return(TRUE);
osku's avatar
osku committed
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
}

/****************************************************************
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 */
{
2738
	page_cur_t	cur;
osku's avatar
osku committed
2739
	ibool		ret	= TRUE;
2740

osku's avatar
osku committed
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757
	page_cur_set_before_first(page, &cur);
	page_cur_move_to_next(&cur);

	for (;;) {
		if (page_cur_is_after_last(&cur)) {

			break;
		}

		if (!btr_index_rec_validate(cur.rec, index, TRUE)) {

			return(FALSE);
		}

		page_cur_move_to_next(&cur);
	}

2758
	return(ret);
osku's avatar
osku committed
2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833
}

/****************************************************************
Report an error on one page of an index tree. */
static
void
btr_validate_report1(
				/* 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));
	dict_index_name_print(stderr, NULL, index);
	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(
				/* 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));
	dict_index_name_print(stderr, NULL, index);
	if (level) {
		fprintf(stderr, ", index tree level %lu", level);
	}
	putc('\n', stderr);
}

/****************************************************************
Validates index tree level. */
static
ibool
btr_validate_level(
/*===============*/
				/* out: TRUE if ok */
	dict_tree_t*	tree,	/* in: index tree */
	trx_t*		trx,	/* in: transaction or NULL */
	ulint		level)	/* in: level number */
{
	ulint		space;
	page_t*		page;
	page_t*		right_page = 0; /* remove warning */
	page_t*		father_page;
	page_t*		right_father_page;
	rec_t*		node_ptr;
	rec_t*		right_node_ptr;
	rec_t*		rec;
	ulint		right_page_no;
	ulint		left_page_no;
	page_cur_t	cursor;
	dtuple_t*	node_ptr_tuple;
	ibool		ret	= TRUE;
	dict_index_t*	index;
	mtr_t		mtr;
	mem_heap_t*	heap	= mem_heap_create(256);
	ulint*		offsets	= NULL;
	ulint*		offsets2= NULL;

	mtr_start(&mtr);

	mtr_x_lock(dict_tree_get_lock(tree), &mtr);
2834

osku's avatar
osku committed
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880
	page = btr_root_get(tree, &mtr);

	space = buf_frame_get_space_id(page);

	index = UT_LIST_GET_FIRST(tree->tree_indexes);

	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);
		offsets = rec_get_offsets(node_ptr, index, offsets,
					ULINT_UNDEFINED, &heap);
		page = btr_node_ptr_get_child(node_ptr, offsets, &mtr);
	}

	/* Now we are on the desired level. Loop through the pages on that
	level. */
loop:
	if (trx_is_interrupted(trx)) {
		mtr_commit(&mtr);
		mem_heap_free(heap);
		return(ret);
	}
	mem_heap_empty(heap);
	offsets = offsets2 = NULL;
	mtr_x_lock(dict_tree_get_lock(tree), &mtr);

	/* Check ordering etc. of records */

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

		ret = FALSE;
	} else if (level == 0) {
		/* We are on level 0. Check that the records have the right
		number of fields, and field lengths are right. */

		if (!btr_index_page_validate(page, index)) {

			ret = FALSE;
		}
	}
2881

osku's avatar
osku committed
2882 2883 2884 2885 2886 2887
	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)
2888 2889
		|| ((level == 0) &&
		(buf_frame_get_page_no(page) == dict_tree_get_page(tree))));
osku's avatar
osku committed
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923

	if (right_page_no != FIL_NULL) {
		rec_t*	right_rec;
		right_page = btr_page_get(space, right_page_no, RW_X_LATCH,
									&mtr);
		ut_a(page_is_comp(right_page) == page_is_comp(page));
		rec = page_rec_get_prev(page_get_supremum_rec(page));
		right_rec = page_rec_get_next(
					page_get_infimum_rec(right_page));
		offsets = rec_get_offsets(rec, index,
					offsets, ULINT_UNDEFINED, &heap);
		offsets2 = rec_get_offsets(right_rec, index,
					offsets2, ULINT_UNDEFINED, &heap);
		if (cmp_rec_rec(rec, right_rec, offsets, offsets2, index)
				>= 0) {

			btr_validate_report2(index, level, page, right_page);

			fputs("InnoDB: records in wrong order"
				" on adjacent pages\n", stderr);

			buf_page_print(page);
			buf_page_print(right_page);

			fputs("InnoDB: record ", stderr);
			rec = page_rec_get_prev(page_get_supremum_rec(page));
			rec_print(stderr, rec, index);
			putc('\n', stderr);
			fputs("InnoDB: record ", stderr);
			rec = page_rec_get_next(page_get_infimum_rec(
						right_page));
			rec_print(stderr, rec, index);
			putc('\n', stderr);

2924 2925
			ret = FALSE;
		}
osku's avatar
osku committed
2926
	}
2927

osku's avatar
osku committed
2928 2929 2930 2931 2932 2933 2934 2935 2936
	if (level > 0 && left_page_no == FIL_NULL) {
		ut_a(REC_INFO_MIN_REC_FLAG & rec_get_info_bits(
			page_rec_get_next(page_get_infimum_rec(page)),
				page_is_comp(page)));
	}

	if (buf_frame_get_page_no(page) != dict_tree_get_page(tree)) {

		/* Check father node pointers */
2937

osku's avatar
osku committed
2938 2939 2940 2941 2942 2943 2944
		node_ptr = btr_page_get_father_node_ptr(tree, page, &mtr);
		father_page = buf_frame_align(node_ptr);
		offsets	= rec_get_offsets(node_ptr, index,
					offsets, ULINT_UNDEFINED, &heap);

		if (btr_node_ptr_get_child_page_no(node_ptr, offsets) !=
						buf_frame_get_page_no(page)
2945 2946 2947
				|| node_ptr
				!= btr_page_get_father_for_rec(tree, page,
				page_rec_get_prev(page_get_supremum_rec(page)),
osku's avatar
osku committed
2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970
							&mtr)) {
			btr_validate_report1(index, level, page);

			fputs("InnoDB: node pointer to the page is wrong\n",
				stderr);

			buf_page_print(father_page);
			buf_page_print(page);

			fputs("InnoDB: node ptr ", stderr);
			rec_print_new(stderr, node_ptr, offsets);

			fprintf(stderr, "\n"
				"InnoDB: node ptr child page n:o %lu\n",
				(unsigned long) btr_node_ptr_get_child_page_no(
						node_ptr, offsets));

			fputs("InnoDB: record on page ", stderr);
			rec = btr_page_get_father_for_rec(tree, page,
				page_rec_get_prev(page_get_supremum_rec(page)),
				&mtr);
			rec_print(stderr, rec, index);
			putc('\n', stderr);
2971
			ret = FALSE;
osku's avatar
osku committed
2972

2973
			goto node_ptr_fails;
osku's avatar
osku committed
2974 2975 2976 2977 2978
		}

		if (btr_page_get_level(page, &mtr) > 0) {
			offsets	= rec_get_offsets(node_ptr, index,
					offsets, ULINT_UNDEFINED, &heap);
2979

osku's avatar
osku committed
2980 2981 2982 2983 2984
			node_ptr_tuple = dict_tree_build_node_ptr(
					tree,
					page_rec_get_next(
						page_get_infimum_rec(page)),
						0, heap,
2985
					btr_page_get_level(page, &mtr));
osku's avatar
osku committed
2986 2987 2988

			if (cmp_dtuple_rec(node_ptr_tuple, node_ptr,
								offsets)) {
2989
				rec_t*	first_rec	= page_rec_get_next(
osku's avatar
osku committed
2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003
					page_get_infimum_rec(page));

				btr_validate_report1(index, level, page);

				buf_page_print(father_page);
				buf_page_print(page);

				fputs("InnoDB: Error: node ptrs differ"
					" on levels > 0\n"
					"InnoDB: node ptr ", stderr);
				rec_print_new(stderr, node_ptr, offsets);
				fputs("InnoDB: first rec ", stderr);
				rec_print(stderr, first_rec, index);
				putc('\n', stderr);
3004
				ret = FALSE;
osku's avatar
osku committed
3005

3006
				goto node_ptr_fails;
osku's avatar
osku committed
3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045
			}
		}

		if (left_page_no == FIL_NULL) {
			ut_a(node_ptr == page_rec_get_next(
					page_get_infimum_rec(father_page)));
			ut_a(btr_page_get_prev(father_page, &mtr) == FIL_NULL);
		}

		if (right_page_no == FIL_NULL) {
			ut_a(node_ptr == page_rec_get_prev(
				page_get_supremum_rec(father_page)));
			ut_a(btr_page_get_next(father_page, &mtr) == FIL_NULL);
		}

		if (right_page_no != FIL_NULL) {

			right_node_ptr = btr_page_get_father_node_ptr(tree,
							right_page, &mtr);
			if (page_rec_get_next(node_ptr) !=
					page_get_supremum_rec(father_page)) {

				if (right_node_ptr !=
						page_rec_get_next(node_ptr)) {
					ret = FALSE;
					fputs(
			"InnoDB: node pointer to the right page is wrong\n",
					stderr);

					btr_validate_report1(index, level,
						page);

					buf_page_print(father_page);
					buf_page_print(page);
					buf_page_print(right_page);
				}
			} else {
				right_father_page = buf_frame_align(
							right_node_ptr);
3046

osku's avatar
osku committed
3047
				if (right_node_ptr != page_rec_get_next(
3048
							page_get_infimum_rec(
osku's avatar
osku committed
3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064
							right_father_page))) {
					ret = FALSE;
					fputs(
			"InnoDB: node pointer 2 to the right page is wrong\n",
					stderr);

					btr_validate_report1(index, level,
						page);

					buf_page_print(father_page);
					buf_page_print(right_father_page);
					buf_page_print(page);
					buf_page_print(right_page);
				}

				if (buf_frame_get_page_no(right_father_page)
3065
				!= btr_page_get_next(father_page, &mtr)) {
osku's avatar
osku committed
3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079

					ret = FALSE;
					fputs(
			"InnoDB: node pointer 3 to the right page is wrong\n",
					stderr);

					btr_validate_report1(index, level,
						page);

					buf_page_print(father_page);
					buf_page_print(right_father_page);
					buf_page_print(page);
					buf_page_print(right_page);
				}
3080
			}
osku's avatar
osku committed
3081 3082 3083 3084 3085 3086 3087 3088 3089
		}
	}

node_ptr_fails:
	mtr_commit(&mtr);

	if (right_page_no != FIL_NULL) {
		ulint	comp = page_is_comp(page);
		mtr_start(&mtr);
3090

osku's avatar
osku committed
3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134
		page = btr_page_get(space, right_page_no, RW_X_LATCH, &mtr);
		ut_a(page_is_comp(page) == comp);

		goto loop;
	}

	mem_heap_free(heap);
	return(ret);
}

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

ibool
btr_validate_tree(
/*==============*/
				/* out: TRUE if ok */
	dict_tree_t*	tree,	/* in: tree */
	trx_t*		trx)	/* in: transaction or NULL */
{
	mtr_t	mtr;
	page_t*	root;
	ulint	i;
	ulint	n;

	mtr_start(&mtr);
	mtr_x_lock(dict_tree_get_lock(tree), &mtr);

	root = btr_root_get(tree, &mtr);
	n = btr_page_get_level(root, &mtr);

	for (i = 0; i <= n && !trx_is_interrupted(trx); i++) {
		if (!btr_validate_level(tree, trx, n - i)) {

			mtr_commit(&mtr);

			return(FALSE);
		}
	}

	mtr_commit(&mtr);

	return(TRUE);
}