Debian Codegraph @

storage/innobase/include/btr0btr.ic

raw
1 /*****************************************************************************
2
3 Copyright (c) 1994, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/btr0btr.ic
29 The B-tree
30
31 Created 6/2/1994 Heikki Tuuri
32 *******************************************************/
33
34 #include "mach0data.h"
35 #ifndef UNIV_HOTBACKUP
36 #include "mtr0log.h"
37 #include "mtr0mtr.h"
38 #include "page0zip.h"
39 #endif /* !UNIV_HOTBACKUP */
40
41 /** NOTE - Changing this from the original number of 50 to 45 as
42 insert_debug.test was failing in ASAN build because of a stack overflow issue.
43 It was found that rtr_info_t was taking up a lot of stack space in the function
44 btr_insert_on_non_leaf_level_func which is part of the recursive stack
45 trace. */
46 /** Maximum B-tree page level (not really a hard limit). Used in debug
47 assertions in btr_page_set_level and btr_page_get_level */
48 constexpr uint32_t BTR_MAX_NODE_LEVEL = 45;
49
50 #ifndef UNIV_HOTBACKUP
51 static inline buf_block_t *btr_block_get_func(
52 const page_id_t &page_id, const page_size_t &page_size, ulint mode,
53 ut::Location location, IF_DEBUG(const dict_index_t *index, ) mtr_t *mtr) {
54 buf_block_t *block;
55
56 block = buf_page_get_gen(page_id, page_size, mode, nullptr,
57 Page_fetch::NORMAL, location, mtr);
58
59 if (mode != RW_NO_LATCH) {
60 buf_block_dbg_add_level(block, index != nullptr && dict_index_is_ibuf(index)
61 ? SYNC_IBUF_TREE_NODE
62 : SYNC_TREE_NODE);
63 }
64
65 return (block);
66 }
67
68 /** Sets the index id field of a page. */
69 static inline void btr_page_set_index_id(
70 page_t *page, /*!< in: page to be created */
71 page_zip_des_t *page_zip, /*!< in: compressed page whose uncompressed
72 part will be updated, or NULL */
73 space_index_t id, /*!< in: index id */
74 mtr_t *mtr) /*!< in: mtr */
75 {
76 if (page_zip) {
77 mach_write_to_8(page + (PAGE_HEADER + PAGE_INDEX_ID), id);
78 page_zip_write_header(page_zip, page + (PAGE_HEADER + PAGE_INDEX_ID), 8,
79 mtr);
80 } else {
81 mlog_write_ull(page + (PAGE_HEADER + PAGE_INDEX_ID), id, mtr);
82 }
83 }
84 #endif /* !UNIV_HOTBACKUP */
85
86 /** Gets the index id field of a page.
87 @return index id */
88 static inline space_index_t btr_page_get_index_id(
89 const page_t *page) /*!< in: index page */
90 {
91 return (mach_read_from_8(page + PAGE_HEADER + PAGE_INDEX_ID));
92 }
93
94 /** Gets the node level field in an index page.
95 @param[in] page index page
96 @return level, leaf level == 0 */
97 static inline ulint btr_page_get_level(const page_t *page) {
98 ulint level;
99
100 ut_ad(page);
101
102 level = mach_read_from_2(page + PAGE_HEADER + PAGE_LEVEL);
103
104 ut_ad(level <= BTR_MAX_NODE_LEVEL);
105
106 return (level);
107 }
108
109 /** Sets the node level field in an index page. */
110 static inline void btr_page_set_level(
111 page_t *page, /*!< in: index page */
112 page_zip_des_t *page_zip, /*!< in: compressed page whose uncompressed
113 part will be updated, or NULL */
114 ulint level, /*!< in: level, leaf level == 0 */
115 mtr_t *mtr) /*!< in: mini-transaction handle */
116 {
117 ut_ad(page != nullptr);
118 ut_ad(mtr != nullptr);
119 ut_ad(level <= BTR_MAX_NODE_LEVEL);
120
121 if (page_zip) {
122 mach_write_to_2(page + (PAGE_HEADER + PAGE_LEVEL), level);
123 page_zip_write_header(page_zip, page + (PAGE_HEADER + PAGE_LEVEL), 2, mtr);
124 } else {
125 mlog_write_ulint(page + (PAGE_HEADER + PAGE_LEVEL), level, MLOG_2BYTES,
126 mtr);
127 }
128 }
129
130 static inline page_no_t btr_page_get_next(const page_t *page,
131 mtr_t *mtr [[maybe_unused]]) {
132 ut_ad(page != nullptr);
133 ut_ad(mtr != nullptr);
134
135 return (mach_read_from_4(page + FIL_PAGE_NEXT));
136 }
137
138 /** Sets the next index page field. */
139 static inline void btr_page_set_next(
140 page_t *page, /*!< in: index page */
141 page_zip_des_t *page_zip, /*!< in: compressed page whose uncompressed
142 part will be updated, or NULL */
143 page_no_t next, /*!< in: next page number */
144 mtr_t *mtr) /*!< in: mini-transaction handle */
145 {
146 ut_ad(page != nullptr);
147 ut_ad(mtr != nullptr);
148
149 if (page_zip) {
150 mach_write_to_4(page + FIL_PAGE_NEXT, next);
151 page_zip_write_header(page_zip, page + FIL_PAGE_NEXT, 4, mtr);
152 } else {
153 mlog_write_ulint(page + FIL_PAGE_NEXT, next, MLOG_4BYTES, mtr);
154 }
155 }
156
157 static inline page_no_t btr_page_get_prev(const page_t *page) {
158 ut_ad(page != nullptr);
159 return (mach_read_from_4(page + FIL_PAGE_PREV));
160 }
161
162 static inline page_no_t btr_page_get_prev(const page_t *page,
163 mtr_t *mtr [[maybe_unused]]) {
164 ut_ad(mtr != nullptr);
165 return btr_page_get_prev(page);
166 }
167
168 /** Sets the previous index page field. */
169 static inline void btr_page_set_prev(
170 page_t *page, /*!< in: index page */
171 page_zip_des_t *page_zip, /*!< in: compressed page whose uncompressed
172 part will be updated, or NULL */
173 page_no_t prev, /*!< in: previous page number */
174 mtr_t *mtr) /*!< in: mini-transaction handle */
175 {
176 ut_ad(page != nullptr);
177 ut_ad(mtr != nullptr);
178
179 if (page_zip) {
180 mach_write_to_4(page + FIL_PAGE_PREV, prev);
181 page_zip_write_header(page_zip, page + FIL_PAGE_PREV, 4, mtr);
182 } else {
183 mlog_write_ulint(page + FIL_PAGE_PREV, prev, MLOG_4BYTES, mtr);
184 }
185 }
186
187 static inline page_no_t btr_node_ptr_get_child_page_no(const rec_t *rec,
188 const ulint *offsets) {
189 const byte *field;
190 ulint len;
191 page_no_t page_no;
192
193 ut_ad(!rec_offs_comp(offsets) || rec_get_node_ptr_flag(rec));
194
195 /* The child address is in the last field */
196 field = rec_get_nth_field(nullptr, rec, offsets,
197 rec_offs_n_fields(offsets) - 1, &len);
198
199 ut_ad(len == 4);
200
201 page_no = mach_read_from_4(field);
202 ut_ad(page_no > 1);
203
204 return (page_no);
205 }
206
207 /** Check if the given page is first page on level.
208 @param[in] page Btree page
209 @return true if it is first page */
210 static inline bool btr_is_first_page_on_level(const page_t *page) {
211 return (btr_page_get_prev(page) == FIL_NULL);
212 }
213
214 #ifndef UNIV_HOTBACKUP
215 /** Releases the latches on a leaf page and bufferunfixes it. */
216 static inline void btr_leaf_page_release(
217 buf_block_t *block, /*!< in: buffer block */
218 ulint latch_mode, /*!< in: BTR_SEARCH_LEAF or
219 BTR_MODIFY_LEAF */
220 mtr_t *mtr) /*!< in: mtr */
221 {
222 ut_ad(latch_mode == BTR_SEARCH_LEAF || latch_mode == BTR_MODIFY_LEAF ||
223 latch_mode == BTR_NO_LATCHES);
224
225 ut_ad(!mtr_memo_contains(mtr, block, MTR_MEMO_MODIFY));
226
227 ulint mode;
228 switch (latch_mode) {
229 case BTR_SEARCH_LEAF:
230 mode = MTR_MEMO_PAGE_S_FIX;
231 break;
232 case BTR_MODIFY_LEAF:
233 mode = MTR_MEMO_PAGE_X_FIX;
234 break;
235 case BTR_NO_LATCHES:
236 mode = MTR_MEMO_BUF_FIX;
237 break;
238 default:
239 ut_error;
240 }
241
242 mtr->memo_release(block, mode);
243 }
244
245 #endif /* !UNIV_HOTBACKUP */