| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/data0data.h |
| 29 |
SQL data field and tuple |
| 30 |
|
| 31 |
Created 5/30/1994 Heikki Tuuri |
| 32 |
*************************************************************************/ |
| 33 |
|
| 34 |
#ifndef data0data_h |
| 35 |
#define data0data_h |
| 36 |
|
| 37 |
#include "univ.i" |
| 38 |
|
| 39 |
#include "data0type.h" |
| 40 |
#include "data0types.h" |
| 41 |
#include "dict0types.h" |
| 42 |
#include "mem0mem.h" |
| 43 |
#include "trx0types.h" |
| 44 |
#include "ut0bitset.h" |
| 45 |
|
| 46 |
#include <ostream> |
| 47 |
|
| 48 |
/** Storage for overflow data in a big record, that is, a clustered |
| 49 |
index record which needs external storage of data fields */ |
| 50 |
struct big_rec_t; |
| 51 |
struct upd_t; |
| 52 |
|
| 53 |
#ifdef UNIV_DEBUG |
| 54 |
/** Gets pointer to the type struct of SQL data field. |
| 55 |
@return pointer to the type struct */ |
| 56 |
[[nodiscard]] static inline dtype_t *dfield_get_type( |
| 57 |
const dfield_t *field); /*!< in: SQL data field */ |
| 58 |
/** Gets pointer to the data in a field. |
| 59 |
@return pointer to data */ |
| 60 |
[[nodiscard]] static inline void *dfield_get_data( |
| 61 |
const dfield_t *field); /*!< in: field */ |
| 62 |
#else /* UNIV_DEBUG */ |
| 63 |
#define dfield_get_type(field) (&(field)->type) |
| 64 |
#define dfield_get_data(field) ((field)->data) |
| 65 |
#endif /* UNIV_DEBUG */ |
| 66 |
|
| 67 |
/** Sets the type struct of SQL data field. |
| 68 |
@param[in] field SQL data field |
| 69 |
@param[in] type pointer to data type struct */ |
| 70 |
static inline void dfield_set_type(dfield_t *field, const dtype_t *type); |
| 71 |
|
| 72 |
/** Gets length of field data. |
| 73 |
@return length of data; UNIV_SQL_NULL if SQL null data */ |
| 74 |
[[nodiscard]] static inline uint32_t dfield_get_len( |
| 75 |
const dfield_t *field); /*!< in: field */ |
| 76 |
|
| 77 |
/** Sets length in a field. |
| 78 |
@param[in] field field |
| 79 |
@param[in] len length or UNIV_SQL_NULL */ |
| 80 |
static inline void dfield_set_len(dfield_t *field, ulint len); |
| 81 |
|
| 82 |
/** Determines if a field is SQL NULL |
| 83 |
@return nonzero if SQL null data */ |
| 84 |
[[nodiscard]] static inline ulint dfield_is_null( |
| 85 |
const dfield_t *field); /*!< in: field */ |
| 86 |
/** Determines if a field is externally stored |
| 87 |
@return nonzero if externally stored */ |
| 88 |
[[nodiscard]] static inline bool dfield_is_ext( |
| 89 |
const dfield_t *field); /*!< in: field */ |
| 90 |
/** Sets the "external storage" flag */ |
| 91 |
static inline void dfield_set_ext(dfield_t *field); /*!< in/out: field */ |
| 92 |
|
| 93 |
/** Gets spatial status for "external storage" |
| 94 |
@param[in,out] field field */ |
| 95 |
static inline spatial_status_t dfield_get_spatial_status(const dfield_t *field); |
| 96 |
|
| 97 |
/** Sets spatial status for "external storage" |
| 98 |
@param[in,out] field field |
| 99 |
@param[in] spatial_status spatial status */ |
| 100 |
static inline void dfield_set_spatial_status(dfield_t *field, |
| 101 |
spatial_status_t spatial_status); |
| 102 |
|
| 103 |
/** Sets pointer to the data and length in a field. |
| 104 |
@param[in] field field |
| 105 |
@param[in] data data |
| 106 |
@param[in] len length or UNIV_SQL_NULL */ |
| 107 |
static inline void dfield_set_data(dfield_t *field, const void *data, |
| 108 |
ulint len); |
| 109 |
|
| 110 |
/** Sets pointer to the data and length in a field. |
| 111 |
@param[in] field field |
| 112 |
@param[in] mbr data */ |
| 113 |
static inline void dfield_write_mbr(dfield_t *field, const double *mbr); |
| 114 |
|
| 115 |
/** Sets a data field to SQL NULL. */ |
| 116 |
static inline void dfield_set_null(dfield_t *field); /*!< in/out: field */ |
| 117 |
|
| 118 |
/** Writes an SQL null field full of zeros. |
| 119 |
@param[in] data pointer to a buffer of size len |
| 120 |
@param[in] len SQL null size in bytes */ |
| 121 |
static inline void data_write_sql_null(byte *data, ulint len); |
| 122 |
|
| 123 |
/** Copies the data and len fields. |
| 124 |
@param[out] field1 field to copy to |
| 125 |
@param[in] field2 field to copy from */ |
| 126 |
static inline void dfield_copy_data(dfield_t *field1, const dfield_t *field2); |
| 127 |
|
| 128 |
/** Copies a data field to another. |
| 129 |
@param[out] field1 field to copy to |
| 130 |
@param[in] field2 field to copy from */ |
| 131 |
static inline void dfield_copy(dfield_t *field1, const dfield_t *field2); |
| 132 |
|
| 133 |
/** Copies the data pointed to by a data field. |
| 134 |
@param[in,out] field data field |
| 135 |
@param[in] heap memory heap where allocated */ |
| 136 |
static inline void dfield_dup(dfield_t *field, mem_heap_t *heap); |
| 137 |
|
| 138 |
/** Copies the data pointed to by a data field. |
| 139 |
This function works for multi-value fields only. |
| 140 |
@param[in,out] field data field |
| 141 |
@param[in] heap memory heap where allocated */ |
| 142 |
static inline void dfield_multi_value_dup(dfield_t *field, mem_heap_t *heap); |
| 143 |
|
| 144 |
/** Determine if a field is of multi-value type |
| 145 |
@param[in] field data field |
| 146 |
@return true if multi-value type field, otherwise false */ |
| 147 |
static inline bool dfield_is_multi_value(const dfield_t *field); |
| 148 |
|
| 149 |
/** Tests if two data fields are equal. |
| 150 |
If len==0, tests the data length and content for equality. |
| 151 |
If len>0, tests the first len bytes of the content for equality. |
| 152 |
@param[in] field1 first field to compare |
| 153 |
@param[in] field2 second field to compare |
| 154 |
@param[in] len maximum prefix to compare, |
| 155 |
or 0 to compare the whole field length. |
| 156 |
This works only if !multi_val |
| 157 |
@return true if both fields are NULL or if they are equal */ |
| 158 |
[[nodiscard]] inline bool dfield_datas_are_binary_equal(const dfield_t *field1, |
| 159 |
const dfield_t *field2, |
| 160 |
ulint len = 0); |
| 161 |
/** Tests if dfield data length and content is equal to the given. |
| 162 |
@param[in] field Field |
| 163 |
@param[in] len Data length or UNIV_SQL_NULL |
| 164 |
@param[in] data Data |
| 165 |
@return true if equal */ |
| 166 |
[[nodiscard]] static inline bool dfield_data_is_binary_equal( |
| 167 |
const dfield_t *field, ulint len, const byte *data); |
| 168 |
/** Gets number of fields in a data tuple. |
| 169 |
@return number of fields */ |
| 170 |
[[nodiscard]] static inline ulint dtuple_get_n_fields( |
| 171 |
const dtuple_t *tuple); /*!< in: tuple */ |
| 172 |
|
| 173 |
/** Gets number of virtual fields in a data tuple. |
| 174 |
@param[in] tuple dtuple to check |
| 175 |
@return number of fields */ |
| 176 |
static inline ulint dtuple_get_n_v_fields(const dtuple_t *tuple); |
| 177 |
|
| 178 |
#ifdef UNIV_DEBUG |
| 179 |
/** Gets nth field of a tuple. |
| 180 |
@param[in] tuple tuple |
| 181 |
@param[in] n index of field |
| 182 |
@return nth field */ |
| 183 |
static inline dfield_t *dtuple_get_nth_field(const dtuple_t *tuple, ulint n); |
| 184 |
|
| 185 |
/** Gets nth virtual field of a tuple. |
| 186 |
@param[in] tuple tuple |
| 187 |
@param[in] n the nth field to get |
| 188 |
@return nth field */ |
| 189 |
static inline dfield_t *dtuple_get_nth_v_field(const dtuple_t *tuple, ulint n); |
| 190 |
|
| 191 |
#else /* UNIV_DEBUG */ |
| 192 |
#define dtuple_get_nth_field(tuple, n) ((tuple)->fields + (n)) |
| 193 |
#define dtuple_get_nth_v_field(tuple, n) \ |
| 194 |
((tuple)->fields + (tuple)->n_fields + (n)) |
| 195 |
#endif /* UNIV_DEBUG */ |
| 196 |
/** Gets info bits in a data tuple. |
| 197 |
@return info bits */ |
| 198 |
[[nodiscard]] static inline ulint dtuple_get_info_bits( |
| 199 |
const dtuple_t *tuple); /*!< in: tuple */ |
| 200 |
|
| 201 |
/** Sets info bits in a data tuple. |
| 202 |
@param[in] tuple tuple |
| 203 |
@param[in] info_bits info bits */ |
| 204 |
static inline void dtuple_set_info_bits(dtuple_t *tuple, ulint info_bits); |
| 205 |
|
| 206 |
/** Gets number of fields used in record comparisons. |
| 207 |
@return number of fields used in comparisons in rem0cmp.* */ |
| 208 |
[[nodiscard]] static inline ulint dtuple_get_n_fields_cmp( |
| 209 |
const dtuple_t *tuple); /*!< in: tuple */ |
| 210 |
|
| 211 |
/** Gets number of fields used in record comparisons. |
| 212 |
@param[in] tuple tuple |
| 213 |
@param[in] n_fields_cmp number of fields used in comparisons in |
| 214 |
rem0cmp */ |
| 215 |
static inline void dtuple_set_n_fields_cmp(dtuple_t *tuple, ulint n_fields_cmp); |
| 216 |
|
| 217 |
/* Estimate the number of bytes that are going to be allocated when |
| 218 |
creating a new dtuple_t object */ |
| 219 |
#define DTUPLE_EST_ALLOC(n_fields) \ |
| 220 |
(sizeof(dtuple_t) + (n_fields) * sizeof(dfield_t)) |
| 221 |
|
| 222 |
/** Creates a data tuple from an already allocated chunk of memory. |
| 223 |
The size of the chunk must be at least DTUPLE_EST_ALLOC(n_fields). |
| 224 |
The default value for number of fields used in record comparisons |
| 225 |
for this tuple is n_fields. |
| 226 |
@param[in,out] buf buffer to use |
| 227 |
@param[in] buf_size buffer size |
| 228 |
@param[in] n_fields number of field |
| 229 |
@param[in] n_v_fields number of fields on virtual columns |
| 230 |
@return created tuple (inside buf) */ |
| 231 |
[[nodiscard]] static inline dtuple_t *dtuple_create_from_mem(void *buf, |
| 232 |
ulint buf_size, |
| 233 |
ulint n_fields, |
| 234 |
ulint n_v_fields); |
| 235 |
/** Creates a data tuple to a memory heap. The default value for number |
| 236 |
of fields used in record comparisons for this tuple is n_fields. |
| 237 |
@return own: created tuple */ |
| 238 |
static inline dtuple_t *dtuple_create( |
| 239 |
mem_heap_t *heap, /*!< in: memory heap where the tuple |
| 240 |
is created, DTUPLE_EST_ALLOC(n_fields) |
| 241 |
bytes will be allocated from this heap */ |
| 242 |
ulint n_fields) /*!< in: number of fields */ |
| 243 |
MY_ATTRIBUTE((malloc)); |
| 244 |
|
| 245 |
/** Initialize the virtual field data in a dtuple_t |
| 246 |
@param[in,out] vrow dtuple contains the virtual fields */ |
| 247 |
static inline void dtuple_init_v_fld(const dtuple_t *vrow); |
| 248 |
|
| 249 |
/** Duplicate the virtual field data in a dtuple_t |
| 250 |
@param[in,out] vrow dtuple contains the virtual fields |
| 251 |
@param[in] heap heap memory to use */ |
| 252 |
static inline void dtuple_dup_v_fld(const dtuple_t *vrow, mem_heap_t *heap); |
| 253 |
|
| 254 |
/** Creates a data tuple with possible virtual columns to a memory heap. |
| 255 |
@param[in] heap memory heap where the tuple is created |
| 256 |
@param[in] n_fields number of fields |
| 257 |
@param[in] n_v_fields number of fields on virtual col |
| 258 |
@return own: created tuple */ |
| 259 |
static inline dtuple_t *dtuple_create_with_vcol(mem_heap_t *heap, |
| 260 |
ulint n_fields, |
| 261 |
ulint n_v_fields); |
| 262 |
/** Sets number of fields used in a tuple. Normally this is set in |
| 263 |
dtuple_create, but if you want later to set it smaller, you can use this. |
| 264 |
@param[in] tuple Tuple. |
| 265 |
@param[in] n_fields Number of fields. */ |
| 266 |
void dtuple_set_n_fields(dtuple_t *tuple, ulint n_fields); |
| 267 |
|
| 268 |
/** Copies a data tuple's virtual fields to another. This is a shallow copy; |
| 269 |
@param[in,out] d_tuple destination tuple |
| 270 |
@param[in] s_tuple source tuple */ |
| 271 |
static inline void dtuple_copy_v_fields(dtuple_t *d_tuple, |
| 272 |
const dtuple_t *s_tuple); |
| 273 |
/** Copies a data tuple to another. This is a shallow copy; if a deep copy |
| 274 |
is desired, dfield_dup() will have to be invoked on each field. |
| 275 |
@return own: copy of tuple */ |
| 276 |
static inline dtuple_t *dtuple_copy( |
| 277 |
const dtuple_t *tuple, /*!< in: tuple to copy from */ |
| 278 |
mem_heap_t *heap) /*!< in: memory heap |
| 279 |
where the tuple is created */ |
| 280 |
MY_ATTRIBUTE((malloc)); |
| 281 |
|
| 282 |
/** The following function returns the sum of data lengths of a tuple. The space |
| 283 |
occupied by the field structs or the tuple struct is not counted. |
| 284 |
@param[in] tuple typed data tuple |
| 285 |
@param[in] comp nonzero=ROW_FORMAT=COMPACT |
| 286 |
@return sum of data lens */ |
| 287 |
static inline ulint dtuple_get_data_size(const dtuple_t *tuple, ulint comp); |
| 288 |
/** Compare two data tuples. |
| 289 |
@param[in] tuple1 first data tuple |
| 290 |
@param[in] tuple2 second data tuple |
| 291 |
@return whether tuple1==tuple2 */ |
| 292 |
[[nodiscard]] bool dtuple_coll_eq(const dtuple_t *tuple1, |
| 293 |
const dtuple_t *tuple2); |
| 294 |
|
| 295 |
/** Compute a hash value of a prefix of an index record. |
| 296 |
@param[in] tuple index record |
| 297 |
@param[in] n_fields number of fields to include |
| 298 |
@param[in] n_bytes number of bytes to hash in the last field |
| 299 |
@param[in] hash_value hash value of the index identifier |
| 300 |
@return the hashed value */ |
| 301 |
[[nodiscard]] static inline uint64_t dtuple_hash(const dtuple_t *tuple, |
| 302 |
ulint n_fields, ulint n_bytes, |
| 303 |
uint64_t hash_value); |
| 304 |
|
| 305 |
/** Sets types of fields binary in a tuple. |
| 306 |
@param[in] tuple data tuple |
| 307 |
@param[in] n number of fields to set */ |
| 308 |
static inline void dtuple_set_types_binary(dtuple_t *tuple, ulint n); |
| 309 |
|
| 310 |
/** Checks if a dtuple contains an SQL null value. |
| 311 |
@return true if some field is SQL null */ |
| 312 |
[[nodiscard]] static inline bool dtuple_contains_null( |
| 313 |
const dtuple_t *tuple); /*!< in: dtuple */ |
| 314 |
/** Checks that a data field is typed. Asserts an error if not. |
| 315 |
@return true if ok */ |
| 316 |
[[nodiscard]] bool dfield_check_typed( |
| 317 |
const dfield_t *field); /*!< in: data field */ |
| 318 |
/** Checks that a data tuple is typed. Asserts an error if not. |
| 319 |
@return true if ok */ |
| 320 |
[[nodiscard]] bool dtuple_check_typed(const dtuple_t *tuple); /*!< in: tuple */ |
| 321 |
#ifdef UNIV_DEBUG |
| 322 |
/** Validates the consistency of a tuple which must be complete, i.e, |
| 323 |
all fields must have been set. |
| 324 |
@return true if ok */ |
| 325 |
[[nodiscard]] bool dtuple_validate(const dtuple_t *tuple); /*!< in: tuple */ |
| 326 |
#endif /* UNIV_DEBUG */ |
| 327 |
/** Pretty prints a dfield value according to its data type. Also the hex string |
| 328 |
is printed if a string contains non-printable characters. */ |
| 329 |
void dfield_print_also_hex(const dfield_t *dfield); /*!< in: dfield */ |
| 330 |
|
| 331 |
/** The following function prints the contents of a tuple. |
| 332 |
@param[in,out] f Output stream. |
| 333 |
@param[in] tuple Tuple to print. */ |
| 334 |
void dtuple_print(FILE *f, const dtuple_t *tuple); |
| 335 |
|
| 336 |
/** Print the contents of a tuple. |
| 337 |
@param[out] o output stream |
| 338 |
@param[in] field array of data fields |
| 339 |
@param[in] n number of data fields */ |
| 340 |
void dfield_print(std::ostream &o, const dfield_t *field, ulint n); |
| 341 |
|
| 342 |
/** Print the contents of a tuple. |
| 343 |
@param[out] o output stream |
| 344 |
@param[in] tuple data tuple */ |
| 345 |
void dtuple_print(std::ostream &o, const dtuple_t *tuple); |
| 346 |
|
| 347 |
/** Print the contents of a tuple. |
| 348 |
@param[out] o output stream |
| 349 |
@param[in] tuple data tuple */ |
| 350 |
inline std::ostream &operator<<(std::ostream &o, const dtuple_t &tuple) { |
| 351 |
dtuple_print(o, &tuple); |
| 352 |
return (o); |
| 353 |
} |
| 354 |
|
| 355 |
/** Moves parts of long fields in entry to the big record vector so that |
| 356 |
the size of tuple drops below the maximum record size allowed in the |
| 357 |
database. Moves data only from those fields which are not necessary |
| 358 |
to determine uniquely the insertion place of the tuple in the index. |
| 359 |
@param[in,out] index Index that owns the record. |
| 360 |
@param[in,out] upd Update vector. |
| 361 |
@param[in,out] entry Index entry. |
| 362 |
@return own: created big record vector, NULL if we are not able to |
| 363 |
shorten the entry enough, i.e., if there are too many fixed-length or |
| 364 |
short fields in entry or the index is clustered */ |
| 365 |
[[nodiscard]] big_rec_t *dtuple_convert_big_rec(dict_index_t *index, upd_t *upd, |
| 366 |
dtuple_t *entry) |
| 367 |
MY_ATTRIBUTE((malloc)); |
| 368 |
|
| 369 |
/** Puts back to entry the data stored in vector. Note that to ensure the |
| 370 |
fields in entry can accommodate the data, vector must have been created |
| 371 |
from entry with dtuple_convert_big_rec. |
| 372 |
@param[in] entry Entry whose data was put to vector. |
| 373 |
@param[in] vector Big rec vector; it is freed in this function*/ |
| 374 |
void dtuple_convert_back_big_rec(dtuple_t *entry, big_rec_t *vector); |
| 375 |
|
| 376 |
/** Frees the memory in a big rec vector. */ |
| 377 |
static inline void dtuple_big_rec_free( |
| 378 |
big_rec_t *vector); /*!< in, own: big rec vector; it is |
| 379 |
freed in this function */ |
| 380 |
|
| 381 |
/*######################################################################*/ |
| 382 |
|
| 383 |
/** Structure to hold number of multiple values */ |
| 384 |
struct multi_value_data { |
| 385 |
public: |
| 386 |
/** points to different value */ |
| 387 |
const void **datap; |
| 388 |
|
| 389 |
/** each individual value length */ |
| 390 |
uint32_t *data_len; |
| 391 |
|
| 392 |
/** convert buffer if the data is an integer */ |
| 393 |
uint64_t *conv_buf; |
| 394 |
|
| 395 |
/** number of values */ |
| 396 |
uint32_t num_v; |
| 397 |
|
| 398 |
/** number of pointers allocated */ |
| 399 |
uint32_t num_alc; |
| 400 |
|
| 401 |
/** Bitset to indicate which data should be handled for current data |
| 402 |
array. This is mainly used for UPDATE case. UPDATE may not need |
| 403 |
to delete all old values and insert all new values because there could |
| 404 |
be some same values in both old and new data array. |
| 405 |
If current data array is for INSERT and DELETE, this can(should) be |
| 406 |
nullptr since all values in current array should be handled in these |
| 407 |
two cases. */ |
| 408 |
Bitset<> *bitset; |
| 409 |
|
| 410 |
#ifdef UNIV_DEBUG |
| 411 |
/** The size of bitmap buffer in bytes. The bitset might point into |
| 412 |
some prefix of it, and be later made shorter or longer, but not longer |
| 413 |
than this length of underlying buffer. */ |
| 414 |
size_t bitmap_bytes{}; |
| 415 |
#endif |
| 416 |
|
| 417 |
/** Allocate specified number of elements for all arrays and initialize |
| 418 |
the structure accordingly |
| 419 |
@param[in] num number of elements to allocate |
| 420 |
@param[in] bitset true if memory for bitset should be |
| 421 |
allocated too |
| 422 |
@param[in,out] heap memory heap */ |
| 423 |
void alloc(uint32_t num, bool bitset, mem_heap_t *heap); |
| 424 |
|
| 425 |
/** Allocate the bitset for current data array |
| 426 |
@param[in,out] heap memory heap |
| 427 |
@param[in] size size of the bitset, if 0 or default, |
| 428 |
the size would be num_v */ |
| 429 |
void alloc_bitset(mem_heap_t *heap, uint32_t size = 0); |
| 430 |
|
| 431 |
/** Check if two multi_value_data are equal or not, regardless of bitset |
| 432 |
@param[in] multi_value Another multi-value data to be compared |
| 433 |
@return true if two data structures are equal, otherwise false */ |
| 434 |
bool equal(const multi_value_data *multi_value) const { |
| 435 |
if (num_v != multi_value->num_v) { |
| 436 |
return (false); |
| 437 |
} |
| 438 |
|
| 439 |
for (uint32_t i = 0; i < num_v; ++i) { |
| 440 |
if (data_len[i] != multi_value->data_len[i] || |
| 441 |
memcmp(datap[i], multi_value->datap[i], data_len[i]) != 0) { |
| 442 |
return (false); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
return (true); |
| 447 |
} |
| 448 |
|
| 449 |
/** Copy a multi_value_data structure |
| 450 |
@param[in] multi_value multi_value structure to copy from |
| 451 |
@param[in,out] heap memory heap */ |
| 452 |
void copy(const multi_value_data *multi_value, mem_heap_t *heap) { |
| 453 |
if (num_alc < multi_value->num_v) { |
| 454 |
alloc(multi_value->num_v, (multi_value->bitset != nullptr), heap); |
| 455 |
} |
| 456 |
|
| 457 |
copy_low(multi_value, heap); |
| 458 |
} |
| 459 |
|
| 460 |
/** Compare and check if one value from dfield_t is in current data set. |
| 461 |
Any caller calls this function to check if one field from clustered index |
| 462 |
is equal to a record on multi-value index should understand that this |
| 463 |
function can only be used for equality comparison, rather than order |
| 464 |
comparison |
| 465 |
@param[in] type type of the data |
| 466 |
@param[in] data data value to compare |
| 467 |
@param[in] len length of data |
| 468 |
@return true if the value exists in current data set, otherwise false */ |
| 469 |
bool has(const dtype_t *type, const byte *data, uint64_t len) const; |
| 470 |
|
| 471 |
/** Compare and check if one value from dfield_t is in current data set. |
| 472 |
Any caller calls this function to check if one field from clustered index |
| 473 |
is equal to a record on multi-value index should understand that this |
| 474 |
function can only be used for equality comparison, rather than order |
| 475 |
comparison |
| 476 |
@param[in] mtype mtype of data |
| 477 |
@param[in] prtype prtype of data |
| 478 |
@param[in] data data value to compare |
| 479 |
@param[in] len length of data |
| 480 |
@return true if the value exists in current data set, otherwise false */ |
| 481 |
bool has(ulint mtype, ulint prtype, const byte *data, uint64_t len) const; |
| 482 |
|
| 483 |
#ifdef UNIV_DEBUG |
| 484 |
/* Check if there is any duplicate data in this array. |
| 485 |
It is safe to assume all the data has been sorted. |
| 486 |
@return true if duplicate data found, otherwise false */ |
| 487 |
bool duplicate() const { |
| 488 |
/* Since the array is guaranteed to be sorted, so it is fine to |
| 489 |
scan it sequentially and only compare current one with previous one |
| 490 |
if exists. */ |
| 491 |
if (num_v > 1) { |
| 492 |
for (uint32_t i = 1; i < num_v; ++i) { |
| 493 |
if (data_len[i] == data_len[i - 1] && |
| 494 |
memcmp(datap[i], datap[i - 1], data_len[i]) == 0) { |
| 495 |
return (true); |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
return (false); |
| 500 |
} |
| 501 |
#endif /* UNIV_DEBUG */ |
| 502 |
|
| 503 |
private: |
| 504 |
/** Copy a multi_value_data structure, current one should be bigger |
| 505 |
or equal to the one to be copied |
| 506 |
@param[in] multi_value multi_value structure to copy from |
| 507 |
@param[in,out] heap memory heap */ |
| 508 |
void copy_low(const multi_value_data *multi_value, mem_heap_t *heap) { |
| 509 |
ut_ad(num_alc >= multi_value->num_v); |
| 510 |
for (uint32_t i = 0; i < multi_value->num_v; ++i) { |
| 511 |
datap[i] = |
| 512 |
mem_heap_dup(heap, multi_value->datap[i], multi_value->data_len[i]); |
| 513 |
} |
| 514 |
memcpy(data_len, multi_value->data_len, |
| 515 |
sizeof(*data_len) * multi_value->num_v); |
| 516 |
memcpy(conv_buf, multi_value->conv_buf, |
| 517 |
sizeof(*conv_buf) * multi_value->num_v); |
| 518 |
if (multi_value->bitset != nullptr) { |
| 519 |
ut_ad(bitset != nullptr); |
| 520 |
ut_ad(multi_value->bitset->size_bytes() <= bitmap_bytes); |
| 521 |
/* adjust the size to match that of multi_value, but keep pointing to the |
| 522 |
old buffer */ |
| 523 |
*bitset = Bitset<>(bitset->data(), multi_value->bitset->size_bytes()); |
| 524 |
/* copy the actual data into the buffer from multi_value's buffer */ |
| 525 |
bitset->copy_from(multi_value->bitset->data()); |
| 526 |
} |
| 527 |
num_v = multi_value->num_v; |
| 528 |
} |
| 529 |
|
| 530 |
public: |
| 531 |
/** default number of multiple values */ |
| 532 |
static constexpr uint32_t s_default_allocate_num = 24; |
| 533 |
}; |
| 534 |
|
| 535 |
/** Class to log the multi-value data and read it from the log */ |
| 536 |
class Multi_value_logger { |
| 537 |
public: |
| 538 |
/** Constructor |
| 539 |
@param[in] mv_data multi-value data structure to log |
| 540 |
@param[in] field_len multi-value data field length */ |
| 541 |
Multi_value_logger(const multi_value_data *mv_data, uint32_t field_len) |
| 542 |
: m_mv_data(mv_data), m_field_len(field_len) {} |
| 543 |
|
| 544 |
/** Get the log length for the multi-value data |
| 545 |
@param[in] precise true if precise length is needed, false if rough |
| 546 |
estimation is OK |
| 547 |
@return the total log length for the multi-value data */ |
| 548 |
uint32_t get_log_len(bool precise) const; |
| 549 |
|
| 550 |
/** Log the multi-value data to specified memory |
| 551 |
@param[in,out] ptr the memory to write |
| 552 |
@return next to the end of the multi-value data log */ |
| 553 |
byte *log(byte **ptr); |
| 554 |
|
| 555 |
/** Read the log length for the multi-value data log starting from ptr |
| 556 |
@param[in] ptr log starting from here |
| 557 |
@return the length of this log */ |
| 558 |
static uint32_t read_log_len(const byte *ptr); |
| 559 |
|
| 560 |
/** Read the multi-value data from the ptr |
| 561 |
@param[in] ptr log starting from here |
| 562 |
@param[in,out] field multi-value data field to store the array |
| 563 |
@param[in,out] heap memory heap |
| 564 |
@return next to the end of the multi-value data log */ |
| 565 |
static const byte *read(const byte *ptr, dfield_t *field, mem_heap_t *heap); |
| 566 |
|
| 567 |
/** Estimate how many multi-value keys at most can be accommodated into the |
| 568 |
log of specified size. |
| 569 |
@param[in] log_size max log size |
| 570 |
@param[in] key_length max multi-value key length, charset considered |
| 571 |
@param[out] num_keys max possible number of multi-value keys |
| 572 |
@return the total size of the keys, let's assume all keys are concatenated |
| 573 |
one by one compactly */ |
| 574 |
static uint32_t get_keys_capacity(uint32_t log_size, uint32_t key_length, |
| 575 |
uint32_t *num_keys); |
| 576 |
|
| 577 |
/** Determine if the log starting from ptr is for multi-value data |
| 578 |
@return true if it is for multi-value data, otherwise false */ |
| 579 |
static bool is_multi_value_log(const byte *ptr) { |
| 580 |
return (*ptr == s_multi_value_virtual_col_length_marker); |
| 581 |
} |
| 582 |
|
| 583 |
private: |
| 584 |
/** Multi-value data */ |
| 585 |
const multi_value_data *m_mv_data; |
| 586 |
|
| 587 |
/** Multi-value field length */ |
| 588 |
uint32_t m_field_len; |
| 589 |
|
| 590 |
/** Length of log for NULL value or no indexed value cases */ |
| 591 |
static constexpr uint32_t s_log_length_for_null_or_empty = 2; |
| 592 |
|
| 593 |
/** Multi-value virtual column length marker. With this length marker, |
| 594 |
a multi-value virtual column undo log can be identified. Meanwhile, this |
| 595 |
marker should/will not conflict with any normal compressed written length |
| 596 |
leading byte */ |
| 597 |
static constexpr uint8_t s_multi_value_virtual_col_length_marker = 0xFF; |
| 598 |
|
| 599 |
/** Multi-value virtual column length, which indicates that there is |
| 600 |
no value on the multi-value index. It's mapped to UNIV_NO_INDEX_VALUE */ |
| 601 |
static constexpr uint16_t s_multi_value_no_index_value = 0x0; |
| 602 |
|
| 603 |
/** Multi-value virtual column length, which indicates that the field |
| 604 |
is NULL. It's mapped to UNIV_SQL_NULL. Since any not NULL and not no value |
| 605 |
multi-value data must be longer than 1 byte, so this is safe for this |
| 606 |
special meaning */ |
| 607 |
static constexpr uint16_t s_multi_value_null = 0x1; |
| 608 |
|
| 609 |
/** The compressed length for multi-value key length logging. |
| 610 |
This would not be longer than 2 bytes for now, while 2 bytes can |
| 611 |
actually support key length of 16384 bytes. And the actual key |
| 612 |
length would never be longer than this */ |
| 613 |
static constexpr uint8_t s_max_compressed_mv_key_length_size = 2; |
| 614 |
}; |
| 615 |
|
| 616 |
/** Structure for an SQL data field */ |
| 617 |
struct dfield_t { |
| 618 |
void *data; /*!< pointer to data */ |
| 619 |
bool ext; /*!< true=externally stored, false=local */ |
| 620 |
unsigned spatial_status : 2; |
| 621 |
/*!< spatial status of externally stored field |
| 622 |
in undo log for purge */ |
| 623 |
unsigned len; /*!< data length; UNIV_SQL_NULL if SQL null */ |
| 624 |
dtype_t type; /*!< type of data */ |
| 625 |
|
| 626 |
bool is_virtual() const { return (type.is_virtual()); } |
| 627 |
|
| 628 |
void reset() { |
| 629 |
data = nullptr; |
| 630 |
ext = false; |
| 631 |
spatial_status = SPATIAL_UNKNOWN, len = 0; |
| 632 |
} |
| 633 |
|
| 634 |
/** Create a deep copy of this object |
| 635 |
@param[in] heap the memory heap in which the clone will be |
| 636 |
created. |
| 637 |
@return the cloned object. */ |
| 638 |
dfield_t *clone(mem_heap_t *heap); |
| 639 |
|
| 640 |
byte *blobref() const; |
| 641 |
|
| 642 |
/** Obtain the LOB version number, if this is an externally |
| 643 |
stored field. */ |
| 644 |
uint32_t lob_version() const; |
| 645 |
|
| 646 |
dfield_t() |
| 647 |
: data(nullptr), ext(0), spatial_status(0), len(0), type({0, 0, 0, 0}) {} |
| 648 |
|
| 649 |
/** Print the dfield_t object into the given output stream. |
| 650 |
@param[in] out the output stream. |
| 651 |
@return the output stream. */ |
| 652 |
std::ostream &print(std::ostream &out) const; |
| 653 |
|
| 654 |
/** Adjust and(or) set virtual column value which is read from undo |
| 655 |
or online DDL log |
| 656 |
@param[in] vcol virtual column definition |
| 657 |
@param[in] comp true if compact format |
| 658 |
@param[in] field virtual column value |
| 659 |
@param[in] len value length |
| 660 |
@param[in,out] heap memory heap to keep value when necessary */ |
| 661 |
void adjust_v_data_mysql(const dict_v_col_t *vcol, bool comp, |
| 662 |
const byte *field, ulint len, mem_heap_t *heap); |
| 663 |
}; |
| 664 |
|
| 665 |
/** Compare a multi-value clustered index field with a secondary index |
| 666 |
field, to see if they are equal. If the clustered index field is the |
| 667 |
array, then equal means it contains the secondary index field |
| 668 |
@param[in] clust_field clustered index field |
| 669 |
@param[in] clust_len clustered index field length |
| 670 |
@param[in] sec_field secondary index field |
| 671 |
@param[in] sec_len secondary index field length |
| 672 |
@param[in] col the column tied to this field |
| 673 |
@return true if they are equal, otherwise false */ |
| 674 |
bool is_multi_value_clust_and_sec_equal(const byte *clust_field, |
| 675 |
uint64_t clust_len, |
| 676 |
const byte *sec_field, uint64_t sec_len, |
| 677 |
const dict_col_t *col); |
| 678 |
|
| 679 |
/** Overloading the global output operator to easily print the given dfield_t |
| 680 |
object into the given output stream. |
| 681 |
@param[in] out the output stream |
| 682 |
@param[in] obj the given object to print. |
| 683 |
@return the output stream. */ |
| 684 |
inline std::ostream &operator<<(std::ostream &out, const dfield_t &obj) { |
| 685 |
return (obj.print(out)); |
| 686 |
} |
| 687 |
|
| 688 |
#ifdef UNIV_DEBUG |
| 689 |
/** Value of dtuple_t::magic_n */ |
| 690 |
constexpr uint32_t DATA_TUPLE_MAGIC_N = 65478679; |
| 691 |
#endif |
| 692 |
|
| 693 |
/** Structure for an SQL data tuple of fields (logical record) */ |
| 694 |
struct dtuple_t { |
| 695 |
/** info bits of an index record: the default is 0; this field is used if an |
| 696 |
index record is built from a data tuple */ |
| 697 |
uint16_t info_bits; |
| 698 |
|
| 699 |
/** Number of fields in dtuple */ |
| 700 |
uint16_t n_fields; |
| 701 |
|
| 702 |
/** number of fields which should be used in comparison services of rem0cmp.*; |
| 703 |
the index search is performed by comparing only these fields, others are |
| 704 |
ignored; the default value in dtuple creation is the same value as n_fields */ |
| 705 |
uint16_t n_fields_cmp; |
| 706 |
|
| 707 |
/** Fields. */ |
| 708 |
dfield_t *fields; |
| 709 |
|
| 710 |
/** Number of virtual fields. */ |
| 711 |
uint16_t n_v_fields; |
| 712 |
|
| 713 |
/** Fields on virtual column */ |
| 714 |
dfield_t *v_fields; |
| 715 |
|
| 716 |
/** Data tuples can be linked into a list using this field */ |
| 717 |
UT_LIST_NODE_T(dtuple_t) tuple_list; |
| 718 |
|
| 719 |
#ifdef UNIV_DEBUG |
| 720 |
/** Memory heap where this tuple is allocated. */ |
| 721 |
mem_heap_t *m_heap{}; |
| 722 |
|
| 723 |
/** Value of dtuple_t::magic_n */ |
| 724 |
static constexpr size_t MAGIC_N = 614679; |
| 725 |
|
| 726 |
/** Magic number, used in debug assertions */ |
| 727 |
size_t magic_n{MAGIC_N}; |
| 728 |
#endif /* UNIV_DEBUG */ |
| 729 |
|
| 730 |
/** Print the tuple to the output stream. |
| 731 |
@param[in,out] out Stream to output to. |
| 732 |
@return stream */ |
| 733 |
std::ostream &print(std::ostream &out) const { |
| 734 |
dtuple_print(out, this); |
| 735 |
return out; |
| 736 |
} |
| 737 |
|
| 738 |
/** Read the trx id from the tuple (DB_TRX_ID) |
| 739 |
@return transaction id of the tuple. */ |
| 740 |
trx_id_t get_trx_id() const; |
| 741 |
|
| 742 |
/** Ignore at most n trailing default fields if this is a tuple |
| 743 |
from instant index |
| 744 |
@param[in] index clustered index object for this tuple */ |
| 745 |
void ignore_trailing_default(const dict_index_t *index); |
| 746 |
|
| 747 |
/** Compare a data tuple to a physical record. |
| 748 |
@param[in] rec record |
| 749 |
@param[in] index index |
| 750 |
@param[in] offsets rec_get_offsets(rec) |
| 751 |
@param[in,out] matched_fields number of completely matched fields |
| 752 |
@return the comparison result of dtuple and rec |
| 753 |
@retval 0 if dtuple is equal to rec |
| 754 |
@retval negative if dtuple is less than rec |
| 755 |
@retval positive if dtuple is greater than rec */ |
| 756 |
int compare(const rec_t *rec, const dict_index_t *index, const ulint *offsets, |
| 757 |
ulint *matched_fields) const; |
| 758 |
|
| 759 |
/** Compare a data tuple to a physical record. |
| 760 |
@param[in] rec record |
| 761 |
@param[in] index index |
| 762 |
@param[in] offsets rec_get_offsets(rec) |
| 763 |
@return the comparison result of dtuple and rec |
| 764 |
@retval 0 if dtuple is equal to rec |
| 765 |
@retval negative if dtuple is less than rec |
| 766 |
@retval positive if dtuple is greater than rec */ |
| 767 |
inline int compare(const rec_t *rec, const dict_index_t *index, |
| 768 |
const ulint *offsets) const { |
| 769 |
ulint matched_fields{}; |
| 770 |
|
| 771 |
return (compare(rec, index, offsets, &matched_fields)); |
| 772 |
} |
| 773 |
|
| 774 |
/** Get number of externally stored fields. |
| 775 |
@retval number of externally stored fields. */ |
| 776 |
inline size_t get_n_ext() const { |
| 777 |
size_t n_ext = 0; |
| 778 |
for (uint32_t i = 0; i < n_fields; ++i) { |
| 779 |
if (dfield_is_ext(&fields[i])) { |
| 780 |
++n_ext; |
| 781 |
} |
| 782 |
} |
| 783 |
return n_ext; |
| 784 |
} |
| 785 |
|
| 786 |
/** Does tuple has externally stored fields. |
| 787 |
@retval true if there is externally stored fields. */ |
| 788 |
inline bool has_ext() const { |
| 789 |
for (uint32_t i = 0; i < n_fields; ++i) { |
| 790 |
if (dfield_is_ext(&fields[i])) { |
| 791 |
return true; |
| 792 |
} |
| 793 |
} |
| 794 |
return false; |
| 795 |
} |
| 796 |
|
| 797 |
#ifdef UNIV_DEBUG |
| 798 |
void validate() const; |
| 799 |
void validate_for_index(const dict_index_t *index) const; |
| 800 |
#endif /* UNIV_DEBUG */ |
| 801 |
}; |
| 802 |
|
| 803 |
/** A slot for a field in a big rec vector */ |
| 804 |
struct big_rec_field_t { |
| 805 |
/** Constructor. |
| 806 |
@param[in] field_no_ the field number |
| 807 |
@param[in] len_ the data length |
| 808 |
@param[in] data_ the data */ |
| 809 |
big_rec_field_t(ulint field_no_, ulint len_, void *data_) |
| 810 |
: field_no(field_no_), |
| 811 |
len(len_), |
| 812 |
data(data_), |
| 813 |
ext_in_old(false), |
| 814 |
ext_in_new(false) {} |
| 815 |
|
| 816 |
byte *ptr() const { return (static_cast<byte *>(data)); } |
| 817 |
|
| 818 |
ulint field_no; /*!< field number in record */ |
| 819 |
ulint len; /*!< stored data length, in bytes */ |
| 820 |
void *data; /*!< stored data */ |
| 821 |
|
| 822 |
/** If true, this field was stored externally in the old row. |
| 823 |
If false, this field was stored inline in the old row.*/ |
| 824 |
bool ext_in_old; |
| 825 |
|
| 826 |
/** If true, this field is stored externally in the new row. |
| 827 |
If false, this field is stored inline in the new row.*/ |
| 828 |
bool ext_in_new; |
| 829 |
|
| 830 |
/** Print the big_rec_field_t object into the given output stream. |
| 831 |
@param[in] out the output stream. |
| 832 |
@return the output stream. */ |
| 833 |
std::ostream &print(std::ostream &out) const; |
| 834 |
}; |
| 835 |
|
| 836 |
/** Overloading the global output operator to easily print the given |
| 837 |
big_rec_field_t object into the given output stream. |
| 838 |
@param[in] out the output stream |
| 839 |
@param[in] obj the given object to print. |
| 840 |
@return the output stream. */ |
| 841 |
inline std::ostream &operator<<(std::ostream &out, const big_rec_field_t &obj) { |
| 842 |
return (obj.print(out)); |
| 843 |
} |
| 844 |
|
| 845 |
/** Storage format for overflow data in a big record, that is, a |
| 846 |
clustered index record which needs external storage of data fields */ |
| 847 |
struct big_rec_t { |
| 848 |
mem_heap_t *heap; /*!< memory heap from which |
| 849 |
allocated */ |
| 850 |
const ulint capacity; /*!< fields array size */ |
| 851 |
ulint n_fields; /*!< number of stored fields */ |
| 852 |
big_rec_field_t *fields; /*!< stored fields */ |
| 853 |
|
| 854 |
/** Constructor. |
| 855 |
@param[in] max the capacity of the array of fields. */ |
| 856 |
explicit big_rec_t(const ulint max) |
| 857 |
: heap(nullptr), capacity(max), n_fields(0), fields(nullptr) {} |
| 858 |
|
| 859 |
/** Append one big_rec_field_t object to the end of array of fields */ |
| 860 |
void append(const big_rec_field_t &field) { |
| 861 |
ut_ad(n_fields < capacity); |
| 862 |
fields[n_fields] = field; |
| 863 |
n_fields++; |
| 864 |
} |
| 865 |
|
| 866 |
/** Allocate a big_rec_t object in the given memory heap, and for |
| 867 |
storing n_fld number of fields. |
| 868 |
@param[in] heap memory heap in which this object is allocated |
| 869 |
@param[in] n_fld maximum number of fields that can be stored in |
| 870 |
this object |
| 871 |
@return the allocated object */ |
| 872 |
static big_rec_t *alloc(mem_heap_t *heap, ulint n_fld); |
| 873 |
|
| 874 |
/** Print the current object into the given output stream. |
| 875 |
@param[in] out the output stream. |
| 876 |
@return the output stream. */ |
| 877 |
std::ostream &print(std::ostream &out) const; |
| 878 |
}; |
| 879 |
|
| 880 |
/** Overloading the global output operator to easily print the given |
| 881 |
big_rec_t object into the given output stream. |
| 882 |
@param[in] out the output stream |
| 883 |
@param[in] obj the given object to print. |
| 884 |
@return the output stream. */ |
| 885 |
inline std::ostream &operator<<(std::ostream &out, const big_rec_t &obj) { |
| 886 |
return (obj.print(out)); |
| 887 |
} |
| 888 |
|
| 889 |
#include "data0data.ic" |
| 890 |
|
| 891 |
#endif |