You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3749 lines
105 KiB

17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  11. Place, Suite 330, Boston, MA 02111-1307 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file btr/btr0btr.c
  15. The B-tree
  16. Created 6/2/1994 Heikki Tuuri
  17. *******************************************************/
  18. #include "btr0btr.h"
  19. #ifdef UNIV_NONINL
  20. #include "btr0btr.ic"
  21. #endif
  22. #include "fsp0fsp.h"
  23. #include "page0page.h"
  24. #include "page0zip.h"
  25. #ifndef UNIV_HOTBACKUP
  26. #include "btr0cur.h"
  27. #include "btr0sea.h"
  28. #include "btr0pcur.h"
  29. #include "rem0cmp.h"
  30. #include "lock0lock.h"
  31. #include "ibuf0ibuf.h"
  32. #include "trx0trx.h"
  33. /*
  34. Latching strategy of the InnoDB B-tree
  35. --------------------------------------
  36. A tree latch protects all non-leaf nodes of the tree. Each node of a tree
  37. also has a latch of its own.
  38. A B-tree operation normally first acquires an S-latch on the tree. It
  39. searches down the tree and releases the tree latch when it has the
  40. leaf node latch. To save CPU time we do not acquire any latch on
  41. non-leaf nodes of the tree during a search, those pages are only bufferfixed.
  42. If an operation needs to restructure the tree, it acquires an X-latch on
  43. the tree before searching to a leaf node. If it needs, for example, to
  44. split a leaf,
  45. (1) InnoDB decides the split point in the leaf,
  46. (2) allocates a new page,
  47. (3) inserts the appropriate node pointer to the first non-leaf level,
  48. (4) releases the tree X-latch,
  49. (5) and then moves records from the leaf to the new allocated page.
  50. Node pointers
  51. -------------
  52. Leaf pages of a B-tree contain the index records stored in the
  53. tree. On levels n > 0 we store 'node pointers' to pages on level
  54. n - 1. For each page there is exactly one node pointer stored:
  55. thus the our tree is an ordinary B-tree, not a B-link tree.
  56. A node pointer contains a prefix P of an index record. The prefix
  57. is long enough so that it determines an index record uniquely.
  58. The file page number of the child page is added as the last
  59. field. To the child page we can store node pointers or index records
  60. which are >= P in the alphabetical order, but < P1 if there is
  61. a next node pointer on the level, and P1 is its prefix.
  62. If a node pointer with a prefix P points to a non-leaf child,
  63. then the leftmost record in the child must have the same
  64. prefix P. If it points to a leaf node, the child is not required
  65. to contain any record with a prefix equal to P. The leaf case
  66. is decided this way to allow arbitrary deletions in a leaf node
  67. without touching upper levels of the tree.
  68. We have predefined a special minimum record which we
  69. define as the smallest record in any alphabetical order.
  70. A minimum record is denoted by setting a bit in the record
  71. header. A minimum record acts as the prefix of a node pointer
  72. which points to a leftmost node on any level of the tree.
  73. File page allocation
  74. --------------------
  75. In the root node of a B-tree there are two file segment headers.
  76. The leaf pages of a tree are allocated from one file segment, to
  77. make them consecutive on disk if possible. From the other file segment
  78. we allocate pages for the non-leaf levels of the tree.
  79. */
  80. #ifdef UNIV_BTR_DEBUG
  81. /**************************************************************//**
  82. Checks a file segment header within a B-tree root page.
  83. @return TRUE if valid */
  84. static
  85. ibool
  86. btr_root_fseg_validate(
  87. /*===================*/
  88. const fseg_header_t* seg_header, /*!< in: segment header */
  89. ulint space) /*!< in: tablespace identifier */
  90. {
  91. ulint offset = mach_read_from_2(seg_header + FSEG_HDR_OFFSET);
  92. ut_a(mach_read_from_4(seg_header + FSEG_HDR_SPACE) == space);
  93. ut_a(offset >= FIL_PAGE_DATA);
  94. ut_a(offset <= UNIV_PAGE_SIZE - FIL_PAGE_DATA_END);
  95. return(TRUE);
  96. }
  97. #endif /* UNIV_BTR_DEBUG */
  98. /**************************************************************//**
  99. Gets the root node of a tree and x-latches it.
  100. @return root page, x-latched */
  101. static
  102. buf_block_t*
  103. btr_root_block_get(
  104. /*===============*/
  105. dict_index_t* index, /*!< in: index tree */
  106. mtr_t* mtr) /*!< in: mtr */
  107. {
  108. ulint space;
  109. ulint zip_size;
  110. ulint root_page_no;
  111. buf_block_t* block;
  112. space = dict_index_get_space(index);
  113. zip_size = dict_table_zip_size(index->table);
  114. root_page_no = dict_index_get_page(index);
  115. block = btr_block_get(space, zip_size, root_page_no, RW_X_LATCH, mtr);
  116. if (srv_pass_corrupt_table && !block) {
  117. return(0);
  118. }
  119. ut_a(block);
  120. ut_a((ibool)!!page_is_comp(buf_block_get_frame(block))
  121. == dict_table_is_comp(index->table));
  122. #ifdef UNIV_BTR_DEBUG
  123. if (!dict_index_is_ibuf(index)) {
  124. const page_t* root = buf_block_get_frame(block);
  125. ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_LEAF
  126. + root, space));
  127. ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_TOP
  128. + root, space));
  129. }
  130. #endif /* UNIV_BTR_DEBUG */
  131. return(block);
  132. }
  133. /**************************************************************//**
  134. Gets the root node of a tree and x-latches it.
  135. @return root page, x-latched */
  136. UNIV_INTERN
  137. page_t*
  138. btr_root_get(
  139. /*=========*/
  140. dict_index_t* index, /*!< in: index tree */
  141. mtr_t* mtr) /*!< in: mtr */
  142. {
  143. return(buf_block_get_frame(btr_root_block_get(index, mtr)));
  144. }
  145. /*************************************************************//**
  146. Gets pointer to the previous user record in the tree. It is assumed that
  147. the caller has appropriate latches on the page and its neighbor.
  148. @return previous user record, NULL if there is none */
  149. UNIV_INTERN
  150. rec_t*
  151. btr_get_prev_user_rec(
  152. /*==================*/
  153. rec_t* rec, /*!< in: record on leaf level */
  154. mtr_t* mtr) /*!< in: mtr holding a latch on the page, and if
  155. needed, also to the previous page */
  156. {
  157. page_t* page;
  158. page_t* prev_page;
  159. ulint prev_page_no;
  160. if (!page_rec_is_infimum(rec)) {
  161. rec_t* prev_rec = page_rec_get_prev(rec);
  162. if (!page_rec_is_infimum(prev_rec)) {
  163. return(prev_rec);
  164. }
  165. }
  166. page = page_align(rec);
  167. prev_page_no = btr_page_get_prev(page, mtr);
  168. if (prev_page_no != FIL_NULL) {
  169. ulint space;
  170. ulint zip_size;
  171. buf_block_t* prev_block;
  172. space = page_get_space_id(page);
  173. zip_size = fil_space_get_zip_size(space);
  174. prev_block = buf_page_get_with_no_latch(space, zip_size,
  175. prev_page_no, mtr);
  176. prev_page = buf_block_get_frame(prev_block);
  177. /* The caller must already have a latch to the brother */
  178. ut_ad(mtr_memo_contains(mtr, prev_block,
  179. MTR_MEMO_PAGE_S_FIX)
  180. || mtr_memo_contains(mtr, prev_block,
  181. MTR_MEMO_PAGE_X_FIX));
  182. #ifdef UNIV_BTR_DEBUG
  183. ut_a(page_is_comp(prev_page) == page_is_comp(page));
  184. ut_a(btr_page_get_next(prev_page, mtr)
  185. == page_get_page_no(page));
  186. #endif /* UNIV_BTR_DEBUG */
  187. return(page_rec_get_prev(page_get_supremum_rec(prev_page)));
  188. }
  189. return(NULL);
  190. }
  191. /*************************************************************//**
  192. Gets pointer to the next user record in the tree. It is assumed that the
  193. caller has appropriate latches on the page and its neighbor.
  194. @return next user record, NULL if there is none */
  195. UNIV_INTERN
  196. rec_t*
  197. btr_get_next_user_rec(
  198. /*==================*/
  199. rec_t* rec, /*!< in: record on leaf level */
  200. mtr_t* mtr) /*!< in: mtr holding a latch on the page, and if
  201. needed, also to the next page */
  202. {
  203. page_t* page;
  204. page_t* next_page;
  205. ulint next_page_no;
  206. if (!page_rec_is_supremum(rec)) {
  207. rec_t* next_rec = page_rec_get_next(rec);
  208. if (!page_rec_is_supremum(next_rec)) {
  209. return(next_rec);
  210. }
  211. }
  212. page = page_align(rec);
  213. next_page_no = btr_page_get_next(page, mtr);
  214. if (next_page_no != FIL_NULL) {
  215. ulint space;
  216. ulint zip_size;
  217. buf_block_t* next_block;
  218. space = page_get_space_id(page);
  219. zip_size = fil_space_get_zip_size(space);
  220. next_block = buf_page_get_with_no_latch(space, zip_size,
  221. next_page_no, mtr);
  222. next_page = buf_block_get_frame(next_block);
  223. /* The caller must already have a latch to the brother */
  224. ut_ad(mtr_memo_contains(mtr, next_block, MTR_MEMO_PAGE_S_FIX)
  225. || mtr_memo_contains(mtr, next_block,
  226. MTR_MEMO_PAGE_X_FIX));
  227. #ifdef UNIV_BTR_DEBUG
  228. ut_a(page_is_comp(next_page) == page_is_comp(page));
  229. ut_a(btr_page_get_prev(next_page, mtr)
  230. == page_get_page_no(page));
  231. #endif /* UNIV_BTR_DEBUG */
  232. return(page_rec_get_next(page_get_infimum_rec(next_page)));
  233. }
  234. return(NULL);
  235. }
  236. /**************************************************************//**
  237. Creates a new index page (not the root, and also not
  238. used in page reorganization). @see btr_page_empty(). */
  239. static
  240. void
  241. btr_page_create(
  242. /*============*/
  243. buf_block_t* block, /*!< in/out: page to be created */
  244. page_zip_des_t* page_zip,/*!< in/out: compressed page, or NULL */
  245. dict_index_t* index, /*!< in: index */
  246. ulint level, /*!< in: the B-tree level of the page */
  247. mtr_t* mtr) /*!< in: mtr */
  248. {
  249. page_t* page = buf_block_get_frame(block);
  250. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  251. if (UNIV_LIKELY_NULL(page_zip)) {
  252. page_create_zip(block, index, level, mtr);
  253. } else {
  254. page_create(block, mtr, dict_table_is_comp(index->table));
  255. /* Set the level of the new index page */
  256. btr_page_set_level(page, NULL, level, mtr);
  257. }
  258. block->check_index_page_at_flush = TRUE;
  259. btr_page_set_index_id(page, page_zip, index->id, mtr);
  260. }
  261. /**************************************************************//**
  262. Allocates a new file page to be used in an ibuf tree. Takes the page from
  263. the free list of the tree, which must contain pages!
  264. @return new allocated block, x-latched */
  265. static
  266. buf_block_t*
  267. btr_page_alloc_for_ibuf(
  268. /*====================*/
  269. dict_index_t* index, /*!< in: index tree */
  270. mtr_t* mtr) /*!< in: mtr */
  271. {
  272. fil_addr_t node_addr;
  273. page_t* root;
  274. page_t* new_page;
  275. buf_block_t* new_block;
  276. root = btr_root_get(index, mtr);
  277. node_addr = flst_get_first(root + PAGE_HEADER
  278. + PAGE_BTR_IBUF_FREE_LIST, mtr);
  279. ut_a(node_addr.page != FIL_NULL);
  280. new_block = buf_page_get(dict_index_get_space(index),
  281. dict_table_zip_size(index->table),
  282. node_addr.page, RW_X_LATCH, mtr);
  283. new_page = buf_block_get_frame(new_block);
  284. buf_block_dbg_add_level(new_block, SYNC_TREE_NODE_NEW);
  285. flst_remove(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
  286. new_page + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST_NODE,
  287. mtr);
  288. ut_ad(flst_validate(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
  289. mtr));
  290. return(new_block);
  291. }
  292. /**************************************************************//**
  293. Allocates a new file page to be used in an index tree. NOTE: we assume
  294. that the caller has made the reservation for free extents!
  295. @return new allocated block, x-latched; NULL if out of space */
  296. UNIV_INTERN
  297. buf_block_t*
  298. btr_page_alloc(
  299. /*===========*/
  300. dict_index_t* index, /*!< in: index */
  301. ulint hint_page_no, /*!< in: hint of a good page */
  302. byte file_direction, /*!< in: direction where a possible
  303. page split is made */
  304. ulint level, /*!< in: level where the page is placed
  305. in the tree */
  306. mtr_t* mtr) /*!< in: mtr */
  307. {
  308. fseg_header_t* seg_header;
  309. page_t* root;
  310. buf_block_t* new_block;
  311. ulint new_page_no;
  312. if (dict_index_is_ibuf(index)) {
  313. return(btr_page_alloc_for_ibuf(index, mtr));
  314. }
  315. root = btr_root_get(index, mtr);
  316. if (level == 0) {
  317. seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
  318. } else {
  319. seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;
  320. }
  321. /* Parameter TRUE below states that the caller has made the
  322. reservation for free extents, and thus we know that a page can
  323. be allocated: */
  324. new_page_no = fseg_alloc_free_page_general(seg_header, hint_page_no,
  325. file_direction, TRUE, mtr);
  326. if (new_page_no == FIL_NULL) {
  327. return(NULL);
  328. }
  329. new_block = buf_page_get(dict_index_get_space(index),
  330. dict_table_zip_size(index->table),
  331. new_page_no, RW_X_LATCH, mtr);
  332. buf_block_dbg_add_level(new_block, SYNC_TREE_NODE_NEW);
  333. return(new_block);
  334. }
  335. /**************************************************************//**
  336. Gets the number of pages in a B-tree.
  337. @return number of pages */
  338. UNIV_INTERN
  339. ulint
  340. btr_get_size(
  341. /*=========*/
  342. dict_index_t* index, /*!< in: index */
  343. ulint flag) /*!< in: BTR_N_LEAF_PAGES or BTR_TOTAL_SIZE */
  344. {
  345. fseg_header_t* seg_header;
  346. page_t* root;
  347. ulint n;
  348. ulint dummy;
  349. mtr_t mtr;
  350. mtr_start(&mtr);
  351. mtr_s_lock(dict_index_get_lock(index), &mtr);
  352. root = btr_root_get(index, &mtr);
  353. if (srv_pass_corrupt_table && !root) {
  354. mtr_commit(&mtr);
  355. return(0);
  356. }
  357. ut_a(root);
  358. if (flag == BTR_N_LEAF_PAGES) {
  359. seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
  360. fseg_n_reserved_pages(seg_header, &n, &mtr);
  361. } else if (flag == BTR_TOTAL_SIZE) {
  362. seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;
  363. n = fseg_n_reserved_pages(seg_header, &dummy, &mtr);
  364. seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
  365. n += fseg_n_reserved_pages(seg_header, &dummy, &mtr);
  366. } else {
  367. ut_error;
  368. }
  369. mtr_commit(&mtr);
  370. return(n);
  371. }
  372. /**************************************************************//**
  373. Frees a page used in an ibuf tree. Puts the page to the free list of the
  374. ibuf tree. */
  375. static
  376. void
  377. btr_page_free_for_ibuf(
  378. /*===================*/
  379. dict_index_t* index, /*!< in: index tree */
  380. buf_block_t* block, /*!< in: block to be freed, x-latched */
  381. mtr_t* mtr) /*!< in: mtr */
  382. {
  383. page_t* root;
  384. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  385. root = btr_root_get(index, mtr);
  386. flst_add_first(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
  387. buf_block_get_frame(block)
  388. + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST_NODE, mtr);
  389. ut_ad(flst_validate(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
  390. mtr));
  391. }
  392. /**************************************************************//**
  393. Frees a file page used in an index tree. Can be used also to (BLOB)
  394. external storage pages, because the page level 0 can be given as an
  395. argument. */
  396. UNIV_INTERN
  397. void
  398. btr_page_free_low(
  399. /*==============*/
  400. dict_index_t* index, /*!< in: index tree */
  401. buf_block_t* block, /*!< in: block to be freed, x-latched */
  402. ulint level, /*!< in: page level */
  403. mtr_t* mtr) /*!< in: mtr */
  404. {
  405. fseg_header_t* seg_header;
  406. page_t* root;
  407. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  408. /* The page gets invalid for optimistic searches: increment the frame
  409. modify clock */
  410. buf_block_modify_clock_inc(block);
  411. if (dict_index_is_ibuf(index)) {
  412. btr_page_free_for_ibuf(index, block, mtr);
  413. return;
  414. }
  415. root = btr_root_get(index, mtr);
  416. if (level == 0) {
  417. seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
  418. } else {
  419. seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;
  420. }
  421. fseg_free_page(seg_header,
  422. buf_block_get_space(block),
  423. buf_block_get_page_no(block), mtr);
  424. }
  425. /**************************************************************//**
  426. Frees a file page used in an index tree. NOTE: cannot free field external
  427. storage pages because the page must contain info on its level. */
  428. UNIV_INTERN
  429. void
  430. btr_page_free(
  431. /*==========*/
  432. dict_index_t* index, /*!< in: index tree */
  433. buf_block_t* block, /*!< in: block to be freed, x-latched */
  434. mtr_t* mtr) /*!< in: mtr */
  435. {
  436. ulint level;
  437. level = btr_page_get_level(buf_block_get_frame(block), mtr);
  438. btr_page_free_low(index, block, level, mtr);
  439. }
  440. /**************************************************************//**
  441. Sets the child node file address in a node pointer. */
  442. UNIV_INLINE
  443. void
  444. btr_node_ptr_set_child_page_no(
  445. /*===========================*/
  446. rec_t* rec, /*!< in: node pointer record */
  447. page_zip_des_t* page_zip,/*!< in/out: compressed page whose uncompressed
  448. part will be updated, or NULL */
  449. const ulint* offsets,/*!< in: array returned by rec_get_offsets() */
  450. ulint page_no,/*!< in: child node address */
  451. mtr_t* mtr) /*!< in: mtr */
  452. {
  453. byte* field;
  454. ulint len;
  455. ut_ad(rec_offs_validate(rec, NULL, offsets));
  456. ut_ad(!page_is_leaf(page_align(rec)));
  457. ut_ad(!rec_offs_comp(offsets) || rec_get_node_ptr_flag(rec));
  458. /* The child address is in the last field */
  459. field = rec_get_nth_field(rec, offsets,
  460. rec_offs_n_fields(offsets) - 1, &len);
  461. ut_ad(len == REC_NODE_PTR_SIZE);
  462. if (UNIV_LIKELY_NULL(page_zip)) {
  463. page_zip_write_node_ptr(page_zip, rec,
  464. rec_offs_data_size(offsets),
  465. page_no, mtr);
  466. } else {
  467. mlog_write_ulint(field, page_no, MLOG_4BYTES, mtr);
  468. }
  469. }
  470. /************************************************************//**
  471. Returns the child page of a node pointer and x-latches it.
  472. @return child page, x-latched */
  473. static
  474. buf_block_t*
  475. btr_node_ptr_get_child(
  476. /*===================*/
  477. const rec_t* node_ptr,/*!< in: node pointer */
  478. dict_index_t* index, /*!< in: index */
  479. const ulint* offsets,/*!< in: array returned by rec_get_offsets() */
  480. mtr_t* mtr) /*!< in: mtr */
  481. {
  482. ulint page_no;
  483. ulint space;
  484. ut_ad(rec_offs_validate(node_ptr, index, offsets));
  485. space = page_get_space_id(page_align(node_ptr));
  486. page_no = btr_node_ptr_get_child_page_no(node_ptr, offsets);
  487. return(btr_block_get(space, dict_table_zip_size(index->table),
  488. page_no, RW_X_LATCH, mtr));
  489. }
  490. /************************************************************//**
  491. Returns the upper level node pointer to a page. It is assumed that mtr holds
  492. an x-latch on the tree.
  493. @return rec_get_offsets() of the node pointer record */
  494. static
  495. ulint*
  496. btr_page_get_father_node_ptr(
  497. /*=========================*/
  498. ulint* offsets,/*!< in: work area for the return value */
  499. mem_heap_t* heap, /*!< in: memory heap to use */
  500. btr_cur_t* cursor, /*!< in: cursor pointing to user record,
  501. out: cursor on node pointer record,
  502. its page x-latched */
  503. mtr_t* mtr) /*!< in: mtr */
  504. {
  505. dtuple_t* tuple;
  506. rec_t* user_rec;
  507. rec_t* node_ptr;
  508. ulint level;
  509. ulint page_no;
  510. dict_index_t* index;
  511. page_no = buf_block_get_page_no(btr_cur_get_block(cursor));
  512. index = btr_cur_get_index(cursor);
  513. ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index),
  514. MTR_MEMO_X_LOCK));
  515. ut_ad(dict_index_get_page(index) != page_no);
  516. level = btr_page_get_level(btr_cur_get_page(cursor), mtr);
  517. user_rec = btr_cur_get_rec(cursor);
  518. ut_a(page_rec_is_user_rec(user_rec));
  519. tuple = dict_index_build_node_ptr(index, user_rec, 0, heap, level);
  520. btr_cur_search_to_nth_level(index, level + 1, tuple, PAGE_CUR_LE,
  521. BTR_CONT_MODIFY_TREE, cursor, 0, mtr);
  522. node_ptr = btr_cur_get_rec(cursor);
  523. ut_ad(!page_rec_is_comp(node_ptr)
  524. || rec_get_status(node_ptr) == REC_STATUS_NODE_PTR);
  525. offsets = rec_get_offsets(node_ptr, index, offsets,
  526. ULINT_UNDEFINED, &heap);
  527. if (UNIV_UNLIKELY(btr_node_ptr_get_child_page_no(node_ptr, offsets)
  528. != page_no)) {
  529. rec_t* print_rec;
  530. fputs("InnoDB: Dump of the child page:\n", stderr);
  531. buf_page_print(page_align(user_rec), 0);
  532. fputs("InnoDB: Dump of the parent page:\n", stderr);
  533. buf_page_print(page_align(node_ptr), 0);
  534. fputs("InnoDB: Corruption of an index tree: table ", stderr);
  535. ut_print_name(stderr, NULL, TRUE, index->table_name);
  536. fputs(", index ", stderr);
  537. ut_print_name(stderr, NULL, FALSE, index->name);
  538. fprintf(stderr, ",\n"
  539. "InnoDB: father ptr page no %lu, child page no %lu\n",
  540. (ulong)
  541. btr_node_ptr_get_child_page_no(node_ptr, offsets),
  542. (ulong) page_no);
  543. print_rec = page_rec_get_next(
  544. page_get_infimum_rec(page_align(user_rec)));
  545. offsets = rec_get_offsets(print_rec, index,
  546. offsets, ULINT_UNDEFINED, &heap);
  547. page_rec_print(print_rec, offsets);
  548. offsets = rec_get_offsets(node_ptr, index, offsets,
  549. ULINT_UNDEFINED, &heap);
  550. page_rec_print(node_ptr, offsets);
  551. fputs("InnoDB: You should dump + drop + reimport the table"
  552. " to fix the\n"
  553. "InnoDB: corruption. If the crash happens at "
  554. "the database startup, see\n"
  555. "InnoDB: " REFMAN "forcing-recovery.html about\n"
  556. "InnoDB: forcing recovery. "
  557. "Then dump + drop + reimport.\n", stderr);
  558. ut_error;
  559. }
  560. return(offsets);
  561. }
  562. /************************************************************//**
  563. Returns the upper level node pointer to a page. It is assumed that mtr holds
  564. an x-latch on the tree.
  565. @return rec_get_offsets() of the node pointer record */
  566. static
  567. ulint*
  568. btr_page_get_father_block(
  569. /*======================*/
  570. ulint* offsets,/*!< in: work area for the return value */
  571. mem_heap_t* heap, /*!< in: memory heap to use */
  572. dict_index_t* index, /*!< in: b-tree index */
  573. buf_block_t* block, /*!< in: child page in the index */
  574. mtr_t* mtr, /*!< in: mtr */
  575. btr_cur_t* cursor) /*!< out: cursor on node pointer record,
  576. its page x-latched */
  577. {
  578. rec_t* rec
  579. = page_rec_get_next(page_get_infimum_rec(buf_block_get_frame(
  580. block)));
  581. btr_cur_position(index, rec, block, cursor);
  582. return(btr_page_get_father_node_ptr(offsets, heap, cursor, mtr));
  583. }
  584. /************************************************************//**
  585. Seeks to the upper level node pointer to a page.
  586. It is assumed that mtr holds an x-latch on the tree. */
  587. static
  588. void
  589. btr_page_get_father(
  590. /*================*/
  591. dict_index_t* index, /*!< in: b-tree index */
  592. buf_block_t* block, /*!< in: child page in the index */
  593. mtr_t* mtr, /*!< in: mtr */
  594. btr_cur_t* cursor) /*!< out: cursor on node pointer record,
  595. its page x-latched */
  596. {
  597. mem_heap_t* heap;
  598. rec_t* rec
  599. = page_rec_get_next(page_get_infimum_rec(buf_block_get_frame(
  600. block)));
  601. btr_cur_position(index, rec, block, cursor);
  602. heap = mem_heap_create(100);
  603. btr_page_get_father_node_ptr(NULL, heap, cursor, mtr);
  604. mem_heap_free(heap);
  605. }
  606. /************************************************************//**
  607. Creates the root node for a new index tree.
  608. @return page number of the created root, FIL_NULL if did not succeed */
  609. UNIV_INTERN
  610. ulint
  611. btr_create(
  612. /*=======*/
  613. ulint type, /*!< in: type of the index */
  614. ulint space, /*!< in: space where created */
  615. ulint zip_size,/*!< in: compressed page size in bytes
  616. or 0 for uncompressed pages */
  617. dulint index_id,/*!< in: index id */
  618. dict_index_t* index, /*!< in: index */
  619. mtr_t* mtr) /*!< in: mini-transaction handle */
  620. {
  621. ulint page_no;
  622. buf_block_t* block;
  623. buf_frame_t* frame;
  624. page_t* page;
  625. page_zip_des_t* page_zip;
  626. /* Create the two new segments (one, in the case of an ibuf tree) for
  627. the index tree; the segment headers are put on the allocated root page
  628. (for an ibuf tree, not in the root, but on a separate ibuf header
  629. page) */
  630. if (type & DICT_IBUF) {
  631. /* Allocate first the ibuf header page */
  632. buf_block_t* ibuf_hdr_block = fseg_create(
  633. space, 0,
  634. IBUF_HEADER + IBUF_TREE_SEG_HEADER, mtr);
  635. buf_block_dbg_add_level(ibuf_hdr_block, SYNC_TREE_NODE_NEW);
  636. ut_ad(buf_block_get_page_no(ibuf_hdr_block)
  637. == IBUF_HEADER_PAGE_NO);
  638. /* Allocate then the next page to the segment: it will be the
  639. tree root page */
  640. page_no = fseg_alloc_free_page(buf_block_get_frame(
  641. ibuf_hdr_block)
  642. + IBUF_HEADER
  643. + IBUF_TREE_SEG_HEADER,
  644. IBUF_TREE_ROOT_PAGE_NO,
  645. FSP_UP, mtr);
  646. ut_ad(page_no == IBUF_TREE_ROOT_PAGE_NO);
  647. block = buf_page_get(space, zip_size, page_no,
  648. RW_X_LATCH, mtr);
  649. } else {
  650. block = fseg_create(space, 0,
  651. PAGE_HEADER + PAGE_BTR_SEG_TOP, mtr);
  652. }
  653. if (block == NULL) {
  654. return(FIL_NULL);
  655. }
  656. page_no = buf_block_get_page_no(block);
  657. frame = buf_block_get_frame(block);
  658. buf_block_dbg_add_level(block, SYNC_TREE_NODE_NEW);
  659. if (type & DICT_IBUF) {
  660. /* It is an insert buffer tree: initialize the free list */
  661. ut_ad(page_no == IBUF_TREE_ROOT_PAGE_NO);
  662. flst_init(frame + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST, mtr);
  663. } else {
  664. /* It is a non-ibuf tree: create a file segment for leaf
  665. pages */
  666. if (!fseg_create(space, page_no,
  667. PAGE_HEADER + PAGE_BTR_SEG_LEAF, mtr)) {
  668. /* Not enough space for new segment, free root
  669. segment before return. */
  670. btr_free_root(space, zip_size, page_no, mtr);
  671. return(FIL_NULL);
  672. }
  673. /* The fseg create acquires a second latch on the page,
  674. therefore we must declare it: */
  675. buf_block_dbg_add_level(block, SYNC_TREE_NODE_NEW);
  676. }
  677. /* Create a new index page on the allocated segment page */
  678. page_zip = buf_block_get_page_zip(block);
  679. if (UNIV_LIKELY_NULL(page_zip)) {
  680. page = page_create_zip(block, index, 0, mtr);
  681. } else {
  682. page = page_create(block, mtr,
  683. dict_table_is_comp(index->table));
  684. /* Set the level of the new index page */
  685. btr_page_set_level(page, NULL, 0, mtr);
  686. }
  687. block->check_index_page_at_flush = TRUE;
  688. /* Set the index id of the page */
  689. btr_page_set_index_id(page, page_zip, index_id, mtr);
  690. /* Set the next node and previous node fields */
  691. btr_page_set_next(page, page_zip, FIL_NULL, mtr);
  692. btr_page_set_prev(page, page_zip, FIL_NULL, mtr);
  693. /* We reset the free bits for the page to allow creation of several
  694. trees in the same mtr, otherwise the latch on a bitmap page would
  695. prevent it because of the latching order */
  696. if (!(type & DICT_CLUSTERED)) {
  697. ibuf_reset_free_bits(block);
  698. }
  699. /* In the following assertion we test that two records of maximum
  700. allowed size fit on the root page: this fact is needed to ensure
  701. correctness of split algorithms */
  702. ut_ad(page_get_max_insert_size(page, 2) > 2 * BTR_PAGE_MAX_REC_SIZE);
  703. return(page_no);
  704. }
  705. /************************************************************//**
  706. Frees a B-tree except the root page, which MUST be freed after this
  707. by calling btr_free_root. */
  708. UNIV_INTERN
  709. void
  710. btr_free_but_not_root(
  711. /*==================*/
  712. ulint space, /*!< in: space where created */
  713. ulint zip_size, /*!< in: compressed page size in bytes
  714. or 0 for uncompressed pages */
  715. ulint root_page_no) /*!< in: root page number */
  716. {
  717. ibool finished;
  718. page_t* root;
  719. mtr_t mtr;
  720. leaf_loop:
  721. mtr_start(&mtr);
  722. root = btr_page_get(space, zip_size, root_page_no, RW_X_LATCH, &mtr);
  723. if (srv_pass_corrupt_table && !root) {
  724. mtr_commit(&mtr);
  725. return;
  726. }
  727. ut_a(root);
  728. #ifdef UNIV_BTR_DEBUG
  729. ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_LEAF
  730. + root, space));
  731. ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_TOP
  732. + root, space));
  733. #endif /* UNIV_BTR_DEBUG */
  734. /* NOTE: page hash indexes are dropped when a page is freed inside
  735. fsp0fsp. */
  736. finished = fseg_free_step(root + PAGE_HEADER + PAGE_BTR_SEG_LEAF,
  737. &mtr);
  738. mtr_commit(&mtr);
  739. if (!finished) {
  740. goto leaf_loop;
  741. }
  742. top_loop:
  743. mtr_start(&mtr);
  744. root = btr_page_get(space, zip_size, root_page_no, RW_X_LATCH, &mtr);
  745. if (srv_pass_corrupt_table && !root) {
  746. mtr_commit(&mtr);
  747. return;
  748. }
  749. ut_a(root);
  750. #ifdef UNIV_BTR_DEBUG
  751. ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_TOP
  752. + root, space));
  753. #endif /* UNIV_BTR_DEBUG */
  754. finished = fseg_free_step_not_header(
  755. root + PAGE_HEADER + PAGE_BTR_SEG_TOP, &mtr);
  756. mtr_commit(&mtr);
  757. if (!finished) {
  758. goto top_loop;
  759. }
  760. }
  761. /************************************************************//**
  762. Frees the B-tree root page. Other tree MUST already have been freed. */
  763. UNIV_INTERN
  764. void
  765. btr_free_root(
  766. /*==========*/
  767. ulint space, /*!< in: space where created */
  768. ulint zip_size, /*!< in: compressed page size in bytes
  769. or 0 for uncompressed pages */
  770. ulint root_page_no, /*!< in: root page number */
  771. mtr_t* mtr) /*!< in: a mini-transaction which has already
  772. been started */
  773. {
  774. buf_block_t* block;
  775. fseg_header_t* header;
  776. block = btr_block_get(space, zip_size, root_page_no, RW_X_LATCH, mtr);
  777. if (srv_pass_corrupt_table && !block) {
  778. return;
  779. }
  780. ut_a(block);
  781. btr_search_drop_page_hash_index(block);
  782. header = buf_block_get_frame(block) + PAGE_HEADER + PAGE_BTR_SEG_TOP;
  783. #ifdef UNIV_BTR_DEBUG
  784. ut_a(btr_root_fseg_validate(header, space));
  785. #endif /* UNIV_BTR_DEBUG */
  786. while (!fseg_free_step(header, mtr));
  787. }
  788. #endif /* !UNIV_HOTBACKUP */
  789. /*************************************************************//**
  790. Reorganizes an index page. */
  791. static
  792. ibool
  793. btr_page_reorganize_low(
  794. /*====================*/
  795. ibool recovery,/*!< in: TRUE if called in recovery:
  796. locks should not be updated, i.e.,
  797. there cannot exist locks on the
  798. page, and a hash index should not be
  799. dropped: it cannot exist */
  800. buf_block_t* block, /*!< in: page to be reorganized */
  801. dict_index_t* index, /*!< in: record descriptor */
  802. mtr_t* mtr) /*!< in: mtr */
  803. {
  804. page_t* page = buf_block_get_frame(block);
  805. page_zip_des_t* page_zip = buf_block_get_page_zip(block);
  806. buf_block_t* temp_block;
  807. page_t* temp_page;
  808. ulint log_mode;
  809. ulint data_size1;
  810. ulint data_size2;
  811. ulint max_ins_size1;
  812. ulint max_ins_size2;
  813. ibool success = FALSE;
  814. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  815. ut_ad(!!page_is_comp(page) == dict_table_is_comp(index->table));
  816. #ifdef UNIV_ZIP_DEBUG
  817. ut_a(!page_zip || page_zip_validate(page_zip, page));
  818. #endif /* UNIV_ZIP_DEBUG */
  819. data_size1 = page_get_data_size(page);
  820. max_ins_size1 = page_get_max_insert_size_after_reorganize(page, 1);
  821. #ifndef UNIV_HOTBACKUP
  822. /* Write the log record */
  823. mlog_open_and_write_index(mtr, page, index, page_is_comp(page)
  824. ? MLOG_COMP_PAGE_REORGANIZE
  825. : MLOG_PAGE_REORGANIZE, 0);
  826. #endif /* !UNIV_HOTBACKUP */
  827. /* Turn logging off */
  828. log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);
  829. #ifndef UNIV_HOTBACKUP
  830. temp_block = buf_block_alloc(0);
  831. #else /* !UNIV_HOTBACKUP */
  832. ut_ad(block == back_block1);
  833. temp_block = back_block2;
  834. #endif /* !UNIV_HOTBACKUP */
  835. temp_page = temp_block->frame;
  836. /* Copy the old page to temporary space */
  837. buf_frame_copy(temp_page, page);
  838. #ifndef UNIV_HOTBACKUP
  839. if (UNIV_LIKELY(!recovery)) {
  840. btr_search_drop_page_hash_index(block);
  841. }
  842. block->check_index_page_at_flush = TRUE;
  843. #endif /* !UNIV_HOTBACKUP */
  844. /* Recreate the page: note that global data on page (possible
  845. segment headers, next page-field, etc.) is preserved intact */
  846. page_create(block, mtr, dict_table_is_comp(index->table));
  847. /* Copy the records from the temporary space to the recreated page;
  848. do not copy the lock bits yet */
  849. page_copy_rec_list_end_no_locks(block, temp_block,
  850. page_get_infimum_rec(temp_page),
  851. index, mtr);
  852. if (dict_index_is_sec_or_ibuf(index) && page_is_leaf(page)) {
  853. /* Copy max trx id to recreated page */
  854. trx_id_t max_trx_id = page_get_max_trx_id(temp_page);
  855. page_set_max_trx_id(block, NULL, max_trx_id, mtr);
  856. /* In crash recovery, dict_index_is_sec_or_ibuf() always
  857. returns TRUE, even for clustered indexes. max_trx_id is
  858. unused in clustered index pages. */
  859. ut_ad(!ut_dulint_is_zero(max_trx_id) || recovery);
  860. }
  861. if (UNIV_LIKELY_NULL(page_zip)
  862. && UNIV_UNLIKELY
  863. (!page_zip_compress(page_zip, page, index, NULL))) {
  864. /* Restore the old page and exit. */
  865. #if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
  866. /* Check that the bytes that we skip are identical. */
  867. ut_a(!memcmp(page, temp_page, PAGE_HEADER));
  868. ut_a(!memcmp(PAGE_HEADER + PAGE_N_RECS + page,
  869. PAGE_HEADER + PAGE_N_RECS + temp_page,
  870. PAGE_DATA - (PAGE_HEADER + PAGE_N_RECS)));
  871. ut_a(!memcmp(UNIV_PAGE_SIZE - FIL_PAGE_DATA_END + page,
  872. UNIV_PAGE_SIZE - FIL_PAGE_DATA_END + temp_page,
  873. FIL_PAGE_DATA_END));
  874. #endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
  875. memcpy(PAGE_HEADER + page, PAGE_HEADER + temp_page,
  876. PAGE_N_RECS - PAGE_N_DIR_SLOTS);
  877. memcpy(PAGE_DATA + page, PAGE_DATA + temp_page,
  878. UNIV_PAGE_SIZE - PAGE_DATA - FIL_PAGE_DATA_END);
  879. #if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
  880. ut_a(!memcmp(page, temp_page, UNIV_PAGE_SIZE));
  881. #endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
  882. goto func_exit;
  883. }
  884. #ifndef UNIV_HOTBACKUP
  885. if (UNIV_LIKELY(!recovery)) {
  886. /* Update the record lock bitmaps */
  887. lock_move_reorganize_page(block, temp_block);
  888. }
  889. #endif /* !UNIV_HOTBACKUP */
  890. data_size2 = page_get_data_size(page);
  891. max_ins_size2 = page_get_max_insert_size_after_reorganize(page, 1);
  892. if (UNIV_UNLIKELY(data_size1 != data_size2)
  893. || UNIV_UNLIKELY(max_ins_size1 != max_ins_size2)) {
  894. buf_page_print(page, 0);
  895. buf_page_print(temp_page, 0);
  896. fprintf(stderr,
  897. "InnoDB: Error: page old data size %lu"
  898. " new data size %lu\n"
  899. "InnoDB: Error: page old max ins size %lu"
  900. " new max ins size %lu\n"
  901. "InnoDB: Submit a detailed bug report"
  902. " to http://bugs.mysql.com\n",
  903. (unsigned long) data_size1, (unsigned long) data_size2,
  904. (unsigned long) max_ins_size1,
  905. (unsigned long) max_ins_size2);
  906. } else {
  907. success = TRUE;
  908. }
  909. func_exit:
  910. #ifdef UNIV_ZIP_DEBUG
  911. ut_a(!page_zip || page_zip_validate(page_zip, page));
  912. #endif /* UNIV_ZIP_DEBUG */
  913. #ifndef UNIV_HOTBACKUP
  914. buf_block_free(temp_block);
  915. #endif /* !UNIV_HOTBACKUP */
  916. /* Restore logging mode */
  917. mtr_set_log_mode(mtr, log_mode);
  918. return(success);
  919. }
  920. #ifndef UNIV_HOTBACKUP
  921. /*************************************************************//**
  922. Reorganizes an index page.
  923. IMPORTANT: if btr_page_reorganize() is invoked on a compressed leaf
  924. page of a non-clustered index, the caller must update the insert
  925. buffer free bits in the same mini-transaction in such a way that the
  926. modification will be redo-logged.
  927. @return TRUE on success, FALSE on failure */
  928. UNIV_INTERN
  929. ibool
  930. btr_page_reorganize(
  931. /*================*/
  932. buf_block_t* block, /*!< in: page to be reorganized */
  933. dict_index_t* index, /*!< in: record descriptor */
  934. mtr_t* mtr) /*!< in: mtr */
  935. {
  936. return(btr_page_reorganize_low(FALSE, block, index, mtr));
  937. }
  938. #endif /* !UNIV_HOTBACKUP */
  939. /***********************************************************//**
  940. Parses a redo log record of reorganizing a page.
  941. @return end of log record or NULL */
  942. UNIV_INTERN
  943. byte*
  944. btr_parse_page_reorganize(
  945. /*======================*/
  946. byte* ptr, /*!< in: buffer */
  947. byte* end_ptr __attribute__((unused)),
  948. /*!< in: buffer end */
  949. dict_index_t* index, /*!< in: record descriptor */
  950. buf_block_t* block, /*!< in: page to be reorganized, or NULL */
  951. mtr_t* mtr) /*!< in: mtr or NULL */
  952. {
  953. ut_ad(ptr && end_ptr);
  954. /* The record is empty, except for the record initial part */
  955. if (UNIV_LIKELY(block != NULL)) {
  956. btr_page_reorganize_low(TRUE, block, index, mtr);
  957. }
  958. return(ptr);
  959. }
  960. #ifndef UNIV_HOTBACKUP
  961. /*************************************************************//**
  962. Empties an index page. @see btr_page_create(). */
  963. static
  964. void
  965. btr_page_empty(
  966. /*===========*/
  967. buf_block_t* block, /*!< in: page to be emptied */
  968. page_zip_des_t* page_zip,/*!< out: compressed page, or NULL */
  969. dict_index_t* index, /*!< in: index of the page */
  970. ulint level, /*!< in: the B-tree level of the page */
  971. mtr_t* mtr) /*!< in: mtr */
  972. {
  973. page_t* page = buf_block_get_frame(block);
  974. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  975. ut_ad(page_zip == buf_block_get_page_zip(block));
  976. #ifdef UNIV_ZIP_DEBUG
  977. ut_a(!page_zip || page_zip_validate(page_zip, page));
  978. #endif /* UNIV_ZIP_DEBUG */
  979. btr_search_drop_page_hash_index(block);
  980. /* Recreate the page: note that global data on page (possible
  981. segment headers, next page-field, etc.) is preserved intact */
  982. if (UNIV_LIKELY_NULL(page_zip)) {
  983. page_create_zip(block, index, level, mtr);
  984. } else {
  985. page_create(block, mtr, dict_table_is_comp(index->table));
  986. btr_page_set_level(page, NULL, level, mtr);
  987. }
  988. block->check_index_page_at_flush = TRUE;
  989. }
  990. /*************************************************************//**
  991. Makes tree one level higher by splitting the root, and inserts
  992. the tuple. It is assumed that mtr contains an x-latch on the tree.
  993. NOTE that the operation of this function must always succeed,
  994. we cannot reverse it: therefore enough free disk space must be
  995. guaranteed to be available before this function is called.
  996. @return inserted record */
  997. UNIV_INTERN
  998. rec_t*
  999. btr_root_raise_and_insert(
  1000. /*======================*/
  1001. btr_cur_t* cursor, /*!< in: cursor at which to insert: must be
  1002. on the root page; when the function returns,
  1003. the cursor is positioned on the predecessor
  1004. of the inserted record */
  1005. const dtuple_t* tuple, /*!< in: tuple to insert */
  1006. ulint n_ext, /*!< in: number of externally stored columns */
  1007. mtr_t* mtr) /*!< in: mtr */
  1008. {
  1009. dict_index_t* index;
  1010. page_t* root;
  1011. page_t* new_page;
  1012. ulint new_page_no;
  1013. rec_t* rec;
  1014. mem_heap_t* heap;
  1015. dtuple_t* node_ptr;
  1016. ulint level;
  1017. rec_t* node_ptr_rec;
  1018. page_cur_t* page_cursor;
  1019. page_zip_des_t* root_page_zip;
  1020. page_zip_des_t* new_page_zip;
  1021. buf_block_t* root_block;
  1022. buf_block_t* new_block;
  1023. root = btr_cur_get_page(cursor);
  1024. root_block = btr_cur_get_block(cursor);
  1025. root_page_zip = buf_block_get_page_zip(root_block);
  1026. #ifdef UNIV_ZIP_DEBUG
  1027. ut_a(!root_page_zip || page_zip_validate(root_page_zip, root));
  1028. #endif /* UNIV_ZIP_DEBUG */
  1029. index = btr_cur_get_index(cursor);
  1030. #ifdef UNIV_BTR_DEBUG
  1031. if (!dict_index_is_ibuf(index)) {
  1032. ulint space = dict_index_get_space(index);
  1033. ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_LEAF
  1034. + root, space));
  1035. ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_TOP
  1036. + root, space));
  1037. }
  1038. ut_a(dict_index_get_page(index) == page_get_page_no(root));
  1039. #endif /* UNIV_BTR_DEBUG */
  1040. ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index),
  1041. MTR_MEMO_X_LOCK));
  1042. ut_ad(mtr_memo_contains(mtr, root_block, MTR_MEMO_PAGE_X_FIX));
  1043. /* Allocate a new page to the tree. Root splitting is done by first
  1044. moving the root records to the new page, emptying the root, putting
  1045. a node pointer to the new page, and then splitting the new page. */
  1046. level = btr_page_get_level(root, mtr);
  1047. new_block = btr_page_alloc(index, 0, FSP_NO_DIR, level, mtr);
  1048. new_page = buf_block_get_frame(new_block);
  1049. new_page_zip = buf_block_get_page_zip(new_block);
  1050. ut_a(!new_page_zip == !root_page_zip);
  1051. ut_a(!new_page_zip
  1052. || page_zip_get_size(new_page_zip)
  1053. == page_zip_get_size(root_page_zip));
  1054. btr_page_create(new_block, new_page_zip, index, level, mtr);
  1055. /* Set the next node and previous node fields of new page */
  1056. btr_page_set_next(new_page, new_page_zip, FIL_NULL, mtr);
  1057. btr_page_set_prev(new_page, new_page_zip, FIL_NULL, mtr);
  1058. /* Copy the records from root to the new page one by one. */
  1059. if (0
  1060. #ifdef UNIV_ZIP_COPY
  1061. || new_page_zip
  1062. #endif /* UNIV_ZIP_COPY */
  1063. || UNIV_UNLIKELY
  1064. (!page_copy_rec_list_end(new_block, root_block,
  1065. page_get_infimum_rec(root),
  1066. index, mtr))) {
  1067. ut_a(new_page_zip);
  1068. /* Copy the page byte for byte. */
  1069. page_zip_copy_recs(new_page_zip, new_page,
  1070. root_page_zip, root, index, mtr);
  1071. /* Update the lock table and possible hash index. */
  1072. lock_move_rec_list_end(new_block, root_block,
  1073. page_get_infimum_rec(root));
  1074. btr_search_move_or_delete_hash_entries(new_block, root_block,
  1075. index);
  1076. }
  1077. /* If this is a pessimistic insert which is actually done to
  1078. perform a pessimistic update then we have stored the lock
  1079. information of the record to be inserted on the infimum of the
  1080. root page: we cannot discard the lock structs on the root page */
  1081. lock_update_root_raise(new_block, root_block);
  1082. /* Create a memory heap where the node pointer is stored */
  1083. heap = mem_heap_create(100);
  1084. rec = page_rec_get_next(page_get_infimum_rec(new_page));
  1085. new_page_no = buf_block_get_page_no(new_block);
  1086. /* Build the node pointer (= node key and page address) for the
  1087. child */
  1088. node_ptr = dict_index_build_node_ptr(index, rec, new_page_no, heap,
  1089. level);
  1090. /* The node pointer must be marked as the predefined minimum record,
  1091. as there is no lower alphabetical limit to records in the leftmost
  1092. node of a level: */
  1093. dtuple_set_info_bits(node_ptr,
  1094. dtuple_get_info_bits(node_ptr)
  1095. | REC_INFO_MIN_REC_FLAG);
  1096. /* Rebuild the root page to get free space */
  1097. btr_page_empty(root_block, root_page_zip, index, level + 1, mtr);
  1098. /* Set the next node and previous node fields, although
  1099. they should already have been set. The previous node field
  1100. must be FIL_NULL if root_page_zip != NULL, because the
  1101. REC_INFO_MIN_REC_FLAG (of the first user record) will be
  1102. set if and only if btr_page_get_prev() == FIL_NULL. */
  1103. btr_page_set_next(root, root_page_zip, FIL_NULL, mtr);
  1104. btr_page_set_prev(root, root_page_zip, FIL_NULL, mtr);
  1105. page_cursor = btr_cur_get_page_cur(cursor);
  1106. /* Insert node pointer to the root */
  1107. page_cur_set_before_first(root_block, page_cursor);
  1108. node_ptr_rec = page_cur_tuple_insert(page_cursor, node_ptr,
  1109. index, 0, mtr);
  1110. /* The root page should only contain the node pointer
  1111. to new_page at this point. Thus, the data should fit. */
  1112. ut_a(node_ptr_rec);
  1113. /* Free the memory heap */
  1114. mem_heap_free(heap);
  1115. /* We play safe and reset the free bits for the new page */
  1116. #if 0
  1117. fprintf(stderr, "Root raise new page no %lu\n", new_page_no);
  1118. #endif
  1119. if (!dict_index_is_clust(index)) {
  1120. ibuf_reset_free_bits(new_block);
  1121. }
  1122. /* Reposition the cursor to the child node */
  1123. page_cur_search(new_block, index, tuple,
  1124. PAGE_CUR_LE, page_cursor);
  1125. /* Split the child and insert tuple */
  1126. return(btr_page_split_and_insert(cursor, tuple, n_ext, mtr));
  1127. }
  1128. /*************************************************************//**
  1129. Decides if the page should be split at the convergence point of inserts
  1130. converging to the left.
  1131. @return TRUE if split recommended */
  1132. UNIV_INTERN
  1133. ibool
  1134. btr_page_get_split_rec_to_left(
  1135. /*===========================*/
  1136. btr_cur_t* cursor, /*!< in: cursor at which to insert */
  1137. rec_t** split_rec) /*!< out: if split recommended,
  1138. the first record on upper half page,
  1139. or NULL if tuple to be inserted should
  1140. be first */
  1141. {
  1142. page_t* page;
  1143. rec_t* insert_point;
  1144. rec_t* infimum;
  1145. page = btr_cur_get_page(cursor);
  1146. insert_point = btr_cur_get_rec(cursor);
  1147. if (page_header_get_ptr(page, PAGE_LAST_INSERT)
  1148. == page_rec_get_next(insert_point)) {
  1149. infimum = page_get_infimum_rec(page);
  1150. /* If the convergence is in the middle of a page, include also
  1151. the record immediately before the new insert to the upper
  1152. page. Otherwise, we could repeatedly move from page to page
  1153. lots of records smaller than the convergence point. */
  1154. if (infimum != insert_point
  1155. && page_rec_get_next(infimum) != insert_point) {
  1156. *split_rec = insert_point;
  1157. } else {
  1158. *split_rec = page_rec_get_next(insert_point);
  1159. }
  1160. return(TRUE);
  1161. }
  1162. return(FALSE);
  1163. }
  1164. /*************************************************************//**
  1165. Decides if the page should be split at the convergence point of inserts
  1166. converging to the right.
  1167. @return TRUE if split recommended */
  1168. UNIV_INTERN
  1169. ibool
  1170. btr_page_get_split_rec_to_right(
  1171. /*============================*/
  1172. btr_cur_t* cursor, /*!< in: cursor at which to insert */
  1173. rec_t** split_rec) /*!< out: if split recommended,
  1174. the first record on upper half page,
  1175. or NULL if tuple to be inserted should
  1176. be first */
  1177. {
  1178. page_t* page;
  1179. rec_t* insert_point;
  1180. page = btr_cur_get_page(cursor);
  1181. insert_point = btr_cur_get_rec(cursor);
  1182. /* We use eager heuristics: if the new insert would be right after
  1183. the previous insert on the same page, we assume that there is a
  1184. pattern of sequential inserts here. */
  1185. if (UNIV_LIKELY(page_header_get_ptr(page, PAGE_LAST_INSERT)
  1186. == insert_point)) {
  1187. rec_t* next_rec;
  1188. next_rec = page_rec_get_next(insert_point);
  1189. if (page_rec_is_supremum(next_rec)) {
  1190. split_at_new:
  1191. /* Split at the new record to insert */
  1192. *split_rec = NULL;
  1193. } else {
  1194. rec_t* next_next_rec = page_rec_get_next(next_rec);
  1195. if (page_rec_is_supremum(next_next_rec)) {
  1196. goto split_at_new;
  1197. }
  1198. /* If there are >= 2 user records up from the insert
  1199. point, split all but 1 off. We want to keep one because
  1200. then sequential inserts can use the adaptive hash
  1201. index, as they can do the necessary checks of the right
  1202. search position just by looking at the records on this
  1203. page. */
  1204. *split_rec = next_next_rec;
  1205. }
  1206. return(TRUE);
  1207. }
  1208. return(FALSE);
  1209. }
  1210. /*************************************************************//**
  1211. Calculates a split record such that the tuple will certainly fit on
  1212. its half-page when the split is performed. We assume in this function
  1213. only that the cursor page has at least one user record.
  1214. @return split record, or NULL if tuple will be the first record on
  1215. upper half-page */
  1216. static
  1217. rec_t*
  1218. btr_page_get_sure_split_rec(
  1219. /*========================*/
  1220. btr_cur_t* cursor, /*!< in: cursor at which insert should be made */
  1221. const dtuple_t* tuple, /*!< in: tuple to insert */
  1222. ulint n_ext) /*!< in: number of externally stored columns */
  1223. {
  1224. page_t* page;
  1225. page_zip_des_t* page_zip;
  1226. ulint insert_size;
  1227. ulint free_space;
  1228. ulint total_data;
  1229. ulint total_n_recs;
  1230. ulint total_space;
  1231. ulint incl_data;
  1232. rec_t* ins_rec;
  1233. rec_t* rec;
  1234. rec_t* next_rec;
  1235. ulint n;
  1236. mem_heap_t* heap;
  1237. ulint* offsets;
  1238. page = btr_cur_get_page(cursor);
  1239. insert_size = rec_get_converted_size(cursor->index, tuple, n_ext);
  1240. free_space = page_get_free_space_of_empty(page_is_comp(page));
  1241. page_zip = btr_cur_get_page_zip(cursor);
  1242. if (UNIV_LIKELY_NULL(page_zip)) {
  1243. /* Estimate the free space of an empty compressed page. */
  1244. ulint free_space_zip = page_zip_empty_size(
  1245. cursor->index->n_fields,
  1246. page_zip_get_size(page_zip));
  1247. if (UNIV_LIKELY(free_space > (ulint) free_space_zip)) {
  1248. free_space = (ulint) free_space_zip;
  1249. }
  1250. }
  1251. /* free_space is now the free space of a created new page */
  1252. total_data = page_get_data_size(page) + insert_size;
  1253. total_n_recs = page_get_n_recs(page) + 1;
  1254. ut_ad(total_n_recs >= 2);
  1255. total_space = total_data + page_dir_calc_reserved_space(total_n_recs);
  1256. n = 0;
  1257. incl_data = 0;
  1258. ins_rec = btr_cur_get_rec(cursor);
  1259. rec = page_get_infimum_rec(page);
  1260. heap = NULL;
  1261. offsets = NULL;
  1262. /* We start to include records to the left half, and when the
  1263. space reserved by them exceeds half of total_space, then if
  1264. the included records fit on the left page, they will be put there
  1265. if something was left over also for the right page,
  1266. otherwise the last included record will be the first on the right
  1267. half page */
  1268. do {
  1269. /* Decide the next record to include */
  1270. if (rec == ins_rec) {
  1271. rec = NULL; /* NULL denotes that tuple is
  1272. now included */
  1273. } else if (rec == NULL) {
  1274. rec = page_rec_get_next(ins_rec);
  1275. } else {
  1276. rec = page_rec_get_next(rec);
  1277. }
  1278. if (rec == NULL) {
  1279. /* Include tuple */
  1280. incl_data += insert_size;
  1281. } else {
  1282. offsets = rec_get_offsets(rec, cursor->index,
  1283. offsets, ULINT_UNDEFINED,
  1284. &heap);
  1285. incl_data += rec_offs_size(offsets);
  1286. }
  1287. n++;
  1288. } while (incl_data + page_dir_calc_reserved_space(n)
  1289. < total_space / 2);
  1290. if (incl_data + page_dir_calc_reserved_space(n) <= free_space) {
  1291. /* The next record will be the first on
  1292. the right half page if it is not the
  1293. supremum record of page */
  1294. if (rec == ins_rec) {
  1295. rec = NULL;
  1296. goto func_exit;
  1297. } else if (rec == NULL) {
  1298. next_rec = page_rec_get_next(ins_rec);
  1299. } else {
  1300. next_rec = page_rec_get_next(rec);
  1301. }
  1302. ut_ad(next_rec);
  1303. if (!page_rec_is_supremum(next_rec)) {
  1304. rec = next_rec;
  1305. }
  1306. }
  1307. func_exit:
  1308. if (UNIV_LIKELY_NULL(heap)) {
  1309. mem_heap_free(heap);
  1310. }
  1311. return(rec);
  1312. }
  1313. /*************************************************************//**
  1314. Returns TRUE if the insert fits on the appropriate half-page with the
  1315. chosen split_rec.
  1316. @return TRUE if fits */
  1317. static
  1318. ibool
  1319. btr_page_insert_fits(
  1320. /*=================*/
  1321. btr_cur_t* cursor, /*!< in: cursor at which insert
  1322. should be made */
  1323. const rec_t* split_rec,/*!< in: suggestion for first record
  1324. on upper half-page, or NULL if
  1325. tuple to be inserted should be first */
  1326. const ulint* offsets,/*!< in: rec_get_offsets(
  1327. split_rec, cursor->index) */
  1328. const dtuple_t* tuple, /*!< in: tuple to insert */
  1329. ulint n_ext, /*!< in: number of externally stored columns */
  1330. mem_heap_t* heap) /*!< in: temporary memory heap */
  1331. {
  1332. page_t* page;
  1333. ulint insert_size;
  1334. ulint free_space;
  1335. ulint total_data;
  1336. ulint total_n_recs;
  1337. const rec_t* rec;
  1338. const rec_t* end_rec;
  1339. ulint* offs;
  1340. page = btr_cur_get_page(cursor);
  1341. ut_ad(!split_rec == !offsets);
  1342. ut_ad(!offsets
  1343. || !page_is_comp(page) == !rec_offs_comp(offsets));
  1344. ut_ad(!offsets
  1345. || rec_offs_validate(split_rec, cursor->index, offsets));
  1346. insert_size = rec_get_converted_size(cursor->index, tuple, n_ext);
  1347. free_space = page_get_free_space_of_empty(page_is_comp(page));
  1348. /* free_space is now the free space of a created new page */
  1349. total_data = page_get_data_size(page) + insert_size;
  1350. total_n_recs = page_get_n_recs(page) + 1;
  1351. /* We determine which records (from rec to end_rec, not including
  1352. end_rec) will end up on the other half page from tuple when it is
  1353. inserted. */
  1354. if (split_rec == NULL) {
  1355. rec = page_rec_get_next(page_get_infimum_rec(page));
  1356. end_rec = page_rec_get_next(btr_cur_get_rec(cursor));
  1357. } else if (cmp_dtuple_rec(tuple, split_rec, offsets) >= 0) {
  1358. rec = page_rec_get_next(page_get_infimum_rec(page));
  1359. end_rec = split_rec;
  1360. } else {
  1361. rec = split_rec;
  1362. end_rec = page_get_supremum_rec(page);
  1363. }
  1364. if (total_data + page_dir_calc_reserved_space(total_n_recs)
  1365. <= free_space) {
  1366. /* Ok, there will be enough available space on the
  1367. half page where the tuple is inserted */
  1368. return(TRUE);
  1369. }
  1370. offs = NULL;
  1371. while (rec != end_rec) {
  1372. /* In this loop we calculate the amount of reserved
  1373. space after rec is removed from page. */
  1374. offs = rec_get_offsets(rec, cursor->index, offs,
  1375. ULINT_UNDEFINED, &heap);
  1376. total_data -= rec_offs_size(offs);
  1377. total_n_recs--;
  1378. if (total_data + page_dir_calc_reserved_space(total_n_recs)
  1379. <= free_space) {
  1380. /* Ok, there will be enough available space on the
  1381. half page where the tuple is inserted */
  1382. return(TRUE);
  1383. }
  1384. rec = page_rec_get_next_const(rec);
  1385. }
  1386. return(FALSE);
  1387. }
  1388. /*******************************************************//**
  1389. Inserts a data tuple to a tree on a non-leaf level. It is assumed
  1390. that mtr holds an x-latch on the tree. */
  1391. UNIV_INTERN
  1392. void
  1393. btr_insert_on_non_leaf_level(
  1394. /*=========================*/
  1395. dict_index_t* index, /*!< in: index */
  1396. ulint level, /*!< in: level, must be > 0 */
  1397. dtuple_t* tuple, /*!< in: the record to be inserted */
  1398. mtr_t* mtr) /*!< in: mtr */
  1399. {
  1400. big_rec_t* dummy_big_rec;
  1401. btr_cur_t cursor;
  1402. ulint err;
  1403. rec_t* rec;
  1404. ut_ad(level > 0);
  1405. btr_cur_search_to_nth_level(index, level, tuple, PAGE_CUR_LE,
  1406. BTR_CONT_MODIFY_TREE,
  1407. &cursor, 0, mtr);
  1408. err = btr_cur_pessimistic_insert(BTR_NO_LOCKING_FLAG
  1409. | BTR_KEEP_SYS_FLAG
  1410. | BTR_NO_UNDO_LOG_FLAG,
  1411. &cursor, tuple, &rec,
  1412. &dummy_big_rec, 0, NULL, mtr);
  1413. ut_a(err == DB_SUCCESS);
  1414. }
  1415. /**************************************************************//**
  1416. Attaches the halves of an index page on the appropriate level in an
  1417. index tree. */
  1418. static
  1419. void
  1420. btr_attach_half_pages(
  1421. /*==================*/
  1422. dict_index_t* index, /*!< in: the index tree */
  1423. buf_block_t* block, /*!< in/out: page to be split */
  1424. rec_t* split_rec, /*!< in: first record on upper
  1425. half page */
  1426. buf_block_t* new_block, /*!< in/out: the new half page */
  1427. ulint direction, /*!< in: FSP_UP or FSP_DOWN */
  1428. mtr_t* mtr) /*!< in: mtr */
  1429. {
  1430. ulint space;
  1431. ulint zip_size;
  1432. ulint prev_page_no;
  1433. ulint next_page_no;
  1434. ulint level;
  1435. page_t* page = buf_block_get_frame(block);
  1436. page_t* lower_page;
  1437. page_t* upper_page;
  1438. ulint lower_page_no;
  1439. ulint upper_page_no;
  1440. page_zip_des_t* lower_page_zip;
  1441. page_zip_des_t* upper_page_zip;
  1442. dtuple_t* node_ptr_upper;
  1443. mem_heap_t* heap;
  1444. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  1445. ut_ad(mtr_memo_contains(mtr, new_block, MTR_MEMO_PAGE_X_FIX));
  1446. /* Create a memory heap where the data tuple is stored */
  1447. heap = mem_heap_create(1024);
  1448. /* Based on split direction, decide upper and lower pages */
  1449. if (direction == FSP_DOWN) {
  1450. btr_cur_t cursor;
  1451. ulint* offsets;
  1452. lower_page = buf_block_get_frame(new_block);
  1453. lower_page_no = buf_block_get_page_no(new_block);
  1454. lower_page_zip = buf_block_get_page_zip(new_block);
  1455. upper_page = buf_block_get_frame(block);
  1456. upper_page_no = buf_block_get_page_no(block);
  1457. upper_page_zip = buf_block_get_page_zip(block);
  1458. /* Look up the index for the node pointer to page */
  1459. offsets = btr_page_get_father_block(NULL, heap, index,
  1460. block, mtr, &cursor);
  1461. /* Replace the address of the old child node (= page) with the
  1462. address of the new lower half */
  1463. btr_node_ptr_set_child_page_no(
  1464. btr_cur_get_rec(&cursor),
  1465. btr_cur_get_page_zip(&cursor),
  1466. offsets, lower_page_no, mtr);
  1467. mem_heap_empty(heap);
  1468. } else {
  1469. lower_page = buf_block_get_frame(block);
  1470. lower_page_no = buf_block_get_page_no(block);
  1471. lower_page_zip = buf_block_get_page_zip(block);
  1472. upper_page = buf_block_get_frame(new_block);
  1473. upper_page_no = buf_block_get_page_no(new_block);
  1474. upper_page_zip = buf_block_get_page_zip(new_block);
  1475. }
  1476. /* Get the level of the split pages */
  1477. level = btr_page_get_level(buf_block_get_frame(block), mtr);
  1478. ut_ad(level
  1479. == btr_page_get_level(buf_block_get_frame(new_block), mtr));
  1480. /* Build the node pointer (= node key and page address) for the upper
  1481. half */
  1482. node_ptr_upper = dict_index_build_node_ptr(index, split_rec,
  1483. upper_page_no, heap, level);
  1484. /* Insert it next to the pointer to the lower half. Note that this
  1485. may generate recursion leading to a split on the higher level. */
  1486. btr_insert_on_non_leaf_level(index, level + 1, node_ptr_upper, mtr);
  1487. /* Free the memory heap */
  1488. mem_heap_free(heap);
  1489. /* Get the previous and next pages of page */
  1490. prev_page_no = btr_page_get_prev(page, mtr);
  1491. next_page_no = btr_page_get_next(page, mtr);
  1492. space = buf_block_get_space(block);
  1493. zip_size = buf_block_get_zip_size(block);
  1494. /* Update page links of the level */
  1495. if (prev_page_no != FIL_NULL) {
  1496. buf_block_t* prev_block = btr_block_get(space, zip_size,
  1497. prev_page_no,
  1498. RW_X_LATCH, mtr);
  1499. #ifdef UNIV_BTR_DEBUG
  1500. ut_a(page_is_comp(prev_block->frame) == page_is_comp(page));
  1501. ut_a(btr_page_get_next(prev_block->frame, mtr)
  1502. == buf_block_get_page_no(block));
  1503. #endif /* UNIV_BTR_DEBUG */
  1504. btr_page_set_next(buf_block_get_frame(prev_block),
  1505. buf_block_get_page_zip(prev_block),
  1506. lower_page_no, mtr);
  1507. }
  1508. if (next_page_no != FIL_NULL) {
  1509. buf_block_t* next_block = btr_block_get(space, zip_size,
  1510. next_page_no,
  1511. RW_X_LATCH, mtr);
  1512. #ifdef UNIV_BTR_DEBUG
  1513. ut_a(page_is_comp(next_block->frame) == page_is_comp(page));
  1514. ut_a(btr_page_get_prev(next_block->frame, mtr)
  1515. == page_get_page_no(page));
  1516. #endif /* UNIV_BTR_DEBUG */
  1517. btr_page_set_prev(buf_block_get_frame(next_block),
  1518. buf_block_get_page_zip(next_block),
  1519. upper_page_no, mtr);
  1520. }
  1521. btr_page_set_prev(lower_page, lower_page_zip, prev_page_no, mtr);
  1522. btr_page_set_next(lower_page, lower_page_zip, upper_page_no, mtr);
  1523. btr_page_set_prev(upper_page, upper_page_zip, lower_page_no, mtr);
  1524. btr_page_set_next(upper_page, upper_page_zip, next_page_no, mtr);
  1525. }
  1526. /*************************************************************//**
  1527. Splits an index page to halves and inserts the tuple. It is assumed
  1528. that mtr holds an x-latch to the index tree. NOTE: the tree x-latch is
  1529. released within this function! NOTE that the operation of this
  1530. function must always succeed, we cannot reverse it: therefore enough
  1531. free disk space (2 pages) must be guaranteed to be available before
  1532. this function is called.
  1533. @return inserted record */
  1534. UNIV_INTERN
  1535. rec_t*
  1536. btr_page_split_and_insert(
  1537. /*======================*/
  1538. btr_cur_t* cursor, /*!< in: cursor at which to insert; when the
  1539. function returns, the cursor is positioned
  1540. on the predecessor of the inserted record */
  1541. const dtuple_t* tuple, /*!< in: tuple to insert */
  1542. ulint n_ext, /*!< in: number of externally stored columns */
  1543. mtr_t* mtr) /*!< in: mtr */
  1544. {
  1545. buf_block_t* block;
  1546. page_t* page;
  1547. page_zip_des_t* page_zip;
  1548. ulint page_no;
  1549. byte direction;
  1550. ulint hint_page_no;
  1551. buf_block_t* new_block;
  1552. page_t* new_page;
  1553. page_zip_des_t* new_page_zip;
  1554. rec_t* split_rec;
  1555. buf_block_t* left_block;
  1556. buf_block_t* right_block;
  1557. buf_block_t* insert_block;
  1558. page_t* insert_page;
  1559. page_cur_t* page_cursor;
  1560. rec_t* first_rec;
  1561. byte* buf = 0; /* remove warning */
  1562. rec_t* move_limit;
  1563. ibool insert_will_fit;
  1564. ibool insert_left;
  1565. ulint n_iterations = 0;
  1566. rec_t* rec;
  1567. mem_heap_t* heap;
  1568. ulint n_uniq;
  1569. ulint* offsets;
  1570. heap = mem_heap_create(1024);
  1571. n_uniq = dict_index_get_n_unique_in_tree(cursor->index);
  1572. func_start:
  1573. mem_heap_empty(heap);
  1574. offsets = NULL;
  1575. ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(cursor->index),
  1576. MTR_MEMO_X_LOCK));
  1577. #ifdef UNIV_SYNC_DEBUG
  1578. ut_ad(rw_lock_own(dict_index_get_lock(cursor->index), RW_LOCK_EX));
  1579. #endif /* UNIV_SYNC_DEBUG */
  1580. block = btr_cur_get_block(cursor);
  1581. page = buf_block_get_frame(block);
  1582. page_zip = buf_block_get_page_zip(block);
  1583. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  1584. ut_ad(page_get_n_recs(page) >= 1);
  1585. page_no = buf_block_get_page_no(block);
  1586. /* 1. Decide the split record; split_rec == NULL means that the
  1587. tuple to be inserted should be the first record on the upper
  1588. half-page */
  1589. if (n_iterations > 0) {
  1590. direction = FSP_UP;
  1591. hint_page_no = page_no + 1;
  1592. split_rec = btr_page_get_sure_split_rec(cursor, tuple, n_ext);
  1593. } else if (btr_page_get_split_rec_to_right(cursor, &split_rec)) {
  1594. direction = FSP_UP;
  1595. hint_page_no = page_no + 1;
  1596. } else if (btr_page_get_split_rec_to_left(cursor, &split_rec)) {
  1597. direction = FSP_DOWN;
  1598. hint_page_no = page_no - 1;
  1599. } else {
  1600. direction = FSP_UP;
  1601. hint_page_no = page_no + 1;
  1602. if (page_get_n_recs(page) == 1) {
  1603. page_cur_t pcur;
  1604. /* There is only one record in the index page
  1605. therefore we can't split the node in the middle
  1606. by default. We need to determine whether the
  1607. new record will be inserted to the left or right. */
  1608. /* Read the first (and only) record in the page. */
  1609. page_cur_set_before_first(block, &pcur);
  1610. page_cur_move_to_next(&pcur);
  1611. first_rec = page_cur_get_rec(&pcur);
  1612. offsets = rec_get_offsets(
  1613. first_rec, cursor->index, offsets,
  1614. n_uniq, &heap);
  1615. /* If the new record is less than the existing record
  1616. the split in the middle will copy the existing
  1617. record to the new node. */
  1618. if (cmp_dtuple_rec(tuple, first_rec, offsets) < 0) {
  1619. split_rec = page_get_middle_rec(page);
  1620. } else {
  1621. split_rec = NULL;
  1622. }
  1623. } else {
  1624. split_rec = page_get_middle_rec(page);
  1625. }
  1626. }
  1627. /* 2. Allocate a new page to the index */
  1628. new_block = btr_page_alloc(cursor->index, hint_page_no, direction,
  1629. btr_page_get_level(page, mtr), mtr);
  1630. new_page = buf_block_get_frame(new_block);
  1631. new_page_zip = buf_block_get_page_zip(new_block);
  1632. btr_page_create(new_block, new_page_zip, cursor->index,
  1633. btr_page_get_level(page, mtr), mtr);
  1634. /* 3. Calculate the first record on the upper half-page, and the
  1635. first record (move_limit) on original page which ends up on the
  1636. upper half */
  1637. if (split_rec) {
  1638. first_rec = move_limit = split_rec;
  1639. offsets = rec_get_offsets(split_rec, cursor->index, offsets,
  1640. n_uniq, &heap);
  1641. insert_left = cmp_dtuple_rec(tuple, split_rec, offsets) < 0;
  1642. if (UNIV_UNLIKELY(!insert_left && new_page_zip
  1643. && n_iterations > 0)) {
  1644. /* If a compressed page has already been split,
  1645. avoid further splits by inserting the record
  1646. to an empty page. */
  1647. split_rec = NULL;
  1648. goto insert_right;
  1649. }
  1650. } else {
  1651. insert_right:
  1652. insert_left = FALSE;
  1653. buf = mem_alloc(rec_get_converted_size(cursor->index,
  1654. tuple, n_ext));
  1655. first_rec = rec_convert_dtuple_to_rec(buf, cursor->index,
  1656. tuple, n_ext);
  1657. move_limit = page_rec_get_next(btr_cur_get_rec(cursor));
  1658. }
  1659. /* 4. Do first the modifications in the tree structure */
  1660. btr_attach_half_pages(cursor->index, block,
  1661. first_rec, new_block, direction, mtr);
  1662. /* If the split is made on the leaf level and the insert will fit
  1663. on the appropriate half-page, we may release the tree x-latch.
  1664. We can then move the records after releasing the tree latch,
  1665. thus reducing the tree latch contention. */
  1666. if (split_rec) {
  1667. insert_will_fit = !new_page_zip
  1668. && btr_page_insert_fits(cursor, split_rec,
  1669. offsets, tuple, n_ext, heap);
  1670. } else {
  1671. mem_free(buf);
  1672. insert_will_fit = !new_page_zip
  1673. && btr_page_insert_fits(cursor, NULL,
  1674. NULL, tuple, n_ext, heap);
  1675. }
  1676. if (insert_will_fit && page_is_leaf(page)) {
  1677. mtr_memo_release(mtr, dict_index_get_lock(cursor->index),
  1678. MTR_MEMO_X_LOCK);
  1679. }
  1680. /* 5. Move then the records to the new page */
  1681. if (direction == FSP_DOWN) {
  1682. /* fputs("Split left\n", stderr); */
  1683. if (0
  1684. #ifdef UNIV_ZIP_COPY
  1685. || page_zip
  1686. #endif /* UNIV_ZIP_COPY */
  1687. || UNIV_UNLIKELY
  1688. (!page_move_rec_list_start(new_block, block, move_limit,
  1689. cursor->index, mtr))) {
  1690. /* For some reason, compressing new_page failed,
  1691. even though it should contain fewer records than
  1692. the original page. Copy the page byte for byte
  1693. and then delete the records from both pages
  1694. as appropriate. Deleting will always succeed. */
  1695. ut_a(new_page_zip);
  1696. page_zip_copy_recs(new_page_zip, new_page,
  1697. page_zip, page, cursor->index, mtr);
  1698. page_delete_rec_list_end(move_limit - page + new_page,
  1699. new_block, cursor->index,
  1700. ULINT_UNDEFINED,
  1701. ULINT_UNDEFINED, mtr);
  1702. /* Update the lock table and possible hash index. */
  1703. lock_move_rec_list_start(
  1704. new_block, block, move_limit,
  1705. new_page + PAGE_NEW_INFIMUM);
  1706. btr_search_move_or_delete_hash_entries(
  1707. new_block, block, cursor->index);
  1708. /* Delete the records from the source page. */
  1709. page_delete_rec_list_start(move_limit, block,
  1710. cursor->index, mtr);
  1711. }
  1712. left_block = new_block;
  1713. right_block = block;
  1714. lock_update_split_left(right_block, left_block);
  1715. } else {
  1716. /* fputs("Split right\n", stderr); */
  1717. if (0
  1718. #ifdef UNIV_ZIP_COPY
  1719. || page_zip
  1720. #endif /* UNIV_ZIP_COPY */
  1721. || UNIV_UNLIKELY
  1722. (!page_move_rec_list_end(new_block, block, move_limit,
  1723. cursor->index, mtr))) {
  1724. /* For some reason, compressing new_page failed,
  1725. even though it should contain fewer records than
  1726. the original page. Copy the page byte for byte
  1727. and then delete the records from both pages
  1728. as appropriate. Deleting will always succeed. */
  1729. ut_a(new_page_zip);
  1730. page_zip_copy_recs(new_page_zip, new_page,
  1731. page_zip, page, cursor->index, mtr);
  1732. page_delete_rec_list_start(move_limit - page
  1733. + new_page, new_block,
  1734. cursor->index, mtr);
  1735. /* Update the lock table and possible hash index. */
  1736. lock_move_rec_list_end(new_block, block, move_limit);
  1737. btr_search_move_or_delete_hash_entries(
  1738. new_block, block, cursor->index);
  1739. /* Delete the records from the source page. */
  1740. page_delete_rec_list_end(move_limit, block,
  1741. cursor->index,
  1742. ULINT_UNDEFINED,
  1743. ULINT_UNDEFINED, mtr);
  1744. }
  1745. left_block = block;
  1746. right_block = new_block;
  1747. lock_update_split_right(right_block, left_block);
  1748. }
  1749. #ifdef UNIV_ZIP_DEBUG
  1750. if (UNIV_LIKELY_NULL(page_zip)) {
  1751. ut_a(page_zip_validate(page_zip, page));
  1752. ut_a(page_zip_validate(new_page_zip, new_page));
  1753. }
  1754. #endif /* UNIV_ZIP_DEBUG */
  1755. /* At this point, split_rec, move_limit and first_rec may point
  1756. to garbage on the old page. */
  1757. /* 6. The split and the tree modification is now completed. Decide the
  1758. page where the tuple should be inserted */
  1759. if (insert_left) {
  1760. insert_block = left_block;
  1761. } else {
  1762. insert_block = right_block;
  1763. }
  1764. insert_page = buf_block_get_frame(insert_block);
  1765. /* 7. Reposition the cursor for insert and try insertion */
  1766. page_cursor = btr_cur_get_page_cur(cursor);
  1767. page_cur_search(insert_block, cursor->index, tuple,
  1768. PAGE_CUR_LE, page_cursor);
  1769. rec = page_cur_tuple_insert(page_cursor, tuple,
  1770. cursor->index, n_ext, mtr);
  1771. #ifdef UNIV_ZIP_DEBUG
  1772. {
  1773. page_zip_des_t* insert_page_zip
  1774. = buf_block_get_page_zip(insert_block);
  1775. ut_a(!insert_page_zip
  1776. || page_zip_validate(insert_page_zip, insert_page));
  1777. }
  1778. #endif /* UNIV_ZIP_DEBUG */
  1779. if (UNIV_LIKELY(rec != NULL)) {
  1780. goto func_exit;
  1781. }
  1782. /* 8. If insert did not fit, try page reorganization */
  1783. if (UNIV_UNLIKELY
  1784. (!btr_page_reorganize(insert_block, cursor->index, mtr))) {
  1785. goto insert_failed;
  1786. }
  1787. page_cur_search(insert_block, cursor->index, tuple,
  1788. PAGE_CUR_LE, page_cursor);
  1789. rec = page_cur_tuple_insert(page_cursor, tuple, cursor->index,
  1790. n_ext, mtr);
  1791. if (UNIV_UNLIKELY(rec == NULL)) {
  1792. /* The insert did not fit on the page: loop back to the
  1793. start of the function for a new split */
  1794. insert_failed:
  1795. /* We play safe and reset the free bits for new_page */
  1796. if (!dict_index_is_clust(cursor->index)) {
  1797. ibuf_reset_free_bits(new_block);
  1798. }
  1799. /* fprintf(stderr, "Split second round %lu\n",
  1800. page_get_page_no(page)); */
  1801. n_iterations++;
  1802. ut_ad(n_iterations < 2
  1803. || buf_block_get_page_zip(insert_block));
  1804. ut_ad(!insert_will_fit);
  1805. goto func_start;
  1806. }
  1807. func_exit:
  1808. /* Insert fit on the page: update the free bits for the
  1809. left and right pages in the same mtr */
  1810. if (!dict_index_is_clust(cursor->index) && page_is_leaf(page)) {
  1811. ibuf_update_free_bits_for_two_pages_low(
  1812. buf_block_get_zip_size(left_block),
  1813. left_block, right_block, mtr);
  1814. }
  1815. #if 0
  1816. fprintf(stderr, "Split and insert done %lu %lu\n",
  1817. buf_block_get_page_no(left_block),
  1818. buf_block_get_page_no(right_block));
  1819. #endif
  1820. ut_ad(page_validate(buf_block_get_frame(left_block), cursor->index));
  1821. ut_ad(page_validate(buf_block_get_frame(right_block), cursor->index));
  1822. mem_heap_free(heap);
  1823. return(rec);
  1824. }
  1825. /*************************************************************//**
  1826. Removes a page from the level list of pages. */
  1827. static
  1828. void
  1829. btr_level_list_remove(
  1830. /*==================*/
  1831. ulint space, /*!< in: space where removed */
  1832. ulint zip_size,/*!< in: compressed page size in bytes
  1833. or 0 for uncompressed pages */
  1834. page_t* page, /*!< in: page to remove */
  1835. mtr_t* mtr) /*!< in: mtr */
  1836. {
  1837. ulint prev_page_no;
  1838. ulint next_page_no;
  1839. ut_ad(page && mtr);
  1840. ut_ad(mtr_memo_contains_page(mtr, page, MTR_MEMO_PAGE_X_FIX));
  1841. ut_ad(space == page_get_space_id(page));
  1842. /* Get the previous and next page numbers of page */
  1843. prev_page_no = btr_page_get_prev(page, mtr);
  1844. next_page_no = btr_page_get_next(page, mtr);
  1845. /* Update page links of the level */
  1846. if (prev_page_no != FIL_NULL) {
  1847. buf_block_t* prev_block
  1848. = btr_block_get(space, zip_size, prev_page_no,
  1849. RW_X_LATCH, mtr);
  1850. page_t* prev_page
  1851. = buf_block_get_frame(prev_block);
  1852. #ifdef UNIV_BTR_DEBUG
  1853. ut_a(page_is_comp(prev_page) == page_is_comp(page));
  1854. ut_a(btr_page_get_next(prev_page, mtr)
  1855. == page_get_page_no(page));
  1856. #endif /* UNIV_BTR_DEBUG */
  1857. btr_page_set_next(prev_page,
  1858. buf_block_get_page_zip(prev_block),
  1859. next_page_no, mtr);
  1860. }
  1861. if (next_page_no != FIL_NULL) {
  1862. buf_block_t* next_block
  1863. = btr_block_get(space, zip_size, next_page_no,
  1864. RW_X_LATCH, mtr);
  1865. page_t* next_page
  1866. = buf_block_get_frame(next_block);
  1867. #ifdef UNIV_BTR_DEBUG
  1868. ut_a(page_is_comp(next_page) == page_is_comp(page));
  1869. ut_a(btr_page_get_prev(next_page, mtr)
  1870. == page_get_page_no(page));
  1871. #endif /* UNIV_BTR_DEBUG */
  1872. btr_page_set_prev(next_page,
  1873. buf_block_get_page_zip(next_block),
  1874. prev_page_no, mtr);
  1875. }
  1876. }
  1877. /****************************************************************//**
  1878. Writes the redo log record for setting an index record as the predefined
  1879. minimum record. */
  1880. UNIV_INLINE
  1881. void
  1882. btr_set_min_rec_mark_log(
  1883. /*=====================*/
  1884. rec_t* rec, /*!< in: record */
  1885. byte type, /*!< in: MLOG_COMP_REC_MIN_MARK or MLOG_REC_MIN_MARK */
  1886. mtr_t* mtr) /*!< in: mtr */
  1887. {
  1888. mlog_write_initial_log_record(rec, type, mtr);
  1889. /* Write rec offset as a 2-byte ulint */
  1890. mlog_catenate_ulint(mtr, page_offset(rec), MLOG_2BYTES);
  1891. }
  1892. #else /* !UNIV_HOTBACKUP */
  1893. # define btr_set_min_rec_mark_log(rec,comp,mtr) ((void) 0)
  1894. #endif /* !UNIV_HOTBACKUP */
  1895. /****************************************************************//**
  1896. Parses the redo log record for setting an index record as the predefined
  1897. minimum record.
  1898. @return end of log record or NULL */
  1899. UNIV_INTERN
  1900. byte*
  1901. btr_parse_set_min_rec_mark(
  1902. /*=======================*/
  1903. byte* ptr, /*!< in: buffer */
  1904. byte* end_ptr,/*!< in: buffer end */
  1905. ulint comp, /*!< in: nonzero=compact page format */
  1906. page_t* page, /*!< in: page or NULL */
  1907. mtr_t* mtr) /*!< in: mtr or NULL */
  1908. {
  1909. rec_t* rec;
  1910. if (end_ptr < ptr + 2) {
  1911. return(NULL);
  1912. }
  1913. if (page) {
  1914. ut_a(!page_is_comp(page) == !comp);
  1915. rec = page + mach_read_from_2(ptr);
  1916. btr_set_min_rec_mark(rec, mtr);
  1917. }
  1918. return(ptr + 2);
  1919. }
  1920. /****************************************************************//**
  1921. Sets a record as the predefined minimum record. */
  1922. UNIV_INTERN
  1923. void
  1924. btr_set_min_rec_mark(
  1925. /*=================*/
  1926. rec_t* rec, /*!< in: record */
  1927. mtr_t* mtr) /*!< in: mtr */
  1928. {
  1929. ulint info_bits;
  1930. if (UNIV_LIKELY(page_rec_is_comp(rec))) {
  1931. info_bits = rec_get_info_bits(rec, TRUE);
  1932. rec_set_info_bits_new(rec, info_bits | REC_INFO_MIN_REC_FLAG);
  1933. btr_set_min_rec_mark_log(rec, MLOG_COMP_REC_MIN_MARK, mtr);
  1934. } else {
  1935. info_bits = rec_get_info_bits(rec, FALSE);
  1936. rec_set_info_bits_old(rec, info_bits | REC_INFO_MIN_REC_FLAG);
  1937. btr_set_min_rec_mark_log(rec, MLOG_REC_MIN_MARK, mtr);
  1938. }
  1939. }
  1940. #ifndef UNIV_HOTBACKUP
  1941. /*************************************************************//**
  1942. Deletes on the upper level the node pointer to a page. */
  1943. UNIV_INTERN
  1944. void
  1945. btr_node_ptr_delete(
  1946. /*================*/
  1947. dict_index_t* index, /*!< in: index tree */
  1948. buf_block_t* block, /*!< in: page whose node pointer is deleted */
  1949. mtr_t* mtr) /*!< in: mtr */
  1950. {
  1951. btr_cur_t cursor;
  1952. ibool compressed;
  1953. ulint err;
  1954. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  1955. /* Delete node pointer on father page */
  1956. btr_page_get_father(index, block, mtr, &cursor);
  1957. compressed = btr_cur_pessimistic_delete(&err, TRUE, &cursor, RB_NONE,
  1958. mtr);
  1959. ut_a(err == DB_SUCCESS);
  1960. if (!compressed) {
  1961. btr_cur_compress_if_useful(&cursor, mtr);
  1962. }
  1963. }
  1964. /*************************************************************//**
  1965. If page is the only on its level, this function moves its records to the
  1966. father page, thus reducing the tree height. */
  1967. static
  1968. void
  1969. btr_lift_page_up(
  1970. /*=============*/
  1971. dict_index_t* index, /*!< in: index tree */
  1972. buf_block_t* block, /*!< in: page which is the only on its level;
  1973. must not be empty: use
  1974. btr_discard_only_page_on_level if the last
  1975. record from the page should be removed */
  1976. mtr_t* mtr) /*!< in: mtr */
  1977. {
  1978. buf_block_t* father_block;
  1979. page_t* father_page;
  1980. ulint page_level;
  1981. page_zip_des_t* father_page_zip;
  1982. page_t* page = buf_block_get_frame(block);
  1983. ulint root_page_no;
  1984. buf_block_t* blocks[BTR_MAX_LEVELS];
  1985. ulint n_blocks; /*!< last used index in blocks[] */
  1986. ulint i;
  1987. ut_ad(btr_page_get_prev(page, mtr) == FIL_NULL);
  1988. ut_ad(btr_page_get_next(page, mtr) == FIL_NULL);
  1989. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  1990. page_level = btr_page_get_level(page, mtr);
  1991. root_page_no = dict_index_get_page(index);
  1992. {
  1993. btr_cur_t cursor;
  1994. mem_heap_t* heap = mem_heap_create(100);
  1995. ulint* offsets;
  1996. buf_block_t* b;
  1997. offsets = btr_page_get_father_block(NULL, heap, index,
  1998. block, mtr, &cursor);
  1999. father_block = btr_cur_get_block(&cursor);
  2000. father_page_zip = buf_block_get_page_zip(father_block);
  2001. father_page = buf_block_get_frame(father_block);
  2002. n_blocks = 0;
  2003. /* Store all ancestor pages so we can reset their
  2004. levels later on. We have to do all the searches on
  2005. the tree now because later on, after we've replaced
  2006. the first level, the tree is in an inconsistent state
  2007. and can not be searched. */
  2008. for (b = father_block;
  2009. buf_block_get_page_no(b) != root_page_no; ) {
  2010. ut_a(n_blocks < BTR_MAX_LEVELS);
  2011. offsets = btr_page_get_father_block(offsets, heap,
  2012. index, b,
  2013. mtr, &cursor);
  2014. blocks[n_blocks++] = b = btr_cur_get_block(&cursor);
  2015. }
  2016. mem_heap_free(heap);
  2017. }
  2018. btr_search_drop_page_hash_index(block);
  2019. /* Make the father empty */
  2020. btr_page_empty(father_block, father_page_zip, index, page_level, mtr);
  2021. /* Copy the records to the father page one by one. */
  2022. if (0
  2023. #ifdef UNIV_ZIP_COPY
  2024. || father_page_zip
  2025. #endif /* UNIV_ZIP_COPY */
  2026. || UNIV_UNLIKELY
  2027. (!page_copy_rec_list_end(father_block, block,
  2028. page_get_infimum_rec(page),
  2029. index, mtr))) {
  2030. const page_zip_des_t* page_zip
  2031. = buf_block_get_page_zip(block);
  2032. ut_a(father_page_zip);
  2033. ut_a(page_zip);
  2034. /* Copy the page byte for byte. */
  2035. page_zip_copy_recs(father_page_zip, father_page,
  2036. page_zip, page, index, mtr);
  2037. /* Update the lock table and possible hash index. */
  2038. lock_move_rec_list_end(father_block, block,
  2039. page_get_infimum_rec(page));
  2040. btr_search_move_or_delete_hash_entries(father_block, block,
  2041. index);
  2042. }
  2043. lock_update_copy_and_discard(father_block, block);
  2044. /* Go upward to root page, decrementing levels by one. */
  2045. for (i = 0; i < n_blocks; i++, page_level++) {
  2046. page_t* page = buf_block_get_frame(blocks[i]);
  2047. page_zip_des_t* page_zip= buf_block_get_page_zip(blocks[i]);
  2048. ut_ad(btr_page_get_level(page, mtr) == page_level + 1);
  2049. btr_page_set_level(page, page_zip, page_level, mtr);
  2050. #ifdef UNIV_ZIP_DEBUG
  2051. ut_a(!page_zip || page_zip_validate(page_zip, page));
  2052. #endif /* UNIV_ZIP_DEBUG */
  2053. }
  2054. /* Free the file page */
  2055. btr_page_free(index, block, mtr);
  2056. /* We play it safe and reset the free bits for the father */
  2057. if (!dict_index_is_clust(index)) {
  2058. ibuf_reset_free_bits(father_block);
  2059. }
  2060. ut_ad(page_validate(father_page, index));
  2061. ut_ad(btr_check_node_ptr(index, father_block, mtr));
  2062. }
  2063. /*************************************************************//**
  2064. Tries to merge the page first to the left immediate brother if such a
  2065. brother exists, and the node pointers to the current page and to the brother
  2066. reside on the same page. If the left brother does not satisfy these
  2067. conditions, looks at the right brother. If the page is the only one on that
  2068. level lifts the records of the page to the father page, thus reducing the
  2069. tree height. It is assumed that mtr holds an x-latch on the tree and on the
  2070. page. If cursor is on the leaf level, mtr must also hold x-latches to the
  2071. brothers, if they exist.
  2072. @return TRUE on success */
  2073. UNIV_INTERN
  2074. ibool
  2075. btr_compress(
  2076. /*=========*/
  2077. btr_cur_t* cursor, /*!< in: cursor on the page to merge or lift;
  2078. the page must not be empty: in record delete
  2079. use btr_discard_page if the page would become
  2080. empty */
  2081. mtr_t* mtr) /*!< in: mtr */
  2082. {
  2083. dict_index_t* index;
  2084. ulint space;
  2085. ulint zip_size;
  2086. ulint left_page_no;
  2087. ulint right_page_no;
  2088. buf_block_t* merge_block;
  2089. page_t* merge_page;
  2090. page_zip_des_t* merge_page_zip;
  2091. ibool is_left;
  2092. buf_block_t* block;
  2093. page_t* page;
  2094. btr_cur_t father_cursor;
  2095. mem_heap_t* heap;
  2096. ulint* offsets;
  2097. ulint data_size;
  2098. ulint n_recs;
  2099. ulint max_ins_size;
  2100. ulint max_ins_size_reorg;
  2101. ulint level;
  2102. block = btr_cur_get_block(cursor);
  2103. page = btr_cur_get_page(cursor);
  2104. index = btr_cur_get_index(cursor);
  2105. ut_a((ibool) !!page_is_comp(page) == dict_table_is_comp(index->table));
  2106. ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index),
  2107. MTR_MEMO_X_LOCK));
  2108. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  2109. level = btr_page_get_level(page, mtr);
  2110. space = dict_index_get_space(index);
  2111. zip_size = dict_table_zip_size(index->table);
  2112. left_page_no = btr_page_get_prev(page, mtr);
  2113. right_page_no = btr_page_get_next(page, mtr);
  2114. #if 0
  2115. fprintf(stderr, "Merge left page %lu right %lu \n",
  2116. left_page_no, right_page_no);
  2117. #endif
  2118. heap = mem_heap_create(100);
  2119. offsets = btr_page_get_father_block(NULL, heap, index, block, mtr,
  2120. &father_cursor);
  2121. /* Decide the page to which we try to merge and which will inherit
  2122. the locks */
  2123. is_left = left_page_no != FIL_NULL;
  2124. if (is_left) {
  2125. merge_block = btr_block_get(space, zip_size, left_page_no,
  2126. RW_X_LATCH, mtr);
  2127. merge_page = buf_block_get_frame(merge_block);
  2128. #ifdef UNIV_BTR_DEBUG
  2129. ut_a(btr_page_get_next(merge_page, mtr)
  2130. == buf_block_get_page_no(block));
  2131. #endif /* UNIV_BTR_DEBUG */
  2132. } else if (right_page_no != FIL_NULL) {
  2133. merge_block = btr_block_get(space, zip_size, right_page_no,
  2134. RW_X_LATCH, mtr);
  2135. merge_page = buf_block_get_frame(merge_block);
  2136. #ifdef UNIV_BTR_DEBUG
  2137. ut_a(btr_page_get_prev(merge_page, mtr)
  2138. == buf_block_get_page_no(block));
  2139. #endif /* UNIV_BTR_DEBUG */
  2140. } else {
  2141. /* The page is the only one on the level, lift the records
  2142. to the father */
  2143. btr_lift_page_up(index, block, mtr);
  2144. mem_heap_free(heap);
  2145. return(TRUE);
  2146. }
  2147. n_recs = page_get_n_recs(page);
  2148. data_size = page_get_data_size(page);
  2149. #ifdef UNIV_BTR_DEBUG
  2150. ut_a(page_is_comp(merge_page) == page_is_comp(page));
  2151. #endif /* UNIV_BTR_DEBUG */
  2152. max_ins_size_reorg = page_get_max_insert_size_after_reorganize(
  2153. merge_page, n_recs);
  2154. if (data_size > max_ins_size_reorg) {
  2155. /* No space for merge */
  2156. err_exit:
  2157. /* We play it safe and reset the free bits. */
  2158. if (zip_size
  2159. && page_is_leaf(merge_page)
  2160. && !dict_index_is_clust(index)) {
  2161. ibuf_reset_free_bits(merge_block);
  2162. }
  2163. mem_heap_free(heap);
  2164. return(FALSE);
  2165. }
  2166. ut_ad(page_validate(merge_page, index));
  2167. max_ins_size = page_get_max_insert_size(merge_page, n_recs);
  2168. if (UNIV_UNLIKELY(data_size > max_ins_size)) {
  2169. /* We have to reorganize merge_page */
  2170. if (UNIV_UNLIKELY(!btr_page_reorganize(merge_block,
  2171. index, mtr))) {
  2172. goto err_exit;
  2173. }
  2174. max_ins_size = page_get_max_insert_size(merge_page, n_recs);
  2175. ut_ad(page_validate(merge_page, index));
  2176. ut_ad(max_ins_size == max_ins_size_reorg);
  2177. if (UNIV_UNLIKELY(data_size > max_ins_size)) {
  2178. /* Add fault tolerance, though this should
  2179. never happen */
  2180. goto err_exit;
  2181. }
  2182. }
  2183. merge_page_zip = buf_block_get_page_zip(merge_block);
  2184. #ifdef UNIV_ZIP_DEBUG
  2185. if (UNIV_LIKELY_NULL(merge_page_zip)) {
  2186. const page_zip_des_t* page_zip
  2187. = buf_block_get_page_zip(block);
  2188. ut_a(page_zip);
  2189. ut_a(page_zip_validate(merge_page_zip, merge_page));
  2190. ut_a(page_zip_validate(page_zip, page));
  2191. }
  2192. #endif /* UNIV_ZIP_DEBUG */
  2193. /* Move records to the merge page */
  2194. if (is_left) {
  2195. rec_t* orig_pred = page_copy_rec_list_start(
  2196. merge_block, block, page_get_supremum_rec(page),
  2197. index, mtr);
  2198. if (UNIV_UNLIKELY(!orig_pred)) {
  2199. goto err_exit;
  2200. }
  2201. btr_search_drop_page_hash_index(block);
  2202. /* Remove the page from the level list */
  2203. btr_level_list_remove(space, zip_size, page, mtr);
  2204. btr_node_ptr_delete(index, block, mtr);
  2205. lock_update_merge_left(merge_block, orig_pred, block);
  2206. } else {
  2207. rec_t* orig_succ;
  2208. #ifdef UNIV_BTR_DEBUG
  2209. byte fil_page_prev[4];
  2210. #endif /* UNIV_BTR_DEBUG */
  2211. if (UNIV_LIKELY_NULL(merge_page_zip)) {
  2212. /* The function page_zip_compress(), which will be
  2213. invoked by page_copy_rec_list_end() below,
  2214. requires that FIL_PAGE_PREV be FIL_NULL.
  2215. Clear the field, but prepare to restore it. */
  2216. #ifdef UNIV_BTR_DEBUG
  2217. memcpy(fil_page_prev, merge_page + FIL_PAGE_PREV, 4);
  2218. #endif /* UNIV_BTR_DEBUG */
  2219. #if FIL_NULL != 0xffffffff
  2220. # error "FIL_NULL != 0xffffffff"
  2221. #endif
  2222. memset(merge_page + FIL_PAGE_PREV, 0xff, 4);
  2223. }
  2224. orig_succ = page_copy_rec_list_end(merge_block, block,
  2225. page_get_infimum_rec(page),
  2226. cursor->index, mtr);
  2227. if (UNIV_UNLIKELY(!orig_succ)) {
  2228. ut_a(merge_page_zip);
  2229. #ifdef UNIV_BTR_DEBUG
  2230. /* FIL_PAGE_PREV was restored from merge_page_zip. */
  2231. ut_a(!memcmp(fil_page_prev,
  2232. merge_page + FIL_PAGE_PREV, 4));
  2233. #endif /* UNIV_BTR_DEBUG */
  2234. goto err_exit;
  2235. }
  2236. btr_search_drop_page_hash_index(block);
  2237. #ifdef UNIV_BTR_DEBUG
  2238. if (UNIV_LIKELY_NULL(merge_page_zip)) {
  2239. /* Restore FIL_PAGE_PREV in order to avoid an assertion
  2240. failure in btr_level_list_remove(), which will set
  2241. the field again to FIL_NULL. Even though this makes
  2242. merge_page and merge_page_zip inconsistent for a
  2243. split second, it is harmless, because the pages
  2244. are X-latched. */
  2245. memcpy(merge_page + FIL_PAGE_PREV, fil_page_prev, 4);
  2246. }
  2247. #endif /* UNIV_BTR_DEBUG */
  2248. /* Remove the page from the level list */
  2249. btr_level_list_remove(space, zip_size, page, mtr);
  2250. /* Replace the address of the old child node (= page) with the
  2251. address of the merge page to the right */
  2252. btr_node_ptr_set_child_page_no(
  2253. btr_cur_get_rec(&father_cursor),
  2254. btr_cur_get_page_zip(&father_cursor),
  2255. offsets, right_page_no, mtr);
  2256. btr_node_ptr_delete(index, merge_block, mtr);
  2257. lock_update_merge_right(merge_block, orig_succ, block);
  2258. }
  2259. mem_heap_free(heap);
  2260. if (!dict_index_is_clust(index) && page_is_leaf(merge_page)) {
  2261. /* Update the free bits of the B-tree page in the
  2262. insert buffer bitmap. This has to be done in a
  2263. separate mini-transaction that is committed before the
  2264. main mini-transaction. We cannot update the insert
  2265. buffer bitmap in this mini-transaction, because
  2266. btr_compress() can be invoked recursively without
  2267. committing the mini-transaction in between. Since
  2268. insert buffer bitmap pages have a lower rank than
  2269. B-tree pages, we must not access other pages in the
  2270. same mini-transaction after accessing an insert buffer
  2271. bitmap page. */
  2272. /* The free bits in the insert buffer bitmap must
  2273. never exceed the free space on a page. It is safe to
  2274. decrement or reset the bits in the bitmap in a
  2275. mini-transaction that is committed before the
  2276. mini-transaction that affects the free space. */
  2277. /* It is unsafe to increment the bits in a separately
  2278. committed mini-transaction, because in crash recovery,
  2279. the free bits could momentarily be set too high. */
  2280. if (zip_size) {
  2281. /* Because the free bits may be incremented
  2282. and we cannot update the insert buffer bitmap
  2283. in the same mini-transaction, the only safe
  2284. thing we can do here is the pessimistic
  2285. approach: reset the free bits. */
  2286. ibuf_reset_free_bits(merge_block);
  2287. } else {
  2288. /* On uncompressed pages, the free bits will
  2289. never increase here. Thus, it is safe to
  2290. write the bits accurately in a separate
  2291. mini-transaction. */
  2292. ibuf_update_free_bits_if_full(merge_block,
  2293. UNIV_PAGE_SIZE,
  2294. ULINT_UNDEFINED);
  2295. }
  2296. }
  2297. ut_ad(page_validate(merge_page, index));
  2298. #ifdef UNIV_ZIP_DEBUG
  2299. ut_a(!merge_page_zip || page_zip_validate(merge_page_zip, merge_page));
  2300. #endif /* UNIV_ZIP_DEBUG */
  2301. /* Free the file page */
  2302. btr_page_free(index, block, mtr);
  2303. ut_ad(btr_check_node_ptr(index, merge_block, mtr));
  2304. return(TRUE);
  2305. }
  2306. /*************************************************************//**
  2307. Discards a page that is the only page on its level. This will empty
  2308. the whole B-tree, leaving just an empty root page. This function
  2309. should never be reached, because btr_compress(), which is invoked in
  2310. delete operations, calls btr_lift_page_up() to flatten the B-tree. */
  2311. static
  2312. void
  2313. btr_discard_only_page_on_level(
  2314. /*===========================*/
  2315. dict_index_t* index, /*!< in: index tree */
  2316. buf_block_t* block, /*!< in: page which is the only on its level */
  2317. mtr_t* mtr) /*!< in: mtr */
  2318. {
  2319. ulint page_level = 0;
  2320. trx_id_t max_trx_id;
  2321. /* Save the PAGE_MAX_TRX_ID from the leaf page. */
  2322. max_trx_id = page_get_max_trx_id(buf_block_get_frame(block));
  2323. while (buf_block_get_page_no(block) != dict_index_get_page(index)) {
  2324. btr_cur_t cursor;
  2325. buf_block_t* father;
  2326. const page_t* page = buf_block_get_frame(block);
  2327. ut_a(page_get_n_recs(page) == 1);
  2328. ut_a(page_level == btr_page_get_level(page, mtr));
  2329. ut_a(btr_page_get_prev(page, mtr) == FIL_NULL);
  2330. ut_a(btr_page_get_next(page, mtr) == FIL_NULL);
  2331. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  2332. btr_search_drop_page_hash_index(block);
  2333. btr_page_get_father(index, block, mtr, &cursor);
  2334. father = btr_cur_get_block(&cursor);
  2335. lock_update_discard(father, PAGE_HEAP_NO_SUPREMUM, block);
  2336. /* Free the file page */
  2337. btr_page_free(index, block, mtr);
  2338. block = father;
  2339. page_level++;
  2340. }
  2341. /* block is the root page, which must be empty, except
  2342. for the node pointer to the (now discarded) block(s). */
  2343. #ifdef UNIV_BTR_DEBUG
  2344. if (!dict_index_is_ibuf(index)) {
  2345. const page_t* root = buf_block_get_frame(block);
  2346. const ulint space = dict_index_get_space(index);
  2347. ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_LEAF
  2348. + root, space));
  2349. ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_TOP
  2350. + root, space));
  2351. }
  2352. #endif /* UNIV_BTR_DEBUG */
  2353. btr_page_empty(block, buf_block_get_page_zip(block), index, 0, mtr);
  2354. if (!dict_index_is_clust(index)) {
  2355. /* We play it safe and reset the free bits for the root */
  2356. ibuf_reset_free_bits(block);
  2357. if (page_is_leaf(buf_block_get_frame(block))) {
  2358. ut_a(!ut_dulint_is_zero(max_trx_id));
  2359. page_set_max_trx_id(block,
  2360. buf_block_get_page_zip(block),
  2361. max_trx_id, mtr);
  2362. }
  2363. }
  2364. }
  2365. /*************************************************************//**
  2366. Discards a page from a B-tree. This is used to remove the last record from
  2367. a B-tree page: the whole page must be removed at the same time. This cannot
  2368. be used for the root page, which is allowed to be empty. */
  2369. UNIV_INTERN
  2370. void
  2371. btr_discard_page(
  2372. /*=============*/
  2373. btr_cur_t* cursor, /*!< in: cursor on the page to discard: not on
  2374. the root page */
  2375. mtr_t* mtr) /*!< in: mtr */
  2376. {
  2377. dict_index_t* index;
  2378. ulint space;
  2379. ulint zip_size;
  2380. ulint left_page_no;
  2381. ulint right_page_no;
  2382. buf_block_t* merge_block;
  2383. page_t* merge_page;
  2384. buf_block_t* block;
  2385. page_t* page;
  2386. rec_t* node_ptr;
  2387. block = btr_cur_get_block(cursor);
  2388. index = btr_cur_get_index(cursor);
  2389. ut_ad(dict_index_get_page(index) != buf_block_get_page_no(block));
  2390. ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index),
  2391. MTR_MEMO_X_LOCK));
  2392. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  2393. space = dict_index_get_space(index);
  2394. zip_size = dict_table_zip_size(index->table);
  2395. /* Decide the page which will inherit the locks */
  2396. left_page_no = btr_page_get_prev(buf_block_get_frame(block), mtr);
  2397. right_page_no = btr_page_get_next(buf_block_get_frame(block), mtr);
  2398. if (left_page_no != FIL_NULL) {
  2399. merge_block = btr_block_get(space, zip_size, left_page_no,
  2400. RW_X_LATCH, mtr);
  2401. merge_page = buf_block_get_frame(merge_block);
  2402. #ifdef UNIV_BTR_DEBUG
  2403. ut_a(btr_page_get_next(merge_page, mtr)
  2404. == buf_block_get_page_no(block));
  2405. #endif /* UNIV_BTR_DEBUG */
  2406. } else if (right_page_no != FIL_NULL) {
  2407. merge_block = btr_block_get(space, zip_size, right_page_no,
  2408. RW_X_LATCH, mtr);
  2409. merge_page = buf_block_get_frame(merge_block);
  2410. #ifdef UNIV_BTR_DEBUG
  2411. ut_a(btr_page_get_prev(merge_page, mtr)
  2412. == buf_block_get_page_no(block));
  2413. #endif /* UNIV_BTR_DEBUG */
  2414. } else {
  2415. btr_discard_only_page_on_level(index, block, mtr);
  2416. return;
  2417. }
  2418. page = buf_block_get_frame(block);
  2419. ut_a(page_is_comp(merge_page) == page_is_comp(page));
  2420. btr_search_drop_page_hash_index(block);
  2421. if (left_page_no == FIL_NULL && !page_is_leaf(page)) {
  2422. /* We have to mark the leftmost node pointer on the right
  2423. side page as the predefined minimum record */
  2424. node_ptr = page_rec_get_next(page_get_infimum_rec(merge_page));
  2425. ut_ad(page_rec_is_user_rec(node_ptr));
  2426. /* This will make page_zip_validate() fail on merge_page
  2427. until btr_level_list_remove() completes. This is harmless,
  2428. because everything will take place within a single
  2429. mini-transaction and because writing to the redo log
  2430. is an atomic operation (performed by mtr_commit()). */
  2431. btr_set_min_rec_mark(node_ptr, mtr);
  2432. }
  2433. btr_node_ptr_delete(index, block, mtr);
  2434. /* Remove the page from the level list */
  2435. btr_level_list_remove(space, zip_size, page, mtr);
  2436. #ifdef UNIV_ZIP_DEBUG
  2437. {
  2438. page_zip_des_t* merge_page_zip
  2439. = buf_block_get_page_zip(merge_block);
  2440. ut_a(!merge_page_zip
  2441. || page_zip_validate(merge_page_zip, merge_page));
  2442. }
  2443. #endif /* UNIV_ZIP_DEBUG */
  2444. if (left_page_no != FIL_NULL) {
  2445. lock_update_discard(merge_block, PAGE_HEAP_NO_SUPREMUM,
  2446. block);
  2447. } else {
  2448. lock_update_discard(merge_block,
  2449. lock_get_min_heap_no(merge_block),
  2450. block);
  2451. }
  2452. /* Free the file page */
  2453. btr_page_free(index, block, mtr);
  2454. ut_ad(btr_check_node_ptr(index, merge_block, mtr));
  2455. }
  2456. #ifdef UNIV_BTR_PRINT
  2457. /*************************************************************//**
  2458. Prints size info of a B-tree. */
  2459. UNIV_INTERN
  2460. void
  2461. btr_print_size(
  2462. /*===========*/
  2463. dict_index_t* index) /*!< in: index tree */
  2464. {
  2465. page_t* root;
  2466. fseg_header_t* seg;
  2467. mtr_t mtr;
  2468. if (dict_index_is_ibuf(index)) {
  2469. fputs("Sorry, cannot print info of an ibuf tree:"
  2470. " use ibuf functions\n", stderr);
  2471. return;
  2472. }
  2473. mtr_start(&mtr);
  2474. root = btr_root_get(index, &mtr);
  2475. seg = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;
  2476. fputs("INFO OF THE NON-LEAF PAGE SEGMENT\n", stderr);
  2477. fseg_print(seg, &mtr);
  2478. if (!(index->type & DICT_UNIVERSAL)) {
  2479. seg = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
  2480. fputs("INFO OF THE LEAF PAGE SEGMENT\n", stderr);
  2481. fseg_print(seg, &mtr);
  2482. }
  2483. mtr_commit(&mtr);
  2484. }
  2485. /************************************************************//**
  2486. Prints recursively index tree pages. */
  2487. static
  2488. void
  2489. btr_print_recursive(
  2490. /*================*/
  2491. dict_index_t* index, /*!< in: index tree */
  2492. buf_block_t* block, /*!< in: index page */
  2493. ulint width, /*!< in: print this many entries from start
  2494. and end */
  2495. mem_heap_t** heap, /*!< in/out: heap for rec_get_offsets() */
  2496. ulint** offsets,/*!< in/out: buffer for rec_get_offsets() */
  2497. mtr_t* mtr) /*!< in: mtr */
  2498. {
  2499. const page_t* page = buf_block_get_frame(block);
  2500. page_cur_t cursor;
  2501. ulint n_recs;
  2502. ulint i = 0;
  2503. mtr_t mtr2;
  2504. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  2505. fprintf(stderr, "NODE ON LEVEL %lu page number %lu\n",
  2506. (ulong) btr_page_get_level(page, mtr),
  2507. (ulong) buf_block_get_page_no(block));
  2508. page_print(block, index, width, width);
  2509. n_recs = page_get_n_recs(page);
  2510. page_cur_set_before_first(block, &cursor);
  2511. page_cur_move_to_next(&cursor);
  2512. while (!page_cur_is_after_last(&cursor)) {
  2513. if (page_is_leaf(page)) {
  2514. /* If this is the leaf level, do nothing */
  2515. } else if ((i <= width) || (i >= n_recs - width)) {
  2516. const rec_t* node_ptr;
  2517. mtr_start(&mtr2);
  2518. node_ptr = page_cur_get_rec(&cursor);
  2519. *offsets = rec_get_offsets(node_ptr, index, *offsets,
  2520. ULINT_UNDEFINED, heap);
  2521. btr_print_recursive(index,
  2522. btr_node_ptr_get_child(node_ptr,
  2523. index,
  2524. *offsets,
  2525. &mtr2),
  2526. width, heap, offsets, &mtr2);
  2527. mtr_commit(&mtr2);
  2528. }
  2529. page_cur_move_to_next(&cursor);
  2530. i++;
  2531. }
  2532. }
  2533. /**************************************************************//**
  2534. Prints directories and other info of all nodes in the tree. */
  2535. UNIV_INTERN
  2536. void
  2537. btr_print_index(
  2538. /*============*/
  2539. dict_index_t* index, /*!< in: index */
  2540. ulint width) /*!< in: print this many entries from start
  2541. and end */
  2542. {
  2543. mtr_t mtr;
  2544. buf_block_t* root;
  2545. mem_heap_t* heap = NULL;
  2546. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  2547. ulint* offsets = offsets_;
  2548. rec_offs_init(offsets_);
  2549. fputs("--------------------------\n"
  2550. "INDEX TREE PRINT\n", stderr);
  2551. mtr_start(&mtr);
  2552. root = btr_root_block_get(index, &mtr);
  2553. btr_print_recursive(index, root, width, &heap, &offsets, &mtr);
  2554. if (UNIV_LIKELY_NULL(heap)) {
  2555. mem_heap_free(heap);
  2556. }
  2557. mtr_commit(&mtr);
  2558. btr_validate_index(index, NULL);
  2559. }
  2560. #endif /* UNIV_BTR_PRINT */
  2561. #ifdef UNIV_DEBUG
  2562. /************************************************************//**
  2563. Checks that the node pointer to a page is appropriate.
  2564. @return TRUE */
  2565. UNIV_INTERN
  2566. ibool
  2567. btr_check_node_ptr(
  2568. /*===============*/
  2569. dict_index_t* index, /*!< in: index tree */
  2570. buf_block_t* block, /*!< in: index page */
  2571. mtr_t* mtr) /*!< in: mtr */
  2572. {
  2573. mem_heap_t* heap;
  2574. dtuple_t* tuple;
  2575. ulint* offsets;
  2576. btr_cur_t cursor;
  2577. page_t* page = buf_block_get_frame(block);
  2578. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  2579. if (dict_index_get_page(index) == buf_block_get_page_no(block)) {
  2580. return(TRUE);
  2581. }
  2582. heap = mem_heap_create(256);
  2583. offsets = btr_page_get_father_block(NULL, heap, index, block, mtr,
  2584. &cursor);
  2585. if (page_is_leaf(page)) {
  2586. goto func_exit;
  2587. }
  2588. tuple = dict_index_build_node_ptr(
  2589. index, page_rec_get_next(page_get_infimum_rec(page)), 0, heap,
  2590. btr_page_get_level(page, mtr));
  2591. ut_a(!cmp_dtuple_rec(tuple, btr_cur_get_rec(&cursor), offsets));
  2592. func_exit:
  2593. mem_heap_free(heap);
  2594. return(TRUE);
  2595. }
  2596. #endif /* UNIV_DEBUG */
  2597. /************************************************************//**
  2598. Display identification information for a record. */
  2599. static
  2600. void
  2601. btr_index_rec_validate_report(
  2602. /*==========================*/
  2603. const page_t* page, /*!< in: index page */
  2604. const rec_t* rec, /*!< in: index record */
  2605. const dict_index_t* index) /*!< in: index */
  2606. {
  2607. fputs("InnoDB: Record in ", stderr);
  2608. dict_index_name_print(stderr, NULL, index);
  2609. fprintf(stderr, ", page %lu, at offset %lu\n",
  2610. page_get_page_no(page), (ulint) page_offset(rec));
  2611. }
  2612. /************************************************************//**
  2613. Checks the size and number of fields in a record based on the definition of
  2614. the index.
  2615. @return TRUE if ok */
  2616. UNIV_INTERN
  2617. ibool
  2618. btr_index_rec_validate(
  2619. /*===================*/
  2620. const rec_t* rec, /*!< in: index record */
  2621. const dict_index_t* index, /*!< in: index */
  2622. ibool dump_on_error) /*!< in: TRUE if the function
  2623. should print hex dump of record
  2624. and page on error */
  2625. {
  2626. ulint len;
  2627. ulint n;
  2628. ulint i;
  2629. const page_t* page;
  2630. mem_heap_t* heap = NULL;
  2631. ulint offsets_[REC_OFFS_NORMAL_SIZE];
  2632. ulint* offsets = offsets_;
  2633. rec_offs_init(offsets_);
  2634. page = page_align(rec);
  2635. if (UNIV_UNLIKELY(index->type & DICT_UNIVERSAL)) {
  2636. /* The insert buffer index tree can contain records from any
  2637. other index: we cannot check the number of fields or
  2638. their length */
  2639. return(TRUE);
  2640. }
  2641. if (UNIV_UNLIKELY((ibool)!!page_is_comp(page)
  2642. != dict_table_is_comp(index->table))) {
  2643. btr_index_rec_validate_report(page, rec, index);
  2644. fprintf(stderr, "InnoDB: compact flag=%lu, should be %lu\n",
  2645. (ulong) !!page_is_comp(page),
  2646. (ulong) dict_table_is_comp(index->table));
  2647. return(FALSE);
  2648. }
  2649. n = dict_index_get_n_fields(index);
  2650. if (!page_is_comp(page)
  2651. && UNIV_UNLIKELY(rec_get_n_fields_old(rec) != n)) {
  2652. btr_index_rec_validate_report(page, rec, index);
  2653. fprintf(stderr, "InnoDB: has %lu fields, should have %lu\n",
  2654. (ulong) rec_get_n_fields_old(rec), (ulong) n);
  2655. if (dump_on_error) {
  2656. buf_page_print(page, 0);
  2657. fputs("InnoDB: corrupt record ", stderr);
  2658. rec_print_old(stderr, rec);
  2659. putc('\n', stderr);
  2660. }
  2661. return(FALSE);
  2662. }
  2663. offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, &heap);
  2664. for (i = 0; i < n; i++) {
  2665. ulint fixed_size = dict_col_get_fixed_size(
  2666. dict_index_get_nth_col(index, i), page_is_comp(page));
  2667. rec_get_nth_field_offs(offsets, i, &len);
  2668. /* Note that if fixed_size != 0, it equals the
  2669. length of a fixed-size column in the clustered index.
  2670. A prefix index of the column is of fixed, but different
  2671. length. When fixed_size == 0, prefix_len is the maximum
  2672. length of the prefix index column. */
  2673. if ((dict_index_get_nth_field(index, i)->prefix_len == 0
  2674. && len != UNIV_SQL_NULL && fixed_size
  2675. && len != fixed_size)
  2676. || (dict_index_get_nth_field(index, i)->prefix_len > 0
  2677. && len != UNIV_SQL_NULL
  2678. && len
  2679. > dict_index_get_nth_field(index, i)->prefix_len)) {
  2680. btr_index_rec_validate_report(page, rec, index);
  2681. fprintf(stderr,
  2682. "InnoDB: field %lu len is %lu,"
  2683. " should be %lu\n",
  2684. (ulong) i, (ulong) len, (ulong) fixed_size);
  2685. if (dump_on_error) {
  2686. buf_page_print(page, 0);
  2687. fputs("InnoDB: corrupt record ", stderr);
  2688. rec_print_new(stderr, rec, offsets);
  2689. putc('\n', stderr);
  2690. }
  2691. if (UNIV_LIKELY_NULL(heap)) {
  2692. mem_heap_free(heap);
  2693. }
  2694. return(FALSE);
  2695. }
  2696. }
  2697. if (UNIV_LIKELY_NULL(heap)) {
  2698. mem_heap_free(heap);
  2699. }
  2700. return(TRUE);
  2701. }
  2702. /************************************************************//**
  2703. Checks the size and number of fields in records based on the definition of
  2704. the index.
  2705. @return TRUE if ok */
  2706. static
  2707. ibool
  2708. btr_index_page_validate(
  2709. /*====================*/
  2710. buf_block_t* block, /*!< in: index page */
  2711. dict_index_t* index) /*!< in: index */
  2712. {
  2713. page_cur_t cur;
  2714. ibool ret = TRUE;
  2715. page_cur_set_before_first(block, &cur);
  2716. page_cur_move_to_next(&cur);
  2717. for (;;) {
  2718. if (page_cur_is_after_last(&cur)) {
  2719. break;
  2720. }
  2721. if (!btr_index_rec_validate(cur.rec, index, TRUE)) {
  2722. return(FALSE);
  2723. }
  2724. page_cur_move_to_next(&cur);
  2725. }
  2726. return(ret);
  2727. }
  2728. /************************************************************//**
  2729. Report an error on one page of an index tree. */
  2730. static
  2731. void
  2732. btr_validate_report1(
  2733. /*=================*/
  2734. dict_index_t* index, /*!< in: index */
  2735. ulint level, /*!< in: B-tree level */
  2736. const buf_block_t* block) /*!< in: index page */
  2737. {
  2738. fprintf(stderr, "InnoDB: Error in page %lu of ",
  2739. buf_block_get_page_no(block));
  2740. dict_index_name_print(stderr, NULL, index);
  2741. if (level) {
  2742. fprintf(stderr, ", index tree level %lu", level);
  2743. }
  2744. putc('\n', stderr);
  2745. }
  2746. /************************************************************//**
  2747. Report an error on two pages of an index tree. */
  2748. static
  2749. void
  2750. btr_validate_report2(
  2751. /*=================*/
  2752. const dict_index_t* index, /*!< in: index */
  2753. ulint level, /*!< in: B-tree level */
  2754. const buf_block_t* block1, /*!< in: first index page */
  2755. const buf_block_t* block2) /*!< in: second index page */
  2756. {
  2757. fprintf(stderr, "InnoDB: Error in pages %lu and %lu of ",
  2758. buf_block_get_page_no(block1),
  2759. buf_block_get_page_no(block2));
  2760. dict_index_name_print(stderr, NULL, index);
  2761. if (level) {
  2762. fprintf(stderr, ", index tree level %lu", level);
  2763. }
  2764. putc('\n', stderr);
  2765. }
  2766. /************************************************************//**
  2767. Validates index tree level.
  2768. @return TRUE if ok */
  2769. static
  2770. ibool
  2771. btr_validate_level(
  2772. /*===============*/
  2773. dict_index_t* index, /*!< in: index tree */
  2774. trx_t* trx, /*!< in: transaction or NULL */
  2775. ulint level) /*!< in: level number */
  2776. {
  2777. ulint space;
  2778. ulint zip_size;
  2779. buf_block_t* block;
  2780. page_t* page;
  2781. buf_block_t* right_block = 0; /* remove warning */
  2782. page_t* right_page = 0; /* remove warning */
  2783. page_t* father_page;
  2784. btr_cur_t node_cur;
  2785. btr_cur_t right_node_cur;
  2786. rec_t* rec;
  2787. ulint right_page_no;
  2788. ulint left_page_no;
  2789. page_cur_t cursor;
  2790. dtuple_t* node_ptr_tuple;
  2791. ibool ret = TRUE;
  2792. mtr_t mtr;
  2793. mem_heap_t* heap = mem_heap_create(256);
  2794. ulint* offsets = NULL;
  2795. ulint* offsets2= NULL;
  2796. #ifdef UNIV_ZIP_DEBUG
  2797. page_zip_des_t* page_zip;
  2798. #endif /* UNIV_ZIP_DEBUG */
  2799. mtr_start(&mtr);
  2800. mtr_x_lock(dict_index_get_lock(index), &mtr);
  2801. block = btr_root_block_get(index, &mtr);
  2802. page = buf_block_get_frame(block);
  2803. space = dict_index_get_space(index);
  2804. zip_size = dict_table_zip_size(index->table);
  2805. while (level != btr_page_get_level(page, &mtr)) {
  2806. const rec_t* node_ptr;
  2807. ut_a(space == buf_block_get_space(block));
  2808. ut_a(space == page_get_space_id(page));
  2809. #ifdef UNIV_ZIP_DEBUG
  2810. page_zip = buf_block_get_page_zip(block);
  2811. ut_a(!page_zip || page_zip_validate(page_zip, page));
  2812. #endif /* UNIV_ZIP_DEBUG */
  2813. ut_a(!page_is_leaf(page));
  2814. page_cur_set_before_first(block, &cursor);
  2815. page_cur_move_to_next(&cursor);
  2816. node_ptr = page_cur_get_rec(&cursor);
  2817. offsets = rec_get_offsets(node_ptr, index, offsets,
  2818. ULINT_UNDEFINED, &heap);
  2819. block = btr_node_ptr_get_child(node_ptr, index, offsets, &mtr);
  2820. page = buf_block_get_frame(block);
  2821. }
  2822. /* Now we are on the desired level. Loop through the pages on that
  2823. level. */
  2824. loop:
  2825. if (trx_is_interrupted(trx)) {
  2826. mtr_commit(&mtr);
  2827. mem_heap_free(heap);
  2828. return(ret);
  2829. }
  2830. mem_heap_empty(heap);
  2831. offsets = offsets2 = NULL;
  2832. mtr_x_lock(dict_index_get_lock(index), &mtr);
  2833. #ifdef UNIV_ZIP_DEBUG
  2834. page_zip = buf_block_get_page_zip(block);
  2835. ut_a(!page_zip || page_zip_validate(page_zip, page));
  2836. #endif /* UNIV_ZIP_DEBUG */
  2837. /* Check ordering etc. of records */
  2838. if (!page_validate(page, index)) {
  2839. btr_validate_report1(index, level, block);
  2840. ret = FALSE;
  2841. } else if (level == 0) {
  2842. /* We are on level 0. Check that the records have the right
  2843. number of fields, and field lengths are right. */
  2844. if (!btr_index_page_validate(block, index)) {
  2845. ret = FALSE;
  2846. }
  2847. }
  2848. ut_a(btr_page_get_level(page, &mtr) == level);
  2849. right_page_no = btr_page_get_next(page, &mtr);
  2850. left_page_no = btr_page_get_prev(page, &mtr);
  2851. ut_a(page_get_n_recs(page) > 0 || (level == 0
  2852. && page_get_page_no(page)
  2853. == dict_index_get_page(index)));
  2854. if (right_page_no != FIL_NULL) {
  2855. const rec_t* right_rec;
  2856. right_block = btr_block_get(space, zip_size, right_page_no,
  2857. RW_X_LATCH, &mtr);
  2858. right_page = buf_block_get_frame(right_block);
  2859. if (UNIV_UNLIKELY(btr_page_get_prev(right_page, &mtr)
  2860. != page_get_page_no(page))) {
  2861. btr_validate_report2(index, level, block, right_block);
  2862. fputs("InnoDB: broken FIL_PAGE_NEXT"
  2863. " or FIL_PAGE_PREV links\n", stderr);
  2864. buf_page_print(page, 0);
  2865. buf_page_print(right_page, 0);
  2866. ret = FALSE;
  2867. }
  2868. if (UNIV_UNLIKELY(page_is_comp(right_page)
  2869. != page_is_comp(page))) {
  2870. btr_validate_report2(index, level, block, right_block);
  2871. fputs("InnoDB: 'compact' flag mismatch\n", stderr);
  2872. buf_page_print(page, 0);
  2873. buf_page_print(right_page, 0);
  2874. ret = FALSE;
  2875. goto node_ptr_fails;
  2876. }
  2877. rec = page_rec_get_prev(page_get_supremum_rec(page));
  2878. right_rec = page_rec_get_next(page_get_infimum_rec(
  2879. right_page));
  2880. offsets = rec_get_offsets(rec, index,
  2881. offsets, ULINT_UNDEFINED, &heap);
  2882. offsets2 = rec_get_offsets(right_rec, index,
  2883. offsets2, ULINT_UNDEFINED, &heap);
  2884. if (UNIV_UNLIKELY(cmp_rec_rec(rec, right_rec,
  2885. offsets, offsets2,
  2886. index) >= 0)) {
  2887. btr_validate_report2(index, level, block, right_block);
  2888. fputs("InnoDB: records in wrong order"
  2889. " on adjacent pages\n", stderr);
  2890. buf_page_print(page, 0);
  2891. buf_page_print(right_page, 0);
  2892. fputs("InnoDB: record ", stderr);
  2893. rec = page_rec_get_prev(page_get_supremum_rec(page));
  2894. rec_print(stderr, rec, index);
  2895. putc('\n', stderr);
  2896. fputs("InnoDB: record ", stderr);
  2897. rec = page_rec_get_next(
  2898. page_get_infimum_rec(right_page));
  2899. rec_print(stderr, rec, index);
  2900. putc('\n', stderr);
  2901. ret = FALSE;
  2902. }
  2903. }
  2904. if (level > 0 && left_page_no == FIL_NULL) {
  2905. ut_a(REC_INFO_MIN_REC_FLAG & rec_get_info_bits(
  2906. page_rec_get_next(page_get_infimum_rec(page)),
  2907. page_is_comp(page)));
  2908. }
  2909. if (buf_block_get_page_no(block) != dict_index_get_page(index)) {
  2910. /* Check father node pointers */
  2911. rec_t* node_ptr;
  2912. offsets = btr_page_get_father_block(offsets, heap, index,
  2913. block, &mtr, &node_cur);
  2914. father_page = btr_cur_get_page(&node_cur);
  2915. node_ptr = btr_cur_get_rec(&node_cur);
  2916. btr_cur_position(
  2917. index, page_rec_get_prev(page_get_supremum_rec(page)),
  2918. block, &node_cur);
  2919. offsets = btr_page_get_father_node_ptr(offsets, heap,
  2920. &node_cur, &mtr);
  2921. if (UNIV_UNLIKELY(node_ptr != btr_cur_get_rec(&node_cur))
  2922. || UNIV_UNLIKELY(btr_node_ptr_get_child_page_no(node_ptr,
  2923. offsets)
  2924. != buf_block_get_page_no(block))) {
  2925. btr_validate_report1(index, level, block);
  2926. fputs("InnoDB: node pointer to the page is wrong\n",
  2927. stderr);
  2928. buf_page_print(father_page, 0);
  2929. buf_page_print(page, 0);
  2930. fputs("InnoDB: node ptr ", stderr);
  2931. rec_print(stderr, node_ptr, index);
  2932. rec = btr_cur_get_rec(&node_cur);
  2933. fprintf(stderr, "\n"
  2934. "InnoDB: node ptr child page n:o %lu\n",
  2935. (ulong) btr_node_ptr_get_child_page_no(
  2936. rec, offsets));
  2937. fputs("InnoDB: record on page ", stderr);
  2938. rec_print_new(stderr, rec, offsets);
  2939. putc('\n', stderr);
  2940. ret = FALSE;
  2941. goto node_ptr_fails;
  2942. }
  2943. if (!page_is_leaf(page)) {
  2944. node_ptr_tuple = dict_index_build_node_ptr(
  2945. index,
  2946. page_rec_get_next(page_get_infimum_rec(page)),
  2947. 0, heap, btr_page_get_level(page, &mtr));
  2948. if (cmp_dtuple_rec(node_ptr_tuple, node_ptr,
  2949. offsets)) {
  2950. const rec_t* first_rec = page_rec_get_next(
  2951. page_get_infimum_rec(page));
  2952. btr_validate_report1(index, level, block);
  2953. buf_page_print(father_page, 0);
  2954. buf_page_print(page, 0);
  2955. fputs("InnoDB: Error: node ptrs differ"
  2956. " on levels > 0\n"
  2957. "InnoDB: node ptr ", stderr);
  2958. rec_print_new(stderr, node_ptr, offsets);
  2959. fputs("InnoDB: first rec ", stderr);
  2960. rec_print(stderr, first_rec, index);
  2961. putc('\n', stderr);
  2962. ret = FALSE;
  2963. goto node_ptr_fails;
  2964. }
  2965. }
  2966. if (left_page_no == FIL_NULL) {
  2967. ut_a(node_ptr == page_rec_get_next(
  2968. page_get_infimum_rec(father_page)));
  2969. ut_a(btr_page_get_prev(father_page, &mtr) == FIL_NULL);
  2970. }
  2971. if (right_page_no == FIL_NULL) {
  2972. ut_a(node_ptr == page_rec_get_prev(
  2973. page_get_supremum_rec(father_page)));
  2974. ut_a(btr_page_get_next(father_page, &mtr) == FIL_NULL);
  2975. } else {
  2976. const rec_t* right_node_ptr
  2977. = page_rec_get_next(node_ptr);
  2978. offsets = btr_page_get_father_block(
  2979. offsets, heap, index, right_block,
  2980. &mtr, &right_node_cur);
  2981. if (right_node_ptr
  2982. != page_get_supremum_rec(father_page)) {
  2983. if (btr_cur_get_rec(&right_node_cur)
  2984. != right_node_ptr) {
  2985. ret = FALSE;
  2986. fputs("InnoDB: node pointer to"
  2987. " the right page is wrong\n",
  2988. stderr);
  2989. btr_validate_report1(index, level,
  2990. block);
  2991. buf_page_print(father_page, 0);
  2992. buf_page_print(page, 0);
  2993. buf_page_print(right_page, 0);
  2994. }
  2995. } else {
  2996. page_t* right_father_page
  2997. = btr_cur_get_page(&right_node_cur);
  2998. if (btr_cur_get_rec(&right_node_cur)
  2999. != page_rec_get_next(
  3000. page_get_infimum_rec(
  3001. right_father_page))) {
  3002. ret = FALSE;
  3003. fputs("InnoDB: node pointer 2 to"
  3004. " the right page is wrong\n",
  3005. stderr);
  3006. btr_validate_report1(index, level,
  3007. block);
  3008. buf_page_print(father_page, 0);
  3009. buf_page_print(right_father_page, 0);
  3010. buf_page_print(page, 0);
  3011. buf_page_print(right_page, 0);
  3012. }
  3013. if (page_get_page_no(right_father_page)
  3014. != btr_page_get_next(father_page, &mtr)) {
  3015. ret = FALSE;
  3016. fputs("InnoDB: node pointer 3 to"
  3017. " the right page is wrong\n",
  3018. stderr);
  3019. btr_validate_report1(index, level,
  3020. block);
  3021. buf_page_print(father_page, 0);
  3022. buf_page_print(right_father_page, 0);
  3023. buf_page_print(page, 0);
  3024. buf_page_print(right_page, 0);
  3025. }
  3026. }
  3027. }
  3028. }
  3029. node_ptr_fails:
  3030. /* Commit the mini-transaction to release the latch on 'page'.
  3031. Re-acquire the latch on right_page, which will become 'page'
  3032. on the next loop. The page has already been checked. */
  3033. mtr_commit(&mtr);
  3034. if (right_page_no != FIL_NULL) {
  3035. mtr_start(&mtr);
  3036. block = btr_block_get(space, zip_size, right_page_no,
  3037. RW_X_LATCH, &mtr);
  3038. page = buf_block_get_frame(block);
  3039. goto loop;
  3040. }
  3041. mem_heap_free(heap);
  3042. return(ret);
  3043. }
  3044. /**************************************************************//**
  3045. Checks the consistency of an index tree.
  3046. @return TRUE if ok */
  3047. UNIV_INTERN
  3048. ibool
  3049. btr_validate_index(
  3050. /*===============*/
  3051. dict_index_t* index, /*!< in: index */
  3052. trx_t* trx) /*!< in: transaction or NULL */
  3053. {
  3054. mtr_t mtr;
  3055. page_t* root;
  3056. ulint i;
  3057. ulint n;
  3058. mtr_start(&mtr);
  3059. mtr_x_lock(dict_index_get_lock(index), &mtr);
  3060. root = btr_root_get(index, &mtr);
  3061. n = btr_page_get_level(root, &mtr);
  3062. for (i = 0; i <= n && !trx_is_interrupted(trx); i++) {
  3063. if (!btr_validate_level(index, trx, n - i)) {
  3064. mtr_commit(&mtr);
  3065. return(FALSE);
  3066. }
  3067. }
  3068. mtr_commit(&mtr);
  3069. return(TRUE);
  3070. }
  3071. #endif /* !UNIV_HOTBACKUP */