/***************************************************************************** Copyright (c) 1994, 2026, Oracle and/or its affiliates. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2.0, as published by the Free Software Foundation. This program is designed to work with certain software (including but not limited to OpenSSL) that is licensed under separate terms, as designated in a particular file or component or in included license documentation. The authors of MySQL hereby grant you an additional permission to link the program and your derivative works with the separately licensed software that they have either included with the program or referenced in the documentation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *****************************************************************************/ /** @file include/rem0rec.ic Record manager Created 5/30/1994 Heikki Tuuri *************************************************************************/ #include "btr0types.h" #include "dict0boot.h" #include "dict0dict.h" #include "mach0data.h" #include "ut0byte.h" /* Offsets of the bit-fields in an old-style record. NOTE! In the table the most significant bytes and bits are written below less significant. (1) byte offset (2) bit usage within byte downward from origin -> 1 8 bits pointer to next record 2 8 bits pointer to next record 3 1 bit short flag 7 bits number of fields 4 3 bits number of fields 5 bits heap number 5 8 bits heap number 6 4 bits n_owned 4 bits info bits */ /* Offsets of the bit-fields in a new-style record. NOTE! In the table the most significant bytes and bits are written below less significant. (1) byte offset (2) bit usage within byte downward from origin -> 1 8 bits relative offset of next record 2 8 bits relative offset of next record the relative offset is an unsigned 16-bit integer: (offset_of_next_record - offset_of_this_record) mod 64Ki, where mod is the modulo as a non-negative number; we can calculate the offset of the next record with the formula: relative_offset + offset_of_this_record mod UNIV_PAGE_SIZE 3 3 bits status: 000=conventional record 001=node pointer record (inside B-tree) 010=infimum record 011=supremum record 1xx=reserved 5 bits heap number 4 8 bits heap number 5 4 bits n_owned 4 bits info bits */ /** Sets a bit field within 1 byte. */ static inline void rec_set_bit_field_1( rec_t *rec, /*!< in: pointer to record origin */ ulint val, /*!< in: value to set */ ulint offs, /*!< in: offset from the origin down */ ulint mask, /*!< in: mask used to filter bits */ ulint shift) /*!< in: shift right applied after masking */ { ut_ad(rec); ut_ad(offs <= REC_N_OLD_EXTRA_BYTES); ut_ad(mask); ut_ad(mask <= 0xFFUL); ut_ad(((mask >> shift) << shift) == mask); ut_ad(((val << shift) & mask) == (val << shift)); mach_write_to_1(rec - offs, (mach_read_from_1(rec - offs) & ~mask) | (val << shift)); } /** Sets a bit field within 2 bytes. */ static inline void rec_set_bit_field_2( rec_t *rec, /*!< in: pointer to record origin */ ulint val, /*!< in: value to set */ ulint offs, /*!< in: offset from the origin down */ ulint mask, /*!< in: mask used to filter bits */ ulint shift) /*!< in: shift right applied after masking */ { ut_ad(rec); ut_ad(offs <= REC_N_OLD_EXTRA_BYTES); ut_ad(mask > 0xFFUL); ut_ad(mask <= 0xFFFFUL); ut_ad((mask >> shift) & 1); ut_ad(0 == ((mask >> shift) & ((mask >> shift) + 1))); ut_ad(((mask >> shift) << shift) == mask); ut_ad(((val << shift) & mask) == (val << shift)); mach_write_to_2(rec - offs, (mach_read_from_2(rec - offs) & ~mask) | (val << shift)); } static inline const rec_t *rec_get_next_ptr_const(const rec_t *rec, ulint comp) { static_assert(REC_NEXT_MASK == 0xFFFFUL); static_assert(REC_NEXT_SHIFT == 0); const auto field_value = mach_read_from_2(rec - REC_NEXT); if (field_value == 0) { return (nullptr); } if (comp) { /** Check if the result offset is still on the same page. We allow `field_value` to be interpreted as negative 16bit integer. This check does nothing for 64KB pages. */ ut_ad(static_cast(field_value + ut_align_offset(rec, UNIV_PAGE_SIZE))); /* There must be at least REC_N_NEW_EXTRA_BYTES + 1 between each record. */ ut_ad((field_value > REC_N_NEW_EXTRA_BYTES && field_value < 32768) || field_value < (uint16_t)-REC_N_NEW_EXTRA_BYTES); return ((byte *)ut_align_down(rec, UNIV_PAGE_SIZE) + ut_align_offset(rec + field_value, UNIV_PAGE_SIZE)); } else { ut_ad(field_value < UNIV_PAGE_SIZE); return ((byte *)ut_align_down(rec, UNIV_PAGE_SIZE) + field_value); } } static inline rec_t *rec_get_next_ptr(rec_t *rec, ulint comp) { return (const_cast(rec_get_next_ptr_const(rec, comp))); } static inline ulint rec_get_next_offs(const rec_t *rec, ulint comp) { ulint field_value; static_assert(REC_NEXT_MASK == 0xFFFFUL, "REC_NEXT_MASK != 0xFFFFUL"); static_assert(REC_NEXT_SHIFT == 0, "REC_NEXT_SHIFT != 0"); field_value = mach_read_from_2(rec - REC_NEXT); if (comp) { /** Check if the result offset is still on the same page. We allow `field_value` to be interpreted as negative 16bit integer. This check does nothing for 64KB pages. */ ut_ad(static_cast(field_value + ut_align_offset(rec, UNIV_PAGE_SIZE))); if (field_value == 0) { return (0); } /* There must be at least REC_N_NEW_EXTRA_BYTES + 1 between each record. */ ut_ad((field_value > REC_N_NEW_EXTRA_BYTES && field_value < 32768) || field_value < (uint16_t)-REC_N_NEW_EXTRA_BYTES); return (ut_align_offset(rec + field_value, UNIV_PAGE_SIZE)); } else { ut_ad(field_value < UNIV_PAGE_SIZE); return (field_value); } } /** The following function is used to set the next record offset field of an old-style record. */ static inline void rec_set_next_offs_old( rec_t *rec, /*!< in: old-style physical record */ ulint next) /*!< in: offset of the next record */ { ut_ad(rec); ut_ad(UNIV_PAGE_SIZE > next); static_assert(REC_NEXT_MASK == 0xFFFFUL, "REC_NEXT_MASK != 0xFFFFUL"); static_assert(REC_NEXT_SHIFT == 0, "REC_NEXT_SHIFT != 0"); mach_write_to_2(rec - REC_NEXT, next); } /** The following function is used to set the next record offset field of a new-style record. */ static inline void rec_set_next_offs_new( rec_t *rec, /*!< in/out: new-style physical record */ ulint next) /*!< in: offset of the next record */ { ulint field_value; ut_ad(rec); ut_ad(UNIV_PAGE_SIZE > next); if (!next) { field_value = 0; } else { /* The following two statements calculate next - offset_of_rec mod 64Ki, where mod is the modulo as a non-negative number */ field_value = (ulint)((lint)next - (lint)ut_align_offset(rec, UNIV_PAGE_SIZE)); field_value &= REC_NEXT_MASK; } mach_write_to_2(rec - REC_NEXT, field_value); } /** The following function is used to set the number of fields in an old-style record. */ static inline void rec_set_n_fields_old( rec_t *rec, /*!< in: physical record */ ulint n_fields) /*!< in: the number of fields */ { ut_ad(rec); ut_ad(n_fields <= REC_MAX_N_FIELDS); ut_ad(n_fields > 0); rec_set_bit_field_2(rec, n_fields, REC_OLD_N_FIELDS, REC_OLD_N_FIELDS_MASK, REC_OLD_N_FIELDS_SHIFT); } /** The following function is used to get the number of records owned by the previous directory record. @return number of owned records */ static inline ulint rec_get_n_owned_old( const rec_t *rec) /*!< in: old-style physical record */ { return (rec_get_bit_field_1(rec, REC_OLD_N_OWNED, REC_N_OWNED_MASK, REC_N_OWNED_SHIFT)); } /** The following function is used to set the number of owned records. */ static inline void rec_set_n_owned_old( rec_t *rec, /*!< in: old-style physical record */ ulint n_owned) /*!< in: the number of owned */ { rec_set_bit_field_1(rec, n_owned, REC_OLD_N_OWNED, REC_N_OWNED_MASK, REC_N_OWNED_SHIFT); } /** The following function is used to get the number of records owned by the previous directory record. @return number of owned records */ static inline ulint rec_get_n_owned_new( const rec_t *rec) /*!< in: new-style physical record */ { return (rec_get_bit_field_1(rec, REC_NEW_N_OWNED, REC_N_OWNED_MASK, REC_N_OWNED_SHIFT)); } /** The following function is used to set the number of owned records. */ static inline void rec_set_n_owned_new( rec_t *rec, /*!< in/out: new-style physical record */ page_zip_des_t *page_zip, /*!< in/out: compressed page, or NULL */ ulint n_owned) /*!< in: the number of owned */ { rec_set_bit_field_1(rec, n_owned, REC_NEW_N_OWNED, REC_N_OWNED_MASK, REC_N_OWNED_SHIFT); if (page_zip && rec_get_status(rec) != REC_STATUS_SUPREMUM) { page_zip_rec_set_owned(page_zip, rec, n_owned); } } /** The following function is used to set the info bits of a record. */ static inline void rec_set_info_bits_old( rec_t *rec, /*!< in: old-style physical record */ ulint bits) /*!< in: info bits */ { ut_ad(rec_info_bits_valid(bits)); rec_set_bit_field_1(rec, bits, REC_OLD_INFO_BITS, REC_INFO_BITS_MASK, REC_INFO_BITS_SHIFT); } /** The following function is used to set the info bits of a record. */ static inline void rec_set_info_bits_new( rec_t *rec, /*!< in/out: new-style physical record */ ulint bits) /*!< in: info bits */ { ut_ad(rec_info_bits_valid(bits)); rec_set_bit_field_1(rec, bits, REC_NEW_INFO_BITS, REC_INFO_BITS_MASK, REC_INFO_BITS_SHIFT); } /** The following function is used to set the info bits of a temp record. param[in,out] rec temp physical record param[in] bits info bits */ static inline void rec_set_info_bits_new_temp(rec_t *rec, ulint bits) { ut_ad(rec_info_bits_valid(bits)); rec_set_bit_field_1(rec, bits, REC_TMP_INFO_BITS, REC_INFO_BITS_MASK, REC_INFO_BITS_SHIFT); } /** The following function is used to set the status bits of a new-style record. */ static inline void rec_set_status(rec_t *rec, /*!< in/out: physical record */ ulint bits) /*!< in: info bits */ { rec_set_bit_field_1(rec, bits, REC_NEW_STATUS, REC_NEW_STATUS_MASK, REC_NEW_STATUS_SHIFT); } static inline ulint rec_get_info_and_status_bits(const rec_t *rec, bool comp) { ulint bits; static_assert(((REC_NEW_STATUS_MASK >> REC_NEW_STATUS_SHIFT) & (REC_INFO_BITS_MASK >> REC_INFO_BITS_SHIFT)) == 0, "REC_NEW_STATUS_MASK and REC_INFO_BITS_MASK overlap"); if (comp) { bits = rec_get_info_bits(rec, true) | rec_get_status(rec); } else { bits = rec_get_info_bits(rec, false); ut_ad(!(bits & ~(REC_INFO_BITS_MASK >> REC_INFO_BITS_SHIFT))); } return (bits); } /** The following function is used to set the info and status bits of a record. (Only compact records have status bits.) */ static inline void rec_set_info_and_status_bits( rec_t *rec, /*!< in/out: physical record */ ulint bits) /*!< in: info bits */ { static_assert(((REC_NEW_STATUS_MASK >> REC_NEW_STATUS_SHIFT) & (REC_INFO_BITS_MASK >> REC_INFO_BITS_SHIFT)) == 0, "REC_NEW_STATUS_MASK and REC_INFO_BITS_MASK overlap"); rec_set_status(rec, bits & REC_NEW_STATUS_MASK); rec_set_info_bits_new(rec, bits & ~REC_NEW_STATUS_MASK); } static inline bool rec_get_deleted_flag(const rec_t *rec, bool comp) { if (comp) { return (rec_get_bit_field_1(rec, REC_NEW_INFO_BITS, REC_INFO_DELETED_FLAG, REC_INFO_BITS_SHIFT)); } else { return (rec_get_bit_field_1(rec, REC_OLD_INFO_BITS, REC_INFO_DELETED_FLAG, REC_INFO_BITS_SHIFT)); } } /** The following function is used to set the deleted bit. */ static inline void rec_set_deleted_flag_old( rec_t *rec, /*!< in: old-style physical record */ bool flag) /*!< in: nonzero if delete marked */ { auto val = rec_get_info_bits(rec, false); if (flag) { val |= REC_INFO_DELETED_FLAG; } else { val &= ~REC_INFO_DELETED_FLAG; } rec_set_info_bits_old(rec, val); } /** The following function is used to set the deleted bit. */ static inline void rec_set_deleted_flag_new( rec_t *rec, /*!< in/out: new-style physical record */ page_zip_des_t *page_zip, /*!< in/out: compressed page, or NULL */ bool flag) /*!< in: nonzero if delete marked */ { ulint val; val = rec_get_info_bits(rec, true); if (flag) { val |= REC_INFO_DELETED_FLAG; } else { val &= ~REC_INFO_DELETED_FLAG; } rec_set_info_bits_new(rec, val); if (page_zip) { page_zip_rec_set_deleted(page_zip, rec, flag); } } /** The following function is used to set the instant bit. @param[in,out] rec new-style physical record @param[in] flag set the bit to this flag */ static inline void rec_set_instant_flag_new(rec_t *rec, bool flag) { ulint val; val = rec_get_info_bits(rec, true); if (flag) { val |= REC_INFO_INSTANT_FLAG; } else { val &= ~REC_INFO_INSTANT_FLAG; } rec_set_info_bits_new(rec, val); } /** The following function is used to set the version bit. @param[in,out] rec new-style (COMPACT/DYNAMIC) physical record @param[in] flag set the bit to this flag */ static inline void rec_new_set_versioned(rec_t *rec, bool flag) { ulint val; val = rec_get_info_bits(rec, true); if (flag) { val |= REC_INFO_VERSION_FLAG; } else { val &= ~REC_INFO_VERSION_FLAG; } rec_set_info_bits_new(rec, val); } static inline void rec_new_set_instant(rec_t *rec) { rec_set_instant_flag_new(rec, true); rec_new_set_versioned(rec, false); } static inline void rec_new_set_versioned(rec_t *rec) { rec_new_set_versioned(rec, true); rec_set_instant_flag_new(rec, false); } static inline void rec_new_reset_instant_version(rec_t *rec) { rec_set_instant_flag_new(rec, false); rec_new_set_versioned(rec, false); } /** The following function is used to set the version bit. @param[in,out] rec old-style (REDUNDANT) physical record @param[in] flag true to set, false to reset */ static inline void rec_old_set_versioned(rec_t *rec, bool flag) { ulint val; val = rec_get_info_bits(rec, false); if (flag) { val |= REC_INFO_VERSION_FLAG; } else { val &= ~REC_INFO_VERSION_FLAG; } rec_set_info_bits_old(rec, val); } /** The following function is used to set the version bit. @param[in,out] rec Temp physical record @param[in] flag true to set, false to reset */ static inline void rec_new_temp_set_versioned(rec_t *rec, bool flag) { ulint val; val = rec_get_info_bits_temp(rec); if (flag) { val |= REC_INFO_VERSION_FLAG; } else { val &= ~REC_INFO_VERSION_FLAG; } rec_set_info_bits_new_temp(rec, val); } /** The following function tells if a new-style record is a node pointer. @return true if node pointer */ static inline bool rec_get_node_ptr_flag( const rec_t *rec) /*!< in: physical record */ { return (REC_STATUS_NODE_PTR == rec_get_status(rec)); } /** The following function is used to get the order number of an old-style record in the heap of the index page. @return heap order number */ [[nodiscard]] static inline uint16_t rec_get_heap_no_old( const rec_t *rec) /*!< in: physical record */ { return (rec_get_bit_field_2(rec, REC_OLD_HEAP_NO, REC_HEAP_NO_MASK, REC_HEAP_NO_SHIFT)); } /** The following function is used to set the heap number field in an old-style record. */ static inline void rec_set_heap_no_old( rec_t *rec, /*!< in: physical record */ ulint heap_no) /*!< in: the heap number */ { rec_set_bit_field_2(rec, heap_no, REC_OLD_HEAP_NO, REC_HEAP_NO_MASK, REC_HEAP_NO_SHIFT); } /** The following function is used to get the order number of a new-style record in the heap of the index page. @return heap order number */ [[nodiscard]] static inline uint16_t rec_get_heap_no_new( const rec_t *rec) /*!< in: physical record */ { return (rec_get_bit_field_2(rec, REC_NEW_HEAP_NO, REC_HEAP_NO_MASK, REC_HEAP_NO_SHIFT)); } /** The following function is used to set the heap number field in a new-style record. */ static inline void rec_set_heap_no_new( rec_t *rec, /*!< in/out: physical record */ ulint heap_no) /*!< in: the heap number */ { rec_set_bit_field_2(rec, heap_no, REC_NEW_HEAP_NO, REC_HEAP_NO_MASK, REC_HEAP_NO_SHIFT); } /** The following function is used to set the 1-byte offsets flag. */ static inline void rec_set_1byte_offs_flag( rec_t *rec, /*!< in: physical record */ bool flag) /*!< in: true if 1byte form */ { rec_set_bit_field_1(rec, flag, REC_OLD_SHORT, REC_OLD_SHORT_MASK, REC_OLD_SHORT_SHIFT); } /** Determine if the field is not NULL and not having default value after instant ADD COLUMN @param[in] len length of a field @return true if not NULL and not having default value */ static inline bool rec_field_not_null_not_add_col_def(ulint len) { return (len != UNIV_SQL_NULL && len != UNIV_SQL_ADD_COL_DEFAULT && len != UNIV_SQL_INSTANT_DROP_COL); } /** Determine if the offsets are for a record in the new compact format. @return nonzero if compact format */ static inline bool rec_offs_comp( const ulint *offsets) /*!< in: array returned by rec_get_offsets() */ { ut_ad(rec_offs_validate(nullptr, nullptr, offsets)); return (*rec_offs_base(offsets) & REC_OFFS_COMPACT) != 0; } /** Determine if the offsets are for a record containing externally stored columns. @return nonzero if externally stored */ static inline bool rec_offs_any_extern( const ulint *offsets) /*!< in: array returned by rec_get_offsets() */ { ut_ad(rec_offs_validate(nullptr, nullptr, offsets)); return (*rec_offs_base(offsets) & REC_OFFS_EXTERNAL); } static inline const byte *rec_offs_any_null_extern(const dict_index_t *index, const rec_t *rec, const ulint *offsets) { ulint i; ut_ad(rec_offs_validate(rec, nullptr, offsets)); if (!rec_offs_any_extern(offsets)) { return (nullptr); } for (i = 0; i < rec_offs_n_fields(offsets); i++) { if (rec_offs_nth_extern(index, offsets, i)) { ulint len; const byte *field = rec_get_nth_field(index, rec, offsets, i, &len); ut_a(len >= BTR_EXTERN_FIELD_REF_SIZE); if (!memcmp(field + len - BTR_EXTERN_FIELD_REF_SIZE, field_ref_zero, BTR_EXTERN_FIELD_REF_SIZE)) { return (field); } } } return (nullptr); } /** Returns the number of extern bits set in a record. @param[in] index record descriptor @param[in] offsets array returned by rec_get_offsets() @return number of externally stored fields */ static inline ulint rec_offs_n_extern(const dict_index_t *index, const ulint *offsets) { ulint n = 0; if (rec_offs_any_extern(offsets)) { ulint i; for (i = rec_offs_n_fields(offsets); i--;) { if (rec_offs_nth_extern(index, offsets, i)) { n++; } } } return (n); } /** Returns the offset of n - 1th field end if the record is stored in the 1-byte offsets form. If the field is SQL null, the flag is ORed in the returned value. This function and the 2-byte counterpart are defined here because the C-compiler was not able to sum negative and positive constant offsets, and warned of constant arithmetic overflow within the compiler. @return offset of the start of the PREVIOUS field, SQL null flag ORed */ static inline ulint rec_1_get_prev_field_end_info( const rec_t *rec, /*!< in: record */ ulint n) /*!< in: field index */ { ut_ad(rec_get_1byte_offs_flag(rec)); ut_ad(n <= rec_get_n_fields_old_raw(rec)); uint32_t version_length = 0; if (rec_old_is_versioned(rec)) { version_length = 1; } return (mach_read_from_1(rec - (REC_N_OLD_EXTRA_BYTES + version_length + n))); } /** Returns the offset of n - 1th field end if the record is stored in the 2-byte offsets form. If the field is SQL null, the flag is ORed in the returned value. @return offset of the start of the PREVIOUS field, SQL null flag ORed */ static inline ulint rec_2_get_prev_field_end_info( const rec_t *rec, /*!< in: record */ ulint n) /*!< in: field index */ { ut_ad(!rec_get_1byte_offs_flag(rec)); ut_ad(n <= rec_get_n_fields_old_raw(rec)); uint32_t version_length = 0; if (rec_old_is_versioned(rec)) { version_length = 1; } return ( mach_read_from_2(rec - (REC_N_OLD_EXTRA_BYTES + version_length + 2 * n))); } /** The following function returns the data size of a physical record, that is the sum of field lengths. SQL null fields are counted as length 0 fields. The value returned by the function is the distance from record origin to record end in bytes. @return size */ static inline ulint rec_offs_data_size( const ulint *offsets) /*!< in: array returned by rec_get_offsets() */ { ulint size; ut_ad(rec_offs_validate(nullptr, nullptr, offsets)); size = rec_offs_base(offsets)[rec_offs_n_fields(offsets)] & REC_OFFS_MASK; ut_ad(size < UNIV_PAGE_SIZE); return (size); } /** Returns the total size of record minus data size of record. The value returned by the function is the distance from record start to record origin in bytes. @return size */ static inline ulint rec_offs_extra_size( const ulint *offsets) /*!< in: array returned by rec_get_offsets() */ { ulint size; ut_ad(rec_offs_validate(nullptr, nullptr, offsets)); size = *rec_offs_base(offsets) & ~(REC_OFFS_COMPACT | REC_OFFS_EXTERNAL); ut_ad(size < UNIV_PAGE_SIZE); return (size); } /** Returns the total size of a physical record. @return size */ static inline ulint rec_offs_size( const ulint *offsets) /*!< in: array returned by rec_get_offsets() */ { return (rec_offs_data_size(offsets) + rec_offs_extra_size(offsets)); } #ifdef UNIV_DEBUG static inline byte *rec_get_end(const rec_t *rec, const ulint *offsets) { ut_ad(rec_offs_validate(rec, nullptr, offsets)); return (const_cast(rec + rec_offs_data_size(offsets))); } static inline byte *rec_get_start(const rec_t *rec, const ulint *offsets) { ut_ad(rec_offs_validate(rec, nullptr, offsets)); return (const_cast(rec - rec_offs_extra_size(offsets))); } #endif /* UNIV_DEBUG */ /** Copy a physical record to a buffer. @param[in] buf buffer @param[in] rec physical record @param[in] offsets array returned by rec_get_offsets() @return pointer to the origin of the copy */ static inline rec_t *rec_copy(void *buf, const rec_t *rec, const ulint *offsets) { ulint extra_len; ulint data_len; ut_ad(rec != nullptr); ut_ad(buf != nullptr); ut_ad(rec_offs_validate(rec, nullptr, offsets)); ut_ad(rec_validate(rec, offsets)); extra_len = rec_offs_extra_size(offsets); data_len = rec_offs_data_size(offsets); ut_memcpy(buf, rec - extra_len, extra_len + data_len); return ((byte *)buf + extra_len); } /** Returns the extra size of an old-style physical record if we know its data size and number of fields. */ static inline ulint rec_get_converted_extra_size(ulint data_size, ulint n_fields, bool has_ext) { if (!has_ext && data_size <= REC_1BYTE_OFFS_LIMIT) { return (REC_N_OLD_EXTRA_BYTES + n_fields); } return (REC_N_OLD_EXTRA_BYTES + 2 * n_fields); } /** The following function returns the size of a data tuple when converted to a physical record. @param[in] index record descriptor @param[in] dtuple data tuple @return size */ static inline ulint rec_get_converted_size(const dict_index_t *index, const dtuple_t *dtuple) { ulint data_size; ulint extra_size; ut_ad(index); ut_ad(dtuple); ut_ad(dtuple_check_typed(dtuple)); ut_ad(dict_index_is_ibuf(index) || /* If this is an instant index and the tuple comes from UPDATE, its fields could be less than index definition */ index->has_instant_cols_or_row_versions() || dtuple_get_n_fields(dtuple) == (((dtuple_get_info_bits(dtuple) & REC_NEW_STATUS_MASK) == REC_STATUS_NODE_PTR) ? dict_index_get_n_unique_in_tree_nonleaf(index) + 1U : dict_index_get_n_fields(index)) || /* a record for older SYS_INDEXES table (missing merge_threshold column) is acceptable. */ (index->table->id == DICT_INDEXES_ID && dtuple_get_n_fields(dtuple) == dict_index_get_n_fields(index) - 1U)); if (dict_table_is_comp(index->table)) { return (rec_get_converted_size_comp( index, dtuple_get_info_bits(dtuple) & REC_NEW_STATUS_MASK, dtuple->fields, dtuple->n_fields, nullptr)); } /* Record is in REDUNDANT format */ data_size = dtuple_get_data_size(dtuple, 0); extra_size = rec_get_converted_extra_size( data_size, dtuple_get_n_fields(dtuple), dtuple->has_ext()); bool is_leaf_rec = (dtuple_get_info_bits(dtuple) & 0x03UL) == REC_STATUS_ORDINARY; if (is_leaf_rec && is_store_version(index, dtuple_get_n_fields(dtuple))) { extra_size++; } #if 0 /* This code is inactive since it may be the wrong place to add in the size of node pointers used in parent pages AND it is not currently needed since ha_innobase::max_supported_key_length() ensures that the key size limit for each page size is well below the actual limit ((free space on page / 4) - record overhead). But those limits will need to be raised when InnoDB can support multiple page sizes. At that time, we will need to consider the node pointer on these universal btrees. */ if (dict_index_is_ibuf(index)) { /* This is for the insert buffer B-tree. All fields in the leaf tuple ascend to the parent node plus the child page pointer. */ /* ibuf cannot contain externally stored fields */ ut_ad(n_ext == 0); /* Add the data pointer and recompute extra_size based on one more field. */ data_size += REC_NODE_PTR_SIZE; extra_size = rec_get_converted_extra_size( data_size, dtuple_get_n_fields(dtuple) + 1, 0); /* Be sure dtuple->n_fields has this node ptr accounted for. This function should correspond to what rec_convert_dtuple_to_rec() needs in storage. In optimistic insert or update-not-in-place, we will have to ensure that if the record is converted to a node pointer, it will not become too large.*/ } #endif return (data_size + extra_size); } #ifndef UNIV_HOTBACKUP /** Compute a hash value of a prefix of a leaf page record. @param[in] rec leaf page record @param[in] offsets rec_get_offsets(rec) @param[in] n_fields number of complete fields to hash @param[in] n_bytes number of bytes to hash in the last field @param[in] hash_value hash value of the index identifier @param[in] index index where the record resides @return the hashed value */ static inline uint64_t rec_hash(const rec_t *rec, const ulint *offsets, ulint n_fields, ulint n_bytes, uint64_t hash_value, const dict_index_t *index) { uint16_t i; const byte *data; ulint len; ulint n_fields_rec; ut_ad(rec_offs_validate(rec, nullptr, offsets)); ut_ad(rec_validate(rec, offsets)); ut_ad(n_fields > 0 || n_bytes > 0); n_fields_rec = rec_offs_n_fields(offsets); ut_ad(n_fields <= n_fields_rec); ut_ad(n_fields < n_fields_rec || n_bytes == 0); if (n_fields > n_fields_rec) { n_fields = n_fields_rec; } if (n_fields == n_fields_rec) { n_bytes = 0; } for (i = 0; i < n_fields; i++) { data = rec_get_nth_field_instant(rec, offsets, i, index, &len); if (len != UNIV_SQL_NULL) { hash_value = ut::hash_binary(data, len, hash_value); } } if (n_bytes > 0) { data = rec_get_nth_field_instant(rec, offsets, i, index, &len); if (len != UNIV_SQL_NULL) { if (len > n_bytes) { len = n_bytes; } hash_value = ut::hash_binary(data, len, hash_value); } } return hash_value; } #endif /* !UNIV_HOTBACKUP */ /** Get the length of the number of fields for any new style record. @param[in] n_fields number of fields in the record @return length of specified number of fields */ static inline uint8_t rec_get_n_fields_length(ulint n_fields) { return (n_fields > REC_N_FIELDS_ONE_BYTE_MAX ? 2 : 1); } /** Set the number of fields for one new style leaf page record. This is only needed for table after instant ADD COLUMN. @param[in,out] rec leaf page record @param[in] n_fields number of fields in the record @return the length of the n_fields occupies */ static inline uint8_t rec_set_n_fields(rec_t *rec, ulint n_fields) { byte *ptr = rec - (REC_N_NEW_EXTRA_BYTES + 1); ut_ad(n_fields < REC_MAX_N_FIELDS); if (n_fields <= REC_N_FIELDS_ONE_BYTE_MAX) { *ptr = static_cast(n_fields); return (1); } --ptr; *ptr++ = static_cast(n_fields & 0xFF); *ptr = static_cast(n_fields >> 8); ut_ad((*ptr & 0x80) == 0); *ptr |= REC_N_FIELDS_TWO_BYTES_FLAG; return (2); } /** Set the row version on a new style leaf page record. This is only needed for table after instant ADD/DROP COLUMN. @param[in,out] rec leaf page record @param[in] row_version row version */ static inline void rec_set_instant_row_version_new(rec_t *rec, uint8_t row_version) { /* Note : row_version might be 0 while storing a new row in upgraded table */ ut_ad(is_valid_row_version(row_version)); byte *ptr = rec - (REC_N_NEW_EXTRA_BYTES + 1); *ptr = static_cast(row_version); } /** Get the row version on a new style leaf page record. This is only needed for table after instant ADD/DROP COLUMN. @param[in] rec leaf page record @return row version */ static inline uint8_t rec_get_instant_row_version_new(const rec_t *rec) { uint8_t row_version = 0; const byte *ptr = rec - (REC_N_NEW_EXTRA_BYTES + 1); row_version = *ptr; /* For upgraded table, we store version 0 */ ut_ad(is_valid_row_version(row_version)); return row_version; } /** Set the row version on an old style leaf page record. This is only needed for table after instant ADD/DROP COLUMN. @param[in,out] rec leaf page record @param[in] row_version row version */ static inline void rec_set_instant_row_version_old(rec_t *rec, uint8_t row_version) { /* Note : row_version might be 0 while storing a new row in upgraded table */ ut_ad(is_valid_row_version(row_version)); byte *ptr = rec - (REC_N_OLD_EXTRA_BYTES + 1); *ptr = static_cast(row_version); } /** Get the row version on an old style leaf page record. This is only needed for table after instant ADD/DROP COLUMN. @param[in] rec leaf page record @return row version */ static inline uint8_t rec_get_instant_row_version_old(const rec_t *rec) { const byte *ptr = rec - (REC_N_OLD_EXTRA_BYTES + 1); uint8_t row_version = *ptr; ut_ad(is_valid_row_version(row_version)); return row_version; } /** Gets the value of the specified field in the record in old style. This is only used for record from instant index, which is clustered index and has some instantly added columns. @param[in] rec physical record @param[in] n index of the field @param[in] index clustered index where the record resides @param[out] len length of the field, UNIV_SQL if SQL null @return value of the field, could be either pointer to rec or default value */ static inline const byte *rec_get_nth_field_old_instant( const rec_t *rec, uint16_t n, const dict_index_t *index, ulint *len) { ut_a(index != nullptr); if (index->has_row_versions()) { /* Fields are stored in record in order of the version they are added in. Get the field number on physical record. */ uint8_t version = UINT8_UNDEFINED; if (rec_old_is_versioned(rec)) { version = rec_get_instant_row_version_old(rec); } n = index->get_field_phy_pos(n, version); } if (n < rec_get_n_fields_old_raw(rec)) { /* nullptr for index as n is physical here */ return (rec_get_nth_field_old(nullptr, rec, n, len)); } const byte *field; ut_ad(index->has_instant_cols_or_row_versions()); field = index->get_nth_default(n, len); return (field); } /** Gets the value of the specified field in the record. This is only used when there is possibility that the record comes from the clustered index, which has some instantly add columns @param[in] rec record @param[in] offsets array returned by rec_get_offsets() @param[in] n index of the field @param[in] index clustered index where the record resides @param[in,out] len length of the field, UNIV_SQL_NULL if SQL null @return value of the field, could be either pointer to rec or default value */ static inline const byte *rec_get_nth_field_instant(const rec_t *rec, const ulint *offsets, ulint n, const dict_index_t *index, ulint *len) { if (index->has_row_versions()) { /* Fields are stored in record in order of the version they are added in. Get the field number on physical record. */ n = index->get_field_off_pos(n); } /* nullptr for index as n is physical here */ ulint off = rec_get_nth_field_offs(nullptr, offsets, n, len); if (*len != UNIV_SQL_ADD_COL_DEFAULT && *len != UNIV_SQL_INSTANT_DROP_COL) { return (rec + off); } ut_a(index != nullptr); ut_ad(index->has_instant_cols_or_row_versions()); if (*len == UNIV_SQL_INSTANT_DROP_COL) { *len = 0; return nullptr; } return (index->get_nth_default(n, len)); } static inline bool rec_is_min_rec_flag_set(const rec_t *rec, bool is_comp) { ut_ad(rec); return (REC_INFO_MIN_REC_FLAG & rec_get_info_bits(rec, is_comp)); }