Debian Codegraph @

storage/innobase/include/trx0undo.ic

raw
1 /*****************************************************************************
2
3 Copyright (c) 1996, 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/trx0undo.ic
29 Transaction undo log
30
31 Created 3/26/1996 Heikki Tuuri
32 *******************************************************/
33
34 #include "data0type.h"
35 #include "page0page.h"
36 #include "trx0purge.h"
37
38 #ifndef UNIV_HOTBACKUP
39 /** Build a roll pointer.
40 @param[in] is_insert true if insert undo log
41 @param[in] space_id space ID of the rollback segment
42 @param[in] page_no page number
43 @param[in] offset offset of the undo entry within page
44 @return roll pointer */
45 inline roll_ptr_t trx_undo_build_roll_ptr(bool is_insert, space_id_t space_id,
46 page_no_t page_no, ulint offset) {
47 roll_ptr_t roll_ptr;
48 ulint id;
49
50 ut_ad(offset < 65536);
51
52 id = (fsp_is_undo_tablespace(space_id) ? undo::id2num(space_id) : 0);
53
54 roll_ptr = (roll_ptr_t)is_insert << 55 | (roll_ptr_t)id << 48 |
55 (roll_ptr_t)page_no << 16 | offset;
56 return (roll_ptr);
57 }
58
59 /** Decodes a roll pointer. */
60 inline void trx_undo_decode_roll_ptr(
61 roll_ptr_t roll_ptr, /*!< in: roll pointer */
62 bool *is_insert, /*!< out: true if insert undo log */
63 ulint *rseg_id, /*!< out: rollback segment id */
64 page_no_t *page_no, /*!< out: page number */
65 ulint *offset) /*!< out: offset of the undo
66 entry within page */
67 {
68 ut_ad(roll_ptr < (1ULL << 56));
69 *offset = (ulint)roll_ptr & 0xFFFF;
70 roll_ptr >>= 16;
71 *page_no = (ulint)roll_ptr & 0xFFFFFFFF;
72 roll_ptr >>= 32;
73 *rseg_id = (ulint)roll_ptr & 0x7F;
74 roll_ptr >>= 7;
75 *is_insert = roll_ptr; /* true==1 */
76 }
77
78 /** Returns true if the roll pointer is of the insert type.
79 @return true if insert undo log */
80 static inline bool trx_undo_roll_ptr_is_insert(
81 roll_ptr_t roll_ptr) /*!< in: roll pointer */
82 {
83 ut_ad(roll_ptr < (1ULL << 56));
84 return roll_ptr >> 55;
85 }
86
87 /** Returns true if the record is of the insert type.
88 @return true if the record was freshly inserted (not updated). */
89 static inline bool trx_undo_trx_id_is_insert(
90 const byte *trx_id) /*!< in: DB_TRX_ID, followed by DB_ROLL_PTR */
91 {
92 return (static_cast<bool>(trx_id[DATA_TRX_ID_LEN] >> 7));
93 }
94 #endif /* !UNIV_HOTBACKUP */
95
96 /** Writes a roll ptr to an index page. In case that the size changes in
97 some future version, this function should be used instead of
98 mach_write_... */
99 static inline void trx_write_roll_ptr(byte *ptr, /*!< in: pointer to memory
100 where written */
101 roll_ptr_t roll_ptr) /*!< in: roll ptr */
102 {
103 mach_write_to_7(ptr, roll_ptr);
104 }
105
106 /** Reads a roll ptr from an index page. In case that the roll ptr size
107 changes in some future version, this function should be used instead of
108 mach_read_...
109 @return roll ptr */
110 static inline roll_ptr_t trx_read_roll_ptr(
111 const byte *ptr) /*!< in: pointer to memory from where to read */
112 {
113 return (mach_read_from_7(ptr));
114 }
115
116 #ifndef UNIV_HOTBACKUP
117
118 /** Gets an undo log page and x-latches it.
119 @param[in] page_id Page id
120 @param[in] page_size Page size
121 @param[in,out] mtr Mini-transaction
122 @return pointer to page x-latched */
123 static inline page_t *trx_undo_page_get(const page_id_t &page_id,
124 const page_size_t &page_size,
125 mtr_t *mtr) {
126 buf_block_t *block =
127 buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, mtr);
128 buf_block_dbg_add_level(block, SYNC_TRX_UNDO_PAGE);
129
130 return (buf_block_get_frame(block));
131 }
132
133 /** Gets an undo log page and s-latches it.
134 @param[in] page_id Page id
135 @param[in] page_size Page size
136 @param[in,out] mtr Mini-transaction
137 @return pointer to page s-latched */
138 static inline page_t *trx_undo_page_get_s_latched(const page_id_t &page_id,
139 const page_size_t &page_size,
140 mtr_t *mtr) {
141 buf_block_t *block =
142 buf_page_get(page_id, page_size, RW_S_LATCH, UT_LOCATION_HERE, mtr);
143 buf_block_dbg_add_level(block, SYNC_TRX_UNDO_PAGE);
144
145 return (buf_block_get_frame(block));
146 }
147
148 /** Returns the start offset of the undo log records of the specified undo
149 log on the page.
150 @return start offset */
151 static inline ulint trx_undo_page_get_start(
152 page_t *undo_page, /*!< in: undo log page */
153 page_no_t page_no, /*!< in: undo log header page number */
154 ulint offset) /*!< in: undo log header offset on page */
155 {
156 ulint start;
157
158 if (page_no == page_get_page_no(undo_page)) {
159 start = mach_read_from_2(offset + undo_page + TRX_UNDO_LOG_START);
160 } else {
161 start = TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_HDR_SIZE;
162 }
163
164 return (start);
165 }
166
167 /** Returns the end offset of the undo log records of the specified undo
168 log on the page.
169 @return end offset */
170 static inline ulint trx_undo_page_get_end(
171 page_t *undo_page, /*!< in: undo log page */
172 page_no_t page_no, /*!< in: undo log header page number */
173 ulint offset) /*!< in: undo log header offset on page */
174 {
175 trx_ulogf_t *log_hdr;
176 ulint end;
177
178 if (page_no == page_get_page_no(undo_page)) {
179 log_hdr = undo_page + offset;
180
181 end = mach_read_from_2(log_hdr + TRX_UNDO_NEXT_LOG);
182
183 if (end == 0) {
184 end =
185 mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE);
186 }
187 } else {
188 end = mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE);
189 }
190
191 return (end);
192 }
193
194 /** Returns the previous undo record on the page in the specified log, or
195 NULL if none exists.
196 @return pointer to record, NULL if none */
197 static inline trx_undo_rec_t *trx_undo_page_get_prev_rec(
198 trx_undo_rec_t *rec, /*!< in: undo log record */
199 page_no_t page_no, /*!< in: undo log header page number */
200 ulint offset) /*!< in: undo log header offset on page */
201 {
202 page_t *undo_page;
203 ulint start;
204
205 undo_page = (page_t *)ut_align_down(rec, UNIV_PAGE_SIZE);
206
207 start = trx_undo_page_get_start(undo_page, page_no, offset);
208
209 if (start + undo_page == rec) {
210 return (nullptr);
211 }
212
213 return (undo_page + mach_read_from_2(rec - 2));
214 }
215
216 /** Returns the next undo log record on the page in the specified log, or
217 NULL if none exists.
218 @return pointer to record, NULL if none */
219 static inline trx_undo_rec_t *trx_undo_page_get_next_rec(
220 trx_undo_rec_t *rec, /*!< in: undo log record */
221 page_no_t page_no, /*!< in: undo log header page number */
222 ulint offset) /*!< in: undo log header offset on page */
223 {
224 page_t *undo_page;
225 ulint end;
226 ulint next;
227
228 undo_page = (page_t *)ut_align_down(rec, UNIV_PAGE_SIZE);
229
230 end = trx_undo_page_get_end(undo_page, page_no, offset);
231
232 next = mach_read_from_2(rec);
233
234 if (next == end) {
235 return (nullptr);
236 }
237
238 return (undo_page + next);
239 }
240
241 /** Returns the last undo record on the page in the specified undo log, or
242 NULL if none exists.
243 @return pointer to record, NULL if none */
244 static inline trx_undo_rec_t *trx_undo_page_get_last_rec(
245 page_t *undo_page, /*!< in: undo log page */
246 page_no_t page_no, /*!< in: undo log header page number */
247 ulint offset) /*!< in: undo log header offset on page */
248 {
249 ulint start;
250 ulint end;
251
252 start = trx_undo_page_get_start(undo_page, page_no, offset);
253 end = trx_undo_page_get_end(undo_page, page_no, offset);
254
255 if (start == end) {
256 return (nullptr);
257 }
258
259 return (undo_page + mach_read_from_2(undo_page + end - 2));
260 }
261
262 /** Returns the first undo record on the page in the specified undo log, or
263 NULL if none exists.
264 @return pointer to record, NULL if none */
265 static inline trx_undo_rec_t *trx_undo_page_get_first_rec(
266 page_t *undo_page, /*!< in: undo log page */
267 page_no_t page_no, /*!< in: undo log header page number */
268 ulint offset) /*!< in: undo log header offset on page */
269 {
270 ulint start;
271 ulint end;
272
273 start = trx_undo_page_get_start(undo_page, page_no, offset);
274 end = trx_undo_page_get_end(undo_page, page_no, offset);
275
276 if (start == end) {
277 return (nullptr);
278 }
279
280 return (undo_page + start);
281 }
282
283 /* Set undo segment to prepared state and set XID
284 @param[in] in_xid transaction XID. */
285 inline void trx_undo_t::set_prepared(const XID *in_xid) {
286 state = TRX_UNDO_PREPARED;
287 xid = *in_xid;
288 flag |= TRX_UNDO_FLAG_XID;
289 }
290
291 inline void trx_undo_t::set_prepared_in_tc() {
292 ut_ad(!xid.is_null());
293 state = TRX_UNDO_PREPARED_IN_TC;
294 }
295
296 inline bool trx_undo_t::is_prepared() const {
297 return state == TRX_UNDO_PREPARED_80028 || state == TRX_UNDO_PREPARED ||
298 state == TRX_UNDO_PREPARED_IN_TC;
299 }
300
301 #endif /* !UNIV_HOTBACKUP */