mirror of
https://github.com/MariaDB/server.git
synced 2026-04-19 06:45:32 +02:00
For InnoDB tables, adding, dropping and reordering columns has
required a rebuild of the table and all its indexes. Since MySQL 5.6
(and MariaDB 10.0) this has been supported online (LOCK=NONE), allowing
concurrent modification of the tables.
This work revises the InnoDB ROW_FORMAT=REDUNDANT, ROW_FORMAT=COMPACT
and ROW_FORMAT=DYNAMIC so that columns can be appended instantaneously,
with only minor changes performed to the table structure. The counter
innodb_instant_alter_column in INFORMATION_SCHEMA.GLOBAL_STATUS
is incremented whenever a table rebuild operation is converted into
an instant ADD COLUMN operation.
ROW_FORMAT=COMPRESSED tables will not support instant ADD COLUMN.
Some usability limitations will be addressed in subsequent work:
MDEV-13134 Introduce ALTER TABLE attributes ALGORITHM=NOCOPY
and ALGORITHM=INSTANT
MDEV-14016 Allow instant ADD COLUMN, ADD INDEX, LOCK=NONE
The format of the clustered index (PRIMARY KEY) is changed as follows:
(1) The FIL_PAGE_TYPE of the root page will be FIL_PAGE_TYPE_INSTANT,
and a new field PAGE_INSTANT will contain the original number of fields
in the clustered index ('core' fields).
If instant ADD COLUMN has not been used or the table becomes empty,
or the very first instant ADD COLUMN operation is rolled back,
the fields PAGE_INSTANT and FIL_PAGE_TYPE will be reset
to 0 and FIL_PAGE_INDEX.
(2) A special 'default row' record is inserted into the leftmost leaf,
between the page infimum and the first user record. This record is
distinguished by the REC_INFO_MIN_REC_FLAG, and it is otherwise in the
same format as records that contain values for the instantly added
columns. This 'default row' always has the same number of fields as
the clustered index according to the table definition. The values of
'core' fields are to be ignored. For other fields, the 'default row'
will contain the default values as they were during the ALTER TABLE
statement. (If the column default values are changed later, those
values will only be stored in the .frm file. The 'default row' will
contain the original evaluated values, which must be the same for
every row.) The 'default row' must be completely hidden from
higher-level access routines. Assertions have been added to ensure
that no 'default row' is ever present in the adaptive hash index
or in locked records. The 'default row' is never delete-marked.
(3) In clustered index leaf page records, the number of fields must
reside between the number of 'core' fields (dict_index_t::n_core_fields
introduced in this work) and dict_index_t::n_fields. If the number
of fields is less than dict_index_t::n_fields, the missing fields
are replaced with the column value of the 'default row'.
Note: The number of fields in the record may shrink if some of the
last instantly added columns are updated to the value that is
in the 'default row'. The function btr_cur_trim() implements this
'compression' on update and rollback; dtuple::trim() implements it
on insert.
(4) In ROW_FORMAT=COMPACT and ROW_FORMAT=DYNAMIC records, the new
status value REC_STATUS_COLUMNS_ADDED will indicate the presence of
a new record header that will encode n_fields-n_core_fields-1 in
1 or 2 bytes. (In ROW_FORMAT=REDUNDANT records, the record header
always explicitly encodes the number of fields.)
We introduce the undo log record type TRX_UNDO_INSERT_DEFAULT for
covering the insert of the 'default row' record when instant ADD COLUMN
is used for the first time. Subsequent instant ADD COLUMN can use
TRX_UNDO_UPD_EXIST_REC.
This is joint work with Vin Chen (陈福荣) from Tencent. The design
that was discussed in April 2017 would not have allowed import or
export of data files, because instead of the 'default row' it would
have introduced a data dictionary table. The test
rpl.rpl_alter_instant is exactly as contributed in pull request #408.
The test innodb.instant_alter is based on a contributed test.
The redo log record format changes for ROW_FORMAT=DYNAMIC and
ROW_FORMAT=COMPACT are as contributed. (With this change present,
crash recovery from MariaDB 10.3.1 will fail in spectacular ways!)
Also the semantics of higher-level redo log records that modify the
PAGE_INSTANT field is changed. The redo log format version identifier
was already changed to LOG_HEADER_FORMAT_CURRENT=103 in MariaDB 10.3.1.
Everything else has been rewritten by me. Thanks to Elena Stepanova,
the code has been tested extensively.
When rolling back an instant ADD COLUMN operation, we must empty the
PAGE_FREE list after deleting or shortening the 'default row' record,
by calling either btr_page_empty() or btr_page_reorganize(). We must
know the size of each entry in the PAGE_FREE list. If rollback left a
freed copy of the 'default row' in the PAGE_FREE list, we would be
unable to determine its size (if it is in ROW_FORMAT=COMPACT or
ROW_FORMAT=DYNAMIC) because it would contain more fields than the
rolled-back definition of the clustered index.
UNIV_SQL_DEFAULT: A new special constant that designates an instantly
added column that is not present in the clustered index record.
len_is_stored(): Check if a length is an actual length. There are
two magic length values: UNIV_SQL_DEFAULT, UNIV_SQL_NULL.
dict_col_t::def_val: The 'default row' value of the column. If the
column is not added instantly, def_val.len will be UNIV_SQL_DEFAULT.
dict_col_t: Add the accessors is_virtual(), is_nullable(), is_instant(),
instant_value().
dict_col_t::remove_instant(): Remove the 'instant ADD' status of
a column.
dict_col_t::name(const dict_table_t& table): Replaces
dict_table_get_col_name().
dict_index_t::n_core_fields: The original number of fields.
For secondary indexes and if instant ADD COLUMN has not been used,
this will be equal to dict_index_t::n_fields.
dict_index_t::n_core_null_bytes: Number of bytes needed to
represent the null flags; usually equal to UT_BITS_IN_BYTES(n_nullable).
dict_index_t::NO_CORE_NULL_BYTES: Magic value signalling that
n_core_null_bytes was not initialized yet from the clustered index
root page.
dict_index_t: Add the accessors is_instant(), is_clust(),
get_n_nullable(), instant_field_value().
dict_index_t::instant_add_field(): Adjust clustered index metadata
for instant ADD COLUMN.
dict_index_t::remove_instant(): Remove the 'instant ADD' status
of a clustered index when the table becomes empty, or the very first
instant ADD COLUMN operation is rolled back.
dict_table_t: Add the accessors is_instant(), is_temporary(),
supports_instant().
dict_table_t::instant_add_column(): Adjust metadata for
instant ADD COLUMN.
dict_table_t::rollback_instant(): Adjust metadata on the rollback
of instant ADD COLUMN.
prepare_inplace_alter_table_dict(): First create the ctx->new_table,
and only then decide if the table really needs to be rebuilt.
We must split the creation of table or index metadata from the
creation of the dictionary table records and the creation of
the data. In this way, we can transform a table-rebuilding operation
into an instant ADD COLUMN operation. Dictionary objects will only
be added to cache when table rebuilding or index creation is needed.
The ctx->instant_table will never be added to cache.
dict_table_t::add_to_cache(): Modified and renamed from
dict_table_add_to_cache(). Do not modify the table metadata.
Let the callers invoke dict_table_add_system_columns() and if needed,
set can_be_evicted.
dict_create_sys_tables_tuple(), dict_create_table_step(): Omit the
system columns (which will now exist in the dict_table_t object
already at this point).
dict_create_table_step(): Expect the callers to invoke
dict_table_add_system_columns().
pars_create_table(): Before creating the table creation execution
graph, invoke dict_table_add_system_columns().
row_create_table_for_mysql(): Expect all callers to invoke
dict_table_add_system_columns().
create_index_dict(): Replaces row_merge_create_index_graph().
innodb_update_n_cols(): Renamed from innobase_update_n_virtual().
Call my_error() if an error occurs.
btr_cur_instant_init(), btr_cur_instant_init_low(),
btr_cur_instant_root_init():
Load additional metadata from the clustered index and set
dict_index_t::n_core_null_bytes. This is invoked
when table metadata is first loaded into the data dictionary.
dict_boot(): Initialize n_core_null_bytes for the four hard-coded
dictionary tables.
dict_create_index_step(): Initialize n_core_null_bytes. This is
executed as part of CREATE TABLE.
dict_index_build_internal_clust(): Initialize n_core_null_bytes to
NO_CORE_NULL_BYTES if table->supports_instant().
row_create_index_for_mysql(): Initialize n_core_null_bytes for
CREATE TEMPORARY TABLE.
commit_cache_norebuild(): Call the code to rename or enlarge columns
in the cache only if instant ADD COLUMN is not being used.
(Instant ADD COLUMN would copy all column metadata from
instant_table to old_table, including the names and lengths.)
PAGE_INSTANT: A new 13-bit field for storing dict_index_t::n_core_fields.
This is repurposing the 16-bit field PAGE_DIRECTION, of which only the
least significant 3 bits were used. The original byte containing
PAGE_DIRECTION will be accessible via the new constant PAGE_DIRECTION_B.
page_get_instant(), page_set_instant(): Accessors for the PAGE_INSTANT.
page_ptr_get_direction(), page_get_direction(),
page_ptr_set_direction(): Accessors for PAGE_DIRECTION.
page_direction_reset(): Reset PAGE_DIRECTION, PAGE_N_DIRECTION.
page_direction_increment(): Increment PAGE_N_DIRECTION
and set PAGE_DIRECTION.
rec_get_offsets(): Use the 'leaf' parameter for non-debug purposes,
and assume that heap_no is always set.
Initialize all dict_index_t::n_fields for ROW_FORMAT=REDUNDANT records,
even if the record contains fewer fields.
rec_offs_make_valid(): Add the parameter 'leaf'.
rec_copy_prefix_to_dtuple(): Assert that the tuple is only built
on the core fields. Instant ADD COLUMN only applies to the
clustered index, and we should never build a search key that has
more than the PRIMARY KEY and possibly DB_TRX_ID,DB_ROLL_PTR.
All these columns are always present.
dict_index_build_data_tuple(): Remove assertions that would be
duplicated in rec_copy_prefix_to_dtuple().
rec_init_offsets(): Support ROW_FORMAT=REDUNDANT records whose
number of fields is between n_core_fields and n_fields.
cmp_rec_rec_with_match(): Implement the comparison between two
MIN_REC_FLAG records.
trx_t::in_rollback: Make the field available in non-debug builds.
trx_start_for_ddl_low(): Remove dangerous error-tolerance.
A dictionary transaction must be flagged as such before it has generated
any undo log records. This is because trx_undo_assign_undo() will mark
the transaction as a dictionary transaction in the undo log header
right before the very first undo log record is being written.
btr_index_rec_validate(): Account for instant ADD COLUMN
row_undo_ins_remove_clust_rec(): On the rollback of an insert into
SYS_COLUMNS, revert instant ADD COLUMN in the cache by removing the
last column from the table and the clustered index.
row_search_on_row_ref(), row_undo_mod_parse_undo_rec(), row_undo_mod(),
trx_undo_update_rec_get_update(): Handle the 'default row'
as a special case.
dtuple_t::trim(index): Omit a redundant suffix of an index tuple right
before insert or update. After instant ADD COLUMN, if the last fields
of a clustered index tuple match the 'default row', there is no
need to store them. While trimming the entry, we must hold a page latch,
so that the table cannot be emptied and the 'default row' be deleted.
btr_cur_optimistic_update(), btr_cur_pessimistic_update(),
row_upd_clust_rec_by_insert(), row_ins_clust_index_entry_low():
Invoke dtuple_t::trim() if needed.
row_ins_clust_index_entry(): Restore dtuple_t::n_fields after calling
row_ins_clust_index_entry_low().
rec_get_converted_size(), rec_get_converted_size_comp(): Allow the number
of fields to be between n_core_fields and n_fields. Do not support
infimum,supremum. They are never supposed to be stored in dtuple_t,
because page creation nowadays uses a lower-level method for initializing
them.
rec_convert_dtuple_to_rec_comp(): Assign the status bits based on the
number of fields.
btr_cur_trim(): In an update, trim the index entry as needed. For the
'default row', handle rollback specially. For user records, omit
fields that match the 'default row'.
btr_cur_optimistic_delete_func(), btr_cur_pessimistic_delete():
Skip locking and adaptive hash index for the 'default row'.
row_log_table_apply_convert_mrec(): Replace 'default row' values if needed.
In the temporary file that is applied by row_log_table_apply(),
we must identify whether the records contain the extra header for
instantly added columns. For now, we will allocate an additional byte
for this for ROW_T_INSERT and ROW_T_UPDATE records when the source table
has been subject to instant ADD COLUMN. The ROW_T_DELETE records are
fine, as they will be converted and will only contain 'core' columns
(PRIMARY KEY and some system columns) that are converted from dtuple_t.
rec_get_converted_size_temp(), rec_init_offsets_temp(),
rec_convert_dtuple_to_temp(): Add the parameter 'status'.
REC_INFO_DEFAULT_ROW = REC_INFO_MIN_REC_FLAG | REC_STATUS_COLUMNS_ADDED:
An info_bits constant for distinguishing the 'default row' record.
rec_comp_status_t: An enum of the status bit values.
rec_leaf_format: An enum that replaces the bool parameter of
rec_init_offsets_comp_ordinary().
1468 lines
41 KiB
Text
1468 lines
41 KiB
Text
/*****************************************************************************
|
|
|
|
Copyright (c) 1994, 2015, Oracle and/or its affiliates. All Rights Reserved.
|
|
Copyright (c) 2017, MariaDB Corporation.
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
Foundation; version 2 of the License.
|
|
|
|
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 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 Street, Suite 500, Boston, MA 02110-1335 USA
|
|
|
|
*****************************************************************************/
|
|
|
|
/********************************************************************//**
|
|
@file include/rem0rec.ic
|
|
Record manager
|
|
|
|
Created 5/30/1994 Heikki Tuuri
|
|
*************************************************************************/
|
|
|
|
#include "mach0data.h"
|
|
#include "ut0byte.h"
|
|
#include "dict0boot.h"
|
|
#include "btr0types.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=REC_STATUS_ORDINARY
|
|
001=REC_STATUS_NODE_PTR
|
|
010=REC_STATUS_INFIMUM
|
|
011=REC_STATUS_SUPREMUM
|
|
100=REC_STATUS_COLUMNS_ADDED
|
|
1xx=reserved
|
|
5 bits heap number
|
|
4 8 bits heap number
|
|
5 4 bits n_owned
|
|
4 bits info bits
|
|
*/
|
|
|
|
/* We list the byte offsets from the origin of the record, the mask,
|
|
and the shift needed to obtain each bit-field of the record. */
|
|
|
|
#define REC_NEXT 2
|
|
#define REC_NEXT_MASK 0xFFFFUL
|
|
#define REC_NEXT_SHIFT 0
|
|
|
|
#define REC_OLD_SHORT 3 /* This is single byte bit-field */
|
|
#define REC_OLD_SHORT_MASK 0x1UL
|
|
#define REC_OLD_SHORT_SHIFT 0
|
|
|
|
#define REC_OLD_N_FIELDS 4
|
|
#define REC_OLD_N_FIELDS_MASK 0x7FEUL
|
|
#define REC_OLD_N_FIELDS_SHIFT 1
|
|
|
|
#define REC_OLD_HEAP_NO 5
|
|
#define REC_HEAP_NO_MASK 0xFFF8UL
|
|
#if 0 /* defined in rem0rec.h for use of page0zip.cc */
|
|
#define REC_NEW_HEAP_NO 4
|
|
#define REC_HEAP_NO_SHIFT 3
|
|
#endif
|
|
|
|
#define REC_OLD_N_OWNED 6 /* This is single byte bit-field */
|
|
#define REC_NEW_N_OWNED 5 /* This is single byte bit-field */
|
|
#define REC_N_OWNED_MASK 0xFUL
|
|
#define REC_N_OWNED_SHIFT 0
|
|
|
|
#define REC_OLD_INFO_BITS 6 /* This is single byte bit-field */
|
|
#define REC_NEW_INFO_BITS 5 /* This is single byte bit-field */
|
|
#define REC_INFO_BITS_MASK 0xF0UL
|
|
#define REC_INFO_BITS_SHIFT 0
|
|
|
|
#if REC_OLD_SHORT_MASK << (8 * (REC_OLD_SHORT - 3)) \
|
|
^ REC_OLD_N_FIELDS_MASK << (8 * (REC_OLD_N_FIELDS - 4)) \
|
|
^ REC_HEAP_NO_MASK << (8 * (REC_OLD_HEAP_NO - 4)) \
|
|
^ REC_N_OWNED_MASK << (8 * (REC_OLD_N_OWNED - 3)) \
|
|
^ REC_INFO_BITS_MASK << (8 * (REC_OLD_INFO_BITS - 3)) \
|
|
^ 0xFFFFFFFFUL
|
|
# error "sum of old-style masks != 0xFFFFFFFFUL"
|
|
#endif
|
|
#if REC_NEW_STATUS_MASK << (8 * (REC_NEW_STATUS - 3)) \
|
|
^ REC_HEAP_NO_MASK << (8 * (REC_NEW_HEAP_NO - 4)) \
|
|
^ REC_N_OWNED_MASK << (8 * (REC_NEW_N_OWNED - 3)) \
|
|
^ REC_INFO_BITS_MASK << (8 * (REC_NEW_INFO_BITS - 3)) \
|
|
^ 0xFFFFFFUL
|
|
# error "sum of new-style masks != 0xFFFFFFUL"
|
|
#endif
|
|
|
|
/***********************************************************//**
|
|
Sets the value of the ith field SQL null bit of an old-style record. */
|
|
void
|
|
rec_set_nth_field_null_bit(
|
|
/*=======================*/
|
|
rec_t* rec, /*!< in: record */
|
|
ulint i, /*!< in: ith field */
|
|
ibool val); /*!< in: value to set */
|
|
/***********************************************************//**
|
|
Sets an old-style record field to SQL null.
|
|
The physical size of the field is not changed. */
|
|
void
|
|
rec_set_nth_field_sql_null(
|
|
/*=======================*/
|
|
rec_t* rec, /*!< in: record */
|
|
ulint n); /*!< in: index of the field */
|
|
|
|
/******************************************************//**
|
|
Gets a bit field from within 1 byte. */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_bit_field_1(
|
|
/*================*/
|
|
const rec_t* rec, /*!< in: pointer to record origin */
|
|
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);
|
|
|
|
return((mach_read_from_1(rec - offs) & mask) >> shift);
|
|
}
|
|
|
|
/******************************************************//**
|
|
Sets a bit field within 1 byte. */
|
|
UNIV_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));
|
|
}
|
|
|
|
/******************************************************//**
|
|
Gets a bit field from within 2 bytes. */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_bit_field_2(
|
|
/*================*/
|
|
const rec_t* rec, /*!< in: pointer to record origin */
|
|
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);
|
|
|
|
return((mach_read_from_2(rec - offs) & mask) >> shift);
|
|
}
|
|
|
|
/******************************************************//**
|
|
Sets a bit field within 2 bytes. */
|
|
UNIV_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));
|
|
}
|
|
|
|
/******************************************************//**
|
|
The following function is used to get the pointer of the next chained record
|
|
on the same page.
|
|
@return pointer to the next chained record, or NULL if none */
|
|
UNIV_INLINE
|
|
const rec_t*
|
|
rec_get_next_ptr_const(
|
|
/*===================*/
|
|
const rec_t* rec, /*!< in: physical record */
|
|
ulint comp) /*!< in: nonzero=compact page format */
|
|
{
|
|
ulint field_value;
|
|
|
|
ut_ad(REC_NEXT_MASK == 0xFFFFUL);
|
|
ut_ad(REC_NEXT_SHIFT == 0);
|
|
|
|
field_value = mach_read_from_2(rec - REC_NEXT);
|
|
|
|
if (field_value == 0) {
|
|
|
|
return(NULL);
|
|
}
|
|
|
|
if (comp) {
|
|
#if UNIV_PAGE_SIZE_MAX <= 32768
|
|
/* Note that for 64 KiB pages, field_value can 'wrap around'
|
|
and the debug assertion is not valid */
|
|
|
|
/* In the following assertion, field_value is interpreted
|
|
as signed 16-bit integer in 2's complement arithmetics.
|
|
If all platforms defined int16_t in the standard headers,
|
|
the expression could be written simpler as
|
|
(int16_t) field_value + ut_align_offset(...) < UNIV_PAGE_SIZE
|
|
*/
|
|
ut_ad((field_value >= 32768
|
|
? field_value - 65536
|
|
: field_value)
|
|
+ ut_align_offset(rec, UNIV_PAGE_SIZE)
|
|
< UNIV_PAGE_SIZE);
|
|
#endif
|
|
/* 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) -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);
|
|
}
|
|
}
|
|
|
|
/******************************************************//**
|
|
The following function is used to get the pointer of the next chained record
|
|
on the same page.
|
|
@return pointer to the next chained record, or NULL if none */
|
|
UNIV_INLINE
|
|
rec_t*
|
|
rec_get_next_ptr(
|
|
/*=============*/
|
|
rec_t* rec, /*!< in: physical record */
|
|
ulint comp) /*!< in: nonzero=compact page format */
|
|
{
|
|
return(const_cast<rec_t*>(rec_get_next_ptr_const(rec, comp)));
|
|
}
|
|
|
|
/******************************************************//**
|
|
The following function is used to get the offset of the next chained record
|
|
on the same page.
|
|
@return the page offset of the next chained record, or 0 if none */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_next_offs(
|
|
/*==============*/
|
|
const rec_t* rec, /*!< in: physical record */
|
|
ulint comp) /*!< in: nonzero=compact page format */
|
|
{
|
|
ulint field_value;
|
|
#if REC_NEXT_MASK != 0xFFFFUL
|
|
# error "REC_NEXT_MASK != 0xFFFFUL"
|
|
#endif
|
|
#if REC_NEXT_SHIFT
|
|
# error "REC_NEXT_SHIFT != 0"
|
|
#endif
|
|
|
|
field_value = mach_read_from_2(rec - REC_NEXT);
|
|
|
|
if (comp) {
|
|
#if UNIV_PAGE_SIZE_MAX <= 32768
|
|
/* Note that for 64 KiB pages, field_value can 'wrap around'
|
|
and the debug assertion is not valid */
|
|
|
|
/* In the following assertion, field_value is interpreted
|
|
as signed 16-bit integer in 2's complement arithmetics.
|
|
If all platforms defined int16_t in the standard headers,
|
|
the expression could be written simpler as
|
|
(int16_t) field_value + ut_align_offset(...) < UNIV_PAGE_SIZE
|
|
*/
|
|
ut_ad((field_value >= 32768
|
|
? field_value - 65536
|
|
: field_value)
|
|
+ ut_align_offset(rec, UNIV_PAGE_SIZE)
|
|
< UNIV_PAGE_SIZE);
|
|
#endif
|
|
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) -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. */
|
|
UNIV_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);
|
|
#if REC_NEXT_MASK != 0xFFFFUL
|
|
# error "REC_NEXT_MASK != 0xFFFFUL"
|
|
#endif
|
|
#if REC_NEXT_SHIFT
|
|
# error "REC_NEXT_SHIFT != 0"
|
|
#endif
|
|
|
|
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. */
|
|
UNIV_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 get the number of fields
|
|
in an old-style record.
|
|
@return number of data fields */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_n_fields_old(
|
|
/*=================*/
|
|
const rec_t* rec) /*!< in: physical record */
|
|
{
|
|
ulint ret;
|
|
|
|
ut_ad(rec);
|
|
|
|
ret = rec_get_bit_field_2(rec, REC_OLD_N_FIELDS,
|
|
REC_OLD_N_FIELDS_MASK,
|
|
REC_OLD_N_FIELDS_SHIFT);
|
|
ut_ad(ret <= REC_MAX_N_FIELDS);
|
|
ut_ad(ret > 0);
|
|
|
|
return(ret);
|
|
}
|
|
|
|
/******************************************************//**
|
|
The following function is used to set the number of fields
|
|
in an old-style record. */
|
|
UNIV_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 fields
|
|
in a record.
|
|
@return number of data fields */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_n_fields(
|
|
/*=============*/
|
|
const rec_t* rec, /*!< in: physical record */
|
|
const dict_index_t* index) /*!< in: record descriptor */
|
|
{
|
|
ut_ad(rec);
|
|
ut_ad(index);
|
|
|
|
if (!dict_table_is_comp(index->table)) {
|
|
return(rec_get_n_fields_old(rec));
|
|
}
|
|
|
|
switch (rec_get_status(rec)) {
|
|
case REC_STATUS_COLUMNS_ADDED:
|
|
case REC_STATUS_ORDINARY:
|
|
return(dict_index_get_n_fields(index));
|
|
case REC_STATUS_NODE_PTR:
|
|
return(dict_index_get_n_unique_in_tree(index) + 1);
|
|
case REC_STATUS_INFIMUM:
|
|
case REC_STATUS_SUPREMUM:
|
|
return(1);
|
|
}
|
|
|
|
ut_error;
|
|
return(ULINT_UNDEFINED);
|
|
}
|
|
|
|
/** Confirms the n_fields of the entry is sane with comparing the other
|
|
record in the same page specified
|
|
@param[in] index index
|
|
@param[in] rec record of the same page
|
|
@param[in] entry index entry
|
|
@return true if n_fields is sane */
|
|
UNIV_INLINE
|
|
bool
|
|
rec_n_fields_is_sane(
|
|
dict_index_t* index,
|
|
const rec_t* rec,
|
|
const dtuple_t* entry)
|
|
{
|
|
const ulint n_fields = rec_get_n_fields(rec, index);
|
|
|
|
return(n_fields == dtuple_get_n_fields(entry)
|
|
|| (index->is_instant()
|
|
&& n_fields >= index->n_core_fields)
|
|
/* a record for older SYS_INDEXES table
|
|
(missing merge_threshold column) is acceptable. */
|
|
|| (index->table->id == DICT_INDEXES_ID
|
|
&& n_fields == dtuple_get_n_fields(entry) - 1));
|
|
}
|
|
|
|
/******************************************************//**
|
|
The following function is used to get the number of records owned by the
|
|
previous directory record.
|
|
@return number of owned records */
|
|
UNIV_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. */
|
|
UNIV_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 */
|
|
UNIV_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. */
|
|
UNIV_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);
|
|
}
|
|
}
|
|
|
|
#ifdef UNIV_DEBUG
|
|
/** Check if the info bits are valid.
|
|
@param[in] bits info bits to check
|
|
@return true if valid */
|
|
inline
|
|
bool
|
|
rec_info_bits_valid(
|
|
ulint bits)
|
|
{
|
|
return(0 == (bits & ~(REC_INFO_DELETED_FLAG | REC_INFO_MIN_REC_FLAG)));
|
|
}
|
|
#endif /* UNIV_DEBUG */
|
|
|
|
/******************************************************//**
|
|
The following function is used to retrieve the info bits of a record.
|
|
@return info bits */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_info_bits(
|
|
/*==============*/
|
|
const rec_t* rec, /*!< in: physical record */
|
|
ulint comp) /*!< in: nonzero=compact page format */
|
|
{
|
|
const ulint val = rec_get_bit_field_1(
|
|
rec, comp ? REC_NEW_INFO_BITS : REC_OLD_INFO_BITS,
|
|
REC_INFO_BITS_MASK, REC_INFO_BITS_SHIFT);
|
|
ut_ad(rec_info_bits_valid(val));
|
|
return(val);
|
|
}
|
|
|
|
/******************************************************//**
|
|
The following function is used to set the info bits of a record. */
|
|
UNIV_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. */
|
|
UNIV_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 retrieve the info and status
|
|
bits of a record. (Only compact records have status bits.)
|
|
@return info bits */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_info_and_status_bits(
|
|
/*=========================*/
|
|
const rec_t* rec, /*!< in: physical record */
|
|
ulint comp) /*!< in: nonzero=compact page format */
|
|
{
|
|
ulint bits;
|
|
#if (REC_NEW_STATUS_MASK >> REC_NEW_STATUS_SHIFT) \
|
|
& (REC_INFO_BITS_MASK >> REC_INFO_BITS_SHIFT)
|
|
# error "REC_NEW_STATUS_MASK and REC_INFO_BITS_MASK overlap"
|
|
#endif
|
|
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.) */
|
|
UNIV_INLINE
|
|
void
|
|
rec_set_info_and_status_bits(
|
|
/*=========================*/
|
|
rec_t* rec, /*!< in/out: physical record */
|
|
ulint bits) /*!< in: info bits */
|
|
{
|
|
#if (REC_NEW_STATUS_MASK >> REC_NEW_STATUS_SHIFT) \
|
|
& (REC_INFO_BITS_MASK >> REC_INFO_BITS_SHIFT)
|
|
# error "REC_NEW_STATUS_MASK and REC_INFO_BITS_MASK overlap"
|
|
#endif
|
|
rec_set_status(rec, bits & REC_NEW_STATUS_MASK);
|
|
rec_set_info_bits_new(rec, bits & ~REC_NEW_STATUS_MASK);
|
|
}
|
|
|
|
/******************************************************//**
|
|
The following function tells if record is delete marked.
|
|
@return nonzero if delete marked */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_deleted_flag(
|
|
/*=================*/
|
|
const rec_t* rec, /*!< in: physical record */
|
|
ulint comp) /*!< in: nonzero=compact page format */
|
|
{
|
|
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. */
|
|
UNIV_INLINE
|
|
void
|
|
rec_set_deleted_flag_old(
|
|
/*=====================*/
|
|
rec_t* rec, /*!< in: old-style physical record */
|
|
ulint flag) /*!< in: nonzero if delete marked */
|
|
{
|
|
ulint val;
|
|
|
|
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. */
|
|
UNIV_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 */
|
|
ulint 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 tells if a new-style record is a node pointer.
|
|
@return TRUE if node pointer */
|
|
UNIV_INLINE
|
|
ibool
|
|
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 */
|
|
UNIV_INLINE
|
|
ulint
|
|
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. */
|
|
UNIV_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 */
|
|
UNIV_INLINE
|
|
ulint
|
|
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. */
|
|
UNIV_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 test whether the data offsets in the record
|
|
are stored in one-byte or two-byte format.
|
|
@return TRUE if 1-byte form */
|
|
UNIV_INLINE
|
|
ibool
|
|
rec_get_1byte_offs_flag(
|
|
/*====================*/
|
|
const rec_t* rec) /*!< in: physical record */
|
|
{
|
|
#if TRUE != 1
|
|
#error "TRUE != 1"
|
|
#endif
|
|
|
|
return(rec_get_bit_field_1(rec, REC_OLD_SHORT, REC_OLD_SHORT_MASK,
|
|
REC_OLD_SHORT_SHIFT));
|
|
}
|
|
|
|
/******************************************************//**
|
|
The following function is used to set the 1-byte offsets flag. */
|
|
UNIV_INLINE
|
|
void
|
|
rec_set_1byte_offs_flag(
|
|
/*====================*/
|
|
rec_t* rec, /*!< in: physical record */
|
|
ibool flag) /*!< in: TRUE if 1byte form */
|
|
{
|
|
#if TRUE != 1
|
|
#error "TRUE != 1"
|
|
#endif
|
|
ut_ad(flag <= TRUE);
|
|
|
|
rec_set_bit_field_1(rec, flag, REC_OLD_SHORT, REC_OLD_SHORT_MASK,
|
|
REC_OLD_SHORT_SHIFT);
|
|
}
|
|
|
|
/******************************************************//**
|
|
Returns the offset of nth 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.
|
|
@return offset of the start of the field, SQL null flag ORed */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_1_get_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(rec));
|
|
|
|
return(mach_read_from_1(rec - (REC_N_OLD_EXTRA_BYTES + n + 1)));
|
|
}
|
|
|
|
/******************************************************//**
|
|
Returns the offset of nth 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 field, SQL null flag and extern
|
|
storage flag ORed */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_2_get_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(rec));
|
|
|
|
return(mach_read_from_2(rec - (REC_N_OLD_EXTRA_BYTES + 2 * n + 2)));
|
|
}
|
|
|
|
/******************************************************//**
|
|
Returns nonzero if the field is stored off-page.
|
|
@retval 0 if the field is stored in-page
|
|
@retval REC_2BYTE_EXTERN_MASK if the field is stored externally */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_2_is_field_extern(
|
|
/*==================*/
|
|
const rec_t* rec, /*!< in: record */
|
|
ulint n) /*!< in: field index */
|
|
{
|
|
return(rec_2_get_field_end_info(rec, n) & REC_2BYTE_EXTERN_MASK);
|
|
}
|
|
|
|
/**********************************************************//**
|
|
The following function sets the number of allocated elements
|
|
for an array of offsets. */
|
|
UNIV_INLINE
|
|
void
|
|
rec_offs_set_n_alloc(
|
|
/*=================*/
|
|
ulint* offsets, /*!< out: array for rec_get_offsets(),
|
|
must be allocated */
|
|
ulint n_alloc) /*!< in: number of elements */
|
|
{
|
|
ut_ad(offsets);
|
|
ut_ad(n_alloc > REC_OFFS_HEADER_SIZE);
|
|
UNIV_MEM_ASSERT_AND_ALLOC(offsets, n_alloc * sizeof *offsets);
|
|
offsets[0] = n_alloc;
|
|
}
|
|
|
|
/************************************************************//**
|
|
The following function is used to get an offset to the nth
|
|
data field in a record.
|
|
@return offset from the origin of rec */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_nth_field_offs(
|
|
/*===================*/
|
|
const ulint* offsets,/*!< in: array returned by rec_get_offsets() */
|
|
ulint n, /*!< in: index of the field */
|
|
ulint* len) /*!< out: length of the field; UNIV_SQL_NULL
|
|
if SQL null; UNIV_SQL_DEFAULT is default value */
|
|
{
|
|
ulint offs;
|
|
ulint length;
|
|
ut_ad(n < rec_offs_n_fields(offsets));
|
|
ut_ad(len);
|
|
|
|
if (n == 0) {
|
|
offs = 0;
|
|
} else {
|
|
offs = rec_offs_base(offsets)[n] & REC_OFFS_MASK;
|
|
}
|
|
|
|
length = rec_offs_base(offsets)[1 + n];
|
|
|
|
if (length & REC_OFFS_SQL_NULL) {
|
|
length = UNIV_SQL_NULL;
|
|
} else if (length & REC_OFFS_DEFAULT) {
|
|
length = UNIV_SQL_DEFAULT;
|
|
} else {
|
|
length &= REC_OFFS_MASK;
|
|
length -= offs;
|
|
}
|
|
|
|
*len = length;
|
|
return(offs);
|
|
}
|
|
|
|
/******************************************************//**
|
|
Determine if the offsets are for a record containing null BLOB pointers.
|
|
@return first field containing a null BLOB pointer, or NULL if none found */
|
|
UNIV_INLINE
|
|
const byte*
|
|
rec_offs_any_null_extern(
|
|
/*=====================*/
|
|
const rec_t* rec, /*!< in: record */
|
|
const ulint* offsets) /*!< in: rec_get_offsets(rec) */
|
|
{
|
|
ulint i;
|
|
ut_ad(rec_offs_validate(rec, NULL, offsets));
|
|
|
|
if (!rec_offs_any_extern(offsets)) {
|
|
return(NULL);
|
|
}
|
|
|
|
for (i = 0; i < rec_offs_n_fields(offsets); i++) {
|
|
if (rec_offs_nth_extern(offsets, i)) {
|
|
ulint len;
|
|
const byte* field
|
|
= rec_get_nth_field(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(NULL);
|
|
}
|
|
|
|
/******************************************************//**
|
|
Returns nonzero if the extern bit is set in nth field of rec.
|
|
@return nonzero if externally stored */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_offs_nth_extern_old(
|
|
/*================*/
|
|
const rec_t* rec, /*!< in: record */
|
|
ulint n /*!< in: index of the field */)
|
|
{
|
|
if(rec_get_1byte_offs_flag(rec))
|
|
return 0;
|
|
return (rec_2_get_field_end_info(rec,n) & REC_2BYTE_EXTERN_MASK);
|
|
}
|
|
|
|
/******************************************************//**
|
|
Gets the physical size of a field.
|
|
@return length of field */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_offs_nth_size(
|
|
/*==============*/
|
|
const ulint* offsets,/*!< in: array returned by rec_get_offsets() */
|
|
ulint n) /*!< in: nth field */
|
|
{
|
|
ut_ad(rec_offs_validate(NULL, NULL, offsets));
|
|
ut_ad(n < rec_offs_n_fields(offsets));
|
|
if (!n) {
|
|
return(rec_offs_base(offsets)[1 + n] & REC_OFFS_MASK);
|
|
}
|
|
return((rec_offs_base(offsets)[1 + n] - rec_offs_base(offsets)[n])
|
|
& REC_OFFS_MASK);
|
|
}
|
|
|
|
/******************************************************//**
|
|
Returns the number of extern bits set in a record.
|
|
@return number of externally stored fields */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_offs_n_extern(
|
|
/*==============*/
|
|
const ulint* offsets)/*!< in: array returned by rec_get_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(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 */
|
|
UNIV_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(rec));
|
|
|
|
return(mach_read_from_1(rec - (REC_N_OLD_EXTRA_BYTES + 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 */
|
|
UNIV_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(rec));
|
|
|
|
return(mach_read_from_2(rec - (REC_N_OLD_EXTRA_BYTES + 2 * n)));
|
|
}
|
|
|
|
/******************************************************//**
|
|
Sets the field end info for the nth field if the record is stored in the
|
|
1-byte format. */
|
|
UNIV_INLINE
|
|
void
|
|
rec_1_set_field_end_info(
|
|
/*=====================*/
|
|
rec_t* rec, /*!< in: record */
|
|
ulint n, /*!< in: field index */
|
|
ulint info) /*!< in: value to set */
|
|
{
|
|
ut_ad(rec_get_1byte_offs_flag(rec));
|
|
ut_ad(n < rec_get_n_fields_old(rec));
|
|
|
|
mach_write_to_1(rec - (REC_N_OLD_EXTRA_BYTES + n + 1), info);
|
|
}
|
|
|
|
/******************************************************//**
|
|
Sets the field end info for the nth field if the record is stored in the
|
|
2-byte format. */
|
|
UNIV_INLINE
|
|
void
|
|
rec_2_set_field_end_info(
|
|
/*=====================*/
|
|
rec_t* rec, /*!< in: record */
|
|
ulint n, /*!< in: field index */
|
|
ulint info) /*!< in: value to set */
|
|
{
|
|
ut_ad(!rec_get_1byte_offs_flag(rec));
|
|
ut_ad(n < rec_get_n_fields_old(rec));
|
|
|
|
mach_write_to_2(rec - (REC_N_OLD_EXTRA_BYTES + 2 * n + 2), info);
|
|
}
|
|
|
|
/******************************************************//**
|
|
Returns the offset of nth field start if the record is stored in the 1-byte
|
|
offsets form.
|
|
@return offset of the start of the field */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_1_get_field_start_offs(
|
|
/*=======================*/
|
|
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(rec));
|
|
|
|
if (n == 0) {
|
|
|
|
return(0);
|
|
}
|
|
|
|
return(rec_1_get_prev_field_end_info(rec, n)
|
|
& ~REC_1BYTE_SQL_NULL_MASK);
|
|
}
|
|
|
|
/******************************************************//**
|
|
Returns the offset of nth field start if the record is stored in the 2-byte
|
|
offsets form.
|
|
@return offset of the start of the field */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_2_get_field_start_offs(
|
|
/*=======================*/
|
|
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(rec));
|
|
|
|
if (n == 0) {
|
|
|
|
return(0);
|
|
}
|
|
|
|
return(rec_2_get_prev_field_end_info(rec, n)
|
|
& ~(REC_2BYTE_SQL_NULL_MASK | REC_2BYTE_EXTERN_MASK));
|
|
}
|
|
|
|
/******************************************************//**
|
|
The following function is used to read the offset of the start of a data field
|
|
in the record. The start of an SQL null field is the end offset of the
|
|
previous non-null field, or 0, if none exists. If n is the number of the last
|
|
field + 1, then the end offset of the last field is returned.
|
|
@return offset of the start of the field */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_field_start_offs(
|
|
/*=====================*/
|
|
const rec_t* rec, /*!< in: record */
|
|
ulint n) /*!< in: field index */
|
|
{
|
|
ut_ad(rec);
|
|
ut_ad(n <= rec_get_n_fields_old(rec));
|
|
|
|
if (n == 0) {
|
|
|
|
return(0);
|
|
}
|
|
|
|
if (rec_get_1byte_offs_flag(rec)) {
|
|
|
|
return(rec_1_get_field_start_offs(rec, n));
|
|
}
|
|
|
|
return(rec_2_get_field_start_offs(rec, n));
|
|
}
|
|
|
|
/************************************************************//**
|
|
Gets the physical size of an old-style field.
|
|
Also an SQL null may have a field of size > 0,
|
|
if the data type is of a fixed size.
|
|
@return field size in bytes */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_nth_field_size(
|
|
/*===================*/
|
|
const rec_t* rec, /*!< in: record */
|
|
ulint n) /*!< in: index of the field */
|
|
{
|
|
ulint os;
|
|
ulint next_os;
|
|
|
|
os = rec_get_field_start_offs(rec, n);
|
|
next_os = rec_get_field_start_offs(rec, n + 1);
|
|
|
|
ut_ad(next_os - os < UNIV_PAGE_SIZE);
|
|
|
|
return(next_os - os);
|
|
}
|
|
|
|
/***********************************************************//**
|
|
This is used to modify the value of an already existing field in a record.
|
|
The previous value must have exactly the same size as the new value. If len
|
|
is UNIV_SQL_NULL then the field is treated as an SQL null.
|
|
For records in ROW_FORMAT=COMPACT (new-style records), len must not be
|
|
UNIV_SQL_NULL unless the field already is SQL null. */
|
|
UNIV_INLINE
|
|
void
|
|
rec_set_nth_field(
|
|
/*==============*/
|
|
rec_t* rec, /*!< in: record */
|
|
const ulint* offsets,/*!< in: array returned by rec_get_offsets() */
|
|
ulint n, /*!< in: index number of the field */
|
|
const void* data, /*!< in: pointer to the data
|
|
if not SQL null */
|
|
ulint len) /*!< in: length of the data or UNIV_SQL_NULL */
|
|
{
|
|
byte* data2;
|
|
ulint len2;
|
|
|
|
ut_ad(rec);
|
|
ut_ad(rec_offs_validate(rec, NULL, offsets));
|
|
ut_ad(!rec_offs_nth_default(offsets, n));
|
|
|
|
if (len == UNIV_SQL_NULL) {
|
|
if (!rec_offs_nth_sql_null(offsets, n)) {
|
|
ut_a(!rec_offs_comp(offsets));
|
|
rec_set_nth_field_sql_null(rec, n);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
data2 = (byte*)rec_get_nth_field(rec, offsets, n, &len2);
|
|
if (len2 == UNIV_SQL_NULL) {
|
|
ut_ad(!rec_offs_comp(offsets));
|
|
rec_set_nth_field_null_bit(rec, n, FALSE);
|
|
ut_ad(len == rec_get_nth_field_size(rec, n));
|
|
} else {
|
|
ut_ad(len2 == len);
|
|
}
|
|
|
|
ut_memcpy(data2, data, len);
|
|
}
|
|
|
|
/**********************************************************//**
|
|
The following function returns the data size of an old-style 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 */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_data_size_old(
|
|
/*==================*/
|
|
const rec_t* rec) /*!< in: physical record */
|
|
{
|
|
ut_ad(rec);
|
|
|
|
return(rec_get_field_start_offs(rec, rec_get_n_fields_old(rec)));
|
|
}
|
|
|
|
/**********************************************************//**
|
|
The following function sets the number of fields in offsets. */
|
|
UNIV_INLINE
|
|
void
|
|
rec_offs_set_n_fields(
|
|
/*==================*/
|
|
ulint* offsets, /*!< in/out: array returned by
|
|
rec_get_offsets() */
|
|
ulint n_fields) /*!< in: number of fields */
|
|
{
|
|
ut_ad(offsets);
|
|
ut_ad(n_fields > 0);
|
|
ut_ad(n_fields <= REC_MAX_N_FIELDS);
|
|
ut_ad(n_fields + REC_OFFS_HEADER_SIZE
|
|
<= rec_offs_get_n_alloc(offsets));
|
|
offsets[1] = n_fields;
|
|
}
|
|
|
|
/**********************************************************//**
|
|
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 */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_offs_data_size(
|
|
/*===============*/
|
|
const ulint* offsets)/*!< in: array returned by rec_get_offsets() */
|
|
{
|
|
ulint size;
|
|
|
|
ut_ad(rec_offs_validate(NULL, NULL, 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 */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_offs_extra_size(
|
|
/*================*/
|
|
const ulint* offsets)/*!< in: array returned by rec_get_offsets() */
|
|
{
|
|
ulint size;
|
|
ut_ad(rec_offs_validate(NULL, NULL, offsets));
|
|
size = *rec_offs_base(offsets) & REC_OFFS_MASK;
|
|
ut_ad(size < UNIV_PAGE_SIZE);
|
|
return(size);
|
|
}
|
|
|
|
/**********************************************************//**
|
|
Returns the total size of a physical record.
|
|
@return size */
|
|
UNIV_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
|
|
/**********************************************************//**
|
|
Returns a pointer to the end of the record.
|
|
@return pointer to end */
|
|
UNIV_INLINE
|
|
byte*
|
|
rec_get_end(
|
|
/*========*/
|
|
const rec_t* rec, /*!< in: pointer to record */
|
|
const ulint* offsets)/*!< in: array returned by rec_get_offsets() */
|
|
{
|
|
ut_ad(rec_offs_validate(rec, NULL, offsets));
|
|
return(const_cast<rec_t*>(rec + rec_offs_data_size(offsets)));
|
|
}
|
|
|
|
/**********************************************************//**
|
|
Returns a pointer to the start of the record.
|
|
@return pointer to start */
|
|
UNIV_INLINE
|
|
byte*
|
|
rec_get_start(
|
|
/*==========*/
|
|
const rec_t* rec, /*!< in: pointer to record */
|
|
const ulint* offsets)/*!< in: array returned by rec_get_offsets() */
|
|
{
|
|
ut_ad(rec_offs_validate(rec, NULL, offsets));
|
|
return(const_cast<rec_t*>(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 */
|
|
UNIV_INLINE
|
|
rec_t*
|
|
rec_copy(
|
|
void* buf,
|
|
const rec_t* rec,
|
|
const ulint* offsets)
|
|
{
|
|
ulint extra_len;
|
|
ulint data_len;
|
|
|
|
ut_ad(rec != NULL);
|
|
ut_ad(buf != NULL);
|
|
ut_ad(rec_offs_validate(rec, NULL, 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.
|
|
@return extra size */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_converted_extra_size(
|
|
/*=========================*/
|
|
ulint data_size, /*!< in: data size */
|
|
ulint n_fields, /*!< in: number of fields */
|
|
ulint n_ext) /*!< in: number of externally stored columns */
|
|
{
|
|
if (!n_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.
|
|
@return size */
|
|
UNIV_INLINE
|
|
ulint
|
|
rec_get_converted_size(
|
|
/*===================*/
|
|
dict_index_t* index, /*!< in: record descriptor */
|
|
const dtuple_t* dtuple, /*!< in: data tuple */
|
|
ulint n_ext) /*!< in: number of externally stored columns */
|
|
{
|
|
ulint data_size;
|
|
ulint extra_size;
|
|
|
|
ut_ad(index);
|
|
ut_ad(dtuple);
|
|
ut_ad(dtuple_check_typed(dtuple));
|
|
#ifdef UNIV_DEBUG
|
|
if (dict_index_is_ibuf(index)) {
|
|
ut_ad(dtuple->n_fields > 1);
|
|
} else if ((dtuple_get_info_bits(dtuple) & REC_NEW_STATUS_MASK)
|
|
== REC_STATUS_NODE_PTR) {
|
|
ut_ad(dtuple->n_fields
|
|
== dict_index_get_n_unique_in_tree_nonleaf(index) + 1);
|
|
} else if (index->table->id == DICT_INDEXES_ID) {
|
|
/* The column SYS_INDEXES.MERGE_THRESHOLD was
|
|
instantly added in MariaDB 10.2.2 (MySQL 5.7). */
|
|
ut_ad(index->n_fields == DICT_NUM_FIELDS__SYS_INDEXES);
|
|
ut_ad(dtuple->n_fields == DICT_NUM_FIELDS__SYS_INDEXES
|
|
|| dtuple->n_fields
|
|
== DICT_FLD__SYS_INDEXES__MERGE_THRESHOLD);
|
|
} else {
|
|
ut_ad(dtuple->n_fields >= index->n_core_fields);
|
|
ut_ad(dtuple->n_fields <= index->n_fields);
|
|
}
|
|
#endif
|
|
|
|
if (dict_table_is_comp(index->table)) {
|
|
return(rec_get_converted_size_comp(
|
|
index,
|
|
static_cast<rec_comp_status_t>(
|
|
dtuple->info_bits
|
|
& REC_NEW_STATUS_MASK),
|
|
dtuple->fields,
|
|
dtuple->n_fields, NULL));
|
|
}
|
|
|
|
data_size = dtuple_get_data_size(dtuple, 0);
|
|
|
|
extra_size = rec_get_converted_extra_size(
|
|
data_size, dtuple_get_n_fields(dtuple), n_ext);
|
|
|
|
return(data_size + extra_size);
|
|
}
|