Debian Codegraph @

storage/innobase/rem/rem0rec.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/rem0rec.cc
29 Record manager
30
31 Created 5/30/1994 Heikki Tuuri
32 *************************************************************************/
33
34 #include "rem0rec.h"
35 #include "rem0lrec.h"
36
37 #include <sys/types.h>
38 #ifndef UNIV_HOTBACKUP
39
40 #include "data0data.h"
41 #include "fts0fts.h"
42 #endif /* !UNIV_HOTBACKUP */
43 #include "gis0geo.h"
44 #include "mach0data.h"
45 #include "mtr0log.h"
46 #include "mtr0mtr.h"
47 #include "page0page.h"
48 #include "trx0sys.h"
49
50 #include "my_dbug.h"
51
52 /* PHYSICAL RECORD (OLD STYLE)
53 ===========================
54
55 The physical record, which is the data type of all the records
56 found in index pages of the database, has the following format
57 (lower addresses and more significant bits inside a byte are below
58 represented on a higher text line):
59
60 | offset of the end of the last field of data, the most significant
61 bit is set to 1 if and only if the field is SQL-null,
62 if the offset is 2-byte, then the second most significant
63 bit is set to 1 if the field is stored on another page:
64 mostly this will occur in the case of big BLOB fields |
65 ...
66 | offset of the end of the first field of data + the SQL-null bit |
67 | 4 bits used to delete mark a record, and mark a predefined
68 minimum record in alphabetical order |
69 | 4 bits giving the number of records owned by this record
70 (this term is explained in page0page.h) |
71 | 13 bits giving the order number of this record in the
72 heap of the index page |
73 | 10 bits giving the number of fields in this record |
74 | 1 bit which is set to 1 if the offsets above are given in
75 one byte format, 0 if in two byte format |
76 | two bytes giving an absolute pointer to the next record in the page |
77 ORIGIN of the record
78 | first field of data |
79 ...
80 | last field of data |
81
82 The origin of the record is the start address of the first field
83 of data. The offsets are given relative to the origin.
84 The offsets of the data fields are stored in an inverted
85 order because then the offset of the first fields are near the
86 origin, giving maybe a better processor cache hit rate in searches.
87
88 The offsets of the data fields are given as one-byte
89 (if there are less than 127 bytes of data in the record)
90 or two-byte unsigned integers. The most significant bit
91 is not part of the offset, instead it indicates the SQL-null
92 if the bit is set to 1. */
93
94 /* PHYSICAL RECORD (NEW STYLE)
95 ===========================
96
97 The physical record, which is the data type of all the records
98 found in index pages of the database, has the following format
99 (lower addresses and more significant bits inside a byte are below
100 represented on a higher text line):
101
102 | length of the last non-null variable-length field of data:
103 if the maximum length is 255, one byte; otherwise,
104 0xxxxxxx (one byte, length=0..127), or 1exxxxxxxxxxxxxx (two bytes,
105 length=128..16383, extern storage flag) |
106 ...
107 | length of first variable-length field of data |
108 | SQL-null flags (1 bit per nullable field), padded to full bytes |
109 | 1 or 2 bytes to indicate number of fields in the record if the table
110 where the record resides has undergone an instant ADD COLUMN
111 before this record gets inserted; If no instant ADD COLUMN ever
112 happened, here should be no byte; So parsing this optional number
113 requires the index or table information |
114 | 4 bits used to delete mark a record, and mark a predefined
115 minimum record in alphabetical order |
116 | 4 bits giving the number of records owned by this record
117 (this term is explained in page0page.h) |
118 | 13 bits giving the order number of this record in the
119 heap of the index page |
120 | 3 bits record type: 000=conventional, 001=node pointer (inside B-tree),
121 010=infimum, 011=supremum, 1xx=reserved |
122 | two bytes giving a relative pointer to the next record in the page |
123 ORIGIN of the record
124 | first field of data |
125 ...
126 | last field of data |
127
128 The origin of the record is the start address of the first field
129 of data. The offsets are given relative to the origin.
130 The offsets of the data fields are stored in an inverted
131 order because then the offset of the first fields are near the
132 origin, giving maybe a better processor cache hit rate in searches.
133
134 The offsets of the data fields are given as one-byte
135 (if there are less than 127 bytes of data in the record)
136 or two-byte unsigned integers. The most significant bit
137 is not part of the offset, instead it indicates the SQL-null
138 if the bit is set to 1. */
139
140 /* CANONICAL COORDINATES. A record can be seen as a single
141 string of 'characters' in the following way: catenate the bytes
142 in each field, in the order of fields. An SQL-null field
143 is taken to be an empty sequence of bytes. Then after
144 the position of each field insert in the string
145 the 'character' <FIELD-END>, except that after an SQL-null field
146 insert <NULL-FIELD-END>. Now the ordinal position of each
147 byte in this canonical string is its canonical coordinate.
148 So, for the record ("AA", SQL-NULL, "BB", ""), the canonical
149 string is "AA<FIELD_END><NULL-FIELD-END>BB<FIELD-END><FIELD-END>".
150 We identify prefixes (= initial segments) of a record
151 with prefixes of the canonical string. The canonical
152 length of the prefix is the length of the corresponding
153 prefix of the canonical string. The canonical length of
154 a record is the length of its canonical string.
155
156 For example, the maximal common prefix of records
157 ("AA", SQL-NULL, "BB", "C") and ("AA", SQL-NULL, "B", "C")
158 is "AA<FIELD-END><NULL-FIELD-END>B", and its canonical
159 length is 5.
160
161 A complete-field prefix of a record is a prefix which ends at the
162 end of some field (containing also <FIELD-END>).
163 A record is a complete-field prefix of another record, if
164 the corresponding canonical strings have the same property. */
165
166 /** Validates the consistency of an old-style physical record.
167 @param[in] rec physical record
168 @return true if ok */
169 static bool rec_validate_old(const rec_t *rec);
170
171 /** Determine how many of the first n columns in a compact
172 physical record are stored externally.
173 @return number of externally stored columns */
174 ulint rec_get_n_extern_new(
175 const rec_t *rec, /*!< in: compact physical record */
176 const dict_index_t *index, /*!< in: record descriptor */
177 ulint n) /*!< in: number of columns to scan */
178 {
179 const byte *nulls;
180 const byte *lens;
181 ulint null_mask;
182 ulint n_extern;
183 ulint i;
184
185 ut_ad(dict_table_is_comp(index->table));
186 ut_ad(rec_get_status(rec) == REC_STATUS_ORDINARY);
187 ut_ad(n == ULINT_UNDEFINED || n <= dict_index_get_n_fields(index));
188
189 if (n == ULINT_UNDEFINED) {
190 n = dict_index_get_n_fields(index);
191 }
192
193 nulls = rec - (REC_N_NEW_EXTRA_BYTES + 1);
194 lens = nulls - UT_BITS_IN_BYTES(index->n_nullable);
195 null_mask = 1;
196 n_extern = 0;
197 i = 0;
198
199 /* read the lengths of fields 0..n */
200 do {
201 const dict_field_t *field = index->get_field(i);
202 const dict_col_t *col = field->col;
203 ulint len;
204
205 if (!(col->prtype & DATA_NOT_NULL)) {
206 /* nullable field => read the null flag */
207
208 if (UNIV_UNLIKELY(!(byte)null_mask)) {
209 nulls--;
210 null_mask = 1;
211 }
212
213 if (*nulls & null_mask) {
214 null_mask <<= 1;
215 /* No length is stored for NULL fields. */
216 continue;
217 }
218 null_mask <<= 1;
219 }
220
221 if (UNIV_UNLIKELY(!field->fixed_len)) {
222 /* Variable-length field: read the length */
223 len = *lens--;
224 /* If the maximum length of the field is up
225 to 255 bytes, the actual length is always
226 stored in one byte. If the maximum length is
227 more than 255 bytes, the actual length is
228 stored in one byte for 0..127. The length
229 will be encoded in two bytes when it is 128 or
230 more, or when the field is stored externally. */
231 if (DATA_BIG_COL(col)) {
232 if (len & 0x80) {
233 /* 1exxxxxxx xxxxxxxx */
234 if (len & 0x40) {
235 n_extern++;
236 }
237 lens--;
238 }
239 }
240 }
241 } while (++i < n);
242
243 return (n_extern);
244 }
245
246 bool is_store_version(const dict_index_t *index, size_t n_tuple_fields) {
247 if (!index->has_instant_cols_or_row_versions()) return false;
248
249 ut_ad(index->table->has_row_versions() ||
250 index->table->is_upgraded_instant());
251
252 ut_ad(n_tuple_fields > 0);
253 /* From an UPDATE when not materializing INSTANT defaults */
254 if (n_tuple_fields < index->n_fields) {
255 ut_ad(index->has_instant_cols());
256 return false;
257 }
258
259 return true;
260 }
261
262 /* Calculate nullable fields which will be there on COMPACT/DYNAMIC format.
263 @param[in] index record descriptor
264 @param[in] n_fields number of fields in tuple
265 @param[in] rec_version version in which record to be stored.
266 @return number of nullable fileds in the physical record. */
267 static size_t get_nullable_fields_for_rec(const dict_index_t *index,
268 const size_t n_fields,
269 uint8_t &rec_version) {
270 const bool is_valid_version = is_valid_row_version(rec_version);
271 size_t nullable_fields = 0;
272
273 if (!is_valid_version) {
274 /* Invalid version. Temp record for redundnat format. Record being logged
275 doesn't have version. It will be stored in version=0 */
276 rec_version = 0;
277 nullable_fields = index->get_nullable_in_version(rec_version);
278 return nullable_fields;
279 }
280
281 if (index->has_row_versions()) {
282 /* Table has version. New records will be stored in latest format. */
283 ut_ad(is_valid_version);
284 nullable_fields = index->get_nullable_in_version(rec_version);
285 } else if (index->has_instant_cols()) {
286 /* Table has no version. New records will be written as of old way. */
287 nullable_fields =
288 index->get_n_nullable_before(static_cast<uint32_t>(n_fields));
289 } else {
290 /* Table has no version no instant. All the fields will be there. */
291 nullable_fields = index->n_nullable;
292 }
293
294 return nullable_fields;
295 }
296
297 /** Determines the size of a data tuple prefix in ROW_FORMAT=COMPACT.
298 @param[in] index record descriptor, dict_table_is_comp() is
299 assumed to hold, even if it does not
300 @param[in] fields array of data fields
301 @param[in] n_fields number of data fields
302 @param[in] v_entry dtuple contains virtual column data
303 @param[out] extra extra size
304 @param[in] status status bits of the record, can be nullptr if
305 unnecessary
306 @param[in] temp whether this is a temporary file record
307 @param[in] rec_version rec version if INSTANT
308 @return total size */
309 [[nodiscard]] static inline ulint rec_get_converted_size_comp_prefix_low(
310 const dict_index_t *index, const dfield_t *fields, ulint n_fields,
311 const dtuple_t *v_entry, ulint *extra, ulint *status, bool temp,
312 uint8_t rec_version) {
313 ut_ad(n_fields <= dict_index_get_n_fields(index));
314 ut_ad(!temp || extra);
315
316 /* At the time being, only temp file record could possible
317 store virtual columns */
318 ut_ad(!v_entry || (index->is_clustered() && temp));
319 ulint n_v_fields = v_entry ? dtuple_get_n_v_fields(v_entry) : 0;
320
321 #ifdef UNIV_DEBUG
322 /* INVALID vesion. Possible for temp record for redundant format. */
323 ut_ad(is_valid_row_version(rec_version) ||
324 (temp && !dict_table_is_comp(index->table)));
325 #endif
326
327 ulint n_null = 0;
328 if (n_fields > 0) {
329 n_null = get_nullable_fields_for_rec(index, n_fields, rec_version);
330 }
331
332 ulint extra_size = 0;
333 if (index->is_tuple_instant_format(n_fields) && status != nullptr) {
334 switch (UNIV_EXPECT(*status, REC_STATUS_ORDINARY)) {
335 case REC_STATUS_ORDINARY:
336 ut_ad(!temp && n_fields > 0);
337 if (index->has_instant_cols_or_row_versions()) {
338 ut_ad(index->table->has_row_versions() ||
339 index->table->is_upgraded_instant());
340 if (is_store_version(index, n_fields)) {
341 /* We need only 1 byte now to store the version. */
342 extra_size += 1;
343 } else {
344 ut_ad(index->has_instant_cols());
345 extra_size += rec_get_n_fields_length(n_fields);
346 }
347 }
348 break;
349 case REC_STATUS_NODE_PTR:
350 ut_ad(!temp && n_fields > 0);
351 n_null = index->get_nullable_before_instant_add_drop();
352 break;
353 case REC_STATUS_INFIMUM:
354 case REC_STATUS_SUPREMUM:
355 break;
356 }
357 }
358
359 extra_size += temp ? UT_BITS_IN_BYTES(n_null)
360 : REC_N_NEW_EXTRA_BYTES + UT_BITS_IN_BYTES(n_null);
361
362 if (temp && dict_table_is_comp(index->table)) {
363 /* No need to do adjust fixed_len=0. We only need to
364 adjust it for ROW_FORMAT=REDUNDANT. */
365 temp = false;
366 }
367
368 ulint data_size = 0;
369 /* read the lengths of fields 0..n */
370 for (size_t i = 0; i < n_fields; i++) {
371 const dict_field_t *field;
372 ulint len;
373 ulint fixed_len;
374 const dict_col_t *col;
375
376 field = index->get_field(i);
377 len = dfield_get_len(&fields[i]);
378 col = field->col;
379
380 /* Skip the columns which are not in the version */
381 if (!col->is_visible_in_version(rec_version)) {
382 continue;
383 }
384
385 #ifdef UNIV_DEBUG
386 dtype_t *type;
387
388 type = dfield_get_type(&fields[i]);
389 if (dict_index_is_spatial(index)) {
390 if (DATA_GEOMETRY_MTYPE(col->mtype) && i == 0) {
391 ut_ad(type->prtype & DATA_GIS_MBR);
392 } else {
393 ut_ad(type->mtype == DATA_SYS_CHILD || col->assert_equal(type));
394 }
395 } else {
396 ut_ad(col->assert_equal(type));
397 }
398 #endif
399
400 /* All NULLable fields must be included in the n_null count. */
401 ut_ad((col->prtype & DATA_NOT_NULL) || n_null--);
402
403 if (dfield_is_null(&fields[i])) {
404 /* No length is stored for NULL fields. */
405 ut_ad(!(col->prtype & DATA_NOT_NULL));
406 continue;
407 }
408
409 ut_ad(len <= col->len || DATA_LARGE_MTYPE(col->mtype) ||
410 (DATA_POINT_MTYPE(col->mtype) && len == DATA_MBR_LEN) ||
411 (col->len == 0 && col->mtype == DATA_VARCHAR));
412
413 fixed_len = field->fixed_len;
414 if (temp && fixed_len && !col->get_fixed_size(temp)) {
415 fixed_len = 0;
416 }
417 /* If the maximum length of a variable-length field
418 is up to 255 bytes, the actual length is always stored
419 in one byte. If the maximum length is more than 255
420 bytes, the actual length is stored in one byte for
421 0..127. The length will be encoded in two bytes when
422 it is 128 or more, or when the field is stored externally. */
423
424 if (fixed_len) {
425 #ifdef UNIV_DEBUG
426 ulint mbminlen = DATA_MBMINLEN(col->mbminmaxlen);
427 ulint mbmaxlen = DATA_MBMAXLEN(col->mbminmaxlen);
428
429 ut_ad(len <= fixed_len);
430
431 if (dict_index_is_spatial(index)) {
432 ut_ad(type->mtype == DATA_SYS_CHILD || !mbmaxlen ||
433 len >= mbminlen * (fixed_len / mbmaxlen));
434 } else {
435 ut_ad(type->mtype != DATA_SYS_CHILD);
436 ut_ad(!mbmaxlen || len >= mbminlen * (fixed_len / mbmaxlen));
437 }
438
439 /* dict_index_add_col() should guarantee this */
440 ut_ad(!field->prefix_len || fixed_len == field->prefix_len);
441 #endif /* UNIV_DEBUG */
442 } else if (dfield_is_ext(&fields[i])) {
443 ut_ad(DATA_BIG_COL(col));
444 extra_size += 2;
445 } else if (len < 128 || !DATA_BIG_COL(col)) {
446 extra_size++;
447 } else {
448 /* For variable-length columns, we look up the
449 maximum length from the column itself. If this
450 is a prefix index column shorter than 256 bytes,
451 this will waste one byte. */
452 extra_size += 2;
453 }
454 data_size += len;
455 }
456
457 if (extra) {
458 *extra = extra_size;
459 }
460
461 /* Log virtual columns */
462 if (n_v_fields != 0) {
463 /* length marker */
464 data_size += 2;
465
466 for (size_t i = 0; i < n_v_fields; i++) {
467 dfield_t *vfield;
468 ulint flen;
469
470 const dict_v_col_t *col = dict_table_get_nth_v_col(index->table, i);
471
472 /* Only those indexed needs to be logged */
473 if (col->m_col.ord_part || !dict_table_is_comp(index->table)) {
474 data_size += mach_get_compressed_size(i + REC_MAX_N_FIELDS);
475 vfield = dtuple_get_nth_v_field(v_entry, col->v_pos);
476
477 if (dfield_is_multi_value(vfield)) {
478 Multi_value_logger mv_logger(
479 static_cast<multi_value_data *>(dfield_get_data(vfield)),
480 dfield_get_len(vfield));
481 data_size += mv_logger.get_log_len(true);
482 } else {
483 flen = vfield->len;
484
485 if (flen != UNIV_SQL_NULL) {
486 flen = std::min(
487 flen,
488 static_cast<ulint>(DICT_MAX_FIELD_LEN_BY_FORMAT(index->table)));
489 data_size += flen;
490 }
491
492 data_size += mach_get_compressed_size(flen);
493 }
494 }
495 }
496 }
497
498 return (extra_size + data_size);
499 }
500
501 /** Determines the size of a data tuple prefix in ROW_FORMAT=COMPACT.
502 @return total size */
503 ulint rec_get_converted_size_comp_prefix(
504 const dict_index_t *index, /*!< in: record descriptor */
505 const dfield_t *fields, /*!< in: array of data fields */
506 ulint n_fields, /*!< in: number of data fields */
507 ulint *extra) /*!< out: extra size */
508 {
509 ut_ad(dict_table_is_comp(index->table));
510 return (rec_get_converted_size_comp_prefix_low(
511 index, fields, n_fields, nullptr, extra, nullptr, false,
512 index->table->current_row_version));
513 }
514
515 /** Determines the size of a data tuple in ROW_FORMAT=COMPACT.
516 @return total size */
517 ulint rec_get_converted_size_comp(
518 const dict_index_t *index, /*!< in: record descriptor;
519 dict_table_is_comp() is
520 assumed to hold, even if
521 it does not */
522 ulint status, /*!< in: status bits of the record */
523 const dfield_t *fields, /*!< in: array of data fields */
524 ulint n_fields, /*!< in: number of data fields */
525 ulint *extra) /*!< out: extra size */
526 {
527 ulint size;
528 ut_ad(n_fields > 0);
529
530 switch (UNIV_EXPECT(status, REC_STATUS_ORDINARY)) {
531 case REC_STATUS_ORDINARY:
532 /* If this is a record for instant index, it could has
533 less fields when it comes from update path */
534 ut_ad(n_fields == dict_index_get_n_fields(index) ||
535 index->has_instant_cols_or_row_versions());
536 size = 0;
537 break;
538 case REC_STATUS_NODE_PTR:
539 n_fields--;
540 ut_ad(n_fields == dict_index_get_n_unique_in_tree_nonleaf(index));
541 ut_ad(dfield_get_len(&fields[n_fields]) == REC_NODE_PTR_SIZE);
542 size = REC_NODE_PTR_SIZE; /* child page number */
543 break;
544 case REC_STATUS_INFIMUM:
545 case REC_STATUS_SUPREMUM:
546 /* infimum or supremum record, 8 data bytes */
547 if (UNIV_LIKELY_NULL(extra)) {
548 *extra = REC_N_NEW_EXTRA_BYTES;
549 }
550 return (REC_N_NEW_EXTRA_BYTES + 8);
551 default:
552 ut_error;
553 }
554
555 return (size + rec_get_converted_size_comp_prefix_low(
556 index, fields, n_fields, nullptr, extra, &status, false,
557 index->table->current_row_version));
558 }
559
560 /** Builds an old-style physical record out of a data tuple and
561 stores it beginning from the start of the given buffer.
562 @param[in] buf start address of the physical record
563 @param[in] index record descriptor
564 @param[in] dtuple data tuple
565 @return pointer to the origin of physical record */
566 static rec_t *rec_convert_dtuple_to_rec_old(byte *buf,
567 const dict_index_t *index,
568 const dtuple_t *dtuple) {
569 ut_ad(buf && dtuple);
570 ut_ad(dtuple_validate(dtuple));
571 ut_ad(dtuple_check_typed(dtuple));
572
573 ulint n_fields = dtuple_get_n_fields(dtuple);
574 ulint data_size = dtuple_get_data_size(dtuple, 0);
575
576 ut_ad(n_fields > 0);
577
578 /* Calculate the offset of the origin in the physical record */
579 const bool has_ext = dtuple->has_ext();
580 rec_t *rec = buf + rec_get_converted_extra_size(data_size, n_fields, has_ext);
581
582 bool store_version = false;
583 {
584 /* dtuple->info bit contains info about record status. */
585 bool is_leaf_record =
586 (dtuple_get_info_bits(dtuple) & 0x03UL) == REC_STATUS_ORDINARY;
587 if (is_leaf_record &&
588 is_store_version(index, dtuple_get_n_fields(dtuple))) {
589 /* We need 1 byte to store the version info in record header */
590 rec++;
591 store_version = true;
592 }
593 }
594
595 #ifdef UNIV_DEBUG
596 /* Suppress Valgrind warnings of ut_ad()
597 in mach_write_to_1(), mach_write_to_2() et al. */
598 memset(buf, 0xff, rec - buf + data_size);
599 #endif /* UNIV_DEBUG */
600 /* Store the number of fields */
601 rec_set_n_fields_old(rec, n_fields);
602
603 /* Set the info bits of the record */
604 rec_set_info_bits_old(rec, dtuple_get_info_bits(dtuple) & REC_INFO_BITS_MASK);
605
606 /* if record has version, store it */
607 if (store_version) {
608 rec_set_instant_row_version_old(rec, index->table->current_row_version);
609 /* Set the info bit to indicate this record has version */
610 rec_old_set_versioned(rec, true);
611 } else {
612 rec_old_set_versioned(rec, false);
613 }
614
615 /* Store the data and the offsets */
616 bool no_ext_and_size_less_1_byte_off_limit = false;
617 if (!has_ext && data_size <= REC_1BYTE_OFFS_LIMIT) {
618 no_ext_and_size_less_1_byte_off_limit = true;
619 }
620
621 if (no_ext_and_size_less_1_byte_off_limit) {
622 rec_set_1byte_offs_flag(rec, true);
623 } else {
624 rec_set_1byte_offs_flag(rec, false);
625 }
626
627 ulint end_offset = 0;
628
629 auto store_field = [&](const dfield_t *field, ulint i) -> void {
630 ulint ored_offset;
631 ulint len;
632
633 if (dfield_is_null(field)) {
634 len = dtype_get_sql_null_size(dfield_get_type(field), 0);
635 data_write_sql_null(rec + end_offset, len);
636
637 end_offset += len;
638 if (no_ext_and_size_less_1_byte_off_limit) {
639 ored_offset = end_offset | REC_1BYTE_SQL_NULL_MASK;
640 } else {
641 ored_offset = end_offset | REC_2BYTE_SQL_NULL_MASK;
642 }
643 } else {
644 /* If the data is not SQL null, store it */
645 len = dfield_get_len(field);
646
647 memcpy(rec + end_offset, dfield_get_data(field), len);
648
649 end_offset += len;
650 ored_offset = end_offset;
651
652 if (dfield_is_ext(field)) {
653 ored_offset |= REC_2BYTE_EXTERN_MASK;
654 }
655 }
656
657 if (no_ext_and_size_less_1_byte_off_limit) {
658 rec_1_set_field_end_info_low(rec, i, ored_offset);
659 } else {
660 rec_2_set_field_end_info_low(rec, i, ored_offset);
661 }
662 };
663
664 size_t n_inst_dropped = 0;
665 size_t n_fields_stored = 0;
666 if (index->has_instant_cols_or_row_versions()) {
667 ut_ad(index->is_clustered());
668 size_t n_total_fields = index->get_n_total_fields();
669
670 /* Traverse index fields in physical order and keep storing values from
671 logical dtuple */
672 for (size_t i = 0; i < n_total_fields && n_fields_stored < n_fields; i++) {
673 dict_field_t *phy_dict_field = index->get_physical_field(i);
674
675 if (phy_dict_field->col->is_instant_dropped()) {
676 n_inst_dropped++;
677 continue;
678 }
679
680 size_t logical_i = (phy_dict_field - index->fields);
681
682 const dfield_t *field = dtuple_get_nth_field(dtuple, logical_i);
683 store_field(field, i - n_inst_dropped);
684 n_fields_stored++;
685 }
686
687 /* Correct the number of stored fields on rec */
688 ut_ad(n_fields >= n_fields_stored);
689 if (n_fields > n_fields_stored) {
690 ut_ad(n_inst_dropped > 0);
691 ut_ad(index->table->has_instant_drop_cols());
692 n_fields = n_fields - n_inst_dropped;
693
694 ut_ad(index->n_fields == n_fields);
695 ut_ad(index->n_total_fields > n_fields);
696 rec_set_n_fields_old(rec, n_fields);
697 }
698 } else {
699 for (ulint i = 0; i < n_fields; i++) {
700 const dfield_t *field = dtuple_get_nth_field(dtuple, i);
701 store_field(field, i);
702 }
703 }
704
705 return (rec);
706 }
707
708 /* A temp record, generated for a REDUNDANT row record, will have info bits
709 iff table has INSTANT ADD columns. And if record has row version, then it will
710 also be stored on temp record header. Following function finds the number of
711 more bytes needed in record header to store this info.
712 @param[in] index record descriptor
713 @param[in] valid_version true if record has version
714 @return number of bytes NULL pointer should be adjusted. */
715 size_t get_extra_bytes_for_temp_redundant(const dict_index_t *index,
716 bool valid_version) {
717 if (!index->has_instant_cols_or_row_versions()) {
718 return 0;
719 }
720
721 size_t bytes_needed = 0;
722
723 /* temp record must have info bits */
724 bytes_needed++;
725
726 if (valid_version) {
727 bytes_needed++;
728 }
729
730 return bytes_needed;
731 }
732
733 /** Builds a ROW_FORMAT=COMPACT record out of a data tuple.
734 @param[in, out] rec origin of record
735 @param[in] index record descriptor
736 @param[in] fields array of data fields
737 @param[in] n_fields number of data fields
738 @param[in] v_entry dtuple contains virtual column data
739 @param[in] status status bits of the record
740 @param[in] temp whether to use the format for temporary
741 files in index creation
742 @param[in] rec_version rec version (could be 0 also)
743 @return record instant information for record on leaf page */
744 static inline Rec_instant_state rec_convert_dtuple_to_rec_comp(
745 rec_t *rec, const dict_index_t *index, const dfield_t *fields,
746 ulint n_fields, const dtuple_t *v_entry, ulint status, bool temp,
747 uint8_t rec_version) {
748 ut_ad(temp || dict_table_is_comp(index->table));
749
750 ulint num_v = v_entry ? dtuple_get_n_v_fields(v_entry) : 0;
751
752 const bool is_valid_version = is_valid_row_version(rec_version);
753 /* INVALID vesion. Possible for temp record for redundant format. */
754 ut_ad(is_valid_version || (temp && !dict_table_is_comp(index->table)));
755
756 ulint n_null = 0;
757 if (n_fields != 0) {
758 n_null = get_nullable_fields_for_rec(index, n_fields, rec_version);
759 }
760
761 byte *nulls = nullptr;
762 auto rec_instant_info = Rec_instant_state::REC_IS_SIMPLE;
763 ulint n_node_ptr_field;
764 if (temp) {
765 ut_ad(status == REC_STATUS_ORDINARY);
766 ut_ad(n_fields <= dict_index_get_n_fields(index));
767 n_node_ptr_field = ULINT_UNDEFINED;
768 nulls = rec - 1;
769
770 /* NOTE : For COMPACT/DYNAMIC record, rec pointer for temp rec is already
771 pointing to just before <row_verison><info-bits>, if needed. So no
772 adjustment is needed there. */
773
774 if (!dict_table_is_comp(index->table)) {
775 size_t bytes_needed =
776 get_extra_bytes_for_temp_redundant(index, is_valid_version);
777 if (bytes_needed > 0) {
778 rec_new_temp_set_versioned(rec, is_valid_version);
779 }
780 /* Move nulls accordingly */
781 nulls = nulls - bytes_needed;
782 }
783
784 if (dict_table_is_comp(index->table)) {
785 /* No need to do adjust fixed_len=0. We only
786 need to adjust it for ROW_FORMAT=REDUNDANT. */
787 temp = false;
788 }
789 } else {
790 ut_ad(v_entry == nullptr);
791 ut_ad(num_v == 0);
792 nulls = rec - (REC_N_NEW_EXTRA_BYTES + 1);
793
794 switch (UNIV_EXPECT(status, REC_STATUS_ORDINARY)) {
795 case REC_STATUS_ORDINARY:
796 ut_ad(n_fields <= dict_index_get_n_fields(index));
797 n_node_ptr_field = ULINT_UNDEFINED;
798
799 if (index->has_instant_cols_or_row_versions()) {
800 ut_ad(index->table->has_row_versions() ||
801 index->table->is_upgraded_instant());
802
803 if (is_store_version(index, n_fields)) {
804 /* Store current version info in one byte. */
805 rec_set_instant_row_version_new(rec,
806 index->table->current_row_version);
807
808 /* Shift pointer to null byte before the version */
809 nulls -= 1;
810 rec_instant_info = Rec_instant_state::REC_IS_VERSIONED;
811 } else {
812 ut_ad(index->has_instant_cols());
813 if (index->is_tuple_instant_format(n_fields)) {
814 /* Not materializing instant default. So need to store in V1 */
815 uint32_t n_fields_len = rec_set_n_fields(rec, n_fields);
816
817 /* Shift pointer to null byte before the number of fileds */
818 nulls -= n_fields_len;
819 rec_instant_info = Rec_instant_state::REC_IS_INSTANT;
820 }
821 }
822 }
823 break;
824 case REC_STATUS_NODE_PTR:
825 ut_ad(n_fields == dict_index_get_n_unique_in_tree_nonleaf(index) + 1U);
826 n_node_ptr_field = n_fields - 1;
827 n_null = index->get_nullable_before_instant_add_drop();
828 break;
829 case REC_STATUS_INFIMUM:
830 case REC_STATUS_SUPREMUM:
831 ut_ad(n_fields == 1);
832 n_node_ptr_field = ULINT_UNDEFINED;
833 break;
834 default:
835 ut_error;
836 }
837 }
838
839 byte *end = rec;
840
841 byte *lens = nullptr;
842 if (n_fields != 0) {
843 lens = nulls - UT_BITS_IN_BYTES(n_null);
844 /* clear the SQL-null flags */
845 memset(lens + 1, 0, nulls - lens);
846 }
847
848 /* Store the data and the offsets */
849 {
850 /* null_mask, points to the next field bit in null bitmap to be set if field
851 is NULL */
852 ulint null_mask = 1;
853
854 auto store_field = [&](const dfield_t *field, uint32_t pos) {
855 const dtype_t *type = dfield_get_type(field);
856 uint32_t len = dfield_get_len(field);
857
858 if (!(dtype_get_prtype(type) & DATA_NOT_NULL)) {
859 /* It's a nullable field */
860 ut_ad(n_null--);
861
862 if (UNIV_UNLIKELY(!(byte)null_mask)) {
863 (nulls)--;
864 null_mask = 1;
865 }
866
867 ut_ad(*nulls < null_mask);
868
869 /* Set the null bit if field is NULL */
870 if (dfield_is_null(field)) {
871 *nulls |= null_mask;
872 null_mask <<= 1;
873 return;
874 }
875
876 /* Set null_mask to point to the next field bit in the null bitmap */
877 null_mask <<= 1;
878 }
879
880 /* We reached here, field must not be null */
881 ut_ad(!dfield_is_null(field));
882
883 const dict_field_t *ifield = index->get_physical_field(pos);
884
885 uint32_t fixed_len = ifield->fixed_len;
886 dict_col_t *col = ifield->col;
887
888 if (temp && fixed_len && !col->get_fixed_size(temp)) {
889 fixed_len = 0;
890 }
891
892 /* If the maximum length of a variable-length field
893 is up to 255 bytes, the actual length is always stored
894 in one byte. If the maximum length is more than 255
895 bytes, the actual length is stored in one byte for
896 0..127. The length will be encoded in two bytes when
897 it is 128 or more, or when the field is stored externally. */
898 if (fixed_len) {
899 #ifdef UNIV_DEBUG
900 ulint mbminlen = DATA_MBMINLEN(col->mbminmaxlen);
901 ulint mbmaxlen = DATA_MBMAXLEN(col->mbminmaxlen);
902
903 ut_ad(len <= fixed_len);
904 ut_ad(!mbmaxlen || len >= mbminlen * (fixed_len / mbmaxlen));
905 ut_ad(!dfield_is_ext(field));
906 #endif /* UNIV_DEBUG */
907 } else if (dfield_is_ext(field)) {
908 ut_ad(DATA_BIG_COL(col));
909 ut_ad(len <=
910 REC_ANTELOPE_MAX_INDEX_COL_LEN + BTR_EXTERN_FIELD_REF_SIZE);
911 *lens = (byte)(len >> 8) | 0xc0;
912 lens--;
913 *lens = (byte)len;
914 lens--;
915 } else {
916 /* DATA_POINT would have a fixed_len */
917 ut_ad(dtype_get_mtype(type) != DATA_POINT);
918 #ifndef UNIV_HOTBACKUP
919 ut_ad(len <= dtype_get_len(type) ||
920 DATA_LARGE_MTYPE(dtype_get_mtype(type)) ||
921 !strcmp(index->name, FTS_INDEX_TABLE_IND_NAME));
922 #endif /* !UNIV_HOTBACKUP */
923 if (len < 128 ||
924 !DATA_BIG_LEN_MTYPE(dtype_get_len(type), dtype_get_mtype(type))) {
925 *lens = (byte)len;
926 lens--;
927 } else {
928 ut_ad(len < 16384);
929 *lens = (byte)(len >> 8) | 0x80;
930 lens--;
931 *lens = (byte)len;
932 lens--;
933 }
934 }
935
936 if (len > 0) memcpy(end, dfield_get_data(field), len);
937 end += len;
938 };
939
940 if (index->has_instant_cols_or_row_versions()) {
941 ut_ad(index->is_clustered());
942
943 size_t n_total_fields = index->get_n_total_fields();
944 size_t n_fields_stored = 0;
945
946 /* Traverse index fields in physical order and keep storing values from
947 logical dtuple */
948 for (size_t i = 0; i < n_total_fields && n_fields_stored < n_fields;
949 i++) {
950 size_t logical_i = i;
951
952 {
953 dict_field_t *phy_dict_field = index->get_physical_field(i);
954 if (!phy_dict_field->col->is_visible_in_version(rec_version)) {
955 continue;
956 }
957
958 logical_i = (phy_dict_field - index->fields);
959 }
960
961 const dfield_t *field = &fields[logical_i];
962 uint32_t len = dfield_get_len(field);
963
964 if (UNIV_UNLIKELY(logical_i == n_node_ptr_field)) {
965 ut_d(const dtype_t *type = dfield_get_type(field));
966 ut_ad(dtype_get_prtype(type) & DATA_NOT_NULL);
967 ut_ad(len == REC_NODE_PTR_SIZE);
968 memcpy(end, dfield_get_data(field), len);
969 end += REC_NODE_PTR_SIZE;
970 break;
971 }
972
973 store_field(field, i);
974 n_fields_stored++;
975 }
976 } else {
977 for (size_t i = 0; i < n_fields; i++) {
978 const dfield_t *field = &fields[i];
979 uint32_t len = dfield_get_len(field);
980
981 if (UNIV_UNLIKELY(i == n_node_ptr_field)) {
982 ut_d(const dtype_t *type = dfield_get_type(field));
983 ut_ad(dtype_get_prtype(type) & DATA_NOT_NULL);
984 ut_ad(len == REC_NODE_PTR_SIZE);
985 memcpy(end, dfield_get_data(field), len);
986 end += REC_NODE_PTR_SIZE;
987 break;
988 }
989 store_field(field, i);
990 }
991 }
992 }
993
994 if (!num_v) {
995 return rec_instant_info;
996 }
997
998 /* reserve 2 bytes for writing length */
999 byte *ptr = end;
1000 ptr += 2;
1001
1002 /* Now log information on indexed virtual columns */
1003 for (ulint col_no = 0; col_no < num_v; col_no++) {
1004 dfield_t *vfield;
1005 ulint flen;
1006
1007 const dict_v_col_t *col = dict_table_get_nth_v_col(index->table, col_no);
1008
1009 if (col->m_col.ord_part || !dict_table_is_comp(index->table)) {
1010 ulint pos = col_no;
1011
1012 pos += REC_MAX_N_FIELDS;
1013
1014 ptr += mach_write_compressed(ptr, pos);
1015
1016 vfield = dtuple_get_nth_v_field(v_entry, col->v_pos);
1017
1018 if (dfield_is_multi_value(vfield)) {
1019 Multi_value_logger mv_logger(
1020 static_cast<multi_value_data *>(dfield_get_data(vfield)),
1021 dfield_get_len(vfield));
1022 mv_logger.log(&ptr);
1023 } else {
1024 flen = vfield->len;
1025
1026 if (flen != UNIV_SQL_NULL) {
1027 /* The virtual column can only be in sec
1028 index, and index key length is bound by
1029 DICT_MAX_FIELD_LEN_BY_FORMAT */
1030 flen = std::min(
1031 flen,
1032 static_cast<ulint>(DICT_MAX_FIELD_LEN_BY_FORMAT(index->table)));
1033 }
1034
1035 ptr += mach_write_compressed(ptr, flen);
1036
1037 if (flen != UNIV_SQL_NULL) {
1038 ut_memcpy(ptr, dfield_get_data(vfield), flen);
1039 ptr += flen;
1040 }
1041 }
1042 }
1043 }
1044
1045 mach_write_to_2(end, ptr - end);
1046
1047 return rec_instant_info;
1048 }
1049
1050 /** Builds a new-style physical record out of a data tuple and stores it
1051 beginning from the start of the given buffer.
1052 @param[in] buf start address of the physical record
1053 @param[in] index record descriptor
1054 @param[in] dtuple data tuple
1055 @return pointer to the origin of physical record */
1056 static rec_t *rec_convert_dtuple_to_rec_new(byte *buf,
1057 const dict_index_t *index,
1058 const dtuple_t *dtuple) {
1059 ulint extra_size;
1060 ulint status;
1061 rec_t *rec;
1062
1063 status = dtuple_get_info_bits(dtuple) & REC_NEW_STATUS_MASK;
1064 rec_get_converted_size_comp(index, status, dtuple->fields, dtuple->n_fields,
1065 &extra_size);
1066 rec = buf + extra_size;
1067
1068 auto rec_state = rec_convert_dtuple_to_rec_comp(
1069 rec, index, dtuple->fields, dtuple->n_fields, nullptr, status, false,
1070 index->table->current_row_version);
1071
1072 /* Set the info bits of the record from dtuple */
1073 rec_set_info_and_status_bits(rec, dtuple_get_info_bits(dtuple));
1074
1075 switch (rec_state) {
1076 case Rec_instant_state::REC_IS_SIMPLE:
1077 rec_new_reset_instant_version(rec);
1078 break;
1079 case Rec_instant_state::REC_IS_VERSIONED:
1080 ut_a(index->has_instant_cols_or_row_versions());
1081 rec_new_set_versioned(rec);
1082 break;
1083 case Rec_instant_state::REC_IS_INSTANT:
1084 ut_a(index->has_instant_cols_or_row_versions());
1085 ut_a(index->table->has_instant_cols());
1086 rec_new_set_instant(rec);
1087 break;
1088 default:
1089 ut_error;
1090 }
1091
1092 /* Only one of the bit (INSTANT or VERSION) could be set */
1093 ut_a(!(rec_get_instant_flag_new(rec) && rec_new_is_versioned(rec)));
1094 return (rec);
1095 }
1096
1097 /** Builds a physical record out of a data tuple and
1098 stores it beginning from the start of the given buffer.
1099 @return pointer to the origin of physical record */
1100 rec_t *rec_convert_dtuple_to_rec(
1101 byte *buf, /*!< in: start address of the
1102 physical record */
1103 const dict_index_t *index, /*!< in: record descriptor */
1104 const dtuple_t *dtuple) /*!< in: data tuple */
1105 {
1106 rec_t *rec;
1107
1108 ut_ad(buf != nullptr);
1109 ut_ad(index != nullptr);
1110 ut_ad(dtuple != nullptr);
1111 ut_ad(dtuple_validate(dtuple));
1112 ut_ad(dtuple_check_typed(dtuple));
1113
1114 if (dict_table_is_comp(index->table)) {
1115 rec = rec_convert_dtuple_to_rec_new(buf, index, dtuple);
1116 } else {
1117 rec = rec_convert_dtuple_to_rec_old(buf, index, dtuple);
1118 }
1119
1120 #ifdef UNIV_DEBUG
1121 /* Can't check this if it's an index with instantly added columns,
1122 because if it comes from UPDATE, the fields of dtuple may be less than
1123 the on from index itself. */
1124 if (!index->has_instant_cols_or_row_versions()) {
1125 mem_heap_t *heap = nullptr;
1126 ulint offsets_[REC_OFFS_NORMAL_SIZE];
1127 const ulint *offsets;
1128 ulint i;
1129 rec_offs_init(offsets_);
1130
1131 offsets = rec_get_offsets(rec, index, offsets_, ULINT_UNDEFINED,
1132 UT_LOCATION_HERE, &heap);
1133 ut_ad(rec_validate(rec, offsets));
1134 ut_ad(dtuple_get_n_fields(dtuple) == rec_offs_n_fields(offsets));
1135
1136 for (i = 0; i < rec_offs_n_fields(offsets); i++) {
1137 ut_ad(!dfield_is_ext(dtuple_get_nth_field(dtuple, i)) ==
1138 !rec_offs_nth_extern(index, offsets, i));
1139 }
1140
1141 if (UNIV_LIKELY_NULL(heap)) {
1142 mem_heap_free(heap);
1143 }
1144 }
1145 #endif /* UNIV_DEBUG */
1146 return (rec);
1147 }
1148
1149 #ifndef UNIV_HOTBACKUP
1150 /** Determines the size of a data tuple prefix in ROW_FORMAT=COMPACT.
1151 @return total size */
1152 ulint rec_get_serialize_size(const dict_index_t *index, const dfield_t *fields,
1153 ulint n_fields, const dtuple_t *v_entry,
1154 ulint *extra, uint8_t rec_version) {
1155 return rec_get_converted_size_comp_prefix_low(
1156 index, fields, n_fields, v_entry, extra, nullptr, true, rec_version);
1157 }
1158
1159 /** Determine the offset to each field in temporary file.
1160 @see rec_serialize_dtuple() */
1161 void rec_deserialize_init_offsets(
1162 const rec_t *rec, /*!< in: temporary file record */
1163 const dict_index_t *index, /*!< in: record descriptor */
1164 ulint *offsets) /*!< in/out: array of offsets;
1165 in: n=rec_offs_n_fields(offsets) */
1166 {
1167 rec_init_offsets_comp_ordinary(rec, true, index, offsets);
1168 }
1169
1170 /** Builds a temporary file record out of a data tuple.
1171 @param[out] rec record
1172 @param[in] index record descriptor
1173 @param[in] fields array of data fields
1174 @param[in] n_fields number of fields
1175 @param[in] v_entry dtuple contains virtual column data
1176 @param[in] rec_version rec version
1177 @see rec_deserialize_init_offsets() */
1178 void rec_serialize_dtuple(rec_t *rec, const dict_index_t *index,
1179 const dfield_t *fields, ulint n_fields,
1180 const dtuple_t *v_entry, uint8_t rec_version) {
1181 rec_convert_dtuple_to_rec_comp(rec, index, fields, n_fields, v_entry,
1182 REC_STATUS_ORDINARY, true, rec_version);
1183 }
1184
1185 /** Copies the first n fields of a physical record to a data tuple. The fields
1186 are copied to the memory heap. */
1187 void rec_copy_prefix_to_dtuple(
1188 dtuple_t *tuple, /*!< out: data tuple */
1189 const rec_t *rec, /*!< in: physical record */
1190 const dict_index_t *index, /*!< in: record descriptor */
1191 ulint n_fields, /*!< in: number of fields
1192 to copy */
1193 mem_heap_t *heap) /*!< in: memory heap */
1194 {
1195 ulint i;
1196 ulint offsets_[REC_OFFS_NORMAL_SIZE];
1197 ulint *offsets = offsets_;
1198 rec_offs_init(offsets_);
1199
1200 offsets =
1201 rec_get_offsets(rec, index, offsets, n_fields, UT_LOCATION_HERE, &heap);
1202
1203 ut_ad(rec_validate(rec, offsets));
1204 ut_ad(dtuple_check_typed(tuple));
1205
1206 dtuple_set_info_bits(
1207 tuple, rec_get_info_bits(rec, dict_table_is_comp(index->table)));
1208
1209 for (i = 0; i < n_fields; i++) {
1210 dfield_t *field;
1211 const byte *data;
1212 ulint len;
1213
1214 field = dtuple_get_nth_field(tuple, i);
1215 data = rec_get_nth_field_instant(rec, offsets, i, index, &len);
1216
1217 if (len != UNIV_SQL_NULL) {
1218 dfield_set_data(field, mem_heap_dup(heap, data, len), len);
1219 ut_ad(!rec_offs_nth_extern(index, offsets, i));
1220 } else {
1221 dfield_set_null(field);
1222 }
1223 }
1224 }
1225
1226 /** Copies the first n fields of an old-style physical record to a new physical
1227 record in a buffer.
1228 @param[in] rec physical record
1229 @param[in] n_fields number of fields to copy
1230 @param[in] area_end end of the prefix data
1231 @param[in,out] buf memory buffer for the copied prefix, or NULL
1232 @param[in,out] buf_size buffer size
1233 @return own: copied record */
1234 static rec_t *rec_copy_prefix_to_buf_old(const rec_t *rec, ulint n_fields,
1235 ulint area_end, byte **buf,
1236 size_t *buf_size) {
1237 uint32_t version_length = 0;
1238 if (rec_old_is_versioned(rec)) {
1239 version_length = 1;
1240 }
1241
1242 ulint area_start;
1243 if (rec_get_1byte_offs_flag(rec)) {
1244 area_start = REC_N_OLD_EXTRA_BYTES + version_length + n_fields;
1245 } else {
1246 area_start = REC_N_OLD_EXTRA_BYTES + version_length + 2 * n_fields;
1247 }
1248
1249 ulint prefix_len = area_start + area_end;
1250
1251 if ((*buf == nullptr) || (*buf_size < prefix_len)) {
1252 ut::free(*buf);
1253 *buf_size = prefix_len;
1254 *buf = static_cast<byte *>(
1255 ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, prefix_len));
1256 }
1257
1258 ut_memcpy(*buf, rec - area_start, prefix_len);
1259
1260 rec_t *copy_rec = *buf + area_start;
1261
1262 rec_set_n_fields_old(copy_rec, n_fields);
1263
1264 return (copy_rec);
1265 }
1266
1267 rec_t *rec_copy_prefix_to_buf(const rec_t *rec, const dict_index_t *index,
1268 ulint n_fields, byte **buf, size_t *buf_size) {
1269 const byte *nulls;
1270 const byte *lens;
1271 uint16_t n_null = 0;
1272 ulint i;
1273 ulint prefix_len;
1274 ulint null_mask;
1275 ulint status;
1276 bool is_rtr_node_ptr = false;
1277
1278 UNIV_PREFETCH_RW(*buf);
1279
1280 if (!dict_table_is_comp(index->table)) {
1281 ut_ad(rec_validate_old(rec));
1282 return (rec_copy_prefix_to_buf_old(
1283 rec, n_fields, rec_get_field_start_offs_low(rec, n_fields), buf,
1284 buf_size));
1285 }
1286
1287 status = rec_get_status(rec);
1288
1289 switch (status) {
1290 case REC_STATUS_ORDINARY:
1291 ut_ad(n_fields <= dict_index_get_n_fields(index));
1292 break;
1293 case REC_STATUS_NODE_PTR:
1294 /* For R-tree, we need to copy the child page number field. */
1295 if (dict_index_is_spatial(index)) {
1296 ut_ad(n_fields == DICT_INDEX_SPATIAL_NODEPTR_SIZE + 1);
1297 is_rtr_node_ptr = true;
1298 } else {
1299 /* it doesn't make sense to copy the child page number
1300 field */
1301 ut_ad(n_fields <= dict_index_get_n_unique_in_tree_nonleaf(index));
1302 }
1303 break;
1304 case REC_STATUS_INFIMUM:
1305 case REC_STATUS_SUPREMUM:
1306 /* infimum or supremum record: no sense to copy anything */
1307 default:
1308 ut_error;
1309 }
1310
1311 {
1312 uint16_t ndf = 0;
1313 uint8_t row_version = UINT8_UNDEFINED;
1314 ut_d(enum REC_INSERT_STATE rec_ins_state =) rec_init_null_and_len_comp(
1315 rec, index, &nulls, &lens, &n_null, ndf, row_version);
1316 ut_ad(rec_ins_state != NONE);
1317 ut_ad(rec_ins_state == INSERTED_INTO_TABLE_WITH_NO_INSTANT_NO_VERSION ||
1318 index->has_instant_cols_or_row_versions());
1319 ut_ad(!rec_new_is_versioned(rec) ||
1320 (is_valid_row_version(row_version) &&
1321 row_version <= index->table->current_row_version));
1322 }
1323
1324 UNIV_PREFETCH_R(lens);
1325 prefix_len = 0;
1326 null_mask = 1;
1327
1328 /* read the lengths of fields 0..n */
1329 for (i = 0; i < n_fields; i++) {
1330 const dict_field_t *field;
1331 const dict_col_t *col;
1332
1333 field = index->get_field(i);
1334 col = field->col;
1335
1336 if (!(col->prtype & DATA_NOT_NULL)) {
1337 /* nullable field => read the null flag */
1338 if (UNIV_UNLIKELY(!(byte)null_mask)) {
1339 nulls--;
1340 null_mask = 1;
1341 }
1342
1343 if (*nulls & null_mask) {
1344 null_mask <<= 1;
1345 continue;
1346 }
1347
1348 null_mask <<= 1;
1349 }
1350
1351 if (is_rtr_node_ptr && i == 1) {
1352 /* For rtree node ptr rec, we need to
1353 copy the page no field with 4 bytes len. */
1354 prefix_len += 4;
1355 } else if (field->fixed_len) {
1356 prefix_len += field->fixed_len;
1357 } else {
1358 ulint len = *lens--;
1359 /* If the maximum length of the column is up
1360 to 255 bytes, the actual length is always
1361 stored in one byte. If the maximum length is
1362 more than 255 bytes, the actual length is
1363 stored in one byte for 0..127. The length
1364 will be encoded in two bytes when it is 128 or
1365 more, or when the column is stored externally. */
1366 if (DATA_BIG_COL(col)) {
1367 if (len & 0x80) {
1368 /* 1exxxxxx */
1369 len &= 0x3f;
1370 len <<= 8;
1371 len |= *lens--;
1372 UNIV_PREFETCH_R(lens);
1373 }
1374 }
1375 prefix_len += len;
1376 }
1377 }
1378
1379 UNIV_PREFETCH_R(rec + prefix_len);
1380
1381 prefix_len += rec - (lens + 1);
1382
1383 if ((*buf == nullptr) || (*buf_size < prefix_len)) {
1384 ut::free(*buf);
1385 *buf_size = prefix_len;
1386 *buf = static_cast<byte *>(
1387 ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, prefix_len));
1388 }
1389
1390 memcpy(*buf, lens + 1, prefix_len);
1391
1392 return (*buf + (rec - (lens + 1)));
1393 }
1394 #endif /* UNIV_HOTBACKUP */
1395
1396 /** Validates the consistency of an old-style physical record.
1397 @param[in] rec physical record
1398 @return true if ok */
1399 static bool rec_validate_old(const rec_t *rec) {
1400 ulint len;
1401 ulint n_fields;
1402 ulint len_sum = 0;
1403 ulint i;
1404
1405 ut_a(rec);
1406 n_fields = rec_get_n_fields_old_raw(rec);
1407
1408 if ((n_fields == 0) || (n_fields > REC_MAX_N_FIELDS)) {
1409 ib::error(ER_IB_MSG_922) << "Record has " << n_fields << " fields";
1410 return false;
1411 }
1412
1413 for (i = 0; i < n_fields; i++) {
1414 /* nullptr for index as we have to check each and every field */
1415 rec_get_nth_field_offs_old(nullptr, rec, i, &len);
1416
1417 if (!((len < UNIV_PAGE_SIZE) || (len == UNIV_SQL_NULL))) {
1418 ib::error(ER_IB_MSG_923) << "Record field " << i << " len " << len;
1419 return false;
1420 }
1421
1422 if (len != UNIV_SQL_NULL) {
1423 len_sum += len;
1424 } else {
1425 len_sum += rec_get_nth_field_size_low(rec, i);
1426 }
1427 }
1428
1429 if (len_sum != rec_get_data_size_old(rec)) {
1430 ib::error(ER_IB_MSG_924) << "Record len should be " << len_sum << ", len "
1431 << rec_get_data_size_old(rec);
1432 return false;
1433 }
1434
1435 return true;
1436 }
1437
1438 /** Validates the consistency of a physical record.
1439 @param[in] rec physical record
1440 @param[in] offsets array returned by rec_get_offsets()
1441 @return true if ok */
1442 bool rec_validate(const rec_t *rec, const ulint *offsets) {
1443 ulint len;
1444 uint16_t n_fields;
1445 ulint len_sum = 0;
1446 ulint i;
1447 uint16_t n_defaults = 0;
1448 uint16_t n_inst_dropped = 0;
1449
1450 ut_a(rec);
1451 n_fields = static_cast<uint16_t>(rec_offs_n_fields(offsets));
1452
1453 if ((n_fields == 0) || (n_fields > REC_MAX_N_FIELDS)) {
1454 ib::error(ER_IB_MSG_925) << "Record has " << n_fields << " fields";
1455 return false;
1456 }
1457
1458 for (i = 0; i < n_fields; i++) {
1459 /* nullptr for index as i is physical here */
1460 rec_get_nth_field_offs(nullptr, offsets, i, &len);
1461
1462 switch (len) {
1463 case UNIV_SQL_NULL:
1464 if (!rec_offs_comp(offsets)) {
1465 if (i - n_inst_dropped < rec_get_n_fields_old_raw(rec)) {
1466 len_sum += rec_get_nth_field_size_low(rec, i - n_inst_dropped);
1467 } else {
1468 /* INSTANT ADD column with NULL default which is not on this row */
1469 ++n_defaults;
1470 }
1471 }
1472 break;
1473 case UNIV_SQL_ADD_COL_DEFAULT:
1474 /* INSTANT ADD Column */
1475 ++n_defaults;
1476 break;
1477 case UNIV_SQL_INSTANT_DROP_COL:
1478 /* INSTANT DROP Column */
1479 ++n_inst_dropped;
1480 break;
1481 default:
1482 /* Normal field with non null value */
1483
1484 /* INSTANT ADD columns will come (physically) after original ones */
1485 ut_a(n_defaults == 0);
1486
1487 if (len >= UNIV_PAGE_SIZE) {
1488 ib::error(ER_IB_MSG_926) << "Record field " << i << " len " << len;
1489 return false;
1490 }
1491
1492 len_sum += len;
1493 break;
1494 }
1495 }
1496
1497 ut_a(rec_offs_comp(offsets) ||
1498 (n_fields - n_inst_dropped) <=
1499 rec_get_n_fields_old_raw(rec) + n_defaults);
1500
1501 if (len_sum != rec_offs_data_size(offsets)) {
1502 ib::error(ER_IB_MSG_927) << "Record len should be " << len_sum << ", len "
1503 << rec_offs_data_size(offsets);
1504 return false;
1505 }
1506
1507 if (!rec_offs_comp(offsets)) {
1508 ut_a(rec_validate_old(rec));
1509 }
1510
1511 return true;
1512 }
1513
1514 /** Prints an old-style physical record.
1515 @param[in] file File where to print
1516 @param[in] rec Physical record */
1517 void rec_print_old(FILE *file, const rec_t *rec) {
1518 const byte *data;
1519 ulint len;
1520 ulint n;
1521 ulint i;
1522
1523 ut_ad(rec);
1524
1525 n = rec_get_n_fields_old_raw(rec);
1526
1527 fprintf(file,
1528 "PHYSICAL RECORD: n_fields %lu;"
1529 " %u-byte offsets; info bits %lu\n",
1530 (ulong)n, rec_get_1byte_offs_flag(rec) ? 1 : 2,
1531 (ulong)rec_get_info_bits(rec, false));
1532
1533 for (i = 0; i < n; i++) {
1534 /* nullptr for index as we print to check each and every field */
1535 data = rec_get_nth_field_old(nullptr, rec, i, &len);
1536
1537 fprintf(file, " %lu:", (ulong)i);
1538
1539 if (len != UNIV_SQL_NULL) {
1540 if (len <= 30) {
1541 ut_print_buf(file, data, len);
1542 } else {
1543 ut_print_buf(file, data, 30);
1544
1545 fprintf(file, " (total %lu bytes)", (ulong)len);
1546 }
1547 } else {
1548 fprintf(file, " SQL NULL, size " ULINTPF " ",
1549 rec_get_nth_field_size_low(rec, i));
1550 }
1551
1552 putc(';', file);
1553 putc('\n', file);
1554 }
1555
1556 rec_validate_old(rec);
1557 }
1558
1559 #ifndef UNIV_HOTBACKUP
1560 /** Prints a physical record in ROW_FORMAT=COMPACT. Ignores the
1561 record header. */
1562 static void rec_print_comp(
1563 FILE *file, /*!< in: file where to print */
1564 const rec_t *rec, /*!< in: physical record */
1565 const ulint *offsets) /*!< in: array returned by rec_get_offsets() */
1566 {
1567 ulint i;
1568
1569 for (i = 0; i < rec_offs_n_fields(offsets); i++) {
1570 const byte *data = nullptr;
1571 ulint len;
1572
1573 /* nullptr for index as we print to check each and every field */
1574 if (rec_offs_nth_default(nullptr, offsets, i)) {
1575 len = UNIV_SQL_ADD_COL_DEFAULT;
1576 } else {
1577 data = rec_get_nth_field(nullptr, rec, offsets, i, &len);
1578 }
1579
1580 fprintf(file, " %lu:", (ulong)i);
1581
1582 switch (len) {
1583 case UNIV_SQL_NULL:
1584 fputs(" SQL NULL", file);
1585 break;
1586 case UNIV_SQL_ADD_COL_DEFAULT:
1587 fputs(" SQL DEFAULT", file);
1588 break;
1589 default:
1590 if (len <= 30) {
1591 ut_print_buf(file, data, len);
1592 } else if (rec_offs_nth_extern(nullptr, offsets, i)) {
1593 ut_print_buf(file, data, 30);
1594 fprintf(file, " (total %lu bytes, external)", (ulong)len);
1595 ut_print_buf(file, data + len - BTR_EXTERN_FIELD_REF_SIZE,
1596 BTR_EXTERN_FIELD_REF_SIZE);
1597 } else {
1598 ut_print_buf(file, data, 30);
1599
1600 fprintf(file, " (total %lu bytes)", (ulong)len);
1601 }
1602 }
1603 putc(';', file);
1604 putc('\n', file);
1605 }
1606 }
1607
1608 /** Prints an old-style spatial index record. */
1609 static void rec_print_mbr_old(FILE *file, /*!< in: file where to print */
1610 const rec_t *rec) /*!< in: physical record */
1611 {
1612 const byte *data;
1613 ulint len;
1614 ulint n;
1615 ulint i;
1616
1617 ut_ad(rec);
1618
1619 n = rec_get_n_fields_old_raw(rec);
1620
1621 fprintf(file,
1622 "PHYSICAL RECORD: n_fields %lu;"
1623 " %u-byte offsets; info bits %lu\n",
1624 (ulong)n, rec_get_1byte_offs_flag(rec) ? 1 : 2,
1625 (ulong)rec_get_info_bits(rec, false));
1626
1627 for (i = 0; i < n; i++) {
1628 data = rec_get_nth_field_old(nullptr, rec, i, &len);
1629
1630 fprintf(file, " %lu:", (ulong)i);
1631
1632 if (len != UNIV_SQL_NULL) {
1633 if (i == 0) {
1634 fprintf(file, " MBR:");
1635 for (; len > 0; len -= sizeof(double)) {
1636 double d = mach_double_read(data);
1637
1638 if (len != sizeof(double)) {
1639 fprintf(file, "%.2lf,", d);
1640 } else {
1641 fprintf(file, "%.2lf", d);
1642 }
1643
1644 data += sizeof(double);
1645 }
1646 } else {
1647 if (len <= 30) {
1648 ut_print_buf(file, data, len);
1649 } else {
1650 ut_print_buf(file, data, 30);
1651
1652 fprintf(file, " (total %lu bytes)", (ulong)len);
1653 }
1654 }
1655 } else {
1656 fprintf(file, " SQL NULL, size " ULINTPF " ",
1657 rec_get_nth_field_size_low(rec, i));
1658 }
1659
1660 putc(';', file);
1661 putc('\n', file);
1662 }
1663
1664 if (rec_get_deleted_flag(rec, false)) {
1665 fprintf(file, " Deleted");
1666 }
1667
1668 if (rec_get_info_bits(rec, true) & REC_INFO_MIN_REC_FLAG) {
1669 fprintf(file, " First rec");
1670 }
1671
1672 rec_validate_old(rec);
1673 }
1674
1675 /** Prints a spatial index record.
1676 @param[in] file File where to print
1677 @param[in] rec Physical record
1678 @param[in] offsets Array returned by rec_get_offsets() */
1679 void rec_print_mbr_rec(FILE *file, const rec_t *rec, const ulint *offsets) {
1680 ut_ad(rec);
1681 ut_ad(offsets);
1682 ut_ad(rec_offs_validate(rec, nullptr, offsets));
1683
1684 if (!rec_offs_comp(offsets)) {
1685 rec_print_mbr_old(file, rec);
1686 return;
1687 }
1688
1689 for (ulint i = 0; i < rec_offs_n_fields(offsets); i++) {
1690 const byte *data;
1691 ulint len;
1692
1693 ut_ad(!rec_offs_nth_default(nullptr, offsets, i));
1694 data = rec_get_nth_field(nullptr, rec, offsets, i, &len);
1695
1696 if (i == 0) {
1697 fprintf(file, " MBR:");
1698 for (; len > 0; len -= sizeof(double)) {
1699 double d = mach_double_read(data);
1700
1701 if (len != sizeof(double)) {
1702 fprintf(file, "%.2lf,", d);
1703 } else {
1704 fprintf(file, "%.2lf", d);
1705 }
1706
1707 data += sizeof(double);
1708 }
1709 } else {
1710 fprintf(file, " %lu:", (ulong)i);
1711
1712 if (len != UNIV_SQL_NULL) {
1713 if (len <= 30) {
1714 ut_print_buf(file, data, len);
1715 } else {
1716 ut_print_buf(file, data, 30);
1717
1718 fprintf(file, " (total %lu bytes)", (ulong)len);
1719 }
1720 } else {
1721 fputs(" SQL NULL", file);
1722 }
1723 }
1724 putc(';', file);
1725 }
1726
1727 if (rec_get_info_bits(rec, true) & REC_INFO_DELETED_FLAG) {
1728 fprintf(file, " Deleted");
1729 }
1730
1731 if (rec_get_info_bits(rec, true) & REC_INFO_MIN_REC_FLAG) {
1732 fprintf(file, " First rec");
1733 }
1734
1735 rec_validate(rec, offsets);
1736 }
1737
1738 /** Prints a physical record. */
1739 void rec_print_new(
1740 FILE *file, /*!< in: file where to print */
1741 const rec_t *rec, /*!< in: physical record */
1742 const ulint *offsets) /*!< in: array returned by rec_get_offsets() */
1743 {
1744 ut_ad(rec);
1745 ut_ad(offsets);
1746 ut_ad(rec_offs_validate(rec, nullptr, offsets));
1747
1748 #ifdef UNIV_DEBUG
1749 if (rec_get_deleted_flag(rec, rec_offs_comp(offsets))) {
1750 DBUG_PRINT("info", ("deleted "));
1751 } else {
1752 DBUG_PRINT("info", ("not-deleted "));
1753 }
1754 #endif /* UNIV_DEBUG */
1755
1756 if (!rec_offs_comp(offsets)) {
1757 rec_print_old(file, rec);
1758 return;
1759 }
1760
1761 fprintf(file,
1762 "PHYSICAL RECORD: n_fields %lu;"
1763 " compact format; info bits %lu\n",
1764 (ulong)rec_offs_n_fields(offsets),
1765 (ulong)rec_get_info_bits(rec, true));
1766
1767 rec_print_comp(file, rec, offsets);
1768 rec_validate(rec, offsets);
1769 }
1770
1771 /** Prints a physical record.
1772 @param[in] file File where to print
1773 @param[in] rec Physical record
1774 @param[in] index Record descriptor */
1775 void rec_print(FILE *file, const rec_t *rec, const dict_index_t *index) {
1776 ut_ad(index);
1777
1778 if (!dict_table_is_comp(index->table)) {
1779 rec_print_old(file, rec);
1780 return;
1781 } else {
1782 mem_heap_t *heap = nullptr;
1783 ulint offsets_[REC_OFFS_NORMAL_SIZE];
1784 rec_offs_init(offsets_);
1785
1786 rec_print_new(file, rec,
1787 rec_get_offsets(rec, index, offsets_, ULINT_UNDEFINED,
1788 UT_LOCATION_HERE, &heap));
1789 if (UNIV_LIKELY_NULL(heap)) {
1790 mem_heap_free(heap);
1791 }
1792 }
1793 }
1794
1795 /** Pretty-print a record.
1796 @param[in,out] o output stream
1797 @param[in] rec physical record
1798 @param[in] info rec_get_info_bits(rec)
1799 @param[in] offsets rec_get_offsets(rec) */
1800 void rec_print(std::ostream &o, const rec_t *rec, ulint info,
1801 const ulint *offsets) {
1802 const ulint comp = rec_offs_comp(offsets);
1803 const ulint n = rec_offs_n_fields(offsets);
1804
1805 ut_ad(rec_offs_validate(rec, nullptr, offsets));
1806
1807 o << (comp ? "COMPACT RECORD" : "RECORD") << "(info_bits=" << info << ", "
1808 << n << " fields): {";
1809
1810 for (ulint i = 0; i < n; i++) {
1811 const byte *data;
1812 ulint len;
1813
1814 if (i) {
1815 o << ',';
1816 }
1817
1818 /* nullptr for index as we print each and every field */
1819 if (rec_offs_nth_default(nullptr, offsets, i)) {
1820 o << "DEFAULT";
1821 continue;
1822 }
1823
1824 /* nullptr for index as we print each and every field */
1825 data = rec_get_nth_field(nullptr, rec, offsets, i, &len);
1826
1827 if (len == UNIV_SQL_NULL) {
1828 o << "NULL";
1829 continue;
1830 }
1831
1832 /* nullptr for index as we print each and every field */
1833 if (rec_offs_nth_extern(nullptr, offsets, i)) {
1834 ulint local_len = len - BTR_EXTERN_FIELD_REF_SIZE;
1835 ut_ad(len >= BTR_EXTERN_FIELD_REF_SIZE);
1836
1837 o << '[' << local_len << '+' << BTR_EXTERN_FIELD_REF_SIZE << ']';
1838 ut_print_buf(o, data, local_len);
1839 ut_print_buf_hex(o, data + local_len, BTR_EXTERN_FIELD_REF_SIZE);
1840 } else {
1841 o << '[' << len << ']';
1842 ut_print_buf(o, data, len);
1843 }
1844 }
1845
1846 o << "}";
1847 }
1848
1849 /** Display a record.
1850 @param[in,out] o output stream
1851 @param[in] r record to display
1852 @return the output stream */
1853 std::ostream &operator<<(std::ostream &o, const rec_index_print &r) {
1854 mem_heap_t *heap = nullptr;
1855 ulint *offsets = rec_get_offsets(r.m_rec, r.m_index, nullptr, ULINT_UNDEFINED,
1856 UT_LOCATION_HERE, &heap);
1857 rec_print(o, r.m_rec, rec_get_info_bits(r.m_rec, rec_offs_comp(offsets)),
1858 offsets);
1859 mem_heap_free(heap);
1860 return (o);
1861 }
1862
1863 /** Display a record.
1864 @param[in,out] o output stream
1865 @param[in] r record to display
1866 @return the output stream */
1867 std::ostream &operator<<(std::ostream &o, const rec_offsets_print &r) {
1868 rec_print(o, r.m_rec, rec_get_info_bits(r.m_rec, rec_offs_comp(r.m_offsets)),
1869 r.m_offsets);
1870 return (o);
1871 }
1872
1873 /** Reads the DB_TRX_ID of a clustered index record.
1874 @return the value of DB_TRX_ID */
1875 trx_id_t rec_get_trx_id(const rec_t *rec, /*!< in: record */
1876 const dict_index_t *index) /*!< in: clustered index */
1877 {
1878 ulint trx_id_col = index->get_sys_col_pos(DATA_TRX_ID);
1879 const byte *trx_id;
1880 ulint len;
1881 mem_heap_t *heap = nullptr;
1882 ulint offsets_[REC_OFFS_NORMAL_SIZE];
1883 ulint *offsets = offsets_;
1884 rec_offs_init(offsets_);
1885
1886 ut_ad(index->is_clustered());
1887 ut_ad(trx_id_col > 0);
1888 ut_ad(trx_id_col != ULINT_UNDEFINED);
1889
1890 #ifdef UNIV_DEBUG
1891 const page_t *page = page_align(rec);
1892 if (fil_page_index_page_check(page)) {
1893 ut_ad(mach_read_from_8(page + PAGE_HEADER + PAGE_INDEX_ID) == index->id);
1894 }
1895 #endif /* UNIV_DEBUG */
1896
1897 offsets = rec_get_offsets(rec, index, offsets, trx_id_col + 1,
1898 UT_LOCATION_HERE, &heap);
1899
1900 trx_id = rec_get_nth_field(index, rec, offsets, trx_id_col, &len);
1901
1902 ut_ad(len == DATA_TRX_ID_LEN);
1903
1904 if (heap) {
1905 mem_heap_free(heap);
1906 }
1907
1908 return (trx_read_trx_id(trx_id));
1909 }
1910 #endif /* !UNIV_HOTBACKUP */