Debian Codegraph @

storage/innobase/include/rem0rec.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/rem0rec.ic
29 Record manager
30
31 Created 5/30/1994 Heikki Tuuri
32 *************************************************************************/
33
34 #include "btr0types.h"
35 #include "dict0boot.h"
36 #include "dict0dict.h"
37 #include "mach0data.h"
38 #include "ut0byte.h"
39
40 /* Offsets of the bit-fields in an old-style record. NOTE! In the table the
41 most significant bytes and bits are written below less significant.
42
43 (1) byte offset (2) bit usage within byte
44 downward from
45 origin -> 1 8 bits pointer to next record
46 2 8 bits pointer to next record
47 3 1 bit short flag
48 7 bits number of fields
49 4 3 bits number of fields
50 5 bits heap number
51 5 8 bits heap number
52 6 4 bits n_owned
53 4 bits info bits
54 */
55
56 /* Offsets of the bit-fields in a new-style record. NOTE! In the table the
57 most significant bytes and bits are written below less significant.
58
59 (1) byte offset (2) bit usage within byte
60 downward from
61 origin -> 1 8 bits relative offset of next record
62 2 8 bits relative offset of next record
63 the relative offset is an unsigned 16-bit
64 integer:
65 (offset_of_next_record
66 - offset_of_this_record) mod 64Ki,
67 where mod is the modulo as a non-negative
68 number;
69 we can calculate the offset of the next
70 record with the formula:
71 relative_offset + offset_of_this_record
72 mod UNIV_PAGE_SIZE
73 3 3 bits status:
74 000=conventional record
75 001=node pointer record (inside B-tree)
76 010=infimum record
77 011=supremum record
78 1xx=reserved
79 5 bits heap number
80 4 8 bits heap number
81 5 4 bits n_owned
82 4 bits info bits
83 */
84
85 /** Sets a bit field within 1 byte. */
86 static inline void rec_set_bit_field_1(
87 rec_t *rec, /*!< in: pointer to record origin */
88 ulint val, /*!< in: value to set */
89 ulint offs, /*!< in: offset from the origin down */
90 ulint mask, /*!< in: mask used to filter bits */
91 ulint shift) /*!< in: shift right applied after masking */
92 {
93 ut_ad(rec);
94 ut_ad(offs <= REC_N_OLD_EXTRA_BYTES);
95 ut_ad(mask);
96 ut_ad(mask <= 0xFFUL);
97 ut_ad(((mask >> shift) << shift) == mask);
98 ut_ad(((val << shift) & mask) == (val << shift));
99
100 mach_write_to_1(rec - offs,
101 (mach_read_from_1(rec - offs) & ~mask) | (val << shift));
102 }
103
104 /** Sets a bit field within 2 bytes. */
105 static inline void rec_set_bit_field_2(
106 rec_t *rec, /*!< in: pointer to record origin */
107 ulint val, /*!< in: value to set */
108 ulint offs, /*!< in: offset from the origin down */
109 ulint mask, /*!< in: mask used to filter bits */
110 ulint shift) /*!< in: shift right applied after masking */
111 {
112 ut_ad(rec);
113 ut_ad(offs <= REC_N_OLD_EXTRA_BYTES);
114 ut_ad(mask > 0xFFUL);
115 ut_ad(mask <= 0xFFFFUL);
116 ut_ad((mask >> shift) & 1);
117 ut_ad(0 == ((mask >> shift) & ((mask >> shift) + 1)));
118 ut_ad(((mask >> shift) << shift) == mask);
119 ut_ad(((val << shift) & mask) == (val << shift));
120
121 mach_write_to_2(rec - offs,
122 (mach_read_from_2(rec - offs) & ~mask) | (val << shift));
123 }
124
125 static inline const rec_t *rec_get_next_ptr_const(const rec_t *rec,
126 ulint comp) {
127 static_assert(REC_NEXT_MASK == 0xFFFFUL);
128 static_assert(REC_NEXT_SHIFT == 0);
129
130 const auto field_value = mach_read_from_2(rec - REC_NEXT);
131
132 if (field_value == 0) {
133 return (nullptr);
134 }
135
136 if (comp) {
137 /** Check if the result offset is still on the same page. We allow
138 `field_value` to be interpreted as negative 16bit integer. This check does
139 nothing for 64KB pages. */
140 ut_ad(static_cast<uint16_t>(field_value +
141 ut_align_offset(rec, UNIV_PAGE_SIZE)));
142
143 /* There must be at least REC_N_NEW_EXTRA_BYTES + 1
144 between each record. */
145 ut_ad((field_value > REC_N_NEW_EXTRA_BYTES && field_value < 32768) ||
146 field_value < (uint16_t)-REC_N_NEW_EXTRA_BYTES);
147
148 return ((byte *)ut_align_down(rec, UNIV_PAGE_SIZE) +
149 ut_align_offset(rec + field_value, UNIV_PAGE_SIZE));
150 } else {
151 ut_ad(field_value < UNIV_PAGE_SIZE);
152
153 return ((byte *)ut_align_down(rec, UNIV_PAGE_SIZE) + field_value);
154 }
155 }
156
157 static inline rec_t *rec_get_next_ptr(rec_t *rec, ulint comp) {
158 return (const_cast<rec_t *>(rec_get_next_ptr_const(rec, comp)));
159 }
160
161 static inline ulint rec_get_next_offs(const rec_t *rec, ulint comp) {
162 ulint field_value;
163 static_assert(REC_NEXT_MASK == 0xFFFFUL, "REC_NEXT_MASK != 0xFFFFUL");
164 static_assert(REC_NEXT_SHIFT == 0, "REC_NEXT_SHIFT != 0");
165
166 field_value = mach_read_from_2(rec - REC_NEXT);
167
168 if (comp) {
169 /** Check if the result offset is still on the same page. We allow
170 `field_value` to be interpreted as negative 16bit integer. This check does
171 nothing for 64KB pages. */
172 ut_ad(static_cast<uint16_t>(field_value +
173 ut_align_offset(rec, UNIV_PAGE_SIZE)));
174
175 if (field_value == 0) {
176 return (0);
177 }
178
179 /* There must be at least REC_N_NEW_EXTRA_BYTES + 1
180 between each record. */
181 ut_ad((field_value > REC_N_NEW_EXTRA_BYTES && field_value < 32768) ||
182 field_value < (uint16_t)-REC_N_NEW_EXTRA_BYTES);
183
184 return (ut_align_offset(rec + field_value, UNIV_PAGE_SIZE));
185 } else {
186 ut_ad(field_value < UNIV_PAGE_SIZE);
187
188 return (field_value);
189 }
190 }
191
192 /** The following function is used to set the next record offset field
193 of an old-style record. */
194 static inline void rec_set_next_offs_old(
195 rec_t *rec, /*!< in: old-style physical record */
196 ulint next) /*!< in: offset of the next record */
197 {
198 ut_ad(rec);
199 ut_ad(UNIV_PAGE_SIZE > next);
200 static_assert(REC_NEXT_MASK == 0xFFFFUL, "REC_NEXT_MASK != 0xFFFFUL");
201 static_assert(REC_NEXT_SHIFT == 0, "REC_NEXT_SHIFT != 0");
202
203 mach_write_to_2(rec - REC_NEXT, next);
204 }
205
206 /** The following function is used to set the next record offset field
207 of a new-style record. */
208 static inline void rec_set_next_offs_new(
209 rec_t *rec, /*!< in/out: new-style physical record */
210 ulint next) /*!< in: offset of the next record */
211 {
212 ulint field_value;
213
214 ut_ad(rec);
215 ut_ad(UNIV_PAGE_SIZE > next);
216
217 if (!next) {
218 field_value = 0;
219 } else {
220 /* The following two statements calculate
221 next - offset_of_rec mod 64Ki, where mod is the modulo
222 as a non-negative number */
223
224 field_value =
225 (ulint)((lint)next - (lint)ut_align_offset(rec, UNIV_PAGE_SIZE));
226 field_value &= REC_NEXT_MASK;
227 }
228
229 mach_write_to_2(rec - REC_NEXT, field_value);
230 }
231
232 /** The following function is used to set the number of fields
233 in an old-style record. */
234 static inline void rec_set_n_fields_old(
235 rec_t *rec, /*!< in: physical record */
236 ulint n_fields) /*!< in: the number of fields */
237 {
238 ut_ad(rec);
239 ut_ad(n_fields <= REC_MAX_N_FIELDS);
240 ut_ad(n_fields > 0);
241
242 rec_set_bit_field_2(rec, n_fields, REC_OLD_N_FIELDS, REC_OLD_N_FIELDS_MASK,
243 REC_OLD_N_FIELDS_SHIFT);
244 }
245
246 /** The following function is used to get the number of records owned by the
247 previous directory record.
248 @return number of owned records */
249 static inline ulint rec_get_n_owned_old(
250 const rec_t *rec) /*!< in: old-style physical record */
251 {
252 return (rec_get_bit_field_1(rec, REC_OLD_N_OWNED, REC_N_OWNED_MASK,
253 REC_N_OWNED_SHIFT));
254 }
255
256 /** The following function is used to set the number of owned records. */
257 static inline void rec_set_n_owned_old(
258 rec_t *rec, /*!< in: old-style physical record */
259 ulint n_owned) /*!< in: the number of owned */
260 {
261 rec_set_bit_field_1(rec, n_owned, REC_OLD_N_OWNED, REC_N_OWNED_MASK,
262 REC_N_OWNED_SHIFT);
263 }
264
265 /** The following function is used to get the number of records owned by the
266 previous directory record.
267 @return number of owned records */
268 static inline ulint rec_get_n_owned_new(
269 const rec_t *rec) /*!< in: new-style physical record */
270 {
271 return (rec_get_bit_field_1(rec, REC_NEW_N_OWNED, REC_N_OWNED_MASK,
272 REC_N_OWNED_SHIFT));
273 }
274
275 /** The following function is used to set the number of owned records. */
276 static inline void rec_set_n_owned_new(
277 rec_t *rec, /*!< in/out: new-style physical record */
278 page_zip_des_t *page_zip, /*!< in/out: compressed page, or NULL */
279 ulint n_owned) /*!< in: the number of owned */
280 {
281 rec_set_bit_field_1(rec, n_owned, REC_NEW_N_OWNED, REC_N_OWNED_MASK,
282 REC_N_OWNED_SHIFT);
283 if (page_zip && rec_get_status(rec) != REC_STATUS_SUPREMUM) {
284 page_zip_rec_set_owned(page_zip, rec, n_owned);
285 }
286 }
287
288 /** The following function is used to set the info bits of a record. */
289 static inline void rec_set_info_bits_old(
290 rec_t *rec, /*!< in: old-style physical record */
291 ulint bits) /*!< in: info bits */
292 {
293 ut_ad(rec_info_bits_valid(bits));
294 rec_set_bit_field_1(rec, bits, REC_OLD_INFO_BITS, REC_INFO_BITS_MASK,
295 REC_INFO_BITS_SHIFT);
296 }
297 /** The following function is used to set the info bits of a record. */
298 static inline void rec_set_info_bits_new(
299 rec_t *rec, /*!< in/out: new-style physical record */
300 ulint bits) /*!< in: info bits */
301 {
302 ut_ad(rec_info_bits_valid(bits));
303 rec_set_bit_field_1(rec, bits, REC_NEW_INFO_BITS, REC_INFO_BITS_MASK,
304 REC_INFO_BITS_SHIFT);
305 }
306
307 /** The following function is used to set the info bits of a temp record.
308 param[in,out] rec temp physical record
309 param[in] bits info bits */
310 static inline void rec_set_info_bits_new_temp(rec_t *rec, ulint bits) {
311 ut_ad(rec_info_bits_valid(bits));
312 rec_set_bit_field_1(rec, bits, REC_TMP_INFO_BITS, REC_INFO_BITS_MASK,
313 REC_INFO_BITS_SHIFT);
314 }
315
316 /** The following function is used to set the status bits of a new-style record.
317 */
318 static inline void rec_set_status(rec_t *rec, /*!< in/out: physical record */
319 ulint bits) /*!< in: info bits */
320 {
321 rec_set_bit_field_1(rec, bits, REC_NEW_STATUS, REC_NEW_STATUS_MASK,
322 REC_NEW_STATUS_SHIFT);
323 }
324
325 static inline ulint rec_get_info_and_status_bits(const rec_t *rec, bool comp) {
326 ulint bits;
327 static_assert(((REC_NEW_STATUS_MASK >> REC_NEW_STATUS_SHIFT) &
328 (REC_INFO_BITS_MASK >> REC_INFO_BITS_SHIFT)) == 0,
329 "REC_NEW_STATUS_MASK and REC_INFO_BITS_MASK overlap");
330
331 if (comp) {
332 bits = rec_get_info_bits(rec, true) | rec_get_status(rec);
333 } else {
334 bits = rec_get_info_bits(rec, false);
335 ut_ad(!(bits & ~(REC_INFO_BITS_MASK >> REC_INFO_BITS_SHIFT)));
336 }
337 return (bits);
338 }
339 /** The following function is used to set the info and status
340 bits of a record. (Only compact records have status bits.) */
341 static inline void rec_set_info_and_status_bits(
342 rec_t *rec, /*!< in/out: physical record */
343 ulint bits) /*!< in: info bits */
344 {
345 static_assert(((REC_NEW_STATUS_MASK >> REC_NEW_STATUS_SHIFT) &
346 (REC_INFO_BITS_MASK >> REC_INFO_BITS_SHIFT)) == 0,
347 "REC_NEW_STATUS_MASK and REC_INFO_BITS_MASK overlap");
348
349 rec_set_status(rec, bits & REC_NEW_STATUS_MASK);
350 rec_set_info_bits_new(rec, bits & ~REC_NEW_STATUS_MASK);
351 }
352
353 static inline bool rec_get_deleted_flag(const rec_t *rec, bool comp) {
354 if (comp) {
355 return (rec_get_bit_field_1(rec, REC_NEW_INFO_BITS, REC_INFO_DELETED_FLAG,
356 REC_INFO_BITS_SHIFT));
357 } else {
358 return (rec_get_bit_field_1(rec, REC_OLD_INFO_BITS, REC_INFO_DELETED_FLAG,
359 REC_INFO_BITS_SHIFT));
360 }
361 }
362
363 /** The following function is used to set the deleted bit. */
364 static inline void rec_set_deleted_flag_old(
365 rec_t *rec, /*!< in: old-style physical record */
366 bool flag) /*!< in: nonzero if delete marked */
367 {
368 auto val = rec_get_info_bits(rec, false);
369
370 if (flag) {
371 val |= REC_INFO_DELETED_FLAG;
372 } else {
373 val &= ~REC_INFO_DELETED_FLAG;
374 }
375
376 rec_set_info_bits_old(rec, val);
377 }
378
379 /** The following function is used to set the deleted bit. */
380 static inline void rec_set_deleted_flag_new(
381 rec_t *rec, /*!< in/out: new-style physical record */
382 page_zip_des_t *page_zip, /*!< in/out: compressed page, or NULL */
383 bool flag) /*!< in: nonzero if delete marked */
384 {
385 ulint val;
386
387 val = rec_get_info_bits(rec, true);
388
389 if (flag) {
390 val |= REC_INFO_DELETED_FLAG;
391 } else {
392 val &= ~REC_INFO_DELETED_FLAG;
393 }
394
395 rec_set_info_bits_new(rec, val);
396
397 if (page_zip) {
398 page_zip_rec_set_deleted(page_zip, rec, flag);
399 }
400 }
401
402 /** The following function is used to set the instant bit.
403 @param[in,out] rec new-style physical record
404 @param[in] flag set the bit to this flag */
405 static inline void rec_set_instant_flag_new(rec_t *rec, bool flag) {
406 ulint val;
407
408 val = rec_get_info_bits(rec, true);
409
410 if (flag) {
411 val |= REC_INFO_INSTANT_FLAG;
412 } else {
413 val &= ~REC_INFO_INSTANT_FLAG;
414 }
415
416 rec_set_info_bits_new(rec, val);
417 }
418 /** The following function is used to set the version bit.
419 @param[in,out] rec new-style (COMPACT/DYNAMIC) physical record
420 @param[in] flag set the bit to this flag */
421 static inline void rec_new_set_versioned(rec_t *rec, bool flag) {
422 ulint val;
423
424 val = rec_get_info_bits(rec, true);
425
426 if (flag) {
427 val |= REC_INFO_VERSION_FLAG;
428 } else {
429 val &= ~REC_INFO_VERSION_FLAG;
430 }
431
432 rec_set_info_bits_new(rec, val);
433 }
434
435 static inline void rec_new_set_instant(rec_t *rec) {
436 rec_set_instant_flag_new(rec, true);
437 rec_new_set_versioned(rec, false);
438 }
439
440 static inline void rec_new_set_versioned(rec_t *rec) {
441 rec_new_set_versioned(rec, true);
442 rec_set_instant_flag_new(rec, false);
443 }
444
445 static inline void rec_new_reset_instant_version(rec_t *rec) {
446 rec_set_instant_flag_new(rec, false);
447 rec_new_set_versioned(rec, false);
448 }
449
450 /** The following function is used to set the version bit.
451 @param[in,out] rec old-style (REDUNDANT) physical record
452 @param[in] flag true to set, false to reset */
453 static inline void rec_old_set_versioned(rec_t *rec, bool flag) {
454 ulint val;
455
456 val = rec_get_info_bits(rec, false);
457
458 if (flag) {
459 val |= REC_INFO_VERSION_FLAG;
460 } else {
461 val &= ~REC_INFO_VERSION_FLAG;
462 }
463
464 rec_set_info_bits_old(rec, val);
465 }
466
467 /** The following function is used to set the version bit.
468 @param[in,out] rec Temp physical record
469 @param[in] flag true to set, false to reset */
470 static inline void rec_new_temp_set_versioned(rec_t *rec, bool flag) {
471 ulint val;
472
473 val = rec_get_info_bits_temp(rec);
474
475 if (flag) {
476 val |= REC_INFO_VERSION_FLAG;
477 } else {
478 val &= ~REC_INFO_VERSION_FLAG;
479 }
480
481 rec_set_info_bits_new_temp(rec, val);
482 }
483
484 /** The following function tells if a new-style record is a node pointer.
485 @return true if node pointer */
486 static inline bool rec_get_node_ptr_flag(
487 const rec_t *rec) /*!< in: physical record */
488 {
489 return (REC_STATUS_NODE_PTR == rec_get_status(rec));
490 }
491
492 /** The following function is used to get the order number
493 of an old-style record in the heap of the index page.
494 @return heap order number */
495 [[nodiscard]] static inline uint16_t rec_get_heap_no_old(
496 const rec_t *rec) /*!< in: physical record */
497 {
498 return (rec_get_bit_field_2(rec, REC_OLD_HEAP_NO, REC_HEAP_NO_MASK,
499 REC_HEAP_NO_SHIFT));
500 }
501
502 /** The following function is used to set the heap number
503 field in an old-style record. */
504 static inline void rec_set_heap_no_old(
505 rec_t *rec, /*!< in: physical record */
506 ulint heap_no) /*!< in: the heap number */
507 {
508 rec_set_bit_field_2(rec, heap_no, REC_OLD_HEAP_NO, REC_HEAP_NO_MASK,
509 REC_HEAP_NO_SHIFT);
510 }
511
512 /** The following function is used to get the order number
513 of a new-style record in the heap of the index page.
514 @return heap order number */
515 [[nodiscard]] static inline uint16_t rec_get_heap_no_new(
516 const rec_t *rec) /*!< in: physical record */
517 {
518 return (rec_get_bit_field_2(rec, REC_NEW_HEAP_NO, REC_HEAP_NO_MASK,
519 REC_HEAP_NO_SHIFT));
520 }
521
522 /** The following function is used to set the heap number
523 field in a new-style record. */
524 static inline void rec_set_heap_no_new(
525 rec_t *rec, /*!< in/out: physical record */
526 ulint heap_no) /*!< in: the heap number */
527 {
528 rec_set_bit_field_2(rec, heap_no, REC_NEW_HEAP_NO, REC_HEAP_NO_MASK,
529 REC_HEAP_NO_SHIFT);
530 }
531
532 /** The following function is used to set the 1-byte offsets flag. */
533 static inline void rec_set_1byte_offs_flag(
534 rec_t *rec, /*!< in: physical record */
535 bool flag) /*!< in: true if 1byte form */
536 {
537 rec_set_bit_field_1(rec, flag, REC_OLD_SHORT, REC_OLD_SHORT_MASK,
538 REC_OLD_SHORT_SHIFT);
539 }
540
541 /** Determine if the field is not NULL and not having default value
542 after instant ADD COLUMN
543 @param[in] len length of a field
544 @return true if not NULL and not having default value */
545 static inline bool rec_field_not_null_not_add_col_def(ulint len) {
546 return (len != UNIV_SQL_NULL && len != UNIV_SQL_ADD_COL_DEFAULT &&
547 len != UNIV_SQL_INSTANT_DROP_COL);
548 }
549
550 /** Determine if the offsets are for a record in the new
551 compact format.
552 @return nonzero if compact format */
553 static inline bool rec_offs_comp(
554 const ulint *offsets) /*!< in: array returned by rec_get_offsets() */
555 {
556 ut_ad(rec_offs_validate(nullptr, nullptr, offsets));
557 return (*rec_offs_base(offsets) & REC_OFFS_COMPACT) != 0;
558 }
559
560 /** Determine if the offsets are for a record containing
561 externally stored columns.
562 @return nonzero if externally stored */
563 static inline bool rec_offs_any_extern(
564 const ulint *offsets) /*!< in: array returned by rec_get_offsets() */
565 {
566 ut_ad(rec_offs_validate(nullptr, nullptr, offsets));
567 return (*rec_offs_base(offsets) & REC_OFFS_EXTERNAL);
568 }
569
570 static inline const byte *rec_offs_any_null_extern(const dict_index_t *index,
571 const rec_t *rec,
572 const ulint *offsets) {
573 ulint i;
574 ut_ad(rec_offs_validate(rec, nullptr, offsets));
575
576 if (!rec_offs_any_extern(offsets)) {
577 return (nullptr);
578 }
579
580 for (i = 0; i < rec_offs_n_fields(offsets); i++) {
581 if (rec_offs_nth_extern(index, offsets, i)) {
582 ulint len;
583 const byte *field = rec_get_nth_field(index, rec, offsets, i, &len);
584
585 ut_a(len >= BTR_EXTERN_FIELD_REF_SIZE);
586 if (!memcmp(field + len - BTR_EXTERN_FIELD_REF_SIZE, field_ref_zero,
587 BTR_EXTERN_FIELD_REF_SIZE)) {
588 return (field);
589 }
590 }
591 }
592
593 return (nullptr);
594 }
595
596 /** Returns the number of extern bits set in a record.
597 @param[in] index record descriptor
598 @param[in] offsets array returned by rec_get_offsets()
599 @return number of externally stored fields */
600 static inline ulint rec_offs_n_extern(const dict_index_t *index,
601 const ulint *offsets) {
602 ulint n = 0;
603
604 if (rec_offs_any_extern(offsets)) {
605 ulint i;
606
607 for (i = rec_offs_n_fields(offsets); i--;) {
608 if (rec_offs_nth_extern(index, offsets, i)) {
609 n++;
610 }
611 }
612 }
613
614 return (n);
615 }
616
617 /** Returns the offset of n - 1th field end if the record is stored in the
618 1-byte offsets form. If the field is SQL null, the flag is ORed in the returned
619 value. This function and the 2-byte counterpart are defined here because the
620 C-compiler was not able to sum negative and positive constant offsets, and
621 warned of constant arithmetic overflow within the compiler.
622 @return offset of the start of the PREVIOUS field, SQL null flag ORed */
623 static inline ulint rec_1_get_prev_field_end_info(
624 const rec_t *rec, /*!< in: record */
625 ulint n) /*!< in: field index */
626 {
627 ut_ad(rec_get_1byte_offs_flag(rec));
628 ut_ad(n <= rec_get_n_fields_old_raw(rec));
629
630 uint32_t version_length = 0;
631 if (rec_old_is_versioned(rec)) {
632 version_length = 1;
633 }
634
635 return (mach_read_from_1(rec - (REC_N_OLD_EXTRA_BYTES + version_length + n)));
636 }
637
638 /** Returns the offset of n - 1th field end if the record is stored in the
639 2-byte offsets form. If the field is SQL null, the flag is ORed in the returned
640 value.
641 @return offset of the start of the PREVIOUS field, SQL null flag ORed */
642 static inline ulint rec_2_get_prev_field_end_info(
643 const rec_t *rec, /*!< in: record */
644 ulint n) /*!< in: field index */
645 {
646 ut_ad(!rec_get_1byte_offs_flag(rec));
647 ut_ad(n <= rec_get_n_fields_old_raw(rec));
648
649 uint32_t version_length = 0;
650 if (rec_old_is_versioned(rec)) {
651 version_length = 1;
652 }
653
654 return (
655 mach_read_from_2(rec - (REC_N_OLD_EXTRA_BYTES + version_length + 2 * n)));
656 }
657
658 /** The following function returns the data size of a physical
659 record, that is the sum of field lengths. SQL null fields
660 are counted as length 0 fields. The value returned by the function
661 is the distance from record origin to record end in bytes.
662 @return size */
663 static inline ulint rec_offs_data_size(
664 const ulint *offsets) /*!< in: array returned by rec_get_offsets() */
665 {
666 ulint size;
667
668 ut_ad(rec_offs_validate(nullptr, nullptr, offsets));
669 size = rec_offs_base(offsets)[rec_offs_n_fields(offsets)] & REC_OFFS_MASK;
670 ut_ad(size < UNIV_PAGE_SIZE);
671 return (size);
672 }
673
674 /** Returns the total size of record minus data size of record. The value
675 returned by the function is the distance from record start to record origin
676 in bytes.
677 @return size */
678 static inline ulint rec_offs_extra_size(
679 const ulint *offsets) /*!< in: array returned by rec_get_offsets() */
680 {
681 ulint size;
682 ut_ad(rec_offs_validate(nullptr, nullptr, offsets));
683 size = *rec_offs_base(offsets) & ~(REC_OFFS_COMPACT | REC_OFFS_EXTERNAL);
684 ut_ad(size < UNIV_PAGE_SIZE);
685 return (size);
686 }
687
688 /** Returns the total size of a physical record.
689 @return size */
690 static inline ulint rec_offs_size(
691 const ulint *offsets) /*!< in: array returned by rec_get_offsets() */
692 {
693 return (rec_offs_data_size(offsets) + rec_offs_extra_size(offsets));
694 }
695
696 #ifdef UNIV_DEBUG
697 static inline byte *rec_get_end(const rec_t *rec, const ulint *offsets) {
698 ut_ad(rec_offs_validate(rec, nullptr, offsets));
699 return (const_cast<rec_t *>(rec + rec_offs_data_size(offsets)));
700 }
701
702 static inline byte *rec_get_start(const rec_t *rec, const ulint *offsets) {
703 ut_ad(rec_offs_validate(rec, nullptr, offsets));
704 return (const_cast<rec_t *>(rec - rec_offs_extra_size(offsets)));
705 }
706 #endif /* UNIV_DEBUG */
707
708 /** Copy a physical record to a buffer.
709 @param[in] buf buffer
710 @param[in] rec physical record
711 @param[in] offsets array returned by rec_get_offsets()
712 @return pointer to the origin of the copy */
713 static inline rec_t *rec_copy(void *buf, const rec_t *rec,
714 const ulint *offsets) {
715 ulint extra_len;
716 ulint data_len;
717
718 ut_ad(rec != nullptr);
719 ut_ad(buf != nullptr);
720 ut_ad(rec_offs_validate(rec, nullptr, offsets));
721 ut_ad(rec_validate(rec, offsets));
722
723 extra_len = rec_offs_extra_size(offsets);
724 data_len = rec_offs_data_size(offsets);
725
726 ut_memcpy(buf, rec - extra_len, extra_len + data_len);
727
728 return ((byte *)buf + extra_len);
729 }
730
731 /** Returns the extra size of an old-style physical record if we know its
732 data size and number of fields. */
733 static inline ulint rec_get_converted_extra_size(ulint data_size,
734 ulint n_fields, bool has_ext) {
735 if (!has_ext && data_size <= REC_1BYTE_OFFS_LIMIT) {
736 return (REC_N_OLD_EXTRA_BYTES + n_fields);
737 }
738
739 return (REC_N_OLD_EXTRA_BYTES + 2 * n_fields);
740 }
741
742 /** The following function returns the size of a data tuple when converted to
743 a physical record.
744 @param[in] index record descriptor
745 @param[in] dtuple data tuple
746 @return size */
747 static inline ulint rec_get_converted_size(const dict_index_t *index,
748 const dtuple_t *dtuple) {
749 ulint data_size;
750 ulint extra_size;
751
752 ut_ad(index);
753 ut_ad(dtuple);
754 ut_ad(dtuple_check_typed(dtuple));
755
756 ut_ad(dict_index_is_ibuf(index) ||
757 /* If this is an instant index and the tuple comes from
758 UPDATE, its fields could be less than index definition */
759 index->has_instant_cols_or_row_versions() ||
760 dtuple_get_n_fields(dtuple) ==
761 (((dtuple_get_info_bits(dtuple) & REC_NEW_STATUS_MASK) ==
762 REC_STATUS_NODE_PTR)
763 ? dict_index_get_n_unique_in_tree_nonleaf(index) + 1U
764 : dict_index_get_n_fields(index)) ||
765 /* a record for older SYS_INDEXES table
766 (missing merge_threshold column) is acceptable. */
767 (index->table->id == DICT_INDEXES_ID &&
768 dtuple_get_n_fields(dtuple) == dict_index_get_n_fields(index) - 1U));
769
770 if (dict_table_is_comp(index->table)) {
771 return (rec_get_converted_size_comp(
772 index, dtuple_get_info_bits(dtuple) & REC_NEW_STATUS_MASK,
773 dtuple->fields, dtuple->n_fields, nullptr));
774 }
775
776 /* Record is in REDUNDANT format */
777 data_size = dtuple_get_data_size(dtuple, 0);
778
779 extra_size = rec_get_converted_extra_size(
780 data_size, dtuple_get_n_fields(dtuple), dtuple->has_ext());
781
782 bool is_leaf_rec =
783 (dtuple_get_info_bits(dtuple) & 0x03UL) == REC_STATUS_ORDINARY;
784 if (is_leaf_rec && is_store_version(index, dtuple_get_n_fields(dtuple))) {
785 extra_size++;
786 }
787
788 #if 0
789 /* This code is inactive since it may be the wrong place to add
790 in the size of node pointers used in parent pages AND it is not
791 currently needed since ha_innobase::max_supported_key_length()
792 ensures that the key size limit for each page size is well below
793 the actual limit ((free space on page / 4) - record overhead).
794 But those limits will need to be raised when InnoDB can
795 support multiple page sizes. At that time, we will need
796 to consider the node pointer on these universal btrees. */
797
798 if (dict_index_is_ibuf(index)) {
799 /* This is for the insert buffer B-tree.
800 All fields in the leaf tuple ascend to the
801 parent node plus the child page pointer. */
802
803 /* ibuf cannot contain externally stored fields */
804 ut_ad(n_ext == 0);
805
806 /* Add the data pointer and recompute extra_size
807 based on one more field. */
808 data_size += REC_NODE_PTR_SIZE;
809 extra_size = rec_get_converted_extra_size(
810 data_size,
811 dtuple_get_n_fields(dtuple) + 1,
812 0);
813
814 /* Be sure dtuple->n_fields has this node ptr
815 accounted for. This function should correspond to
816 what rec_convert_dtuple_to_rec() needs in storage.
817 In optimistic insert or update-not-in-place, we will
818 have to ensure that if the record is converted to a
819 node pointer, it will not become too large.*/
820 }
821 #endif
822
823 return (data_size + extra_size);
824 }
825
826 #ifndef UNIV_HOTBACKUP
827 /** Compute a hash value of a prefix of a leaf page record.
828 @param[in] rec leaf page record
829 @param[in] offsets rec_get_offsets(rec)
830 @param[in] n_fields number of complete fields to hash
831 @param[in] n_bytes number of bytes to hash in the last field
832 @param[in] hash_value hash value of the index identifier
833 @param[in] index index where the record resides
834 @return the hashed value */
835 static inline uint64_t rec_hash(const rec_t *rec, const ulint *offsets,
836 ulint n_fields, ulint n_bytes,
837 uint64_t hash_value,
838 const dict_index_t *index) {
839 uint16_t i;
840 const byte *data;
841 ulint len;
842 ulint n_fields_rec;
843
844 ut_ad(rec_offs_validate(rec, nullptr, offsets));
845 ut_ad(rec_validate(rec, offsets));
846 ut_ad(n_fields > 0 || n_bytes > 0);
847
848 n_fields_rec = rec_offs_n_fields(offsets);
849 ut_ad(n_fields <= n_fields_rec);
850 ut_ad(n_fields < n_fields_rec || n_bytes == 0);
851
852 if (n_fields > n_fields_rec) {
853 n_fields = n_fields_rec;
854 }
855
856 if (n_fields == n_fields_rec) {
857 n_bytes = 0;
858 }
859
860 for (i = 0; i < n_fields; i++) {
861 data = rec_get_nth_field_instant(rec, offsets, i, index, &len);
862
863 if (len != UNIV_SQL_NULL) {
864 hash_value = ut::hash_binary(data, len, hash_value);
865 }
866 }
867
868 if (n_bytes > 0) {
869 data = rec_get_nth_field_instant(rec, offsets, i, index, &len);
870
871 if (len != UNIV_SQL_NULL) {
872 if (len > n_bytes) {
873 len = n_bytes;
874 }
875
876 hash_value = ut::hash_binary(data, len, hash_value);
877 }
878 }
879
880 return hash_value;
881 }
882 #endif /* !UNIV_HOTBACKUP */
883
884 /** Get the length of the number of fields for any new style record.
885 @param[in] n_fields number of fields in the record
886 @return length of specified number of fields */
887 static inline uint8_t rec_get_n_fields_length(ulint n_fields) {
888 return (n_fields > REC_N_FIELDS_ONE_BYTE_MAX ? 2 : 1);
889 }
890
891 /** Set the number of fields for one new style leaf page record.
892 This is only needed for table after instant ADD COLUMN.
893 @param[in,out] rec leaf page record
894 @param[in] n_fields number of fields in the record
895 @return the length of the n_fields occupies */
896 static inline uint8_t rec_set_n_fields(rec_t *rec, ulint n_fields) {
897 byte *ptr = rec - (REC_N_NEW_EXTRA_BYTES + 1);
898
899 ut_ad(n_fields < REC_MAX_N_FIELDS);
900
901 if (n_fields <= REC_N_FIELDS_ONE_BYTE_MAX) {
902 *ptr = static_cast<byte>(n_fields);
903 return (1);
904 }
905
906 --ptr;
907 *ptr++ = static_cast<byte>(n_fields & 0xFF);
908 *ptr = static_cast<byte>(n_fields >> 8);
909 ut_ad((*ptr & 0x80) == 0);
910 *ptr |= REC_N_FIELDS_TWO_BYTES_FLAG;
911
912 return (2);
913 }
914
915 /** Set the row version on a new style leaf page record.
916 This is only needed for table after instant ADD/DROP COLUMN.
917 @param[in,out] rec leaf page record
918 @param[in] row_version row version */
919 static inline void rec_set_instant_row_version_new(rec_t *rec,
920 uint8_t row_version) {
921 /* Note : row_version might be 0 while storing a new row in upgraded table */
922 ut_ad(is_valid_row_version(row_version));
923
924 byte *ptr = rec - (REC_N_NEW_EXTRA_BYTES + 1);
925 *ptr = static_cast<byte>(row_version);
926 }
927
928 /** Get the row version on a new style leaf page record.
929 This is only needed for table after instant ADD/DROP COLUMN.
930 @param[in] rec leaf page record
931 @return row version */
932 static inline uint8_t rec_get_instant_row_version_new(const rec_t *rec) {
933 uint8_t row_version = 0;
934 const byte *ptr = rec - (REC_N_NEW_EXTRA_BYTES + 1);
935 row_version = *ptr;
936 /* For upgraded table, we store version 0 */
937 ut_ad(is_valid_row_version(row_version));
938
939 return row_version;
940 }
941
942 /** Set the row version on an old style leaf page record.
943 This is only needed for table after instant ADD/DROP COLUMN.
944 @param[in,out] rec leaf page record
945 @param[in] row_version row version */
946 static inline void rec_set_instant_row_version_old(rec_t *rec,
947 uint8_t row_version) {
948 /* Note : row_version might be 0 while storing a new row in upgraded table */
949 ut_ad(is_valid_row_version(row_version));
950
951 byte *ptr = rec - (REC_N_OLD_EXTRA_BYTES + 1);
952 *ptr = static_cast<byte>(row_version);
953 }
954
955 /** Get the row version on an old style leaf page record.
956 This is only needed for table after instant ADD/DROP COLUMN.
957 @param[in] rec leaf page record
958 @return row version */
959 static inline uint8_t rec_get_instant_row_version_old(const rec_t *rec) {
960 const byte *ptr = rec - (REC_N_OLD_EXTRA_BYTES + 1);
961 uint8_t row_version = *ptr;
962 ut_ad(is_valid_row_version(row_version));
963
964 return row_version;
965 }
966
967 /** Gets the value of the specified field in the record in old style.
968 This is only used for record from instant index, which is clustered
969 index and has some instantly added columns.
970 @param[in] rec physical record
971 @param[in] n index of the field
972 @param[in] index clustered index where the record resides
973 @param[out] len length of the field, UNIV_SQL if SQL null
974 @return value of the field, could be either pointer to rec or default value */
975 static inline const byte *rec_get_nth_field_old_instant(
976 const rec_t *rec, uint16_t n, const dict_index_t *index, ulint *len) {
977 ut_a(index != nullptr);
978
979 if (index->has_row_versions()) {
980 /* Fields are stored in record in order of the version they are added in.
981 Get the field number on physical record. */
982
983 uint8_t version = UINT8_UNDEFINED;
984 if (rec_old_is_versioned(rec)) {
985 version = rec_get_instant_row_version_old(rec);
986 }
987
988 n = index->get_field_phy_pos(n, version);
989 }
990
991 if (n < rec_get_n_fields_old_raw(rec)) {
992 /* nullptr for index as n is physical here */
993 return (rec_get_nth_field_old(nullptr, rec, n, len));
994 }
995
996 const byte *field;
997
998 ut_ad(index->has_instant_cols_or_row_versions());
999
1000 field = index->get_nth_default(n, len);
1001 return (field);
1002 }
1003
1004 /** Gets the value of the specified field in the record.
1005 This is only used when there is possibility that the record comes from the
1006 clustered index, which has some instantly add columns
1007 @param[in] rec record
1008 @param[in] offsets array returned by rec_get_offsets()
1009 @param[in] n index of the field
1010 @param[in] index clustered index where the record resides
1011 @param[in,out] len length of the field, UNIV_SQL_NULL if SQL null
1012 @return value of the field, could be either pointer to rec or default value */
1013 static inline const byte *rec_get_nth_field_instant(const rec_t *rec,
1014 const ulint *offsets,
1015 ulint n,
1016 const dict_index_t *index,
1017 ulint *len) {
1018 if (index->has_row_versions()) {
1019 /* Fields are stored in record in order of the version they are added in.
1020 Get the field number on physical record. */
1021 n = index->get_field_off_pos(n);
1022 }
1023
1024 /* nullptr for index as n is physical here */
1025 ulint off = rec_get_nth_field_offs(nullptr, offsets, n, len);
1026
1027 if (*len != UNIV_SQL_ADD_COL_DEFAULT && *len != UNIV_SQL_INSTANT_DROP_COL) {
1028 return (rec + off);
1029 }
1030
1031 ut_a(index != nullptr);
1032 ut_ad(index->has_instant_cols_or_row_versions());
1033
1034 if (*len == UNIV_SQL_INSTANT_DROP_COL) {
1035 *len = 0;
1036 return nullptr;
1037 }
1038
1039 return (index->get_nth_default(n, len));
1040 }
1041
1042 static inline bool rec_is_min_rec_flag_set(const rec_t *rec, bool is_comp) {
1043 ut_ad(rec);
1044 return (REC_INFO_MIN_REC_FLAG & rec_get_info_bits(rec, is_comp));
1045 }