Debian Codegraph @

storage/innobase/rem/rec.cc

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 rem/rec.cc
29 Record manager
30
31 Created 5/30/1994 Heikki Tuuri
32 *************************************************************************/
33
34 /** NOTE: The functions in this file should only use functions from
35 other files in library. The code in this file is used to make a library for
36 external tools. */
37
38 #include <stddef.h>
39
40 #include "dict0dict.h"
41 #include "mem0mem.h"
42 #include "rem/rec.h"
43 #include "rem0lrec.h"
44 #include "rem0rec.h"
45
46 /** Initialize offset for each field in a new style record.
47 @param[in] rec physical record
48 @param[in] index record descriptor
49 @param[in, out] offsets array of offsets */
50 static void rec_init_offsets_new(const rec_t *rec, const dict_index_t *index,
51 ulint *offsets) {
52 ulint status = rec_get_status(rec);
53 ulint n_node_ptr_field = ULINT_UNDEFINED;
54
55 switch (UNIV_EXPECT(status, REC_STATUS_ORDINARY)) {
56 case REC_STATUS_INFIMUM:
57 case REC_STATUS_SUPREMUM:
58 /* the field is 8 bytes long */
59 rec_offs_base(offsets)[0] = REC_N_NEW_EXTRA_BYTES | REC_OFFS_COMPACT;
60 rec_offs_base(offsets)[1] = 8;
61 return;
62 case REC_STATUS_NODE_PTR:
63 n_node_ptr_field = dict_index_get_n_unique_in_tree_nonleaf(index);
64 break;
65 case REC_STATUS_ORDINARY:
66 rec_init_offsets_comp_ordinary(rec, false, index, offsets);
67 return;
68 }
69
70 /* This is non-leaf record. */
71 ut_ad(!rec_new_is_versioned(rec));
72
73 const byte *nulls = rec - (REC_N_NEW_EXTRA_BYTES + 1);
74 const size_t nullable_cols = index->get_nullable_before_instant_add_drop();
75
76 const byte *lens = nulls - UT_BITS_IN_BYTES(nullable_cols);
77 ulint offs = 0;
78 ulint null_mask = 1;
79
80 /* read the lengths of fields 0..n */
81 ulint i = 0;
82 dict_field_t *field = nullptr;
83 do {
84 ulint len;
85 if (UNIV_UNLIKELY(i == n_node_ptr_field)) {
86 len = offs += REC_NODE_PTR_SIZE;
87 goto resolved;
88 }
89
90 field = index->get_field(i);
91 if (!(field->col->prtype & DATA_NOT_NULL)) {
92 /* nullable field => read the null flag */
93
94 if (UNIV_UNLIKELY(!(byte)null_mask)) {
95 nulls--;
96 null_mask = 1;
97 }
98
99 if (*nulls & null_mask) {
100 null_mask <<= 1;
101 /* No length is stored for NULL fields.
102 We do not advance offs, and we set
103 the length to zero and enable the
104 SQL NULL flag in offsets[]. */
105 len = offs | REC_OFFS_SQL_NULL;
106 goto resolved;
107 }
108 null_mask <<= 1;
109 }
110
111 if (UNIV_UNLIKELY(!field->fixed_len)) {
112 const dict_col_t *col = field->col;
113 /* DATA_POINT should always be a fixed
114 length column. */
115 ut_ad(col->mtype != DATA_POINT);
116 /* Variable-length field: read the length */
117 len = *lens--;
118 /* If the maximum length of the field
119 is up to 255 bytes, the actual length
120 is always stored in one byte. If the
121 maximum length is more than 255 bytes,
122 the actual length is stored in one
123 byte for 0..127. The length will be
124 encoded in two bytes when it is 128 or
125 more, or when the field is stored
126 externally. */
127 if (DATA_BIG_COL(col)) {
128 if (len & 0x80) {
129 /* 1exxxxxxx xxxxxxxx */
130
131 len <<= 8;
132 len |= *lens--;
133
134 /* B-tree node pointers
135 must not contain externally
136 stored columns. Thus
137 the "e" flag must be 0. */
138 ut_a(!(len & 0x4000));
139 offs += len & 0x3fff;
140 len = offs;
141
142 goto resolved;
143 }
144 }
145
146 len = offs += len;
147 } else {
148 len = offs += field->fixed_len;
149 }
150 resolved:
151 rec_offs_base(offsets)[i + 1] = len;
152 } while (++i < rec_offs_n_fields(offsets));
153
154 *rec_offs_base(offsets) = (rec - (lens + 1)) | REC_OFFS_COMPACT;
155 }
156
157 /** Initialize offsets for record in REDUNDNT format when each field offsets is
158 stored in 1 byte.
159 @param[in] rec physical record
160 @param[in] index record descriptor
161 @param[in,out] offsets array of offsets
162 @param[in] row_version row version in record */
163 static void rec_init_offset_old_1byte(const rec_t *rec,
164 const dict_index_t *index, ulint *offsets,
165 uint8_t row_version) {
166 ut_ad(is_valid_row_version(row_version));
167
168 ulint offs = REC_N_OLD_EXTRA_BYTES;
169 /* 1 byte for row version */
170 offs += rec_old_is_versioned(rec) ? 1 : 0;
171 offs += rec_get_n_fields_old_raw(rec);
172 *rec_offs_base(offsets) = offs;
173
174 /* Determine offsets to fields */
175 ulint i = 0;
176 uint32_t dropped_col_count = 0;
177 do {
178 if (index->has_instant_cols_or_row_versions()) {
179 dict_field_t *dfield = index->get_physical_field(i);
180 dict_col_t *col = dfield->col;
181
182 if (rec_old_is_versioned(rec)) { /* rec is in V2 */
183 ut_ad(index->has_row_versions() ||
184 (index->table->is_upgraded_instant() && row_version == 0));
185
186 /* Based on the row version in record and column information, see if
187 this column is there in this record or not. */
188 if (col->is_added_after(row_version)) {
189 /* This columns is added after this row version. In this case no
190 need to store the length. Instead store only if it is NULL or
191 DEFAULT value. */
192
193 offs &= ~REC_OFFS_SQL_NULL;
194 offs = rec_get_instant_offset(index, i, offs);
195 } else if (col->is_dropped_in_or_before(row_version)) {
196 /* This columns is dropped before or on this row version so its data
197 won't be there on row. So no need to store the length. Instead,
198 store offs ORed with REC_OFFS_DROP to indicate the same. */
199 offs &= ~REC_OFFS_SQL_NULL;
200 offs = offs | REC_OFFS_DROP;
201 dropped_col_count++;
202
203 /* NOTE : Existing rows, which have data for this column, would still
204 need to process this column, so store the correct length there.
205 Though it will be skipped while fetching row. */
206 } else {
207 // offs = rec_1_get_field_end_info(rec, col->phy_pos);
208 /* i is physical pos here */
209 offs = rec_1_get_field_end_info_low(rec, i - dropped_col_count);
210 }
211 } else if (i >= rec_get_n_fields_old_raw(rec)) {
212 /* This field is not present in rec */
213 offs &= ~REC_OFFS_SQL_NULL;
214 offs = rec_get_instant_offset(index, i, offs);
215 } else {
216 /* This field is present in rec */
217 /* i is physical pos here */
218 offs = rec_1_get_field_end_info_low(rec, i);
219 }
220 } else {
221 /* i is physical pos here */
222 offs = rec_1_get_field_end_info_low(rec, i);
223 }
224
225 if (offs & REC_1BYTE_SQL_NULL_MASK) {
226 offs &= ~REC_1BYTE_SQL_NULL_MASK;
227 offs |= REC_OFFS_SQL_NULL;
228 }
229
230 ut_ad(i - dropped_col_count < rec_get_n_fields_old_raw(rec) ||
231 (offs & REC_OFFS_SQL_NULL) || (offs & REC_OFFS_DEFAULT) ||
232 (offs & REC_OFFS_DROP));
233 rec_offs_base(offsets)[1 + i] = offs;
234 } while (++i < rec_offs_n_fields(offsets));
235 }
236
237 /** Initialize offsets for record in REDUNDNT format when each field offsets is
238 stored in 2 byte.
239 @param[in] rec physical record
240 @param[in] index record descriptor
241 @param[in, out] offsets array of offsets
242 @param[in] row_version row version in record */
243 static void rec_init_offset_old_2byte(const rec_t *rec,
244 const dict_index_t *index, ulint *offsets,
245 uint8_t row_version) {
246 ut_ad(is_valid_row_version(row_version));
247
248 ulint offs = REC_N_OLD_EXTRA_BYTES;
249 /* 1 byte for row version */
250 offs += rec_old_is_versioned(rec) ? 1 : 0;
251 offs += 2 * rec_get_n_fields_old_raw(rec);
252 *rec_offs_base(offsets) = offs;
253
254 /* Determine offsets to fields */
255 ulint i = 0;
256 uint32_t dropped_col_count = 0;
257 do {
258 if (index->has_instant_cols_or_row_versions()) {
259 dict_field_t *dfield = index->get_physical_field(i);
260 dict_col_t *col = dfield->col;
261
262 if (rec_old_is_versioned(rec)) { /* rec is in V2 */
263 ut_ad(index->has_row_versions() ||
264 (index->table->is_upgraded_instant() && row_version == 0));
265
266 /* Based on the row version in record and column information, see if
267 this column is there in this record or not. */
268 if (col->is_added_after(row_version)) {
269 /* This columns is added after this row version. In this case no
270 need to store the length. Instead store only if it is NULL or
271 DEFAULT value. */
272
273 offs &= ~(REC_OFFS_SQL_NULL | REC_OFFS_EXTERNAL);
274 offs = rec_get_instant_offset(index, i, offs);
275 } else if (col->is_dropped_in_or_before(row_version)) {
276 /* This columns is dropped before or on this row version so its data
277 won't be there on row. So no need to store the length. Instead,
278 store offs ORed with REC_OFFS_DROP to indicate the same. */
279 offs &= ~(REC_OFFS_SQL_NULL | REC_OFFS_EXTERNAL);
280 offs = offs | REC_OFFS_DROP;
281 dropped_col_count++;
282
283 /* NOTE : Existing rows, which have data for this column, would still
284 need to process this column, so store the correct length there.
285 Though it will be skipped while fetching row. */
286 } else {
287 // offs = rec_2_get_field_end_info(rec, col->phy_pos);
288 /* i is physical pos here */
289 offs = rec_2_get_field_end_info_low(rec, i - dropped_col_count);
290 }
291 } else if (i >= rec_get_n_fields_old_raw(rec)) {
292 /* This field is not present in rec */
293 offs &= ~(REC_OFFS_SQL_NULL | REC_OFFS_EXTERNAL);
294 offs = rec_get_instant_offset(index, i, offs);
295 } else {
296 /* This field is present in rec */
297 /* i is physical pos here */
298 offs = rec_2_get_field_end_info_low(rec, i);
299 }
300 } else {
301 /* i is physical pos here */
302 offs = rec_2_get_field_end_info_low(rec, i);
303 }
304
305 if (offs & REC_2BYTE_SQL_NULL_MASK) {
306 offs &= ~REC_2BYTE_SQL_NULL_MASK;
307 offs |= REC_OFFS_SQL_NULL;
308 }
309
310 if (offs & REC_2BYTE_EXTERN_MASK) {
311 offs &= ~REC_2BYTE_EXTERN_MASK;
312 offs |= REC_OFFS_EXTERNAL;
313 *rec_offs_base(offsets) |= REC_OFFS_EXTERNAL;
314 }
315
316 ut_ad(i - dropped_col_count < rec_get_n_fields_old_raw(rec) ||
317 (offs & REC_OFFS_SQL_NULL) || (offs & REC_OFFS_DEFAULT) ||
318 (offs & REC_OFFS_DROP));
319 rec_offs_base(offsets)[1 + i] = offs;
320 } while (++i < rec_offs_n_fields(offsets));
321 }
322
323 /** Initialize offset for each field in an old style record.
324 @param[in] rec physical record
325 @param[in] index record descriptor
326 @param[in, out] offsets array of offsets */
327 static void rec_init_offsets_old(const rec_t *rec, const dict_index_t *index,
328 ulint *offsets) {
329 /* Old-style record: determine extra size and end offsets */
330
331 uint32_t row_version = 0;
332 if (rec_old_is_versioned(rec)) {
333 ut_ad(index->is_clustered());
334 /* Read the version information */
335 row_version = rec_get_instant_row_version_old(rec);
336 ut_ad(is_valid_row_version(row_version));
337 ut_ad(index->has_row_versions() ||
338 (index->table->is_upgraded_instant() && row_version == 0));
339 }
340
341 if (rec_get_1byte_offs_flag(rec)) {
342 rec_init_offset_old_1byte(rec, index, offsets, row_version);
343 } else {
344 rec_init_offset_old_2byte(rec, index, offsets, row_version);
345 }
346 }
347
348 void rec_init_offsets(const rec_t *rec, const dict_index_t *index,
349 ulint *offsets) {
350 rec_offs_make_valid(rec, index, offsets);
351
352 if (dict_table_is_comp(index->table)) {
353 rec_init_offsets_new(rec, index, offsets);
354 } else {
355 rec_init_offsets_old(rec, index, offsets);
356 }
357 }
358
359 ulint *rec_get_offsets(const rec_t *rec, const dict_index_t *index,
360 ulint *offsets, ulint n_fields, ut::Location location,
361 mem_heap_t **heap) {
362 ulint n;
363
364 ut_ad(rec);
365 ut_ad(index);
366 ut_ad(heap);
367
368 if (dict_table_is_comp(index->table)) {
369 switch (UNIV_EXPECT(rec_get_status(rec), REC_STATUS_ORDINARY)) {
370 case REC_STATUS_ORDINARY:
371 n = dict_index_get_n_fields(index);
372 break;
373 case REC_STATUS_NODE_PTR:
374 /* Node pointer records consist of the
375 uniquely identifying fields of the record
376 followed by a child page number field. */
377 n = dict_index_get_n_unique_in_tree_nonleaf(index) + 1;
378 break;
379 case REC_STATUS_INFIMUM:
380 case REC_STATUS_SUPREMUM:
381 /* infimum or supremum record */
382 n = 1;
383 break;
384 default:
385 ut_error;
386 }
387 } else {
388 n = rec_get_n_fields_old(rec, index);
389 }
390
391 if (UNIV_UNLIKELY(n_fields < n)) {
392 n = n_fields;
393 }
394
395 /* The offsets header consists of the allocation size at
396 offsets[0] and the REC_OFFS_HEADER_SIZE bytes. */
397 ulint size = n + (1 + REC_OFFS_HEADER_SIZE);
398
399 if (UNIV_UNLIKELY(!offsets) ||
400 UNIV_UNLIKELY(rec_offs_get_n_alloc(offsets) < size)) {
401 if (UNIV_UNLIKELY(!*heap)) {
402 *heap = mem_heap_create(size * sizeof(ulint), location);
403 }
404 offsets = static_cast<ulint *>(mem_heap_alloc(*heap, size * sizeof(ulint)));
405
406 rec_offs_set_n_alloc(offsets, size);
407 }
408
409 rec_offs_set_n_fields(offsets, n);
410 rec_init_offsets(rec, index, offsets);
411 return (offsets);
412 }
413
414 /** The following function determines the offsets to each field
415 in the record. It can reuse a previously allocated array. */
416 void rec_get_offsets_reverse(
417 const byte *extra, /*!< in: the extra bytes of a
418 compact record in reverse order,
419 excluding the fixed-size
420 REC_N_NEW_EXTRA_BYTES */
421 const dict_index_t *index, /*!< in: record descriptor */
422 ulint node_ptr, /*!< in: nonzero=node pointer,
423 0=leaf node */
424 ulint *offsets) /*!< in/out: array consisting of
425 offsets[0] allocated elements */
426 {
427 ulint n;
428 ulint i;
429 ulint offs;
430 ulint any_ext;
431 const byte *nulls;
432 const byte *lens;
433 dict_field_t *field;
434 ulint null_mask;
435 ulint n_node_ptr_field;
436
437 ut_ad(extra);
438 ut_ad(index);
439 ut_ad(offsets);
440 ut_ad(dict_table_is_comp(index->table));
441
442 if (UNIV_UNLIKELY(node_ptr)) {
443 n_node_ptr_field = dict_index_get_n_unique_in_tree_nonleaf(index);
444 n = n_node_ptr_field + 1;
445 } else {
446 n_node_ptr_field = ULINT_UNDEFINED;
447 n = dict_index_get_n_fields(index);
448 }
449
450 ut_a(rec_offs_get_n_alloc(offsets) >= n + (1 + REC_OFFS_HEADER_SIZE));
451 rec_offs_set_n_fields(offsets, n);
452
453 nulls = extra;
454 lens = nulls + UT_BITS_IN_BYTES(index->n_nullable);
455 i = offs = 0;
456 null_mask = 1;
457 any_ext = 0;
458
459 /* read the lengths of fields 0..n */
460 do {
461 ulint len;
462 if (UNIV_UNLIKELY(i == n_node_ptr_field)) {
463 len = offs += REC_NODE_PTR_SIZE;
464 goto resolved;
465 }
466
467 field = index->get_field(i);
468 if (!(field->col->prtype & DATA_NOT_NULL)) {
469 /* nullable field => read the null flag */
470
471 if (UNIV_UNLIKELY(!(byte)null_mask)) {
472 nulls++;
473 null_mask = 1;
474 }
475
476 if (*nulls & null_mask) {
477 null_mask <<= 1;
478 /* No length is stored for NULL fields.
479 We do not advance offs, and we set
480 the length to zero and enable the
481 SQL NULL flag in offsets[]. */
482 len = offs | REC_OFFS_SQL_NULL;
483 goto resolved;
484 }
485 null_mask <<= 1;
486 }
487
488 if (UNIV_UNLIKELY(!field->fixed_len)) {
489 /* Variable-length field: read the length */
490 const dict_col_t *col = field->col;
491 len = *lens++;
492 /* If the maximum length of the field is up
493 to 255 bytes, the actual length is always
494 stored in one byte. If the maximum length is
495 more than 255 bytes, the actual length is
496 stored in one byte for 0..127. The length
497 will be encoded in two bytes when it is 128 or
498 more, or when the field is stored externally. */
499 if (DATA_BIG_COL(col)) {
500 if (len & 0x80) {
501 /* 1exxxxxxx xxxxxxxx */
502 len <<= 8;
503 len |= *lens++;
504
505 offs += len & 0x3fff;
506 if (UNIV_UNLIKELY(len & 0x4000)) {
507 any_ext = REC_OFFS_EXTERNAL;
508 len = offs | REC_OFFS_EXTERNAL;
509 } else {
510 len = offs;
511 }
512
513 goto resolved;
514 }
515 }
516
517 len = offs += len;
518 } else {
519 len = offs += field->fixed_len;
520 }
521 resolved:
522 rec_offs_base(offsets)[i + 1] = len;
523 } while (++i < rec_offs_n_fields(offsets));
524
525 ut_ad(lens >= extra);
526 *rec_offs_base(offsets) =
527 (lens - extra + REC_N_NEW_EXTRA_BYTES) | REC_OFFS_COMPACT | any_ext;
528 }
529
530 #ifdef UNIV_DEBUG
531 /** Check if the given two record offsets are identical.
532 @param[in] offsets1 field offsets of a record
533 @param[in] offsets2 field offsets of a record
534 @return true if they are identical, false otherwise. */
535 bool rec_offs_cmp(ulint *offsets1, ulint *offsets2) {
536 ulint n1 = rec_offs_n_fields(offsets1);
537 ulint n2 = rec_offs_n_fields(offsets2);
538
539 if (n1 != n2) {
540 return (false);
541 }
542
543 for (ulint i = 0; i < n1; ++i) {
544 ulint len_1;
545 /* nullptr for index as we compare all the fields, so it doesn't matter */
546 ulint field_offset_1 = rec_get_nth_field_offs(nullptr, offsets1, i, &len_1);
547
548 ulint len_2;
549 /* nullptr for index as we compare all the fields, so it doesn't matter */
550 ulint field_offset_2 = rec_get_nth_field_offs(nullptr, offsets2, i, &len_2);
551
552 if (field_offset_1 != field_offset_2) {
553 return (false);
554 }
555
556 if (len_1 != len_2) {
557 return (false);
558 }
559 }
560 return (true);
561 }
562
563 /** Print the record offsets.
564 @param[in] out the output stream to which offsets are printed.
565 @param[in] offsets the field offsets of the record.
566 @return the output stream. */
567 std::ostream &rec_offs_print(std::ostream &out, const ulint *offsets) {
568 ulint n = rec_offs_n_fields(offsets);
569
570 out << "[rec offsets: &offsets[0]=" << (void *)&offsets[0] << ", n=" << n
571 << std::endl;
572 for (ulint i = 0; i < n; ++i) {
573 ulint len;
574 /* nullptr for index as we print all the fields, so it doesn't matter */
575 ulint field_offset = rec_get_nth_field_offs(nullptr, offsets, i, &len);
576 out << "i=" << i << ", offsets[" << i << "]=" << offsets[i]
577 << ", field_offset=" << field_offset << ", len=" << len << std::endl;
578 }
579 out << "]" << std::endl;
580 return (out);
581 }
582
583 #endif /* UNIV_DEBUG */