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.

299 lines
8.6 KiB

  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 include/page0cur.ic
  15. The page cursor
  16. Created 10/4/1994 Heikki Tuuri
  17. *************************************************************************/
  18. #include "page0page.h"
  19. #include "buf0types.h"
  20. #ifdef UNIV_DEBUG
  21. /*********************************************************//**
  22. Gets pointer to the page frame where the cursor is positioned.
  23. @return page */
  24. UNIV_INLINE
  25. page_t*
  26. page_cur_get_page(
  27. /*==============*/
  28. page_cur_t* cur) /*!< in: page cursor */
  29. {
  30. ut_ad(cur);
  31. ut_ad(page_align(cur->rec) == cur->block->frame);
  32. return(page_align(cur->rec));
  33. }
  34. /*********************************************************//**
  35. Gets pointer to the buffer block where the cursor is positioned.
  36. @return page */
  37. UNIV_INLINE
  38. buf_block_t*
  39. page_cur_get_block(
  40. /*===============*/
  41. page_cur_t* cur) /*!< in: page cursor */
  42. {
  43. ut_ad(cur);
  44. ut_ad(page_align(cur->rec) == cur->block->frame);
  45. return(cur->block);
  46. }
  47. /*********************************************************//**
  48. Gets pointer to the page frame where the cursor is positioned.
  49. @return page */
  50. UNIV_INLINE
  51. page_zip_des_t*
  52. page_cur_get_page_zip(
  53. /*==================*/
  54. page_cur_t* cur) /*!< in: page cursor */
  55. {
  56. return(buf_block_get_page_zip(page_cur_get_block(cur)));
  57. }
  58. /*********************************************************//**
  59. Gets the record where the cursor is positioned.
  60. @return record */
  61. UNIV_INLINE
  62. rec_t*
  63. page_cur_get_rec(
  64. /*=============*/
  65. page_cur_t* cur) /*!< in: page cursor */
  66. {
  67. ut_ad(cur);
  68. ut_ad(page_align(cur->rec) == cur->block->frame);
  69. return(cur->rec);
  70. }
  71. #endif /* UNIV_DEBUG */
  72. /*********************************************************//**
  73. Sets the cursor object to point before the first user record
  74. on the page. */
  75. UNIV_INLINE
  76. void
  77. page_cur_set_before_first(
  78. /*======================*/
  79. const buf_block_t* block, /*!< in: index page */
  80. page_cur_t* cur) /*!< in: cursor */
  81. {
  82. cur->block = (buf_block_t*) block;
  83. cur->rec = page_get_infimum_rec(buf_block_get_frame(cur->block));
  84. }
  85. /*********************************************************//**
  86. Sets the cursor object to point after the last user record on
  87. the page. */
  88. UNIV_INLINE
  89. void
  90. page_cur_set_after_last(
  91. /*====================*/
  92. const buf_block_t* block, /*!< in: index page */
  93. page_cur_t* cur) /*!< in: cursor */
  94. {
  95. cur->block = (buf_block_t*) block;
  96. cur->rec = page_get_supremum_rec(buf_block_get_frame(cur->block));
  97. }
  98. /*********************************************************//**
  99. Returns TRUE if the cursor is before first user record on page.
  100. @return TRUE if at start */
  101. UNIV_INLINE
  102. ibool
  103. page_cur_is_before_first(
  104. /*=====================*/
  105. const page_cur_t* cur) /*!< in: cursor */
  106. {
  107. ut_ad(cur);
  108. ut_ad(page_align(cur->rec) == cur->block->frame);
  109. return(page_rec_is_infimum(cur->rec));
  110. }
  111. /*********************************************************//**
  112. Returns TRUE if the cursor is after last user record.
  113. @return TRUE if at end */
  114. UNIV_INLINE
  115. ibool
  116. page_cur_is_after_last(
  117. /*===================*/
  118. const page_cur_t* cur) /*!< in: cursor */
  119. {
  120. ut_ad(cur);
  121. ut_ad(page_align(cur->rec) == cur->block->frame);
  122. return(page_rec_is_supremum(cur->rec));
  123. }
  124. /**********************************************************//**
  125. Positions the cursor on the given record. */
  126. UNIV_INLINE
  127. void
  128. page_cur_position(
  129. /*==============*/
  130. const rec_t* rec, /*!< in: record on a page */
  131. const buf_block_t* block, /*!< in: buffer block containing
  132. the record */
  133. page_cur_t* cur) /*!< out: page cursor */
  134. {
  135. ut_ad(rec && block && cur);
  136. ut_ad(page_align(rec) == block->frame);
  137. cur->rec = (rec_t*) rec;
  138. cur->block = (buf_block_t*) block;
  139. }
  140. /**********************************************************//**
  141. Invalidates a page cursor by setting the record pointer NULL. */
  142. UNIV_INLINE
  143. void
  144. page_cur_invalidate(
  145. /*================*/
  146. page_cur_t* cur) /*!< out: page cursor */
  147. {
  148. ut_ad(cur);
  149. cur->rec = NULL;
  150. cur->block = NULL;
  151. }
  152. /**********************************************************//**
  153. Moves the cursor to the next record on page. */
  154. UNIV_INLINE
  155. void
  156. page_cur_move_to_next(
  157. /*==================*/
  158. page_cur_t* cur) /*!< in/out: cursor; must not be after last */
  159. {
  160. ut_ad(!page_cur_is_after_last(cur));
  161. cur->rec = page_rec_get_next(cur->rec);
  162. }
  163. /**********************************************************//**
  164. Moves the cursor to the previous record on page. */
  165. UNIV_INLINE
  166. void
  167. page_cur_move_to_prev(
  168. /*==================*/
  169. page_cur_t* cur) /*!< in/out: page cursor, not before first */
  170. {
  171. ut_ad(!page_cur_is_before_first(cur));
  172. cur->rec = page_rec_get_prev(cur->rec);
  173. }
  174. #ifndef UNIV_HOTBACKUP
  175. /****************************************************************//**
  176. Searches the right position for a page cursor.
  177. @return number of matched fields on the left */
  178. UNIV_INLINE
  179. ulint
  180. page_cur_search(
  181. /*============*/
  182. const buf_block_t* block, /*!< in: buffer block */
  183. const dict_index_t* index, /*!< in: record descriptor */
  184. const dtuple_t* tuple, /*!< in: data tuple */
  185. ulint mode, /*!< in: PAGE_CUR_L,
  186. PAGE_CUR_LE, PAGE_CUR_G, or
  187. PAGE_CUR_GE */
  188. page_cur_t* cursor) /*!< out: page cursor */
  189. {
  190. ulint low_matched_fields = 0;
  191. ulint low_matched_bytes = 0;
  192. ulint up_matched_fields = 0;
  193. ulint up_matched_bytes = 0;
  194. ut_ad(dtuple_check_typed(tuple));
  195. page_cur_search_with_match(block, index, tuple, mode,
  196. &up_matched_fields,
  197. &up_matched_bytes,
  198. &low_matched_fields,
  199. &low_matched_bytes,
  200. cursor);
  201. return(low_matched_fields);
  202. }
  203. /***********************************************************//**
  204. Inserts a record next to page cursor. Returns pointer to inserted record if
  205. succeed, i.e., enough space available, NULL otherwise. The cursor stays at
  206. the same logical position, but the physical position may change if it is
  207. pointing to a compressed page that was reorganized.
  208. @return pointer to record if succeed, NULL otherwise */
  209. UNIV_INLINE
  210. rec_t*
  211. page_cur_tuple_insert(
  212. /*==================*/
  213. page_cur_t* cursor, /*!< in/out: a page cursor */
  214. const dtuple_t* tuple, /*!< in: pointer to a data tuple */
  215. dict_index_t* index, /*!< in: record descriptor */
  216. ulint n_ext, /*!< in: number of externally stored columns */
  217. mtr_t* mtr) /*!< in: mini-transaction handle, or NULL */
  218. {
  219. mem_heap_t* heap;
  220. ulint* offsets;
  221. ulint size
  222. = rec_get_converted_size(index, tuple, n_ext);
  223. rec_t* rec;
  224. heap = mem_heap_create(size
  225. + (4 + REC_OFFS_HEADER_SIZE
  226. + dtuple_get_n_fields(tuple))
  227. * sizeof *offsets);
  228. rec = rec_convert_dtuple_to_rec((byte*) mem_heap_alloc(heap, size),
  229. index, tuple, n_ext);
  230. offsets = rec_get_offsets(rec, index, NULL, ULINT_UNDEFINED, &heap);
  231. if (buf_block_get_page_zip(cursor->block)) {
  232. rec = page_cur_insert_rec_zip(&cursor->rec, cursor->block,
  233. index, rec, offsets, mtr);
  234. } else {
  235. rec = page_cur_insert_rec_low(cursor->rec,
  236. index, rec, offsets, mtr);
  237. }
  238. mem_heap_free(heap);
  239. return(rec);
  240. }
  241. #endif /* !UNIV_HOTBACKUP */
  242. /***********************************************************//**
  243. Inserts a record next to page cursor. Returns pointer to inserted record if
  244. succeed, i.e., enough space available, NULL otherwise. The cursor stays at
  245. the same logical position, but the physical position may change if it is
  246. pointing to a compressed page that was reorganized.
  247. @return pointer to record if succeed, NULL otherwise */
  248. UNIV_INLINE
  249. rec_t*
  250. page_cur_rec_insert(
  251. /*================*/
  252. page_cur_t* cursor, /*!< in/out: a page cursor */
  253. const rec_t* rec, /*!< in: record to insert */
  254. dict_index_t* index, /*!< in: record descriptor */
  255. ulint* offsets,/*!< in/out: rec_get_offsets(rec, index) */
  256. mtr_t* mtr) /*!< in: mini-transaction handle, or NULL */
  257. {
  258. if (buf_block_get_page_zip(cursor->block)) {
  259. return(page_cur_insert_rec_zip(&cursor->rec, cursor->block,
  260. index, rec, offsets, mtr));
  261. } else {
  262. return(page_cur_insert_rec_low(cursor->rec,
  263. index, rec, offsets, mtr));
  264. }
  265. }