| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2008, 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 api/api0api.cc |
| 29 |
InnoDB Native API |
| 30 |
|
| 31 |
2008-08-01 Created Sunny Bains |
| 32 |
3/20/2011 Jimmy Yang extracted from Embedded InnoDB |
| 33 |
*******************************************************/ |
| 34 |
|
| 35 |
#include <dd/properties.h> |
| 36 |
#include <dd/types/tablespace.h> |
| 37 |
#include <errno.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <sys/types.h> |
| 40 |
|
| 41 |
#include "api0api.h" |
| 42 |
#include "api0misc.h" |
| 43 |
#include "btr0pcur.h" |
| 44 |
#include "dict0crea.h" |
| 45 |
#include "dict0dd.h" |
| 46 |
#include "dict0dict.h" |
| 47 |
#include "dict0priv.h" |
| 48 |
#include "fsp0fsp.h" |
| 49 |
#include "ha_prototypes.h" |
| 50 |
#include "lob0lob.h" |
| 51 |
#include "lock0lock.h" |
| 52 |
#include "lock0types.h" |
| 53 |
|
| 54 |
#include "ddl0ddl.h" |
| 55 |
#include "dict0sdi-decompress.h" |
| 56 |
#include "dict0sdi.h" |
| 57 |
#include "pars0pars.h" |
| 58 |
#include "rem0cmp.h" |
| 59 |
#include "row0ins.h" |
| 60 |
#include "row0sel.h" |
| 61 |
#include "row0upd.h" |
| 62 |
#include "row0vers.h" |
| 63 |
#include "srv0start.h" |
| 64 |
#include "trx0roll.h" |
| 65 |
|
| 66 |
/** configure variable for binlog option with InnoDB APIs */ |
| 67 |
bool ib_binlog_enabled = false; |
| 68 |
|
| 69 |
/** configure variable for MDL option with InnoDB APIs */ |
| 70 |
bool ib_mdl_enabled = false; |
| 71 |
|
| 72 |
/** configure variable for disable rowlock with InnoDB APIs */ |
| 73 |
bool ib_disable_row_lock = false; |
| 74 |
|
| 75 |
/** configure variable for Transaction isolation levels */ |
| 76 |
ulong ib_trx_level_setting = IB_TRX_READ_UNCOMMITTED; |
| 77 |
|
| 78 |
/** configure variable for background commit interval in seconds */ |
| 79 |
ulong ib_bk_commit_interval = 0; |
| 80 |
|
| 81 |
/** InnoDB tuple types. */ |
| 82 |
enum ib_tuple_type_t { |
| 83 |
TPL_TYPE_ROW, /*!< Data row tuple */ |
| 84 |
TPL_TYPE_KEY /*!< Index key tuple */ |
| 85 |
}; |
| 86 |
|
| 87 |
/** Query types supported. */ |
| 88 |
enum ib_qry_type_t { |
| 89 |
QRY_NON, /*!< None/Sentinel */ |
| 90 |
QRY_INS, /*!< Insert operation */ |
| 91 |
QRY_UPD, /*!< Update operation */ |
| 92 |
QRY_SEL /*!< Select operation */ |
| 93 |
}; |
| 94 |
|
| 95 |
/** Query graph types. */ |
| 96 |
struct ib_qry_grph_t { |
| 97 |
que_fork_t *ins; /*!< Innobase SQL query graph used |
| 98 |
in inserts */ |
| 99 |
que_fork_t *upd; /*!< Innobase SQL query graph used |
| 100 |
in updates or deletes */ |
| 101 |
que_fork_t *sel; /*!< dummy query graph used in |
| 102 |
selects */ |
| 103 |
}; |
| 104 |
|
| 105 |
/** Query node types. */ |
| 106 |
struct ib_qry_node_t { |
| 107 |
ins_node_t *ins; /*!< Innobase SQL insert node |
| 108 |
used to perform inserts to the table */ |
| 109 |
upd_node_t *upd; /*!< Innobase SQL update node |
| 110 |
used to perform updates and deletes */ |
| 111 |
sel_node_t *sel; /*!< Innobase SQL select node |
| 112 |
used to perform selects on the table */ |
| 113 |
}; |
| 114 |
|
| 115 |
/** Query processing fields. */ |
| 116 |
struct ib_qry_proc_t { |
| 117 |
ib_qry_node_t node; /*!< Query node*/ |
| 118 |
|
| 119 |
ib_qry_grph_t grph; /*!< Query graph */ |
| 120 |
}; |
| 121 |
|
| 122 |
/** Cursor instance for traversing tables/indexes. This will eventually |
| 123 |
become row_prebuilt_t. */ |
| 124 |
struct ib_cursor_t { |
| 125 |
mem_heap_t *heap; /*!< Instance heap */ |
| 126 |
|
| 127 |
mem_heap_t *query_heap; /*!< Heap to use for query graphs */ |
| 128 |
|
| 129 |
ib_qry_proc_t q_proc; /*!< Query processing info */ |
| 130 |
|
| 131 |
ib_match_mode_t match_mode; /*!< ib_cursor_moveto match mode */ |
| 132 |
|
| 133 |
MDL_ticket *mdl; /*!< meta-data lock on the table */ |
| 134 |
|
| 135 |
row_prebuilt_t *prebuilt; /*!< For reading rows */ |
| 136 |
|
| 137 |
bool valid_trx; /*!< Valid transaction attached */ |
| 138 |
}; |
| 139 |
|
| 140 |
/** InnoDB table columns used during table and index schema creation. */ |
| 141 |
struct ib_col_t { |
| 142 |
const char *name; /*!< Name of column */ |
| 143 |
|
| 144 |
ib_col_type_t ib_col_type; /*!< Main type of the column */ |
| 145 |
|
| 146 |
ulint len; /*!< Length of the column */ |
| 147 |
|
| 148 |
ib_col_attr_t ib_col_attr; /*!< Column attributes */ |
| 149 |
}; |
| 150 |
|
| 151 |
/** InnoDB index columns used during index and index schema creation. */ |
| 152 |
struct ib_key_col_t { |
| 153 |
const char *name; /*!< Name of column */ |
| 154 |
|
| 155 |
ulint prefix_len; /*!< Column index prefix len or 0 */ |
| 156 |
}; |
| 157 |
|
| 158 |
struct ib_table_def_t; |
| 159 |
|
| 160 |
/** InnoDB index schema used during index creation */ |
| 161 |
struct ib_Index_defn { |
| 162 |
mem_heap_t *heap; /*!< Heap used to build this and all |
| 163 |
its columns in the list */ |
| 164 |
|
| 165 |
const char *name; /*!< Index name */ |
| 166 |
|
| 167 |
dict_table_t *table; /*!< Parent InnoDB table */ |
| 168 |
|
| 169 |
ib_table_def_t *schema; /*!< Parent table schema that owns |
| 170 |
this instance */ |
| 171 |
|
| 172 |
bool clustered; /*!< True if clustered index */ |
| 173 |
|
| 174 |
bool unique; /*!< True if unique index */ |
| 175 |
|
| 176 |
ib_vector_t *cols; /*!< Vector of columns */ |
| 177 |
|
| 178 |
trx_t *usr_trx; /*!< User transacton covering the |
| 179 |
DDL operations */ |
| 180 |
}; |
| 181 |
|
| 182 |
/** InnoDB table schema used during table creation */ |
| 183 |
struct ib_table_def_t { |
| 184 |
mem_heap_t *heap; /*!< Heap used to build this and all |
| 185 |
its columns in the list */ |
| 186 |
const char *name; /*!< Table name */ |
| 187 |
|
| 188 |
ib_tbl_fmt_t ib_tbl_fmt; /*!< Row format */ |
| 189 |
|
| 190 |
ulint page_size; /*!< Page size */ |
| 191 |
|
| 192 |
ib_vector_t *cols; /*!< Vector of columns */ |
| 193 |
|
| 194 |
ib_vector_t *indexes; /*!< Vector of indexes */ |
| 195 |
|
| 196 |
dict_table_t *table; /* Table read from or NULL */ |
| 197 |
}; |
| 198 |
|
| 199 |
/** InnoDB tuple used for key operations. */ |
| 200 |
struct ib_tuple_t { |
| 201 |
mem_heap_t *heap; /*!< Heap used to build |
| 202 |
this and for copying |
| 203 |
the column values. */ |
| 204 |
|
| 205 |
ib_tuple_type_t type; /*!< Tuple discriminitor. */ |
| 206 |
|
| 207 |
const dict_index_t *index; /*!< Index for tuple can be either |
| 208 |
secondary or cluster index. */ |
| 209 |
|
| 210 |
dtuple_t *ptr; /*!< The internal tuple |
| 211 |
instance */ |
| 212 |
}; |
| 213 |
|
| 214 |
/** The following counter is used to convey information to InnoDB |
| 215 |
about server activity: in case of normal DML ops it is not |
| 216 |
sensible to call srv_active_wake_master_thread after each |
| 217 |
operation, we only do it every INNOBASE_WAKE_INTERVAL'th step. */ |
| 218 |
constexpr uint32_t INNOBASE_WAKE_INTERVAL = 32; |
| 219 |
|
| 220 |
/** Check whether the InnoDB persistent cursor is positioned. |
| 221 |
@return IB_true if positioned */ |
| 222 |
static inline bool ib_btr_cursor_is_positioned( |
| 223 |
btr_pcur_t *pcur) /*!< in: InnoDB persistent cursor */ |
| 224 |
{ |
| 225 |
return (pcur->is_positioned()); |
| 226 |
} |
| 227 |
|
| 228 |
/** Find table using table name. |
| 229 |
@return table instance if found */ |
| 230 |
static dict_table_t *ib_lookup_table_by_name( |
| 231 |
const char *name) /*!< in: table name to lookup */ |
| 232 |
{ |
| 233 |
dict_table_t *table; |
| 234 |
|
| 235 |
table = dict_table_get_low(name); |
| 236 |
|
| 237 |
if (table != nullptr && table->ibd_file_missing) { |
| 238 |
table = nullptr; |
| 239 |
} |
| 240 |
|
| 241 |
return (table); |
| 242 |
} |
| 243 |
|
| 244 |
/** Increments innobase_active_counter and every INNOBASE_WAKE_INTERVALth |
| 245 |
time calls srv_active_wake_master_thread. This function should be used |
| 246 |
when a single database operation may introduce a small need for |
| 247 |
server utility activity, like checkpointing. */ |
| 248 |
static inline void ib_wake_master_thread(void) { |
| 249 |
static ulint ib_signal_counter = 0; |
| 250 |
|
| 251 |
++ib_signal_counter; |
| 252 |
|
| 253 |
if ((ib_signal_counter % INNOBASE_WAKE_INTERVAL) == 0) { |
| 254 |
srv_active_wake_master_thread(); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
/** Read the columns from a rec into a tuple. */ |
| 259 |
static ib_err_t ib_read_tuple( |
| 260 |
const rec_t *rec, /*!< in: Record to read */ |
| 261 |
bool page_format, /*!< in: true if compressed format */ |
| 262 |
ib_tuple_t *tuple, /*!< in: tuple to read into */ |
| 263 |
ib_tuple_t *cmp_tuple, /*!< in: tuple to compare and stop |
| 264 |
reading */ |
| 265 |
int mode, /*!< in: mode determine when to |
| 266 |
stop read */ |
| 267 |
void **rec_buf_list, /*!< in/out: row buffer */ |
| 268 |
ulint *cur_slot, /*!< in/out: buffer slot being used */ |
| 269 |
ulint *used_len) /*!< in/out: used buf len */ |
| 270 |
{ |
| 271 |
ulint i; |
| 272 |
void *ptr; |
| 273 |
rec_t *copy; |
| 274 |
ulint rec_meta_data; |
| 275 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 276 |
ulint *offsets = offsets_; |
| 277 |
dtuple_t *dtuple = tuple->ptr; |
| 278 |
const dict_index_t *index = tuple->index; |
| 279 |
ulint offset_size; |
| 280 |
byte *next_ptr; |
| 281 |
int cmp; |
| 282 |
ulint match = 0; |
| 283 |
|
| 284 |
rec_offs_init(offsets_); |
| 285 |
|
| 286 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 287 |
UT_LOCATION_HERE, &tuple->heap); |
| 288 |
|
| 289 |
rec_meta_data = rec_get_info_bits(rec, page_format); |
| 290 |
dtuple_set_info_bits(dtuple, rec_meta_data); |
| 291 |
|
| 292 |
offset_size = rec_offs_size(offsets); |
| 293 |
|
| 294 |
if (cmp_tuple && mode) { |
| 295 |
/* This is a case of "read up to" certain value. Used for |
| 296 |
index scan for "<" or "<=" case */ |
| 297 |
cmp = cmp_tuple->ptr->compare(rec, index, offsets, &match); |
| 298 |
|
| 299 |
if ((mode == IB_CUR_LE && cmp < 0) || (mode == IB_CUR_L && cmp <= 0)) { |
| 300 |
return (DB_END_OF_INDEX); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
if (rec_buf_list && *rec_buf_list) { |
| 305 |
void *rec_buf = rec_buf_list[*cur_slot]; |
| 306 |
|
| 307 |
if ((REC_BUF_SLOT_SIZE - *used_len) < offset_size + 8) { |
| 308 |
(*cur_slot) += 1; |
| 309 |
|
| 310 |
/* Limit the record buffer size to 16 MB */ |
| 311 |
if (*cur_slot >= 1024) { |
| 312 |
return (DB_END_OF_INDEX); |
| 313 |
} |
| 314 |
|
| 315 |
if (rec_buf_list[*cur_slot] == nullptr) { |
| 316 |
rec_buf_list[*cur_slot] = malloc(REC_BUF_SLOT_SIZE); |
| 317 |
} |
| 318 |
|
| 319 |
rec_buf = rec_buf_list[*cur_slot]; |
| 320 |
|
| 321 |
if (rec_buf == nullptr) { |
| 322 |
return (DB_END_OF_INDEX); |
| 323 |
} |
| 324 |
*used_len = 0; |
| 325 |
} |
| 326 |
|
| 327 |
ptr = ((byte *)rec_buf + *used_len); |
| 328 |
|
| 329 |
next_ptr = static_cast<byte *>( |
| 330 |
ut_align((byte *)(rec_buf) + *used_len + offset_size + 8, 8)); |
| 331 |
|
| 332 |
*used_len = next_ptr - (byte *)(rec_buf); |
| 333 |
} else { |
| 334 |
/* Make a copy of the rec. */ |
| 335 |
ptr = mem_heap_alloc(tuple->heap, offset_size); |
| 336 |
} |
| 337 |
|
| 338 |
copy = rec_copy(ptr, rec, offsets); |
| 339 |
|
| 340 |
/* DB_ROW_ID is added in dict_table_t unconditionally but is used only if PK |
| 341 |
isn't there. dtuple is made from the dict_table_t so it will also have it but |
| 342 |
record might not if PK is there. */ |
| 343 |
size_t n_index_fields = |
| 344 |
std::min(rec_offs_n_fields(offsets), dtuple_get_n_fields(dtuple)); |
| 345 |
|
| 346 |
/* If table has INSTANT DROP columns, then offsets would have count for these |
| 347 |
columns as well, where as logical record won't have them. */ |
| 348 |
if (index->has_row_versions()) { |
| 349 |
n_index_fields = std::min(n_index_fields, (size_t)index->n_fields); |
| 350 |
} |
| 351 |
|
| 352 |
for (i = 0; i < n_index_fields; ++i) { |
| 353 |
ulint len; |
| 354 |
const byte *data; |
| 355 |
dfield_t *dfield; |
| 356 |
|
| 357 |
if (tuple->type == TPL_TYPE_ROW) { |
| 358 |
const dict_col_t *col; |
| 359 |
ulint col_no; |
| 360 |
const dict_field_t *index_field; |
| 361 |
|
| 362 |
index_field = index->get_field(i); |
| 363 |
col = index_field->col; |
| 364 |
col_no = dict_col_get_no(col); |
| 365 |
|
| 366 |
dfield = dtuple_get_nth_field(dtuple, col_no); |
| 367 |
} else { |
| 368 |
dfield = dtuple_get_nth_field(dtuple, i); |
| 369 |
} |
| 370 |
|
| 371 |
data = rec_get_nth_field_instant(copy, offsets, i, index, &len); |
| 372 |
|
| 373 |
/* Fetch and copy any externally stored column. */ |
| 374 |
if (rec_offs_nth_extern(index, offsets, i)) { |
| 375 |
const page_size_t page_size(dict_table_page_size(index->table)); |
| 376 |
|
| 377 |
/* Passing nullptr to the transaction object. This |
| 378 |
means that partial update of LOB is not supported |
| 379 |
via this interface.*/ |
| 380 |
data = lob::btr_rec_copy_externally_stored_field( |
| 381 |
nullptr, index, copy, offsets, page_size, i, &len, nullptr, |
| 382 |
dict_index_is_sdi(index), tuple->heap); |
| 383 |
|
| 384 |
ut_a(len != UNIV_SQL_NULL); |
| 385 |
} |
| 386 |
|
| 387 |
dfield_set_data(dfield, data, len); |
| 388 |
} |
| 389 |
|
| 390 |
return (DB_SUCCESS); |
| 391 |
} |
| 392 |
|
| 393 |
/** Create an InnoDB key tuple. |
| 394 |
@return tuple instance created, or NULL */ |
| 395 |
static ib_tpl_t ib_key_tuple_new_low( |
| 396 |
const dict_index_t *index, /*!< in: index for which tuple |
| 397 |
required */ |
| 398 |
ulint n_cols, /*!< in: no. of user defined cols */ |
| 399 |
mem_heap_t *heap) /*!< in: memory heap */ |
| 400 |
{ |
| 401 |
ib_tuple_t *tuple; |
| 402 |
ulint i; |
| 403 |
ulint n_cmp_cols; |
| 404 |
|
| 405 |
tuple = static_cast<ib_tuple_t *>(mem_heap_alloc(heap, sizeof(*tuple))); |
| 406 |
|
| 407 |
if (tuple == nullptr) { |
| 408 |
mem_heap_free(heap); |
| 409 |
return (nullptr); |
| 410 |
} |
| 411 |
|
| 412 |
tuple->heap = heap; |
| 413 |
tuple->index = index; |
| 414 |
tuple->type = TPL_TYPE_KEY; |
| 415 |
|
| 416 |
/* Is it a generated clustered index ? */ |
| 417 |
if (n_cols == 0) { |
| 418 |
++n_cols; |
| 419 |
} |
| 420 |
|
| 421 |
tuple->ptr = dtuple_create(heap, n_cols); |
| 422 |
|
| 423 |
/* Copy types and set to SQL_NULL. */ |
| 424 |
dict_index_copy_types(tuple->ptr, index, n_cols); |
| 425 |
|
| 426 |
for (i = 0; i < n_cols; i++) { |
| 427 |
dfield_t *dfield; |
| 428 |
|
| 429 |
dfield = dtuple_get_nth_field(tuple->ptr, i); |
| 430 |
dfield_set_null(dfield); |
| 431 |
} |
| 432 |
|
| 433 |
n_cmp_cols = dict_index_get_n_ordering_defined_by_user(index); |
| 434 |
|
| 435 |
dtuple_set_n_fields_cmp(tuple->ptr, n_cmp_cols); |
| 436 |
|
| 437 |
return ((ib_tpl_t)tuple); |
| 438 |
} |
| 439 |
|
| 440 |
/** Create an InnoDB key tuple. |
| 441 |
@return tuple instance created, or NULL */ |
| 442 |
static ib_tpl_t ib_key_tuple_new( |
| 443 |
const dict_index_t *index, /*!< in: index of tuple */ |
| 444 |
ulint n_cols) /*!< in: no. of user defined cols */ |
| 445 |
{ |
| 446 |
mem_heap_t *heap; |
| 447 |
|
| 448 |
heap = mem_heap_create(64, UT_LOCATION_HERE); |
| 449 |
|
| 450 |
if (heap == nullptr) { |
| 451 |
return (nullptr); |
| 452 |
} |
| 453 |
|
| 454 |
return (ib_key_tuple_new_low(index, n_cols, heap)); |
| 455 |
} |
| 456 |
|
| 457 |
/** Create an InnoDB row tuple. |
| 458 |
@return tuple instance, or NULL */ |
| 459 |
static ib_tpl_t ib_row_tuple_new_low( |
| 460 |
const dict_index_t *index, /*!< in: index of tuple */ |
| 461 |
ulint n_cols, /*!< in: no. of cols in tuple */ |
| 462 |
mem_heap_t *heap) /*!< in: memory heap */ |
| 463 |
{ |
| 464 |
ib_tuple_t *tuple; |
| 465 |
|
| 466 |
tuple = static_cast<ib_tuple_t *>(mem_heap_alloc(heap, sizeof(*tuple))); |
| 467 |
|
| 468 |
if (tuple == nullptr) { |
| 469 |
mem_heap_free(heap); |
| 470 |
return (nullptr); |
| 471 |
} |
| 472 |
|
| 473 |
tuple->heap = heap; |
| 474 |
tuple->index = index; |
| 475 |
tuple->type = TPL_TYPE_ROW; |
| 476 |
|
| 477 |
tuple->ptr = dtuple_create(heap, n_cols); |
| 478 |
|
| 479 |
/* Copy types and set to SQL_NULL. */ |
| 480 |
dict_table_copy_types(tuple->ptr, index->table); |
| 481 |
|
| 482 |
return ((ib_tpl_t)tuple); |
| 483 |
} |
| 484 |
|
| 485 |
/** Create an InnoDB row tuple. |
| 486 |
@return tuple instance, or NULL */ |
| 487 |
static ib_tpl_t ib_row_tuple_new( |
| 488 |
const dict_index_t *index, /*!< in: index of tuple */ |
| 489 |
ulint n_cols) /*!< in: no. of cols in tuple */ |
| 490 |
{ |
| 491 |
mem_heap_t *heap; |
| 492 |
|
| 493 |
heap = mem_heap_create(64, UT_LOCATION_HERE); |
| 494 |
|
| 495 |
if (heap == nullptr) { |
| 496 |
return (nullptr); |
| 497 |
} |
| 498 |
|
| 499 |
return (ib_row_tuple_new_low(index, n_cols, heap)); |
| 500 |
} |
| 501 |
|
| 502 |
/** Begin a transaction. |
| 503 |
@return innobase txn handle */ |
| 504 |
ib_err_t ib_trx_start( |
| 505 |
ib_trx_t ib_trx, /*!< in: transaction to restart */ |
| 506 |
ib_trx_level_t ib_trx_level, /*!< in: trx isolation level */ |
| 507 |
bool read_write, /*!< in: true if read write |
| 508 |
transaction */ |
| 509 |
bool auto_commit, /*!< in: auto commit after each |
| 510 |
single DML */ |
| 511 |
void *thd) /*!< in: THD */ |
| 512 |
{ |
| 513 |
ib_err_t err = DB_SUCCESS; |
| 514 |
trx_t *trx = (trx_t *)ib_trx; |
| 515 |
|
| 516 |
ut_a(ib_trx_level <= IB_TRX_SERIALIZABLE); |
| 517 |
|
| 518 |
trx->api_trx = true; |
| 519 |
trx->api_auto_commit = auto_commit; |
| 520 |
trx->read_write = read_write; |
| 521 |
|
| 522 |
trx_start_if_not_started(trx, read_write, UT_LOCATION_HERE); |
| 523 |
|
| 524 |
trx->isolation_level = static_cast<trx_t::isolation_level_t>(ib_trx_level); |
| 525 |
|
| 526 |
/* FIXME: This is a place holder, we should add an arg that comes |
| 527 |
from the client. */ |
| 528 |
trx->mysql_thd = static_cast<THD *>(thd); |
| 529 |
|
| 530 |
return (err); |
| 531 |
} |
| 532 |
|
| 533 |
/** Begin a transaction. This will allocate a new transaction handle. |
| 534 |
put the transaction in the active state. |
| 535 |
@return innobase txn handle */ |
| 536 |
ib_trx_t ib_trx_begin( |
| 537 |
ib_trx_level_t ib_trx_level, /*!< in: trx isolation level */ |
| 538 |
bool read_write, /*!< in: true if read write |
| 539 |
transaction */ |
| 540 |
bool auto_commit, /*!< in: auto commit after each |
| 541 |
single DML */ |
| 542 |
void *thd) /*!< in,out: MySQL THD */ |
| 543 |
{ |
| 544 |
trx_t *trx; |
| 545 |
|
| 546 |
trx = trx_allocate_for_mysql(); |
| 547 |
|
| 548 |
ib_err_t started = ib_trx_start(static_cast<ib_trx_t>(trx), ib_trx_level, |
| 549 |
read_write, auto_commit, thd); |
| 550 |
ut_a(started); |
| 551 |
|
| 552 |
return (static_cast<ib_trx_t>(trx)); |
| 553 |
} |
| 554 |
|
| 555 |
/** Check if transaction is read_only |
| 556 |
@return transaction read_only status */ |
| 557 |
uint32_t ib_trx_read_only(ib_trx_t ib_trx) /*!< in: trx handle */ |
| 558 |
{ |
| 559 |
trx_t *trx = (trx_t *)ib_trx; |
| 560 |
|
| 561 |
return (trx->read_only); |
| 562 |
} |
| 563 |
/** Get a trx start time. |
| 564 |
@return trx start_time */ |
| 565 |
uint64_t ib_trx_get_start_time(ib_trx_t ib_trx) /*!< in: transaction */ |
| 566 |
{ |
| 567 |
trx_t *trx = (trx_t *)ib_trx; |
| 568 |
return static_cast<uint64_t>(std::chrono::system_clock::to_time_t( |
| 569 |
trx->start_time.load(std::memory_order_relaxed))); |
| 570 |
} |
| 571 |
/** Release the resources of the transaction. |
| 572 |
@return DB_SUCCESS or err code */ |
| 573 |
ib_err_t ib_trx_release(ib_trx_t ib_trx) /*!< in: trx handle */ |
| 574 |
{ |
| 575 |
trx_t *trx = (trx_t *)ib_trx; |
| 576 |
|
| 577 |
ut_ad(trx != nullptr); |
| 578 |
trx_free_for_mysql(trx); |
| 579 |
|
| 580 |
return (DB_SUCCESS); |
| 581 |
} |
| 582 |
|
| 583 |
/** Commit a transaction. This function will also release the schema |
| 584 |
latches too. |
| 585 |
@return DB_SUCCESS or err code */ |
| 586 |
ib_err_t ib_trx_commit(ib_trx_t ib_trx) /*!< in: trx handle */ |
| 587 |
{ |
| 588 |
ib_err_t err = DB_SUCCESS; |
| 589 |
trx_t *trx = reinterpret_cast<trx_t *>(ib_trx); |
| 590 |
|
| 591 |
if (!trx_is_started(trx)) { |
| 592 |
return (err); |
| 593 |
} |
| 594 |
|
| 595 |
trx_commit(trx); |
| 596 |
|
| 597 |
return (DB_SUCCESS); |
| 598 |
} |
| 599 |
|
| 600 |
/** Rollback a transaction. This function will also release the schema |
| 601 |
latches too. |
| 602 |
@return DB_SUCCESS or err code */ |
| 603 |
ib_err_t ib_trx_rollback(ib_trx_t ib_trx) /*!< in: trx handle */ |
| 604 |
{ |
| 605 |
ib_err_t err; |
| 606 |
trx_t *trx = (trx_t *)ib_trx; |
| 607 |
|
| 608 |
err = static_cast<ib_err_t>(trx_rollback_for_mysql(trx)); |
| 609 |
|
| 610 |
/* It should always succeed */ |
| 611 |
ut_a(err == DB_SUCCESS); |
| 612 |
|
| 613 |
return (err); |
| 614 |
} |
| 615 |
|
| 616 |
#ifdef _WIN32 |
| 617 |
/** Convert a string to lower case. */ |
| 618 |
static void ib_to_lower_case(char *ptr) /*!< string to convert to lower case */ |
| 619 |
{ |
| 620 |
while (*ptr) { |
| 621 |
*ptr = tolower(*ptr); |
| 622 |
++ptr; |
| 623 |
} |
| 624 |
} |
| 625 |
#endif /* _WIN32 */ |
| 626 |
|
| 627 |
/** Normalizes a table name string. A normalized name consists of the |
| 628 |
database name catenated to '/' and table name. An example: |
| 629 |
test/mytable. On Windows normalization puts both the database name and the |
| 630 |
table name always to lower case. This function can be called for system |
| 631 |
tables and they don't have a database component. For tables that don't have |
| 632 |
a database component, we don't normalize them to lower case on Windows. |
| 633 |
The assumption is that they are system tables that reside in the system |
| 634 |
table space. */ |
| 635 |
static void ib_normalize_table_name( |
| 636 |
char *norm_name, /*!< out: normalized name as a |
| 637 |
null-terminated string */ |
| 638 |
const char *name) /*!< in: table name string */ |
| 639 |
{ |
| 640 |
const char *ptr = name; |
| 641 |
|
| 642 |
/* Scan name from the end */ |
| 643 |
|
| 644 |
ptr += ut_strlen(name) - 1; |
| 645 |
|
| 646 |
/* Find the start of the table name. */ |
| 647 |
while (ptr >= name && *ptr != '\\' && *ptr != '/' && ptr > name) { |
| 648 |
--ptr; |
| 649 |
} |
| 650 |
|
| 651 |
/* For system tables there is no '/' or dbname. */ |
| 652 |
ut_a(ptr >= name); |
| 653 |
|
| 654 |
if (ptr > name) { |
| 655 |
const char *db_name; |
| 656 |
const char *table_name; |
| 657 |
|
| 658 |
table_name = ptr + 1; |
| 659 |
|
| 660 |
--ptr; |
| 661 |
|
| 662 |
while (ptr >= name && *ptr != '\\' && *ptr != '/') { |
| 663 |
ptr--; |
| 664 |
} |
| 665 |
|
| 666 |
db_name = ptr + 1; |
| 667 |
|
| 668 |
memcpy(norm_name, db_name, ut_strlen(name) + 1 - (db_name - name)); |
| 669 |
|
| 670 |
norm_name[table_name - db_name - 1] = '/'; |
| 671 |
#ifdef _WIN32 |
| 672 |
ib_to_lower_case(norm_name); |
| 673 |
#endif |
| 674 |
} else { |
| 675 |
ut_strcpy(norm_name, name); |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
/** Get a table id. The caller must have acquired the dictionary mutex. |
| 680 |
@return DB_SUCCESS if found */ |
| 681 |
static ib_err_t ib_table_get_id_low( |
| 682 |
const char *table_name, /*!< in: table to find */ |
| 683 |
ib_id_u64_t *table_id) /*!< out: table id if found */ |
| 684 |
{ |
| 685 |
dict_table_t *table; |
| 686 |
ib_err_t err = DB_TABLE_NOT_FOUND; |
| 687 |
|
| 688 |
*table_id = 0; |
| 689 |
|
| 690 |
table = ib_lookup_table_by_name(table_name); |
| 691 |
|
| 692 |
if (table != nullptr) { |
| 693 |
*table_id = (table->id); |
| 694 |
|
| 695 |
err = DB_SUCCESS; |
| 696 |
} |
| 697 |
|
| 698 |
return (err); |
| 699 |
} |
| 700 |
|
| 701 |
/** Create an internal cursor instance. |
| 702 |
@return DB_SUCCESS or err code */ |
| 703 |
static ib_err_t ib_create_cursor(ib_crsr_t *ib_crsr, /*!< out: InnoDB cursor */ |
| 704 |
dict_table_t *table, /*!< in: table instance */ |
| 705 |
dict_index_t *index, /*!< in: index to use */ |
| 706 |
trx_t *trx) /*!< in: transaction */ |
| 707 |
{ |
| 708 |
mem_heap_t *heap; |
| 709 |
ib_cursor_t *cursor; |
| 710 |
ib_err_t err = DB_SUCCESS; |
| 711 |
|
| 712 |
// passing non-null might mean a memleak of old cursor |
| 713 |
ut_ad(*ib_crsr == nullptr); |
| 714 |
heap = mem_heap_create(sizeof(*cursor) * 2, UT_LOCATION_HERE); |
| 715 |
|
| 716 |
if (heap != nullptr) { |
| 717 |
row_prebuilt_t *prebuilt; |
| 718 |
|
| 719 |
cursor = static_cast<ib_cursor_t *>(mem_heap_zalloc(heap, sizeof(*cursor))); |
| 720 |
|
| 721 |
cursor->heap = heap; |
| 722 |
|
| 723 |
cursor->query_heap = mem_heap_create(64, UT_LOCATION_HERE); |
| 724 |
|
| 725 |
if (cursor->query_heap == nullptr) { |
| 726 |
mem_heap_free(heap); |
| 727 |
|
| 728 |
return (DB_OUT_OF_MEMORY); |
| 729 |
} |
| 730 |
|
| 731 |
cursor->prebuilt = row_create_prebuilt(table, 0); |
| 732 |
|
| 733 |
prebuilt = cursor->prebuilt; |
| 734 |
|
| 735 |
prebuilt->trx = trx; |
| 736 |
|
| 737 |
cursor->valid_trx = true; |
| 738 |
|
| 739 |
prebuilt->table = table; |
| 740 |
prebuilt->select_lock_type = LOCK_NONE; |
| 741 |
prebuilt->innodb_api = true; |
| 742 |
|
| 743 |
prebuilt->index = index; |
| 744 |
|
| 745 |
ut_a(prebuilt->index != nullptr); |
| 746 |
|
| 747 |
if (prebuilt->trx != nullptr) { |
| 748 |
++prebuilt->trx->n_mysql_tables_in_use; |
| 749 |
|
| 750 |
prebuilt->index_usable = prebuilt->index->is_usable(prebuilt->trx); |
| 751 |
|
| 752 |
/* Assign a read view if the transaction does |
| 753 |
not have it yet */ |
| 754 |
|
| 755 |
trx_assign_read_view(prebuilt->trx); |
| 756 |
} |
| 757 |
|
| 758 |
*ib_crsr = (ib_crsr_t)cursor; |
| 759 |
} else { |
| 760 |
err = DB_OUT_OF_MEMORY; |
| 761 |
} |
| 762 |
|
| 763 |
return (err); |
| 764 |
} |
| 765 |
|
| 766 |
/** Create an internal cursor instance on the clustered index. |
| 767 |
@param[out] ib_crsr InnoDB cursor |
| 768 |
@param[in,out] table table instance |
| 769 |
@param[in,out] trx transaction |
| 770 |
@return DB_SUCCESS or err code */ |
| 771 |
static ib_err_t ib_create_cursor_with_clust_index(ib_crsr_t *ib_crsr, |
| 772 |
dict_table_t *table, |
| 773 |
trx_t *trx) { |
| 774 |
dict_index_t *index = table->first_index(); |
| 775 |
|
| 776 |
return (ib_create_cursor(ib_crsr, table, index, trx)); |
| 777 |
} |
| 778 |
|
| 779 |
/** Open an InnoDB secondary index cursor and return a cursor handle to it. |
| 780 |
@return DB_SUCCESS or err code */ |
| 781 |
ib_err_t ib_cursor_open_index_using_name( |
| 782 |
ib_crsr_t ib_open_crsr, /*!< in: open/active cursor */ |
| 783 |
const char *index_name, /*!< in: secondary index name */ |
| 784 |
ib_crsr_t *ib_crsr, /*!< out,own: InnoDB index cursor */ |
| 785 |
int *idx_type, /*!< out: index is cluster index */ |
| 786 |
ib_id_u64_t *idx_id) /*!< out: index id */ |
| 787 |
{ |
| 788 |
dict_table_t *table; |
| 789 |
dict_index_t *index; |
| 790 |
space_index_t index_id = 0; |
| 791 |
ib_err_t err = DB_TABLE_NOT_FOUND; |
| 792 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_open_crsr; |
| 793 |
|
| 794 |
*idx_type = 0; |
| 795 |
*idx_id = 0; |
| 796 |
*ib_crsr = nullptr; |
| 797 |
|
| 798 |
table = cursor->prebuilt->table; |
| 799 |
ut_a(table != nullptr); |
| 800 |
|
| 801 |
dict_sys_mutex_enter(); |
| 802 |
table->acquire(); |
| 803 |
dict_sys_mutex_exit(); |
| 804 |
|
| 805 |
/* The first index is always the cluster index. */ |
| 806 |
index = table->first_index(); |
| 807 |
|
| 808 |
/* Traverse the user defined indexes. */ |
| 809 |
while (index != nullptr) { |
| 810 |
if (innobase_strcasecmp(index->name, index_name) == 0) { |
| 811 |
index_id = index->id; |
| 812 |
*idx_type = index->type; |
| 813 |
*idx_id = index_id; |
| 814 |
break; |
| 815 |
} |
| 816 |
index = UT_LIST_GET_NEXT(indexes, index); |
| 817 |
} |
| 818 |
|
| 819 |
if (!index_id) { |
| 820 |
dict_table_close(table, false, false); |
| 821 |
return (DB_ERROR); |
| 822 |
} |
| 823 |
|
| 824 |
if (index_id > 0) { |
| 825 |
ut_ad(index->id == index_id); |
| 826 |
err = ib_create_cursor(ib_crsr, table, index, cursor->prebuilt->trx); |
| 827 |
} |
| 828 |
|
| 829 |
if (*ib_crsr != nullptr) { |
| 830 |
const ib_cursor_t *cursor; |
| 831 |
|
| 832 |
cursor = *(ib_cursor_t **)ib_crsr; |
| 833 |
|
| 834 |
if (cursor->prebuilt->index == nullptr) { |
| 835 |
err = ib_cursor_close(*ib_crsr); |
| 836 |
ut_a(err == DB_SUCCESS); |
| 837 |
*ib_crsr = nullptr; |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
return (err); |
| 842 |
} |
| 843 |
|
| 844 |
/** Open an InnoDB table and return a cursor handle to it. |
| 845 |
@return DB_SUCCESS or err code */ |
| 846 |
ib_err_t ib_cursor_open_table(const char *name, /*!< in: table name */ |
| 847 |
ib_trx_t ib_trx, /*!< in: Current transaction |
| 848 |
handle can be NULL */ |
| 849 |
ib_crsr_t *ib_crsr) /*!< out,own: InnoDB cursor */ |
| 850 |
{ |
| 851 |
ib_err_t err; |
| 852 |
dict_table_t *table; |
| 853 |
char *normalized_name; |
| 854 |
trx_t *trx = static_cast<trx_t *>(ib_trx); |
| 855 |
MDL_ticket *mdl = nullptr; |
| 856 |
|
| 857 |
normalized_name = static_cast<char *>( |
| 858 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, ut_strlen(name) + 1)); |
| 859 |
ib_normalize_table_name(normalized_name, name); |
| 860 |
|
| 861 |
ut_ad(ib_trx != nullptr); |
| 862 |
|
| 863 |
if (!ib_schema_lock_is_exclusive(ib_trx)) { |
| 864 |
table = dd_table_open_on_name(trx->mysql_thd, &mdl, normalized_name, false, |
| 865 |
DICT_ERR_IGNORE_NONE); |
| 866 |
} else { |
| 867 |
/* NOTE: We do not acquire MySQL metadata lock */ |
| 868 |
table = ib_lookup_table_by_name(normalized_name); |
| 869 |
} |
| 870 |
|
| 871 |
ut::free(normalized_name); |
| 872 |
normalized_name = nullptr; |
| 873 |
|
| 874 |
/* It can happen that another thread has created the table but |
| 875 |
not the cluster index or it's a broken table definition. Refuse to |
| 876 |
open if that's the case. */ |
| 877 |
if (table != nullptr && table->first_index() == nullptr) { |
| 878 |
table = nullptr; |
| 879 |
} |
| 880 |
|
| 881 |
if (table != nullptr) { |
| 882 |
err = ib_create_cursor_with_clust_index(ib_crsr, table, (trx_t *)ib_trx); |
| 883 |
if (mdl != nullptr) { |
| 884 |
(*ib_crsr)->mdl = mdl; |
| 885 |
} |
| 886 |
} else { |
| 887 |
err = DB_TABLE_NOT_FOUND; |
| 888 |
} |
| 889 |
|
| 890 |
return (err); |
| 891 |
} |
| 892 |
|
| 893 |
/** Check the table whether it contains virtual columns. |
| 894 |
@param[in] crsr InnoDB Cursor |
| 895 |
@return true if the table contains virtual column else failure. */ |
| 896 |
bool ib_is_virtual_table(ib_crsr_t crsr) { |
| 897 |
return (crsr->prebuilt->table->n_v_cols > 0); |
| 898 |
} |
| 899 |
|
| 900 |
/** Free a context struct for a table handle. */ |
| 901 |
static void ib_qry_proc_free( |
| 902 |
ib_qry_proc_t *q_proc) /*!< in, own: qproc struct */ |
| 903 |
{ |
| 904 |
que_graph_free_recursive(q_proc->grph.ins); |
| 905 |
que_graph_free_recursive(q_proc->grph.upd); |
| 906 |
que_graph_free_recursive(q_proc->grph.sel); |
| 907 |
if (q_proc->node.upd && q_proc->node.upd->update) { |
| 908 |
q_proc->node.upd->update->free_per_stmt_heap(); |
| 909 |
} |
| 910 |
|
| 911 |
memset(q_proc, 0x0, sizeof(*q_proc)); |
| 912 |
} |
| 913 |
|
| 914 |
/** Reset the cursor. |
| 915 |
@return DB_SUCCESS or err code */ |
| 916 |
ib_err_t ib_cursor_reset(ib_crsr_t ib_crsr) /*!< in/out: InnoDB cursor */ |
| 917 |
{ |
| 918 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 919 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 920 |
|
| 921 |
if (cursor->valid_trx && prebuilt->trx != nullptr && |
| 922 |
prebuilt->trx->n_mysql_tables_in_use > 0) { |
| 923 |
--prebuilt->trx->n_mysql_tables_in_use; |
| 924 |
} |
| 925 |
|
| 926 |
/* The fields in this data structure are allocated from |
| 927 |
the query heap and so need to be reset too. */ |
| 928 |
ib_qry_proc_free(&cursor->q_proc); |
| 929 |
|
| 930 |
mem_heap_empty(cursor->query_heap); |
| 931 |
|
| 932 |
return (DB_SUCCESS); |
| 933 |
} |
| 934 |
|
| 935 |
/** update the cursor with new transactions and also reset the cursor |
| 936 |
@return DB_SUCCESS or err code */ |
| 937 |
ib_err_t ib_cursor_new_trx(ib_crsr_t ib_crsr, /*!< in/out: InnoDB cursor */ |
| 938 |
ib_trx_t ib_trx) /*!< in: transaction */ |
| 939 |
{ |
| 940 |
ib_err_t err = DB_SUCCESS; |
| 941 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 942 |
trx_t *trx = (trx_t *)ib_trx; |
| 943 |
|
| 944 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 945 |
|
| 946 |
row_update_prebuilt_trx(prebuilt, trx); |
| 947 |
|
| 948 |
cursor->valid_trx = true; |
| 949 |
|
| 950 |
trx_assign_read_view(prebuilt->trx); |
| 951 |
|
| 952 |
ib_qry_proc_free(&cursor->q_proc); |
| 953 |
|
| 954 |
mem_heap_empty(cursor->query_heap); |
| 955 |
|
| 956 |
return (err); |
| 957 |
} |
| 958 |
|
| 959 |
/** Commit the transaction in a cursor |
| 960 |
@return DB_SUCCESS or err code */ |
| 961 |
ib_err_t ib_cursor_commit_trx(ib_crsr_t ib_crsr, /*!< in/out: InnoDB cursor */ |
| 962 |
ib_trx_t ib_trx) /*!< in: transaction */ |
| 963 |
{ |
| 964 |
ib_err_t err = DB_SUCCESS; |
| 965 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 966 |
#ifdef UNIV_DEBUG |
| 967 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 968 |
|
| 969 |
ut_ad(prebuilt->trx == (trx_t *)ib_trx); |
| 970 |
#endif /* UNIV_DEBUG */ |
| 971 |
ib_trx_commit(ib_trx); |
| 972 |
cursor->valid_trx = false; |
| 973 |
return (err); |
| 974 |
} |
| 975 |
|
| 976 |
/** Close an InnoDB table and free the cursor. |
| 977 |
@return DB_SUCCESS or err code */ |
| 978 |
ib_err_t ib_cursor_close(ib_crsr_t ib_crsr) /*!< in,own: InnoDB cursor */ |
| 979 |
{ |
| 980 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 981 |
row_prebuilt_t *prebuilt; |
| 982 |
trx_t *trx; |
| 983 |
|
| 984 |
if (!cursor) { |
| 985 |
return (DB_SUCCESS); |
| 986 |
} |
| 987 |
|
| 988 |
prebuilt = cursor->prebuilt; |
| 989 |
trx = prebuilt->trx; |
| 990 |
|
| 991 |
ib_qry_proc_free(&cursor->q_proc); |
| 992 |
|
| 993 |
/* The transaction could have been detached from the cursor. */ |
| 994 |
if (cursor->valid_trx && trx != nullptr && trx->n_mysql_tables_in_use > 0) { |
| 995 |
--trx->n_mysql_tables_in_use; |
| 996 |
} |
| 997 |
|
| 998 |
row_prebuilt_free(prebuilt, false); |
| 999 |
cursor->prebuilt = nullptr; |
| 1000 |
|
| 1001 |
if (cursor->mdl != nullptr) { |
| 1002 |
dd_mdl_release(trx->mysql_thd, &cursor->mdl); |
| 1003 |
} |
| 1004 |
|
| 1005 |
mem_heap_free(cursor->query_heap); |
| 1006 |
mem_heap_free(cursor->heap); |
| 1007 |
cursor = nullptr; |
| 1008 |
|
| 1009 |
return (DB_SUCCESS); |
| 1010 |
} |
| 1011 |
|
| 1012 |
/** Run the insert query and do error handling. |
| 1013 |
@return DB_SUCCESS or error code */ |
| 1014 |
static inline ib_err_t ib_insert_row_with_lock_retry( |
| 1015 |
que_thr_t *thr, /*!< in: insert query graph */ |
| 1016 |
ins_node_t *node, /*!< in: insert node for the query */ |
| 1017 |
trx_savept_t *savept) /*!< in: savepoint to rollback to |
| 1018 |
in case of an error */ |
| 1019 |
{ |
| 1020 |
trx_t *trx; |
| 1021 |
ib_err_t err; |
| 1022 |
bool lock_wait; |
| 1023 |
|
| 1024 |
bool is_sdi = dict_table_is_sdi(node->table->id); |
| 1025 |
|
| 1026 |
trx = thr_get_trx(thr); |
| 1027 |
|
| 1028 |
do { |
| 1029 |
thr->run_node = node; |
| 1030 |
thr->prev_node = node; |
| 1031 |
|
| 1032 |
row_ins_step(thr); |
| 1033 |
|
| 1034 |
err = trx->error_state; |
| 1035 |
|
| 1036 |
if (err != DB_SUCCESS) { |
| 1037 |
que_thr_stop_for_mysql(thr); |
| 1038 |
|
| 1039 |
thr->lock_state = QUE_THR_LOCK_ROW; |
| 1040 |
lock_wait = ib_handle_errors(&err, trx, thr, savept, is_sdi); |
| 1041 |
thr->lock_state = QUE_THR_LOCK_NOLOCK; |
| 1042 |
} else { |
| 1043 |
lock_wait = false; |
| 1044 |
} |
| 1045 |
} while (lock_wait); |
| 1046 |
|
| 1047 |
return (err); |
| 1048 |
} |
| 1049 |
|
| 1050 |
/** Write a row. |
| 1051 |
@return DB_SUCCESS or err code */ |
| 1052 |
static ib_err_t ib_execute_insert_query_graph( |
| 1053 |
dict_table_t *table, /*!< in: table where to insert */ |
| 1054 |
que_fork_t *ins_graph, /*!< in: query graph */ |
| 1055 |
ins_node_t *node) /*!< in: insert node */ |
| 1056 |
{ |
| 1057 |
trx_t *trx; |
| 1058 |
que_thr_t *thr; |
| 1059 |
trx_savept_t savept; |
| 1060 |
ib_err_t err = DB_SUCCESS; |
| 1061 |
|
| 1062 |
trx = ins_graph->trx; |
| 1063 |
|
| 1064 |
savept = trx_savept_take(trx); |
| 1065 |
|
| 1066 |
thr = que_fork_get_first_thr(ins_graph); |
| 1067 |
|
| 1068 |
que_thr_move_to_run_state_for_mysql(thr, trx); |
| 1069 |
|
| 1070 |
err = ib_insert_row_with_lock_retry(thr, node, &savept); |
| 1071 |
|
| 1072 |
if (err == DB_SUCCESS) { |
| 1073 |
que_thr_stop_for_mysql_no_error(thr, trx); |
| 1074 |
|
| 1075 |
dict_table_n_rows_inc(table); |
| 1076 |
|
| 1077 |
if (table->is_system_table) { |
| 1078 |
srv_stats.n_system_rows_inserted.inc(); |
| 1079 |
} else { |
| 1080 |
srv_stats.n_rows_inserted.inc(); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
trx->op_info = ""; |
| 1085 |
|
| 1086 |
return (err); |
| 1087 |
} |
| 1088 |
|
| 1089 |
/** Create an insert query graph node. */ |
| 1090 |
static void ib_insert_query_graph_create( |
| 1091 |
ib_cursor_t *cursor) /*!< in: Cursor instance */ |
| 1092 |
{ |
| 1093 |
ib_qry_proc_t *q_proc = &cursor->q_proc; |
| 1094 |
ib_qry_node_t *node = &q_proc->node; |
| 1095 |
trx_t *trx = cursor->prebuilt->trx; |
| 1096 |
|
| 1097 |
ut_a(trx_is_started(trx)); |
| 1098 |
|
| 1099 |
if (node->ins == nullptr) { |
| 1100 |
dtuple_t *row; |
| 1101 |
ib_qry_grph_t *grph = &q_proc->grph; |
| 1102 |
mem_heap_t *heap = cursor->query_heap; |
| 1103 |
dict_table_t *table = cursor->prebuilt->table; |
| 1104 |
|
| 1105 |
node->ins = ins_node_create(INS_DIRECT, table, heap); |
| 1106 |
|
| 1107 |
node->ins->select = nullptr; |
| 1108 |
node->ins->values_list = nullptr; |
| 1109 |
|
| 1110 |
row = dtuple_create(heap, table->get_n_cols()); |
| 1111 |
dict_table_copy_types(row, table); |
| 1112 |
|
| 1113 |
ut_ad(!dict_table_have_virtual_index(table)); |
| 1114 |
|
| 1115 |
ins_node_set_new_row(node->ins, row); |
| 1116 |
|
| 1117 |
grph->ins = static_cast<que_fork_t *>(que_node_get_parent( |
| 1118 |
pars_complete_graph_for_exec(node->ins, trx, heap, nullptr))); |
| 1119 |
|
| 1120 |
grph->ins->state = QUE_FORK_ACTIVE; |
| 1121 |
} else { |
| 1122 |
node->ins->ins_multi_val_pos = 0; |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
/** Insert a row to a table. |
| 1127 |
@return DB_SUCCESS or err code */ |
| 1128 |
ib_err_t ib_cursor_insert_row( |
| 1129 |
ib_crsr_t ib_crsr, /*!< in/out: InnoDB cursor instance */ |
| 1130 |
const ib_tpl_t ib_tpl) /*!< in: tuple to insert */ |
| 1131 |
{ |
| 1132 |
ib_qry_node_t *node; |
| 1133 |
ib_qry_proc_t *q_proc; |
| 1134 |
ulint n_fields; |
| 1135 |
dtuple_t *dst_dtuple; |
| 1136 |
ib_err_t err = DB_SUCCESS; |
| 1137 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 1138 |
const ib_tuple_t *src_tuple = (const ib_tuple_t *)ib_tpl; |
| 1139 |
|
| 1140 |
ib_insert_query_graph_create(cursor); |
| 1141 |
|
| 1142 |
ut_ad(src_tuple->type == TPL_TYPE_ROW); |
| 1143 |
|
| 1144 |
q_proc = &cursor->q_proc; |
| 1145 |
node = &q_proc->node; |
| 1146 |
|
| 1147 |
node->ins->state = INS_NODE_ALLOC_ROW_ID; |
| 1148 |
dst_dtuple = node->ins->row; |
| 1149 |
|
| 1150 |
n_fields = dtuple_get_n_fields(src_tuple->ptr); |
| 1151 |
ut_ad(n_fields == dtuple_get_n_fields(dst_dtuple)); |
| 1152 |
|
| 1153 |
/* Do a shallow copy of the data fields and check for NULL |
| 1154 |
constraints on columns. */ |
| 1155 |
for (ulint i = 0; i < n_fields; i++) { |
| 1156 |
ulint mtype; |
| 1157 |
dfield_t *src_field; |
| 1158 |
dfield_t *dst_field; |
| 1159 |
|
| 1160 |
src_field = dtuple_get_nth_field(src_tuple->ptr, i); |
| 1161 |
|
| 1162 |
mtype = dtype_get_mtype(dfield_get_type(src_field)); |
| 1163 |
|
| 1164 |
/* Don't touch the system columns. */ |
| 1165 |
if (mtype != DATA_SYS) { |
| 1166 |
ulint prtype; |
| 1167 |
|
| 1168 |
prtype = dtype_get_prtype(dfield_get_type(src_field)); |
| 1169 |
|
| 1170 |
if ((prtype & DATA_NOT_NULL) && dfield_is_null(src_field)) { |
| 1171 |
err = DB_DATA_MISMATCH; |
| 1172 |
break; |
| 1173 |
} |
| 1174 |
|
| 1175 |
dst_field = dtuple_get_nth_field(dst_dtuple, i); |
| 1176 |
ut_ad(mtype == dtype_get_mtype(dfield_get_type(dst_field))); |
| 1177 |
|
| 1178 |
/* Do a shallow copy. */ |
| 1179 |
dfield_set_data(dst_field, src_field->data, src_field->len); |
| 1180 |
|
| 1181 |
if (dst_field->len != IB_SQL_NULL) { |
| 1182 |
UNIV_MEM_ASSERT_RW(dst_field->data, dst_field->len); |
| 1183 |
} |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
if (err == DB_SUCCESS) { |
| 1188 |
err = ib_execute_insert_query_graph(src_tuple->index->table, |
| 1189 |
q_proc->grph.ins, node->ins); |
| 1190 |
} |
| 1191 |
|
| 1192 |
ib_wake_master_thread(); |
| 1193 |
|
| 1194 |
return (err); |
| 1195 |
} |
| 1196 |
|
| 1197 |
/** Gets pointer to a prebuilt update vector used in updates. |
| 1198 |
@return update vector */ |
| 1199 |
static inline upd_t *ib_update_vector_create( |
| 1200 |
ib_cursor_t *cursor) /*!< in: current cursor */ |
| 1201 |
{ |
| 1202 |
trx_t *trx = cursor->prebuilt->trx; |
| 1203 |
mem_heap_t *heap = cursor->query_heap; |
| 1204 |
dict_table_t *table = cursor->prebuilt->table; |
| 1205 |
ib_qry_proc_t *q_proc = &cursor->q_proc; |
| 1206 |
ib_qry_grph_t *grph = &q_proc->grph; |
| 1207 |
ib_qry_node_t *node = &q_proc->node; |
| 1208 |
|
| 1209 |
ut_a(trx_is_started(trx)); |
| 1210 |
|
| 1211 |
if (node->upd == nullptr) { |
| 1212 |
node->upd = static_cast<upd_node_t *>( |
| 1213 |
row_create_update_node_for_mysql(table, heap)); |
| 1214 |
} else { |
| 1215 |
node->upd->del_multi_val_pos = 0; |
| 1216 |
node->upd->upd_multi_val_pos = 0; |
| 1217 |
} |
| 1218 |
|
| 1219 |
ut_ad(!dict_table_have_virtual_index(table)); |
| 1220 |
|
| 1221 |
grph->upd = static_cast<que_fork_t *>(que_node_get_parent( |
| 1222 |
pars_complete_graph_for_exec(node->upd, trx, heap, nullptr))); |
| 1223 |
|
| 1224 |
grph->upd->state = QUE_FORK_ACTIVE; |
| 1225 |
|
| 1226 |
return (node->upd->update); |
| 1227 |
} |
| 1228 |
|
| 1229 |
/** Note that a column has changed. */ |
| 1230 |
static void ib_update_col( |
| 1231 |
|
| 1232 |
ib_cursor_t *cursor, /*!< in: current cursor */ |
| 1233 |
upd_field_t *upd_field, /*!< in/out: update field */ |
| 1234 |
ulint col_no, /*!< in: column number */ |
| 1235 |
dfield_t *dfield) /*!< in: updated dfield */ |
| 1236 |
{ |
| 1237 |
ulint data_len; |
| 1238 |
dict_table_t *table = cursor->prebuilt->table; |
| 1239 |
dict_index_t *index = table->first_index(); |
| 1240 |
|
| 1241 |
data_len = dfield_get_len(dfield); |
| 1242 |
|
| 1243 |
if (data_len == UNIV_SQL_NULL) { |
| 1244 |
dfield_set_null(&upd_field->new_val); |
| 1245 |
} else { |
| 1246 |
dfield_copy_data(&upd_field->new_val, dfield); |
| 1247 |
} |
| 1248 |
|
| 1249 |
upd_field->exp = nullptr; |
| 1250 |
|
| 1251 |
upd_field->orig_len = 0; |
| 1252 |
|
| 1253 |
upd_field->field_no = dict_col_get_clust_pos(&table->cols[col_no], index); |
| 1254 |
IF_DEBUG(upd_field->field_phy_pos = table->cols[col_no].get_col_phy_pos();) |
| 1255 |
} |
| 1256 |
|
| 1257 |
/** Checks which fields have changed in a row and stores the new data |
| 1258 |
to an update vector. |
| 1259 |
@return DB_SUCCESS or err code */ |
| 1260 |
static ib_err_t ib_calc_diff( |
| 1261 |
ib_cursor_t *cursor, /*!< in: current cursor */ |
| 1262 |
upd_t *upd, /*!< in/out: update vector */ |
| 1263 |
const ib_tuple_t *old_tuple, /*!< in: Old tuple in table */ |
| 1264 |
const ib_tuple_t *new_tuple) /*!< in: New tuple to update */ |
| 1265 |
{ |
| 1266 |
ulint i; |
| 1267 |
ulint n_changed = 0; |
| 1268 |
ib_err_t err = DB_SUCCESS; |
| 1269 |
ulint n_fields = dtuple_get_n_fields(new_tuple->ptr); |
| 1270 |
|
| 1271 |
ut_a(old_tuple->type == TPL_TYPE_ROW); |
| 1272 |
ut_a(new_tuple->type == TPL_TYPE_ROW); |
| 1273 |
ut_a(old_tuple->index->table == new_tuple->index->table); |
| 1274 |
|
| 1275 |
for (i = 0; i < n_fields; ++i) { |
| 1276 |
ulint mtype; |
| 1277 |
ulint prtype; |
| 1278 |
upd_field_t *upd_field; |
| 1279 |
dfield_t *new_dfield; |
| 1280 |
dfield_t *old_dfield; |
| 1281 |
|
| 1282 |
new_dfield = dtuple_get_nth_field(new_tuple->ptr, i); |
| 1283 |
old_dfield = dtuple_get_nth_field(old_tuple->ptr, i); |
| 1284 |
|
| 1285 |
mtype = dtype_get_mtype(dfield_get_type(old_dfield)); |
| 1286 |
prtype = dtype_get_prtype(dfield_get_type(old_dfield)); |
| 1287 |
|
| 1288 |
/* Skip the system columns */ |
| 1289 |
if (mtype == DATA_SYS) { |
| 1290 |
continue; |
| 1291 |
|
| 1292 |
} else if ((prtype & DATA_NOT_NULL) && dfield_is_null(new_dfield)) { |
| 1293 |
err = DB_DATA_MISMATCH; |
| 1294 |
break; |
| 1295 |
} |
| 1296 |
|
| 1297 |
if (dfield_get_len(new_dfield) != dfield_get_len(old_dfield) || |
| 1298 |
(!dfield_is_null(old_dfield) && |
| 1299 |
memcmp(dfield_get_data(new_dfield), dfield_get_data(old_dfield), |
| 1300 |
dfield_get_len(old_dfield)) != 0)) { |
| 1301 |
upd_field = &upd->fields[n_changed]; |
| 1302 |
|
| 1303 |
ib_update_col(cursor, upd_field, i, new_dfield); |
| 1304 |
|
| 1305 |
++n_changed; |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
if (err == DB_SUCCESS) { |
| 1310 |
upd->info_bits = 0; |
| 1311 |
upd->n_fields = n_changed; |
| 1312 |
} |
| 1313 |
|
| 1314 |
return (err); |
| 1315 |
} |
| 1316 |
|
| 1317 |
/** Run the update query and do error handling. |
| 1318 |
@return DB_SUCCESS or error code */ |
| 1319 |
static inline ib_err_t ib_update_row_with_lock_retry( |
| 1320 |
que_thr_t *thr, /*!< in: Update query graph */ |
| 1321 |
upd_node_t *node, /*!< in: Update node for the query */ |
| 1322 |
trx_savept_t *savept) /*!< in: savepoint to rollback to |
| 1323 |
in case of an error */ |
| 1324 |
|
| 1325 |
{ |
| 1326 |
trx_t *trx; |
| 1327 |
ib_err_t err; |
| 1328 |
bool lock_wait; |
| 1329 |
|
| 1330 |
bool is_sdi = dict_table_is_sdi(node->table->id); |
| 1331 |
|
| 1332 |
trx = thr_get_trx(thr); |
| 1333 |
|
| 1334 |
do { |
| 1335 |
thr->run_node = node; |
| 1336 |
thr->prev_node = node; |
| 1337 |
|
| 1338 |
row_upd_step(thr); |
| 1339 |
|
| 1340 |
err = trx->error_state; |
| 1341 |
|
| 1342 |
if (err != DB_SUCCESS) { |
| 1343 |
que_thr_stop_for_mysql(thr); |
| 1344 |
|
| 1345 |
if (err != DB_RECORD_NOT_FOUND) { |
| 1346 |
thr->lock_state = QUE_THR_LOCK_ROW; |
| 1347 |
|
| 1348 |
lock_wait = ib_handle_errors(&err, trx, thr, savept, is_sdi); |
| 1349 |
|
| 1350 |
thr->lock_state = QUE_THR_LOCK_NOLOCK; |
| 1351 |
} else { |
| 1352 |
lock_wait = false; |
| 1353 |
} |
| 1354 |
} else { |
| 1355 |
lock_wait = false; |
| 1356 |
} |
| 1357 |
} while (lock_wait); |
| 1358 |
|
| 1359 |
return (err); |
| 1360 |
} |
| 1361 |
|
| 1362 |
/** Does an update or delete of a row. |
| 1363 |
@return DB_SUCCESS or err code */ |
| 1364 |
static inline ib_err_t ib_execute_update_query_graph( |
| 1365 |
ib_cursor_t *cursor, /*!< in: Cursor instance */ |
| 1366 |
btr_pcur_t *pcur) /*!< in: Btree persistent cursor */ |
| 1367 |
{ |
| 1368 |
ib_err_t err; |
| 1369 |
que_thr_t *thr; |
| 1370 |
upd_node_t *node; |
| 1371 |
trx_savept_t savept; |
| 1372 |
trx_t *trx = cursor->prebuilt->trx; |
| 1373 |
dict_table_t *table = cursor->prebuilt->table; |
| 1374 |
ib_qry_proc_t *q_proc = &cursor->q_proc; |
| 1375 |
|
| 1376 |
/* The transaction must be running. */ |
| 1377 |
ut_a(trx_is_started(trx)); |
| 1378 |
|
| 1379 |
node = q_proc->node.upd; |
| 1380 |
|
| 1381 |
ut_a(pcur->m_btr_cur.index->is_clustered()); |
| 1382 |
btr_pcur_t::copy_stored_position(node->pcur, pcur); |
| 1383 |
|
| 1384 |
ut_a(node->pcur->m_rel_pos == BTR_PCUR_ON); |
| 1385 |
|
| 1386 |
savept = trx_savept_take(trx); |
| 1387 |
|
| 1388 |
thr = que_fork_get_first_thr(q_proc->grph.upd); |
| 1389 |
|
| 1390 |
node->state = UPD_NODE_UPDATE_CLUSTERED; |
| 1391 |
|
| 1392 |
que_thr_move_to_run_state_for_mysql(thr, trx); |
| 1393 |
|
| 1394 |
err = ib_update_row_with_lock_retry(thr, node, &savept); |
| 1395 |
|
| 1396 |
if (err == DB_SUCCESS) { |
| 1397 |
que_thr_stop_for_mysql_no_error(thr, trx); |
| 1398 |
|
| 1399 |
if (node->is_delete) { |
| 1400 |
dict_table_n_rows_dec(table); |
| 1401 |
|
| 1402 |
if (table->is_system_table) { |
| 1403 |
srv_stats.n_system_rows_deleted.inc(); |
| 1404 |
} else { |
| 1405 |
srv_stats.n_rows_deleted.inc(); |
| 1406 |
} |
| 1407 |
} else { |
| 1408 |
if (table->is_system_table) { |
| 1409 |
srv_stats.n_system_rows_updated.inc(); |
| 1410 |
} else { |
| 1411 |
srv_stats.n_rows_updated.inc(); |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
} else if (err == DB_RECORD_NOT_FOUND) { |
| 1416 |
trx->error_state = DB_SUCCESS; |
| 1417 |
} |
| 1418 |
|
| 1419 |
trx->op_info = ""; |
| 1420 |
|
| 1421 |
return (err); |
| 1422 |
} |
| 1423 |
|
| 1424 |
/** Update a row in a table. |
| 1425 |
@return DB_SUCCESS or err code */ |
| 1426 |
ib_err_t ib_cursor_update_row( |
| 1427 |
ib_crsr_t ib_crsr, /*!< in: InnoDB cursor instance */ |
| 1428 |
const ib_tpl_t ib_old_tpl, /*!< in: Old tuple in table */ |
| 1429 |
const ib_tpl_t ib_new_tpl) /*!< in: New tuple to update */ |
| 1430 |
{ |
| 1431 |
upd_t *upd; |
| 1432 |
ib_err_t err; |
| 1433 |
btr_pcur_t *pcur; |
| 1434 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 1435 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 1436 |
const ib_tuple_t *old_tuple = (const ib_tuple_t *)ib_old_tpl; |
| 1437 |
const ib_tuple_t *new_tuple = (const ib_tuple_t *)ib_new_tpl; |
| 1438 |
|
| 1439 |
if (prebuilt->index->is_clustered()) { |
| 1440 |
pcur = cursor->prebuilt->pcur; |
| 1441 |
} else if (prebuilt->need_to_access_clustered) { |
| 1442 |
pcur = cursor->prebuilt->clust_pcur; |
| 1443 |
} else { |
| 1444 |
return (DB_ERROR); |
| 1445 |
} |
| 1446 |
|
| 1447 |
ut_a(old_tuple->type == TPL_TYPE_ROW); |
| 1448 |
ut_a(new_tuple->type == TPL_TYPE_ROW); |
| 1449 |
|
| 1450 |
upd = ib_update_vector_create(cursor); |
| 1451 |
|
| 1452 |
err = ib_calc_diff(cursor, upd, old_tuple, new_tuple); |
| 1453 |
|
| 1454 |
if (err == DB_SUCCESS) { |
| 1455 |
/* Note that this is not a delete. */ |
| 1456 |
cursor->q_proc.node.upd->is_delete = false; |
| 1457 |
|
| 1458 |
err = ib_execute_update_query_graph(cursor, pcur); |
| 1459 |
} |
| 1460 |
|
| 1461 |
ib_wake_master_thread(); |
| 1462 |
|
| 1463 |
return (err); |
| 1464 |
} |
| 1465 |
|
| 1466 |
/** Build the update query graph to delete a row from an index. |
| 1467 |
@return DB_SUCCESS or err code */ |
| 1468 |
static ib_err_t ib_delete_row( |
| 1469 |
ib_cursor_t *cursor, /*!< in: current cursor */ |
| 1470 |
btr_pcur_t *pcur, /*!< in: Btree persistent cursor */ |
| 1471 |
const rec_t *rec) /*!< in: record to delete */ |
| 1472 |
{ |
| 1473 |
ulint i; |
| 1474 |
upd_t *upd; |
| 1475 |
ib_err_t err; |
| 1476 |
ib_tuple_t *tuple; |
| 1477 |
ib_tpl_t ib_tpl; |
| 1478 |
ulint n_cols; |
| 1479 |
upd_field_t *upd_field; |
| 1480 |
dict_table_t *table = cursor->prebuilt->table; |
| 1481 |
dict_index_t *index = table->first_index(); |
| 1482 |
|
| 1483 |
n_cols = dict_index_get_n_ordering_defined_by_user(index); |
| 1484 |
ib_tpl = ib_key_tuple_new(index, n_cols); |
| 1485 |
|
| 1486 |
if (!ib_tpl) { |
| 1487 |
return (DB_OUT_OF_MEMORY); |
| 1488 |
} |
| 1489 |
|
| 1490 |
tuple = (ib_tuple_t *)ib_tpl; |
| 1491 |
|
| 1492 |
upd = ib_update_vector_create(cursor); |
| 1493 |
|
| 1494 |
bool page_format = dict_table_is_comp(index->table); |
| 1495 |
|
| 1496 |
ib_read_tuple(rec, page_format, tuple, nullptr, 0, nullptr, nullptr, nullptr); |
| 1497 |
|
| 1498 |
upd->n_fields = ib_tuple_get_n_cols(ib_tpl); |
| 1499 |
|
| 1500 |
for (i = 0; i < upd->n_fields; ++i) { |
| 1501 |
dfield_t *dfield; |
| 1502 |
|
| 1503 |
upd_field = &upd->fields[i]; |
| 1504 |
dfield = dtuple_get_nth_field(tuple->ptr, i); |
| 1505 |
|
| 1506 |
dfield_copy_data(&upd_field->new_val, dfield); |
| 1507 |
|
| 1508 |
upd_field->exp = nullptr; |
| 1509 |
|
| 1510 |
upd_field->orig_len = 0; |
| 1511 |
|
| 1512 |
upd->info_bits = 0; |
| 1513 |
|
| 1514 |
upd_field->field_no = dict_col_get_clust_pos(&table->cols[i], index); |
| 1515 |
} |
| 1516 |
|
| 1517 |
/* Note that this is a delete. */ |
| 1518 |
cursor->q_proc.node.upd->is_delete = true; |
| 1519 |
|
| 1520 |
err = ib_execute_update_query_graph(cursor, pcur); |
| 1521 |
|
| 1522 |
ib_tuple_delete(ib_tpl); |
| 1523 |
|
| 1524 |
return (err); |
| 1525 |
} |
| 1526 |
|
| 1527 |
/** Delete a row in a table. |
| 1528 |
@return DB_SUCCESS or err code */ |
| 1529 |
ib_err_t ib_cursor_delete_row( |
| 1530 |
ib_crsr_t ib_crsr) /*!< in: InnoDB cursor instance */ |
| 1531 |
{ |
| 1532 |
ib_err_t err; |
| 1533 |
btr_pcur_t *pcur; |
| 1534 |
dict_index_t *index; |
| 1535 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 1536 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 1537 |
|
| 1538 |
index = prebuilt->index->table->first_index(); |
| 1539 |
|
| 1540 |
/* Check whether this is a secondary index cursor */ |
| 1541 |
if (index != prebuilt->index) { |
| 1542 |
if (prebuilt->need_to_access_clustered) { |
| 1543 |
pcur = prebuilt->clust_pcur; |
| 1544 |
} else { |
| 1545 |
return (DB_ERROR); |
| 1546 |
} |
| 1547 |
} else { |
| 1548 |
pcur = prebuilt->pcur; |
| 1549 |
} |
| 1550 |
|
| 1551 |
if (ib_btr_cursor_is_positioned(pcur)) { |
| 1552 |
const rec_t *rec; |
| 1553 |
mtr_t mtr; |
| 1554 |
rec_t *copy = nullptr; |
| 1555 |
byte ptr[UNIV_PAGE_SIZE_MAX]; |
| 1556 |
|
| 1557 |
bool page_format = dict_table_is_comp(index->table); |
| 1558 |
|
| 1559 |
mtr_start(&mtr); |
| 1560 |
|
| 1561 |
if (pcur->restore_position(BTR_SEARCH_LEAF, &mtr, UT_LOCATION_HERE)) { |
| 1562 |
mem_heap_t *heap = nullptr; |
| 1563 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 1564 |
ulint *offsets = offsets_; |
| 1565 |
|
| 1566 |
rec_offs_init(offsets_); |
| 1567 |
|
| 1568 |
rec = pcur->get_rec(); |
| 1569 |
|
| 1570 |
/* Since mtr will be committed, the rec |
| 1571 |
will not be protected. Make a copy of |
| 1572 |
the rec. */ |
| 1573 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 1574 |
UT_LOCATION_HERE, &heap); |
| 1575 |
ut_ad(rec_offs_size(offsets) < UNIV_PAGE_SIZE_MAX); |
| 1576 |
copy = rec_copy(ptr, rec, offsets); |
| 1577 |
} |
| 1578 |
|
| 1579 |
mtr_commit(&mtr); |
| 1580 |
|
| 1581 |
if (copy && !rec_get_deleted_flag(copy, page_format)) { |
| 1582 |
err = ib_delete_row(cursor, pcur, copy); |
| 1583 |
} else { |
| 1584 |
err = DB_RECORD_NOT_FOUND; |
| 1585 |
} |
| 1586 |
} else { |
| 1587 |
err = DB_RECORD_NOT_FOUND; |
| 1588 |
} |
| 1589 |
|
| 1590 |
ib_wake_master_thread(); |
| 1591 |
|
| 1592 |
return (err); |
| 1593 |
} |
| 1594 |
|
| 1595 |
/** Read current row. |
| 1596 |
@return DB_SUCCESS or err code */ |
| 1597 |
ib_err_t ib_cursor_read_row( |
| 1598 |
ib_crsr_t ib_crsr, /*!< in: InnoDB cursor instance */ |
| 1599 |
ib_tpl_t ib_tpl, /*!< out: read cols into this tuple */ |
| 1600 |
ib_tpl_t cmp_tpl, /*!< in: tuple to compare and stop |
| 1601 |
reading */ |
| 1602 |
int mode, /*!< in: mode determine when to |
| 1603 |
stop read */ |
| 1604 |
void **row_buf, /*!< in/out: row buffer */ |
| 1605 |
uint64_t *slot, /*!< in/out: slot being used */ |
| 1606 |
uint64_t *used_len) /*!< in/out: buffer len used */ |
| 1607 |
{ |
| 1608 |
ib_err_t err; |
| 1609 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 1610 |
ib_tuple_t *cmp_tuple = (ib_tuple_t *)cmp_tpl; |
| 1611 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 1612 |
|
| 1613 |
ut_a(trx_is_started(cursor->prebuilt->trx)); |
| 1614 |
|
| 1615 |
/* When searching with IB_EXACT_MATCH set, row_search_for_mysql() |
| 1616 |
will not position the persistent cursor but will copy the record |
| 1617 |
found into the row cache. It should be the only entry. */ |
| 1618 |
if (!ib_cursor_is_positioned(ib_crsr)) { |
| 1619 |
err = DB_RECORD_NOT_FOUND; |
| 1620 |
} else { |
| 1621 |
mtr_t mtr; |
| 1622 |
btr_pcur_t *pcur; |
| 1623 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 1624 |
|
| 1625 |
if (prebuilt->need_to_access_clustered && tuple->type == TPL_TYPE_ROW) { |
| 1626 |
pcur = prebuilt->clust_pcur; |
| 1627 |
} else { |
| 1628 |
pcur = prebuilt->pcur; |
| 1629 |
} |
| 1630 |
|
| 1631 |
if (pcur == nullptr) { |
| 1632 |
return (DB_ERROR); |
| 1633 |
} |
| 1634 |
|
| 1635 |
mtr_start(&mtr); |
| 1636 |
|
| 1637 |
if (pcur->restore_position(BTR_SEARCH_LEAF, &mtr, UT_LOCATION_HERE)) { |
| 1638 |
const rec_t *rec; |
| 1639 |
|
| 1640 |
bool page_format = dict_table_is_comp(tuple->index->table); |
| 1641 |
rec = pcur->get_rec(); |
| 1642 |
|
| 1643 |
if (!rec_get_deleted_flag(rec, page_format)) { |
| 1644 |
if (prebuilt->innodb_api && prebuilt->innodb_api_rec != nullptr) { |
| 1645 |
rec = prebuilt->innodb_api_rec; |
| 1646 |
} |
| 1647 |
} |
| 1648 |
|
| 1649 |
if (!rec_get_deleted_flag(rec, page_format)) { |
| 1650 |
err = ib_read_tuple(rec, page_format, tuple, cmp_tuple, mode, row_buf, |
| 1651 |
(ulint *)slot, (ulint *)used_len); |
| 1652 |
} else { |
| 1653 |
err = DB_RECORD_NOT_FOUND; |
| 1654 |
} |
| 1655 |
|
| 1656 |
} else { |
| 1657 |
err = DB_RECORD_NOT_FOUND; |
| 1658 |
} |
| 1659 |
|
| 1660 |
mtr_commit(&mtr); |
| 1661 |
} |
| 1662 |
|
| 1663 |
return (err); |
| 1664 |
} |
| 1665 |
|
| 1666 |
/** Move cursor to the first record in the table. |
| 1667 |
@return DB_SUCCESS or err code */ |
| 1668 |
static inline ib_err_t ib_cursor_position( |
| 1669 |
ib_cursor_t *cursor, /*!< in: InnoDB cursor instance */ |
| 1670 |
ib_srch_mode_t mode) /*!< in: Search mode */ |
| 1671 |
{ |
| 1672 |
ib_err_t err; |
| 1673 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 1674 |
unsigned char *buf; |
| 1675 |
|
| 1676 |
if (prebuilt->innodb_api) { |
| 1677 |
prebuilt->cursor_heap = cursor->heap; |
| 1678 |
} |
| 1679 |
buf = static_cast<unsigned char *>( |
| 1680 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, UNIV_PAGE_SIZE)); |
| 1681 |
prebuilt->clear_search_tuples(); |
| 1682 |
|
| 1683 |
/* We want to position at one of the ends, row_search_for_mysql() |
| 1684 |
uses the search_tuple fields to work out what to do. */ |
| 1685 |
|
| 1686 |
err = static_cast<ib_err_t>(row_search_for_mysql( |
| 1687 |
buf, static_cast<page_cur_mode_t>(mode), prebuilt, 0, 0)); |
| 1688 |
|
| 1689 |
ut::free(buf); |
| 1690 |
|
| 1691 |
return (err); |
| 1692 |
} |
| 1693 |
|
| 1694 |
/** Move cursor to the first record in the table. |
| 1695 |
@return DB_SUCCESS or err code */ |
| 1696 |
ib_err_t ib_cursor_first(ib_crsr_t ib_crsr) /*!< in: InnoDB cursor instance */ |
| 1697 |
{ |
| 1698 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 1699 |
|
| 1700 |
return (ib_cursor_position(cursor, IB_CUR_G)); |
| 1701 |
} |
| 1702 |
|
| 1703 |
/** Move cursor to the next user record in the table. |
| 1704 |
@return DB_SUCCESS or err code */ |
| 1705 |
ib_err_t ib_cursor_next(ib_crsr_t ib_crsr) /*!< in: InnoDB cursor instance */ |
| 1706 |
{ |
| 1707 |
ib_err_t err; |
| 1708 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 1709 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 1710 |
byte buf[UNIV_PAGE_SIZE_MAX]; |
| 1711 |
|
| 1712 |
if (prebuilt->innodb_api) { |
| 1713 |
prebuilt->cursor_heap = cursor->heap; |
| 1714 |
} |
| 1715 |
/* We want to move to the next record */ |
| 1716 |
prebuilt->clear_search_tuples(); |
| 1717 |
|
| 1718 |
err = static_cast<ib_err_t>( |
| 1719 |
row_search_for_mysql(buf, PAGE_CUR_G, prebuilt, 0, ROW_SEL_NEXT)); |
| 1720 |
|
| 1721 |
return (err); |
| 1722 |
} |
| 1723 |
|
| 1724 |
/** Search for key. |
| 1725 |
@return DB_SUCCESS or err code */ |
| 1726 |
ib_err_t ib_cursor_moveto(ib_crsr_t ib_crsr, /*!< in: InnoDB cursor instance */ |
| 1727 |
ib_tpl_t ib_tpl, /*!< in: Key to search for */ |
| 1728 |
ib_srch_mode_t ib_srch_mode, /*!< in: search mode */ |
| 1729 |
uint64_t direction) /*!< in: search direction */ |
| 1730 |
{ |
| 1731 |
ulint i; |
| 1732 |
ulint n_fields; |
| 1733 |
ib_err_t err = DB_SUCCESS; |
| 1734 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 1735 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 1736 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 1737 |
dtuple_t *search_tuple = prebuilt->search_tuple; |
| 1738 |
unsigned char *buf; |
| 1739 |
|
| 1740 |
ut_a(tuple->type == TPL_TYPE_KEY); |
| 1741 |
|
| 1742 |
n_fields = dict_index_get_n_ordering_defined_by_user(prebuilt->index); |
| 1743 |
|
| 1744 |
if (n_fields > dtuple_get_n_fields(tuple->ptr)) { |
| 1745 |
n_fields = dtuple_get_n_fields(tuple->ptr); |
| 1746 |
} |
| 1747 |
|
| 1748 |
dtuple_set_n_fields(prebuilt->m_stop_tuple, 0); |
| 1749 |
dtuple_set_n_fields(search_tuple, n_fields); |
| 1750 |
dtuple_set_n_fields_cmp(search_tuple, n_fields); |
| 1751 |
|
| 1752 |
/* Do a shallow copy */ |
| 1753 |
for (i = 0; i < n_fields; ++i) { |
| 1754 |
dfield_copy(dtuple_get_nth_field(search_tuple, i), |
| 1755 |
dtuple_get_nth_field(tuple->ptr, i)); |
| 1756 |
} |
| 1757 |
|
| 1758 |
ut_a(prebuilt->select_lock_type <= LOCK_NUM); |
| 1759 |
|
| 1760 |
prebuilt->innodb_api_rec = nullptr; |
| 1761 |
|
| 1762 |
buf = static_cast<unsigned char *>( |
| 1763 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, UNIV_PAGE_SIZE)); |
| 1764 |
|
| 1765 |
if (prebuilt->innodb_api) { |
| 1766 |
prebuilt->cursor_heap = cursor->heap; |
| 1767 |
} |
| 1768 |
err = static_cast<ib_err_t>( |
| 1769 |
row_search_for_mysql(buf, static_cast<page_cur_mode_t>(ib_srch_mode), |
| 1770 |
prebuilt, cursor->match_mode, direction)); |
| 1771 |
|
| 1772 |
ut::free(buf); |
| 1773 |
|
| 1774 |
return (err); |
| 1775 |
} |
| 1776 |
|
| 1777 |
/** Set the cursor search mode. */ |
| 1778 |
void ib_cursor_set_match_mode( |
| 1779 |
ib_crsr_t ib_crsr, /*!< in: Cursor instance */ |
| 1780 |
ib_match_mode_t match_mode) /*!< in: ib_cursor_moveto match mode */ |
| 1781 |
{ |
| 1782 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 1783 |
|
| 1784 |
cursor->match_mode = match_mode; |
| 1785 |
} |
| 1786 |
|
| 1787 |
/** Get the dfield instance for the column in the tuple. |
| 1788 |
@return dfield instance in tuple */ |
| 1789 |
static inline dfield_t *ib_col_get_dfield( |
| 1790 |
ib_tuple_t *tuple, /*!< in: tuple instance */ |
| 1791 |
ulint col_no) /*!< in: col no. in tuple */ |
| 1792 |
{ |
| 1793 |
dfield_t *dfield; |
| 1794 |
|
| 1795 |
dfield = dtuple_get_nth_field(tuple->ptr, col_no); |
| 1796 |
|
| 1797 |
return (dfield); |
| 1798 |
} |
| 1799 |
|
| 1800 |
/** Predicate to check whether a column type contains variable length data. |
| 1801 |
@return DB_SUCCESS or error code */ |
| 1802 |
static inline ib_err_t ib_col_is_capped( |
| 1803 |
const dtype_t *dtype) /*!< in: column type */ |
| 1804 |
{ |
| 1805 |
return (static_cast<ib_err_t>((dtype_get_mtype(dtype) == DATA_VARCHAR || |
| 1806 |
dtype_get_mtype(dtype) == DATA_CHAR || |
| 1807 |
dtype_get_mtype(dtype) == DATA_MYSQL || |
| 1808 |
dtype_get_mtype(dtype) == DATA_VARMYSQL || |
| 1809 |
dtype_get_mtype(dtype) == DATA_FIXBINARY || |
| 1810 |
dtype_get_mtype(dtype) == DATA_BINARY || |
| 1811 |
dtype_get_mtype(dtype) == DATA_POINT) && |
| 1812 |
dtype_get_len(dtype) > 0)); |
| 1813 |
} |
| 1814 |
|
| 1815 |
ib_err_t ib_col_set_value(ib_tpl_t ib_tpl, ib_ulint_t col_no, const void *src, |
| 1816 |
uint64_t len, bool need_cpy) { |
| 1817 |
const dtype_t *dtype; |
| 1818 |
dfield_t *dfield; |
| 1819 |
void *dst = nullptr; |
| 1820 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 1821 |
ulint col_len; |
| 1822 |
|
| 1823 |
dfield = ib_col_get_dfield(tuple, col_no); |
| 1824 |
|
| 1825 |
/* User wants to set the column to NULL. */ |
| 1826 |
if (len == IB_SQL_NULL) { |
| 1827 |
dfield_set_null(dfield); |
| 1828 |
return (DB_SUCCESS); |
| 1829 |
} |
| 1830 |
|
| 1831 |
dtype = dfield_get_type(dfield); |
| 1832 |
col_len = dtype_get_len(dtype); |
| 1833 |
|
| 1834 |
/* Not allowed to update system columns. */ |
| 1835 |
if (dtype_get_mtype(dtype) == DATA_SYS) { |
| 1836 |
return (DB_DATA_MISMATCH); |
| 1837 |
} |
| 1838 |
|
| 1839 |
dst = dfield_get_data(dfield); |
| 1840 |
|
| 1841 |
/* Since TEXT/CLOB also map to DATA_VARCHAR we need to make an |
| 1842 |
exception. Perhaps we need to set the precise type and check |
| 1843 |
for that. */ |
| 1844 |
if (ib_col_is_capped(dtype)) { |
| 1845 |
len = std::min(len, static_cast<uint64_t>(col_len)); |
| 1846 |
|
| 1847 |
if (dst == nullptr || len > dfield_get_len(dfield)) { |
| 1848 |
dst = mem_heap_alloc(tuple->heap, col_len); |
| 1849 |
ut_a(dst != nullptr); |
| 1850 |
} |
| 1851 |
} else if (dst == nullptr || len > dfield_get_len(dfield)) { |
| 1852 |
dst = mem_heap_alloc(tuple->heap, len); |
| 1853 |
} |
| 1854 |
|
| 1855 |
if (dst == nullptr) { |
| 1856 |
return (DB_OUT_OF_MEMORY); |
| 1857 |
} |
| 1858 |
|
| 1859 |
switch (dtype_get_mtype(dtype)) { |
| 1860 |
case DATA_INT: { |
| 1861 |
if (col_len == len) { |
| 1862 |
bool usign = (dtype_get_prtype(dtype) & DATA_UNSIGNED) != 0; |
| 1863 |
mach_write_int_type(static_cast<byte *>(dst), |
| 1864 |
static_cast<const byte *>(src), len, usign); |
| 1865 |
|
| 1866 |
} else { |
| 1867 |
return (DB_DATA_MISMATCH); |
| 1868 |
} |
| 1869 |
break; |
| 1870 |
} |
| 1871 |
|
| 1872 |
case DATA_FLOAT: |
| 1873 |
if (len == sizeof(float)) { |
| 1874 |
mach_float_write(static_cast<byte *>(dst), *(float *)src); |
| 1875 |
} else { |
| 1876 |
return (DB_DATA_MISMATCH); |
| 1877 |
} |
| 1878 |
break; |
| 1879 |
|
| 1880 |
case DATA_DOUBLE: |
| 1881 |
if (len == sizeof(double)) { |
| 1882 |
mach_double_write(static_cast<byte *>(dst), *(double *)src); |
| 1883 |
} else { |
| 1884 |
return (DB_DATA_MISMATCH); |
| 1885 |
} |
| 1886 |
break; |
| 1887 |
|
| 1888 |
case DATA_SYS: |
| 1889 |
ut_error; |
| 1890 |
break; |
| 1891 |
|
| 1892 |
case DATA_CHAR: |
| 1893 |
memcpy(dst, src, len); |
| 1894 |
memset((byte *)dst + len, 0x20, col_len - len); |
| 1895 |
len = col_len; |
| 1896 |
break; |
| 1897 |
|
| 1898 |
case DATA_POINT: |
| 1899 |
memcpy(dst, src, len); |
| 1900 |
break; |
| 1901 |
|
| 1902 |
case DATA_BLOB: |
| 1903 |
case DATA_VAR_POINT: |
| 1904 |
case DATA_GEOMETRY: |
| 1905 |
case DATA_BINARY: |
| 1906 |
case DATA_DECIMAL: |
| 1907 |
case DATA_VARCHAR: |
| 1908 |
case DATA_FIXBINARY: |
| 1909 |
if (need_cpy) { |
| 1910 |
memcpy(dst, src, len); |
| 1911 |
} else { |
| 1912 |
dfield_set_data(dfield, src, len); |
| 1913 |
dst = dfield_get_data(dfield); |
| 1914 |
} |
| 1915 |
break; |
| 1916 |
|
| 1917 |
case DATA_MYSQL: |
| 1918 |
case DATA_VARMYSQL: { |
| 1919 |
ulint cset; |
| 1920 |
CHARSET_INFO *cs; |
| 1921 |
int error = 0; |
| 1922 |
ulint true_len = len; |
| 1923 |
|
| 1924 |
/* For multi byte character sets we need to |
| 1925 |
calculate the true length of the data. */ |
| 1926 |
cset = dtype_get_charset_coll(dtype_get_prtype(dtype)); |
| 1927 |
cs = all_charsets[cset]; |
| 1928 |
if (cs) { |
| 1929 |
uint pos = (uint)(col_len / cs->mbmaxlen); |
| 1930 |
|
| 1931 |
if (len > 0 && cs->mbmaxlen > 1) { |
| 1932 |
true_len = (ulint)cs->cset->well_formed_len( |
| 1933 |
cs, (const char *)src, (const char *)src + len, pos, &error); |
| 1934 |
|
| 1935 |
if (true_len < len) { |
| 1936 |
len = true_len; |
| 1937 |
} |
| 1938 |
} |
| 1939 |
} |
| 1940 |
|
| 1941 |
/* All invalid bytes in data need be truncated. |
| 1942 |
If len == 0, means all bytes of the data is invalid. |
| 1943 |
In this case, the data will be truncated to empty.*/ |
| 1944 |
memcpy(dst, src, len); |
| 1945 |
|
| 1946 |
/* For DATA_MYSQL, need to pad the unused |
| 1947 |
space with spaces. */ |
| 1948 |
if (dtype_get_mtype(dtype) == DATA_MYSQL) { |
| 1949 |
ulint n_chars; |
| 1950 |
|
| 1951 |
if (len < col_len) { |
| 1952 |
ulint pad_len = col_len - len; |
| 1953 |
|
| 1954 |
ut_a(cs != nullptr); |
| 1955 |
ut_a(!(pad_len % cs->mbminlen)); |
| 1956 |
|
| 1957 |
cs->cset->fill(cs, (char *)dst + len, pad_len, 0x20 /* space */); |
| 1958 |
} |
| 1959 |
|
| 1960 |
/* Why we should do below? See function |
| 1961 |
row_mysql_store_col_in_innobase_format */ |
| 1962 |
|
| 1963 |
ut_a(!(dtype_get_len(dtype) % dtype_get_mbmaxlen(dtype))); |
| 1964 |
|
| 1965 |
n_chars = dtype_get_len(dtype) / dtype_get_mbmaxlen(dtype); |
| 1966 |
|
| 1967 |
/* Strip space padding. */ |
| 1968 |
while (col_len > n_chars && ((char *)dst)[col_len - 1] == 0x20) { |
| 1969 |
col_len--; |
| 1970 |
} |
| 1971 |
|
| 1972 |
len = col_len; |
| 1973 |
} |
| 1974 |
break; |
| 1975 |
} |
| 1976 |
|
| 1977 |
default: |
| 1978 |
ut_error; |
| 1979 |
} |
| 1980 |
|
| 1981 |
if (dst != dfield_get_data(dfield)) { |
| 1982 |
dfield_set_data(dfield, dst, len); |
| 1983 |
} else { |
| 1984 |
dfield_set_len(dfield, len); |
| 1985 |
} |
| 1986 |
|
| 1987 |
return (DB_SUCCESS); |
| 1988 |
} |
| 1989 |
|
| 1990 |
uint64_t ib_col_get_len(ib_tpl_t ib_tpl, ib_ulint_t i) { |
| 1991 |
const dfield_t *dfield; |
| 1992 |
ulint data_len; |
| 1993 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 1994 |
|
| 1995 |
dfield = ib_col_get_dfield(tuple, i); |
| 1996 |
|
| 1997 |
data_len = dfield_get_len(dfield); |
| 1998 |
|
| 1999 |
return data_len == UNIV_SQL_NULL ? IB_SQL_NULL : data_len; |
| 2000 |
} |
| 2001 |
|
| 2002 |
uint64_t ib_col_copy_value(ib_tpl_t ib_tpl, ib_ulint_t i, void *dst, |
| 2003 |
uint32_t len) { |
| 2004 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 2005 |
|
| 2006 |
const auto dfield = ib_col_get_dfield(tuple, i); |
| 2007 |
|
| 2008 |
const auto data = dfield_get_data(dfield); |
| 2009 |
auto data_len = dfield_get_len(dfield); |
| 2010 |
|
| 2011 |
if (data_len != UNIV_SQL_NULL) { |
| 2012 |
const dtype_t *dtype = dfield_get_type(dfield); |
| 2013 |
|
| 2014 |
switch (dtype_get_mtype(dfield_get_type(dfield))) { |
| 2015 |
case DATA_INT: { |
| 2016 |
ut_a(data_len == len); |
| 2017 |
|
| 2018 |
bool usign = (dtype_get_prtype(dtype) & DATA_UNSIGNED) != 0; |
| 2019 |
auto ret = mach_read_int_type(static_cast<const byte *>(data), data_len, |
| 2020 |
usign); |
| 2021 |
|
| 2022 |
if (usign) { |
| 2023 |
if (len == 1) { |
| 2024 |
*(int8_t *)dst = (int8_t)ret; |
| 2025 |
} else if (len == 2) { |
| 2026 |
*(int16_t *)dst = (int16_t)ret; |
| 2027 |
} else if (len == 4) { |
| 2028 |
*(int32_t *)dst = (int32_t)ret; |
| 2029 |
} else { |
| 2030 |
*(int64_t *)dst = (int64_t)ret; |
| 2031 |
} |
| 2032 |
} else { |
| 2033 |
if (len == 1) { |
| 2034 |
*(uint8_t *)dst = (int8_t)ret; |
| 2035 |
} else if (len == 2) { |
| 2036 |
*(uint16_t *)dst = (int16_t)ret; |
| 2037 |
} else if (len == 4) { |
| 2038 |
*(uint32_t *)dst = (int32_t)ret; |
| 2039 |
} else { |
| 2040 |
*(uint64_t *)dst = (int64_t)ret; |
| 2041 |
} |
| 2042 |
} |
| 2043 |
|
| 2044 |
break; |
| 2045 |
} |
| 2046 |
case DATA_FLOAT: |
| 2047 |
if (len == data_len) { |
| 2048 |
float f; |
| 2049 |
|
| 2050 |
ut_a(data_len == sizeof(f)); |
| 2051 |
f = mach_float_read(static_cast<const byte *>(data)); |
| 2052 |
memcpy(dst, &f, sizeof(f)); |
| 2053 |
} else { |
| 2054 |
data_len = 0; |
| 2055 |
} |
| 2056 |
break; |
| 2057 |
case DATA_DOUBLE: |
| 2058 |
if (len == data_len) { |
| 2059 |
double d; |
| 2060 |
|
| 2061 |
ut_a(data_len == sizeof(d)); |
| 2062 |
d = mach_double_read(static_cast<const byte *>(data)); |
| 2063 |
memcpy(dst, &d, sizeof(d)); |
| 2064 |
} else { |
| 2065 |
data_len = 0; |
| 2066 |
} |
| 2067 |
break; |
| 2068 |
default: |
| 2069 |
data_len = std::min(data_len, len); |
| 2070 |
memcpy(dst, data, data_len); |
| 2071 |
} |
| 2072 |
} else { |
| 2073 |
data_len = IB_SQL_NULL; |
| 2074 |
} |
| 2075 |
|
| 2076 |
return data_len; |
| 2077 |
} |
| 2078 |
|
| 2079 |
/** Get the InnoDB column attribute from the internal column precise type. |
| 2080 |
@return precise type in api format */ |
| 2081 |
static inline ib_col_attr_t ib_col_get_attr( |
| 2082 |
ulint prtype) /*!< in: column definition */ |
| 2083 |
{ |
| 2084 |
ib_col_attr_t attr = IB_COL_NONE; |
| 2085 |
|
| 2086 |
if (prtype & DATA_UNSIGNED) { |
| 2087 |
attr = static_cast<ib_col_attr_t>(attr | IB_COL_UNSIGNED); |
| 2088 |
} |
| 2089 |
|
| 2090 |
if (prtype & DATA_NOT_NULL) { |
| 2091 |
attr = static_cast<ib_col_attr_t>(attr | IB_COL_NOT_NULL); |
| 2092 |
} |
| 2093 |
|
| 2094 |
return (attr); |
| 2095 |
} |
| 2096 |
|
| 2097 |
const char *ib_col_get_name(ib_crsr_t ib_crsr, ulint i) { |
| 2098 |
const char *name; |
| 2099 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 2100 |
dict_table_t *table = cursor->prebuilt->table; |
| 2101 |
dict_col_t *col = table->get_col(i); |
| 2102 |
ulint col_no = dict_col_get_no(col); |
| 2103 |
|
| 2104 |
name = table->get_col_name(col_no); |
| 2105 |
|
| 2106 |
return (name); |
| 2107 |
} |
| 2108 |
|
| 2109 |
const char *ib_get_idx_field_name(ib_crsr_t ib_crsr, ulint i) { |
| 2110 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 2111 |
dict_index_t *index = cursor->prebuilt->index; |
| 2112 |
dict_field_t *field; |
| 2113 |
|
| 2114 |
if (index) { |
| 2115 |
field = cursor->prebuilt->index->get_field(i); |
| 2116 |
|
| 2117 |
if (field) { |
| 2118 |
return (field->name); |
| 2119 |
} |
| 2120 |
} |
| 2121 |
|
| 2122 |
return (nullptr); |
| 2123 |
} |
| 2124 |
uint64_t ib_col_get_meta(ib_tpl_t ib_tpl, ulint i, ib_col_meta_t *ib_col_meta) { |
| 2125 |
uint16_t prtype; |
| 2126 |
const dfield_t *dfield; |
| 2127 |
ulint data_len; |
| 2128 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 2129 |
|
| 2130 |
dfield = ib_col_get_dfield(tuple, i); |
| 2131 |
|
| 2132 |
data_len = dfield_get_len(dfield); |
| 2133 |
|
| 2134 |
/* We assume 1-1 mapping between the ENUM and internal type codes. */ |
| 2135 |
ib_col_meta->type = |
| 2136 |
static_cast<ib_col_type_t>(dtype_get_mtype(dfield_get_type(dfield))); |
| 2137 |
|
| 2138 |
ib_col_meta->type_len = |
| 2139 |
static_cast<uint32_t>(dtype_get_len(dfield_get_type(dfield))); |
| 2140 |
|
| 2141 |
prtype = (uint16_t)dtype_get_prtype(dfield_get_type(dfield)); |
| 2142 |
|
| 2143 |
ib_col_meta->attr = ib_col_get_attr(prtype); |
| 2144 |
ib_col_meta->client_type = prtype & DATA_MYSQL_TYPE_MASK; |
| 2145 |
|
| 2146 |
return data_len; |
| 2147 |
} |
| 2148 |
|
| 2149 |
/** Read a signed int 8 bit column from an InnoDB tuple. */ |
| 2150 |
static inline ib_err_t ib_tuple_check_int( |
| 2151 |
ib_tpl_t ib_tpl, /*!< in: InnoDB tuple */ |
| 2152 |
ulint i, /*!< in: column number */ |
| 2153 |
bool usign, /*!< in: true if unsigned */ |
| 2154 |
ulint size) /*!< in: size of integer */ |
| 2155 |
{ |
| 2156 |
ib_col_meta_t ib_col_meta; |
| 2157 |
|
| 2158 |
ib_col_get_meta(ib_tpl, i, &ib_col_meta); |
| 2159 |
|
| 2160 |
if (ib_col_meta.type != IB_INT) { |
| 2161 |
return (DB_DATA_MISMATCH); |
| 2162 |
} else if (ib_col_meta.type_len == IB_SQL_NULL) { |
| 2163 |
return (DB_UNDERFLOW); |
| 2164 |
} else if (ib_col_meta.type_len != size) { |
| 2165 |
return (DB_DATA_MISMATCH); |
| 2166 |
} else if ((ib_col_meta.attr & IB_COL_UNSIGNED) && !usign) { |
| 2167 |
return (DB_DATA_MISMATCH); |
| 2168 |
} |
| 2169 |
|
| 2170 |
return (DB_SUCCESS); |
| 2171 |
} |
| 2172 |
|
| 2173 |
ib_err_t ib_tuple_read_i8(ib_tpl_t ib_tpl, ib_ulint_t i, int8_t *ival) { |
| 2174 |
ib_err_t err; |
| 2175 |
|
| 2176 |
err = ib_tuple_check_int(ib_tpl, i, false, sizeof(*ival)); |
| 2177 |
|
| 2178 |
if (err == DB_SUCCESS) { |
| 2179 |
ib_col_copy_value(ib_tpl, i, ival, sizeof(*ival)); |
| 2180 |
} |
| 2181 |
|
| 2182 |
return (err); |
| 2183 |
} |
| 2184 |
|
| 2185 |
ib_err_t ib_tuple_read_u8(ib_tpl_t ib_tpl, ib_ulint_t i, uint8_t *ival) { |
| 2186 |
ib_err_t err; |
| 2187 |
|
| 2188 |
err = ib_tuple_check_int(ib_tpl, i, true, sizeof(*ival)); |
| 2189 |
|
| 2190 |
if (err == DB_SUCCESS) { |
| 2191 |
ib_col_copy_value(ib_tpl, i, ival, sizeof(*ival)); |
| 2192 |
} |
| 2193 |
|
| 2194 |
return (err); |
| 2195 |
} |
| 2196 |
|
| 2197 |
ib_err_t ib_tuple_read_i16(ib_tpl_t ib_tpl, ib_ulint_t i, int16_t *ival) { |
| 2198 |
ib_err_t err; |
| 2199 |
|
| 2200 |
err = ib_tuple_check_int(ib_tpl, i, false, sizeof(*ival)); |
| 2201 |
|
| 2202 |
if (err == DB_SUCCESS) { |
| 2203 |
ib_col_copy_value(ib_tpl, i, ival, sizeof(*ival)); |
| 2204 |
} |
| 2205 |
|
| 2206 |
return (err); |
| 2207 |
} |
| 2208 |
|
| 2209 |
ib_err_t ib_tuple_read_u16(ib_tpl_t ib_tpl, ib_ulint_t i, uint16_t *ival) { |
| 2210 |
ib_err_t err; |
| 2211 |
|
| 2212 |
err = ib_tuple_check_int(ib_tpl, i, true, sizeof(*ival)); |
| 2213 |
|
| 2214 |
if (err == DB_SUCCESS) { |
| 2215 |
ib_col_copy_value(ib_tpl, i, ival, sizeof(*ival)); |
| 2216 |
} |
| 2217 |
|
| 2218 |
return (err); |
| 2219 |
} |
| 2220 |
|
| 2221 |
ib_err_t ib_tuple_read_i32(ib_tpl_t ib_tpl, ib_ulint_t i, int32_t *ival) { |
| 2222 |
ib_err_t err; |
| 2223 |
|
| 2224 |
err = ib_tuple_check_int(ib_tpl, i, false, sizeof(*ival)); |
| 2225 |
|
| 2226 |
if (err == DB_SUCCESS) { |
| 2227 |
ib_col_copy_value(ib_tpl, i, ival, sizeof(*ival)); |
| 2228 |
} |
| 2229 |
|
| 2230 |
return (err); |
| 2231 |
} |
| 2232 |
|
| 2233 |
ib_err_t ib_tuple_read_u32(ib_tpl_t ib_tpl, ib_ulint_t i, uint32_t *ival) { |
| 2234 |
ib_err_t err; |
| 2235 |
|
| 2236 |
err = ib_tuple_check_int(ib_tpl, i, true, sizeof(*ival)); |
| 2237 |
|
| 2238 |
if (err == DB_SUCCESS) { |
| 2239 |
ib_col_copy_value(ib_tpl, i, ival, sizeof(*ival)); |
| 2240 |
} |
| 2241 |
|
| 2242 |
return (err); |
| 2243 |
} |
| 2244 |
|
| 2245 |
ib_err_t ib_tuple_read_i64(ib_tpl_t ib_tpl, ib_ulint_t i, int64_t *ival) { |
| 2246 |
ib_err_t err; |
| 2247 |
|
| 2248 |
err = ib_tuple_check_int(ib_tpl, i, false, sizeof(*ival)); |
| 2249 |
|
| 2250 |
if (err == DB_SUCCESS) { |
| 2251 |
ib_col_copy_value(ib_tpl, i, ival, sizeof(*ival)); |
| 2252 |
} |
| 2253 |
|
| 2254 |
return (err); |
| 2255 |
} |
| 2256 |
|
| 2257 |
ib_err_t ib_tuple_read_u64(ib_tpl_t ib_tpl, ib_ulint_t i, uint64_t *ival) { |
| 2258 |
ib_err_t err; |
| 2259 |
|
| 2260 |
err = ib_tuple_check_int(ib_tpl, i, true, sizeof(*ival)); |
| 2261 |
|
| 2262 |
if (err == DB_SUCCESS) { |
| 2263 |
ib_col_copy_value(ib_tpl, i, ival, sizeof(*ival)); |
| 2264 |
} |
| 2265 |
|
| 2266 |
return (err); |
| 2267 |
} |
| 2268 |
|
| 2269 |
const void *ib_col_get_value(ib_tpl_t ib_tpl, ib_ulint_t i) { |
| 2270 |
const void *data; |
| 2271 |
const dfield_t *dfield; |
| 2272 |
ulint data_len; |
| 2273 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 2274 |
|
| 2275 |
dfield = ib_col_get_dfield(tuple, i); |
| 2276 |
|
| 2277 |
data = dfield_get_data(dfield); |
| 2278 |
data_len = dfield_get_len(dfield); |
| 2279 |
|
| 2280 |
return (data_len != UNIV_SQL_NULL ? data : nullptr); |
| 2281 |
} |
| 2282 |
|
| 2283 |
/** "Clear" or reset an InnoDB tuple. We free the heap and recreate the tuple. |
| 2284 |
@return new tuple, or NULL */ |
| 2285 |
ib_tpl_t ib_tuple_clear(ib_tpl_t ib_tpl) /*!< in,own: tuple (will be freed) */ |
| 2286 |
{ |
| 2287 |
const dict_index_t *index; |
| 2288 |
ulint n_cols; |
| 2289 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 2290 |
ib_tuple_type_t type = tuple->type; |
| 2291 |
mem_heap_t *heap = tuple->heap; |
| 2292 |
|
| 2293 |
index = tuple->index; |
| 2294 |
n_cols = dtuple_get_n_fields(tuple->ptr); |
| 2295 |
|
| 2296 |
mem_heap_empty(heap); |
| 2297 |
|
| 2298 |
if (type == TPL_TYPE_ROW) { |
| 2299 |
return (ib_row_tuple_new_low(index, n_cols, heap)); |
| 2300 |
} else { |
| 2301 |
return (ib_key_tuple_new_low(index, n_cols, heap)); |
| 2302 |
} |
| 2303 |
} |
| 2304 |
|
| 2305 |
/** Create a new cluster key search tuple and copy the contents of the |
| 2306 |
secondary index key tuple columns that refer to the cluster index record |
| 2307 |
to the cluster key. It does a deep copy of the column data. |
| 2308 |
@return DB_SUCCESS or error code */ |
| 2309 |
ib_err_t ib_tuple_get_cluster_key( |
| 2310 |
ib_crsr_t ib_crsr, /*!< in: secondary index cursor */ |
| 2311 |
ib_tpl_t *ib_dst_tpl, /*!< out,own: destination tuple */ |
| 2312 |
const ib_tpl_t ib_src_tpl) /*!< in: source tuple */ |
| 2313 |
{ |
| 2314 |
ulint i; |
| 2315 |
ulint n_fields; |
| 2316 |
ib_err_t err = DB_SUCCESS; |
| 2317 |
ib_tuple_t *dst_tuple = nullptr; |
| 2318 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 2319 |
ib_tuple_t *src_tuple = (ib_tuple_t *)ib_src_tpl; |
| 2320 |
dict_index_t *clust_index; |
| 2321 |
|
| 2322 |
clust_index = cursor->prebuilt->table->first_index(); |
| 2323 |
|
| 2324 |
/* We need to ensure that the src tuple belongs to the same table |
| 2325 |
as the open cursor and that it's not a tuple for a cluster index. */ |
| 2326 |
if (src_tuple->type != TPL_TYPE_KEY) { |
| 2327 |
return (DB_ERROR); |
| 2328 |
} else if (src_tuple->index->table != cursor->prebuilt->table) { |
| 2329 |
return (DB_DATA_MISMATCH); |
| 2330 |
} else if (src_tuple->index == clust_index) { |
| 2331 |
return (DB_ERROR); |
| 2332 |
} |
| 2333 |
|
| 2334 |
/* Create the cluster index key search tuple. */ |
| 2335 |
*ib_dst_tpl = ib_clust_search_tuple_create(ib_crsr); |
| 2336 |
|
| 2337 |
if (!*ib_dst_tpl) { |
| 2338 |
return (DB_OUT_OF_MEMORY); |
| 2339 |
} |
| 2340 |
|
| 2341 |
dst_tuple = (ib_tuple_t *)*ib_dst_tpl; |
| 2342 |
ut_a(dst_tuple->index == clust_index); |
| 2343 |
|
| 2344 |
n_fields = dict_index_get_n_unique(dst_tuple->index); |
| 2345 |
|
| 2346 |
/* Do a deep copy of the data fields. */ |
| 2347 |
for (i = 0; i < n_fields; i++) { |
| 2348 |
ulint pos; |
| 2349 |
dfield_t *src_field; |
| 2350 |
dfield_t *dst_field; |
| 2351 |
|
| 2352 |
pos = dict_index_get_nth_field_pos(src_tuple->index, dst_tuple->index, i); |
| 2353 |
|
| 2354 |
ut_a(pos != ULINT_UNDEFINED); |
| 2355 |
|
| 2356 |
src_field = dtuple_get_nth_field(src_tuple->ptr, pos); |
| 2357 |
dst_field = dtuple_get_nth_field(dst_tuple->ptr, i); |
| 2358 |
|
| 2359 |
if (!dfield_is_null(src_field)) { |
| 2360 |
UNIV_MEM_ASSERT_RW(src_field->data, src_field->len); |
| 2361 |
|
| 2362 |
dst_field->data = |
| 2363 |
mem_heap_dup(dst_tuple->heap, src_field->data, src_field->len); |
| 2364 |
|
| 2365 |
dst_field->len = src_field->len; |
| 2366 |
} else { |
| 2367 |
dfield_set_null(dst_field); |
| 2368 |
} |
| 2369 |
} |
| 2370 |
|
| 2371 |
return (err); |
| 2372 |
} |
| 2373 |
|
| 2374 |
/** Create an InnoDB tuple used for index/table search. |
| 2375 |
@return own: Tuple for current index */ |
| 2376 |
ib_tpl_t ib_sec_search_tuple_create( |
| 2377 |
ib_crsr_t ib_crsr) /*!< in: Cursor instance */ |
| 2378 |
{ |
| 2379 |
ulint n_cols; |
| 2380 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 2381 |
dict_index_t *index = cursor->prebuilt->index; |
| 2382 |
|
| 2383 |
n_cols = dict_index_get_n_unique_in_tree(index); |
| 2384 |
return (ib_key_tuple_new(index, n_cols)); |
| 2385 |
} |
| 2386 |
|
| 2387 |
/** Create an InnoDB tuple used for index/table search. |
| 2388 |
@return own: Tuple for current index */ |
| 2389 |
ib_tpl_t ib_sec_read_tuple_create(ib_crsr_t ib_crsr) /*!< in: Cursor instance */ |
| 2390 |
{ |
| 2391 |
ulint n_cols; |
| 2392 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 2393 |
dict_index_t *index = cursor->prebuilt->index; |
| 2394 |
|
| 2395 |
n_cols = dict_index_get_n_fields(index); |
| 2396 |
return (ib_row_tuple_new(index, n_cols)); |
| 2397 |
} |
| 2398 |
|
| 2399 |
/** Create an InnoDB tuple used for table key operations. |
| 2400 |
@return own: Tuple for current table */ |
| 2401 |
ib_tpl_t ib_clust_search_tuple_create( |
| 2402 |
ib_crsr_t ib_crsr) /*!< in: Cursor instance */ |
| 2403 |
{ |
| 2404 |
ulint n_cols; |
| 2405 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 2406 |
dict_index_t *index; |
| 2407 |
|
| 2408 |
index = cursor->prebuilt->table->first_index(); |
| 2409 |
|
| 2410 |
n_cols = dict_index_get_n_ordering_defined_by_user(index); |
| 2411 |
return (ib_key_tuple_new(index, n_cols)); |
| 2412 |
} |
| 2413 |
|
| 2414 |
/** Create an InnoDB tuple for table row operations. |
| 2415 |
@return own: Tuple for current table */ |
| 2416 |
ib_tpl_t ib_clust_read_tuple_create( |
| 2417 |
ib_crsr_t ib_crsr) /*!< in: Cursor instance */ |
| 2418 |
{ |
| 2419 |
ulint n_cols; |
| 2420 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 2421 |
dict_index_t *index; |
| 2422 |
|
| 2423 |
index = cursor->prebuilt->table->first_index(); |
| 2424 |
|
| 2425 |
n_cols = cursor->prebuilt->table->get_n_cols(); |
| 2426 |
return (ib_row_tuple_new(index, n_cols)); |
| 2427 |
} |
| 2428 |
|
| 2429 |
/** Return the number of user columns in the tuple definition. |
| 2430 |
@return number of user columns */ |
| 2431 |
uint64_t ib_tuple_get_n_user_cols( |
| 2432 |
const ib_tpl_t ib_tpl) /*!< in: Tuple for current table */ |
| 2433 |
{ |
| 2434 |
const ib_tuple_t *tuple = (const ib_tuple_t *)ib_tpl; |
| 2435 |
|
| 2436 |
if (tuple->type == TPL_TYPE_ROW) { |
| 2437 |
return tuple->index->table->get_n_user_cols(); |
| 2438 |
} |
| 2439 |
|
| 2440 |
return dict_index_get_n_ordering_defined_by_user(tuple->index); |
| 2441 |
} |
| 2442 |
|
| 2443 |
/** Return the number of columns in the tuple definition. |
| 2444 |
@return number of columns */ |
| 2445 |
uint64_t ib_tuple_get_n_cols( |
| 2446 |
const ib_tpl_t ib_tpl) /*!< in: Tuple for table/index */ |
| 2447 |
{ |
| 2448 |
const ib_tuple_t *tuple = (const ib_tuple_t *)ib_tpl; |
| 2449 |
|
| 2450 |
return dtuple_get_n_fields(tuple->ptr); |
| 2451 |
} |
| 2452 |
|
| 2453 |
/** Destroy an InnoDB tuple. */ |
| 2454 |
void ib_tuple_delete(ib_tpl_t ib_tpl) /*!< in,own: Tuple instance to delete */ |
| 2455 |
{ |
| 2456 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 2457 |
|
| 2458 |
if (!ib_tpl) { |
| 2459 |
return; |
| 2460 |
} |
| 2461 |
|
| 2462 |
mem_heap_free(tuple->heap); |
| 2463 |
} |
| 2464 |
|
| 2465 |
/** Get a table id. This function will acquire the dictionary mutex. |
| 2466 |
@return DB_SUCCESS if found */ |
| 2467 |
ib_err_t ib_table_get_id(const char *table_name, /*!< in: table to find */ |
| 2468 |
ib_id_u64_t *table_id) /*!< out: table id if found */ |
| 2469 |
{ |
| 2470 |
ib_err_t err; |
| 2471 |
|
| 2472 |
dict_mutex_enter_for_mysql(); |
| 2473 |
|
| 2474 |
err = ib_table_get_id_low(table_name, table_id); |
| 2475 |
|
| 2476 |
dict_mutex_exit_for_mysql(); |
| 2477 |
|
| 2478 |
return (err); |
| 2479 |
} |
| 2480 |
|
| 2481 |
/** Check if cursor is positioned. |
| 2482 |
@param[in] ib_crsr InnoDB cursor instance |
| 2483 |
@return IB_true if positioned */ |
| 2484 |
bool ib_cursor_is_positioned(const ib_crsr_t ib_crsr) { |
| 2485 |
const ib_cursor_t *cursor = (const ib_cursor_t *)ib_crsr; |
| 2486 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 2487 |
|
| 2488 |
return ib_btr_cursor_is_positioned(prebuilt->pcur); |
| 2489 |
} |
| 2490 |
|
| 2491 |
/** Checks if the data dictionary is latched in exclusive mode. |
| 2492 |
@return true if exclusive latch */ |
| 2493 |
bool ib_schema_lock_is_exclusive(const ib_trx_t ib_trx) /*!< in: transaction */ |
| 2494 |
{ |
| 2495 |
const trx_t *trx = (const trx_t *)ib_trx; |
| 2496 |
|
| 2497 |
return (trx->dict_operation_lock_mode == RW_X_LATCH); |
| 2498 |
} |
| 2499 |
|
| 2500 |
/** Set the Lock an InnoDB cursor/table. |
| 2501 |
@return DB_SUCCESS or error code */ |
| 2502 |
ib_err_t ib_cursor_lock(ib_crsr_t ib_crsr, /*!< in/out: InnoDB cursor */ |
| 2503 |
ib_lck_mode_t ib_lck_mode) /*!< in: InnoDB lock mode */ |
| 2504 |
{ |
| 2505 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 2506 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 2507 |
trx_t *trx = prebuilt->trx; |
| 2508 |
dict_table_t *table = prebuilt->table; |
| 2509 |
|
| 2510 |
return ( |
| 2511 |
ib_trx_lock_table_with_retry(trx, table, (enum lock_mode)ib_lck_mode)); |
| 2512 |
} |
| 2513 |
|
| 2514 |
/** Set the Lock mode of the cursor. |
| 2515 |
@return DB_SUCCESS or error code */ |
| 2516 |
ib_err_t ib_cursor_set_lock_mode( |
| 2517 |
ib_crsr_t ib_crsr, /*!< in/out: InnoDB cursor */ |
| 2518 |
ib_lck_mode_t ib_lck_mode) /*!< in: InnoDB lock mode */ |
| 2519 |
{ |
| 2520 |
ib_err_t err = DB_SUCCESS; |
| 2521 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 2522 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 2523 |
|
| 2524 |
ut_a(ib_lck_mode <= static_cast<ib_lck_mode_t>(LOCK_NUM)); |
| 2525 |
|
| 2526 |
if (ib_lck_mode == IB_LOCK_X) { |
| 2527 |
err = ib_cursor_lock(ib_crsr, IB_LOCK_IX); |
| 2528 |
} else if (ib_lck_mode == IB_LOCK_S) { |
| 2529 |
err = ib_cursor_lock(ib_crsr, IB_LOCK_IS); |
| 2530 |
} |
| 2531 |
|
| 2532 |
if (err == DB_SUCCESS) { |
| 2533 |
prebuilt->select_lock_type = (lock_mode)ib_lck_mode; |
| 2534 |
ut_a(trx_is_started(prebuilt->trx)); |
| 2535 |
} |
| 2536 |
|
| 2537 |
return (err); |
| 2538 |
} |
| 2539 |
|
| 2540 |
/** Set need to access clustered index record. */ |
| 2541 |
void ib_cursor_set_cluster_access( |
| 2542 |
ib_crsr_t ib_crsr) /*!< in/out: InnoDB cursor */ |
| 2543 |
{ |
| 2544 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 2545 |
row_prebuilt_t *prebuilt = cursor->prebuilt; |
| 2546 |
|
| 2547 |
prebuilt->need_to_access_clustered = true; |
| 2548 |
} |
| 2549 |
|
| 2550 |
/** Inform the cursor that it's the start of an SQL statement. */ |
| 2551 |
void ib_cursor_stmt_begin(ib_crsr_t ib_crsr) /*!< in: cursor */ |
| 2552 |
{ |
| 2553 |
ib_cursor_t *cursor = (ib_cursor_t *)ib_crsr; |
| 2554 |
|
| 2555 |
cursor->prebuilt->sql_stat_start = true; |
| 2556 |
} |
| 2557 |
|
| 2558 |
/** Write a double value to a column. |
| 2559 |
@return DB_SUCCESS or error */ |
| 2560 |
ib_err_t ib_tuple_write_double( |
| 2561 |
ib_tpl_t ib_tpl, /*!< in/out: tuple to write to */ |
| 2562 |
int col_no, /*!< in: column number */ |
| 2563 |
double val) /*!< in: value to write */ |
| 2564 |
{ |
| 2565 |
const dfield_t *dfield; |
| 2566 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 2567 |
|
| 2568 |
dfield = ib_col_get_dfield(tuple, col_no); |
| 2569 |
|
| 2570 |
if (dtype_get_mtype(dfield_get_type(dfield)) == DATA_DOUBLE) { |
| 2571 |
return (ib_col_set_value(ib_tpl, col_no, &val, sizeof(val), true)); |
| 2572 |
} else { |
| 2573 |
return (DB_DATA_MISMATCH); |
| 2574 |
} |
| 2575 |
} |
| 2576 |
|
| 2577 |
/** Read a double column value from an InnoDB tuple. |
| 2578 |
@return DB_SUCCESS or error */ |
| 2579 |
ib_err_t ib_tuple_read_double(ib_tpl_t ib_tpl, /*!< in: InnoDB tuple */ |
| 2580 |
uint64_t col_no, /*!< in: column number */ |
| 2581 |
double *dval) /*!< out: double value */ |
| 2582 |
{ |
| 2583 |
ib_err_t err; |
| 2584 |
const dfield_t *dfield; |
| 2585 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 2586 |
|
| 2587 |
dfield = ib_col_get_dfield(tuple, col_no); |
| 2588 |
|
| 2589 |
if (dtype_get_mtype(dfield_get_type(dfield)) == DATA_DOUBLE) { |
| 2590 |
ib_col_copy_value(ib_tpl, col_no, dval, sizeof(*dval)); |
| 2591 |
err = DB_SUCCESS; |
| 2592 |
} else { |
| 2593 |
err = DB_DATA_MISMATCH; |
| 2594 |
} |
| 2595 |
|
| 2596 |
return (err); |
| 2597 |
} |
| 2598 |
|
| 2599 |
ib_err_t ib_tuple_write_float(ib_tpl_t ib_tpl, uint64_t col_no, float val) { |
| 2600 |
const dfield_t *dfield; |
| 2601 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 2602 |
|
| 2603 |
dfield = ib_col_get_dfield(tuple, col_no); |
| 2604 |
|
| 2605 |
if (dtype_get_mtype(dfield_get_type(dfield)) == DATA_FLOAT) { |
| 2606 |
return (ib_col_set_value(ib_tpl, col_no, &val, sizeof(val), true)); |
| 2607 |
} else { |
| 2608 |
return (DB_DATA_MISMATCH); |
| 2609 |
} |
| 2610 |
} |
| 2611 |
|
| 2612 |
/** Read a float value from an InnoDB tuple. |
| 2613 |
@return DB_SUCCESS or error */ |
| 2614 |
ib_err_t ib_tuple_read_float(ib_tpl_t ib_tpl, /*!< in: InnoDB tuple */ |
| 2615 |
ulint col_no, /*!< in: column number */ |
| 2616 |
float *fval) /*!< out: float value */ |
| 2617 |
{ |
| 2618 |
ib_err_t err; |
| 2619 |
const dfield_t *dfield; |
| 2620 |
ib_tuple_t *tuple = (ib_tuple_t *)ib_tpl; |
| 2621 |
|
| 2622 |
dfield = ib_col_get_dfield(tuple, col_no); |
| 2623 |
|
| 2624 |
if (dtype_get_mtype(dfield_get_type(dfield)) == DATA_FLOAT) { |
| 2625 |
ib_col_copy_value(ib_tpl, col_no, fval, sizeof(*fval)); |
| 2626 |
err = DB_SUCCESS; |
| 2627 |
} else { |
| 2628 |
err = DB_DATA_MISMATCH; |
| 2629 |
} |
| 2630 |
|
| 2631 |
return (err); |
| 2632 |
} |
| 2633 |
|
| 2634 |
/** Return isolation configuration set by "innodb_api_trx_level" |
| 2635 |
@return trx isolation level*/ |
| 2636 |
ib_trx_level_t ib_cfg_trx_level() { |
| 2637 |
return (static_cast<ib_trx_level_t>(ib_trx_level_setting)); |
| 2638 |
} |
| 2639 |
|
| 2640 |
/** Return configure value for background commit interval (in seconds) |
| 2641 |
@return background commit interval (in seconds) */ |
| 2642 |
uint64_t ib_cfg_bk_commit_interval() { return ib_bk_commit_interval; } |
| 2643 |
|
| 2644 |
/** Get generic configure status |
| 2645 |
@return configure status*/ |
| 2646 |
int ib_cfg_get_cfg() { |
| 2647 |
int cfg_status; |
| 2648 |
|
| 2649 |
cfg_status = (ib_binlog_enabled) ? IB_CFG_BINLOG_ENABLED : 0; |
| 2650 |
|
| 2651 |
if (ib_mdl_enabled) { |
| 2652 |
cfg_status |= IB_CFG_MDL_ENABLED; |
| 2653 |
} |
| 2654 |
|
| 2655 |
if (ib_disable_row_lock) { |
| 2656 |
cfg_status |= IB_CFG_DISABLE_ROWLOCK; |
| 2657 |
} |
| 2658 |
|
| 2659 |
return (cfg_status); |
| 2660 |
} |
| 2661 |
|
| 2662 |
/** Wrapper of ut_strerr() which converts an InnoDB error number to a |
| 2663 |
human readable text message. |
| 2664 |
@return string, describing the error */ |
| 2665 |
const char *ib_ut_strerr(ib_err_t num) /*!< in: error number */ |
| 2666 |
{ |
| 2667 |
return (ut_strerr(num)); |
| 2668 |
} |
| 2669 |
|
| 2670 |
/** Open an InnoDB table and return a cursor handle to it. |
| 2671 |
@return DB_SUCCESS or err code */ |
| 2672 |
static ib_err_t ib_cursor_open_table_using_id( |
| 2673 |
ib_id_u64_t table_id, /*!< in: table id of table to open */ |
| 2674 |
ib_trx_t ib_trx, /*!< in: Current transaction handle |
| 2675 |
can be NULL */ |
| 2676 |
ib_crsr_t *ib_crsr) /*!< out,own: InnoDB cursor */ |
| 2677 |
{ |
| 2678 |
ib_err_t err; |
| 2679 |
dict_table_t *table; |
| 2680 |
MDL_ticket *mdl = nullptr; |
| 2681 |
|
| 2682 |
table = dd_table_open_on_id(table_id, ib_trx->mysql_thd, &mdl, false, true); |
| 2683 |
|
| 2684 |
if (table == nullptr) { |
| 2685 |
return (DB_TABLE_NOT_FOUND); |
| 2686 |
} |
| 2687 |
|
| 2688 |
err = ib_create_cursor_with_clust_index(ib_crsr, table, (trx_t *)ib_trx); |
| 2689 |
(*ib_crsr)->mdl = mdl; |
| 2690 |
|
| 2691 |
return (err); |
| 2692 |
} |
| 2693 |
|
| 2694 |
/** Create a tuple to search from SDI table |
| 2695 |
@param[in,out] ib_crsr Memcached cursor |
| 2696 |
@param[in,out] sdi_key SDI Key |
| 2697 |
@return search tuple */ |
| 2698 |
static ib_tpl_t ib_sdi_create_search_tuple(ib_crsr_t ib_crsr, |
| 2699 |
const sdi_key_t *sdi_key) { |
| 2700 |
ut_ad(ib_crsr->prebuilt->index->get_field(0)->fixed_len == SDI_TYPE_LEN); |
| 2701 |
ut_ad(ib_crsr->prebuilt->index->get_field(1)->fixed_len == SDI_KEY_LEN); |
| 2702 |
|
| 2703 |
ib_tpl_t key_tpl = ib_clust_search_tuple_create(ib_crsr); |
| 2704 |
ib_col_set_value(key_tpl, 0, &sdi_key->type, SDI_TYPE_LEN, false); |
| 2705 |
ib_col_set_value(key_tpl, 1, &sdi_key->id, SDI_KEY_LEN, false); |
| 2706 |
|
| 2707 |
return (key_tpl); |
| 2708 |
} |
| 2709 |
|
| 2710 |
/** Create a tuple to insert into SDI table |
| 2711 |
@param[in,out] ib_crsr Memcached cursor |
| 2712 |
@param[in] sdi_key SDI Key |
| 2713 |
@param[in] uncomp_len uncompressed length of SDI |
| 2714 |
@param[in] comp_len compressed length of SDI |
| 2715 |
@param[in] sdi compressed SDI data |
| 2716 |
@return insert tuple */ |
| 2717 |
static ib_tpl_t ib_sdi_create_insert_tuple(ib_crsr_t ib_crsr, |
| 2718 |
const sdi_key_t *sdi_key, |
| 2719 |
uint32_t uncomp_len, |
| 2720 |
uint32_t comp_len, const void *sdi) { |
| 2721 |
ut_ad(ib_crsr->prebuilt->index->get_field(0)->fixed_len == SDI_TYPE_LEN); |
| 2722 |
ut_ad(ib_crsr->prebuilt->index->get_field(1)->fixed_len == SDI_KEY_LEN); |
| 2723 |
|
| 2724 |
ib_tpl_t tuple = ib_clust_read_tuple_create(ib_crsr); |
| 2725 |
ib_col_set_value(tuple, 0, &sdi_key->type, SDI_TYPE_LEN, false); |
| 2726 |
ib_col_set_value(tuple, 1, &sdi_key->id, SDI_KEY_LEN, false); |
| 2727 |
ib_col_set_value(tuple, 2, &uncomp_len, 4, false); |
| 2728 |
ib_col_set_value(tuple, 3, &comp_len, 4, false); |
| 2729 |
ib_col_set_value(tuple, 4, sdi, comp_len, false); |
| 2730 |
return (tuple); |
| 2731 |
} |
| 2732 |
|
| 2733 |
/** Open SDI table |
| 2734 |
@param[in] tablespace_id tablespace id |
| 2735 |
@param[in,out] trx innodb transaction |
| 2736 |
@param[in,out] ib_crsr memcached cursor |
| 2737 |
@return DB_SUCCESS if SDI table is opened, else error */ |
| 2738 |
static ib_err_t ib_sdi_open_table(uint32_t tablespace_id, trx_t *trx, |
| 2739 |
ib_crsr_t *ib_crsr) { |
| 2740 |
if (fsp_has_sdi(tablespace_id) != DB_SUCCESS) { |
| 2741 |
return (DB_ERROR); |
| 2742 |
} |
| 2743 |
|
| 2744 |
ib_err_t err = ib_cursor_open_table_using_id( |
| 2745 |
dict_sdi_get_table_id(tablespace_id), trx, ib_crsr); |
| 2746 |
|
| 2747 |
DBUG_EXECUTE_IF( |
| 2748 |
"ib_sdi", if (err != DB_SUCCESS) { |
| 2749 |
ib::warn(ER_IB_MSG_1) |
| 2750 |
<< "Unable to open SDI dict table for tablespace: " << tablespace_id |
| 2751 |
<< " error returned is " << err; |
| 2752 |
}); |
| 2753 |
return (err); |
| 2754 |
} |
| 2755 |
|
| 2756 |
/** Insert/Update SDI in tablespace |
| 2757 |
@param[in] tablespace_id tablespace id |
| 2758 |
@param[in] ib_sdi_key SDI key to uniquely identify the tablespace |
| 2759 |
object |
| 2760 |
@param[in] uncomp_len uncompressed length of SDI |
| 2761 |
@param[in] comp_len compressed length of SDI |
| 2762 |
@param[in] sdi compressed SDI to be stored in tablespace |
| 2763 |
@param[in,out] trx innodb transaction |
| 2764 |
@return DB_SUCCESS if SDI Insert/Update is successful, else error */ |
| 2765 |
dberr_t ib_sdi_set(uint32_t tablespace_id, const ib_sdi_key_t *ib_sdi_key, |
| 2766 |
uint32_t uncomp_len, uint32_t comp_len, const void *sdi, |
| 2767 |
trx_t *trx) { |
| 2768 |
ut_ad(ib_sdi_key != nullptr); |
| 2769 |
ut_ad(sdi != nullptr); |
| 2770 |
|
| 2771 |
DBUG_EXECUTE_IF("ib_sdi", ib::info(ER_IB_MSG_2) |
| 2772 |
<< "ib_sdi: sdi_set: " << tablespace_id |
| 2773 |
<< " Key: " << ib_sdi_key->sdi_key->type << " " |
| 2774 |
<< ib_sdi_key->sdi_key->id |
| 2775 |
<< " sdi_len: " << comp_len;); |
| 2776 |
|
| 2777 |
ib_crsr_t ib_crsr = nullptr; |
| 2778 |
ib_err_t err = ib_sdi_open_table(tablespace_id, trx, &ib_crsr); |
| 2779 |
|
| 2780 |
if (err != DB_SUCCESS) { |
| 2781 |
return (err); |
| 2782 |
} |
| 2783 |
|
| 2784 |
ib_tpl_t new_tuple = ib_sdi_create_insert_tuple(ib_crsr, ib_sdi_key->sdi_key, |
| 2785 |
uncomp_len, comp_len, sdi); |
| 2786 |
|
| 2787 |
ib_cursor_set_lock_mode(ib_crsr, IB_LOCK_X); |
| 2788 |
|
| 2789 |
/* Do insert. If row exists, handle the duplicate */ |
| 2790 |
err = ib_cursor_insert_row(ib_crsr, new_tuple); |
| 2791 |
|
| 2792 |
if (err == DB_DUPLICATE_KEY) { |
| 2793 |
/* Existing row found. We should update it. */ |
| 2794 |
|
| 2795 |
/* First check if the new row and old row are same */ |
| 2796 |
/* We only S-lock the record when doing the comparison. */ |
| 2797 |
|
| 2798 |
ib_tpl_t key_tpl = ib_sdi_create_search_tuple(ib_crsr, ib_sdi_key->sdi_key); |
| 2799 |
|
| 2800 |
ib_cursor_set_match_mode(ib_crsr, IB_EXACT_MATCH); |
| 2801 |
ib_cursor_set_lock_mode(ib_crsr, IB_LOCK_S); |
| 2802 |
err = ib_cursor_moveto(ib_crsr, key_tpl, IB_CUR_LE, 0); |
| 2803 |
ut_ad(err == DB_SUCCESS); |
| 2804 |
|
| 2805 |
ut_ad(ib_btr_cursor_is_positioned(ib_crsr->prebuilt->pcur)); |
| 2806 |
|
| 2807 |
ib_tpl_t old_tuple = ib_clust_read_tuple_create(ib_crsr); |
| 2808 |
ib_cursor_stmt_begin(ib_crsr); |
| 2809 |
ib_cursor_read_row(ib_crsr, old_tuple, nullptr, 0, nullptr, nullptr, |
| 2810 |
nullptr); |
| 2811 |
|
| 2812 |
/* Move the cursor to position of the record to update |
| 2813 |
and X-latch the record */ |
| 2814 |
upd_t *upd; |
| 2815 |
upd = ib_update_vector_create(ib_crsr); |
| 2816 |
|
| 2817 |
err = ib_calc_diff(ib_crsr, upd, old_tuple, new_tuple); |
| 2818 |
ut_ad(err == DB_SUCCESS); |
| 2819 |
|
| 2820 |
if (upd->n_fields == 0) { |
| 2821 |
/* Old row is same as new row */ |
| 2822 |
err = DB_SUCCESS; |
| 2823 |
DBUG_EXECUTE_IF("ib_sdi", ib::info(ER_IB_MSG_3) |
| 2824 |
<< "ib_sdi: sdi_set: Update row:" |
| 2825 |
<< " old row same as new: " << tablespace_id |
| 2826 |
<< " Key: " << ib_sdi_key->sdi_key->type |
| 2827 |
<< " " << ib_sdi_key->sdi_key->id |
| 2828 |
<< " trx: " << trx->id;); |
| 2829 |
|
| 2830 |
} else { |
| 2831 |
/* We compared the record and there is change. X-lock |
| 2832 |
the record */ |
| 2833 |
ib_cursor_set_match_mode(ib_crsr, IB_EXACT_MATCH); |
| 2834 |
ib_cursor_set_lock_mode(ib_crsr, IB_LOCK_X); |
| 2835 |
err = ib_cursor_moveto(ib_crsr, key_tpl, IB_CUR_LE, 0); |
| 2836 |
ut_ad(err == DB_SUCCESS); |
| 2837 |
|
| 2838 |
DBUG_EXECUTE_IF("ib_sdi", ib::info(ER_IB_MSG_4) |
| 2839 |
<< "ib_sdi: sdi_set: Existing row found: " |
| 2840 |
<< tablespace_id |
| 2841 |
<< " Key: " << ib_sdi_key->sdi_key->type |
| 2842 |
<< " " << ib_sdi_key->sdi_key->id |
| 2843 |
<< " trx: " << trx->id;); |
| 2844 |
|
| 2845 |
err = ib_cursor_update_row(ib_crsr, old_tuple, new_tuple); |
| 2846 |
|
| 2847 |
ut_ad(err == DB_SUCCESS || trx_is_interrupted(trx) || |
| 2848 |
!"sdi_update_failed"); |
| 2849 |
} |
| 2850 |
|
| 2851 |
ib_tuple_delete(old_tuple); |
| 2852 |
ib_tuple_delete(key_tpl); |
| 2853 |
|
| 2854 |
} else if (err == DB_SUCCESS) { |
| 2855 |
DBUG_EXECUTE_IF("ib_sdi", |
| 2856 |
ib::info(ER_IB_MSG_5) |
| 2857 |
<< "ib_sdi: sdi_set: insert: " << tablespace_id |
| 2858 |
<< " Key: " << ib_sdi_key->sdi_key->type << " " |
| 2859 |
<< ib_sdi_key->sdi_key->id << " trx: " << trx->id;); |
| 2860 |
} else { |
| 2861 |
DBUG_EXECUTE_IF("ib_sdi", ib::warn(ER_IB_MSG_6) |
| 2862 |
<< "ib_sdi: sdi_set: failed for" |
| 2863 |
<< " tablespace_id: " << tablespace_id |
| 2864 |
<< " Key: " << ib_sdi_key->sdi_key->type |
| 2865 |
<< " " << ib_sdi_key->sdi_key->id |
| 2866 |
<< " Error returned: " << err |
| 2867 |
<< " by trx->id: " << trx->id;); |
| 2868 |
|
| 2869 |
ut_ad(err == DB_SUCCESS || trx_is_interrupted(trx) || !"sdi_insert_failed"); |
| 2870 |
} |
| 2871 |
|
| 2872 |
ib_tuple_delete(new_tuple); |
| 2873 |
ib_cursor_close(ib_crsr); |
| 2874 |
return (err); |
| 2875 |
} |
| 2876 |
|
| 2877 |
/** Get the SDI keys in a tablespace into vector. |
| 2878 |
@param[in] tablespace_id tablespace id |
| 2879 |
@param[in,out] ib_sdi_vector vector to hold objects with tablespace types |
| 2880 |
and ids |
| 2881 |
@param[in,out] trx data dictionary transaction |
| 2882 |
@return DB_SUCCESS if retrieval of SDI kyes is successful, else error */ |
| 2883 |
dberr_t ib_sdi_get_keys(uint32_t tablespace_id, ib_sdi_vector_t *ib_sdi_vector, |
| 2884 |
trx_t *trx) { |
| 2885 |
ut_ad(ib_sdi_vector != nullptr); |
| 2886 |
ut_ad(ib_sdi_vector->sdi_vector->m_vec.empty()); |
| 2887 |
|
| 2888 |
ib_crsr_t ib_crsr = nullptr; |
| 2889 |
ib_err_t err = ib_sdi_open_table(tablespace_id, trx, &ib_crsr); |
| 2890 |
|
| 2891 |
if (err != DB_SUCCESS) { |
| 2892 |
return (err); |
| 2893 |
} |
| 2894 |
|
| 2895 |
ib_cursor_stmt_begin(ib_crsr); |
| 2896 |
err = ib_cursor_first(ib_crsr); |
| 2897 |
if (err != DB_SUCCESS) { |
| 2898 |
ib_cursor_close(ib_crsr); |
| 2899 |
return (err); |
| 2900 |
} |
| 2901 |
|
| 2902 |
ib_tpl_t tuple = ib_clust_read_tuple_create(ib_crsr); |
| 2903 |
do { |
| 2904 |
/* Read the current row from cursor position */ |
| 2905 |
err = ib_cursor_read_row(ib_crsr, tuple, nullptr, 0, nullptr, nullptr, |
| 2906 |
nullptr); |
| 2907 |
if (err != DB_SUCCESS) { |
| 2908 |
break; |
| 2909 |
} |
| 2910 |
|
| 2911 |
sdi_key_t ts; |
| 2912 |
|
| 2913 |
ib_tuple_read_u32(tuple, 0, &ts.type); |
| 2914 |
ib_tuple_read_u64(tuple, 1, reinterpret_cast<uint64_t *>(&ts.id)); |
| 2915 |
ib_sdi_vector->sdi_vector->m_vec.push_back(ts); |
| 2916 |
|
| 2917 |
} while (ib_cursor_next(ib_crsr) != DB_END_OF_INDEX); |
| 2918 |
|
| 2919 |
ib_tuple_delete(tuple); |
| 2920 |
ib_cursor_close(ib_crsr); |
| 2921 |
return (err); |
| 2922 |
} |
| 2923 |
|
| 2924 |
/** Retrieve SDI from tablespace. |
| 2925 |
@param[in] tablespace_id tablespace id |
| 2926 |
@param[in] ib_sdi_key SDI key |
| 2927 |
@param[in,out] comp_sdi in: buffer to hold the SDI BLOB |
| 2928 |
out: compressed SDI retrieved from tablespace |
| 2929 |
@param[in,out] comp_sdi_len in: Size of memory allocated |
| 2930 |
out: compressed length of SDI |
| 2931 |
@param[out] uncomp_sdi_len out: uncompressed length of SDI |
| 2932 |
@param[in,out] trx innodb transaction |
| 2933 |
@return DB_SUCCESS if SDI retrieval is successful, else error. |
| 2934 |
@return DB_OUT_OF_MEMORY if the passed buffer is not sufficient to |
| 2935 |
hold the compressed SDI retrieved from tablespace. */ |
| 2936 |
dberr_t ib_sdi_get(uint32_t tablespace_id, const ib_sdi_key_t *ib_sdi_key, |
| 2937 |
void *comp_sdi, uint32_t *comp_sdi_len, |
| 2938 |
uint32_t *uncomp_sdi_len, trx_t *trx) { |
| 2939 |
ut_ad(ib_sdi_key != nullptr); |
| 2940 |
ut_ad(comp_sdi != nullptr); |
| 2941 |
ut_ad(comp_sdi_len != nullptr); |
| 2942 |
|
| 2943 |
if (comp_sdi_len == nullptr || comp_sdi == nullptr) { |
| 2944 |
return (DB_ERROR); |
| 2945 |
} |
| 2946 |
|
| 2947 |
DBUG_EXECUTE_IF("ib_sdi", ib::info(ER_IB_MSG_7) |
| 2948 |
<< "ib_sdi: sdi_get: " << tablespace_id |
| 2949 |
<< " Key: " << ib_sdi_key->sdi_key->type << " " |
| 2950 |
<< ib_sdi_key->sdi_key->id |
| 2951 |
<< " input_buffer_len " << *comp_sdi_len; |
| 2952 |
|
| 2953 |
); |
| 2954 |
|
| 2955 |
ib_crsr_t ib_crsr = nullptr; |
| 2956 |
ib_err_t err = ib_sdi_open_table(tablespace_id, trx, &ib_crsr); |
| 2957 |
|
| 2958 |
if (err != DB_SUCCESS) { |
| 2959 |
*comp_sdi_len = UINT32_MAX; |
| 2960 |
return (err); |
| 2961 |
} |
| 2962 |
|
| 2963 |
ib_tpl_t key_tpl = ib_sdi_create_search_tuple(ib_crsr, ib_sdi_key->sdi_key); |
| 2964 |
|
| 2965 |
ib_cursor_set_match_mode(ib_crsr, IB_EXACT_MATCH); |
| 2966 |
|
| 2967 |
err = ib_cursor_moveto(ib_crsr, key_tpl, IB_CUR_GE, 0); |
| 2968 |
if (err == DB_SUCCESS) { |
| 2969 |
/* Read the current row from cursor position */ |
| 2970 |
ib_tpl_t tuple = ib_clust_read_tuple_create(ib_crsr); |
| 2971 |
ib_cursor_stmt_begin(ib_crsr); |
| 2972 |
err = ib_cursor_read_row(ib_crsr, tuple, nullptr, 0, nullptr, nullptr, |
| 2973 |
nullptr); |
| 2974 |
if (err == DB_SUCCESS) { |
| 2975 |
uint32_t buf_len = *comp_sdi_len; |
| 2976 |
ib_tuple_read_u32(tuple, 2, uncomp_sdi_len); |
| 2977 |
ib_tuple_read_u32(tuple, 3, comp_sdi_len); |
| 2978 |
|
| 2979 |
/* If the passed memory is not sufficient to hold the compressed SDI, we |
| 2980 |
return failure and the actual length of SDI. */ |
| 2981 |
if (buf_len < *comp_sdi_len) { |
| 2982 |
ib_tuple_delete(tuple); |
| 2983 |
ib_tuple_delete(key_tpl); |
| 2984 |
ib_cursor_close(ib_crsr); |
| 2985 |
return (DB_OUT_OF_MEMORY); |
| 2986 |
} |
| 2987 |
|
| 2988 |
ib_col_copy_value(tuple, 4, comp_sdi, *comp_sdi_len); |
| 2989 |
} |
| 2990 |
|
| 2991 |
ib_tuple_delete(tuple); |
| 2992 |
} else { |
| 2993 |
DBUG_EXECUTE_IF( |
| 2994 |
"ib_sdi", |
| 2995 |
if (err == DB_RECORD_NOT_FOUND) { |
| 2996 |
ib::warn(ER_IB_MSG_8) << "sdi_get: Record not found:" |
| 2997 |
<< " tablespace " << tablespace_id |
| 2998 |
<< " Key: " << ib_sdi_key->sdi_key->type << " " |
| 2999 |
<< ib_sdi_key->sdi_key->id; |
| 3000 |
} else if (err != DB_SUCCESS) { |
| 3001 |
ib::warn(ER_IB_MSG_9) |
| 3002 |
<< "sdi_get: Get Failed: tablespace " << tablespace_id |
| 3003 |
<< " Key: " << ib_sdi_key->sdi_key->type << " " |
| 3004 |
<< ib_sdi_key->sdi_key->id << " error: " << err; |
| 3005 |
}); |
| 3006 |
} |
| 3007 |
|
| 3008 |
ib_tuple_delete(key_tpl); |
| 3009 |
ib_cursor_close(ib_crsr); |
| 3010 |
|
| 3011 |
if (err != DB_SUCCESS) { |
| 3012 |
/* Return sdi_len as UINT32_MAX in case of any other failure |
| 3013 |
like searching for non-existent row */ |
| 3014 |
*comp_sdi_len = UINT32_MAX; |
| 3015 |
*uncomp_sdi_len = UINT32_MAX; |
| 3016 |
} |
| 3017 |
|
| 3018 |
return (err); |
| 3019 |
} |
| 3020 |
|
| 3021 |
/** Delete SDI from tablespace |
| 3022 |
@param[in] tablespace_id tablespace id |
| 3023 |
@param[in] ib_sdi_key SDI key to uniquely identify the tablespace |
| 3024 |
object |
| 3025 |
@param[in,out] trx innodb transaction |
| 3026 |
@return DB_SUCCESS if SDI deletion is successful, else error */ |
| 3027 |
ib_err_t ib_sdi_delete(uint32_t tablespace_id, const ib_sdi_key_t *ib_sdi_key, |
| 3028 |
trx_t *trx) { |
| 3029 |
ut_ad(ib_sdi_key != nullptr); |
| 3030 |
|
| 3031 |
DBUG_EXECUTE_IF("ib_sdi", ib::info(ER_IB_MSG_10) |
| 3032 |
<< "ib_sdi: sdi_delete: " << tablespace_id |
| 3033 |
<< " Key: " << ib_sdi_key->sdi_key->type << " " |
| 3034 |
<< ib_sdi_key->sdi_key->id;); |
| 3035 |
|
| 3036 |
ib_crsr_t ib_crsr = nullptr; |
| 3037 |
ib_err_t err = ib_sdi_open_table(tablespace_id, trx, &ib_crsr); |
| 3038 |
|
| 3039 |
if (err != DB_SUCCESS) { |
| 3040 |
return (err); |
| 3041 |
} |
| 3042 |
|
| 3043 |
ib_tpl_t key_tpl = ib_sdi_create_search_tuple(ib_crsr, ib_sdi_key->sdi_key); |
| 3044 |
|
| 3045 |
ib_cursor_set_match_mode(ib_crsr, IB_EXACT_MATCH); |
| 3046 |
ib_cursor_set_lock_mode(ib_crsr, IB_LOCK_X); |
| 3047 |
err = ib_cursor_moveto(ib_crsr, key_tpl, IB_CUR_LE, 0); |
| 3048 |
if (err == DB_SUCCESS) { |
| 3049 |
ib_cursor_stmt_begin(ib_crsr); |
| 3050 |
err = ib_cursor_delete_row(ib_crsr); |
| 3051 |
} |
| 3052 |
|
| 3053 |
if (err != DB_SUCCESS && !trx_is_interrupted(trx)) { |
| 3054 |
if (err == DB_RECORD_NOT_FOUND) { |
| 3055 |
ib::warn(ER_IB_MSG_11) << "sdi_delete failed: Record Doesn't exist:" |
| 3056 |
<< " tablespace_id: " << tablespace_id |
| 3057 |
<< " Key: " << ib_sdi_key->sdi_key->type << " " |
| 3058 |
<< ib_sdi_key->sdi_key->id; |
| 3059 |
// Emit warning, and report missing record error, but do not |
| 3060 |
// assert since this situation can occur when upgrading from a |
| 3061 |
// version where sdis were not stored for subpartitioned tables, |
| 3062 |
// and then attempting an instant alter, e.g. ALTER ... ADD |
| 3063 |
// COLUMN, bug#30360695. |
| 3064 |
} |
| 3065 |
#ifdef UNIV_DEBUG |
| 3066 |
else { |
| 3067 |
ib::warn(ER_IB_MSG_12) |
| 3068 |
<< "sdi_delete failed: tablespace_id: " << tablespace_id |
| 3069 |
<< " Key: " << ib_sdi_key->sdi_key->type << " " |
| 3070 |
<< ib_sdi_key->sdi_key->id << " Error returned: " << err; |
| 3071 |
} |
| 3072 |
#endif /* UNIV_DEBUG */ |
| 3073 |
} |
| 3074 |
|
| 3075 |
ib_tuple_delete(key_tpl); |
| 3076 |
ib_cursor_close(ib_crsr); |
| 3077 |
return (err); |
| 3078 |
} |
| 3079 |
|
| 3080 |
/** Create SDI in a tablespace |
| 3081 |
@param[in] tablespace_id InnoDB tablespace id |
| 3082 |
@return DB_SUCCESS if SDI index creation is successful, else error */ |
| 3083 |
ib_err_t ib_sdi_create(space_id_t tablespace_id) { |
| 3084 |
/* Check if the FSP_FLAG_SDI has already been set. If it |
| 3085 |
is set, then we assume SDI indexes are already created and |
| 3086 |
we don't re-create SDI indexes */ |
| 3087 |
fil_space_t *space = fil_space_acquire(tablespace_id); |
| 3088 |
if (space == nullptr) { |
| 3089 |
return (DB_ERROR); |
| 3090 |
} |
| 3091 |
|
| 3092 |
bool has_sdi = FSP_FLAGS_HAS_SDI(space->flags); |
| 3093 |
#ifdef UNIV_DEBUG |
| 3094 |
/* Read page 0 to confirm the SDI flag presence */ |
| 3095 |
const page_size_t page_size(space->flags); |
| 3096 |
mtr_t mtr; |
| 3097 |
|
| 3098 |
mtr.start(); |
| 3099 |
const fsp_header_t *header = |
| 3100 |
fsp_get_space_header(tablespace_id, page_size, &mtr); |
| 3101 |
mtr.commit(); |
| 3102 |
ut_ad(mach_read_from_4(FSP_SPACE_FLAGS + header) == space->flags); |
| 3103 |
#endif /* UNIV_DEBUG */ |
| 3104 |
|
| 3105 |
if (has_sdi) { |
| 3106 |
fil_space_release(space); |
| 3107 |
return (DB_SUCCESS); |
| 3108 |
} |
| 3109 |
|
| 3110 |
ib_err_t err = btr_sdi_create_index(tablespace_id, false); |
| 3111 |
|
| 3112 |
fil_space_release(space); |
| 3113 |
return (err); |
| 3114 |
} |
| 3115 |
|
| 3116 |
/** Drop SDI Index from tablespace. This should be used only when SDI |
| 3117 |
is corrupted. |
| 3118 |
@param[in] tablespace_id InnoDB tablespace id |
| 3119 |
@return DB_SUCCESS if dropping of SDI indexes is successful, else error */ |
| 3120 |
ib_err_t ib_sdi_drop(space_id_t tablespace_id) { |
| 3121 |
fil_space_t *space = fil_space_acquire(tablespace_id); |
| 3122 |
if (space == nullptr) { |
| 3123 |
return (DB_ERROR); |
| 3124 |
} |
| 3125 |
|
| 3126 |
rw_lock_x_lock(&space->latch, UT_LOCATION_HERE); |
| 3127 |
|
| 3128 |
page_size_t page_size(space->flags); |
| 3129 |
|
| 3130 |
mtr_t mtr; |
| 3131 |
|
| 3132 |
/* We use separate mtrs because latching IBUF BITMAP Page and |
| 3133 |
a B-Tree Index page in same mtr will cause latch violation */ |
| 3134 |
mtr.start(); |
| 3135 |
page_no_t root_page_num = |
| 3136 |
fsp_sdi_get_root_page_num(tablespace_id, page_size, &mtr); |
| 3137 |
|
| 3138 |
mtr.commit(); |
| 3139 |
|
| 3140 |
mtr.start(); |
| 3141 |
btr_free_if_exists(page_id_t(tablespace_id, root_page_num), page_size, |
| 3142 |
dict_sdi_get_index_id(), &mtr); |
| 3143 |
mtr.commit(); |
| 3144 |
|
| 3145 |
/* Remove SDI Flag presence from Page 0 */ |
| 3146 |
mtr.start(); |
| 3147 |
|
| 3148 |
uint32_t flags = space->flags & ~FSP_FLAGS_MASK_SDI; |
| 3149 |
|
| 3150 |
buf_block_t *block = buf_page_get(page_id_t(space->id, 0), page_size, |
| 3151 |
RW_SX_LATCH, UT_LOCATION_HERE, &mtr); |
| 3152 |
|
| 3153 |
buf_block_dbg_add_level(block, SYNC_FSP_PAGE); |
| 3154 |
page_t *page = buf_block_get_frame(block); |
| 3155 |
|
| 3156 |
mlog_write_ulint(FSP_HEADER_OFFSET + FSP_SPACE_FLAGS + page, flags, |
| 3157 |
MLOG_4BYTES, &mtr); |
| 3158 |
|
| 3159 |
fil_space_set_flags(space, flags); |
| 3160 |
|
| 3161 |
mtr.commit(); |
| 3162 |
rw_lock_x_unlock(&space->latch); |
| 3163 |
fil_space_release(space); |
| 3164 |
|
| 3165 |
dict_sdi_remove_from_cache(space->id, nullptr, false); |
| 3166 |
|
| 3167 |
return (DB_SUCCESS); |
| 3168 |
} |
| 3169 |
|
| 3170 |
/** Flush SDI in a tablespace. The pages of a SDI Index modified by the |
| 3171 |
transaction will be flushed to disk. |
| 3172 |
@param[in] space_id tablespace id |
| 3173 |
@return DB_SUCCESS always */ |
| 3174 |
ib_err_t ib_sdi_flush(space_id_t space_id [[maybe_unused]]) { |
| 3175 |
return (DB_SUCCESS); |
| 3176 |
} |
| 3177 |
|
| 3178 |
#ifdef UNIV_MEMCACHED_SDI |
| 3179 |
/** Parse string a unsigned long number |
| 3180 |
@param[in] num_str input string which has number |
| 3181 |
@param[out] dest_num Number converted from input string |
| 3182 |
@return DB_SUCCESS on successful conversion, else DB_ERROR */ |
| 3183 |
static ib_err_t parse_string_to_number(const char *num_str, |
| 3184 |
uint64_t *dest_num) { |
| 3185 |
char *endptr; |
| 3186 |
errno = 0; |
| 3187 |
unsigned long result = strtoul(num_str, &endptr, 10); |
| 3188 |
if (endptr == num_str || *endptr != 0) { |
| 3189 |
/* nothing parsed from the string, return error */ |
| 3190 |
return (DB_ERROR); |
| 3191 |
} |
| 3192 |
if (result == ULONG_MAX && errno == ERANGE) { |
| 3193 |
/* out of range */ |
| 3194 |
return (DB_ERROR); |
| 3195 |
} |
| 3196 |
|
| 3197 |
*dest_num = static_cast<uint64_t>(result); |
| 3198 |
return (DB_SUCCESS); |
| 3199 |
} |
| 3200 |
|
| 3201 |
/** Extracts SDI key from the memcached key. For example if the key is |
| 3202 |
"sdi_3:4", it parses as type:3, id:4 |
| 3203 |
@param[in] key_str Memcached key |
| 3204 |
@param[in,out] sk SDI key |
| 3205 |
@return DB_SUCCESS if SDI key extraction is successful, else error */ |
| 3206 |
static ib_err_t parse_mem_key_to_sdi_key(const char *key_str, sdi_key_t *sk) { |
| 3207 |
/* 25 is sufficient here, the prefix will be |
| 3208 |
sdi_number:number:number */ |
| 3209 |
char key[25 + 1]; |
| 3210 |
char *saveptr1; |
| 3211 |
|
| 3212 |
strncpy(key, key_str + strlen("sdi_"), sizeof(key) - 1); |
| 3213 |
key[sizeof(key) - 1] = '\0'; |
| 3214 |
|
| 3215 |
char *type_str = strtok_r(key, ":", &saveptr1); |
| 3216 |
char *id_str = strtok_r(NULL, ":", &saveptr1); |
| 3217 |
|
| 3218 |
if (id_str == NULL || type_str == NULL) { |
| 3219 |
return (DB_ERROR); |
| 3220 |
} |
| 3221 |
|
| 3222 |
uint64_t number; |
| 3223 |
if (parse_string_to_number(type_str, &number) == DB_SUCCESS) { |
| 3224 |
sk->type = static_cast<uint32_t>(number); |
| 3225 |
} else { |
| 3226 |
return (DB_ERROR); |
| 3227 |
} |
| 3228 |
|
| 3229 |
if (parse_string_to_number(id_str, &number) == DB_SUCCESS) { |
| 3230 |
sk->id = number; |
| 3231 |
} else { |
| 3232 |
return (DB_ERROR); |
| 3233 |
} |
| 3234 |
|
| 3235 |
return (DB_SUCCESS); |
| 3236 |
} |
| 3237 |
|
| 3238 |
/** Wrapper function to retrieve SDI from tablespace |
| 3239 |
@param[in,out] crsr Memcached cursor |
| 3240 |
@param[in] key_str Memcached key |
| 3241 |
@param[in,out] sdi SDI data retrieved |
| 3242 |
@param[in,out] sdi_len Length of SDI data |
| 3243 |
@return DB_SUCCESS if SDI retrieval is successful, else error */ |
| 3244 |
ib_err_t ib_memc_sdi_get(ib_crsr_t crsr, const char *key_str, void *sdi, |
| 3245 |
uint64_t *sdi_len) { |
| 3246 |
uint32_t tablespace_id = crsr->prebuilt->table->space; |
| 3247 |
ib_trx_t trx = crsr->prebuilt->trx; |
| 3248 |
ib_err_t err; |
| 3249 |
ib_sdi_key_t sk; |
| 3250 |
sdi_key_t sdi_key; |
| 3251 |
ut_ad(trx != NULL); |
| 3252 |
|
| 3253 |
sk.sdi_key = &sdi_key; |
| 3254 |
err = parse_mem_key_to_sdi_key(key_str, &sdi_key); |
| 3255 |
if (err != DB_SUCCESS) { |
| 3256 |
return (err); |
| 3257 |
} |
| 3258 |
|
| 3259 |
ut_ad(*sdi_len < UINT32_MAX); |
| 3260 |
uint32_t uncompressed_sdi_len; |
| 3261 |
uint32_t compressed_sdi_len = static_cast<uint32_t>(*sdi_len); |
| 3262 |
byte *compressed_sdi = static_cast<byte *>( |
| 3263 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, compressed_sdi_len)); |
| 3264 |
|
| 3265 |
err = ib_sdi_get(tablespace_id, &sk, compressed_sdi, &compressed_sdi_len, |
| 3266 |
&uncompressed_sdi_len, trx); |
| 3267 |
|
| 3268 |
if (err == DB_OUT_OF_MEMORY) { |
| 3269 |
*sdi_len = uncompressed_sdi_len; |
| 3270 |
} else if (err != DB_SUCCESS) { |
| 3271 |
*sdi_len = UINT64_MAX; |
| 3272 |
} else { |
| 3273 |
*sdi_len = uncompressed_sdi_len; |
| 3274 |
/* Decompress the data */ |
| 3275 |
Sdi_Decompressor decompressor(static_cast<byte *>(sdi), |
| 3276 |
uncompressed_sdi_len, compressed_sdi, |
| 3277 |
compressed_sdi_len); |
| 3278 |
decompressor.decompress(); |
| 3279 |
} |
| 3280 |
|
| 3281 |
return (err); |
| 3282 |
} |
| 3283 |
|
| 3284 |
/** Wrapper function to delete SDI from tablespace |
| 3285 |
@param[in,out] crsr Memcached cursor |
| 3286 |
@param[in] key_str Memcached key |
| 3287 |
@return DB_SUCCESS if SDI deletion is successful, else error */ |
| 3288 |
ib_err_t ib_memc_sdi_delete(ib_crsr_t crsr, const char *key_str) { |
| 3289 |
uint32_t tablespace_id = crsr->prebuilt->table->space; |
| 3290 |
ib_trx_t trx = crsr->prebuilt->trx; |
| 3291 |
ib_sdi_key_t sk; |
| 3292 |
sdi_key_t sdi_key; |
| 3293 |
ib_err_t err; |
| 3294 |
ut_ad(trx != NULL); |
| 3295 |
|
| 3296 |
sk.sdi_key = &sdi_key; |
| 3297 |
/* We only need sdi key */ |
| 3298 |
err = parse_mem_key_to_sdi_key(key_str, &sdi_key); |
| 3299 |
if (err != DB_SUCCESS) { |
| 3300 |
return (err); |
| 3301 |
} |
| 3302 |
|
| 3303 |
err = ib_sdi_delete(tablespace_id, &sk, trx); |
| 3304 |
|
| 3305 |
DBUG_EXECUTE_IF("ib_sdi_delete_crash", DBUG_SUICIDE();); |
| 3306 |
|
| 3307 |
return (err); |
| 3308 |
} |
| 3309 |
|
| 3310 |
/** Wrapper function to insert SDI into tablespace |
| 3311 |
@param[in,out] crsr Memcached cursor |
| 3312 |
@param[in] key_str Memcached key |
| 3313 |
@param[in] sdi SDI to be stored in tablespace |
| 3314 |
@param[in] sdi_len SDI length |
| 3315 |
@return DB_SUCCESS if SDI insertion is successful, else error */ |
| 3316 |
ib_err_t ib_memc_sdi_set(ib_crsr_t crsr, const char *key_str, const void *sdi, |
| 3317 |
uint64_t *sdi_len) { |
| 3318 |
uint32_t tablespace_id = crsr->prebuilt->table->space; |
| 3319 |
ib_trx_t trx = crsr->prebuilt->trx; |
| 3320 |
ib_sdi_key_t sk; |
| 3321 |
sdi_key_t sdi_key; |
| 3322 |
ib_err_t err; |
| 3323 |
ut_ad(trx != NULL); |
| 3324 |
|
| 3325 |
sk.sdi_key = &sdi_key; |
| 3326 |
|
| 3327 |
err = parse_mem_key_to_sdi_key(key_str, &sdi_key); |
| 3328 |
if (err != DB_SUCCESS) { |
| 3329 |
return (err); |
| 3330 |
} |
| 3331 |
|
| 3332 |
Sdi_Compressor compressor(*sdi_len, sdi); |
| 3333 |
compressor.compress(); |
| 3334 |
err = ib_sdi_set(tablespace_id, &sk, *sdi_len, compressor.get_comp_len(), |
| 3335 |
compressor.get_data(), trx); |
| 3336 |
|
| 3337 |
DBUG_EXECUTE_IF("ib_sdi_set_crash", DBUG_SUICIDE();); |
| 3338 |
|
| 3339 |
return (err); |
| 3340 |
} |
| 3341 |
|
| 3342 |
/** Wrapper function to create SDI in a tablespace |
| 3343 |
@param[in,out] crsr Memcached cursor |
| 3344 |
@return DB_SUCCESS if SDI creation is successful, else error */ |
| 3345 |
ib_err_t ib_memc_sdi_create(ib_crsr_t crsr) { |
| 3346 |
uint32_t tablespace_id = crsr->prebuilt->table->space; |
| 3347 |
return (ib_sdi_create(tablespace_id)); |
| 3348 |
} |
| 3349 |
|
| 3350 |
/** Wrapper function to drop SDI in a tablespace |
| 3351 |
@param[in,out] crsr Memcached cursor |
| 3352 |
@return DB_SUCCESS if dropping of SDI is successful, else error */ |
| 3353 |
ib_err_t ib_memc_sdi_drop(ib_crsr_t crsr) { |
| 3354 |
uint32_t tablespace_id = crsr->prebuilt->table->space; |
| 3355 |
return (ib_sdi_drop(tablespace_id)); |
| 3356 |
} |
| 3357 |
|
| 3358 |
/** Wrapper function to retrieve list of SDI keys into the buffer |
| 3359 |
The SDI keys are copied in the from x:y and separated by '|' |
| 3360 |
@param[in,out] crsr Memcached cursor |
| 3361 |
@param[in] key_str Memcached key |
| 3362 |
@param[in,out] sdi The keys are copies into this buffer |
| 3363 |
@return DB_SUCCESS if SDI keys retrieval is successful, else error */ |
| 3364 |
ib_err_t ib_memc_sdi_get_keys(ib_crsr_t crsr, const char *key_str, void *sdi, |
| 3365 |
uint64_t list_buf_len) { |
| 3366 |
uint32_t tablespace_id = crsr->prebuilt->table->space; |
| 3367 |
ib_trx_t trx = crsr->prebuilt->trx; |
| 3368 |
ut_ad(trx != NULL); |
| 3369 |
|
| 3370 |
uint32_t pattern_len = strlen("sdi_list_"); |
| 3371 |
int diff_len = key_str != NULL ? strlen(key_str) - pattern_len : -1; |
| 3372 |
if (diff_len >= 0 && strncmp(key_str, "sdi_list_", pattern_len) == 0) { |
| 3373 |
/* Pattern matched exactly with "sdi_list_" */ |
| 3374 |
} |
| 3375 |
|
| 3376 |
sdi_vector_t sdi_vector; |
| 3377 |
ib_sdi_vector ib_vector; |
| 3378 |
ib_vector.sdi_vector = &sdi_vector; |
| 3379 |
|
| 3380 |
ib_err_t err = ib_sdi_get_keys(tablespace_id, &ib_vector, trx); |
| 3381 |
|
| 3382 |
char *ptr = static_cast<char *>(sdi); |
| 3383 |
uint64_t cur_len = 0; |
| 3384 |
uint64_t bytes_printed; |
| 3385 |
for (sdi_container::iterator it = ib_vector.sdi_vector->m_vec.begin(); |
| 3386 |
it != ib_vector.sdi_vector->m_vec.end(); it++) { |
| 3387 |
bytes_printed = snprintf(ptr, list_buf_len - cur_len, "%" PRIu64 ":%u|", |
| 3388 |
it->id, it->type); |
| 3389 |
ptr += bytes_printed; |
| 3390 |
cur_len += bytes_printed; |
| 3391 |
} |
| 3392 |
*ptr = 0; |
| 3393 |
|
| 3394 |
return (err); |
| 3395 |
} |
| 3396 |
#endif /* UNIV_MEMCACHED_SDI */ |