| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2012, Facebook Inc. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 8 |
Free Software Foundation. |
| 9 |
|
| 10 |
This program is designed to work with certain software (including |
| 11 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 12 |
as designated in a particular file or component or in included license |
| 13 |
documentation. The authors of MySQL hereby grant you an additional |
| 14 |
permission to link the program and your derivative works with the |
| 15 |
separately licensed software that they have either included with |
| 16 |
the program or referenced in the documentation. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 19 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 20 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 21 |
for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License along with |
| 24 |
this program; if not, write to the Free Software Foundation, Inc., |
| 25 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 |
|
| 27 |
*****************************************************************************/ |
| 28 |
|
| 29 |
/** @file dict/mem.cc |
| 30 |
Data dictionary memory object creation |
| 31 |
|
| 32 |
Created 1/8/1996 Heikki Tuuri |
| 33 |
***********************************************************************/ |
| 34 |
|
| 35 |
/** NOTE: The functions in this file should only use functions from |
| 36 |
other files in library. The code in this file is used to make a library for |
| 37 |
external tools. */ |
| 38 |
|
| 39 |
#include <new> |
| 40 |
|
| 41 |
#include "dict0dict.h" |
| 42 |
#ifndef UNIV_HOTBACKUP |
| 43 |
#ifndef UNIV_LIBRARY |
| 44 |
#include "lock0lock.h" |
| 45 |
#endif /* !UNIV_LIBRARY */ |
| 46 |
#endif /* !UNIV_HOTBACKUP */ |
| 47 |
|
| 48 |
/** Append 'name' to 'col_names'. @see dict_table_t::col_names |
| 49 |
@return new column names array */ |
| 50 |
const char *dict_add_col_name(const char *col_names, /*!< in: existing column |
| 51 |
names, or NULL */ |
| 52 |
ulint cols, /*!< in: number of existing columns */ |
| 53 |
const char *name, /*!< in: new column name */ |
| 54 |
mem_heap_t *heap) /*!< in: heap */ |
| 55 |
{ |
| 56 |
ulint old_len; |
| 57 |
ulint new_len; |
| 58 |
ulint total_len; |
| 59 |
char *res; |
| 60 |
|
| 61 |
ut_ad(!cols == !col_names); |
| 62 |
|
| 63 |
/* Find out length of existing array. */ |
| 64 |
if (col_names) { |
| 65 |
const char *s = col_names; |
| 66 |
ulint i; |
| 67 |
|
| 68 |
for (i = 0; i < cols; i++) { |
| 69 |
s += strlen(s) + 1; |
| 70 |
} |
| 71 |
|
| 72 |
old_len = s - col_names; |
| 73 |
} else { |
| 74 |
old_len = 0; |
| 75 |
} |
| 76 |
|
| 77 |
new_len = strlen(name) + 1; |
| 78 |
total_len = old_len + new_len; |
| 79 |
|
| 80 |
res = static_cast<char *>(mem_heap_alloc(heap, total_len)); |
| 81 |
|
| 82 |
if (old_len > 0) { |
| 83 |
memcpy(res, col_names, old_len); |
| 84 |
} |
| 85 |
|
| 86 |
memcpy(res + old_len, name, new_len); |
| 87 |
|
| 88 |
return (res); |
| 89 |
} |
| 90 |
|
| 91 |
/** Free a table memory object. */ |
| 92 |
void dict_mem_table_free(dict_table_t *table) /*!< in: table */ |
| 93 |
{ |
| 94 |
ut_ad(table); |
| 95 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 96 |
ut_d(table->cached = false); |
| 97 |
|
| 98 |
#ifndef UNIV_HOTBACKUP |
| 99 |
#ifndef UNIV_LIBRARY |
| 100 |
if (dict_table_has_fts_index(table) || |
| 101 |
DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID) || |
| 102 |
DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_ADD_DOC_ID)) { |
| 103 |
if (table->fts) { |
| 104 |
fts_optimize_remove_table(table); |
| 105 |
|
| 106 |
fts_free(table); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
dict_table_mutex_destroy(table); |
| 111 |
|
| 112 |
dict_table_autoinc_destroy(table); |
| 113 |
|
| 114 |
dict_table_stats_latch_destroy(table); |
| 115 |
|
| 116 |
table->foreign_set.~dict_foreign_set(); |
| 117 |
table->referenced_set.~dict_foreign_set(); |
| 118 |
#endif /* !UNIV_LIBRARY */ |
| 119 |
#endif /* !UNIV_HOTBACKUP */ |
| 120 |
|
| 121 |
ut::free(table->name.m_name); |
| 122 |
table->name.m_name = nullptr; |
| 123 |
|
| 124 |
#ifndef UNIV_HOTBACKUP |
| 125 |
#ifndef UNIV_LIBRARY |
| 126 |
/* Clean up virtual index info structures that are registered |
| 127 |
with virtual columns */ |
| 128 |
for (ulint i = 0; i < table->n_v_def; i++) { |
| 129 |
dict_v_col_t *vcol = dict_table_get_nth_v_col(table, i); |
| 130 |
|
| 131 |
ut::delete_(vcol->v_indexes); |
| 132 |
} |
| 133 |
#endif /* !UNIV_LIBRARY */ |
| 134 |
#endif /* !UNIV_HOTBACKUP */ |
| 135 |
|
| 136 |
if (table->s_cols != nullptr) { |
| 137 |
ut::delete_(table->s_cols); |
| 138 |
} |
| 139 |
|
| 140 |
#ifndef UNIV_HOTBACKUP |
| 141 |
if (table->temp_prebuilt != nullptr) { |
| 142 |
ut_ad(table->is_intrinsic()); |
| 143 |
ut::delete_(table->temp_prebuilt); |
| 144 |
} |
| 145 |
#endif /* !UNIV_HOTBACKUP */ |
| 146 |
|
| 147 |
mem_heap_free(table->heap); |
| 148 |
} |
| 149 |
|
| 150 |
/** System databases */ |
| 151 |
static std::string innobase_system_databases[] = { |
| 152 |
"mysql/", "information_schema/", "performance_schema/", ""}; |
| 153 |
|
| 154 |
/** Determines if a table is a system table |
| 155 |
@param[in] name table_name |
| 156 |
@return true if table is system table */ |
| 157 |
static bool dict_mem_table_is_system(const std::string name) { |
| 158 |
/* Table has the following format: database/table and some system table are |
| 159 |
of the form SYS_* */ |
| 160 |
if (name.find('/') != std::string::npos) { |
| 161 |
size_t table_len = name.length(); |
| 162 |
|
| 163 |
std::string system_db = std::string(innobase_system_databases[0]); |
| 164 |
int i = 0; |
| 165 |
|
| 166 |
while (system_db.compare("") != 0) { |
| 167 |
size_t len = system_db.length(); |
| 168 |
|
| 169 |
if (table_len > len && name.compare(0, len, system_db) == 0) { |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
system_db = std::string(innobase_system_databases[++i]); |
| 174 |
} |
| 175 |
|
| 176 |
return false; |
| 177 |
} else { |
| 178 |
return true; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
dict_table_t *dict_mem_table_create(const char *name, space_id_t space, |
| 183 |
ulint n_cols, ulint n_v_cols, |
| 184 |
ulint n_m_v_cols, uint32_t flags, |
| 185 |
uint32_t flags2, uint32_t n_drop_cols) { |
| 186 |
dict_table_t *table; |
| 187 |
mem_heap_t *heap; |
| 188 |
|
| 189 |
ut_ad(name); |
| 190 |
#ifndef UNIV_HOTBACKUP |
| 191 |
ut_a(dict_tf2_is_valid(flags, flags2)); |
| 192 |
ut_a(!(flags2 & DICT_TF2_UNUSED_BIT_MASK)); |
| 193 |
#endif /* !UNIV_HOTBACKUP */ |
| 194 |
|
| 195 |
heap = mem_heap_create(DICT_HEAP_SIZE, UT_LOCATION_HERE); |
| 196 |
|
| 197 |
table = static_cast<dict_table_t *>(mem_heap_zalloc(heap, sizeof(*table))); |
| 198 |
|
| 199 |
#ifndef UNIV_HOTBACKUP |
| 200 |
#ifndef UNIV_LIBRARY |
| 201 |
UT_LIST_INIT(table->locks); |
| 202 |
#endif /* !UNIV_LIBRARY */ |
| 203 |
#endif /* !UNIV_HOTBACKUP */ |
| 204 |
|
| 205 |
UT_LIST_INIT(table->indexes); |
| 206 |
|
| 207 |
table->heap = heap; |
| 208 |
|
| 209 |
ut_d(table->magic_n = DICT_TABLE_MAGIC_N); |
| 210 |
|
| 211 |
table->flags = (unsigned int)flags; |
| 212 |
table->flags2 = (unsigned int)flags2; |
| 213 |
table->name.m_name = mem_strdup(name); |
| 214 |
table->is_system_table = dict_mem_table_is_system(table->name.m_name); |
| 215 |
table->space = (unsigned int)space; |
| 216 |
table->dd_space_id = dd::INVALID_OBJECT_ID; |
| 217 |
table->n_t_cols = (unsigned int)(n_cols + table->get_n_sys_cols()); |
| 218 |
table->n_v_cols = (unsigned int)(n_v_cols); |
| 219 |
table->n_m_v_cols = (unsigned int)(n_m_v_cols); |
| 220 |
table->n_cols = table->n_t_cols - table->n_v_cols; |
| 221 |
table->n_instant_cols = table->n_cols; |
| 222 |
|
| 223 |
table->cols = static_cast<dict_col_t *>( |
| 224 |
mem_heap_alloc(heap, (table->n_cols + n_drop_cols) * sizeof(dict_col_t))); |
| 225 |
table->v_cols = static_cast<dict_v_col_t *>( |
| 226 |
mem_heap_alloc(heap, n_v_cols * sizeof(*table->v_cols))); |
| 227 |
|
| 228 |
#ifndef UNIV_HOTBACKUP |
| 229 |
#ifndef UNIV_LIBRARY |
| 230 |
dict_table_mutex_create_lazy(table); |
| 231 |
|
| 232 |
/* true means that the stats latch will be enabled - |
| 233 |
dict_table_stats_lock() will not be noop. */ |
| 234 |
dict_table_stats_latch_create(table, true); |
| 235 |
|
| 236 |
table->autoinc_lock = lock_alloc_from_heap(heap); |
| 237 |
|
| 238 |
/* lazy creation of table autoinc latch */ |
| 239 |
dict_table_autoinc_create_lazy(table); |
| 240 |
|
| 241 |
table->version = 0; |
| 242 |
table->autoinc = 0; |
| 243 |
table->autoinc_persisted = 0; |
| 244 |
table->autoinc_field_no = ULINT_UNDEFINED; |
| 245 |
table->sess_row_id = 0; |
| 246 |
table->sess_trx_id = 0; |
| 247 |
|
| 248 |
/* If the table has an FTS index or we are in the process |
| 249 |
of building one, create the table->fts */ |
| 250 |
if (dict_table_has_fts_index(table) || |
| 251 |
DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID) || |
| 252 |
DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_ADD_DOC_ID)) { |
| 253 |
table->fts = fts_create(table); |
| 254 |
table->fts->cache = fts_cache_create(table); |
| 255 |
} else { |
| 256 |
table->fts = nullptr; |
| 257 |
} |
| 258 |
|
| 259 |
if (DICT_TF_HAS_SHARED_SPACE(table->flags)) { |
| 260 |
dict_get_and_save_space_name(table); |
| 261 |
} |
| 262 |
|
| 263 |
new (&table->foreign_set) dict_foreign_set(); |
| 264 |
new (&table->referenced_set) dict_foreign_set(); |
| 265 |
|
| 266 |
#endif /* !UNIV_LIBRARY */ |
| 267 |
#endif /* !UNIV_HOTBACKUP */ |
| 268 |
table->is_dd_table = false; |
| 269 |
table->explicitly_non_lru = false; |
| 270 |
|
| 271 |
return (table); |
| 272 |
} |
| 273 |
|
| 274 |
/** Creates an index memory object. |
| 275 |
@return own: index object */ |
| 276 |
dict_index_t *dict_mem_index_create( |
| 277 |
const char *table_name, /*!< in: table name */ |
| 278 |
const char *index_name, /*!< in: index name */ |
| 279 |
ulint space, /*!< in: space where the index tree is |
| 280 |
placed, ignored if the index is of |
| 281 |
the clustered type */ |
| 282 |
ulint type, /*!< in: DICT_UNIQUE, |
| 283 |
DICT_CLUSTERED, ... ORed */ |
| 284 |
ulint n_fields) /*!< in: number of fields */ |
| 285 |
{ |
| 286 |
dict_index_t *index; |
| 287 |
mem_heap_t *heap; |
| 288 |
|
| 289 |
ut_ad(table_name && index_name); |
| 290 |
|
| 291 |
heap = mem_heap_create(DICT_HEAP_SIZE, UT_LOCATION_HERE); |
| 292 |
|
| 293 |
index = static_cast<dict_index_t *>(mem_heap_zalloc(heap, sizeof(*index))); |
| 294 |
|
| 295 |
new (&index->fields_array)(decltype(index->fields_array))(); |
| 296 |
dict_mem_fill_index_struct(index, heap, table_name, index_name, space, type, |
| 297 |
n_fields); |
| 298 |
|
| 299 |
#ifndef UNIV_HOTBACKUP |
| 300 |
#ifndef UNIV_LIBRARY |
| 301 |
dict_index_zip_pad_mutex_create_lazy(index); |
| 302 |
|
| 303 |
if (type & DICT_SPATIAL) { |
| 304 |
mutex_create(LATCH_ID_RTR_SSN_MUTEX, &index->rtr_ssn.mutex); |
| 305 |
index->rtr_track = static_cast<rtr_info_track_t *>( |
| 306 |
mem_heap_alloc(heap, sizeof(*index->rtr_track))); |
| 307 |
mutex_create(LATCH_ID_RTR_ACTIVE_MUTEX, |
| 308 |
&index->rtr_track->rtr_active_mutex); |
| 309 |
index->rtr_track->rtr_active = |
| 310 |
ut::new_withkey<rtr_info_active>(UT_NEW_THIS_FILE_PSI_KEY); |
| 311 |
} |
| 312 |
#endif /* !UNIV_LIBRARY */ |
| 313 |
#endif /* !UNIV_HOTBACKUP */ |
| 314 |
|
| 315 |
return (index); |
| 316 |
} |
| 317 |
|
| 318 |
/** Adds a column definition to a table. */ |
| 319 |
void dict_mem_table_add_col(dict_table_t *table, mem_heap_t *heap, |
| 320 |
const char *name, ulint mtype, ulint prtype, |
| 321 |
ulint len, bool is_visible, uint32_t phy_pos, |
| 322 |
uint8_t v_added, uint8_t v_dropped) { |
| 323 |
dict_col_t *col; |
| 324 |
ulint i; |
| 325 |
|
| 326 |
ut_ad(table); |
| 327 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 328 |
ut_ad(!heap == !name); |
| 329 |
|
| 330 |
ut_ad(!(prtype & DATA_VIRTUAL)); |
| 331 |
|
| 332 |
i = table->n_def++; |
| 333 |
|
| 334 |
table->n_t_def++; |
| 335 |
|
| 336 |
if (name) { |
| 337 |
if (table->n_def == (table->n_cols + table->get_n_instant_drop_cols())) { |
| 338 |
heap = table->heap; |
| 339 |
} |
| 340 |
if (i && !table->col_names) { |
| 341 |
/* All preceding column names are empty. */ |
| 342 |
char *s = static_cast<char *>(mem_heap_zalloc(heap, table->n_def)); |
| 343 |
|
| 344 |
table->col_names = s; |
| 345 |
} |
| 346 |
|
| 347 |
table->col_names = dict_add_col_name(table->col_names, i, name, heap); |
| 348 |
} |
| 349 |
|
| 350 |
col = table->get_col(i); |
| 351 |
|
| 352 |
dict_mem_fill_column_struct(col, i, mtype, prtype, len, is_visible, phy_pos, |
| 353 |
v_added, v_dropped); |
| 354 |
} |
| 355 |
|
| 356 |
void dict_mem_fill_column_struct(dict_col_t *column, ulint col_pos, ulint mtype, |
| 357 |
ulint prtype, ulint col_len, bool is_visible, |
| 358 |
uint32_t phy_pos, uint8_t v_added, |
| 359 |
uint8_t v_dropped) { |
| 360 |
column->ind = (unsigned int)col_pos; |
| 361 |
column->ord_part = 0; |
| 362 |
column->max_prefix = 0; |
| 363 |
column->mtype = (unsigned int)mtype; |
| 364 |
column->prtype = (unsigned int)prtype; |
| 365 |
column->len = (unsigned int)col_len; |
| 366 |
column->instant_default = nullptr; |
| 367 |
column->is_visible = is_visible; |
| 368 |
column->set_phy_pos(phy_pos); |
| 369 |
column->set_version_added(v_added); |
| 370 |
column->set_version_dropped(v_dropped); |
| 371 |
#ifndef UNIV_HOTBACKUP |
| 372 |
#ifndef UNIV_LIBRARY |
| 373 |
ulint mbminlen; |
| 374 |
ulint mbmaxlen; |
| 375 |
dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen); |
| 376 |
column->set_mbminmaxlen(mbminlen, mbmaxlen); |
| 377 |
#endif /* !UNIV_LIBRARY */ |
| 378 |
#endif /* !UNIV_HOTBACKUP */ |
| 379 |
} |