| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2011, 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 fts/fts0fts.cc |
| 29 |
Full Text Search interface |
| 30 |
***********************************************************************/ |
| 31 |
|
| 32 |
#include <current_thd.h> |
| 33 |
#include <sys/types.h> |
| 34 |
#include <new> |
| 35 |
|
| 36 |
#include "btr0pcur.h" |
| 37 |
#include "dict0priv.h" |
| 38 |
#include "dict0stats.h" |
| 39 |
#include "dict0types.h" |
| 40 |
#include "fts0fts.h" |
| 41 |
#include "fts0plugin.h" |
| 42 |
#include "fts0priv.h" |
| 43 |
#include "fts0types.h" |
| 44 |
#include "fts0types.ic" |
| 45 |
#include "fts0vlc.ic" |
| 46 |
#include "ha_prototypes.h" |
| 47 |
#include "lob0lob.h" |
| 48 |
|
| 49 |
#include "my_dbug.h" |
| 50 |
|
| 51 |
#include "dict0dd.h" |
| 52 |
#include "lob0lob.h" |
| 53 |
#include "row0mysql.h" |
| 54 |
#include "row0sel.h" |
| 55 |
#include "row0upd.h" |
| 56 |
#include "sync0sync.h" |
| 57 |
#include "trx0roll.h" |
| 58 |
#include "ut0new.h" |
| 59 |
|
| 60 |
static const ulint FTS_MAX_ID_LEN = 32; |
| 61 |
|
| 62 |
/** Column name from the FTS config table */ |
| 63 |
#define FTS_MAX_CACHE_SIZE_IN_MB "cache_size_in_mb" |
| 64 |
|
| 65 |
/** Verify if a aux table name is a obsolete table |
| 66 |
by looking up the key word in the obsolete table names */ |
| 67 |
#define FTS_IS_OBSOLETE_AUX_TABLE(table_name) \ |
| 68 |
(strstr((table_name), "DOC_ID") != NULL || \ |
| 69 |
strstr((table_name), "ADDED") != NULL || \ |
| 70 |
strstr((table_name), "STOPWORDS") != NULL) |
| 71 |
|
| 72 |
/** This is maximum FTS cache for each table and would be |
| 73 |
a configurable variable */ |
| 74 |
ulong fts_max_cache_size; |
| 75 |
|
| 76 |
/** Whether the total memory used for FTS cache is exhausted, and we will |
| 77 |
need a sync to free some memory */ |
| 78 |
bool fts_need_sync = false; |
| 79 |
|
| 80 |
/** Variable specifying the total memory allocated for FTS cache */ |
| 81 |
ulong fts_max_total_cache_size; |
| 82 |
|
| 83 |
/** This is FTS result cache limit for each query and would be |
| 84 |
a configurable variable */ |
| 85 |
ulong fts_result_cache_limit; |
| 86 |
|
| 87 |
/** Variable specifying the maximum FTS max token size */ |
| 88 |
ulong fts_max_token_size; |
| 89 |
|
| 90 |
/** Variable specifying the minimum FTS max token size */ |
| 91 |
ulong fts_min_token_size; |
| 92 |
|
| 93 |
// FIXME: testing |
| 94 |
static std::chrono::steady_clock::duration elapsed_time; |
| 95 |
static ulint n_nodes = 0; |
| 96 |
|
| 97 |
#ifdef FTS_CACHE_SIZE_DEBUG |
| 98 |
/** The cache size permissible lower limit (1K) */ |
| 99 |
static const ulint FTS_CACHE_SIZE_LOWER_LIMIT_IN_MB = 1; |
| 100 |
|
| 101 |
/** The cache size permissible upper limit (1G) */ |
| 102 |
static const ulint FTS_CACHE_SIZE_UPPER_LIMIT_IN_MB = 1024; |
| 103 |
#endif |
| 104 |
|
| 105 |
/** Time to sleep after DEADLOCK error before retrying operation in |
| 106 |
milliseconds. */ |
| 107 |
static constexpr uint32_t FTS_DEADLOCK_RETRY_WAIT_MS = 100; |
| 108 |
|
| 109 |
/** variable to record innodb_fts_internal_tbl_name for information |
| 110 |
schema table INNODB_FTS_INSERTED etc. */ |
| 111 |
char *fts_internal_tbl_name = nullptr; |
| 112 |
|
| 113 |
/** InnoDB default stopword list: |
| 114 |
There are different versions of stopwords, the stop words listed |
| 115 |
below comes from "Google Stopword" list. Reference: |
| 116 |
http://meta.wikimedia.org/wiki/Stop_word_list/google_stop_word_list. |
| 117 |
The final version of InnoDB default stopword list is still pending |
| 118 |
for decision */ |
| 119 |
const char *fts_default_stopword[] = { |
| 120 |
"a", "about", "an", "are", "as", "at", "be", "by", |
| 121 |
"com", "de", "en", "for", "from", "how", "i", "in", |
| 122 |
"is", "it", "la", "of", "on", "or", "that", "the", |
| 123 |
"this", "to", "was", "what", "when", "where", "who", "will", |
| 124 |
"with", "und", "the", "www", nullptr}; |
| 125 |
|
| 126 |
/** FTS auxiliary table prefix that are common to all FT indexes.*/ |
| 127 |
const char *FTS_PREFIX = "fts_"; |
| 128 |
|
| 129 |
/** FTS auxiliary table prefix that are common to all FT indexes.*/ |
| 130 |
const char *FTS_PREFIX_5_7 = "FTS_"; |
| 131 |
|
| 132 |
/** FTS auxiliary table suffixes that are common to all FT indexes. */ |
| 133 |
const char *fts_common_tables[] = {"being_deleted", "being_deleted_cache", |
| 134 |
"config", "deleted", |
| 135 |
"deleted_cache", nullptr}; |
| 136 |
|
| 137 |
const char *FTS_SUFFIX_BEING_DELETED = fts_common_tables[0]; |
| 138 |
const char *FTS_SUFFIX_BEING_DELETED_CACHE = fts_common_tables[1]; |
| 139 |
const char *FTS_SUFFIX_CONFIG = fts_common_tables[2]; |
| 140 |
const char *FTS_SUFFIX_DELETED = fts_common_tables[3]; |
| 141 |
const char *FTS_SUFFIX_DELETED_CACHE = fts_common_tables[4]; |
| 142 |
|
| 143 |
/** FTS auxiliary table suffixes that are common to all FT indexes. */ |
| 144 |
const char *fts_common_tables_5_7[] = {"BEING_DELETED", "BEING_DELETED_CACHE", |
| 145 |
"CONFIG", "DELETED", |
| 146 |
"DELETED_CACHE", nullptr}; |
| 147 |
|
| 148 |
const char *FTS_SUFFIX_CONFIG_5_7 = fts_common_tables_5_7[2]; |
| 149 |
|
| 150 |
/** FTS auxiliary INDEX split intervals. */ |
| 151 |
const fts_index_selector_t fts_index_selector[] = { |
| 152 |
{9, "index_1"}, {65, "index_2"}, {70, "index_3"}, {75, "index_4"}, |
| 153 |
{80, "index_5"}, {85, "index_6"}, {0, nullptr}}; |
| 154 |
|
| 155 |
/** FTS auxiliary INDEX split intervals. */ |
| 156 |
const fts_index_selector_t fts_index_selector_5_7[] = { |
| 157 |
{9, "INDEX_1"}, {65, "INDEX_2"}, {70, "INDEX_3"}, {75, "INDEX_4"}, |
| 158 |
{80, "INDEX_5"}, {85, "INDEX_6"}, {0, nullptr}}; |
| 159 |
|
| 160 |
/** Default config values for FTS indexes on a table. */ |
| 161 |
static const char *fts_config_table_insert_values_sql = |
| 162 |
"BEGIN\n" |
| 163 |
"\n" |
| 164 |
"INSERT INTO $config_table VALUES('" FTS_MAX_CACHE_SIZE_IN_MB |
| 165 |
"', '256');\n" |
| 166 |
"" |
| 167 |
"INSERT INTO $config_table VALUES('" FTS_OPTIMIZE_LIMIT_IN_SECS |
| 168 |
"', '180');\n" |
| 169 |
"" |
| 170 |
"INSERT INTO $config_table VALUES ('" FTS_SYNCED_DOC_ID |
| 171 |
"', '0');\n" |
| 172 |
"" |
| 173 |
"INSERT INTO $config_table VALUES ('" FTS_TOTAL_DELETED_COUNT |
| 174 |
"', '0');\n" |
| 175 |
"" /* Note: 0 == FTS_TABLE_STATE_RUNNING */ |
| 176 |
"INSERT INTO $config_table VALUES ('" FTS_TABLE_STATE "', '0');\n"; |
| 177 |
|
| 178 |
/** FTS tokenize parameter for plugin parser */ |
| 179 |
struct fts_tokenize_param_t { |
| 180 |
fts_doc_t *result_doc; /*!< Result doc for tokens */ |
| 181 |
ulint add_pos; /*!< Added position for tokens */ |
| 182 |
}; |
| 183 |
|
| 184 |
/** Run SYNC on the table, i.e., write out data from the cache to the |
| 185 |
FTS auxiliary INDEX table and clear the cache at the end. |
| 186 |
@param[in,out] sync sync state |
| 187 |
@param[in] unlock_cache whether unlock cache lock when write node |
| 188 |
@param[in] wait whether wait when a sync is in progress |
| 189 |
@param[in] has_dict whether has dict operation lock |
| 190 |
@return DB_SUCCESS if all OK */ |
| 191 |
static dberr_t fts_sync(fts_sync_t *sync, bool unlock_cache, bool wait, |
| 192 |
bool has_dict); |
| 193 |
|
| 194 |
/** Release all resources help by the words rb tree e.g., the node ilist. */ |
| 195 |
static void fts_words_free(ib_rbt_t *words); /*!< in: rb tree of words */ |
| 196 |
#ifdef FTS_CACHE_SIZE_DEBUG |
| 197 |
/** Read the max cache size parameter from the config table. */ |
| 198 |
static void fts_update_max_cache_size(fts_sync_t *sync); /*!< in: sync state */ |
| 199 |
#endif |
| 200 |
|
| 201 |
/** This function fetches the document just inserted right before |
| 202 |
we commit the transaction, and tokenize the inserted text data |
| 203 |
and insert into FTS auxiliary table and its cache. |
| 204 |
@param[in] ftt FTS transaction table |
| 205 |
@param[in] doc_id doc id |
| 206 |
@param[in] fts_indexes affected FTS indexes |
| 207 |
@return true if successful */ |
| 208 |
static ulint fts_add_doc_by_id(fts_trx_table_t *ftt, doc_id_t doc_id, |
| 209 |
ib_vector_t *fts_indexes); |
| 210 |
|
| 211 |
/** Update the last document id. This function could create a new |
| 212 |
transaction to update the last document id. |
| 213 |
@return DB_SUCCESS if OK */ |
| 214 |
static dberr_t fts_update_sync_doc_id( |
| 215 |
const dict_table_t *table, /*!< in: table */ |
| 216 |
const char *table_name, /*!< in: table name, or NULL */ |
| 217 |
doc_id_t doc_id, /*!< in: last document id */ |
| 218 |
trx_t *trx); /*!< in: update trx, or NULL */ |
| 219 |
|
| 220 |
/** Tokenize a document. |
| 221 |
@param[in,out] doc document to tokenize |
| 222 |
@param[out] result tokenization result |
| 223 |
@param[in] parser pluggable parser */ |
| 224 |
static void fts_tokenize_document(fts_doc_t *doc, fts_doc_t *result, |
| 225 |
st_mysql_ftparser *parser); |
| 226 |
|
| 227 |
/** Continue to tokenize a document. |
| 228 |
@param[in,out] doc document to tokenize |
| 229 |
@param[in] add_pos add this position to all tokens from this tokenization |
| 230 |
@param[out] result tokenization result |
| 231 |
@param[in] parser pluggable parser */ |
| 232 |
static void fts_tokenize_document_next(fts_doc_t *doc, ulint add_pos, |
| 233 |
fts_doc_t *result, |
| 234 |
st_mysql_ftparser *parser); |
| 235 |
|
| 236 |
namespace detail { |
| 237 |
/** Create the vector of fts_get_doc_t instances. |
| 238 |
@param[in,out] cache fts cache |
| 239 |
@return vector of fts_get_doc_t instances */ |
| 240 |
ib_vector_t *fts_get_docs_create(fts_cache_t *cache); |
| 241 |
|
| 242 |
/** Free the FTS cache. |
| 243 |
@param[in,out] cache to be freed */ |
| 244 |
void fts_cache_destroy(fts_cache_t *cache) { |
| 245 |
rw_lock_free(&cache->lock); |
| 246 |
rw_lock_free(&cache->init_lock); |
| 247 |
mutex_free(&cache->optimize_lock); |
| 248 |
mutex_free(&cache->deleted_lock); |
| 249 |
mutex_free(&cache->doc_id_lock); |
| 250 |
os_event_destroy(cache->sync->event); |
| 251 |
|
| 252 |
if (cache->stopword_info.cached_stopword) { |
| 253 |
rbt_free(cache->stopword_info.cached_stopword); |
| 254 |
} |
| 255 |
|
| 256 |
if (cache->sync_heap->arg) { |
| 257 |
mem_heap_free(static_cast<mem_heap_t *>(cache->sync_heap->arg)); |
| 258 |
} |
| 259 |
|
| 260 |
mem_heap_free(cache->cache_heap); |
| 261 |
} |
| 262 |
|
| 263 |
} // namespace detail |
| 264 |
using detail::fts_cache_destroy; |
| 265 |
using detail::fts_get_docs_create; |
| 266 |
|
| 267 |
/** Get a character set based on precise type. |
| 268 |
@param prtype precise type |
| 269 |
@return the corresponding character set */ |
| 270 |
static inline CHARSET_INFO *fts_get_charset(ulint prtype) { |
| 271 |
#ifdef UNIV_DEBUG |
| 272 |
switch (prtype & DATA_MYSQL_TYPE_MASK) { |
| 273 |
case MYSQL_TYPE_BIT: |
| 274 |
case MYSQL_TYPE_STRING: |
| 275 |
case MYSQL_TYPE_VAR_STRING: |
| 276 |
case MYSQL_TYPE_TINY_BLOB: |
| 277 |
case MYSQL_TYPE_MEDIUM_BLOB: |
| 278 |
case MYSQL_TYPE_BLOB: |
| 279 |
case MYSQL_TYPE_LONG_BLOB: |
| 280 |
case MYSQL_TYPE_VARCHAR: |
| 281 |
break; |
| 282 |
default: |
| 283 |
ut_error; |
| 284 |
} |
| 285 |
#endif /* UNIV_DEBUG */ |
| 286 |
|
| 287 |
uint cs_num = (uint)dtype_get_charset_coll(prtype); |
| 288 |
|
| 289 |
if (CHARSET_INFO *cs = get_charset(cs_num, MYF(MY_WME))) { |
| 290 |
return (cs); |
| 291 |
} |
| 292 |
|
| 293 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_461) |
| 294 |
<< "Unable to find charset-collation " << cs_num; |
| 295 |
return (nullptr); |
| 296 |
} |
| 297 |
|
| 298 |
/** This function loads the default InnoDB stopword list */ |
| 299 |
static void fts_load_default_stopword( |
| 300 |
fts_stopword_t *stopword_info) /*!< in: stopword info */ |
| 301 |
{ |
| 302 |
fts_string_t str; |
| 303 |
mem_heap_t *heap; |
| 304 |
ib_alloc_t *allocator; |
| 305 |
ib_rbt_t *stop_words; |
| 306 |
|
| 307 |
allocator = stopword_info->heap; |
| 308 |
heap = static_cast<mem_heap_t *>(allocator->arg); |
| 309 |
|
| 310 |
if (!stopword_info->cached_stopword) { |
| 311 |
stopword_info->cached_stopword = |
| 312 |
rbt_create_arg_cmp(sizeof(fts_tokenizer_word_t), innobase_fts_text_cmp, |
| 313 |
&my_charset_latin1); |
| 314 |
} |
| 315 |
|
| 316 |
stop_words = stopword_info->cached_stopword; |
| 317 |
|
| 318 |
str.f_n_char = 0; |
| 319 |
|
| 320 |
for (ulint i = 0; fts_default_stopword[i]; ++i) { |
| 321 |
char *word; |
| 322 |
fts_tokenizer_word_t new_word; |
| 323 |
|
| 324 |
/* We are going to duplicate the value below. */ |
| 325 |
word = const_cast<char *>(fts_default_stopword[i]); |
| 326 |
|
| 327 |
new_word.nodes = ib_vector_create(allocator, sizeof(fts_node_t), 4); |
| 328 |
|
| 329 |
str.f_len = ut_strlen(word); |
| 330 |
str.f_str = reinterpret_cast<byte *>(word); |
| 331 |
|
| 332 |
fts_string_dup(&new_word.text, &str, heap); |
| 333 |
|
| 334 |
rbt_insert(stop_words, &new_word, &new_word); |
| 335 |
} |
| 336 |
|
| 337 |
stopword_info->status = STOPWORD_FROM_DEFAULT; |
| 338 |
} |
| 339 |
|
| 340 |
/** Callback function to read a single stopword value. |
| 341 |
@return Always return true */ |
| 342 |
static bool fts_read_stopword(void *row, /*!< in: sel_node_t* */ |
| 343 |
void *user_arg) /*!< in: pointer to ib_vector_t */ |
| 344 |
{ |
| 345 |
ib_alloc_t *allocator; |
| 346 |
fts_stopword_t *stopword_info; |
| 347 |
sel_node_t *sel_node; |
| 348 |
que_node_t *exp; |
| 349 |
ib_rbt_t *stop_words; |
| 350 |
dfield_t *dfield; |
| 351 |
fts_string_t str; |
| 352 |
mem_heap_t *heap; |
| 353 |
ib_rbt_bound_t parent; |
| 354 |
|
| 355 |
sel_node = static_cast<sel_node_t *>(row); |
| 356 |
stopword_info = static_cast<fts_stopword_t *>(user_arg); |
| 357 |
|
| 358 |
stop_words = stopword_info->cached_stopword; |
| 359 |
allocator = static_cast<ib_alloc_t *>(stopword_info->heap); |
| 360 |
heap = static_cast<mem_heap_t *>(allocator->arg); |
| 361 |
|
| 362 |
exp = sel_node->select_list; |
| 363 |
|
| 364 |
/* We only need to read the first column */ |
| 365 |
dfield = que_node_get_val(exp); |
| 366 |
|
| 367 |
str.f_n_char = 0; |
| 368 |
str.f_str = static_cast<byte *>(dfield_get_data(dfield)); |
| 369 |
str.f_len = dfield_get_len(dfield); |
| 370 |
|
| 371 |
/* Only create new node if it is a value not already existed */ |
| 372 |
if (str.f_len != UNIV_SQL_NULL && |
| 373 |
rbt_search(stop_words, &parent, &str) != 0) { |
| 374 |
fts_tokenizer_word_t new_word; |
| 375 |
|
| 376 |
new_word.nodes = ib_vector_create(allocator, sizeof(fts_node_t), 4); |
| 377 |
|
| 378 |
new_word.text.f_str = |
| 379 |
static_cast<byte *>(mem_heap_alloc(heap, str.f_len + 1)); |
| 380 |
|
| 381 |
memcpy(new_word.text.f_str, str.f_str, str.f_len); |
| 382 |
|
| 383 |
new_word.text.f_n_char = 0; |
| 384 |
new_word.text.f_len = str.f_len; |
| 385 |
new_word.text.f_str[str.f_len] = 0; |
| 386 |
|
| 387 |
rbt_insert(stop_words, &new_word, &new_word); |
| 388 |
} |
| 389 |
|
| 390 |
return true; |
| 391 |
} |
| 392 |
|
| 393 |
/** Load user defined stopword from designated user table |
| 394 |
@return true if load operation is successful */ |
| 395 |
static bool fts_load_user_stopword( |
| 396 |
const char *stopword_table_name, /*!< in: Stopword table |
| 397 |
name */ |
| 398 |
fts_stopword_t *stopword_info) /*!< in: Stopword info */ |
| 399 |
{ |
| 400 |
pars_info_t *info; |
| 401 |
que_t *graph; |
| 402 |
dberr_t error = DB_SUCCESS; |
| 403 |
bool ret = true; |
| 404 |
trx_t *trx; |
| 405 |
|
| 406 |
trx = trx_allocate_for_background(); |
| 407 |
trx->op_info = "Load user stopword table into FTS cache"; |
| 408 |
|
| 409 |
/* Validate the user table existence and in the right |
| 410 |
format */ |
| 411 |
stopword_info->charset = fts_valid_stopword_table(stopword_table_name); |
| 412 |
if (!stopword_info->charset) { |
| 413 |
ret = false; |
| 414 |
goto cleanup; |
| 415 |
} else if (!stopword_info->cached_stopword) { |
| 416 |
/* Create the stopword RB tree with the stopword column |
| 417 |
charset. All comparison will use this charset */ |
| 418 |
stopword_info->cached_stopword = |
| 419 |
rbt_create_arg_cmp(sizeof(fts_tokenizer_word_t), innobase_fts_text_cmp, |
| 420 |
stopword_info->charset); |
| 421 |
} |
| 422 |
|
| 423 |
info = pars_info_create(); |
| 424 |
|
| 425 |
pars_info_bind_id(info, true, "table_stopword", stopword_table_name); |
| 426 |
|
| 427 |
pars_info_bind_function(info, "my_func", fts_read_stopword, stopword_info); |
| 428 |
|
| 429 |
graph = fts_parse_sql(nullptr, info, |
| 430 |
"DECLARE FUNCTION my_func;\n" |
| 431 |
"DECLARE CURSOR c IS" |
| 432 |
" SELECT value" |
| 433 |
" FROM $table_stopword;\n" |
| 434 |
"BEGIN\n" |
| 435 |
"\n" |
| 436 |
"OPEN c;\n" |
| 437 |
"WHILE 1 = 1 LOOP\n" |
| 438 |
" FETCH c INTO my_func();\n" |
| 439 |
" IF c % NOTFOUND THEN\n" |
| 440 |
" EXIT;\n" |
| 441 |
" END IF;\n" |
| 442 |
"END LOOP;\n" |
| 443 |
"CLOSE c;"); |
| 444 |
|
| 445 |
for (;;) { |
| 446 |
error = fts_eval_sql(trx, graph); |
| 447 |
|
| 448 |
if (error == DB_SUCCESS) { |
| 449 |
fts_sql_commit(trx); |
| 450 |
stopword_info->status = STOPWORD_USER_TABLE; |
| 451 |
break; |
| 452 |
} else { |
| 453 |
fts_sql_rollback(trx); |
| 454 |
|
| 455 |
if (error == DB_LOCK_WAIT_TIMEOUT) { |
| 456 |
ib::warn(ER_IB_MSG_462) << "Lock wait timeout reading user" |
| 457 |
" stopword table. Retrying!"; |
| 458 |
|
| 459 |
trx->error_state = DB_SUCCESS; |
| 460 |
} else { |
| 461 |
ib::error(ER_IB_MSG_463) << "Error '" << ut_strerr(error) |
| 462 |
<< "' while reading user stopword" |
| 463 |
" table."; |
| 464 |
ret = false; |
| 465 |
break; |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
que_graph_free(graph); |
| 471 |
|
| 472 |
cleanup: |
| 473 |
trx_free_for_background(trx); |
| 474 |
return (ret); |
| 475 |
} |
| 476 |
|
| 477 |
/** Initialize the index cache. */ |
| 478 |
static void fts_index_cache_init( |
| 479 |
ib_alloc_t *allocator, /*!< in: the allocator to use */ |
| 480 |
fts_index_cache_t *index_cache) /*!< in: index cache */ |
| 481 |
{ |
| 482 |
ulint i; |
| 483 |
|
| 484 |
ut_a(index_cache->words == nullptr); |
| 485 |
|
| 486 |
index_cache->words = |
| 487 |
rbt_create_arg_cmp(sizeof(fts_tokenizer_word_t), innobase_fts_text_cmp, |
| 488 |
index_cache->charset); |
| 489 |
|
| 490 |
ut_a(index_cache->doc_stats == nullptr); |
| 491 |
|
| 492 |
index_cache->doc_stats = |
| 493 |
ib_vector_create(allocator, sizeof(fts_doc_stats_t), 4); |
| 494 |
|
| 495 |
for (i = 0; i < FTS_NUM_AUX_INDEX; ++i) { |
| 496 |
ut_a(index_cache->ins_graph[i] == nullptr); |
| 497 |
ut_a(index_cache->sel_graph[i] == nullptr); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
/** Initialize FTS cache. */ |
| 502 |
void fts_cache_init(fts_cache_t *cache) /*!< in: cache to initialize */ |
| 503 |
{ |
| 504 |
ulint i; |
| 505 |
|
| 506 |
/* Just to make sure */ |
| 507 |
ut_a(cache->sync_heap->arg == nullptr); |
| 508 |
|
| 509 |
cache->sync_heap->arg = mem_heap_create(1024, UT_LOCATION_HERE); |
| 510 |
|
| 511 |
cache->total_size = 0; |
| 512 |
cache->total_size_before_sync = 0; |
| 513 |
|
| 514 |
mutex_enter((ib_mutex_t *)&cache->deleted_lock); |
| 515 |
cache->deleted_doc_ids = |
| 516 |
ib_vector_create(cache->sync_heap, sizeof(fts_update_t), 4); |
| 517 |
mutex_exit((ib_mutex_t *)&cache->deleted_lock); |
| 518 |
|
| 519 |
/* Reset the cache data for all the FTS indexes. */ |
| 520 |
for (i = 0; i < ib_vector_size(cache->indexes); ++i) { |
| 521 |
fts_index_cache_t *index_cache; |
| 522 |
|
| 523 |
index_cache = |
| 524 |
static_cast<fts_index_cache_t *>(ib_vector_get(cache->indexes, i)); |
| 525 |
|
| 526 |
fts_index_cache_init(cache->sync_heap, index_cache); |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
/** Create a FTS cache. */ |
| 531 |
fts_cache_t *fts_cache_create( |
| 532 |
dict_table_t *table) /*!< in: table owns the FTS cache */ |
| 533 |
{ |
| 534 |
mem_heap_t *heap; |
| 535 |
fts_cache_t *cache; |
| 536 |
|
| 537 |
heap = static_cast<mem_heap_t *>(mem_heap_create(512, UT_LOCATION_HERE)); |
| 538 |
|
| 539 |
cache = static_cast<fts_cache_t *>(mem_heap_zalloc(heap, sizeof(*cache))); |
| 540 |
|
| 541 |
cache->cache_heap = heap; |
| 542 |
|
| 543 |
rw_lock_create(fts_cache_rw_lock_key, &cache->lock, LATCH_ID_FTS_CACHE); |
| 544 |
|
| 545 |
rw_lock_create(fts_cache_init_rw_lock_key, &cache->init_lock, |
| 546 |
LATCH_ID_FTS_CACHE_INIT); |
| 547 |
|
| 548 |
mutex_create(LATCH_ID_FTS_DELETE, &cache->deleted_lock); |
| 549 |
|
| 550 |
mutex_create(LATCH_ID_FTS_OPTIMIZE, &cache->optimize_lock); |
| 551 |
|
| 552 |
mutex_create(LATCH_ID_FTS_DOC_ID, &cache->doc_id_lock); |
| 553 |
|
| 554 |
/* This is the heap used to create the cache itself. */ |
| 555 |
cache->self_heap = ib_heap_allocator_create(heap); |
| 556 |
|
| 557 |
/* This is a transient heap, used for storing sync data. */ |
| 558 |
cache->sync_heap = ib_heap_allocator_create(heap); |
| 559 |
cache->sync_heap->arg = nullptr; |
| 560 |
|
| 561 |
cache->sync = |
| 562 |
static_cast<fts_sync_t *>(mem_heap_zalloc(heap, sizeof(fts_sync_t))); |
| 563 |
|
| 564 |
cache->sync->table = table; |
| 565 |
cache->sync->event = os_event_create(); |
| 566 |
|
| 567 |
/* Create the index cache vector that will hold the inverted indexes. */ |
| 568 |
cache->indexes = |
| 569 |
ib_vector_create(cache->self_heap, sizeof(fts_index_cache_t), 2); |
| 570 |
|
| 571 |
fts_cache_init(cache); |
| 572 |
|
| 573 |
cache->stopword_info.cached_stopword = nullptr; |
| 574 |
cache->stopword_info.charset = nullptr; |
| 575 |
|
| 576 |
cache->stopword_info.heap = cache->self_heap; |
| 577 |
|
| 578 |
cache->stopword_info.status = STOPWORD_NOT_INIT; |
| 579 |
|
| 580 |
return (cache); |
| 581 |
} |
| 582 |
|
| 583 |
/** Add a newly create index into FTS cache */ |
| 584 |
void fts_add_index(dict_index_t *index, /*!< FTS index to be added */ |
| 585 |
dict_table_t *table) /*!< table */ |
| 586 |
{ |
| 587 |
fts_t *fts = table->fts; |
| 588 |
fts_cache_t *cache; |
| 589 |
fts_index_cache_t *index_cache; |
| 590 |
|
| 591 |
ut_ad(fts); |
| 592 |
cache = table->fts->cache; |
| 593 |
|
| 594 |
rw_lock_x_lock(&cache->init_lock, UT_LOCATION_HERE); |
| 595 |
|
| 596 |
ib_vector_push(fts->indexes, &index); |
| 597 |
|
| 598 |
index_cache = fts_find_index_cache(cache, index); |
| 599 |
|
| 600 |
if (!index_cache) { |
| 601 |
/* Add new index cache structure */ |
| 602 |
index_cache = fts_cache_index_cache_create(table, index); |
| 603 |
} |
| 604 |
|
| 605 |
rw_lock_x_unlock(&cache->init_lock); |
| 606 |
} |
| 607 |
|
| 608 |
namespace detail { |
| 609 |
|
| 610 |
/** recalibrate get_doc structure after index_cache in cache->indexes changed */ |
| 611 |
void fts_reset_get_doc(fts_cache_t *cache) /*!< in: FTS index cache */ |
| 612 |
{ |
| 613 |
ut_ad(rw_lock_own(&cache->init_lock, RW_LOCK_X)); |
| 614 |
|
| 615 |
ib_vector_reset(cache->get_docs); |
| 616 |
|
| 617 |
for (unsigned long i = 0; i < ib_vector_size(cache->indexes); i++) { |
| 618 |
fts_index_cache_t *ind_cache = |
| 619 |
static_cast<fts_index_cache_t *>(ib_vector_get(cache->indexes, i)); |
| 620 |
|
| 621 |
fts_get_doc_t *get_doc = |
| 622 |
static_cast<fts_get_doc_t *>(ib_vector_push(cache->get_docs, nullptr)); |
| 623 |
|
| 624 |
memset(get_doc, 0x0, sizeof(*get_doc)); |
| 625 |
|
| 626 |
get_doc->index_cache = ind_cache; |
| 627 |
get_doc->cache = cache; |
| 628 |
} |
| 629 |
|
| 630 |
ut_ad(ib_vector_size(cache->get_docs) == ib_vector_size(cache->indexes)); |
| 631 |
} |
| 632 |
|
| 633 |
} // namespace detail |
| 634 |
|
| 635 |
using detail::fts_reset_get_doc; |
| 636 |
|
| 637 |
/** Check an index is in the table->indexes list |
| 638 |
@return true if it exists */ |
| 639 |
static bool fts_in_dict_index( |
| 640 |
dict_table_t *table, /*!< in: Table */ |
| 641 |
dict_index_t *index_check) /*!< in: index to be checked */ |
| 642 |
{ |
| 643 |
dict_index_t *index; |
| 644 |
|
| 645 |
for (index = table->first_index(); index != nullptr; index = index->next()) { |
| 646 |
if (index == index_check) { |
| 647 |
return true; |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
return false; |
| 652 |
} |
| 653 |
|
| 654 |
/** Check an index is in the fts->cache->indexes list |
| 655 |
@return true if it exists */ |
| 656 |
static bool fts_in_index_cache( |
| 657 |
dict_table_t *table, /*!< in: Table */ |
| 658 |
dict_index_t *index) /*!< in: index to be checked */ |
| 659 |
{ |
| 660 |
ulint i; |
| 661 |
|
| 662 |
for (i = 0; i < ib_vector_size(table->fts->cache->indexes); i++) { |
| 663 |
fts_index_cache_t *index_cache; |
| 664 |
|
| 665 |
index_cache = static_cast<fts_index_cache_t *>( |
| 666 |
ib_vector_get(table->fts->cache->indexes, i)); |
| 667 |
|
| 668 |
if (index_cache->index == index) { |
| 669 |
return true; |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
return false; |
| 674 |
} |
| 675 |
|
| 676 |
/** Check indexes in the fts->indexes is also present in index cache and |
| 677 |
table->indexes list |
| 678 |
@return true if all indexes match */ |
| 679 |
bool fts_check_cached_index( |
| 680 |
dict_table_t *table) /*!< in: Table where indexes are dropped */ |
| 681 |
{ |
| 682 |
ulint i; |
| 683 |
|
| 684 |
if (!table->fts || !table->fts->cache) { |
| 685 |
return true; |
| 686 |
} |
| 687 |
|
| 688 |
ut_a(ib_vector_size(table->fts->indexes) == |
| 689 |
ib_vector_size(table->fts->cache->indexes)); |
| 690 |
|
| 691 |
for (i = 0; i < ib_vector_size(table->fts->indexes); i++) { |
| 692 |
dict_index_t *index; |
| 693 |
|
| 694 |
index = static_cast<dict_index_t *>(ib_vector_getp(table->fts->indexes, i)); |
| 695 |
|
| 696 |
if (!fts_in_index_cache(table, index)) { |
| 697 |
return false; |
| 698 |
} |
| 699 |
|
| 700 |
if (!fts_in_dict_index(table, index)) { |
| 701 |
return false; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
return true; |
| 706 |
} |
| 707 |
|
| 708 |
/** Drop auxiliary tables related to an FTS index |
| 709 |
@param[in] table Table where indexes are dropped |
| 710 |
@param[in] index Index to be dropped |
| 711 |
@param[in] trx Transaction for the drop |
| 712 |
@param[in,out] aux_vec Aux table name vector |
| 713 |
@param[in] adding_another Another FTS index is to be added as part |
| 714 |
of the same transaction |
| 715 |
@return DB_SUCCESS or error number */ |
| 716 |
dberr_t fts_drop_index(dict_table_t *table, dict_index_t *index, trx_t *trx, |
| 717 |
aux_name_vec_t *aux_vec, bool adding_another) { |
| 718 |
ib_vector_t *indexes = table->fts->indexes; |
| 719 |
dberr_t err = DB_SUCCESS; |
| 720 |
|
| 721 |
ut_a(indexes); |
| 722 |
|
| 723 |
const bool last_index = (ib_vector_size(indexes) == 1 && |
| 724 |
(index == static_cast<dict_index_t *>(ib_vector_getp( |
| 725 |
table->fts->indexes, 0)))) || |
| 726 |
ib_vector_is_empty(indexes); |
| 727 |
|
| 728 |
if (last_index && !adding_another) { |
| 729 |
doc_id_t current_doc_id; |
| 730 |
doc_id_t first_doc_id; |
| 731 |
|
| 732 |
/* If we are dropping the only FTS index of the table, |
| 733 |
remove it from optimize thread */ |
| 734 |
fts_optimize_remove_table(table); |
| 735 |
|
| 736 |
DICT_TF2_FLAG_UNSET(table, DICT_TF2_FTS); |
| 737 |
|
| 738 |
/* If Doc ID column is not added internally by FTS index, |
| 739 |
we can drop all FTS auxiliary tables. Otherwise, we will |
| 740 |
need to keep some common table such as CONFIG table, so |
| 741 |
as to keep track of incrementing Doc IDs */ |
| 742 |
if (!DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID)) { |
| 743 |
err = fts_drop_tables(trx, table, aux_vec); |
| 744 |
|
| 745 |
fts_free(table); |
| 746 |
|
| 747 |
return (err); |
| 748 |
} else { |
| 749 |
if (!(index->type & DICT_CORRUPT) && !dict_table_is_discarded(table)) { |
| 750 |
err = fts_empty_common_tables(trx, table); |
| 751 |
ut_ad(err == DB_SUCCESS); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
current_doc_id = table->fts->cache->next_doc_id; |
| 756 |
first_doc_id = table->fts->cache->first_doc_id; |
| 757 |
fts_cache_clear(table->fts->cache); |
| 758 |
fts_cache_destroy(table->fts->cache); |
| 759 |
table->fts->cache = fts_cache_create(table); |
| 760 |
table->fts->cache->next_doc_id = current_doc_id; |
| 761 |
table->fts->cache->first_doc_id = first_doc_id; |
| 762 |
} else { |
| 763 |
fts_cache_t *cache = table->fts->cache; |
| 764 |
fts_index_cache_t *index_cache; |
| 765 |
|
| 766 |
rw_lock_x_lock(&cache->init_lock, UT_LOCATION_HERE); |
| 767 |
|
| 768 |
index_cache = fts_find_index_cache(cache, index); |
| 769 |
|
| 770 |
if (index_cache != nullptr) { |
| 771 |
if (index_cache->words) { |
| 772 |
fts_words_free(index_cache->words); |
| 773 |
rbt_free(index_cache->words); |
| 774 |
} |
| 775 |
|
| 776 |
ib_vector_remove(cache->indexes, *(void **)index_cache); |
| 777 |
} |
| 778 |
|
| 779 |
if (cache->get_docs) { |
| 780 |
fts_reset_get_doc(cache); |
| 781 |
} |
| 782 |
|
| 783 |
rw_lock_x_unlock(&cache->init_lock); |
| 784 |
} |
| 785 |
|
| 786 |
err = fts_drop_index_tables(trx, index, aux_vec); |
| 787 |
|
| 788 |
ib_vector_remove(indexes, (const void *)index); |
| 789 |
|
| 790 |
return (err); |
| 791 |
} |
| 792 |
|
| 793 |
/** Create an FTS index cache. */ |
| 794 |
CHARSET_INFO *fts_index_get_charset(dict_index_t *index) /*!< in: FTS index */ |
| 795 |
{ |
| 796 |
CHARSET_INFO *charset = nullptr; |
| 797 |
dict_field_t *field; |
| 798 |
ulint prtype; |
| 799 |
|
| 800 |
field = index->get_field(0); |
| 801 |
prtype = field->col->prtype; |
| 802 |
|
| 803 |
charset = fts_get_charset(prtype); |
| 804 |
|
| 805 |
#ifdef FTS_DEBUG |
| 806 |
/* Set up charset info for this index. Please note all |
| 807 |
field of the FTS index should have the same charset */ |
| 808 |
for (i = 1; i < index->n_fields; i++) { |
| 809 |
CHARSET_INFO *fld_charset; |
| 810 |
|
| 811 |
field = index->get_field(i); |
| 812 |
prtype = field->col->prtype; |
| 813 |
|
| 814 |
fld_charset = fts_get_charset(prtype); |
| 815 |
|
| 816 |
/* All FTS columns should have the same charset */ |
| 817 |
if (charset) { |
| 818 |
ut_a(charset == fld_charset); |
| 819 |
} else { |
| 820 |
charset = fld_charset; |
| 821 |
} |
| 822 |
} |
| 823 |
#endif |
| 824 |
|
| 825 |
return (charset); |
| 826 |
} |
| 827 |
/** Create an FTS index cache. |
| 828 |
@return Index Cache */ |
| 829 |
fts_index_cache_t *fts_cache_index_cache_create( |
| 830 |
dict_table_t *table, /*!< in: table with FTS index */ |
| 831 |
dict_index_t *index) /*!< in: FTS index */ |
| 832 |
{ |
| 833 |
ulint n_bytes; |
| 834 |
fts_index_cache_t *index_cache; |
| 835 |
fts_cache_t *cache = table->fts->cache; |
| 836 |
|
| 837 |
ut_a(cache != nullptr); |
| 838 |
|
| 839 |
ut_ad(rw_lock_own(&cache->init_lock, RW_LOCK_X)); |
| 840 |
|
| 841 |
/* Must not already exist in the cache vector. */ |
| 842 |
ut_a(fts_find_index_cache(cache, index) == nullptr); |
| 843 |
|
| 844 |
index_cache = |
| 845 |
static_cast<fts_index_cache_t *>(ib_vector_push(cache->indexes, nullptr)); |
| 846 |
|
| 847 |
memset(index_cache, 0x0, sizeof(*index_cache)); |
| 848 |
|
| 849 |
index_cache->index = index; |
| 850 |
|
| 851 |
index_cache->charset = fts_index_get_charset(index); |
| 852 |
|
| 853 |
n_bytes = sizeof(que_t *) * FTS_NUM_AUX_INDEX; |
| 854 |
|
| 855 |
index_cache->ins_graph = static_cast<que_t **>(mem_heap_zalloc( |
| 856 |
static_cast<mem_heap_t *>(cache->self_heap->arg), n_bytes)); |
| 857 |
|
| 858 |
index_cache->sel_graph = static_cast<que_t **>(mem_heap_zalloc( |
| 859 |
static_cast<mem_heap_t *>(cache->self_heap->arg), n_bytes)); |
| 860 |
|
| 861 |
fts_index_cache_init(cache->sync_heap, index_cache); |
| 862 |
|
| 863 |
if (cache->get_docs) { |
| 864 |
fts_reset_get_doc(cache); |
| 865 |
} |
| 866 |
|
| 867 |
return (index_cache); |
| 868 |
} |
| 869 |
|
| 870 |
/** Remove a FTS index cache |
| 871 |
@param[in] table table with FTS index |
| 872 |
@param[in] index FTS index */ |
| 873 |
void fts_cache_index_cache_remove(dict_table_t *table, dict_index_t *index) { |
| 874 |
ut_ad(table->fts != nullptr); |
| 875 |
ut_ad(index->type & DICT_FTS); |
| 876 |
|
| 877 |
fts_index_cache_t *index_cache; |
| 878 |
|
| 879 |
rw_lock_x_lock(&table->fts->cache->init_lock, UT_LOCATION_HERE); |
| 880 |
|
| 881 |
index_cache = static_cast<fts_index_cache_t *>( |
| 882 |
fts_find_index_cache(table->fts->cache, index)); |
| 883 |
|
| 884 |
if (index_cache->words != nullptr) { |
| 885 |
rbt_free(index_cache->words); |
| 886 |
index_cache->words = nullptr; |
| 887 |
} |
| 888 |
|
| 889 |
ib_vector_remove(table->fts->cache->indexes, |
| 890 |
*reinterpret_cast<void **>(index_cache)); |
| 891 |
|
| 892 |
rw_lock_x_unlock(&table->fts->cache->init_lock); |
| 893 |
} |
| 894 |
|
| 895 |
/** Release all resources help by the words rb tree e.g., the node ilist. */ |
| 896 |
static void fts_words_free(ib_rbt_t *words) /*!< in: rb tree of words */ |
| 897 |
{ |
| 898 |
const ib_rbt_node_t *rbt_node; |
| 899 |
|
| 900 |
/* Free the resources held by a word. */ |
| 901 |
for (rbt_node = rbt_first(words); rbt_node != nullptr; |
| 902 |
rbt_node = rbt_first(words)) { |
| 903 |
ulint i; |
| 904 |
fts_tokenizer_word_t *word; |
| 905 |
|
| 906 |
word = rbt_value(fts_tokenizer_word_t, rbt_node); |
| 907 |
|
| 908 |
/* Free the ilists of this word. */ |
| 909 |
for (i = 0; i < ib_vector_size(word->nodes); ++i) { |
| 910 |
fts_node_t *fts_node = |
| 911 |
static_cast<fts_node_t *>(ib_vector_get(word->nodes, i)); |
| 912 |
|
| 913 |
ut::free(fts_node->ilist); |
| 914 |
fts_node->ilist = nullptr; |
| 915 |
} |
| 916 |
|
| 917 |
/* NOTE: We are responsible for free'ing the node */ |
| 918 |
ut::free(rbt_remove_node(words, rbt_node)); |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
/** Clear cache. |
| 923 |
@param[in,out] cache fts cache */ |
| 924 |
void fts_cache_clear(fts_cache_t *cache) { |
| 925 |
ulint i; |
| 926 |
|
| 927 |
for (i = 0; i < ib_vector_size(cache->indexes); ++i) { |
| 928 |
ulint j; |
| 929 |
fts_index_cache_t *index_cache; |
| 930 |
|
| 931 |
index_cache = |
| 932 |
static_cast<fts_index_cache_t *>(ib_vector_get(cache->indexes, i)); |
| 933 |
|
| 934 |
fts_words_free(index_cache->words); |
| 935 |
|
| 936 |
rbt_free(index_cache->words); |
| 937 |
|
| 938 |
index_cache->words = nullptr; |
| 939 |
|
| 940 |
for (j = 0; j < FTS_NUM_AUX_INDEX; ++j) { |
| 941 |
if (index_cache->ins_graph[j] != nullptr) { |
| 942 |
que_graph_free(index_cache->ins_graph[j]); |
| 943 |
|
| 944 |
index_cache->ins_graph[j] = nullptr; |
| 945 |
} |
| 946 |
|
| 947 |
if (index_cache->sel_graph[j] != nullptr) { |
| 948 |
que_graph_free(index_cache->sel_graph[j]); |
| 949 |
|
| 950 |
index_cache->sel_graph[j] = nullptr; |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
index_cache->doc_stats = nullptr; |
| 955 |
} |
| 956 |
|
| 957 |
mem_heap_free(static_cast<mem_heap_t *>(cache->sync_heap->arg)); |
| 958 |
cache->sync_heap->arg = nullptr; |
| 959 |
|
| 960 |
fts_need_sync = false; |
| 961 |
|
| 962 |
cache->total_size = 0; |
| 963 |
|
| 964 |
mutex_enter((ib_mutex_t *)&cache->deleted_lock); |
| 965 |
cache->deleted_doc_ids = nullptr; |
| 966 |
mutex_exit((ib_mutex_t *)&cache->deleted_lock); |
| 967 |
} |
| 968 |
|
| 969 |
/** Search the index specific cache for a particular FTS index. |
| 970 |
@return the index cache else NULL */ |
| 971 |
static inline fts_index_cache_t *fts_get_index_cache( |
| 972 |
fts_cache_t *cache, /*!< in: cache to search */ |
| 973 |
const dict_index_t *index) /*!< in: index to search for */ |
| 974 |
{ |
| 975 |
ulint i; |
| 976 |
|
| 977 |
ut_ad(rw_lock_own((rw_lock_t *)&cache->lock, RW_LOCK_X) || |
| 978 |
rw_lock_own((rw_lock_t *)&cache->init_lock, RW_LOCK_X)); |
| 979 |
|
| 980 |
for (i = 0; i < ib_vector_size(cache->indexes); ++i) { |
| 981 |
fts_index_cache_t *index_cache; |
| 982 |
|
| 983 |
index_cache = |
| 984 |
static_cast<fts_index_cache_t *>(ib_vector_get(cache->indexes, i)); |
| 985 |
|
| 986 |
if (index_cache->index == index) { |
| 987 |
return (index_cache); |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
return (nullptr); |
| 992 |
} |
| 993 |
|
| 994 |
#ifdef FTS_DEBUG |
| 995 |
/** Search the index cache for a get_doc structure. |
| 996 |
@return the fts_get_doc_t item else NULL */ |
| 997 |
static fts_get_doc_t *fts_get_index_get_doc( |
| 998 |
fts_cache_t *cache, /*!< in: cache to search */ |
| 999 |
const dict_index_t *index) /*!< in: index to search for */ |
| 1000 |
{ |
| 1001 |
ulint i; |
| 1002 |
|
| 1003 |
ut_ad(rw_lock_own((rw_lock_t *)&cache->init_lock, RW_LOCK_X)); |
| 1004 |
|
| 1005 |
for (i = 0; i < ib_vector_size(cache->get_docs); ++i) { |
| 1006 |
fts_get_doc_t *get_doc; |
| 1007 |
|
| 1008 |
get_doc = static_cast<fts_get_doc_t *>(ib_vector_get(cache->get_docs, i)); |
| 1009 |
|
| 1010 |
if (get_doc->index_cache->index == index) { |
| 1011 |
return (get_doc); |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
return (NULL); |
| 1016 |
} |
| 1017 |
#endif |
| 1018 |
|
| 1019 |
/** Find an existing word, or if not found, create one and return it. |
| 1020 |
@return specified word token */ |
| 1021 |
static fts_tokenizer_word_t *fts_tokenizer_word_get( |
| 1022 |
fts_cache_t *cache, /*!< in: cache */ |
| 1023 |
fts_index_cache_t *index_cache, /*!< in: index cache */ |
| 1024 |
fts_string_t *text) /*!< in: node text */ |
| 1025 |
{ |
| 1026 |
fts_tokenizer_word_t *word; |
| 1027 |
ib_rbt_bound_t parent; |
| 1028 |
|
| 1029 |
ut_ad(rw_lock_own(&cache->lock, RW_LOCK_X)); |
| 1030 |
|
| 1031 |
/* If it is a stopword, do not index it */ |
| 1032 |
if (!fts_check_token(text, cache->stopword_info.cached_stopword, |
| 1033 |
index_cache->index->is_ngram, index_cache->charset)) { |
| 1034 |
return (nullptr); |
| 1035 |
} |
| 1036 |
|
| 1037 |
/* Check if we found a match, if not then add word to tree. */ |
| 1038 |
if (rbt_search(index_cache->words, &parent, text) != 0) { |
| 1039 |
mem_heap_t *heap; |
| 1040 |
fts_tokenizer_word_t new_word; |
| 1041 |
|
| 1042 |
heap = static_cast<mem_heap_t *>(cache->sync_heap->arg); |
| 1043 |
|
| 1044 |
new_word.nodes = ib_vector_create(cache->sync_heap, sizeof(fts_node_t), 4); |
| 1045 |
|
| 1046 |
fts_string_dup(&new_word.text, text, heap); |
| 1047 |
|
| 1048 |
parent.last = rbt_add_node(index_cache->words, &parent, &new_word); |
| 1049 |
|
| 1050 |
/* Take into account the RB tree memory use and the vector. */ |
| 1051 |
cache->total_size += sizeof(new_word) + sizeof(ib_rbt_node_t) + |
| 1052 |
text->f_len + (sizeof(fts_node_t) * 4) + |
| 1053 |
sizeof(*new_word.nodes); |
| 1054 |
|
| 1055 |
ut_ad(rbt_validate(index_cache->words)); |
| 1056 |
} |
| 1057 |
|
| 1058 |
word = rbt_value(fts_tokenizer_word_t, parent.last); |
| 1059 |
|
| 1060 |
return (word); |
| 1061 |
} |
| 1062 |
|
| 1063 |
/** Add the given doc_id/word positions to the given node's ilist. */ |
| 1064 |
void fts_cache_node_add_positions( |
| 1065 |
fts_cache_t *cache, /*!< in: cache */ |
| 1066 |
fts_node_t *node, /*!< in: word node */ |
| 1067 |
doc_id_t doc_id, /*!< in: doc id */ |
| 1068 |
ib_vector_t *positions) /*!< in: fts_token_t::positions */ |
| 1069 |
{ |
| 1070 |
ulint i; |
| 1071 |
byte *ptr; |
| 1072 |
byte *ilist; |
| 1073 |
ulint enc_len; |
| 1074 |
ulint last_pos; |
| 1075 |
byte *ptr_start; |
| 1076 |
|
| 1077 |
#ifdef UNIV_DEBUG |
| 1078 |
if (cache) { |
| 1079 |
ut_ad(rw_lock_own(&cache->lock, RW_LOCK_X)); |
| 1080 |
} |
| 1081 |
#endif /* UNIV_DEBUG */ |
| 1082 |
|
| 1083 |
ut_ad(doc_id >= node->last_doc_id); |
| 1084 |
|
| 1085 |
/* Calculate the space required to store the ilist. */ |
| 1086 |
const uint64_t doc_id_delta = doc_id - node->last_doc_id; |
| 1087 |
enc_len = fts_get_encoded_len(doc_id_delta); |
| 1088 |
|
| 1089 |
last_pos = 0; |
| 1090 |
for (i = 0; i < ib_vector_size(positions); i++) { |
| 1091 |
ulint pos = *(static_cast<ulint *>(ib_vector_get(positions, i))); |
| 1092 |
|
| 1093 |
ut_ad(last_pos == 0 || pos > last_pos); |
| 1094 |
|
| 1095 |
enc_len += fts_get_encoded_len(pos - last_pos); |
| 1096 |
last_pos = pos; |
| 1097 |
} |
| 1098 |
|
| 1099 |
/* The 0x00 byte at the end of the token positions list. */ |
| 1100 |
enc_len++; |
| 1101 |
|
| 1102 |
if ((node->ilist_size_alloc - node->ilist_size) >= enc_len) { |
| 1103 |
/* No need to allocate more space, we can fit in the new |
| 1104 |
data at the end of the old one. */ |
| 1105 |
ilist = nullptr; |
| 1106 |
ptr = node->ilist + node->ilist_size; |
| 1107 |
} else { |
| 1108 |
ulint new_size = node->ilist_size + enc_len; |
| 1109 |
|
| 1110 |
/* Over-reserve space by a fixed size for small lengths and |
| 1111 |
by 20% for lengths >= 48 bytes. */ |
| 1112 |
if (new_size < 16) { |
| 1113 |
new_size = 16; |
| 1114 |
} else if (new_size < 32) { |
| 1115 |
new_size = 32; |
| 1116 |
} else if (new_size < 48) { |
| 1117 |
new_size = 48; |
| 1118 |
} else { |
| 1119 |
new_size = (ulint)(1.2 * new_size); |
| 1120 |
} |
| 1121 |
|
| 1122 |
ilist = static_cast<byte *>( |
| 1123 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, new_size)); |
| 1124 |
ptr = ilist + node->ilist_size; |
| 1125 |
|
| 1126 |
node->ilist_size_alloc = new_size; |
| 1127 |
if (cache) { |
| 1128 |
cache->total_size += new_size; |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
ptr_start = ptr; |
| 1133 |
|
| 1134 |
/* Encode the new fragment. */ |
| 1135 |
ptr += fts_encode_int(doc_id_delta, ptr); |
| 1136 |
|
| 1137 |
last_pos = 0; |
| 1138 |
for (i = 0; i < ib_vector_size(positions); i++) { |
| 1139 |
ulint pos = *(static_cast<ulint *>(ib_vector_get(positions, i))); |
| 1140 |
|
| 1141 |
ptr += fts_encode_int(pos - last_pos, ptr); |
| 1142 |
last_pos = pos; |
| 1143 |
} |
| 1144 |
|
| 1145 |
*ptr++ = 0; |
| 1146 |
|
| 1147 |
ut_a(enc_len == (ulint)(ptr - ptr_start)); |
| 1148 |
|
| 1149 |
if (ilist) { |
| 1150 |
/* Copy old ilist to the start of the new one and switch the |
| 1151 |
new one into place in the node. */ |
| 1152 |
if (node->ilist_size > 0) { |
| 1153 |
memcpy(ilist, node->ilist, node->ilist_size); |
| 1154 |
ut::free(node->ilist); |
| 1155 |
if (cache) { |
| 1156 |
cache->total_size -= node->ilist_size; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
node->ilist = ilist; |
| 1161 |
} |
| 1162 |
|
| 1163 |
node->ilist_size += enc_len; |
| 1164 |
|
| 1165 |
if (node->first_doc_id == FTS_NULL_DOC_ID) { |
| 1166 |
node->first_doc_id = doc_id; |
| 1167 |
} |
| 1168 |
|
| 1169 |
node->last_doc_id = doc_id; |
| 1170 |
++node->doc_count; |
| 1171 |
} |
| 1172 |
|
| 1173 |
/** Add document to the cache. */ |
| 1174 |
static void fts_cache_add_doc( |
| 1175 |
fts_cache_t *cache, /*!< in: cache */ |
| 1176 |
fts_index_cache_t *index_cache, /*!< in: index cache */ |
| 1177 |
doc_id_t doc_id, /*!< in: doc id to add */ |
| 1178 |
ib_rbt_t *tokens) /*!< in: document tokens */ |
| 1179 |
{ |
| 1180 |
const ib_rbt_node_t *node; |
| 1181 |
ulint n_words; |
| 1182 |
fts_doc_stats_t *doc_stats; |
| 1183 |
|
| 1184 |
if (!tokens) { |
| 1185 |
return; |
| 1186 |
} |
| 1187 |
|
| 1188 |
ut_ad(rw_lock_own(&cache->lock, RW_LOCK_X)); |
| 1189 |
|
| 1190 |
n_words = rbt_size(tokens); |
| 1191 |
|
| 1192 |
for (node = rbt_first(tokens); node; node = rbt_first(tokens)) { |
| 1193 |
fts_tokenizer_word_t *word; |
| 1194 |
fts_node_t *fts_node = nullptr; |
| 1195 |
fts_token_t *token = rbt_value(fts_token_t, node); |
| 1196 |
|
| 1197 |
/* Find and/or add token to the cache. */ |
| 1198 |
word = fts_tokenizer_word_get(cache, index_cache, &token->text); |
| 1199 |
|
| 1200 |
if (!word) { |
| 1201 |
ut::free(rbt_remove_node(tokens, node)); |
| 1202 |
continue; |
| 1203 |
} |
| 1204 |
|
| 1205 |
if (ib_vector_size(word->nodes) > 0) { |
| 1206 |
fts_node = static_cast<fts_node_t *>(ib_vector_last(word->nodes)); |
| 1207 |
} |
| 1208 |
|
| 1209 |
if (fts_node == nullptr || fts_node->synced || |
| 1210 |
fts_node->ilist_size > FTS_ILIST_MAX_SIZE || |
| 1211 |
doc_id < fts_node->last_doc_id) { |
| 1212 |
fts_node = |
| 1213 |
static_cast<fts_node_t *>(ib_vector_push(word->nodes, nullptr)); |
| 1214 |
|
| 1215 |
memset(fts_node, 0x0, sizeof(*fts_node)); |
| 1216 |
|
| 1217 |
cache->total_size += sizeof(*fts_node); |
| 1218 |
} |
| 1219 |
|
| 1220 |
fts_cache_node_add_positions(cache, fts_node, doc_id, token->positions); |
| 1221 |
|
| 1222 |
ut::free(rbt_remove_node(tokens, node)); |
| 1223 |
} |
| 1224 |
|
| 1225 |
ut_a(rbt_empty(tokens)); |
| 1226 |
|
| 1227 |
/* Add to doc ids processed so far. */ |
| 1228 |
doc_stats = static_cast<fts_doc_stats_t *>( |
| 1229 |
ib_vector_push(index_cache->doc_stats, nullptr)); |
| 1230 |
|
| 1231 |
doc_stats->doc_id = doc_id; |
| 1232 |
doc_stats->word_count = n_words; |
| 1233 |
|
| 1234 |
/* Add the doc stats memory usage too. */ |
| 1235 |
cache->total_size += sizeof(*doc_stats); |
| 1236 |
|
| 1237 |
if (doc_id > cache->sync->max_doc_id) { |
| 1238 |
cache->sync->max_doc_id = doc_id; |
| 1239 |
} |
| 1240 |
} |
| 1241 |
|
| 1242 |
/** Drop FTS AUX table DD table objects in vector |
| 1243 |
@param[in] aux_vec aux table name vector |
| 1244 |
@param[in] file_per_table whether file per table |
| 1245 |
@return true on success, false on failure. */ |
| 1246 |
bool fts_drop_dd_tables(const aux_name_vec_t *aux_vec, bool file_per_table) { |
| 1247 |
bool ret = true; |
| 1248 |
|
| 1249 |
if (aux_vec == nullptr || aux_vec->aux_name.size() == 0) { |
| 1250 |
return (true); |
| 1251 |
} |
| 1252 |
|
| 1253 |
for (ulint i = 0; i < aux_vec->aux_name.size(); i++) { |
| 1254 |
bool retval; |
| 1255 |
|
| 1256 |
retval = dd_drop_fts_table(aux_vec->aux_name[i], file_per_table); |
| 1257 |
|
| 1258 |
if (!retval) { |
| 1259 |
ret = false; |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
return (ret); |
| 1264 |
} |
| 1265 |
|
| 1266 |
/** Free FTS AUX table names in vector |
| 1267 |
@param[in] aux_vec aux table name vector */ |
| 1268 |
void fts_free_aux_names(aux_name_vec_t *aux_vec) { |
| 1269 |
if (aux_vec == nullptr || aux_vec->aux_name.size() == 0) { |
| 1270 |
return; |
| 1271 |
} |
| 1272 |
|
| 1273 |
while (aux_vec->aux_name.size() > 0) { |
| 1274 |
char *name = aux_vec->aux_name.back(); |
| 1275 |
ut::free(name); |
| 1276 |
aux_vec->aux_name.pop_back(); |
| 1277 |
} |
| 1278 |
|
| 1279 |
ut_ad(aux_vec->aux_name.size() == 0); |
| 1280 |
} |
| 1281 |
|
| 1282 |
/** Drops a table. If the table can't be found we return a SUCCESS code. |
| 1283 |
@param[in,out] trx transaction |
| 1284 |
@param[in] table_name table to drop |
| 1285 |
@param[in,out] aux_vec fts aux table name vector |
| 1286 |
@return DB_SUCCESS or error code */ |
| 1287 |
static dberr_t fts_drop_table(trx_t *trx, const char *table_name, |
| 1288 |
aux_name_vec_t *aux_vec) { |
| 1289 |
dict_table_t *table; |
| 1290 |
dberr_t error = DB_SUCCESS; |
| 1291 |
THD *thd = current_thd; |
| 1292 |
MDL_ticket *mdl = nullptr; |
| 1293 |
|
| 1294 |
/* Check that the table exists in our data dictionary. |
| 1295 |
Similar to regular drop table case, we will open table with |
| 1296 |
DICT_ERR_IGNORE_INDEX_ROOT and DICT_ERR_IGNORE_CORRUPT option */ |
| 1297 |
table = dd_table_open_on_name( |
| 1298 |
thd, &mdl, table_name, true, |
| 1299 |
static_cast<dict_err_ignore_t>(DICT_ERR_IGNORE_INDEX_ROOT | |
| 1300 |
DICT_ERR_IGNORE_CORRUPT)); |
| 1301 |
|
| 1302 |
if (table != nullptr) { |
| 1303 |
char table_name2[MAX_FULL_NAME_LEN]; |
| 1304 |
|
| 1305 |
strcpy(table_name2, table_name); |
| 1306 |
|
| 1307 |
bool file_per_table = dict_table_is_file_per_table(table); |
| 1308 |
|
| 1309 |
dd_table_close(table, thd, &mdl, true); |
| 1310 |
|
| 1311 |
/* Pass nonatomic=false (dont allow data dict unlock), |
| 1312 |
because the transaction may hold locks on SYS_* tables from |
| 1313 |
previous calls to fts_drop_table(). */ |
| 1314 |
error = row_drop_table_for_mysql(table_name, trx, false, nullptr); |
| 1315 |
|
| 1316 |
if (error != DB_SUCCESS) { |
| 1317 |
ib::error(ER_IB_MSG_464) << "Unable to drop FTS index aux table " |
| 1318 |
<< table_name << ": " << ut_strerr(error); |
| 1319 |
return (error); |
| 1320 |
} |
| 1321 |
|
| 1322 |
if (aux_vec == nullptr) { |
| 1323 |
dict_sys_mutex_exit(); |
| 1324 |
|
| 1325 |
if (!dd_drop_fts_table(table_name2, file_per_table)) { |
| 1326 |
error = DB_FAIL; |
| 1327 |
} |
| 1328 |
|
| 1329 |
dict_sys_mutex_enter(); |
| 1330 |
} else { |
| 1331 |
aux_vec->aux_name.push_back(mem_strdup(table_name2)); |
| 1332 |
} |
| 1333 |
|
| 1334 |
} else { |
| 1335 |
error = DB_FAIL; |
| 1336 |
} |
| 1337 |
|
| 1338 |
return (error); |
| 1339 |
} |
| 1340 |
|
| 1341 |
/** Rename a single auxiliary table due to database name change. |
| 1342 |
@return DB_SUCCESS or error code */ |
| 1343 |
[[nodiscard]] static dberr_t fts_rename_one_aux_table( |
| 1344 |
const char *new_name, /*!< in: new parent tbl name */ |
| 1345 |
const char *fts_table_old_name, /*!< in: old aux tbl name */ |
| 1346 |
trx_t *trx, /*!< in: transaction */ |
| 1347 |
bool replay) /*!< Whether in replay stage */ |
| 1348 |
{ |
| 1349 |
char fts_table_new_name[MAX_TABLE_NAME_LEN]; |
| 1350 |
ulint new_db_name_len = dict_get_db_name_len(new_name); |
| 1351 |
ulint old_db_name_len = dict_get_db_name_len(fts_table_old_name); |
| 1352 |
ulint table_new_name_len = |
| 1353 |
strlen(fts_table_old_name) + new_db_name_len - old_db_name_len; |
| 1354 |
|
| 1355 |
/* Check if the new and old database names are the same, if so, |
| 1356 |
nothing to do */ |
| 1357 |
ut_ad((new_db_name_len != old_db_name_len) || |
| 1358 |
strncmp(new_name, fts_table_old_name, old_db_name_len) != 0); |
| 1359 |
|
| 1360 |
/* Get the database name from "new_name", and table name |
| 1361 |
from the fts_table_old_name */ |
| 1362 |
strncpy(fts_table_new_name, new_name, new_db_name_len); |
| 1363 |
strncpy(fts_table_new_name + new_db_name_len, strchr(fts_table_old_name, '/'), |
| 1364 |
table_new_name_len - new_db_name_len); |
| 1365 |
fts_table_new_name[table_new_name_len] = 0; |
| 1366 |
|
| 1367 |
dberr_t error; |
| 1368 |
error = row_rename_table_for_mysql(fts_table_old_name, fts_table_new_name, |
| 1369 |
nullptr, trx, replay); |
| 1370 |
|
| 1371 |
if (error == DB_SUCCESS) { |
| 1372 |
/* Update dd tablespace filename. */ |
| 1373 |
dict_table_t *table; |
| 1374 |
table = dict_table_check_if_in_cache_low(fts_table_new_name); |
| 1375 |
ut_ad(table != nullptr); |
| 1376 |
|
| 1377 |
/* Release dict_sys->mutex to avoid mutex reentrant. */ |
| 1378 |
table->acquire(); |
| 1379 |
dict_sys_mutex_exit(); |
| 1380 |
|
| 1381 |
if (!replay && !dd_rename_fts_table(table, fts_table_old_name)) { |
| 1382 |
ut_d(ut_error); |
| 1383 |
} |
| 1384 |
|
| 1385 |
dict_sys_mutex_enter(); |
| 1386 |
table->release(); |
| 1387 |
} |
| 1388 |
|
| 1389 |
return (error); |
| 1390 |
} |
| 1391 |
|
| 1392 |
/** Rename auxiliary tables for all fts index for a table. This(rename) |
| 1393 |
is due to database name change |
| 1394 |
@return DB_SUCCESS or error code */ |
| 1395 |
dberr_t fts_rename_aux_tables(dict_table_t *table, /*!< in: user Table */ |
| 1396 |
const char *new_name, /*!< in: new table name */ |
| 1397 |
trx_t *trx, /*!< in: transaction */ |
| 1398 |
bool replay) /*!< in: Whether in replay |
| 1399 |
stage */ |
| 1400 |
{ |
| 1401 |
ulint i; |
| 1402 |
fts_table_t fts_table; |
| 1403 |
|
| 1404 |
FTS_INIT_FTS_TABLE(&fts_table, nullptr, FTS_COMMON_TABLE, table); |
| 1405 |
|
| 1406 |
/* Rename common auxiliary tables */ |
| 1407 |
for (i = 0; fts_common_tables[i] != nullptr; ++i) { |
| 1408 |
char old_table_name[MAX_FULL_NAME_LEN]; |
| 1409 |
dberr_t err = DB_SUCCESS; |
| 1410 |
|
| 1411 |
fts_table.suffix = fts_common_tables[i]; |
| 1412 |
|
| 1413 |
fts_get_table_name(&fts_table, old_table_name); |
| 1414 |
|
| 1415 |
err = fts_rename_one_aux_table(new_name, old_table_name, trx, replay); |
| 1416 |
|
| 1417 |
if (err != DB_SUCCESS) { |
| 1418 |
return (err); |
| 1419 |
} |
| 1420 |
} |
| 1421 |
|
| 1422 |
fts_t *fts = table->fts; |
| 1423 |
|
| 1424 |
/* Rename index specific auxiliary tables */ |
| 1425 |
for (i = 0; fts->indexes != nullptr && i < ib_vector_size(fts->indexes); |
| 1426 |
++i) { |
| 1427 |
dict_index_t *index; |
| 1428 |
|
| 1429 |
index = static_cast<dict_index_t *>(ib_vector_getp(fts->indexes, i)); |
| 1430 |
|
| 1431 |
FTS_INIT_INDEX_TABLE(&fts_table, nullptr, FTS_INDEX_TABLE, index); |
| 1432 |
|
| 1433 |
for (ulint j = 0; j < FTS_NUM_AUX_INDEX; ++j) { |
| 1434 |
dberr_t err; |
| 1435 |
char old_table_name[MAX_FULL_NAME_LEN]; |
| 1436 |
|
| 1437 |
fts_table.suffix = fts_get_suffix(j); |
| 1438 |
|
| 1439 |
fts_get_table_name(&fts_table, old_table_name); |
| 1440 |
|
| 1441 |
err = fts_rename_one_aux_table(new_name, old_table_name, trx, replay); |
| 1442 |
|
| 1443 |
DBUG_EXECUTE_IF("fts_rename_failure", err = DB_DEADLOCK;); |
| 1444 |
|
| 1445 |
if (err != DB_SUCCESS) { |
| 1446 |
return (err); |
| 1447 |
} |
| 1448 |
} |
| 1449 |
} |
| 1450 |
|
| 1451 |
return (DB_SUCCESS); |
| 1452 |
} |
| 1453 |
|
| 1454 |
/** Drops the common ancillary tables needed for supporting an FTS index |
| 1455 |
on the given table. row_mysql_lock_data_dictionary must have been called |
| 1456 |
before this. |
| 1457 |
@param[in,out] trx transaction |
| 1458 |
@param[in,out] fts_table table with fts index |
| 1459 |
@param[in,out] aux_vec fts table name vector |
| 1460 |
@return DB_SUCCESS or error code */ |
| 1461 |
[[nodiscard]] static dberr_t fts_drop_common_tables(trx_t *trx, |
| 1462 |
fts_table_t *fts_table, |
| 1463 |
aux_name_vec_t *aux_vec) { |
| 1464 |
ulint i; |
| 1465 |
dberr_t error = DB_SUCCESS; |
| 1466 |
|
| 1467 |
for (i = 0; fts_common_tables[i] != nullptr; ++i) { |
| 1468 |
dberr_t err; |
| 1469 |
char table_name[MAX_FULL_NAME_LEN]; |
| 1470 |
|
| 1471 |
fts_table->suffix = fts_common_tables[i]; |
| 1472 |
|
| 1473 |
fts_get_table_name(fts_table, table_name); |
| 1474 |
|
| 1475 |
err = fts_drop_table(trx, table_name, aux_vec); |
| 1476 |
|
| 1477 |
/* We only return the status of the last error. */ |
| 1478 |
if (err != DB_SUCCESS && err != DB_FAIL) { |
| 1479 |
error = err; |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
return (error); |
| 1484 |
} |
| 1485 |
|
| 1486 |
/** Since we do a horizontal split on the index table, we need to drop |
| 1487 |
all the split tables. |
| 1488 |
@param[in] trx transaction |
| 1489 |
@param[in] index fts index |
| 1490 |
@param[out] aux_vec dropped table names vector |
| 1491 |
@return DB_SUCCESS or error code */ |
| 1492 |
dberr_t fts_drop_index_tables(trx_t *trx, dict_index_t *index, |
| 1493 |
aux_name_vec_t *aux_vec) { |
| 1494 |
ulint i; |
| 1495 |
fts_table_t fts_table; |
| 1496 |
dberr_t error = DB_SUCCESS; |
| 1497 |
|
| 1498 |
FTS_INIT_INDEX_TABLE(&fts_table, nullptr, FTS_INDEX_TABLE, index); |
| 1499 |
|
| 1500 |
for (i = 0; i < FTS_NUM_AUX_INDEX; ++i) { |
| 1501 |
dberr_t err; |
| 1502 |
char table_name[MAX_FULL_NAME_LEN]; |
| 1503 |
|
| 1504 |
fts_table.suffix = fts_get_suffix(i); |
| 1505 |
|
| 1506 |
fts_get_table_name(&fts_table, table_name); |
| 1507 |
|
| 1508 |
err = fts_drop_table(trx, table_name, aux_vec); |
| 1509 |
|
| 1510 |
/* We only return the status of the last error. */ |
| 1511 |
if (err != DB_SUCCESS && err != DB_FAIL) { |
| 1512 |
error = err; |
| 1513 |
} |
| 1514 |
} |
| 1515 |
|
| 1516 |
return (error); |
| 1517 |
} |
| 1518 |
|
| 1519 |
/** Write the default settings to the config table. |
| 1520 |
@param[in] fts_table fts table |
| 1521 |
@return DB_SUCCESS or error code. */ |
| 1522 |
static dberr_t fts_init_config_table(fts_table_t *fts_table) { |
| 1523 |
pars_info_t *info; |
| 1524 |
que_t *graph; |
| 1525 |
char table_name[MAX_FULL_NAME_LEN]; |
| 1526 |
dberr_t error = DB_SUCCESS; |
| 1527 |
trx_t *trx; |
| 1528 |
|
| 1529 |
ut_ad(!dict_sys_mutex_own()); |
| 1530 |
|
| 1531 |
info = pars_info_create(); |
| 1532 |
|
| 1533 |
fts_table->suffix = FTS_SUFFIX_CONFIG; |
| 1534 |
fts_get_table_name(fts_table, table_name); |
| 1535 |
pars_info_bind_id(info, true, "config_table", table_name); |
| 1536 |
trx = trx_allocate_for_background(); |
| 1537 |
|
| 1538 |
graph = fts_parse_sql(fts_table, info, fts_config_table_insert_values_sql); |
| 1539 |
|
| 1540 |
error = fts_eval_sql(trx, graph); |
| 1541 |
|
| 1542 |
que_graph_free(graph); |
| 1543 |
|
| 1544 |
if (error == DB_SUCCESS) { |
| 1545 |
fts_sql_commit(trx); |
| 1546 |
} else { |
| 1547 |
fts_sql_rollback(trx); |
| 1548 |
} |
| 1549 |
|
| 1550 |
trx_free_for_background(trx); |
| 1551 |
|
| 1552 |
return (error); |
| 1553 |
} |
| 1554 |
|
| 1555 |
/** Empty a common talbes. |
| 1556 |
@param[in,out] trx transaction |
| 1557 |
@param[in] fts_table fts table |
| 1558 |
@return DB_SUCCESS or error code. */ |
| 1559 |
static dberr_t fts_empty_table(trx_t *trx, fts_table_t *fts_table) { |
| 1560 |
pars_info_t *info; |
| 1561 |
que_t *graph; |
| 1562 |
char table_name[MAX_FULL_NAME_LEN]; |
| 1563 |
dberr_t error = DB_SUCCESS; |
| 1564 |
|
| 1565 |
info = pars_info_create(); |
| 1566 |
|
| 1567 |
fts_get_table_name(fts_table, table_name); |
| 1568 |
pars_info_bind_id(info, true, "table_name", table_name); |
| 1569 |
|
| 1570 |
ut_ad(dict_sys_mutex_own()); |
| 1571 |
|
| 1572 |
dict_sys_mutex_exit(); |
| 1573 |
|
| 1574 |
graph = fts_parse_sql(fts_table, info, "BEGIN DELETE FROM $table_name;"); |
| 1575 |
|
| 1576 |
error = fts_eval_sql(trx, graph); |
| 1577 |
|
| 1578 |
que_graph_free(graph); |
| 1579 |
|
| 1580 |
dict_sys_mutex_enter(); |
| 1581 |
|
| 1582 |
return (error); |
| 1583 |
} |
| 1584 |
|
| 1585 |
/** Empty all common talbes. |
| 1586 |
@param[in,out] trx transaction |
| 1587 |
@param[in] table dict table |
| 1588 |
@return DB_SUCCESS or error code. */ |
| 1589 |
dberr_t fts_empty_common_tables(trx_t *trx, dict_table_t *table) { |
| 1590 |
ulint i; |
| 1591 |
fts_table_t fts_table; |
| 1592 |
dberr_t error = DB_SUCCESS; |
| 1593 |
|
| 1594 |
FTS_INIT_FTS_TABLE(&fts_table, nullptr, FTS_COMMON_TABLE, table); |
| 1595 |
|
| 1596 |
for (i = 0; fts_common_tables[i] != nullptr; ++i) { |
| 1597 |
dberr_t err; |
| 1598 |
|
| 1599 |
fts_table.suffix = fts_common_tables[i]; |
| 1600 |
|
| 1601 |
/* "config" table should not be emptied, as it has the |
| 1602 |
last used DOC ID info */ |
| 1603 |
if (i == 2) { |
| 1604 |
ut_ad(ut_strcmp(fts_table.suffix, "config") == 0); |
| 1605 |
continue; |
| 1606 |
} |
| 1607 |
|
| 1608 |
err = fts_empty_table(trx, &fts_table); |
| 1609 |
|
| 1610 |
if (err != DB_SUCCESS) { |
| 1611 |
error = err; |
| 1612 |
} |
| 1613 |
} |
| 1614 |
|
| 1615 |
return (error); |
| 1616 |
} |
| 1617 |
|
| 1618 |
/** Drops FTS ancillary tables needed for supporting an FTS index |
| 1619 |
on the given table. row_mysql_lock_data_dictionary must have been called |
| 1620 |
before this. |
| 1621 |
@param[in,out] trx transaction |
| 1622 |
@param[in] fts fts instance |
| 1623 |
@param[in,out] aux_vec fts aux table name vector |
| 1624 |
@return DB_SUCCESS or error code */ |
| 1625 |
[[nodiscard]] static dberr_t fts_drop_all_index_tables( |
| 1626 |
trx_t *trx, fts_t *fts, aux_name_vec_t *aux_vec) { |
| 1627 |
dberr_t error = DB_SUCCESS; |
| 1628 |
|
| 1629 |
for (ulint i = 0; fts->indexes != nullptr && i < ib_vector_size(fts->indexes); |
| 1630 |
++i) { |
| 1631 |
dberr_t err; |
| 1632 |
dict_index_t *index; |
| 1633 |
|
| 1634 |
index = static_cast<dict_index_t *>(ib_vector_getp(fts->indexes, i)); |
| 1635 |
|
| 1636 |
err = fts_drop_index_tables(trx, index, aux_vec); |
| 1637 |
|
| 1638 |
if (err != DB_SUCCESS) { |
| 1639 |
error = err; |
| 1640 |
} |
| 1641 |
} |
| 1642 |
|
| 1643 |
return (error); |
| 1644 |
} |
| 1645 |
|
| 1646 |
/** Drops the ancillary tables needed for supporting an FTS index on a |
| 1647 |
given table. row_mysql_lock_data_dictionary must have been called before |
| 1648 |
this. |
| 1649 |
@param[in,out] trx transaction |
| 1650 |
@param[in] table table has the fts index |
| 1651 |
@param[in,out] aux_vec fts aux table name vector |
| 1652 |
@return DB_SUCCESS or error code */ |
| 1653 |
dberr_t fts_drop_tables(trx_t *trx, dict_table_t *table, |
| 1654 |
aux_name_vec_t *aux_vec) { |
| 1655 |
dberr_t error; |
| 1656 |
fts_table_t fts_table; |
| 1657 |
|
| 1658 |
FTS_INIT_FTS_TABLE(&fts_table, nullptr, FTS_COMMON_TABLE, table); |
| 1659 |
|
| 1660 |
error = fts_drop_common_tables(trx, &fts_table, aux_vec); |
| 1661 |
|
| 1662 |
if (error == DB_SUCCESS) { |
| 1663 |
error = fts_drop_all_index_tables(trx, table->fts, aux_vec); |
| 1664 |
} |
| 1665 |
|
| 1666 |
return (error); |
| 1667 |
} |
| 1668 |
|
| 1669 |
/** Lock all FTS AUX COMMON tables (for dropping table) |
| 1670 |
@param[in] thd thread locking the AUX table |
| 1671 |
@param[in,out] fts_table table with fts index |
| 1672 |
@return DB_SUCCESS or error code */ |
| 1673 |
[[nodiscard]] static dberr_t fts_lock_common_tables(THD *thd, |
| 1674 |
fts_table_t *fts_table) { |
| 1675 |
for (ulint i = 0; fts_common_tables[i] != nullptr; ++i) { |
| 1676 |
fts_table->suffix = fts_common_tables[i]; |
| 1677 |
|
| 1678 |
char table_name[MAX_FULL_NAME_LEN]; |
| 1679 |
fts_get_table_name(fts_table, table_name); |
| 1680 |
|
| 1681 |
std::string db_n; |
| 1682 |
std::string table_n; |
| 1683 |
dict_name::get_table(table_name, db_n, table_n); |
| 1684 |
|
| 1685 |
MDL_ticket *exclusiv_mdl = nullptr; |
| 1686 |
if (dd::acquire_exclusive_table_mdl(thd, db_n.c_str(), table_n.c_str(), |
| 1687 |
false, &exclusiv_mdl)) { |
| 1688 |
return (DB_ERROR); |
| 1689 |
} |
| 1690 |
} |
| 1691 |
|
| 1692 |
return (DB_SUCCESS); |
| 1693 |
} |
| 1694 |
|
| 1695 |
/** Lock all FTS INDEX AUX tables (for dropping table) |
| 1696 |
@param[in] thd thread locking the AUX table |
| 1697 |
@param[in] index fts index |
| 1698 |
@return DB_SUCCESS or error code */ |
| 1699 |
dberr_t fts_lock_index_tables(THD *thd, dict_index_t *index) { |
| 1700 |
ulint i; |
| 1701 |
fts_table_t fts_table; |
| 1702 |
|
| 1703 |
FTS_INIT_INDEX_TABLE(&fts_table, nullptr, FTS_INDEX_TABLE, index); |
| 1704 |
|
| 1705 |
for (i = 0; i < FTS_NUM_AUX_INDEX; ++i) { |
| 1706 |
fts_table.suffix = fts_get_suffix(i); |
| 1707 |
|
| 1708 |
char table_name[MAX_FULL_NAME_LEN]; |
| 1709 |
fts_get_table_name(&fts_table, table_name); |
| 1710 |
|
| 1711 |
std::string db_n; |
| 1712 |
std::string table_n; |
| 1713 |
dict_name::get_table(table_name, db_n, table_n); |
| 1714 |
|
| 1715 |
MDL_ticket *exclusiv_mdl = nullptr; |
| 1716 |
if (dd::acquire_exclusive_table_mdl(thd, db_n.c_str(), table_n.c_str(), |
| 1717 |
false, &exclusiv_mdl)) { |
| 1718 |
return (DB_ERROR); |
| 1719 |
} |
| 1720 |
} |
| 1721 |
return (DB_SUCCESS); |
| 1722 |
} |
| 1723 |
|
| 1724 |
/** Lock all FTS index AUX tables (for dropping table) |
| 1725 |
@param[in] thd thread locking the AUX table |
| 1726 |
@param[in] fts fts instance |
| 1727 |
@return DB_SUCCESS or error code */ |
| 1728 |
[[nodiscard]] static dberr_t fts_lock_all_index_tables(THD *thd, fts_t *fts) { |
| 1729 |
dberr_t error = DB_SUCCESS; |
| 1730 |
|
| 1731 |
for (ulint i = 0; fts->indexes != nullptr && i < ib_vector_size(fts->indexes); |
| 1732 |
++i) { |
| 1733 |
dberr_t err; |
| 1734 |
dict_index_t *index; |
| 1735 |
|
| 1736 |
index = static_cast<dict_index_t *>(ib_vector_getp(fts->indexes, i)); |
| 1737 |
|
| 1738 |
err = fts_lock_index_tables(thd, index); |
| 1739 |
|
| 1740 |
if (err != DB_SUCCESS) { |
| 1741 |
error = err; |
| 1742 |
} |
| 1743 |
} |
| 1744 |
|
| 1745 |
return (error); |
| 1746 |
} |
| 1747 |
|
| 1748 |
/** Lock all FTS AUX tables (for dropping table) |
| 1749 |
@param[in] thd thread locking the AUX table |
| 1750 |
@param[in] table table has the fts index |
| 1751 |
@return DB_SUCCESS or error code */ |
| 1752 |
dberr_t fts_lock_all_aux_tables(THD *thd, dict_table_t *table) { |
| 1753 |
dberr_t error; |
| 1754 |
fts_table_t fts_table; |
| 1755 |
|
| 1756 |
FTS_INIT_FTS_TABLE(&fts_table, nullptr, FTS_COMMON_TABLE, table); |
| 1757 |
|
| 1758 |
error = fts_lock_common_tables(thd, &fts_table); |
| 1759 |
|
| 1760 |
if (error == DB_SUCCESS) { |
| 1761 |
error = fts_lock_all_index_tables(thd, table->fts); |
| 1762 |
} |
| 1763 |
|
| 1764 |
return (error); |
| 1765 |
} |
| 1766 |
|
| 1767 |
/** Extract only the required flags from table->flags2 for FTS Aux |
| 1768 |
tables. |
| 1769 |
@param[in] flags2 Table flags2 |
| 1770 |
@return extracted flags2 for FTS aux tables */ |
| 1771 |
static inline uint32_t fts_get_table_flags2_for_aux_tables(uint32_t flags2) { |
| 1772 |
/* Extract the file_per_table flag, temporary file flag and encryption flag |
| 1773 |
from the main FTS table flags2 */ |
| 1774 |
return ((flags2 & DICT_TF2_USE_FILE_PER_TABLE) | |
| 1775 |
(flags2 & DICT_TF2_ENCRYPTION_FILE_PER_TABLE) | |
| 1776 |
(flags2 & DICT_TF2_TEMPORARY) | DICT_TF2_AUX); |
| 1777 |
} |
| 1778 |
|
| 1779 |
/** Create dict_table_t object for FTS Aux tables. |
| 1780 |
@param[in] aux_table_name FTS Aux table name |
| 1781 |
@param[in] table table object of FTS Index |
| 1782 |
@param[in] n_cols number of columns for FTS Aux table |
| 1783 |
@return table object for FTS Aux table */ |
| 1784 |
static dict_table_t *fts_create_in_mem_aux_table(const char *aux_table_name, |
| 1785 |
const dict_table_t *table, |
| 1786 |
ulint n_cols) { |
| 1787 |
dict_table_t *new_table = dict_mem_table_create( |
| 1788 |
aux_table_name, table->space, n_cols, 0, 0, table->flags, |
| 1789 |
fts_get_table_flags2_for_aux_tables(table->flags2)); |
| 1790 |
|
| 1791 |
if (DICT_TF_HAS_SHARED_SPACE(table->flags)) { |
| 1792 |
ut_ad(table->space == fil_space_get_id_by_name(table->tablespace())); |
| 1793 |
new_table->tablespace = mem_heap_strdup(new_table->heap, table->tablespace); |
| 1794 |
} |
| 1795 |
|
| 1796 |
if (DICT_TF_HAS_DATA_DIR(table->flags)) { |
| 1797 |
ut_ad(table->data_dir_path != nullptr); |
| 1798 |
new_table->data_dir_path = |
| 1799 |
mem_heap_strdup(new_table->heap, table->data_dir_path); |
| 1800 |
} |
| 1801 |
|
| 1802 |
return (new_table); |
| 1803 |
} |
| 1804 |
|
| 1805 |
/** Function to create on FTS common table. |
| 1806 |
@param[in,out] trx InnoDB transaction |
| 1807 |
@param[in] table Table that has FTS Index |
| 1808 |
@param[in] fts_table_name FTS AUX table name |
| 1809 |
@param[in] fts_suffix FTS AUX table suffix |
| 1810 |
@param[in] heap heap |
| 1811 |
@return table object if created, else NULL */ |
| 1812 |
static dict_table_t *fts_create_one_common_table(trx_t *trx, |
| 1813 |
const dict_table_t *table, |
| 1814 |
const char *fts_table_name, |
| 1815 |
const char *fts_suffix, |
| 1816 |
mem_heap_t *heap) { |
| 1817 |
dict_table_t *new_table = nullptr; |
| 1818 |
dberr_t error; |
| 1819 |
bool is_config = fts_suffix == FTS_SUFFIX_CONFIG; |
| 1820 |
|
| 1821 |
if (!is_config) { |
| 1822 |
new_table = fts_create_in_mem_aux_table(fts_table_name, table, |
| 1823 |
FTS_DELETED_TABLE_NUM_COLS); |
| 1824 |
|
| 1825 |
dict_mem_table_add_col(new_table, heap, "doc_id", DATA_INT, DATA_UNSIGNED, |
| 1826 |
FTS_DELETED_TABLE_COL_LEN, true); |
| 1827 |
} else { |
| 1828 |
/* Config table has different schema. */ |
| 1829 |
new_table = fts_create_in_mem_aux_table(fts_table_name, table, |
| 1830 |
FTS_CONFIG_TABLE_NUM_COLS); |
| 1831 |
|
| 1832 |
dict_mem_table_add_col( |
| 1833 |
new_table, heap, "key", DATA_VARCHAR, |
| 1834 |
dtype_form_prtype(DATA_VARCHAR, data_mysql_default_charset_coll), |
| 1835 |
FTS_CONFIG_TABLE_KEY_COL_LEN, true); |
| 1836 |
|
| 1837 |
dict_mem_table_add_col(new_table, heap, "value", DATA_VARCHAR, |
| 1838 |
dtype_form_prtype(DATA_NOT_NULL | DATA_VARCHAR, |
| 1839 |
data_mysql_default_charset_coll), |
| 1840 |
FTS_CONFIG_TABLE_VALUE_COL_LEN, true); |
| 1841 |
} |
| 1842 |
|
| 1843 |
error = row_create_table_for_mysql(new_table, nullptr, nullptr, trx, nullptr); |
| 1844 |
|
| 1845 |
if (error == DB_SUCCESS) { |
| 1846 |
dict_index_t *index = dict_mem_index_create( |
| 1847 |
fts_table_name, "FTS_COMMON_TABLE_IND", new_table->space, |
| 1848 |
DICT_UNIQUE | DICT_CLUSTERED, 1); |
| 1849 |
|
| 1850 |
if (!is_config) { |
| 1851 |
index->add_field("doc_id", 0, true); |
| 1852 |
} else { |
| 1853 |
index->add_field("key", 0, true); |
| 1854 |
} |
| 1855 |
|
| 1856 |
/* We save and restore trx->dict_operation because |
| 1857 |
row_create_index_for_mysql() changes the operation to |
| 1858 |
TRX_DICT_OP_TABLE. */ |
| 1859 |
trx_dict_op_t op = trx_get_dict_operation(trx); |
| 1860 |
|
| 1861 |
error = row_create_index_for_mysql(index, trx, nullptr, nullptr); |
| 1862 |
|
| 1863 |
trx->dict_operation = op; |
| 1864 |
} |
| 1865 |
|
| 1866 |
if (error != DB_SUCCESS) { |
| 1867 |
trx->error_state = error; |
| 1868 |
new_table = nullptr; |
| 1869 |
ib::warn(ER_IB_MSG_465) |
| 1870 |
<< "Failed to create FTS common table " << fts_table_name; |
| 1871 |
} |
| 1872 |
|
| 1873 |
return (new_table); |
| 1874 |
} |
| 1875 |
|
| 1876 |
/** Check if common tables already exist |
| 1877 |
@param[in] table table with fts index |
| 1878 |
@return true on success, false on failure */ |
| 1879 |
bool fts_check_common_tables_exist(const dict_table_t *table) { |
| 1880 |
fts_table_t fts_table; |
| 1881 |
char fts_name[MAX_FULL_NAME_LEN]; |
| 1882 |
|
| 1883 |
/* TODO: set a new flag for the situation table has hidden |
| 1884 |
FTS_DOC_ID but no FTS indexes. */ |
| 1885 |
FTS_INIT_FTS_TABLE(&fts_table, nullptr, FTS_COMMON_TABLE, table); |
| 1886 |
fts_table.suffix = FTS_SUFFIX_CONFIG; |
| 1887 |
fts_get_table_name(&fts_table, fts_name); |
| 1888 |
|
| 1889 |
dict_table_t *config_table; |
| 1890 |
THD *thd = current_thd; |
| 1891 |
MDL_ticket *mdl = reinterpret_cast<MDL_ticket *>(-1); |
| 1892 |
|
| 1893 |
/* Check that the table exists in our data dictionary */ |
| 1894 |
config_table = dd_table_open_on_name( |
| 1895 |
thd, &mdl, fts_name, false, |
| 1896 |
static_cast<dict_err_ignore_t>(DICT_ERR_IGNORE_INDEX_ROOT | |
| 1897 |
DICT_ERR_IGNORE_CORRUPT)); |
| 1898 |
|
| 1899 |
bool exist = false; |
| 1900 |
if (config_table != nullptr) { |
| 1901 |
dd_table_close(config_table, thd, &mdl, false); |
| 1902 |
exist = true; |
| 1903 |
} |
| 1904 |
|
| 1905 |
return (exist); |
| 1906 |
} |
| 1907 |
|
| 1908 |
/** Creates the common auxiliary tables needed for supporting an FTS index |
| 1909 |
on the given table. row_mysql_lock_data_dictionary must have been called |
| 1910 |
before this. |
| 1911 |
The following tables are created. |
| 1912 |
CREATE TABLE $FTS_PREFIX_DELETED |
| 1913 |
(doc_id BIGINT UNSIGNED, UNIQUE CLUSTERED INDEX on doc_id) |
| 1914 |
CREATE TABLE $FTS_PREFIX_DELETED_CACHE |
| 1915 |
(doc_id BIGINT UNSIGNED, UNIQUE CLUSTERED INDEX on doc_id) |
| 1916 |
CREATE TABLE $FTS_PREFIX_BEING_DELETED |
| 1917 |
(doc_id BIGINT UNSIGNED, UNIQUE CLUSTERED INDEX on doc_id) |
| 1918 |
CREATE TABLE $FTS_PREFIX_BEING_DELETED_CACHE |
| 1919 |
(doc_id BIGINT UNSIGNED, UNIQUE CLUSTERED INDEX on doc_id) |
| 1920 |
CREATE TABLE $FTS_PREFIX_CONFIG |
| 1921 |
(key CHAR(50), value CHAR(200), UNIQUE CLUSTERED INDEX on key) |
| 1922 |
@param[in,out] trx transaction |
| 1923 |
@param[in] table table with FTS index |
| 1924 |
@param[in] name table name normalized |
| 1925 |
@param[in] skip_doc_id_index Skip index on doc id |
| 1926 |
@return DB_SUCCESS if succeed */ |
| 1927 |
dberr_t fts_create_common_tables(trx_t *trx, const dict_table_t *table, |
| 1928 |
const char *name, bool skip_doc_id_index) { |
| 1929 |
dberr_t error; |
| 1930 |
fts_table_t fts_table; |
| 1931 |
char full_name[sizeof(fts_common_tables) / sizeof(char *)][MAX_FULL_NAME_LEN]; |
| 1932 |
dict_index_t *index = nullptr; |
| 1933 |
trx_dict_op_t op; |
| 1934 |
|
| 1935 |
ut_ad(!dict_sys_mutex_own()); |
| 1936 |
ut_ad(!fts_check_common_tables_exist(table)); |
| 1937 |
|
| 1938 |
mem_heap_t *heap = mem_heap_create(1024, UT_LOCATION_HERE); |
| 1939 |
|
| 1940 |
FTS_INIT_FTS_TABLE(&fts_table, nullptr, FTS_COMMON_TABLE, table); |
| 1941 |
|
| 1942 |
/* Create the FTS tables that are common to an FTS index. */ |
| 1943 |
for (ulint i = 0; fts_common_tables[i] != nullptr; ++i) { |
| 1944 |
fts_table.suffix = fts_common_tables[i]; |
| 1945 |
fts_get_table_name(&fts_table, full_name[i]); |
| 1946 |
dict_table_t *common_table = fts_create_one_common_table( |
| 1947 |
trx, table, full_name[i], fts_table.suffix, heap); |
| 1948 |
|
| 1949 |
if (common_table == nullptr) { |
| 1950 |
error = DB_ERROR; |
| 1951 |
goto func_exit; |
| 1952 |
} |
| 1953 |
|
| 1954 |
DBUG_EXECUTE_IF( |
| 1955 |
"ib_fts_aux_table_error", |
| 1956 |
/* Return error after creating FTS_AUX_CONFIG table. */ |
| 1957 |
if (i == 4) { |
| 1958 |
error = DB_ERROR; |
| 1959 |
goto func_exit; |
| 1960 |
}); |
| 1961 |
} |
| 1962 |
|
| 1963 |
/* Write the default settings to the config table. */ |
| 1964 |
error = fts_init_config_table(&fts_table); |
| 1965 |
|
| 1966 |
if (error != DB_SUCCESS || skip_doc_id_index) { |
| 1967 |
goto func_exit; |
| 1968 |
} |
| 1969 |
|
| 1970 |
index = dict_mem_index_create(name, FTS_DOC_ID_INDEX_NAME, table->space, |
| 1971 |
DICT_UNIQUE, 1); |
| 1972 |
index->add_field(FTS_DOC_ID_COL_NAME, 0, true); |
| 1973 |
|
| 1974 |
op = trx_get_dict_operation(trx); |
| 1975 |
|
| 1976 |
error = row_create_index_for_mysql(index, trx, nullptr, nullptr); |
| 1977 |
|
| 1978 |
trx->dict_operation = op; |
| 1979 |
|
| 1980 |
func_exit: |
| 1981 |
mem_heap_free(heap); |
| 1982 |
|
| 1983 |
return (error); |
| 1984 |
} |
| 1985 |
|
| 1986 |
/** Creates one FTS auxiliary index table for an FTS index. |
| 1987 |
@param[in,out] trx transaction |
| 1988 |
@param[in] index the index instance |
| 1989 |
@param[in] fts_table fts_table structure |
| 1990 |
@param[in] heap memory heap |
| 1991 |
@return DB_SUCCESS or error code */ |
| 1992 |
static dict_table_t *fts_create_one_index_table(trx_t *trx, |
| 1993 |
const dict_index_t *index, |
| 1994 |
fts_table_t *fts_table, |
| 1995 |
mem_heap_t *heap) { |
| 1996 |
dict_field_t *field; |
| 1997 |
dict_table_t *new_table = nullptr; |
| 1998 |
char table_name[MAX_FULL_NAME_LEN]; |
| 1999 |
dberr_t error; |
| 2000 |
CHARSET_INFO *charset; |
| 2001 |
|
| 2002 |
ut_ad(index->type & DICT_FTS); |
| 2003 |
|
| 2004 |
fts_get_table_name(fts_table, table_name); |
| 2005 |
|
| 2006 |
new_table = fts_create_in_mem_aux_table(table_name, fts_table->table, |
| 2007 |
FTS_AUX_INDEX_TABLE_NUM_COLS); |
| 2008 |
|
| 2009 |
field = index->get_field(0); |
| 2010 |
charset = fts_get_charset(field->col->prtype); |
| 2011 |
|
| 2012 |
dict_mem_table_add_col( |
| 2013 |
new_table, heap, "word", |
| 2014 |
charset == &my_charset_latin1 ? DATA_VARCHAR : DATA_VARMYSQL, |
| 2015 |
field->col->prtype, FTS_INDEX_WORD_LEN, true); |
| 2016 |
|
| 2017 |
dict_mem_table_add_col(new_table, heap, "first_doc_id", DATA_INT, |
| 2018 |
DATA_NOT_NULL | DATA_UNSIGNED, |
| 2019 |
FTS_INDEX_FIRST_DOC_ID_LEN, true); |
| 2020 |
|
| 2021 |
dict_mem_table_add_col(new_table, heap, "last_doc_id", DATA_INT, |
| 2022 |
DATA_NOT_NULL | DATA_UNSIGNED, |
| 2023 |
FTS_INDEX_LAST_DOC_ID_LEN, true); |
| 2024 |
|
| 2025 |
dict_mem_table_add_col(new_table, heap, "doc_count", DATA_INT, |
| 2026 |
DATA_NOT_NULL | DATA_UNSIGNED, FTS_INDEX_DOC_COUNT_LEN, |
| 2027 |
true); |
| 2028 |
|
| 2029 |
/* The precise type calculation is as follows: |
| 2030 |
least significant byte: MySQL type code (not applicable for sys cols) |
| 2031 |
second least : DATA_NOT_NULL | DATA_BINARY_TYPE |
| 2032 |
third least : the MySQL charset-collation code (DATA_MTYPE_MAX) */ |
| 2033 |
|
| 2034 |
dict_mem_table_add_col(new_table, heap, "ilist", DATA_BLOB, |
| 2035 |
(DATA_MTYPE_MAX << 16) | DATA_UNSIGNED | DATA_NOT_NULL, |
| 2036 |
FTS_INDEX_ILIST_LEN, true); |
| 2037 |
|
| 2038 |
error = row_create_table_for_mysql(new_table, nullptr, nullptr, trx, nullptr); |
| 2039 |
|
| 2040 |
if (error == DB_SUCCESS) { |
| 2041 |
dict_index_t *index = dict_mem_index_create( |
| 2042 |
table_name, "FTS_INDEX_TABLE_IND", new_table->space, |
| 2043 |
DICT_UNIQUE | DICT_CLUSTERED, 2); |
| 2044 |
index->add_field("word", 0, true); |
| 2045 |
index->add_field("first_doc_id", 0, true); |
| 2046 |
|
| 2047 |
trx_dict_op_t op = trx_get_dict_operation(trx); |
| 2048 |
|
| 2049 |
error = row_create_index_for_mysql(index, trx, nullptr, nullptr); |
| 2050 |
|
| 2051 |
trx->dict_operation = op; |
| 2052 |
} |
| 2053 |
|
| 2054 |
if (error != DB_SUCCESS) { |
| 2055 |
trx->error_state = error; |
| 2056 |
new_table = nullptr; |
| 2057 |
ib::warn(ER_IB_MSG_466) |
| 2058 |
<< "Failed to create FTS index table " << table_name; |
| 2059 |
} |
| 2060 |
|
| 2061 |
return (new_table); |
| 2062 |
} |
| 2063 |
|
| 2064 |
/** Freeze all auiliary tables to be not evictable if exist, with dict_mutex |
| 2065 |
held |
| 2066 |
@param[in] table InnoDB table object */ |
| 2067 |
void fts_freeze_aux_tables(const dict_table_t *table) { |
| 2068 |
fts_table_t fts_table; |
| 2069 |
char table_name[MAX_FULL_NAME_LEN]; |
| 2070 |
|
| 2071 |
FTS_INIT_FTS_TABLE(&fts_table, nullptr, FTS_COMMON_TABLE, table); |
| 2072 |
|
| 2073 |
for (ulint i = 0; fts_common_tables[i] != nullptr; ++i) { |
| 2074 |
fts_table.suffix = fts_common_tables[i]; |
| 2075 |
fts_get_table_name(&fts_table, table_name); |
| 2076 |
|
| 2077 |
dict_table_t *common; |
| 2078 |
common = dd_table_open_on_name_in_mem(table_name, true); |
| 2079 |
if (common != nullptr && common->can_be_evicted) { |
| 2080 |
dict_table_prevent_eviction(common); |
| 2081 |
} |
| 2082 |
|
| 2083 |
if (common != nullptr) { |
| 2084 |
dd_table_close(common, nullptr, nullptr, true); |
| 2085 |
} |
| 2086 |
} |
| 2087 |
|
| 2088 |
fts_t *fts = table->fts; |
| 2089 |
if (fts == nullptr) { |
| 2090 |
return; |
| 2091 |
} |
| 2092 |
|
| 2093 |
for (ulint i = 0; fts->indexes != nullptr && i < ib_vector_size(fts->indexes); |
| 2094 |
++i) { |
| 2095 |
dict_index_t *index; |
| 2096 |
index = static_cast<dict_index_t *>(ib_vector_getp(fts->indexes, i)); |
| 2097 |
|
| 2098 |
FTS_INIT_INDEX_TABLE(&fts_table, nullptr, FTS_INDEX_TABLE, index); |
| 2099 |
|
| 2100 |
for (ulint j = 0; j < FTS_NUM_AUX_INDEX; ++j) { |
| 2101 |
fts_table.suffix = fts_get_suffix(j); |
| 2102 |
fts_get_table_name(&fts_table, table_name); |
| 2103 |
|
| 2104 |
dict_table_t *index_table; |
| 2105 |
index_table = dd_table_open_on_name_in_mem(table_name, true); |
| 2106 |
if (index_table != nullptr && index_table->can_be_evicted) { |
| 2107 |
dict_table_prevent_eviction(index_table); |
| 2108 |
} |
| 2109 |
|
| 2110 |
if (index_table != nullptr) { |
| 2111 |
dd_table_close(index_table, nullptr, nullptr, true); |
| 2112 |
} |
| 2113 |
} |
| 2114 |
} |
| 2115 |
} |
| 2116 |
|
| 2117 |
/** Allow all the auxiliary tables of specified base table to be evictable |
| 2118 |
if they exist, if not exist just ignore |
| 2119 |
@param[in] table InnoDB table object |
| 2120 |
@param[in] dict_locked True if we have dict_sys mutex */ |
| 2121 |
void fts_detach_aux_tables(const dict_table_t *table, bool dict_locked) { |
| 2122 |
fts_table_t fts_table; |
| 2123 |
char table_name[MAX_FULL_NAME_LEN]; |
| 2124 |
|
| 2125 |
if (!dict_locked) { |
| 2126 |
dict_sys_mutex_enter(); |
| 2127 |
} |
| 2128 |
|
| 2129 |
FTS_INIT_FTS_TABLE(&fts_table, nullptr, FTS_COMMON_TABLE, table); |
| 2130 |
|
| 2131 |
for (ulint i = 0; fts_common_tables[i] != nullptr; ++i) { |
| 2132 |
fts_table.suffix = fts_common_tables[i]; |
| 2133 |
fts_get_table_name(&fts_table, table_name); |
| 2134 |
|
| 2135 |
dict_table_t *common; |
| 2136 |
common = dd_table_open_on_name_in_mem(table_name, true); |
| 2137 |
if (common != nullptr && !common->can_be_evicted) { |
| 2138 |
dict_table_allow_eviction(common); |
| 2139 |
} |
| 2140 |
|
| 2141 |
if (common != nullptr) { |
| 2142 |
dd_table_close(common, nullptr, nullptr, true); |
| 2143 |
} |
| 2144 |
} |
| 2145 |
|
| 2146 |
fts_t *fts = table->fts; |
| 2147 |
if (fts == nullptr) { |
| 2148 |
if (!dict_locked) { |
| 2149 |
dict_sys_mutex_exit(); |
| 2150 |
} |
| 2151 |
|
| 2152 |
return; |
| 2153 |
} |
| 2154 |
|
| 2155 |
for (ulint i = 0; fts->indexes != nullptr && i < ib_vector_size(fts->indexes); |
| 2156 |
++i) { |
| 2157 |
dict_index_t *index; |
| 2158 |
index = static_cast<dict_index_t *>(ib_vector_getp(fts->indexes, i)); |
| 2159 |
|
| 2160 |
FTS_INIT_INDEX_TABLE(&fts_table, nullptr, FTS_INDEX_TABLE, index); |
| 2161 |
|
| 2162 |
for (ulint j = 0; j < FTS_NUM_AUX_INDEX; ++j) { |
| 2163 |
fts_table.suffix = fts_get_suffix(j); |
| 2164 |
fts_get_table_name(&fts_table, table_name); |
| 2165 |
|
| 2166 |
dict_table_t *index_table; |
| 2167 |
index_table = dd_table_open_on_name_in_mem(table_name, true); |
| 2168 |
if (index_table != nullptr && !index_table->can_be_evicted) { |
| 2169 |
dict_table_allow_eviction(index_table); |
| 2170 |
} |
| 2171 |
|
| 2172 |
if (index_table != nullptr) { |
| 2173 |
dd_table_close(index_table, nullptr, nullptr, true); |
| 2174 |
} |
| 2175 |
} |
| 2176 |
} |
| 2177 |
|
| 2178 |
if (!dict_locked) { |
| 2179 |
dict_sys_mutex_exit(); |
| 2180 |
} |
| 2181 |
} |
| 2182 |
|
| 2183 |
/** Update DD system table for auxiliary common tables for an FTS index. |
| 2184 |
@param[in] table dict table instance |
| 2185 |
@return true on success, false on failure */ |
| 2186 |
bool fts_create_common_dd_tables(const dict_table_t *table) { |
| 2187 |
fts_table_t fts_table; |
| 2188 |
bool ret = true; |
| 2189 |
|
| 2190 |
FTS_INIT_FTS_TABLE(&fts_table, nullptr, FTS_COMMON_TABLE, table); |
| 2191 |
|
| 2192 |
/* Create the FTS tables that are common to an FTS index. */ |
| 2193 |
for (ulint i = 0; fts_common_tables[i] != nullptr && ret; ++i) { |
| 2194 |
char table_name[MAX_FULL_NAME_LEN]; |
| 2195 |
|
| 2196 |
fts_table.suffix = fts_common_tables[i]; |
| 2197 |
fts_get_table_name(&fts_table, table_name); |
| 2198 |
|
| 2199 |
dict_table_t *common_table; |
| 2200 |
common_table = dd_table_open_on_name_in_mem(table_name, false); |
| 2201 |
ut_ad(common_table != nullptr); |
| 2202 |
|
| 2203 |
bool is_config = fts_table.suffix == FTS_SUFFIX_CONFIG; |
| 2204 |
ret = dd_create_fts_common_table(table, common_table, is_config); |
| 2205 |
|
| 2206 |
dd_table_close(common_table, nullptr, nullptr, false); |
| 2207 |
} |
| 2208 |
|
| 2209 |
return (ret); |
| 2210 |
} |
| 2211 |
|
| 2212 |
/** Update DD system table for auxiliary index tables for an FTS index. |
| 2213 |
@param[in] index the index instance |
| 2214 |
@return DB_SUCCESS or error code */ |
| 2215 |
static dberr_t fts_create_one_index_dd_tables(const dict_index_t *index) { |
| 2216 |
ulint i; |
| 2217 |
fts_table_t fts_table; |
| 2218 |
dberr_t error = DB_SUCCESS; |
| 2219 |
char *parent_name = index->table->name.m_name; |
| 2220 |
|
| 2221 |
fts_table.type = FTS_INDEX_TABLE; |
| 2222 |
fts_table.index_id = index->id; |
| 2223 |
fts_table.table_id = index->table->id; |
| 2224 |
fts_table.parent = parent_name; |
| 2225 |
fts_table.table = index->table; |
| 2226 |
|
| 2227 |
for (i = 0; i < FTS_NUM_AUX_INDEX && error == DB_SUCCESS; ++i) { |
| 2228 |
dict_table_t *new_table; |
| 2229 |
char table_name[MAX_FULL_NAME_LEN]; |
| 2230 |
CHARSET_INFO *charset; |
| 2231 |
dict_field_t *field; |
| 2232 |
|
| 2233 |
ut_ad(index->type & DICT_FTS); |
| 2234 |
|
| 2235 |
field = index->get_field(0); |
| 2236 |
charset = fts_get_charset(field->col->prtype); |
| 2237 |
|
| 2238 |
fts_table.suffix = fts_get_suffix(i); |
| 2239 |
fts_get_table_name(&fts_table, table_name); |
| 2240 |
|
| 2241 |
new_table = dd_table_open_on_name_in_mem(table_name, false); |
| 2242 |
ut_ad(new_table != nullptr); |
| 2243 |
|
| 2244 |
if (!dd_create_fts_index_table(fts_table.table, new_table, charset)) { |
| 2245 |
ib::warn(ER_IB_MSG_467) |
| 2246 |
<< "Failed to create FTS index dd table " << table_name; |
| 2247 |
error = DB_FAIL; |
| 2248 |
} |
| 2249 |
|
| 2250 |
dd_table_close(new_table, nullptr, nullptr, false); |
| 2251 |
} |
| 2252 |
|
| 2253 |
return (error); |
| 2254 |
} |
| 2255 |
|
| 2256 |
/** Check if a table has FTS index needs to have its auxiliary index |
| 2257 |
tables' metadata updated in DD |
| 2258 |
@param[in,out] table table to check |
| 2259 |
@return DB_SUCCESS or error code */ |
| 2260 |
dberr_t fts_create_index_dd_tables(dict_table_t *table) { |
| 2261 |
dberr_t error = DB_SUCCESS; |
| 2262 |
|
| 2263 |
for (dict_index_t *index = table->first_index(); |
| 2264 |
index != nullptr && error == DB_SUCCESS; index = index->next()) { |
| 2265 |
if ((index->type & DICT_FTS) && index->fill_dd) { |
| 2266 |
error = fts_create_one_index_dd_tables(index); |
| 2267 |
index->fill_dd = false; |
| 2268 |
} |
| 2269 |
|
| 2270 |
ut_ad(!index->fill_dd); |
| 2271 |
} |
| 2272 |
|
| 2273 |
return (error); |
| 2274 |
} |
| 2275 |
|
| 2276 |
/** Create auxiliary index tables for an FTS index. |
| 2277 |
@param[in,out] trx transaction |
| 2278 |
@param[in] index the index instance |
| 2279 |
@param[in] table_name table name |
| 2280 |
@param[in] table_id the table id |
| 2281 |
@return DB_SUCCESS or error code */ |
| 2282 |
dberr_t fts_create_index_tables_low(trx_t *trx, dict_index_t *index, |
| 2283 |
const char *table_name, |
| 2284 |
table_id_t table_id) { |
| 2285 |
ulint i; |
| 2286 |
fts_table_t fts_table; |
| 2287 |
dberr_t error = DB_SUCCESS; |
| 2288 |
mem_heap_t *heap = mem_heap_create(1024, UT_LOCATION_HERE); |
| 2289 |
|
| 2290 |
fts_table.type = FTS_INDEX_TABLE; |
| 2291 |
fts_table.index_id = index->id; |
| 2292 |
fts_table.table_id = table_id; |
| 2293 |
fts_table.parent = table_name; |
| 2294 |
fts_table.table = index->table; |
| 2295 |
|
| 2296 |
for (i = 0; i < FTS_NUM_AUX_INDEX && error == DB_SUCCESS; ++i) { |
| 2297 |
dict_table_t *new_table; |
| 2298 |
|
| 2299 |
/* Create the FTS auxiliary tables that are specific |
| 2300 |
to an FTS index. We need to preserve the table_id %s |
| 2301 |
which fts_parse_sql() will fill in for us. */ |
| 2302 |
fts_table.suffix = fts_get_suffix(i); |
| 2303 |
|
| 2304 |
new_table = fts_create_one_index_table(trx, index, &fts_table, heap); |
| 2305 |
|
| 2306 |
if (new_table == nullptr) { |
| 2307 |
error = DB_FAIL; |
| 2308 |
break; |
| 2309 |
} |
| 2310 |
|
| 2311 |
DBUG_EXECUTE_IF( |
| 2312 |
"ib_fts_index_table_error", |
| 2313 |
/* Return error after creating FTS_INDEX_5 |
| 2314 |
aux table. */ |
| 2315 |
if (i == 4) { |
| 2316 |
error = DB_FAIL; |
| 2317 |
break; |
| 2318 |
}); |
| 2319 |
} |
| 2320 |
|
| 2321 |
if (error == DB_SUCCESS) { |
| 2322 |
index->fill_dd = true; |
| 2323 |
} |
| 2324 |
|
| 2325 |
mem_heap_free(heap); |
| 2326 |
|
| 2327 |
return (error); |
| 2328 |
} |
| 2329 |
|
| 2330 |
/** Creates the column specific ancillary tables needed for supporting an |
| 2331 |
FTS index on the given table. row_mysql_lock_data_dictionary must have |
| 2332 |
been called before this. |
| 2333 |
|
| 2334 |
All FTS AUX Index tables have the following schema. |
| 2335 |
CREATE TABLE $FTS_PREFIX_INDEX_[1-6]( |
| 2336 |
word VARCHAR(FTS_MAX_WORD_LEN), |
| 2337 |
first_doc_id INT NOT NULL, |
| 2338 |
last_doc_id UNSIGNED NOT NULL, |
| 2339 |
doc_count UNSIGNED INT NOT NULL, |
| 2340 |
ilist VARBINARY NOT NULL, |
| 2341 |
UNIQUE CLUSTERED INDEX ON (word, first_doc_id)) |
| 2342 |
@param[in,out] trx transaction |
| 2343 |
@param[in] index index instance |
| 2344 |
@return DB_SUCCESS or error code */ |
| 2345 |
dberr_t fts_create_index_tables(trx_t *trx, dict_index_t *index) { |
| 2346 |
dberr_t err; |
| 2347 |
dict_table_t *table; |
| 2348 |
|
| 2349 |
ut_ad(!dict_sys_mutex_own()); |
| 2350 |
|
| 2351 |
table = dd_table_open_on_name_in_mem(index->table_name, false); |
| 2352 |
ut_a(table != nullptr); |
| 2353 |
ut_d(dict_sys_mutex_enter()); |
| 2354 |
ut_ad(table->get_ref_count() > 1); |
| 2355 |
ut_d(dict_sys_mutex_exit()); |
| 2356 |
|
| 2357 |
err = fts_create_index_tables_low(trx, index, table->name.m_name, table->id); |
| 2358 |
|
| 2359 |
dd_table_close(table, nullptr, nullptr, false); |
| 2360 |
|
| 2361 |
return (err); |
| 2362 |
} |
| 2363 |
#if 0 |
| 2364 |
/******************************************************************//** |
| 2365 |
Return string representation of state. */ |
| 2366 |
static |
| 2367 |
const char* |
| 2368 |
fts_get_state_str( |
| 2369 |
/* out: string representation of state */ |
| 2370 |
fts_row_state zstate) /*!< in: state */ |
| 2371 |
{ |
| 2372 |
switch (state) { |
| 2373 |
case FTS_INSERT: |
| 2374 |
return("INSERT"); |
| 2375 |
|
| 2376 |
case FTS_MODIFY: |
| 2377 |
return("MODIFY"); |
| 2378 |
|
| 2379 |
case FTS_DELETE: |
| 2380 |
return("DELETE"); |
| 2381 |
|
| 2382 |
case FTS_NOTHING: |
| 2383 |
return("NOTHING"); |
| 2384 |
|
| 2385 |
case FTS_INVALID: |
| 2386 |
return("INVALID"); |
| 2387 |
|
| 2388 |
default: |
| 2389 |
return("UNKNOWN"); |
| 2390 |
} |
| 2391 |
} |
| 2392 |
#endif |
| 2393 |
|
| 2394 |
/** Calculate the new state of a row given the existing state and a new event. |
| 2395 |
@return new state of row */ |
| 2396 |
static fts_row_state fts_trx_row_get_new_state( |
| 2397 |
fts_row_state old_state, /*!< in: existing state of row */ |
| 2398 |
fts_row_state event) /*!< in: new event */ |
| 2399 |
{ |
| 2400 |
/* The rules for transforming states: |
| 2401 |
|
| 2402 |
I = inserted |
| 2403 |
M = modified |
| 2404 |
D = deleted |
| 2405 |
N = nothing |
| 2406 |
|
| 2407 |
M+D -> D: |
| 2408 |
|
| 2409 |
If the row existed before the transaction started and it is modified |
| 2410 |
during the transaction, followed by a deletion of the row, only the |
| 2411 |
deletion will be signaled. |
| 2412 |
|
| 2413 |
M+ -> M: |
| 2414 |
|
| 2415 |
If the row existed before the transaction started and it is modified |
| 2416 |
more than once during the transaction, only the last modification |
| 2417 |
will be signaled. |
| 2418 |
|
| 2419 |
IM*D -> N: |
| 2420 |
|
| 2421 |
If a new row is added during the transaction (and possibly modified |
| 2422 |
after its initial insertion) but it is deleted before the end of the |
| 2423 |
transaction, nothing will be signaled. |
| 2424 |
|
| 2425 |
IM* -> I: |
| 2426 |
|
| 2427 |
If a new row is added during the transaction and modified after its |
| 2428 |
initial insertion, only the addition will be signaled. |
| 2429 |
|
| 2430 |
M*DI -> M: |
| 2431 |
|
| 2432 |
If the row existed before the transaction started and it is deleted, |
| 2433 |
then re-inserted, only a modification will be signaled. Note that |
| 2434 |
this case is only possible if the table is using the row's primary |
| 2435 |
key for FTS row ids, since those can be re-inserted by the user, |
| 2436 |
which is not true for InnoDB generated row ids. |
| 2437 |
|
| 2438 |
It is easily seen that the above rules decompose such that we do not |
| 2439 |
need to store the row's entire history of events. Instead, we can |
| 2440 |
store just one state for the row and update that when new events |
| 2441 |
arrive. Then we can implement the above rules as a two-dimensional |
| 2442 |
look-up table, and get checking of invalid combinations "for free" |
| 2443 |
in the process. */ |
| 2444 |
|
| 2445 |
/* The lookup table for transforming states. old_state is the |
| 2446 |
Y-axis, event is the X-axis. */ |
| 2447 |
static const fts_row_state table[4][4] = { |
| 2448 |
/* I M D N */ |
| 2449 |
/* I */ {FTS_INVALID, FTS_INSERT, FTS_NOTHING, FTS_INVALID}, |
| 2450 |
/* M */ {FTS_INVALID, FTS_MODIFY, FTS_DELETE, FTS_INVALID}, |
| 2451 |
/* D */ {FTS_MODIFY, FTS_INVALID, FTS_INVALID, FTS_INVALID}, |
| 2452 |
/* N */ {FTS_INVALID, FTS_INVALID, FTS_INVALID, FTS_INVALID}}; |
| 2453 |
|
| 2454 |
fts_row_state result; |
| 2455 |
|
| 2456 |
ut_a(old_state < FTS_INVALID); |
| 2457 |
ut_a(event < FTS_INVALID); |
| 2458 |
|
| 2459 |
result = table[(int)old_state][(int)event]; |
| 2460 |
ut_a(result != FTS_INVALID); |
| 2461 |
|
| 2462 |
return (result); |
| 2463 |
} |
| 2464 |
|
| 2465 |
/** Create a savepoint instance. |
| 2466 |
@return savepoint instance */ |
| 2467 |
static fts_savepoint_t *fts_savepoint_create( |
| 2468 |
ib_vector_t *savepoints, /*!< out: InnoDB transaction */ |
| 2469 |
const char *name, /*!< in: savepoint name */ |
| 2470 |
mem_heap_t *heap) /*!< in: heap */ |
| 2471 |
{ |
| 2472 |
fts_savepoint_t *savepoint; |
| 2473 |
|
| 2474 |
savepoint = |
| 2475 |
static_cast<fts_savepoint_t *>(ib_vector_push(savepoints, nullptr)); |
| 2476 |
|
| 2477 |
memset(savepoint, 0x0, sizeof(*savepoint)); |
| 2478 |
|
| 2479 |
if (name) { |
| 2480 |
savepoint->name = mem_heap_strdup(heap, name); |
| 2481 |
} |
| 2482 |
|
| 2483 |
savepoint->tables = rbt_create(sizeof(fts_trx_table_t *), fts_trx_table_cmp); |
| 2484 |
|
| 2485 |
return (savepoint); |
| 2486 |
} |
| 2487 |
|
| 2488 |
/** Create an FTS trx. |
| 2489 |
@param[in,out] trx InnoDB Transaction |
| 2490 |
@return FTS transaction. */ |
| 2491 |
fts_trx_t *fts_trx_create(trx_t *trx) { |
| 2492 |
fts_trx_t *ftt; |
| 2493 |
ib_alloc_t *heap_alloc; |
| 2494 |
mem_heap_t *heap = mem_heap_create(1024, UT_LOCATION_HERE); |
| 2495 |
|
| 2496 |
ut_a(trx->fts_trx == nullptr); |
| 2497 |
|
| 2498 |
ftt = static_cast<fts_trx_t *>(mem_heap_alloc(heap, sizeof(fts_trx_t))); |
| 2499 |
ftt->trx = trx; |
| 2500 |
ftt->heap = heap; |
| 2501 |
|
| 2502 |
heap_alloc = ib_heap_allocator_create(heap); |
| 2503 |
|
| 2504 |
ftt->savepoints = static_cast<ib_vector_t *>( |
| 2505 |
ib_vector_create(heap_alloc, sizeof(fts_savepoint_t), 4)); |
| 2506 |
|
| 2507 |
ftt->last_stmt = static_cast<ib_vector_t *>( |
| 2508 |
ib_vector_create(heap_alloc, sizeof(fts_savepoint_t), 4)); |
| 2509 |
|
| 2510 |
/* Default instance has no name and no heap. */ |
| 2511 |
fts_savepoint_create(ftt->savepoints, nullptr, nullptr); |
| 2512 |
fts_savepoint_create(ftt->last_stmt, nullptr, nullptr); |
| 2513 |
|
| 2514 |
/* Copy savepoints that already set before. */ |
| 2515 |
for (auto savep : trx->trx_savepoints) { |
| 2516 |
fts_savepoint_take(ftt, savep->name); |
| 2517 |
} |
| 2518 |
|
| 2519 |
return (ftt); |
| 2520 |
} |
| 2521 |
|
| 2522 |
/** Create an FTS trx table. |
| 2523 |
@return FTS trx table */ |
| 2524 |
static fts_trx_table_t *fts_trx_table_create( |
| 2525 |
fts_trx_t *fts_trx, /*!< in: FTS trx */ |
| 2526 |
dict_table_t *table) /*!< in: table */ |
| 2527 |
{ |
| 2528 |
fts_trx_table_t *ftt; |
| 2529 |
|
| 2530 |
ftt = static_cast<fts_trx_table_t *>( |
| 2531 |
mem_heap_alloc(fts_trx->heap, sizeof(*ftt))); |
| 2532 |
|
| 2533 |
if (ftt != nullptr) memset(ftt, 0x0, sizeof(*ftt)); |
| 2534 |
|
| 2535 |
ftt->table = table; |
| 2536 |
ftt->fts_trx = fts_trx; |
| 2537 |
|
| 2538 |
ftt->rows = |
| 2539 |
rbt_create(sizeof(fts_trx_row_t), fts_doc_id_field_cmp<fts_trx_row_t>); |
| 2540 |
|
| 2541 |
return (ftt); |
| 2542 |
} |
| 2543 |
|
| 2544 |
/** Clone an FTS trx table. |
| 2545 |
@return FTS trx table */ |
| 2546 |
static fts_trx_table_t *fts_trx_table_clone( |
| 2547 |
const fts_trx_table_t *ftt_src) /*!< in: FTS trx */ |
| 2548 |
{ |
| 2549 |
fts_trx_table_t *ftt; |
| 2550 |
|
| 2551 |
ftt = static_cast<fts_trx_table_t *>( |
| 2552 |
mem_heap_alloc(ftt_src->fts_trx->heap, sizeof(*ftt))); |
| 2553 |
|
| 2554 |
memset(ftt, 0x0, sizeof(*ftt)); |
| 2555 |
|
| 2556 |
ftt->table = ftt_src->table; |
| 2557 |
ftt->fts_trx = ftt_src->fts_trx; |
| 2558 |
|
| 2559 |
ftt->rows = |
| 2560 |
rbt_create(sizeof(fts_trx_row_t), fts_doc_id_field_cmp<fts_trx_row_t>); |
| 2561 |
|
| 2562 |
/* Copy the rb tree values to the new savepoint. */ |
| 2563 |
rbt_merge_uniq(ftt->rows, ftt_src->rows); |
| 2564 |
|
| 2565 |
/* These are only added on commit. At this stage we only have |
| 2566 |
the updated row state. */ |
| 2567 |
ut_a(ftt_src->added_doc_ids == nullptr); |
| 2568 |
|
| 2569 |
return (ftt); |
| 2570 |
} |
| 2571 |
|
| 2572 |
/** Initialize the FTS trx instance. |
| 2573 |
@return FTS trx instance */ |
| 2574 |
static fts_trx_table_t *fts_trx_init( |
| 2575 |
trx_t *trx, /*!< in: transaction */ |
| 2576 |
dict_table_t *table, /*!< in: FTS table instance */ |
| 2577 |
ib_vector_t *savepoints) /*!< in: Savepoints */ |
| 2578 |
{ |
| 2579 |
fts_trx_table_t *ftt; |
| 2580 |
ib_rbt_bound_t parent; |
| 2581 |
ib_rbt_t *tables; |
| 2582 |
fts_savepoint_t *savepoint; |
| 2583 |
|
| 2584 |
savepoint = static_cast<fts_savepoint_t *>(ib_vector_last(savepoints)); |
| 2585 |
|
| 2586 |
tables = savepoint->tables; |
| 2587 |
rbt_search_cmp(tables, &parent, &table->id, fts_trx_table_id_cmp, nullptr); |
| 2588 |
|
| 2589 |
if (parent.result == 0) { |
| 2590 |
fts_trx_table_t **fttp; |
| 2591 |
|
| 2592 |
fttp = rbt_value(fts_trx_table_t *, parent.last); |
| 2593 |
ftt = *fttp; |
| 2594 |
} else { |
| 2595 |
ftt = fts_trx_table_create(trx->fts_trx, table); |
| 2596 |
rbt_add_node(tables, &parent, &ftt); |
| 2597 |
} |
| 2598 |
|
| 2599 |
ut_a(ftt->table == table); |
| 2600 |
|
| 2601 |
return (ftt); |
| 2602 |
} |
| 2603 |
|
| 2604 |
/** Notify the FTS system about an operation on an FTS-indexed table. */ |
| 2605 |
static void fts_trx_table_add_op( |
| 2606 |
fts_trx_table_t *ftt, /*!< in: FTS trx table */ |
| 2607 |
doc_id_t doc_id, /*!< in: doc id */ |
| 2608 |
fts_row_state state, /*!< in: state of the row */ |
| 2609 |
ib_vector_t *fts_indexes) /*!< in: FTS indexes affected */ |
| 2610 |
{ |
| 2611 |
ib_rbt_t *rows; |
| 2612 |
ib_rbt_bound_t parent; |
| 2613 |
|
| 2614 |
rows = ftt->rows; |
| 2615 |
rbt_search(rows, &parent, &doc_id); |
| 2616 |
|
| 2617 |
/* Row id found, update state, and if new state is FTS_NOTHING, |
| 2618 |
we delete the row from our tree. */ |
| 2619 |
if (parent.result == 0) { |
| 2620 |
fts_trx_row_t *row = rbt_value(fts_trx_row_t, parent.last); |
| 2621 |
|
| 2622 |
row->state = fts_trx_row_get_new_state(row->state, state); |
| 2623 |
|
| 2624 |
if (row->state == FTS_NOTHING) { |
| 2625 |
if (row->fts_indexes) { |
| 2626 |
ib_vector_free(row->fts_indexes); |
| 2627 |
} |
| 2628 |
|
| 2629 |
ut::free(rbt_remove_node(rows, parent.last)); |
| 2630 |
row = nullptr; |
| 2631 |
} else if (row->fts_indexes != nullptr) { |
| 2632 |
ib_vector_free(row->fts_indexes); |
| 2633 |
row->fts_indexes = fts_indexes; |
| 2634 |
} |
| 2635 |
|
| 2636 |
} else { /* Row-id not found, create a new one. */ |
| 2637 |
fts_trx_row_t row; |
| 2638 |
|
| 2639 |
row.doc_id = doc_id; |
| 2640 |
row.state = state; |
| 2641 |
row.fts_indexes = fts_indexes; |
| 2642 |
|
| 2643 |
rbt_add_node(rows, &parent, &row); |
| 2644 |
} |
| 2645 |
} |
| 2646 |
|
| 2647 |
/** Notify the FTS system about an operation on an FTS-indexed table. |
| 2648 |
@param[in] trx Innodb transaction |
| 2649 |
@param[in] table Table |
| 2650 |
@param[in] doc_id Doc id |
| 2651 |
@param[in] state State of the row |
| 2652 |
@param[in] fts_indexes Fts indexes affected (null=all) */ |
| 2653 |
void fts_trx_add_op(trx_t *trx, dict_table_t *table, doc_id_t doc_id, |
| 2654 |
fts_row_state state, ib_vector_t *fts_indexes) { |
| 2655 |
fts_trx_table_t *tran_ftt; |
| 2656 |
fts_trx_table_t *stmt_ftt; |
| 2657 |
|
| 2658 |
if (!trx->fts_trx) { |
| 2659 |
trx->fts_trx = fts_trx_create(trx); |
| 2660 |
} |
| 2661 |
|
| 2662 |
tran_ftt = fts_trx_init(trx, table, trx->fts_trx->savepoints); |
| 2663 |
stmt_ftt = fts_trx_init(trx, table, trx->fts_trx->last_stmt); |
| 2664 |
|
| 2665 |
fts_trx_table_add_op(tran_ftt, doc_id, state, fts_indexes); |
| 2666 |
fts_trx_table_add_op(stmt_ftt, doc_id, state, fts_indexes); |
| 2667 |
} |
| 2668 |
|
| 2669 |
/** Fetch callback that converts a textual document id to a binary value and |
| 2670 |
stores it in the given place. |
| 2671 |
@return always returns NULL */ |
| 2672 |
static bool fts_fetch_store_doc_id(void *row, /*!< in: sel_node_t* */ |
| 2673 |
void *user_arg) /*!< in: doc_id_t* to store |
| 2674 |
doc_id in */ |
| 2675 |
{ |
| 2676 |
int n_parsed; |
| 2677 |
sel_node_t *node = static_cast<sel_node_t *>(row); |
| 2678 |
doc_id_t *doc_id = static_cast<doc_id_t *>(user_arg); |
| 2679 |
dfield_t *dfield = que_node_get_val(node->select_list); |
| 2680 |
dtype_t *type = dfield_get_type(dfield); |
| 2681 |
ulint len = dfield_get_len(dfield); |
| 2682 |
|
| 2683 |
char buf[32]; |
| 2684 |
|
| 2685 |
ut_a(dtype_get_mtype(type) == DATA_VARCHAR); |
| 2686 |
ut_a(len > 0 && len < sizeof(buf)); |
| 2687 |
|
| 2688 |
memcpy(buf, dfield_get_data(dfield), len); |
| 2689 |
buf[len] = '\0'; |
| 2690 |
|
| 2691 |
n_parsed = sscanf(buf, FTS_DOC_ID_FORMAT, doc_id); |
| 2692 |
ut_a(n_parsed == 1); |
| 2693 |
|
| 2694 |
return false; |
| 2695 |
} |
| 2696 |
|
| 2697 |
#ifdef FTS_CACHE_SIZE_DEBUG |
| 2698 |
/** Get the max cache size in bytes. If there is an error reading the |
| 2699 |
value we simply print an error message here and return the default |
| 2700 |
value to the caller. |
| 2701 |
@return max cache size in bytes */ |
| 2702 |
static ulint fts_get_max_cache_size( |
| 2703 |
trx_t *trx, /*!< in: transaction */ |
| 2704 |
fts_table_t *fts_table) /*!< in: table instance */ |
| 2705 |
{ |
| 2706 |
dberr_t error; |
| 2707 |
fts_string_t value; |
| 2708 |
ulint cache_size_in_mb; |
| 2709 |
|
| 2710 |
/* Set to the default value. */ |
| 2711 |
cache_size_in_mb = FTS_CACHE_SIZE_LOWER_LIMIT_IN_MB; |
| 2712 |
|
| 2713 |
/* We set the length of value to the max bytes it can hold. This |
| 2714 |
information is used by the callback that reads the value. */ |
| 2715 |
value.f_n_char = 0; |
| 2716 |
value.f_len = FTS_MAX_CONFIG_VALUE_LEN; |
| 2717 |
value.f_str = ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, value.f_len + 1); |
| 2718 |
|
| 2719 |
error = |
| 2720 |
fts_config_get_value(trx, fts_table, FTS_MAX_CACHE_SIZE_IN_MB, &value); |
| 2721 |
|
| 2722 |
if (error == DB_SUCCESS) { |
| 2723 |
value.f_str[value.f_len] = 0; |
| 2724 |
cache_size_in_mb = strtoul((char *)value.f_str, NULL, 10); |
| 2725 |
|
| 2726 |
if (cache_size_in_mb > FTS_CACHE_SIZE_UPPER_LIMIT_IN_MB) { |
| 2727 |
ib::warn(ER_IB_MSG_468) |
| 2728 |
<< "FTS max cache size (" << cache_size_in_mb |
| 2729 |
<< ") out of range." |
| 2730 |
" Minimum value is " |
| 2731 |
<< FTS_CACHE_SIZE_LOWER_LIMIT_IN_MB << "MB and the maximum value is " |
| 2732 |
<< FTS_CACHE_SIZE_UPPER_LIMIT_IN_MB |
| 2733 |
<< "MB, setting cache size to upper limit"; |
| 2734 |
|
| 2735 |
cache_size_in_mb = FTS_CACHE_SIZE_UPPER_LIMIT_IN_MB; |
| 2736 |
|
| 2737 |
} else if (cache_size_in_mb < FTS_CACHE_SIZE_LOWER_LIMIT_IN_MB) { |
| 2738 |
ib::warn(ER_IB_MSG_469) |
| 2739 |
<< "FTS max cache size (" << cache_size_in_mb |
| 2740 |
<< ") out of range." |
| 2741 |
" Minimum value is " |
| 2742 |
<< FTS_CACHE_SIZE_LOWER_LIMIT_IN_MB << "MB and the maximum value is" |
| 2743 |
<< FTS_CACHE_SIZE_UPPER_LIMIT_IN_MB |
| 2744 |
<< "MB, setting cache size to lower limit"; |
| 2745 |
|
| 2746 |
cache_size_in_mb = FTS_CACHE_SIZE_LOWER_LIMIT_IN_MB; |
| 2747 |
} |
| 2748 |
} else { |
| 2749 |
ib::error(ER_IB_MSG_470) << "(" << ut_strerr(error) |
| 2750 |
<< ") reading max" |
| 2751 |
" cache config value from config table"; |
| 2752 |
} |
| 2753 |
|
| 2754 |
ut::free(value.f_str); |
| 2755 |
|
| 2756 |
return (cache_size_in_mb * 1024 * 1024); |
| 2757 |
} |
| 2758 |
#endif |
| 2759 |
|
| 2760 |
/** Update the next and last Doc ID in the CONFIG table to be the input |
| 2761 |
"doc_id" value (+ 1). We would do so after each FTS index build or |
| 2762 |
table truncate */ |
| 2763 |
void fts_update_next_doc_id( |
| 2764 |
trx_t *trx, /*!< in/out: transaction */ |
| 2765 |
const dict_table_t *table, /*!< in: table */ |
| 2766 |
const char *table_name, /*!< in: table name, or NULL */ |
| 2767 |
doc_id_t doc_id) /*!< in: DOC ID to set */ |
| 2768 |
{ |
| 2769 |
table->fts->cache->synced_doc_id = doc_id; |
| 2770 |
table->fts->cache->next_doc_id = doc_id + 1; |
| 2771 |
|
| 2772 |
table->fts->cache->first_doc_id = table->fts->cache->next_doc_id; |
| 2773 |
|
| 2774 |
fts_update_sync_doc_id(table, table_name, table->fts->cache->synced_doc_id, |
| 2775 |
trx); |
| 2776 |
} |
| 2777 |
|
| 2778 |
/** Get the next available document id. |
| 2779 |
@return DB_SUCCESS if OK */ |
| 2780 |
dberr_t fts_get_next_doc_id(const dict_table_t *table, /*!< in: table */ |
| 2781 |
doc_id_t *doc_id) /*!< out: new document id */ |
| 2782 |
{ |
| 2783 |
fts_cache_t *cache = table->fts->cache; |
| 2784 |
|
| 2785 |
/* If the Doc ID system has not yet been initialized, we |
| 2786 |
will consult the CONFIG table and user table to re-establish |
| 2787 |
the initial value of the Doc ID */ |
| 2788 |
if (cache->first_doc_id == FTS_NULL_DOC_ID) { |
| 2789 |
fts_init_doc_id(table); |
| 2790 |
} |
| 2791 |
|
| 2792 |
if (!DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID)) { |
| 2793 |
*doc_id = FTS_NULL_DOC_ID; |
| 2794 |
return (DB_SUCCESS); |
| 2795 |
} |
| 2796 |
|
| 2797 |
mutex_enter(&cache->doc_id_lock); |
| 2798 |
*doc_id = ++cache->next_doc_id; |
| 2799 |
mutex_exit(&cache->doc_id_lock); |
| 2800 |
|
| 2801 |
return (DB_SUCCESS); |
| 2802 |
} |
| 2803 |
|
| 2804 |
/** This function fetch the Doc ID from CONFIG table, and compare with |
| 2805 |
the Doc ID supplied. And store the larger one to the CONFIG table. |
| 2806 |
@return DB_SUCCESS if OK */ |
| 2807 |
static dberr_t fts_cmp_set_sync_doc_id( |
| 2808 |
const dict_table_t *table, /*!< in: table */ |
| 2809 |
doc_id_t doc_id_cmp, /*!< in: Doc ID to compare */ |
| 2810 |
bool read_only, /*!< in: true if read the |
| 2811 |
synced_doc_id only */ |
| 2812 |
doc_id_t *doc_id, /*!< out: larger document id |
| 2813 |
after comparing "doc_id_cmp" |
| 2814 |
to the one stored in CONFIG |
| 2815 |
table */ |
| 2816 |
trx_t *trx = nullptr) /*!< in: transaction in which |
| 2817 |
the doc_id is retrieved and |
| 2818 |
stored */ |
| 2819 |
{ |
| 2820 |
pars_info_t *info; |
| 2821 |
dberr_t error; |
| 2822 |
fts_table_t fts_table; |
| 2823 |
que_t *graph = nullptr; |
| 2824 |
fts_cache_t *cache = table->fts->cache; |
| 2825 |
char table_name[MAX_FULL_NAME_LEN]; |
| 2826 |
bool trx_allocated; |
| 2827 |
trx_savept_t savept; |
| 2828 |
retry: |
| 2829 |
ut_a(table->fts->doc_col != ULINT_UNDEFINED); |
| 2830 |
|
| 2831 |
fts_table.suffix = FTS_SUFFIX_CONFIG; |
| 2832 |
fts_table.table_id = table->id; |
| 2833 |
fts_table.type = FTS_COMMON_TABLE; |
| 2834 |
fts_table.table = table; |
| 2835 |
|
| 2836 |
fts_table.parent = table->name.m_name; |
| 2837 |
|
| 2838 |
trx_allocated = false; |
| 2839 |
if (trx == nullptr) { |
| 2840 |
trx = trx_allocate_for_background(); |
| 2841 |
trx_allocated = true; |
| 2842 |
} else { |
| 2843 |
savept = trx_savept_take(trx); |
| 2844 |
} |
| 2845 |
|
| 2846 |
trx->op_info = "update the next FTS document id"; |
| 2847 |
|
| 2848 |
info = pars_info_create(); |
| 2849 |
|
| 2850 |
pars_info_bind_function(info, "my_func", fts_fetch_store_doc_id, doc_id); |
| 2851 |
|
| 2852 |
fts_get_table_name(&fts_table, table_name); |
| 2853 |
pars_info_bind_id(info, true, "config_table", table_name); |
| 2854 |
|
| 2855 |
graph = fts_parse_sql(&fts_table, info, |
| 2856 |
"DECLARE FUNCTION my_func;\n" |
| 2857 |
"DECLARE CURSOR c IS SELECT value FROM $config_table" |
| 2858 |
" WHERE key = 'synced_doc_id' FOR UPDATE;\n" |
| 2859 |
"BEGIN\n" |
| 2860 |
"" |
| 2861 |
"OPEN c;\n" |
| 2862 |
"WHILE 1 = 1 LOOP\n" |
| 2863 |
" FETCH c INTO my_func();\n" |
| 2864 |
" IF c % NOTFOUND THEN\n" |
| 2865 |
" EXIT;\n" |
| 2866 |
" END IF;\n" |
| 2867 |
"END LOOP;\n" |
| 2868 |
"CLOSE c;"); |
| 2869 |
|
| 2870 |
*doc_id = 0; |
| 2871 |
|
| 2872 |
error = fts_eval_sql(trx, graph); |
| 2873 |
|
| 2874 |
que_graph_free(graph); |
| 2875 |
|
| 2876 |
// FIXME: We need to retry deadlock errors |
| 2877 |
if (error != DB_SUCCESS) { |
| 2878 |
goto func_exit; |
| 2879 |
} |
| 2880 |
|
| 2881 |
if (read_only) { |
| 2882 |
goto func_exit; |
| 2883 |
} |
| 2884 |
|
| 2885 |
if (doc_id_cmp == 0 && *doc_id) { |
| 2886 |
cache->synced_doc_id = *doc_id - 1; |
| 2887 |
} else { |
| 2888 |
cache->synced_doc_id = std::max(doc_id_cmp, *doc_id); |
| 2889 |
} |
| 2890 |
|
| 2891 |
mutex_enter(&cache->doc_id_lock); |
| 2892 |
/* For each sync operation, we will add next_doc_id by 1, |
| 2893 |
so to mark a sync operation */ |
| 2894 |
if (cache->next_doc_id < cache->synced_doc_id + 1) { |
| 2895 |
cache->next_doc_id = cache->synced_doc_id + 1; |
| 2896 |
} |
| 2897 |
mutex_exit(&cache->doc_id_lock); |
| 2898 |
|
| 2899 |
if (doc_id_cmp > *doc_id) { |
| 2900 |
error = fts_update_sync_doc_id(table, table->name.m_name, |
| 2901 |
cache->synced_doc_id, trx); |
| 2902 |
} |
| 2903 |
|
| 2904 |
*doc_id = cache->next_doc_id; |
| 2905 |
|
| 2906 |
func_exit: |
| 2907 |
|
| 2908 |
if (error == DB_SUCCESS) { |
| 2909 |
if (trx_allocated) { |
| 2910 |
fts_sql_commit(trx); |
| 2911 |
} |
| 2912 |
} else { |
| 2913 |
*doc_id = 0; |
| 2914 |
|
| 2915 |
ib::error(ER_IB_MSG_471) << "(" << ut_strerr(error) |
| 2916 |
<< ") while getting" |
| 2917 |
" next doc id."; |
| 2918 |
if (trx_allocated) { |
| 2919 |
fts_sql_rollback(trx); |
| 2920 |
} else { |
| 2921 |
trx_rollback_to_savepoint(trx, &savept); |
| 2922 |
} |
| 2923 |
|
| 2924 |
if (error == DB_DEADLOCK) { |
| 2925 |
std::this_thread::sleep_for( |
| 2926 |
std::chrono::milliseconds(FTS_DEADLOCK_RETRY_WAIT_MS)); |
| 2927 |
if (trx_allocated) { |
| 2928 |
/* free trx before retry */ |
| 2929 |
trx_free_for_background(trx); |
| 2930 |
trx = nullptr; |
| 2931 |
} |
| 2932 |
goto retry; |
| 2933 |
} |
| 2934 |
} |
| 2935 |
|
| 2936 |
if (trx_allocated) { |
| 2937 |
trx_free_for_background(trx); |
| 2938 |
} |
| 2939 |
|
| 2940 |
return (error); |
| 2941 |
} |
| 2942 |
|
| 2943 |
/** Update the last document id. This function could create a new |
| 2944 |
transaction to update the last document id. |
| 2945 |
@return DB_SUCCESS if OK */ |
| 2946 |
static dberr_t fts_update_sync_doc_id( |
| 2947 |
const dict_table_t *table, /*!< in: table */ |
| 2948 |
const char *table_name, /*!< in: table name, or NULL */ |
| 2949 |
doc_id_t doc_id, /*!< in: last document id */ |
| 2950 |
trx_t *trx) /*!< in: update trx, or NULL */ |
| 2951 |
{ |
| 2952 |
byte id[FTS_MAX_ID_LEN]; |
| 2953 |
pars_info_t *info; |
| 2954 |
fts_table_t fts_table; |
| 2955 |
ulint id_len; |
| 2956 |
que_t *graph = nullptr; |
| 2957 |
dberr_t error; |
| 2958 |
bool local_trx = false; |
| 2959 |
fts_cache_t *cache = table->fts->cache; |
| 2960 |
char fts_name[MAX_FULL_NAME_LEN]; |
| 2961 |
|
| 2962 |
fts_table.suffix = FTS_SUFFIX_CONFIG; |
| 2963 |
fts_table.table_id = table->id; |
| 2964 |
fts_table.type = FTS_COMMON_TABLE; |
| 2965 |
fts_table.table = table; |
| 2966 |
if (table_name) { |
| 2967 |
fts_table.parent = table_name; |
| 2968 |
} else { |
| 2969 |
fts_table.parent = table->name.m_name; |
| 2970 |
} |
| 2971 |
|
| 2972 |
if (!trx) { |
| 2973 |
trx = trx_allocate_for_background(); |
| 2974 |
|
| 2975 |
trx->op_info = "setting last FTS document id"; |
| 2976 |
local_trx = true; |
| 2977 |
} |
| 2978 |
|
| 2979 |
info = pars_info_create(); |
| 2980 |
|
| 2981 |
id_len = snprintf((char *)id, sizeof(id), FTS_DOC_ID_FORMAT, doc_id + 1); |
| 2982 |
|
| 2983 |
pars_info_bind_varchar_literal(info, "doc_id", id, id_len); |
| 2984 |
|
| 2985 |
fts_get_table_name(&fts_table, fts_name); |
| 2986 |
pars_info_bind_id(info, true, "table_name", fts_name); |
| 2987 |
|
| 2988 |
graph = fts_parse_sql(&fts_table, info, |
| 2989 |
"BEGIN" |
| 2990 |
" UPDATE $table_name SET value = :doc_id" |
| 2991 |
" WHERE key = 'synced_doc_id';"); |
| 2992 |
|
| 2993 |
error = fts_eval_sql(trx, graph); |
| 2994 |
|
| 2995 |
que_graph_free(graph); |
| 2996 |
|
| 2997 |
if (local_trx) { |
| 2998 |
if (error == DB_SUCCESS) { |
| 2999 |
fts_sql_commit(trx); |
| 3000 |
cache->synced_doc_id = doc_id; |
| 3001 |
} else { |
| 3002 |
ib::error(ER_IB_MSG_472) << "(" << ut_strerr(error) |
| 3003 |
<< ") while" |
| 3004 |
" updating last doc id."; |
| 3005 |
|
| 3006 |
fts_sql_rollback(trx); |
| 3007 |
} |
| 3008 |
trx_free_for_background(trx); |
| 3009 |
} |
| 3010 |
|
| 3011 |
return (error); |
| 3012 |
} |
| 3013 |
|
| 3014 |
/** Create a new fts_doc_ids_t. |
| 3015 |
@return new fts_doc_ids_t */ |
| 3016 |
fts_doc_ids_t *fts_doc_ids_create(void) { |
| 3017 |
fts_doc_ids_t *fts_doc_ids; |
| 3018 |
mem_heap_t *heap = mem_heap_create(512, UT_LOCATION_HERE); |
| 3019 |
|
| 3020 |
fts_doc_ids = |
| 3021 |
static_cast<fts_doc_ids_t *>(mem_heap_alloc(heap, sizeof(*fts_doc_ids))); |
| 3022 |
|
| 3023 |
fts_doc_ids->self_heap = ib_heap_allocator_create(heap); |
| 3024 |
|
| 3025 |
fts_doc_ids->doc_ids = static_cast<ib_vector_t *>( |
| 3026 |
ib_vector_create(fts_doc_ids->self_heap, sizeof(fts_update_t), 32)); |
| 3027 |
|
| 3028 |
return (fts_doc_ids); |
| 3029 |
} |
| 3030 |
|
| 3031 |
/** Free a fts_doc_ids_t. */ |
| 3032 |
void fts_doc_ids_free(fts_doc_ids_t *fts_doc_ids) { |
| 3033 |
mem_heap_t *heap = static_cast<mem_heap_t *>(fts_doc_ids->self_heap->arg); |
| 3034 |
|
| 3035 |
memset(fts_doc_ids, 0, sizeof(*fts_doc_ids)); |
| 3036 |
|
| 3037 |
mem_heap_free(heap); |
| 3038 |
} |
| 3039 |
|
| 3040 |
/** Do commit-phase steps necessary for the insertion of a new row. |
| 3041 |
@param[in] ftt FTS transaction table |
| 3042 |
@param[in] row row to be inserted in index |
| 3043 |
*/ |
| 3044 |
static void fts_add(fts_trx_table_t *ftt, fts_trx_row_t *row) { |
| 3045 |
dict_table_t *table = ftt->table; |
| 3046 |
doc_id_t doc_id = row->doc_id; |
| 3047 |
|
| 3048 |
ut_a(row->state == FTS_INSERT || row->state == FTS_MODIFY); |
| 3049 |
|
| 3050 |
fts_add_doc_by_id(ftt, doc_id, row->fts_indexes); |
| 3051 |
|
| 3052 |
mutex_enter(&table->fts->cache->deleted_lock); |
| 3053 |
++table->fts->cache->added; |
| 3054 |
mutex_exit(&table->fts->cache->deleted_lock); |
| 3055 |
|
| 3056 |
if (!DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID) && |
| 3057 |
doc_id >= table->fts->cache->next_doc_id) { |
| 3058 |
table->fts->cache->next_doc_id = doc_id + 1; |
| 3059 |
} |
| 3060 |
} |
| 3061 |
|
| 3062 |
/** Do commit-phase steps necessary for the deletion of a row. |
| 3063 |
@return DB_SUCCESS or error code */ |
| 3064 |
[[nodiscard]] static dberr_t fts_delete( |
| 3065 |
fts_trx_table_t *ftt, /*!< in: FTS trx table */ |
| 3066 |
fts_trx_row_t *row) /*!< in: row */ |
| 3067 |
{ |
| 3068 |
que_t *graph; |
| 3069 |
fts_table_t fts_table; |
| 3070 |
dberr_t error = DB_SUCCESS; |
| 3071 |
doc_id_t write_doc_id; |
| 3072 |
dict_table_t *table = ftt->table; |
| 3073 |
doc_id_t doc_id = row->doc_id; |
| 3074 |
trx_t *trx = ftt->fts_trx->trx; |
| 3075 |
pars_info_t *info = pars_info_create(); |
| 3076 |
fts_cache_t *cache = table->fts->cache; |
| 3077 |
|
| 3078 |
/* we do not index Documents whose Doc ID value is 0 */ |
| 3079 |
if (doc_id == FTS_NULL_DOC_ID) { |
| 3080 |
ut_ad(!DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID)); |
| 3081 |
return (error); |
| 3082 |
} |
| 3083 |
|
| 3084 |
ut_a(row->state == FTS_DELETE || row->state == FTS_MODIFY); |
| 3085 |
|
| 3086 |
FTS_INIT_FTS_TABLE(&fts_table, FTS_SUFFIX_DELETED, FTS_COMMON_TABLE, table); |
| 3087 |
|
| 3088 |
/* Convert to "storage" byte order. */ |
| 3089 |
fts_write_doc_id((byte *)&write_doc_id, doc_id); |
| 3090 |
fts_bind_doc_id(info, "doc_id", &write_doc_id); |
| 3091 |
|
| 3092 |
/* It is possible we update a record that has not yet been sync-ed |
| 3093 |
into cache from last crash (delete Doc will not initialize the |
| 3094 |
sync). Avoid any added counter accounting until the FTS cache |
| 3095 |
is re-established and sync-ed */ |
| 3096 |
if (table->fts->fts_status & ADDED_TABLE_SYNCED && |
| 3097 |
doc_id > cache->synced_doc_id) { |
| 3098 |
mutex_enter(&table->fts->cache->deleted_lock); |
| 3099 |
|
| 3100 |
/* The Doc ID could belong to those left in |
| 3101 |
ADDED table from last crash. So need to check |
| 3102 |
if it is less than first_doc_id when we initialize |
| 3103 |
the Doc ID system after reboot */ |
| 3104 |
if (doc_id >= table->fts->cache->first_doc_id && |
| 3105 |
table->fts->cache->added > 0) { |
| 3106 |
--table->fts->cache->added; |
| 3107 |
} |
| 3108 |
|
| 3109 |
mutex_exit(&table->fts->cache->deleted_lock); |
| 3110 |
|
| 3111 |
/* Only if the row was really deleted. */ |
| 3112 |
ut_a(row->state == FTS_DELETE || row->state == FTS_MODIFY); |
| 3113 |
} |
| 3114 |
|
| 3115 |
/* Note the deleted document for OPTIMIZE to purge. */ |
| 3116 |
if (error == DB_SUCCESS) { |
| 3117 |
char table_name[MAX_FULL_NAME_LEN]; |
| 3118 |
|
| 3119 |
trx->op_info = "adding doc id to FTS DELETED"; |
| 3120 |
|
| 3121 |
info->graph_owns_us = true; |
| 3122 |
|
| 3123 |
fts_table.suffix = FTS_SUFFIX_DELETED; |
| 3124 |
|
| 3125 |
fts_get_table_name(&fts_table, table_name); |
| 3126 |
pars_info_bind_id(info, true, "deleted", table_name); |
| 3127 |
|
| 3128 |
graph = fts_parse_sql(&fts_table, info, |
| 3129 |
"BEGIN INSERT INTO $deleted VALUES (:doc_id);"); |
| 3130 |
|
| 3131 |
error = fts_eval_sql(trx, graph); |
| 3132 |
|
| 3133 |
fts_que_graph_free(graph); |
| 3134 |
} else { |
| 3135 |
pars_info_free(info); |
| 3136 |
} |
| 3137 |
|
| 3138 |
/* Increment the total deleted count, this is used to calculate the |
| 3139 |
number of documents indexed. */ |
| 3140 |
if (error == DB_SUCCESS) { |
| 3141 |
mutex_enter(&table->fts->cache->deleted_lock); |
| 3142 |
|
| 3143 |
++table->fts->cache->deleted; |
| 3144 |
|
| 3145 |
mutex_exit(&table->fts->cache->deleted_lock); |
| 3146 |
} |
| 3147 |
|
| 3148 |
return (error); |
| 3149 |
} |
| 3150 |
|
| 3151 |
/** Do commit-phase steps necessary for the modification of a row. |
| 3152 |
@return DB_SUCCESS or error code */ |
| 3153 |
[[nodiscard]] static dberr_t fts_modify( |
| 3154 |
fts_trx_table_t *ftt, /*!< in: FTS trx table */ |
| 3155 |
fts_trx_row_t *row) /*!< in: row */ |
| 3156 |
{ |
| 3157 |
dberr_t error; |
| 3158 |
|
| 3159 |
ut_a(row->state == FTS_MODIFY); |
| 3160 |
|
| 3161 |
error = fts_delete(ftt, row); |
| 3162 |
|
| 3163 |
if (error == DB_SUCCESS) { |
| 3164 |
fts_add(ftt, row); |
| 3165 |
} |
| 3166 |
|
| 3167 |
return (error); |
| 3168 |
} |
| 3169 |
|
| 3170 |
dberr_t fts_create_doc_id(dict_table_t *table, dtuple_t *row, |
| 3171 |
mem_heap_t *heap) { |
| 3172 |
doc_id_t doc_id; |
| 3173 |
dberr_t error = DB_SUCCESS; |
| 3174 |
|
| 3175 |
ut_a(table->fts->doc_col != ULINT_UNDEFINED); |
| 3176 |
|
| 3177 |
if (!DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID)) { |
| 3178 |
if (table->fts->cache->first_doc_id == FTS_NULL_DOC_ID) { |
| 3179 |
error = fts_get_next_doc_id(table, &doc_id); |
| 3180 |
} |
| 3181 |
return (error); |
| 3182 |
} |
| 3183 |
|
| 3184 |
error = fts_get_next_doc_id(table, &doc_id); |
| 3185 |
|
| 3186 |
if (error == DB_SUCCESS) { |
| 3187 |
dfield_t *dfield; |
| 3188 |
doc_id_t *write_doc_id; |
| 3189 |
|
| 3190 |
ut_a(doc_id > 0); |
| 3191 |
|
| 3192 |
dfield = dtuple_get_nth_field(row, table->fts->doc_col); |
| 3193 |
write_doc_id = |
| 3194 |
static_cast<doc_id_t *>(mem_heap_alloc(heap, sizeof(*write_doc_id))); |
| 3195 |
|
| 3196 |
ut_a(doc_id != FTS_NULL_DOC_ID); |
| 3197 |
ut_a(sizeof(doc_id) == dfield->type.len); |
| 3198 |
fts_write_doc_id((byte *)write_doc_id, doc_id); |
| 3199 |
|
| 3200 |
dfield_set_data(dfield, write_doc_id, sizeof(*write_doc_id)); |
| 3201 |
} |
| 3202 |
|
| 3203 |
return (error); |
| 3204 |
} |
| 3205 |
|
| 3206 |
/** The given transaction is about to be committed; do whatever is necessary |
| 3207 |
from the FTS system's POV. |
| 3208 |
@return DB_SUCCESS or error code */ |
| 3209 |
[[nodiscard]] static dberr_t fts_commit_table( |
| 3210 |
fts_trx_table_t *ftt) /*!< in: FTS table to commit*/ |
| 3211 |
{ |
| 3212 |
const ib_rbt_node_t *node; |
| 3213 |
ib_rbt_t *rows; |
| 3214 |
dberr_t error = DB_SUCCESS; |
| 3215 |
fts_cache_t *cache = ftt->table->fts->cache; |
| 3216 |
trx_t *trx = trx_allocate_for_background(); |
| 3217 |
|
| 3218 |
rows = ftt->rows; |
| 3219 |
|
| 3220 |
ftt->fts_trx->trx = trx; |
| 3221 |
|
| 3222 |
if (cache->get_docs == nullptr) { |
| 3223 |
rw_lock_x_lock(&cache->init_lock, UT_LOCATION_HERE); |
| 3224 |
if (cache->get_docs == nullptr) { |
| 3225 |
cache->get_docs = fts_get_docs_create(cache); |
| 3226 |
} |
| 3227 |
rw_lock_x_unlock(&cache->init_lock); |
| 3228 |
} |
| 3229 |
|
| 3230 |
for (node = rbt_first(rows); node != nullptr && error == DB_SUCCESS; |
| 3231 |
node = rbt_next(rows, node)) { |
| 3232 |
fts_trx_row_t *row = rbt_value(fts_trx_row_t, node); |
| 3233 |
|
| 3234 |
switch (row->state) { |
| 3235 |
case FTS_INSERT: |
| 3236 |
fts_add(ftt, row); |
| 3237 |
break; |
| 3238 |
|
| 3239 |
case FTS_MODIFY: |
| 3240 |
error = fts_modify(ftt, row); |
| 3241 |
break; |
| 3242 |
|
| 3243 |
case FTS_DELETE: |
| 3244 |
error = fts_delete(ftt, row); |
| 3245 |
break; |
| 3246 |
|
| 3247 |
default: |
| 3248 |
ut_error; |
| 3249 |
} |
| 3250 |
} |
| 3251 |
|
| 3252 |
fts_sql_commit(trx); |
| 3253 |
|
| 3254 |
trx_free_for_background(trx); |
| 3255 |
|
| 3256 |
return (error); |
| 3257 |
} |
| 3258 |
|
| 3259 |
/** The given transaction is about to be committed; do whatever is necessary |
| 3260 |
from the FTS system's POV. |
| 3261 |
@return DB_SUCCESS or error code */ |
| 3262 |
dberr_t fts_commit(trx_t *trx) /*!< in: transaction */ |
| 3263 |
{ |
| 3264 |
const ib_rbt_node_t *node; |
| 3265 |
dberr_t error; |
| 3266 |
ib_rbt_t *tables; |
| 3267 |
fts_savepoint_t *savepoint; |
| 3268 |
|
| 3269 |
savepoint = |
| 3270 |
static_cast<fts_savepoint_t *>(ib_vector_last(trx->fts_trx->savepoints)); |
| 3271 |
tables = savepoint->tables; |
| 3272 |
|
| 3273 |
for (node = rbt_first(tables), error = DB_SUCCESS; |
| 3274 |
node != nullptr && error == DB_SUCCESS; node = rbt_next(tables, node)) { |
| 3275 |
fts_trx_table_t **ftt; |
| 3276 |
|
| 3277 |
ftt = rbt_value(fts_trx_table_t *, node); |
| 3278 |
|
| 3279 |
error = fts_commit_table(*ftt); |
| 3280 |
} |
| 3281 |
|
| 3282 |
return (error); |
| 3283 |
} |
| 3284 |
|
| 3285 |
/** Initialize a document. */ |
| 3286 |
void fts_doc_init(fts_doc_t *doc) /*!< in: doc to initialize */ |
| 3287 |
{ |
| 3288 |
mem_heap_t *heap = mem_heap_create(32, UT_LOCATION_HERE); |
| 3289 |
|
| 3290 |
memset(doc, 0, sizeof(*doc)); |
| 3291 |
|
| 3292 |
doc->self_heap = ib_heap_allocator_create(heap); |
| 3293 |
} |
| 3294 |
|
| 3295 |
/** Free document. */ |
| 3296 |
void fts_doc_free(fts_doc_t *doc) /*!< in: document */ |
| 3297 |
{ |
| 3298 |
mem_heap_t *heap = static_cast<mem_heap_t *>(doc->self_heap->arg); |
| 3299 |
|
| 3300 |
if (doc->tokens) { |
| 3301 |
rbt_free(doc->tokens); |
| 3302 |
} |
| 3303 |
|
| 3304 |
ut_d(memset(doc, 0, sizeof(*doc))); |
| 3305 |
|
| 3306 |
mem_heap_free(heap); |
| 3307 |
} |
| 3308 |
|
| 3309 |
/** Callback function for fetch that stores the text of an FTS document, |
| 3310 |
converting each column to UTF-16. |
| 3311 |
@return always false */ |
| 3312 |
bool fts_query_expansion_fetch_doc(void *row, /*!< in: sel_node_t* */ |
| 3313 |
void *user_arg) /*!< in: fts_doc_t* */ |
| 3314 |
{ |
| 3315 |
que_node_t *exp; |
| 3316 |
sel_node_t *node = static_cast<sel_node_t *>(row); |
| 3317 |
fts_doc_t *result_doc = static_cast<fts_doc_t *>(user_arg); |
| 3318 |
dfield_t *dfield; |
| 3319 |
ulint len; |
| 3320 |
ulint doc_len; |
| 3321 |
fts_doc_t doc; |
| 3322 |
CHARSET_INFO *doc_charset = nullptr; |
| 3323 |
ulint field_no = 0; |
| 3324 |
|
| 3325 |
len = 0; |
| 3326 |
|
| 3327 |
fts_doc_init(&doc); |
| 3328 |
doc.found = true; |
| 3329 |
|
| 3330 |
exp = node->select_list; |
| 3331 |
doc_len = 0; |
| 3332 |
|
| 3333 |
doc_charset = result_doc->charset; |
| 3334 |
|
| 3335 |
/* Copy each indexed column content into doc->text.f_str */ |
| 3336 |
while (exp) { |
| 3337 |
dfield = que_node_get_val(exp); |
| 3338 |
len = dfield_get_len(dfield); |
| 3339 |
|
| 3340 |
/* NULL column */ |
| 3341 |
if (len == UNIV_SQL_NULL) { |
| 3342 |
exp = que_node_get_next(exp); |
| 3343 |
continue; |
| 3344 |
} |
| 3345 |
|
| 3346 |
if (!doc_charset) { |
| 3347 |
doc_charset = fts_get_charset(dfield->type.prtype); |
| 3348 |
} |
| 3349 |
|
| 3350 |
doc.charset = doc_charset; |
| 3351 |
doc.is_ngram = result_doc->is_ngram; |
| 3352 |
|
| 3353 |
if (dfield_is_ext(dfield)) { |
| 3354 |
/* We ignore columns that are stored externally, this |
| 3355 |
could result in too many words to search */ |
| 3356 |
exp = que_node_get_next(exp); |
| 3357 |
continue; |
| 3358 |
} else { |
| 3359 |
doc.text.f_n_char = 0; |
| 3360 |
|
| 3361 |
doc.text.f_str = static_cast<byte *>(dfield_get_data(dfield)); |
| 3362 |
|
| 3363 |
doc.text.f_len = len; |
| 3364 |
} |
| 3365 |
|
| 3366 |
if (field_no == 0) { |
| 3367 |
fts_tokenize_document(&doc, result_doc, result_doc->parser); |
| 3368 |
} else { |
| 3369 |
fts_tokenize_document_next(&doc, doc_len, result_doc, result_doc->parser); |
| 3370 |
} |
| 3371 |
|
| 3372 |
exp = que_node_get_next(exp); |
| 3373 |
|
| 3374 |
doc_len += (exp) ? len + 1 : len; |
| 3375 |
|
| 3376 |
field_no++; |
| 3377 |
} |
| 3378 |
|
| 3379 |
ut_ad(doc_charset); |
| 3380 |
|
| 3381 |
if (!result_doc->charset) { |
| 3382 |
result_doc->charset = doc_charset; |
| 3383 |
} |
| 3384 |
|
| 3385 |
fts_doc_free(&doc); |
| 3386 |
|
| 3387 |
return false; |
| 3388 |
} |
| 3389 |
|
| 3390 |
/** fetch and tokenize the document. */ |
| 3391 |
static void fts_fetch_doc_from_rec( |
| 3392 |
fts_get_doc_t *get_doc, /*!< in: FTS index's get_doc struct */ |
| 3393 |
dict_index_t *clust_index, /*!< in: cluster index */ |
| 3394 |
btr_pcur_t *pcur, /*!< in: cursor whose position |
| 3395 |
has been stored */ |
| 3396 |
ulint *offsets, /*!< in: offsets */ |
| 3397 |
fts_doc_t *doc) /*!< out: fts doc to hold parsed |
| 3398 |
documents */ |
| 3399 |
{ |
| 3400 |
dict_index_t *index; |
| 3401 |
dict_table_t *table; |
| 3402 |
const rec_t *clust_rec; |
| 3403 |
ulint num_field; |
| 3404 |
const dict_field_t *ifield; |
| 3405 |
const dict_col_t *col; |
| 3406 |
uint16_t clust_pos; |
| 3407 |
ulint i; |
| 3408 |
ulint doc_len = 0; |
| 3409 |
ulint processed_doc = 0; |
| 3410 |
st_mysql_ftparser *parser; |
| 3411 |
|
| 3412 |
if (!get_doc) { |
| 3413 |
return; |
| 3414 |
} |
| 3415 |
|
| 3416 |
index = get_doc->index_cache->index; |
| 3417 |
table = get_doc->index_cache->index->table; |
| 3418 |
parser = get_doc->index_cache->index->parser; |
| 3419 |
|
| 3420 |
clust_rec = pcur->get_rec(); |
| 3421 |
|
| 3422 |
num_field = dict_index_get_n_fields(index); |
| 3423 |
|
| 3424 |
for (i = 0; i < num_field; i++) { |
| 3425 |
ifield = index->get_field(i); |
| 3426 |
col = ifield->col; |
| 3427 |
clust_pos = static_cast<uint16_t>(dict_col_get_clust_pos(col, clust_index)); |
| 3428 |
|
| 3429 |
if (!get_doc->index_cache->charset) { |
| 3430 |
get_doc->index_cache->charset = fts_get_charset(ifield->col->prtype); |
| 3431 |
} |
| 3432 |
|
| 3433 |
if (rec_offs_nth_extern(index, offsets, clust_pos)) { |
| 3434 |
doc->text.f_str = lob::btr_rec_copy_externally_stored_field( |
| 3435 |
nullptr, clust_index, clust_rec, offsets, dict_table_page_size(table), |
| 3436 |
clust_pos, &doc->text.f_len, nullptr, false, |
| 3437 |
static_cast<mem_heap_t *>(doc->self_heap->arg)); |
| 3438 |
} else { |
| 3439 |
doc->text.f_str = const_cast<byte *>(rec_get_nth_field_instant( |
| 3440 |
clust_rec, offsets, clust_pos, clust_index, &doc->text.f_len)); |
| 3441 |
} |
| 3442 |
|
| 3443 |
doc->found = true; |
| 3444 |
doc->charset = get_doc->index_cache->charset; |
| 3445 |
doc->is_ngram = index->is_ngram; |
| 3446 |
|
| 3447 |
/* Null Field */ |
| 3448 |
if (doc->text.f_len == UNIV_SQL_NULL || doc->text.f_len == 0) { |
| 3449 |
continue; |
| 3450 |
} |
| 3451 |
|
| 3452 |
if (processed_doc == 0) { |
| 3453 |
fts_tokenize_document(doc, nullptr, parser); |
| 3454 |
} else { |
| 3455 |
fts_tokenize_document_next(doc, doc_len, nullptr, parser); |
| 3456 |
} |
| 3457 |
|
| 3458 |
processed_doc++; |
| 3459 |
doc_len += doc->text.f_len + 1; |
| 3460 |
} |
| 3461 |
} |
| 3462 |
|
| 3463 |
/** Fetch the data from tuple and tokenize the document. |
| 3464 |
@param[in] get_doc FTS index's get_doc struct |
| 3465 |
@param[in] tuple tuple should be arranged in table schema order |
| 3466 |
@param[out] doc fts doc to hold parsed documents. */ |
| 3467 |
static void fts_fetch_doc_from_tuple(fts_get_doc_t *get_doc, |
| 3468 |
const dtuple_t *tuple, fts_doc_t *doc) { |
| 3469 |
dict_index_t *index; |
| 3470 |
st_mysql_ftparser *parser; |
| 3471 |
ulint doc_len = 0; |
| 3472 |
ulint processed_doc = 0; |
| 3473 |
ulint num_field; |
| 3474 |
|
| 3475 |
if (get_doc == nullptr) { |
| 3476 |
return; |
| 3477 |
} |
| 3478 |
|
| 3479 |
index = get_doc->index_cache->index; |
| 3480 |
parser = get_doc->index_cache->index->parser; |
| 3481 |
num_field = dict_index_get_n_fields(index); |
| 3482 |
|
| 3483 |
for (ulint i = 0; i < num_field; i++) { |
| 3484 |
const dict_field_t *ifield; |
| 3485 |
const dict_col_t *col; |
| 3486 |
ulint pos; |
| 3487 |
dfield_t *field; |
| 3488 |
|
| 3489 |
ifield = index->get_field(i); |
| 3490 |
col = ifield->col; |
| 3491 |
pos = dict_col_get_no(col); |
| 3492 |
field = dtuple_get_nth_field(tuple, pos); |
| 3493 |
|
| 3494 |
if (!get_doc->index_cache->charset) { |
| 3495 |
get_doc->index_cache->charset = fts_get_charset(ifield->col->prtype); |
| 3496 |
} |
| 3497 |
|
| 3498 |
ut_ad(!dfield_is_ext(field)); |
| 3499 |
|
| 3500 |
doc->text.f_str = (byte *)dfield_get_data(field); |
| 3501 |
doc->text.f_len = dfield_get_len(field); |
| 3502 |
doc->found = true; |
| 3503 |
doc->charset = get_doc->index_cache->charset; |
| 3504 |
doc->is_ngram = index->is_ngram; |
| 3505 |
|
| 3506 |
/* field data is NULL. */ |
| 3507 |
if (doc->text.f_len == UNIV_SQL_NULL || doc->text.f_len == 0) { |
| 3508 |
continue; |
| 3509 |
} |
| 3510 |
|
| 3511 |
if (processed_doc == 0) { |
| 3512 |
fts_tokenize_document(doc, nullptr, parser); |
| 3513 |
} else { |
| 3514 |
fts_tokenize_document_next(doc, doc_len, nullptr, parser); |
| 3515 |
} |
| 3516 |
|
| 3517 |
processed_doc++; |
| 3518 |
doc_len += doc->text.f_len + 1; |
| 3519 |
} |
| 3520 |
} |
| 3521 |
|
| 3522 |
/** Fetch the document from tuple, tokenize the text data and |
| 3523 |
insert the text data into fts auxiliary table and |
| 3524 |
its cache. Moreover this tuple fields doesn't contain any information |
| 3525 |
about externally stored field. This tuple contains data directly |
| 3526 |
converted from mysql. |
| 3527 |
@param[in] ftt FTS transaction table |
| 3528 |
@param[in] doc_id doc id |
| 3529 |
@param[in] tuple tuple from where data can be retrieved |
| 3530 |
and tuple should be arranged in table |
| 3531 |
schema order. */ |
| 3532 |
void fts_add_doc_from_tuple(fts_trx_table_t *ftt, doc_id_t doc_id, |
| 3533 |
const dtuple_t *tuple) { |
| 3534 |
mtr_t mtr; |
| 3535 |
fts_cache_t *cache = ftt->table->fts->cache; |
| 3536 |
|
| 3537 |
ut_ad(cache->get_docs); |
| 3538 |
|
| 3539 |
if (!(ftt->table->fts->fts_status & ADDED_TABLE_SYNCED)) { |
| 3540 |
fts_init_index(ftt->table, false); |
| 3541 |
} |
| 3542 |
|
| 3543 |
mtr_start(&mtr); |
| 3544 |
|
| 3545 |
ulint num_idx = ib_vector_size(cache->get_docs); |
| 3546 |
|
| 3547 |
for (ulint i = 0; i < num_idx; ++i) { |
| 3548 |
fts_doc_t doc; |
| 3549 |
dict_table_t *table; |
| 3550 |
fts_get_doc_t *get_doc; |
| 3551 |
|
| 3552 |
get_doc = static_cast<fts_get_doc_t *>(ib_vector_get(cache->get_docs, i)); |
| 3553 |
table = get_doc->index_cache->index->table; |
| 3554 |
|
| 3555 |
fts_doc_init(&doc); |
| 3556 |
fts_fetch_doc_from_tuple(get_doc, tuple, &doc); |
| 3557 |
|
| 3558 |
if (doc.found) { |
| 3559 |
mtr_commit(&mtr); |
| 3560 |
rw_lock_x_lock(&table->fts->cache->lock, UT_LOCATION_HERE); |
| 3561 |
|
| 3562 |
if (table->fts->cache->stopword_info.status & STOPWORD_NOT_INIT) { |
| 3563 |
fts_load_stopword(table, nullptr, nullptr, nullptr, true, true); |
| 3564 |
} |
| 3565 |
|
| 3566 |
fts_cache_add_doc(table->fts->cache, get_doc->index_cache, doc_id, |
| 3567 |
doc.tokens); |
| 3568 |
|
| 3569 |
rw_lock_x_unlock(&table->fts->cache->lock); |
| 3570 |
|
| 3571 |
if (cache->total_size > fts_max_cache_size / 5 || fts_need_sync) { |
| 3572 |
fts_sync(cache->sync, true, false, false); |
| 3573 |
} |
| 3574 |
|
| 3575 |
mtr_start(&mtr); |
| 3576 |
} |
| 3577 |
|
| 3578 |
fts_doc_free(&doc); |
| 3579 |
} |
| 3580 |
|
| 3581 |
mtr_commit(&mtr); |
| 3582 |
} |
| 3583 |
|
| 3584 |
static ulint fts_add_doc_by_id(fts_trx_table_t *ftt, doc_id_t doc_id, |
| 3585 |
ib_vector_t *fts_indexes [[maybe_unused]]) { |
| 3586 |
mtr_t mtr; |
| 3587 |
mem_heap_t *heap; |
| 3588 |
btr_pcur_t pcur; |
| 3589 |
dict_table_t *table; |
| 3590 |
dtuple_t *tuple; |
| 3591 |
dfield_t *dfield; |
| 3592 |
fts_get_doc_t *get_doc; |
| 3593 |
doc_id_t temp_doc_id; |
| 3594 |
dict_index_t *clust_index; |
| 3595 |
dict_index_t *fts_id_index; |
| 3596 |
fts_cache_t *cache = ftt->table->fts->cache; |
| 3597 |
|
| 3598 |
ut_ad(cache->get_docs); |
| 3599 |
|
| 3600 |
/* If Doc ID has been supplied by the user, then the table |
| 3601 |
might not yet be sync-ed */ |
| 3602 |
|
| 3603 |
if (!(ftt->table->fts->fts_status & ADDED_TABLE_SYNCED)) { |
| 3604 |
fts_init_index(ftt->table, false); |
| 3605 |
} |
| 3606 |
|
| 3607 |
/* Get the first FTS index's get_doc */ |
| 3608 |
get_doc = static_cast<fts_get_doc_t *>(ib_vector_get(cache->get_docs, 0)); |
| 3609 |
ut_ad(get_doc); |
| 3610 |
|
| 3611 |
table = get_doc->index_cache->index->table; |
| 3612 |
|
| 3613 |
heap = mem_heap_create(512, UT_LOCATION_HERE); |
| 3614 |
|
| 3615 |
clust_index = table->first_index(); |
| 3616 |
fts_id_index = table->fts_doc_id_index; |
| 3617 |
|
| 3618 |
/* Check whether the index on FTS_DOC_ID is cluster index */ |
| 3619 |
auto is_id_cluster = (clust_index == fts_id_index); |
| 3620 |
|
| 3621 |
mtr_start(&mtr); |
| 3622 |
pcur.init(); |
| 3623 |
|
| 3624 |
/* Search based on Doc ID. Here, we'll need to consider the case |
| 3625 |
when there is no primary index on Doc ID */ |
| 3626 |
tuple = dtuple_create(heap, 1); |
| 3627 |
dfield = dtuple_get_nth_field(tuple, 0); |
| 3628 |
dfield->type.mtype = DATA_INT; |
| 3629 |
dfield->type.prtype = DATA_NOT_NULL | DATA_UNSIGNED | DATA_BINARY_TYPE; |
| 3630 |
|
| 3631 |
mach_write_to_8((byte *)&temp_doc_id, doc_id); |
| 3632 |
dfield_set_data(dfield, &temp_doc_id, sizeof(temp_doc_id)); |
| 3633 |
|
| 3634 |
pcur.open_no_init(fts_id_index, tuple, PAGE_CUR_LE, BTR_SEARCH_LEAF, 0, &mtr, |
| 3635 |
UT_LOCATION_HERE); |
| 3636 |
|
| 3637 |
/* If we have a match, add the data to doc structure */ |
| 3638 |
if (pcur.get_low_match() == 1) { |
| 3639 |
const rec_t *rec; |
| 3640 |
btr_pcur_t *doc_pcur; |
| 3641 |
const rec_t *clust_rec; |
| 3642 |
btr_pcur_t clust_pcur; |
| 3643 |
ulint *offsets = nullptr; |
| 3644 |
ulint num_idx = ib_vector_size(cache->get_docs); |
| 3645 |
|
| 3646 |
rec = pcur.get_rec(); |
| 3647 |
|
| 3648 |
/* Doc could be deleted */ |
| 3649 |
if (page_rec_is_infimum(rec) || |
| 3650 |
rec_get_deleted_flag(rec, dict_table_is_comp(table))) { |
| 3651 |
goto func_exit; |
| 3652 |
} |
| 3653 |
|
| 3654 |
if (is_id_cluster) { |
| 3655 |
clust_rec = rec; |
| 3656 |
doc_pcur = &pcur; |
| 3657 |
} else { |
| 3658 |
dtuple_t *clust_ref; |
| 3659 |
ulint n_fields; |
| 3660 |
|
| 3661 |
clust_pcur.init(); |
| 3662 |
n_fields = dict_index_get_n_unique(clust_index); |
| 3663 |
|
| 3664 |
clust_ref = dtuple_create(heap, n_fields); |
| 3665 |
dict_index_copy_types(clust_ref, clust_index, n_fields); |
| 3666 |
|
| 3667 |
row_build_row_ref_in_tuple(clust_ref, rec, fts_id_index, nullptr); |
| 3668 |
|
| 3669 |
clust_pcur.open_no_init(clust_index, clust_ref, PAGE_CUR_LE, |
| 3670 |
BTR_SEARCH_LEAF, 0, &mtr, UT_LOCATION_HERE); |
| 3671 |
|
| 3672 |
doc_pcur = &clust_pcur; |
| 3673 |
clust_rec = clust_pcur.get_rec(); |
| 3674 |
} |
| 3675 |
|
| 3676 |
offsets = rec_get_offsets(clust_rec, clust_index, nullptr, ULINT_UNDEFINED, |
| 3677 |
UT_LOCATION_HERE, &heap); |
| 3678 |
|
| 3679 |
for (ulint i = 0; i < num_idx; ++i) { |
| 3680 |
fts_doc_t doc; |
| 3681 |
dict_table_t *table; |
| 3682 |
fts_get_doc_t *get_doc; |
| 3683 |
|
| 3684 |
get_doc = static_cast<fts_get_doc_t *>(ib_vector_get(cache->get_docs, i)); |
| 3685 |
|
| 3686 |
table = get_doc->index_cache->index->table; |
| 3687 |
|
| 3688 |
fts_doc_init(&doc); |
| 3689 |
|
| 3690 |
fts_fetch_doc_from_rec(get_doc, clust_index, doc_pcur, offsets, &doc); |
| 3691 |
|
| 3692 |
if (doc.found) { |
| 3693 |
doc_pcur->store_position(&mtr); |
| 3694 |
mtr_commit(&mtr); |
| 3695 |
|
| 3696 |
DEBUG_SYNC_C("fts_instrument_sync_cache_wait"); |
| 3697 |
rw_lock_x_lock(&table->fts->cache->lock, UT_LOCATION_HERE); |
| 3698 |
|
| 3699 |
if (table->fts->cache->stopword_info.status & STOPWORD_NOT_INIT) { |
| 3700 |
fts_load_stopword(table, nullptr, nullptr, nullptr, true, true); |
| 3701 |
} |
| 3702 |
|
| 3703 |
fts_cache_add_doc(table->fts->cache, get_doc->index_cache, doc_id, |
| 3704 |
doc.tokens); |
| 3705 |
|
| 3706 |
bool need_sync = false; |
| 3707 |
if ((cache->total_size - cache->total_size_before_sync > |
| 3708 |
fts_max_cache_size / 10 || |
| 3709 |
fts_need_sync) && |
| 3710 |
!cache->sync->in_progress) { |
| 3711 |
need_sync = true; |
| 3712 |
cache->total_size_before_sync = cache->total_size; |
| 3713 |
} |
| 3714 |
|
| 3715 |
rw_lock_x_unlock(&table->fts->cache->lock); |
| 3716 |
|
| 3717 |
DBUG_EXECUTE_IF("fts_instrument_sync_cache_wait", { |
| 3718 |
ut_a(srv_fatal_semaphore_wait_extend.load() == 0); |
| 3719 |
// we size smaller than permissible min value for this sys var |
| 3720 |
const auto old_fts_max_cache_size = fts_max_cache_size; |
| 3721 |
fts_max_cache_size = 100; |
| 3722 |
fts_sync(cache->sync, true, true, false); |
| 3723 |
fts_max_cache_size = old_fts_max_cache_size; |
| 3724 |
}); |
| 3725 |
|
| 3726 |
DBUG_EXECUTE_IF("fts_instrument_sync", |
| 3727 |
fts_optimize_request_sync_table(table); |
| 3728 |
os_event_wait(cache->sync->event);); |
| 3729 |
|
| 3730 |
DBUG_EXECUTE_IF("fts_instrument_sync_debug", |
| 3731 |
fts_sync(cache->sync, true, true, false);); |
| 3732 |
|
| 3733 |
DEBUG_SYNC_C("fts_instrument_sync_request"); |
| 3734 |
DBUG_EXECUTE_IF("fts_instrument_sync_request", |
| 3735 |
fts_optimize_request_sync_table(table);); |
| 3736 |
|
| 3737 |
if (need_sync) { |
| 3738 |
fts_optimize_request_sync_table(table); |
| 3739 |
} |
| 3740 |
|
| 3741 |
mtr_start(&mtr); |
| 3742 |
|
| 3743 |
if (i < num_idx - 1) { |
| 3744 |
[[maybe_unused]] auto success = doc_pcur->restore_position( |
| 3745 |
BTR_SEARCH_LEAF, &mtr, UT_LOCATION_HERE); |
| 3746 |
|
| 3747 |
ut_ad(success); |
| 3748 |
} |
| 3749 |
} |
| 3750 |
|
| 3751 |
fts_doc_free(&doc); |
| 3752 |
} |
| 3753 |
|
| 3754 |
if (!is_id_cluster) { |
| 3755 |
doc_pcur->close(); |
| 3756 |
} |
| 3757 |
} |
| 3758 |
func_exit: |
| 3759 |
mtr_commit(&mtr); |
| 3760 |
|
| 3761 |
pcur.close(); |
| 3762 |
|
| 3763 |
mem_heap_free(heap); |
| 3764 |
return true; |
| 3765 |
} |
| 3766 |
|
| 3767 |
/** Callback function to read a single ulint column. |
| 3768 |
return always returns true */ |
| 3769 |
static bool fts_read_ulint(void *row, /*!< in: sel_node_t* */ |
| 3770 |
void *user_arg) /*!< in: pointer to ulint */ |
| 3771 |
{ |
| 3772 |
sel_node_t *sel_node = static_cast<sel_node_t *>(row); |
| 3773 |
ulint *value = static_cast<ulint *>(user_arg); |
| 3774 |
que_node_t *exp = sel_node->select_list; |
| 3775 |
dfield_t *dfield = que_node_get_val(exp); |
| 3776 |
void *data = dfield_get_data(dfield); |
| 3777 |
|
| 3778 |
*value = |
| 3779 |
static_cast<ulint>(mach_read_from_4(static_cast<const byte *>(data))); |
| 3780 |
|
| 3781 |
return true; |
| 3782 |
} |
| 3783 |
|
| 3784 |
/** Get maximum Doc ID in a table if index "FTS_DOC_ID_INDEX" exists |
| 3785 |
@return max Doc ID or 0 if index "FTS_DOC_ID_INDEX" does not exist */ |
| 3786 |
doc_id_t fts_get_max_doc_id(dict_table_t *table) /*!< in: user table */ |
| 3787 |
{ |
| 3788 |
dict_index_t *index; |
| 3789 |
dict_field_t *dfield [[maybe_unused]] = nullptr; |
| 3790 |
doc_id_t doc_id = 0; |
| 3791 |
mtr_t mtr; |
| 3792 |
btr_pcur_t pcur; |
| 3793 |
|
| 3794 |
index = table->fts_doc_id_index; |
| 3795 |
|
| 3796 |
if (!index) { |
| 3797 |
return (0); |
| 3798 |
} |
| 3799 |
|
| 3800 |
dfield = index->get_field(0); |
| 3801 |
|
| 3802 |
#if 0 /* This can fail when renaming a column to FTS_DOC_ID_COL_NAME. */ |
| 3803 |
ut_ad(innobase_strcasecmp(FTS_DOC_ID_COL_NAME, dfield->name) == 0); |
| 3804 |
#endif |
| 3805 |
|
| 3806 |
mtr_start(&mtr); |
| 3807 |
|
| 3808 |
/* fetch the largest indexes value */ |
| 3809 |
pcur.open_at_side(false, index, BTR_SEARCH_LEAF, true, 0, &mtr); |
| 3810 |
|
| 3811 |
if (!page_is_empty(pcur.get_page())) { |
| 3812 |
const rec_t *rec = nullptr; |
| 3813 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 3814 |
ulint *offsets = offsets_; |
| 3815 |
mem_heap_t *heap = nullptr; |
| 3816 |
ulint len; |
| 3817 |
const void *data; |
| 3818 |
|
| 3819 |
rec_offs_init(offsets_); |
| 3820 |
|
| 3821 |
do { |
| 3822 |
rec = pcur.get_rec(); |
| 3823 |
|
| 3824 |
if (page_rec_is_user_rec(rec)) { |
| 3825 |
break; |
| 3826 |
} |
| 3827 |
} while (pcur.move_to_prev(&mtr)); |
| 3828 |
|
| 3829 |
if (!rec) { |
| 3830 |
goto func_exit; |
| 3831 |
} |
| 3832 |
|
| 3833 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 3834 |
UT_LOCATION_HERE, &heap); |
| 3835 |
|
| 3836 |
data = rec_get_nth_field(nullptr, rec, offsets, 0, &len); |
| 3837 |
|
| 3838 |
doc_id = |
| 3839 |
static_cast<doc_id_t>(fts_read_doc_id(static_cast<const byte *>(data))); |
| 3840 |
} |
| 3841 |
|
| 3842 |
func_exit: |
| 3843 |
pcur.close(); |
| 3844 |
mtr_commit(&mtr); |
| 3845 |
return (doc_id); |
| 3846 |
} |
| 3847 |
|
| 3848 |
/** Fetch document with the given document id. |
| 3849 |
@return DB_SUCCESS if OK else error */ |
| 3850 |
dberr_t fts_doc_fetch_by_doc_id( |
| 3851 |
fts_get_doc_t *get_doc, /*!< in: state */ |
| 3852 |
doc_id_t doc_id, /*!< in: id of document to |
| 3853 |
fetch */ |
| 3854 |
dict_index_t *index_to_use, /*!< in: caller supplied FTS index, |
| 3855 |
or NULL */ |
| 3856 |
ulint option, /*!< in: search option, if it is |
| 3857 |
greater than doc_id or equal */ |
| 3858 |
fts_sql_callback callback, /*!< in: callback to read */ |
| 3859 |
void *arg) /*!< in: callback arg */ |
| 3860 |
{ |
| 3861 |
pars_info_t *info; |
| 3862 |
dberr_t error; |
| 3863 |
const char *select_str; |
| 3864 |
doc_id_t write_doc_id; |
| 3865 |
dict_index_t *index; |
| 3866 |
trx_t *trx = trx_allocate_for_background(); |
| 3867 |
que_t *graph; |
| 3868 |
|
| 3869 |
trx->op_info = "fetching indexed FTS document"; |
| 3870 |
|
| 3871 |
/* The FTS index can be supplied by caller directly with |
| 3872 |
"index_to_use", otherwise, get it from "get_doc" */ |
| 3873 |
index = (index_to_use) ? index_to_use : get_doc->index_cache->index; |
| 3874 |
|
| 3875 |
if (get_doc && get_doc->get_document_graph) { |
| 3876 |
info = get_doc->get_document_graph->info; |
| 3877 |
} else { |
| 3878 |
info = pars_info_create(); |
| 3879 |
} |
| 3880 |
|
| 3881 |
/* Convert to "storage" byte order. */ |
| 3882 |
fts_write_doc_id((byte *)&write_doc_id, doc_id); |
| 3883 |
fts_bind_doc_id(info, "doc_id", &write_doc_id); |
| 3884 |
pars_info_bind_function(info, "my_func", callback, arg); |
| 3885 |
|
| 3886 |
select_str = fts_get_select_columns_str(index, info, info->heap); |
| 3887 |
pars_info_bind_id(info, true, "table_name", index->table_name); |
| 3888 |
|
| 3889 |
if (!get_doc || !get_doc->get_document_graph) { |
| 3890 |
if (option == FTS_FETCH_DOC_BY_ID_EQUAL) { |
| 3891 |
graph = fts_parse_sql(nullptr, info, |
| 3892 |
mem_heap_printf(info->heap, |
| 3893 |
"DECLARE FUNCTION my_func;\n" |
| 3894 |
"DECLARE CURSOR c IS" |
| 3895 |
" SELECT %s FROM $table_name" |
| 3896 |
" WHERE %s = :doc_id;\n" |
| 3897 |
"BEGIN\n" |
| 3898 |
"" |
| 3899 |
"OPEN c;\n" |
| 3900 |
"WHILE 1 = 1 LOOP\n" |
| 3901 |
" FETCH c INTO my_func();\n" |
| 3902 |
" IF c %% NOTFOUND THEN\n" |
| 3903 |
" EXIT;\n" |
| 3904 |
" END IF;\n" |
| 3905 |
"END LOOP;\n" |
| 3906 |
"CLOSE c;", |
| 3907 |
select_str, FTS_DOC_ID_COL_NAME)); |
| 3908 |
} else { |
| 3909 |
ut_ad(option == FTS_FETCH_DOC_BY_ID_LARGE); |
| 3910 |
|
| 3911 |
/* This is used for crash recovery of table with |
| 3912 |
hidden DOC ID or FTS indexes. We will scan the table |
| 3913 |
to re-processing user table rows whose DOC ID or |
| 3914 |
FTS indexed documents have not been sync-ed to disc |
| 3915 |
during recent crash. |
| 3916 |
In the case that all fulltext indexes are dropped |
| 3917 |
for a table, we will keep the "hidden" FTS_DOC_ID |
| 3918 |
column, and this scan is to retrieve the largest |
| 3919 |
DOC ID being used in the table to determine the |
| 3920 |
appropriate next DOC ID. |
| 3921 |
In the case of there exists fulltext index(es), this |
| 3922 |
operation will re-tokenize any docs that have not |
| 3923 |
been sync-ed to the disk, and re-prime the FTS |
| 3924 |
cached */ |
| 3925 |
graph = fts_parse_sql(nullptr, info, |
| 3926 |
mem_heap_printf(info->heap, |
| 3927 |
"DECLARE FUNCTION my_func;\n" |
| 3928 |
"DECLARE CURSOR c IS" |
| 3929 |
" SELECT %s, %s FROM $table_name" |
| 3930 |
" WHERE %s > :doc_id;\n" |
| 3931 |
"BEGIN\n" |
| 3932 |
"" |
| 3933 |
"OPEN c;\n" |
| 3934 |
"WHILE 1 = 1 LOOP\n" |
| 3935 |
" FETCH c INTO my_func();\n" |
| 3936 |
" IF c %% NOTFOUND THEN\n" |
| 3937 |
" EXIT;\n" |
| 3938 |
" END IF;\n" |
| 3939 |
"END LOOP;\n" |
| 3940 |
"CLOSE c;", |
| 3941 |
FTS_DOC_ID_COL_NAME, select_str, |
| 3942 |
FTS_DOC_ID_COL_NAME)); |
| 3943 |
} |
| 3944 |
if (get_doc) { |
| 3945 |
get_doc->get_document_graph = graph; |
| 3946 |
} |
| 3947 |
} else { |
| 3948 |
graph = get_doc->get_document_graph; |
| 3949 |
} |
| 3950 |
|
| 3951 |
error = fts_eval_sql(trx, graph); |
| 3952 |
|
| 3953 |
if (error == DB_SUCCESS) { |
| 3954 |
fts_sql_commit(trx); |
| 3955 |
} else { |
| 3956 |
fts_sql_rollback(trx); |
| 3957 |
} |
| 3958 |
|
| 3959 |
trx_free_for_background(trx); |
| 3960 |
|
| 3961 |
if (!get_doc) { |
| 3962 |
fts_que_graph_free(graph); |
| 3963 |
} |
| 3964 |
|
| 3965 |
return (error); |
| 3966 |
} |
| 3967 |
|
| 3968 |
/** Write out a single word's data as new entry/entries in the INDEX table. |
| 3969 |
@return DB_SUCCESS if all OK. */ |
| 3970 |
dberr_t fts_write_node(trx_t *trx, /*!< in: transaction */ |
| 3971 |
que_t **graph, /*!< in: query graph */ |
| 3972 |
fts_table_t *fts_table, /*!< in: aux table */ |
| 3973 |
fts_string_t *word, /*!< in: word in UTF-8 */ |
| 3974 |
fts_node_t *node) /*!< in: node columns */ |
| 3975 |
{ |
| 3976 |
pars_info_t *info; |
| 3977 |
dberr_t error; |
| 3978 |
uint32_t doc_count; |
| 3979 |
doc_id_t last_doc_id; |
| 3980 |
doc_id_t first_doc_id; |
| 3981 |
char table_name[MAX_FULL_NAME_LEN]; |
| 3982 |
|
| 3983 |
ut_a(node->ilist != nullptr); |
| 3984 |
|
| 3985 |
if (*graph) { |
| 3986 |
info = (*graph)->info; |
| 3987 |
} else { |
| 3988 |
info = pars_info_create(); |
| 3989 |
|
| 3990 |
fts_get_table_name(fts_table, table_name); |
| 3991 |
pars_info_bind_id(info, true, "index_table_name", table_name); |
| 3992 |
} |
| 3993 |
|
| 3994 |
pars_info_bind_varchar_literal(info, "token", word->f_str, word->f_len); |
| 3995 |
|
| 3996 |
/* Convert to "storage" byte order. */ |
| 3997 |
fts_write_doc_id((byte *)&first_doc_id, node->first_doc_id); |
| 3998 |
fts_bind_doc_id(info, "first_doc_id", &first_doc_id); |
| 3999 |
|
| 4000 |
/* Convert to "storage" byte order. */ |
| 4001 |
fts_write_doc_id((byte *)&last_doc_id, node->last_doc_id); |
| 4002 |
fts_bind_doc_id(info, "last_doc_id", &last_doc_id); |
| 4003 |
|
| 4004 |
ut_a(node->last_doc_id >= node->first_doc_id); |
| 4005 |
|
| 4006 |
/* Convert to "storage" byte order. */ |
| 4007 |
mach_write_to_4((byte *)&doc_count, node->doc_count); |
| 4008 |
pars_info_bind_int4_literal(info, "doc_count", (const uint32_t *)&doc_count); |
| 4009 |
|
| 4010 |
/* Set copy_name to false since it's a static. */ |
| 4011 |
pars_info_bind_literal(info, "ilist", node->ilist, node->ilist_size, |
| 4012 |
DATA_BLOB, DATA_BINARY_TYPE); |
| 4013 |
|
| 4014 |
if (!*graph) { |
| 4015 |
*graph = fts_parse_sql(fts_table, info, |
| 4016 |
"BEGIN\n" |
| 4017 |
"INSERT INTO $index_table_name VALUES" |
| 4018 |
" (:token, :first_doc_id," |
| 4019 |
" :last_doc_id, :doc_count, :ilist);"); |
| 4020 |
} |
| 4021 |
|
| 4022 |
const auto start_time = std::chrono::steady_clock::now(); |
| 4023 |
error = fts_eval_sql(trx, *graph); |
| 4024 |
elapsed_time += std::chrono::steady_clock::now() - start_time; |
| 4025 |
++n_nodes; |
| 4026 |
|
| 4027 |
return (error); |
| 4028 |
} |
| 4029 |
|
| 4030 |
/** Add rows to the DELETED_CACHE table. |
| 4031 |
@return DB_SUCCESS if all went well else error code*/ |
| 4032 |
[[nodiscard]] static dberr_t fts_sync_add_deleted_cache( |
| 4033 |
fts_sync_t *sync, /*!< in: sync state */ |
| 4034 |
ib_vector_t *doc_ids) /*!< in: doc ids to add */ |
| 4035 |
{ |
| 4036 |
ulint i; |
| 4037 |
pars_info_t *info; |
| 4038 |
que_t *graph; |
| 4039 |
fts_table_t fts_table; |
| 4040 |
char table_name[MAX_FULL_NAME_LEN]; |
| 4041 |
doc_id_t dummy = 0; |
| 4042 |
dberr_t error = DB_SUCCESS; |
| 4043 |
ulint n_elems = ib_vector_size(doc_ids); |
| 4044 |
|
| 4045 |
ut_a(ib_vector_size(doc_ids) > 0); |
| 4046 |
|
| 4047 |
ib_vector_sort(doc_ids, fts_doc_id_field_cmp<fts_update_t>); |
| 4048 |
|
| 4049 |
info = pars_info_create(); |
| 4050 |
|
| 4051 |
fts_bind_doc_id(info, "doc_id", &dummy); |
| 4052 |
|
| 4053 |
FTS_INIT_FTS_TABLE(&fts_table, FTS_SUFFIX_DELETED_CACHE, FTS_COMMON_TABLE, |
| 4054 |
sync->table); |
| 4055 |
|
| 4056 |
fts_get_table_name(&fts_table, table_name); |
| 4057 |
pars_info_bind_id(info, true, "table_name", table_name); |
| 4058 |
|
| 4059 |
graph = fts_parse_sql(&fts_table, info, |
| 4060 |
"BEGIN INSERT INTO $table_name VALUES (:doc_id);"); |
| 4061 |
|
| 4062 |
for (i = 0; i < n_elems && error == DB_SUCCESS; ++i) { |
| 4063 |
fts_update_t *update; |
| 4064 |
doc_id_t write_doc_id; |
| 4065 |
|
| 4066 |
update = static_cast<fts_update_t *>(ib_vector_get(doc_ids, i)); |
| 4067 |
|
| 4068 |
/* Convert to "storage" byte order. */ |
| 4069 |
fts_write_doc_id((byte *)&write_doc_id, update->doc_id); |
| 4070 |
fts_bind_doc_id(info, "doc_id", &write_doc_id); |
| 4071 |
|
| 4072 |
error = fts_eval_sql(sync->trx, graph); |
| 4073 |
} |
| 4074 |
|
| 4075 |
fts_que_graph_free(graph); |
| 4076 |
|
| 4077 |
return (error); |
| 4078 |
} |
| 4079 |
|
| 4080 |
/** Write the words and ilist to disk. |
| 4081 |
@param[in,out] trx transaction |
| 4082 |
@param[in] index_cache index cache |
| 4083 |
@param[in] unlock_cache whether unlock cache when write node |
| 4084 |
@param[in] sync_start_time Holds the timestamp of start of sync |
| 4085 |
for deducing the length of sync time |
| 4086 |
@return DB_SUCCESS if all went well else error code */ |
| 4087 |
[[nodiscard]] static MY_ATTRIBUTE((nonnull)) dberr_t fts_sync_write_words( |
| 4088 |
trx_t *trx, fts_index_cache_t *index_cache, bool unlock_cache, |
| 4089 |
std::chrono::steady_clock::time_point sync_start_time) { |
| 4090 |
fts_table_t fts_table; |
| 4091 |
ulint n_nodes = 0; |
| 4092 |
ulint n_words = 0; |
| 4093 |
const ib_rbt_node_t *rbt_node; |
| 4094 |
dberr_t error = DB_SUCCESS; |
| 4095 |
bool print_error = false; |
| 4096 |
dict_table_t *table = index_cache->index->table; |
| 4097 |
const float cutoff = 0.98f; |
| 4098 |
auto lock_threshold = get_srv_fatal_semaphore_wait_threshold() * cutoff; |
| 4099 |
|
| 4100 |
bool timeout_extended = false; |
| 4101 |
|
| 4102 |
FTS_INIT_INDEX_TABLE(&fts_table, nullptr, FTS_INDEX_TABLE, |
| 4103 |
index_cache->index); |
| 4104 |
|
| 4105 |
n_words = rbt_size(index_cache->words); |
| 4106 |
|
| 4107 |
/* We iterate over the entire tree, even if there is an error, |
| 4108 |
since we want to free the memory used during caching. */ |
| 4109 |
for (rbt_node = rbt_first(index_cache->words); rbt_node; |
| 4110 |
rbt_node = rbt_next(index_cache->words, rbt_node)) { |
| 4111 |
ulint i; |
| 4112 |
ulint selected; |
| 4113 |
fts_tokenizer_word_t *word; |
| 4114 |
|
| 4115 |
word = rbt_value(fts_tokenizer_word_t, rbt_node); |
| 4116 |
|
| 4117 |
selected = fts_select_index(index_cache->charset, word->text.f_str, |
| 4118 |
word->text.f_len); |
| 4119 |
|
| 4120 |
fts_table.suffix = fts_get_suffix(selected); |
| 4121 |
|
| 4122 |
/* We iterate over all the nodes even if there was an error */ |
| 4123 |
for (i = 0; i < ib_vector_size(word->nodes); ++i) { |
| 4124 |
fts_node_t *fts_node = |
| 4125 |
static_cast<fts_node_t *>(ib_vector_get(word->nodes, i)); |
| 4126 |
|
| 4127 |
if (fts_node->synced) { |
| 4128 |
continue; |
| 4129 |
} else { |
| 4130 |
fts_node->synced = true; |
| 4131 |
} |
| 4132 |
|
| 4133 |
/*FIXME: we need to handle the error properly. */ |
| 4134 |
if (error == DB_SUCCESS) { |
| 4135 |
DBUG_EXECUTE_IF("fts_instrument_sync_write", |
| 4136 |
std::this_thread::sleep_for(std::chrono::seconds(10));); |
| 4137 |
if (!unlock_cache) { |
| 4138 |
const auto cache_lock_time = |
| 4139 |
std::chrono::steady_clock::now() - sync_start_time; |
| 4140 |
if (cache_lock_time > lock_threshold) { |
| 4141 |
if (!timeout_extended) { |
| 4142 |
srv_fatal_semaphore_wait_extend.fetch_add(1); |
| 4143 |
timeout_extended = true; |
| 4144 |
lock_threshold += std::chrono::hours{2}; |
| 4145 |
} else { |
| 4146 |
unlock_cache = true; |
| 4147 |
srv_fatal_semaphore_wait_extend.fetch_sub(1); |
| 4148 |
timeout_extended = false; |
| 4149 |
} |
| 4150 |
} |
| 4151 |
} |
| 4152 |
|
| 4153 |
if (unlock_cache) { |
| 4154 |
rw_lock_x_unlock(&table->fts->cache->lock); |
| 4155 |
} |
| 4156 |
|
| 4157 |
error = fts_write_node(trx, &index_cache->ins_graph[selected], |
| 4158 |
&fts_table, &word->text, fts_node); |
| 4159 |
|
| 4160 |
DBUG_EXECUTE_IF("fts_instrument_sync_write", |
| 4161 |
std::this_thread::sleep_for(std::chrono::seconds(10));); |
| 4162 |
|
| 4163 |
DEBUG_SYNC_C("fts_write_node"); |
| 4164 |
DBUG_EXECUTE_IF("fts_write_node_crash", DBUG_SUICIDE();); |
| 4165 |
|
| 4166 |
DBUG_EXECUTE_IF("fts_instrument_sync_sleep", |
| 4167 |
std::this_thread::sleep_for(std::chrono::seconds(1));); |
| 4168 |
|
| 4169 |
if (unlock_cache) { |
| 4170 |
rw_lock_x_lock(&table->fts->cache->lock, UT_LOCATION_HERE); |
| 4171 |
} |
| 4172 |
} |
| 4173 |
} |
| 4174 |
|
| 4175 |
n_nodes += ib_vector_size(word->nodes); |
| 4176 |
|
| 4177 |
if (error != DB_SUCCESS && !print_error) { |
| 4178 |
ib::error(ER_IB_MSG_473) << "(" << ut_strerr(error) |
| 4179 |
<< ") writing" |
| 4180 |
" word node to FTS auxiliary index table."; |
| 4181 |
print_error = true; |
| 4182 |
} |
| 4183 |
} |
| 4184 |
|
| 4185 |
if (fts_enable_diag_print) { |
| 4186 |
printf("Avg number of nodes: %lf\n", |
| 4187 |
(double)n_nodes / (double)(n_words > 1 ? n_words : 1)); |
| 4188 |
} |
| 4189 |
|
| 4190 |
if (timeout_extended) { |
| 4191 |
srv_fatal_semaphore_wait_extend.fetch_sub(1); |
| 4192 |
} |
| 4193 |
|
| 4194 |
return (error); |
| 4195 |
} |
| 4196 |
|
| 4197 |
/** Begin Sync, create transaction, acquire locks, etc. */ |
| 4198 |
static void fts_sync_begin(fts_sync_t *sync) /*!< in: sync state */ |
| 4199 |
{ |
| 4200 |
fts_cache_t *cache = sync->table->fts->cache; |
| 4201 |
|
| 4202 |
n_nodes = 0; |
| 4203 |
elapsed_time = std::chrono::seconds::zero(); |
| 4204 |
|
| 4205 |
sync->start_time = std::chrono::steady_clock::now(); |
| 4206 |
|
| 4207 |
sync->trx = trx_allocate_for_background(); |
| 4208 |
|
| 4209 |
if (fts_enable_diag_print) { |
| 4210 |
ib::info(ER_IB_MSG_474) |
| 4211 |
<< "FTS SYNC for table " << sync->table->name |
| 4212 |
<< ", deleted count: " << ib_vector_size(cache->deleted_doc_ids) |
| 4213 |
<< " size: " << cache->total_size << " bytes"; |
| 4214 |
} |
| 4215 |
} |
| 4216 |
|
| 4217 |
/** Run SYNC on the table, i.e., write out data from the index specific |
| 4218 |
cache to the FTS aux INDEX table and FTS aux doc id stats table. |
| 4219 |
@return DB_SUCCESS if all OK */ |
| 4220 |
[[nodiscard]] static dberr_t fts_sync_index( |
| 4221 |
fts_sync_t *sync, /*!< in: sync state */ |
| 4222 |
fts_index_cache_t *index_cache) /*!< in: index cache */ |
| 4223 |
{ |
| 4224 |
trx_t *trx = sync->trx; |
| 4225 |
|
| 4226 |
trx->op_info = "doing SYNC index"; |
| 4227 |
|
| 4228 |
if (fts_enable_diag_print) { |
| 4229 |
ib::info(ER_IB_MSG_475) << "SYNC words: " << rbt_size(index_cache->words); |
| 4230 |
} |
| 4231 |
|
| 4232 |
ut_ad(rbt_validate(index_cache->words)); |
| 4233 |
|
| 4234 |
return (fts_sync_write_words(trx, index_cache, sync->unlock_cache, |
| 4235 |
sync->start_time)); |
| 4236 |
} |
| 4237 |
|
| 4238 |
/** Check if index cache has been synced completely |
| 4239 |
@param[in,out] index_cache index cache |
| 4240 |
@return true if index is synced, otherwise false. */ |
| 4241 |
static bool fts_sync_index_check(fts_index_cache_t *index_cache) { |
| 4242 |
const ib_rbt_node_t *rbt_node; |
| 4243 |
|
| 4244 |
for (rbt_node = rbt_first(index_cache->words); rbt_node != nullptr; |
| 4245 |
rbt_node = rbt_next(index_cache->words, rbt_node)) { |
| 4246 |
fts_tokenizer_word_t *word; |
| 4247 |
word = rbt_value(fts_tokenizer_word_t, rbt_node); |
| 4248 |
|
| 4249 |
fts_node_t *fts_node; |
| 4250 |
fts_node = static_cast<fts_node_t *>(ib_vector_last(word->nodes)); |
| 4251 |
|
| 4252 |
if (!fts_node->synced) { |
| 4253 |
return (false); |
| 4254 |
} |
| 4255 |
} |
| 4256 |
|
| 4257 |
return (true); |
| 4258 |
} |
| 4259 |
|
| 4260 |
/** Reset synced flag in index cache when rollback |
| 4261 |
@param[in,out] index_cache index cache */ |
| 4262 |
static void fts_sync_index_reset(fts_index_cache_t *index_cache) { |
| 4263 |
const ib_rbt_node_t *rbt_node; |
| 4264 |
|
| 4265 |
for (rbt_node = rbt_first(index_cache->words); rbt_node != nullptr; |
| 4266 |
rbt_node = rbt_next(index_cache->words, rbt_node)) { |
| 4267 |
fts_tokenizer_word_t *word; |
| 4268 |
word = rbt_value(fts_tokenizer_word_t, rbt_node); |
| 4269 |
|
| 4270 |
fts_node_t *fts_node; |
| 4271 |
fts_node = static_cast<fts_node_t *>(ib_vector_last(word->nodes)); |
| 4272 |
|
| 4273 |
fts_node->synced = false; |
| 4274 |
} |
| 4275 |
} |
| 4276 |
|
| 4277 |
/** Commit the SYNC, change state of processed doc ids etc. |
| 4278 |
@param[in,out] sync sync state |
| 4279 |
@return DB_SUCCESS if all OK */ |
| 4280 |
[[nodiscard]] static dberr_t fts_sync_commit(fts_sync_t *sync) { |
| 4281 |
dberr_t error; |
| 4282 |
trx_t *trx = sync->trx; |
| 4283 |
fts_cache_t *cache = sync->table->fts->cache; |
| 4284 |
doc_id_t last_doc_id; |
| 4285 |
|
| 4286 |
trx->op_info = "doing SYNC commit"; |
| 4287 |
|
| 4288 |
/* After each Sync, update the CONFIG table about the max doc id |
| 4289 |
we just sync-ed to index table */ |
| 4290 |
error = fts_cmp_set_sync_doc_id(sync->table, sync->max_doc_id, false, |
| 4291 |
&last_doc_id, trx); |
| 4292 |
|
| 4293 |
/* Get the list of deleted documents that are either in the |
| 4294 |
cache or were headed there but were deleted before the add |
| 4295 |
thread got to them. */ |
| 4296 |
|
| 4297 |
if (error == DB_SUCCESS && ib_vector_size(cache->deleted_doc_ids) > 0) { |
| 4298 |
error = fts_sync_add_deleted_cache(sync, cache->deleted_doc_ids); |
| 4299 |
} |
| 4300 |
|
| 4301 |
/* We need to do this within the deleted lock since fts_delete() can |
| 4302 |
attempt to add a deleted doc id to the cache deleted id array. */ |
| 4303 |
fts_cache_clear(cache); |
| 4304 |
DEBUG_SYNC_C("fts_deleted_doc_ids_clear"); |
| 4305 |
fts_cache_init(cache); |
| 4306 |
rw_lock_x_unlock(&cache->lock); |
| 4307 |
|
| 4308 |
if (error == DB_SUCCESS) { |
| 4309 |
DBUG_EXECUTE_IF("fts_crash_before_commit_sync", { DBUG_SUICIDE(); }); |
| 4310 |
fts_sql_commit(trx); |
| 4311 |
|
| 4312 |
} else if (error != DB_SUCCESS) { |
| 4313 |
fts_sql_rollback(trx); |
| 4314 |
|
| 4315 |
ib::error(ER_IB_MSG_476) << "(" << ut_strerr(error) << ") during SYNC."; |
| 4316 |
} |
| 4317 |
|
| 4318 |
if (fts_enable_diag_print && elapsed_time != std::chrono::seconds::zero()) { |
| 4319 |
ib::info(ER_IB_MSG_477) |
| 4320 |
<< "SYNC for table " << sync->table->name << ": SYNC time: " |
| 4321 |
<< std::chrono::duration_cast<std::chrono::seconds>( |
| 4322 |
std::chrono::steady_clock::now() - sync->start_time) |
| 4323 |
.count() |
| 4324 |
<< " secs: elapsed " |
| 4325 |
<< n_nodes / std::chrono::duration_cast<std::chrono::duration<double>>( |
| 4326 |
elapsed_time) |
| 4327 |
.count() |
| 4328 |
<< " ins/sec"; |
| 4329 |
} |
| 4330 |
|
| 4331 |
/* Avoid assertion in trx_free(). */ |
| 4332 |
trx->dict_operation_lock_mode = 0; |
| 4333 |
trx_free_for_background(trx); |
| 4334 |
|
| 4335 |
return (error); |
| 4336 |
} |
| 4337 |
|
| 4338 |
/** Rollback a sync operation */ |
| 4339 |
static void fts_sync_rollback(fts_sync_t *sync) /*!< in: sync state */ |
| 4340 |
{ |
| 4341 |
trx_t *trx = sync->trx; |
| 4342 |
fts_cache_t *cache = sync->table->fts->cache; |
| 4343 |
|
| 4344 |
for (ulint i = 0; i < ib_vector_size(cache->indexes); ++i) { |
| 4345 |
ulint j; |
| 4346 |
fts_index_cache_t *index_cache; |
| 4347 |
|
| 4348 |
index_cache = |
| 4349 |
static_cast<fts_index_cache_t *>(ib_vector_get(cache->indexes, i)); |
| 4350 |
|
| 4351 |
/* Reset synced flag so nodes will not be skipped |
| 4352 |
in the next sync, see fts_sync_write_words(). */ |
| 4353 |
fts_sync_index_reset(index_cache); |
| 4354 |
|
| 4355 |
for (j = 0; fts_index_selector[j].value; ++j) { |
| 4356 |
if (index_cache->ins_graph[j] != nullptr) { |
| 4357 |
que_graph_free(index_cache->ins_graph[j]); |
| 4358 |
|
| 4359 |
index_cache->ins_graph[j] = nullptr; |
| 4360 |
} |
| 4361 |
|
| 4362 |
if (index_cache->sel_graph[j] != nullptr) { |
| 4363 |
que_graph_free(index_cache->sel_graph[j]); |
| 4364 |
|
| 4365 |
index_cache->sel_graph[j] = nullptr; |
| 4366 |
} |
| 4367 |
} |
| 4368 |
} |
| 4369 |
|
| 4370 |
rw_lock_x_unlock(&cache->lock); |
| 4371 |
|
| 4372 |
fts_sql_rollback(trx); |
| 4373 |
|
| 4374 |
/* Avoid assertion in trx_free(). */ |
| 4375 |
trx->dict_operation_lock_mode = 0; |
| 4376 |
trx_free_for_background(trx); |
| 4377 |
} |
| 4378 |
|
| 4379 |
/** Run SYNC on the table, i.e., write out data from the cache to the |
| 4380 |
FTS auxiliary INDEX table and clear the cache at the end. |
| 4381 |
@param[in,out] sync sync state |
| 4382 |
@param[in] unlock_cache whether unlock cache lock when write node |
| 4383 |
@param[in] wait whether wait when a sync is in progress |
| 4384 |
@param[in] has_dict whether has dict operation lock |
| 4385 |
@return DB_SUCCESS if all OK */ |
| 4386 |
static dberr_t fts_sync(fts_sync_t *sync, bool unlock_cache, bool wait, |
| 4387 |
bool has_dict) { |
| 4388 |
ulint i; |
| 4389 |
dberr_t error = DB_SUCCESS; |
| 4390 |
fts_cache_t *cache = sync->table->fts->cache; |
| 4391 |
|
| 4392 |
rw_lock_x_lock(&cache->lock, UT_LOCATION_HERE); |
| 4393 |
|
| 4394 |
/* Check if cache is being synced. |
| 4395 |
Note: we release cache lock in fts_sync_write_words() to |
| 4396 |
avoid long wait for the lock by other threads. */ |
| 4397 |
while (sync->in_progress) { |
| 4398 |
rw_lock_x_unlock(&cache->lock); |
| 4399 |
|
| 4400 |
if (wait) { |
| 4401 |
os_event_wait(sync->event); |
| 4402 |
} else { |
| 4403 |
return (DB_SUCCESS); |
| 4404 |
} |
| 4405 |
|
| 4406 |
rw_lock_x_lock(&cache->lock, UT_LOCATION_HERE); |
| 4407 |
} |
| 4408 |
sync->unlock_cache = unlock_cache; |
| 4409 |
sync->in_progress = true; |
| 4410 |
|
| 4411 |
DEBUG_SYNC_C("fts_sync_begin"); |
| 4412 |
fts_sync_begin(sync); |
| 4413 |
|
| 4414 |
/* When sync in background, we hold dict operation lock |
| 4415 |
to prevent DDL like DROP INDEX, etc. */ |
| 4416 |
if (has_dict) { |
| 4417 |
sync->trx->dict_operation_lock_mode = RW_S_LATCH; |
| 4418 |
} |
| 4419 |
|
| 4420 |
begin_sync: |
| 4421 |
if (cache->total_size > fts_max_cache_size) { |
| 4422 |
/* Avoid the case: sync never finish when |
| 4423 |
insert/update keeps coming. */ |
| 4424 |
ut_ad(sync->unlock_cache); |
| 4425 |
sync->unlock_cache = false; |
| 4426 |
} |
| 4427 |
|
| 4428 |
DEBUG_SYNC_C("fts_instrument_sync1"); |
| 4429 |
for (i = 0; i < ib_vector_size(cache->indexes); ++i) { |
| 4430 |
fts_index_cache_t *index_cache; |
| 4431 |
|
| 4432 |
index_cache = |
| 4433 |
static_cast<fts_index_cache_t *>(ib_vector_get(cache->indexes, i)); |
| 4434 |
|
| 4435 |
error = fts_sync_index(sync, index_cache); |
| 4436 |
|
| 4437 |
if (error != DB_SUCCESS && !sync->interrupted) { |
| 4438 |
goto end_sync; |
| 4439 |
} |
| 4440 |
} |
| 4441 |
|
| 4442 |
DBUG_EXECUTE_IF("fts_instrument_sync_interrupted", sync->interrupted = true; |
| 4443 |
error = DB_INTERRUPTED; goto end_sync;); |
| 4444 |
|
| 4445 |
/* Make sure all the caches are synced. */ |
| 4446 |
for (i = 0; i < ib_vector_size(cache->indexes); ++i) { |
| 4447 |
fts_index_cache_t *index_cache; |
| 4448 |
|
| 4449 |
index_cache = |
| 4450 |
static_cast<fts_index_cache_t *>(ib_vector_get(cache->indexes, i)); |
| 4451 |
|
| 4452 |
if (index_cache->index->to_be_dropped || |
| 4453 |
fts_sync_index_check(index_cache)) { |
| 4454 |
continue; |
| 4455 |
} |
| 4456 |
|
| 4457 |
goto begin_sync; |
| 4458 |
} |
| 4459 |
|
| 4460 |
end_sync: |
| 4461 |
if (error == DB_SUCCESS && !sync->interrupted) { |
| 4462 |
error = fts_sync_commit(sync); |
| 4463 |
} else { |
| 4464 |
fts_sync_rollback(sync); |
| 4465 |
} |
| 4466 |
|
| 4467 |
rw_lock_x_lock(&cache->lock, UT_LOCATION_HERE); |
| 4468 |
sync->interrupted = false; |
| 4469 |
sync->in_progress = false; |
| 4470 |
os_event_set(sync->event); |
| 4471 |
rw_lock_x_unlock(&cache->lock); |
| 4472 |
|
| 4473 |
/* We need to check whether an optimize is required, for that |
| 4474 |
we make copies of the two variables that control the trigger. These |
| 4475 |
variables can change behind our back and we don't want to hold the |
| 4476 |
lock for longer than is needed. */ |
| 4477 |
mutex_enter(&cache->deleted_lock); |
| 4478 |
|
| 4479 |
cache->added = 0; |
| 4480 |
cache->deleted = 0; |
| 4481 |
|
| 4482 |
mutex_exit(&cache->deleted_lock); |
| 4483 |
|
| 4484 |
return (error); |
| 4485 |
} |
| 4486 |
|
| 4487 |
/** Run SYNC on the table, i.e., write out data from the cache to the |
| 4488 |
FTS auxiliary INDEX table and clear the cache at the end. |
| 4489 |
@param[in,out] table fts table |
| 4490 |
@param[in] unlock_cache whether unlock cache when write node |
| 4491 |
@param[in] wait whether wait for existing sync to finish |
| 4492 |
@param[in] has_dict whether has dict operation lock |
| 4493 |
@return DB_SUCCESS on success, error code on failure. */ |
| 4494 |
dberr_t fts_sync_table(dict_table_t *table, bool unlock_cache, bool wait, |
| 4495 |
bool has_dict) { |
| 4496 |
dberr_t err = DB_SUCCESS; |
| 4497 |
|
| 4498 |
ut_ad(table->fts); |
| 4499 |
|
| 4500 |
if (!dict_table_is_discarded(table) && table->fts->cache && |
| 4501 |
!table->is_corrupted()) { |
| 4502 |
err = fts_sync(table->fts->cache->sync, unlock_cache, wait, has_dict); |
| 4503 |
} |
| 4504 |
|
| 4505 |
return (err); |
| 4506 |
} |
| 4507 |
|
| 4508 |
/** Check fts token |
| 4509 |
1. for ngram token, check whether the token contains any words in stopwords |
| 4510 |
2. for non-ngram token, check if it's stopword or less than fts_min_token_size |
| 4511 |
or greater than fts_max_token_size. |
| 4512 |
@param[in] token token string |
| 4513 |
@param[in] stopwords stopwords rb tree |
| 4514 |
@param[in] is_ngram is ngram parser |
| 4515 |
@param[in] cs token charset |
| 4516 |
@retval true if it is not stopword and length in range |
| 4517 |
@retval false if it is stopword or length not in range */ |
| 4518 |
bool fts_check_token(const fts_string_t *token, const ib_rbt_t *stopwords, |
| 4519 |
bool is_ngram, const CHARSET_INFO *cs) { |
| 4520 |
ut_ad(cs != nullptr || stopwords == nullptr); |
| 4521 |
|
| 4522 |
if (!is_ngram) { |
| 4523 |
ib_rbt_bound_t parent; |
| 4524 |
|
| 4525 |
if (token->f_n_char < fts_min_token_size || |
| 4526 |
token->f_n_char > fts_max_token_size || |
| 4527 |
(stopwords != nullptr && rbt_search(stopwords, &parent, token) == 0)) { |
| 4528 |
return (false); |
| 4529 |
} else { |
| 4530 |
return (true); |
| 4531 |
} |
| 4532 |
} |
| 4533 |
|
| 4534 |
/* Check token for ngram. */ |
| 4535 |
DBUG_EXECUTE_IF("fts_instrument_ignore_ngram_check", return (true);); |
| 4536 |
|
| 4537 |
/* We ignore fts_min_token_size when ngram */ |
| 4538 |
ut_ad(token->f_n_char > 0 && token->f_n_char <= fts_max_token_size); |
| 4539 |
|
| 4540 |
if (stopwords == nullptr) { |
| 4541 |
return (true); |
| 4542 |
} |
| 4543 |
|
| 4544 |
/*Ngram checks whether the token contains any words in stopwords. |
| 4545 |
We can't simply use CONTAIN to search in stopwords, because it's |
| 4546 |
built on COMPARE. So we need to tokenize the token into words |
| 4547 |
from unigram to f_n_char, and check them separately. */ |
| 4548 |
for (ulint ngram_token_size = 1; ngram_token_size <= token->f_n_char; |
| 4549 |
ngram_token_size++) { |
| 4550 |
const char *start; |
| 4551 |
const char *next; |
| 4552 |
const char *end; |
| 4553 |
ulint char_len; |
| 4554 |
ulint n_chars; |
| 4555 |
|
| 4556 |
start = reinterpret_cast<char *>(token->f_str); |
| 4557 |
next = start; |
| 4558 |
end = start + token->f_len; |
| 4559 |
n_chars = 0; |
| 4560 |
|
| 4561 |
while (next < end) { |
| 4562 |
char_len = my_mbcharlen_ptr(cs, next, end); |
| 4563 |
|
| 4564 |
if (next + char_len > end || char_len == 0) { |
| 4565 |
break; |
| 4566 |
} else { |
| 4567 |
/* Skip SPACE */ |
| 4568 |
if (char_len == 1 && *next == ' ') { |
| 4569 |
start = next + 1; |
| 4570 |
next = start; |
| 4571 |
n_chars = 0; |
| 4572 |
|
| 4573 |
continue; |
| 4574 |
} |
| 4575 |
|
| 4576 |
next += char_len; |
| 4577 |
n_chars++; |
| 4578 |
} |
| 4579 |
|
| 4580 |
if (n_chars == ngram_token_size) { |
| 4581 |
fts_string_t ngram_token; |
| 4582 |
ngram_token.f_str = reinterpret_cast<byte *>(const_cast<char *>(start)); |
| 4583 |
ngram_token.f_len = next - start; |
| 4584 |
ngram_token.f_n_char = ngram_token_size; |
| 4585 |
|
| 4586 |
ib_rbt_bound_t parent; |
| 4587 |
if (rbt_search(stopwords, &parent, &ngram_token) == 0) { |
| 4588 |
return (false); |
| 4589 |
} |
| 4590 |
|
| 4591 |
/* Move a char forward */ |
| 4592 |
start += my_mbcharlen_ptr(cs, start, end); |
| 4593 |
n_chars = ngram_token_size - 1; |
| 4594 |
} |
| 4595 |
} |
| 4596 |
} |
| 4597 |
|
| 4598 |
return (true); |
| 4599 |
} |
| 4600 |
|
| 4601 |
/** Add the token and its start position to the token's list of positions. |
| 4602 |
@param[in,out] result_doc result doc rb tree |
| 4603 |
@param[in] str token string |
| 4604 |
@param[in] position token position */ |
| 4605 |
static void fts_add_token(fts_doc_t *result_doc, fts_string_t str, |
| 4606 |
ulint position) { |
| 4607 |
/* Ignore string whose character number is less than |
| 4608 |
"fts_min_token_size" or more than "fts_max_token_size" */ |
| 4609 |
|
| 4610 |
if (fts_check_token(&str, nullptr, result_doc->is_ngram, |
| 4611 |
result_doc->charset)) { |
| 4612 |
mem_heap_t *heap; |
| 4613 |
fts_string_t t_str; |
| 4614 |
fts_token_t *token; |
| 4615 |
ib_rbt_bound_t parent; |
| 4616 |
ulint newlen; |
| 4617 |
|
| 4618 |
heap = static_cast<mem_heap_t *>(result_doc->self_heap->arg); |
| 4619 |
|
| 4620 |
t_str.f_n_char = str.f_n_char; |
| 4621 |
|
| 4622 |
t_str.f_len = str.f_len * result_doc->charset->casedn_multiply + 1; |
| 4623 |
|
| 4624 |
t_str.f_str = static_cast<byte *>(mem_heap_alloc(heap, t_str.f_len)); |
| 4625 |
|
| 4626 |
/* For binary collations, a case sensitive search is |
| 4627 |
performed. Hence don't convert to lower case. */ |
| 4628 |
if (my_binary_compare(result_doc->charset)) { |
| 4629 |
memcpy(t_str.f_str, str.f_str, str.f_len); |
| 4630 |
t_str.f_str[str.f_len] = 0; |
| 4631 |
newlen = str.f_len; |
| 4632 |
} else { |
| 4633 |
newlen = |
| 4634 |
innobase_fts_casedn_str(result_doc->charset, (char *)str.f_str, |
| 4635 |
str.f_len, (char *)t_str.f_str, t_str.f_len); |
| 4636 |
} |
| 4637 |
|
| 4638 |
t_str.f_len = newlen; |
| 4639 |
t_str.f_str[newlen] = 0; |
| 4640 |
|
| 4641 |
/* Add the word to the document statistics. If the word |
| 4642 |
hasn't been seen before we create a new entry for it. */ |
| 4643 |
if (rbt_search(result_doc->tokens, &parent, &t_str) != 0) { |
| 4644 |
fts_token_t new_token; |
| 4645 |
|
| 4646 |
new_token.text.f_len = newlen; |
| 4647 |
new_token.text.f_str = t_str.f_str; |
| 4648 |
new_token.text.f_n_char = t_str.f_n_char; |
| 4649 |
|
| 4650 |
new_token.positions = |
| 4651 |
ib_vector_create(result_doc->self_heap, sizeof(ulint), 32); |
| 4652 |
|
| 4653 |
parent.last = rbt_add_node(result_doc->tokens, &parent, &new_token); |
| 4654 |
|
| 4655 |
ut_ad(rbt_validate(result_doc->tokens)); |
| 4656 |
} |
| 4657 |
|
| 4658 |
token = rbt_value(fts_token_t, parent.last); |
| 4659 |
ib_vector_push(token->positions, &position); |
| 4660 |
} |
| 4661 |
} |
| 4662 |
|
| 4663 |
/** Process next token from document starting at the given position, i.e., add |
| 4664 |
the token's start position to the token's list of positions. |
| 4665 |
@param[in,out] doc document to tokenize |
| 4666 |
@param[out] result if provided, save result here |
| 4667 |
@param[in] start_pos start position in text |
| 4668 |
@param[in] add_pos add this position to all tokens from this |
| 4669 |
tokenization |
| 4670 |
@return number of characters handled in this call */ |
| 4671 |
static ulint fts_process_token(fts_doc_t *doc, fts_doc_t *result, |
| 4672 |
ulint start_pos, ulint add_pos) { |
| 4673 |
ulint ret; |
| 4674 |
fts_string_t str; |
| 4675 |
ulint position; |
| 4676 |
fts_doc_t *result_doc; |
| 4677 |
byte buf[FTS_MAX_WORD_LEN + 1]; |
| 4678 |
|
| 4679 |
str.f_str = buf; |
| 4680 |
|
| 4681 |
/* Determine where to save the result. */ |
| 4682 |
result_doc = (result != nullptr) ? result : doc; |
| 4683 |
|
| 4684 |
/* The length of a string in characters is set here only. */ |
| 4685 |
|
| 4686 |
ret = innobase_mysql_fts_get_token(doc->charset, doc->text.f_str + start_pos, |
| 4687 |
doc->text.f_str + doc->text.f_len, &str); |
| 4688 |
|
| 4689 |
position = start_pos + ret - str.f_len + add_pos; |
| 4690 |
|
| 4691 |
fts_add_token(result_doc, str, position); |
| 4692 |
|
| 4693 |
return (ret); |
| 4694 |
} |
| 4695 |
|
| 4696 |
/** Get token char size by charset |
| 4697 |
@return token size */ |
| 4698 |
ulint fts_get_token_size(const CHARSET_INFO *cs, /*!< in: Character set */ |
| 4699 |
const char *token, /*!< in: token */ |
| 4700 |
ulint len) /*!< in: token length */ |
| 4701 |
{ |
| 4702 |
char *start; |
| 4703 |
char *end; |
| 4704 |
ulint size = 0; |
| 4705 |
|
| 4706 |
/* const_cast is for reinterpret_cast below, or it will fail. */ |
| 4707 |
start = const_cast<char *>(token); |
| 4708 |
end = start + len; |
| 4709 |
while (start < end) { |
| 4710 |
int ctype; |
| 4711 |
int mbl; |
| 4712 |
|
| 4713 |
mbl = cs->cset->ctype(cs, &ctype, reinterpret_cast<uchar *>(start), |
| 4714 |
reinterpret_cast<uchar *>(end)); |
| 4715 |
|
| 4716 |
size++; |
| 4717 |
|
| 4718 |
start += mbl > 0 ? mbl : (mbl < 0 ? -mbl : 1); |
| 4719 |
} |
| 4720 |
|
| 4721 |
return (size); |
| 4722 |
} |
| 4723 |
|
| 4724 |
/** FTS plugin parser 'myql_parser' callback function for document tokenize. |
| 4725 |
Refer to 'MYSQL_FTPARSER_PARAM' for more detail. |
| 4726 |
@return always returns 0 */ |
| 4727 |
int fts_tokenize_document_internal( |
| 4728 |
MYSQL_FTPARSER_PARAM *param, /*!< in: parser parameter */ |
| 4729 |
char *doc, /*!< in/out: document */ |
| 4730 |
int len) /*!< in: document length */ |
| 4731 |
{ |
| 4732 |
fts_string_t str; |
| 4733 |
byte buf[FTS_MAX_WORD_LEN + 1]; |
| 4734 |
MYSQL_FTPARSER_BOOLEAN_INFO bool_info = {FT_TOKEN_WORD, 0, 0, 0, 0, 0, |
| 4735 |
' ', nullptr}; |
| 4736 |
|
| 4737 |
ut_ad(len >= 0); |
| 4738 |
|
| 4739 |
str.f_str = buf; |
| 4740 |
|
| 4741 |
for (ulint i = 0, inc = 0; i < static_cast<ulint>(len); i += inc) { |
| 4742 |
inc = |
| 4743 |
innobase_mysql_fts_get_token(const_cast<CHARSET_INFO *>(param->cs), |
| 4744 |
reinterpret_cast<byte *>(doc) + i, |
| 4745 |
reinterpret_cast<byte *>(doc) + len, &str); |
| 4746 |
|
| 4747 |
if (str.f_len > 0) { |
| 4748 |
bool_info.position = static_cast<int>(i + inc - str.f_len); |
| 4749 |
ut_ad(bool_info.position >= 0); |
| 4750 |
|
| 4751 |
/* Stop when add word fails */ |
| 4752 |
if (param->mysql_add_word(param, reinterpret_cast<char *>(str.f_str), |
| 4753 |
static_cast<int>(str.f_len), &bool_info)) { |
| 4754 |
break; |
| 4755 |
} |
| 4756 |
} |
| 4757 |
} |
| 4758 |
|
| 4759 |
return (0); |
| 4760 |
} |
| 4761 |
|
| 4762 |
/** FTS plugin parser 'myql_add_word' callback function for document tokenize. |
| 4763 |
Refer to 'MYSQL_FTPARSER_PARAM' for more detail. |
| 4764 |
@return always returns 0 */ |
| 4765 |
static int fts_tokenize_add_word_for_parser( |
| 4766 |
MYSQL_FTPARSER_PARAM *param, /* in: parser parameter */ |
| 4767 |
char *word, /* in: token word */ |
| 4768 |
int word_len, /* in: word len */ |
| 4769 |
MYSQL_FTPARSER_BOOLEAN_INFO *boolean_info) /* in: word boolean info */ |
| 4770 |
{ |
| 4771 |
fts_string_t str; |
| 4772 |
fts_tokenize_param_t *fts_param; |
| 4773 |
fts_doc_t *result_doc; |
| 4774 |
ulint position; |
| 4775 |
|
| 4776 |
fts_param = static_cast<fts_tokenize_param_t *>(param->mysql_ftparam); |
| 4777 |
result_doc = fts_param->result_doc; |
| 4778 |
ut_ad(result_doc != nullptr); |
| 4779 |
|
| 4780 |
str.f_str = reinterpret_cast<byte *>(word); |
| 4781 |
str.f_len = word_len; |
| 4782 |
str.f_n_char = |
| 4783 |
fts_get_token_size(const_cast<CHARSET_INFO *>(param->cs), word, word_len); |
| 4784 |
|
| 4785 |
ut_ad(boolean_info->position >= 0); |
| 4786 |
position = boolean_info->position + fts_param->add_pos; |
| 4787 |
|
| 4788 |
fts_add_token(result_doc, str, position); |
| 4789 |
|
| 4790 |
return (0); |
| 4791 |
} |
| 4792 |
|
| 4793 |
/** Parse a document using an external / user supplied parser */ |
| 4794 |
static void fts_tokenize_by_parser( |
| 4795 |
fts_doc_t *doc, /* in/out: document to tokenize */ |
| 4796 |
st_mysql_ftparser *parser, /* in: plugin fts parser */ |
| 4797 |
fts_tokenize_param_t *fts_param) /* in: fts tokenize param */ |
| 4798 |
{ |
| 4799 |
MYSQL_FTPARSER_PARAM param; |
| 4800 |
|
| 4801 |
ut_a(parser); |
| 4802 |
|
| 4803 |
/* Set parameters for param */ |
| 4804 |
param.mysql_parse = fts_tokenize_document_internal; |
| 4805 |
param.mysql_add_word = fts_tokenize_add_word_for_parser; |
| 4806 |
param.mysql_ftparam = fts_param; |
| 4807 |
param.cs = doc->charset; |
| 4808 |
param.doc = reinterpret_cast<char *>(doc->text.f_str); |
| 4809 |
param.length = static_cast<int>(doc->text.f_len); |
| 4810 |
param.mode = MYSQL_FTPARSER_SIMPLE_MODE; |
| 4811 |
|
| 4812 |
PARSER_INIT(parser, ¶m); |
| 4813 |
parser->parse(¶m); |
| 4814 |
PARSER_DEINIT(parser, ¶m); |
| 4815 |
} |
| 4816 |
|
| 4817 |
/** Tokenize a document. |
| 4818 |
@param[in,out] doc document to tokenize |
| 4819 |
@param[out] result tokenization result |
| 4820 |
@param[in] parser pluggable parser */ |
| 4821 |
static void fts_tokenize_document(fts_doc_t *doc, fts_doc_t *result, |
| 4822 |
st_mysql_ftparser *parser) { |
| 4823 |
ut_a(!doc->tokens); |
| 4824 |
ut_a(doc->charset); |
| 4825 |
|
| 4826 |
doc->tokens = rbt_create_arg_cmp(sizeof(fts_token_t), innobase_fts_text_cmp, |
| 4827 |
doc->charset); |
| 4828 |
|
| 4829 |
if (parser != nullptr) { |
| 4830 |
fts_tokenize_param_t fts_param; |
| 4831 |
|
| 4832 |
fts_param.result_doc = (result != nullptr) ? result : doc; |
| 4833 |
fts_param.add_pos = 0; |
| 4834 |
|
| 4835 |
fts_tokenize_by_parser(doc, parser, &fts_param); |
| 4836 |
} else { |
| 4837 |
ulint inc; |
| 4838 |
|
| 4839 |
for (ulint i = 0; i < doc->text.f_len; i += inc) { |
| 4840 |
inc = fts_process_token(doc, result, i, 0); |
| 4841 |
ut_a(inc > 0); |
| 4842 |
} |
| 4843 |
} |
| 4844 |
} |
| 4845 |
|
| 4846 |
/** Continue to tokenize a document. |
| 4847 |
@param[in,out] doc document to tokenize |
| 4848 |
@param[in] add_pos add this position to all tokens from this tokenization |
| 4849 |
@param[out] result tokenization result |
| 4850 |
@param[in] parser pluggable parser */ |
| 4851 |
static void fts_tokenize_document_next(fts_doc_t *doc, ulint add_pos, |
| 4852 |
fts_doc_t *result, |
| 4853 |
st_mysql_ftparser *parser) { |
| 4854 |
ut_a(doc->tokens); |
| 4855 |
|
| 4856 |
if (parser) { |
| 4857 |
fts_tokenize_param_t fts_param; |
| 4858 |
|
| 4859 |
fts_param.result_doc = (result != nullptr) ? result : doc; |
| 4860 |
fts_param.add_pos = add_pos; |
| 4861 |
|
| 4862 |
fts_tokenize_by_parser(doc, parser, &fts_param); |
| 4863 |
} else { |
| 4864 |
ulint inc; |
| 4865 |
|
| 4866 |
for (ulint i = 0; i < doc->text.f_len; i += inc) { |
| 4867 |
inc = fts_process_token(doc, result, i, add_pos); |
| 4868 |
ut_a(inc > 0); |
| 4869 |
} |
| 4870 |
} |
| 4871 |
} |
| 4872 |
|
| 4873 |
namespace detail { |
| 4874 |
/** Create the vector of fts_get_doc_t instances. |
| 4875 |
@param[in,out] cache fts cache |
| 4876 |
@return vector of fts_get_doc_t instances */ |
| 4877 |
ib_vector_t *fts_get_docs_create(fts_cache_t *cache) { |
| 4878 |
ib_vector_t *get_docs; |
| 4879 |
|
| 4880 |
ut_ad(rw_lock_own(&cache->init_lock, RW_LOCK_X)); |
| 4881 |
|
| 4882 |
/* We need one instance of fts_get_doc_t per index. */ |
| 4883 |
get_docs = ib_vector_create(cache->self_heap, sizeof(fts_get_doc_t), 4); |
| 4884 |
|
| 4885 |
/* Create the get_doc instance, we need one of these |
| 4886 |
per FTS index. */ |
| 4887 |
for (ulint i = 0; i < ib_vector_size(cache->indexes); ++i) { |
| 4888 |
dict_index_t **index; |
| 4889 |
fts_get_doc_t *get_doc; |
| 4890 |
|
| 4891 |
index = static_cast<dict_index_t **>(ib_vector_get(cache->indexes, i)); |
| 4892 |
|
| 4893 |
get_doc = static_cast<fts_get_doc_t *>(ib_vector_push(get_docs, nullptr)); |
| 4894 |
|
| 4895 |
memset(get_doc, 0x0, sizeof(*get_doc)); |
| 4896 |
|
| 4897 |
get_doc->index_cache = fts_get_index_cache(cache, *index); |
| 4898 |
get_doc->cache = cache; |
| 4899 |
|
| 4900 |
/* Must find the index cache. */ |
| 4901 |
ut_a(get_doc->index_cache != nullptr); |
| 4902 |
} |
| 4903 |
|
| 4904 |
return (get_docs); |
| 4905 |
} |
| 4906 |
} // namespace detail |
| 4907 |
|
| 4908 |
/******************************************************************** |
| 4909 |
Release any resources held by the fts_get_doc_t instances. */ |
| 4910 |
static void fts_get_docs_clear( |
| 4911 |
ib_vector_t *get_docs) /*!< in: Doc retrieval vector */ |
| 4912 |
{ |
| 4913 |
ulint i; |
| 4914 |
|
| 4915 |
/* Release the get doc graphs if any. */ |
| 4916 |
for (i = 0; i < ib_vector_size(get_docs); ++i) { |
| 4917 |
fts_get_doc_t *get_doc = |
| 4918 |
static_cast<fts_get_doc_t *>(ib_vector_get(get_docs, i)); |
| 4919 |
|
| 4920 |
if (get_doc->get_document_graph != nullptr) { |
| 4921 |
ut_a(get_doc->index_cache); |
| 4922 |
|
| 4923 |
fts_que_graph_free(get_doc->get_document_graph); |
| 4924 |
get_doc->get_document_graph = nullptr; |
| 4925 |
} |
| 4926 |
} |
| 4927 |
} |
| 4928 |
|
| 4929 |
/** Get the initial Doc ID by consulting the CONFIG table |
| 4930 |
@return initial Doc ID */ |
| 4931 |
doc_id_t fts_init_doc_id(const dict_table_t *table) /*!< in: table */ |
| 4932 |
{ |
| 4933 |
doc_id_t max_doc_id = 0; |
| 4934 |
|
| 4935 |
rw_lock_x_lock(&table->fts->cache->lock, UT_LOCATION_HERE); |
| 4936 |
|
| 4937 |
/* Return if the table is already initialized for DOC ID */ |
| 4938 |
if (table->fts->cache->first_doc_id != FTS_NULL_DOC_ID) { |
| 4939 |
rw_lock_x_unlock(&table->fts->cache->lock); |
| 4940 |
return (0); |
| 4941 |
} |
| 4942 |
|
| 4943 |
DEBUG_SYNC_C("fts_initialize_doc_id"); |
| 4944 |
|
| 4945 |
/* Then compare this value with the ID value stored in the CONFIG |
| 4946 |
table. The larger one will be our new initial Doc ID */ |
| 4947 |
fts_cmp_set_sync_doc_id(table, 0, false, &max_doc_id); |
| 4948 |
|
| 4949 |
/* If DICT_TF2_FTS_ADD_DOC_ID is set, we are in the process of |
| 4950 |
creating index (and add doc id column. No need to recovery |
| 4951 |
documents */ |
| 4952 |
if (!DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_ADD_DOC_ID)) { |
| 4953 |
fts_init_index((dict_table_t *)table, true); |
| 4954 |
} |
| 4955 |
|
| 4956 |
table->fts->fts_status |= ADDED_TABLE_SYNCED; |
| 4957 |
|
| 4958 |
table->fts->cache->first_doc_id = max_doc_id; |
| 4959 |
|
| 4960 |
rw_lock_x_unlock(&table->fts->cache->lock); |
| 4961 |
|
| 4962 |
ut_ad(max_doc_id > 0); |
| 4963 |
|
| 4964 |
return (max_doc_id); |
| 4965 |
} |
| 4966 |
|
| 4967 |
#ifdef FTS_MULT_INDEX |
| 4968 |
/** Check if the index is in the affected set. |
| 4969 |
@return true if index is updated */ |
| 4970 |
static bool fts_is_index_updated( |
| 4971 |
const ib_vector_t *fts_indexes, /*!< in: affected FTS indexes */ |
| 4972 |
const fts_get_doc_t *get_doc) /*!< in: info for reading |
| 4973 |
document */ |
| 4974 |
{ |
| 4975 |
ulint i; |
| 4976 |
dict_index_t *index = get_doc->index_cache->index; |
| 4977 |
|
| 4978 |
for (i = 0; i < ib_vector_size(fts_indexes); ++i) { |
| 4979 |
const dict_index_t *updated_fts_index; |
| 4980 |
|
| 4981 |
updated_fts_index = |
| 4982 |
static_cast<const dict_index_t *>(ib_vector_getp_const(fts_indexes, i)); |
| 4983 |
|
| 4984 |
ut_a(updated_fts_index != NULL); |
| 4985 |
|
| 4986 |
if (updated_fts_index == index) { |
| 4987 |
return true; |
| 4988 |
} |
| 4989 |
} |
| 4990 |
|
| 4991 |
return false; |
| 4992 |
} |
| 4993 |
#endif |
| 4994 |
|
| 4995 |
/** Fetch COUNT(*) from specified table. |
| 4996 |
@return the number of rows in the table */ |
| 4997 |
ulint fts_get_rows_count(fts_table_t *fts_table) /*!< in: fts table to read */ |
| 4998 |
{ |
| 4999 |
trx_t *trx; |
| 5000 |
pars_info_t *info; |
| 5001 |
que_t *graph; |
| 5002 |
dberr_t error; |
| 5003 |
ulint count = 0; |
| 5004 |
char table_name[MAX_FULL_NAME_LEN]; |
| 5005 |
|
| 5006 |
trx = trx_allocate_for_background(); |
| 5007 |
|
| 5008 |
trx->op_info = "fetching FT table rows count"; |
| 5009 |
|
| 5010 |
info = pars_info_create(); |
| 5011 |
|
| 5012 |
pars_info_bind_function(info, "my_func", fts_read_ulint, &count); |
| 5013 |
|
| 5014 |
fts_get_table_name(fts_table, table_name); |
| 5015 |
pars_info_bind_id(info, true, "table_name", table_name); |
| 5016 |
|
| 5017 |
graph = fts_parse_sql(fts_table, info, |
| 5018 |
"DECLARE FUNCTION my_func;\n" |
| 5019 |
"DECLARE CURSOR c IS" |
| 5020 |
" SELECT COUNT(*)" |
| 5021 |
" FROM $table_name;\n" |
| 5022 |
"BEGIN\n" |
| 5023 |
"\n" |
| 5024 |
"OPEN c;\n" |
| 5025 |
"WHILE 1 = 1 LOOP\n" |
| 5026 |
" FETCH c INTO my_func();\n" |
| 5027 |
" IF c % NOTFOUND THEN\n" |
| 5028 |
" EXIT;\n" |
| 5029 |
" END IF;\n" |
| 5030 |
"END LOOP;\n" |
| 5031 |
"CLOSE c;"); |
| 5032 |
|
| 5033 |
for (;;) { |
| 5034 |
error = fts_eval_sql(trx, graph); |
| 5035 |
|
| 5036 |
if (error == DB_SUCCESS) { |
| 5037 |
fts_sql_commit(trx); |
| 5038 |
|
| 5039 |
break; /* Exit the loop. */ |
| 5040 |
} else { |
| 5041 |
fts_sql_rollback(trx); |
| 5042 |
|
| 5043 |
if (error == DB_LOCK_WAIT_TIMEOUT) { |
| 5044 |
ib::warn(ER_IB_MSG_478) << "lock wait timeout reading" |
| 5045 |
" FTS table. Retrying!"; |
| 5046 |
|
| 5047 |
trx->error_state = DB_SUCCESS; |
| 5048 |
} else { |
| 5049 |
ib::error(ER_IB_MSG_479) |
| 5050 |
<< "(" << ut_strerr(error) << ") while reading FTS table."; |
| 5051 |
|
| 5052 |
break; /* Exit the loop. */ |
| 5053 |
} |
| 5054 |
} |
| 5055 |
} |
| 5056 |
|
| 5057 |
fts_que_graph_free(graph); |
| 5058 |
|
| 5059 |
trx_free_for_background(trx); |
| 5060 |
|
| 5061 |
return (count); |
| 5062 |
} |
| 5063 |
|
| 5064 |
#ifdef FTS_CACHE_SIZE_DEBUG |
| 5065 |
/** Read the max cache size parameter from the config table. */ |
| 5066 |
static void fts_update_max_cache_size(fts_sync_t *sync) /*!< in: sync state */ |
| 5067 |
{ |
| 5068 |
trx_t *trx; |
| 5069 |
fts_table_t fts_table; |
| 5070 |
|
| 5071 |
trx = trx_allocate_for_background(); |
| 5072 |
|
| 5073 |
FTS_INIT_FTS_TABLE(&fts_table, FTS_SUFFIX_CONFIG, FTS_COMMON_TABLE, |
| 5074 |
sync->table); |
| 5075 |
|
| 5076 |
/* The size returned is in bytes. */ |
| 5077 |
sync->max_cache_size = fts_get_max_cache_size(trx, &fts_table); |
| 5078 |
|
| 5079 |
fts_sql_commit(trx); |
| 5080 |
|
| 5081 |
trx_free_for_background(trx); |
| 5082 |
} |
| 5083 |
#endif /* FTS_CACHE_SIZE_DEBUG */ |
| 5084 |
|
| 5085 |
/** Free the modified rows of a table. */ |
| 5086 |
static inline void fts_trx_table_rows_free( |
| 5087 |
ib_rbt_t *rows) /*!< in: rbt of rows to free */ |
| 5088 |
{ |
| 5089 |
const ib_rbt_node_t *node; |
| 5090 |
|
| 5091 |
for (node = rbt_first(rows); node; node = rbt_first(rows)) { |
| 5092 |
fts_trx_row_t *row; |
| 5093 |
|
| 5094 |
row = rbt_value(fts_trx_row_t, node); |
| 5095 |
|
| 5096 |
if (row->fts_indexes != nullptr) { |
| 5097 |
/* This vector shouldn't be using the |
| 5098 |
heap allocator. */ |
| 5099 |
ut_a(row->fts_indexes->allocator->arg == nullptr); |
| 5100 |
|
| 5101 |
ib_vector_free(row->fts_indexes); |
| 5102 |
row->fts_indexes = nullptr; |
| 5103 |
} |
| 5104 |
|
| 5105 |
ut::free(rbt_remove_node(rows, node)); |
| 5106 |
} |
| 5107 |
|
| 5108 |
ut_a(rbt_empty(rows)); |
| 5109 |
rbt_free(rows); |
| 5110 |
} |
| 5111 |
|
| 5112 |
/** Free an FTS savepoint instance. */ |
| 5113 |
static inline void fts_savepoint_free( |
| 5114 |
fts_savepoint_t *savepoint) /*!< in: savepoint instance */ |
| 5115 |
{ |
| 5116 |
const ib_rbt_node_t *node; |
| 5117 |
ib_rbt_t *tables = savepoint->tables; |
| 5118 |
|
| 5119 |
/* Nothing to free! */ |
| 5120 |
if (tables == nullptr) { |
| 5121 |
return; |
| 5122 |
} |
| 5123 |
|
| 5124 |
for (node = rbt_first(tables); node; node = rbt_first(tables)) { |
| 5125 |
fts_trx_table_t *ftt; |
| 5126 |
fts_trx_table_t **fttp; |
| 5127 |
|
| 5128 |
fttp = rbt_value(fts_trx_table_t *, node); |
| 5129 |
ftt = *fttp; |
| 5130 |
|
| 5131 |
/* This can be NULL if a savepoint was released. */ |
| 5132 |
if (ftt->rows != nullptr) { |
| 5133 |
fts_trx_table_rows_free(ftt->rows); |
| 5134 |
ftt->rows = nullptr; |
| 5135 |
} |
| 5136 |
|
| 5137 |
/* This can be NULL if a savepoint was released. */ |
| 5138 |
if (ftt->added_doc_ids != nullptr) { |
| 5139 |
fts_doc_ids_free(ftt->added_doc_ids); |
| 5140 |
ftt->added_doc_ids = nullptr; |
| 5141 |
} |
| 5142 |
|
| 5143 |
/* The default savepoint name must be NULL. */ |
| 5144 |
if (ftt->docs_added_graph) { |
| 5145 |
fts_que_graph_free(ftt->docs_added_graph); |
| 5146 |
} |
| 5147 |
|
| 5148 |
/* NOTE: We are responsible for free'ing the node */ |
| 5149 |
ut::free(rbt_remove_node(tables, node)); |
| 5150 |
} |
| 5151 |
|
| 5152 |
ut_a(rbt_empty(tables)); |
| 5153 |
rbt_free(tables); |
| 5154 |
savepoint->tables = nullptr; |
| 5155 |
} |
| 5156 |
|
| 5157 |
/** Free an FTS trx. */ |
| 5158 |
void fts_trx_free(fts_trx_t *fts_trx) /* in, own: FTS trx */ |
| 5159 |
{ |
| 5160 |
ulint i; |
| 5161 |
|
| 5162 |
for (i = 0; i < ib_vector_size(fts_trx->savepoints); ++i) { |
| 5163 |
fts_savepoint_t *savepoint; |
| 5164 |
|
| 5165 |
savepoint = |
| 5166 |
static_cast<fts_savepoint_t *>(ib_vector_get(fts_trx->savepoints, i)); |
| 5167 |
|
| 5168 |
/* The default savepoint name must be NULL. */ |
| 5169 |
if (i == 0) { |
| 5170 |
ut_a(savepoint->name == nullptr); |
| 5171 |
} |
| 5172 |
|
| 5173 |
fts_savepoint_free(savepoint); |
| 5174 |
} |
| 5175 |
|
| 5176 |
for (i = 0; i < ib_vector_size(fts_trx->last_stmt); ++i) { |
| 5177 |
fts_savepoint_t *savepoint; |
| 5178 |
|
| 5179 |
savepoint = |
| 5180 |
static_cast<fts_savepoint_t *>(ib_vector_get(fts_trx->last_stmt, i)); |
| 5181 |
|
| 5182 |
/* The default savepoint name must be NULL. */ |
| 5183 |
if (i == 0) { |
| 5184 |
ut_a(savepoint->name == nullptr); |
| 5185 |
} |
| 5186 |
|
| 5187 |
fts_savepoint_free(savepoint); |
| 5188 |
} |
| 5189 |
|
| 5190 |
if (fts_trx->heap) { |
| 5191 |
mem_heap_free(fts_trx->heap); |
| 5192 |
} |
| 5193 |
} |
| 5194 |
|
| 5195 |
/** Extract the doc id from the FTS hidden column. |
| 5196 |
@return doc id that was extracted from rec */ |
| 5197 |
doc_id_t fts_get_doc_id_from_row(dict_table_t *table, /*!< in: table */ |
| 5198 |
dtuple_t *row) /*!< in: row whose FTS doc id we |
| 5199 |
want to extract.*/ |
| 5200 |
{ |
| 5201 |
dfield_t *field; |
| 5202 |
doc_id_t doc_id = 0; |
| 5203 |
|
| 5204 |
ut_a(table->fts->doc_col != ULINT_UNDEFINED); |
| 5205 |
|
| 5206 |
field = dtuple_get_nth_field(row, table->fts->doc_col); |
| 5207 |
|
| 5208 |
ut_a(dfield_get_len(field) == sizeof(doc_id)); |
| 5209 |
ut_a(dfield_get_type(field)->mtype == DATA_INT); |
| 5210 |
|
| 5211 |
doc_id = fts_read_doc_id(static_cast<const byte *>(dfield_get_data(field))); |
| 5212 |
|
| 5213 |
return (doc_id); |
| 5214 |
} |
| 5215 |
|
| 5216 |
/** Extract the doc id from the record that belongs to index. |
| 5217 |
@param[in] table table |
| 5218 |
@param[in] rec record contains FTS_DOC_ID |
| 5219 |
@param[in] index index of rec |
| 5220 |
@param[in] heap heap memory |
| 5221 |
@return doc id that was extracted from rec */ |
| 5222 |
doc_id_t fts_get_doc_id_from_rec(dict_table_t *table, const rec_t *rec, |
| 5223 |
const dict_index_t *index, mem_heap_t *heap) { |
| 5224 |
ulint len; |
| 5225 |
const byte *data; |
| 5226 |
ulint col_no; |
| 5227 |
doc_id_t doc_id = 0; |
| 5228 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 5229 |
ulint *offsets = offsets_; |
| 5230 |
mem_heap_t *my_heap = heap; |
| 5231 |
|
| 5232 |
ut_a(table->fts->doc_col != ULINT_UNDEFINED); |
| 5233 |
|
| 5234 |
rec_offs_init(offsets_); |
| 5235 |
|
| 5236 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 5237 |
UT_LOCATION_HERE, &my_heap); |
| 5238 |
|
| 5239 |
col_no = index->get_col_pos(table->fts->doc_col); |
| 5240 |
|
| 5241 |
ut_ad(col_no != ULINT_UNDEFINED); |
| 5242 |
|
| 5243 |
data = rec_get_nth_field(nullptr, rec, offsets, col_no, &len); |
| 5244 |
|
| 5245 |
ut_a(len == 8); |
| 5246 |
static_assert(8 == sizeof(doc_id)); |
| 5247 |
doc_id = static_cast<doc_id_t>(mach_read_from_8(data)); |
| 5248 |
|
| 5249 |
if (my_heap && !heap) { |
| 5250 |
mem_heap_free(my_heap); |
| 5251 |
} |
| 5252 |
|
| 5253 |
return (doc_id); |
| 5254 |
} |
| 5255 |
|
| 5256 |
/** Search the index specific cache for a particular FTS index. |
| 5257 |
@return the index specific cache else NULL */ |
| 5258 |
fts_index_cache_t *fts_find_index_cache( |
| 5259 |
const fts_cache_t *cache, /*!< in: cache to search */ |
| 5260 |
const dict_index_t *index) /*!< in: index to search for */ |
| 5261 |
{ |
| 5262 |
/* We cast away the const because our internal function, takes |
| 5263 |
non-const cache arg and returns a non-const pointer. */ |
| 5264 |
return (static_cast<fts_index_cache_t *>( |
| 5265 |
fts_get_index_cache((fts_cache_t *)cache, index))); |
| 5266 |
} |
| 5267 |
|
| 5268 |
/** Search cache for word. |
| 5269 |
@return the word node vector if found else NULL */ |
| 5270 |
const ib_vector_t *fts_cache_find_word( |
| 5271 |
const fts_index_cache_t *index_cache, /*!< in: cache to search */ |
| 5272 |
const fts_string_t *text) /*!< in: word to search for */ |
| 5273 |
{ |
| 5274 |
ib_rbt_bound_t parent; |
| 5275 |
const ib_vector_t *nodes = nullptr; |
| 5276 |
#ifdef UNIV_DEBUG |
| 5277 |
dict_table_t *table = index_cache->index->table; |
| 5278 |
fts_cache_t *cache = table->fts->cache; |
| 5279 |
|
| 5280 |
ut_ad(rw_lock_own(&cache->lock, RW_LOCK_X)); |
| 5281 |
#endif /* UNIV_DEBUG */ |
| 5282 |
|
| 5283 |
/* Lookup the word in the rb tree */ |
| 5284 |
if (rbt_search(index_cache->words, &parent, text) == 0) { |
| 5285 |
const fts_tokenizer_word_t *word; |
| 5286 |
|
| 5287 |
word = rbt_value(fts_tokenizer_word_t, parent.last); |
| 5288 |
|
| 5289 |
nodes = word->nodes; |
| 5290 |
} |
| 5291 |
|
| 5292 |
return (nodes); |
| 5293 |
} |
| 5294 |
|
| 5295 |
/** Append deleted doc ids to vector. */ |
| 5296 |
void fts_cache_append_deleted_doc_ids( |
| 5297 |
const fts_cache_t *cache, /*!< in: cache to use */ |
| 5298 |
ib_vector_t *vector) /*!< in: append to this vector */ |
| 5299 |
{ |
| 5300 |
mutex_enter(const_cast<ib_mutex_t *>(&cache->deleted_lock)); |
| 5301 |
|
| 5302 |
if (cache->deleted_doc_ids == nullptr) { |
| 5303 |
mutex_exit((ib_mutex_t *)&cache->deleted_lock); |
| 5304 |
return; |
| 5305 |
} |
| 5306 |
|
| 5307 |
for (ulint i = 0; i < ib_vector_size(cache->deleted_doc_ids); ++i) { |
| 5308 |
fts_update_t *update; |
| 5309 |
|
| 5310 |
update = |
| 5311 |
static_cast<fts_update_t *>(ib_vector_get(cache->deleted_doc_ids, i)); |
| 5312 |
|
| 5313 |
ib_vector_push(vector, &update->doc_id); |
| 5314 |
} |
| 5315 |
|
| 5316 |
mutex_exit((ib_mutex_t *)&cache->deleted_lock); |
| 5317 |
} |
| 5318 |
|
| 5319 |
bool fts_wait_for_background_thread_to_start( |
| 5320 |
dict_table_t *table, std::chrono::microseconds max_wait) { |
| 5321 |
ulint count = 0; |
| 5322 |
bool done = false; |
| 5323 |
|
| 5324 |
ut_a(max_wait == std::chrono::seconds::zero() || |
| 5325 |
max_wait >= FTS_MAX_BACKGROUND_THREAD_WAIT); |
| 5326 |
|
| 5327 |
for (;;) { |
| 5328 |
fts_t *fts = table->fts; |
| 5329 |
|
| 5330 |
mutex_enter(&fts->bg_threads_mutex); |
| 5331 |
|
| 5332 |
if (fts->fts_status & BG_THREAD_READY) { |
| 5333 |
done = true; |
| 5334 |
} |
| 5335 |
|
| 5336 |
mutex_exit(&fts->bg_threads_mutex); |
| 5337 |
|
| 5338 |
if (!done) { |
| 5339 |
std::this_thread::sleep_for(FTS_MAX_BACKGROUND_THREAD_WAIT); |
| 5340 |
|
| 5341 |
if (max_wait > std::chrono::seconds::zero()) { |
| 5342 |
max_wait -= FTS_MAX_BACKGROUND_THREAD_WAIT; |
| 5343 |
|
| 5344 |
/* We ignore the residual value. */ |
| 5345 |
if (max_wait < FTS_MAX_BACKGROUND_THREAD_WAIT) { |
| 5346 |
break; |
| 5347 |
} |
| 5348 |
} |
| 5349 |
|
| 5350 |
++count; |
| 5351 |
} else { |
| 5352 |
break; |
| 5353 |
} |
| 5354 |
|
| 5355 |
if (count >= FTS_BACKGROUND_THREAD_WAIT_COUNT) { |
| 5356 |
ib::error(ER_IB_MSG_480) << "The background thread for the FTS" |
| 5357 |
" table " |
| 5358 |
<< table->name << " refuses to start"; |
| 5359 |
|
| 5360 |
count = 0; |
| 5361 |
} |
| 5362 |
} |
| 5363 |
|
| 5364 |
return (done); |
| 5365 |
} |
| 5366 |
|
| 5367 |
/** Add the FTS document id hidden column. |
| 5368 |
@param[in,out] table Table with FTS index |
| 5369 |
@param[in] heap Temporary memory heap, or NULL |
| 5370 |
*/ |
| 5371 |
void fts_add_doc_id_column(dict_table_t *table, mem_heap_t *heap) { |
| 5372 |
dict_mem_table_add_col( |
| 5373 |
table, heap, FTS_DOC_ID_COL_NAME, DATA_INT, |
| 5374 |
dtype_form_prtype( |
| 5375 |
DATA_NOT_NULL | DATA_UNSIGNED | DATA_BINARY_TYPE | DATA_FTS_DOC_ID, |
| 5376 |
0), |
| 5377 |
sizeof(doc_id_t), false); |
| 5378 |
DICT_TF2_FLAG_SET(table, DICT_TF2_FTS_HAS_DOC_ID); |
| 5379 |
} |
| 5380 |
|
| 5381 |
/** Add new fts doc id to the update vector. |
| 5382 |
@param[in] table the table that contains the FTS index. |
| 5383 |
@param[in,out] ufield the fts doc id field in the update vector. |
| 5384 |
No new memory is allocated for this in this |
| 5385 |
function. |
| 5386 |
@param[in,out] next_doc_id the fts doc id that has been added to the |
| 5387 |
update vector. If 0, a new fts doc id is |
| 5388 |
automatically generated. The memory provided |
| 5389 |
for this argument will be used by the update |
| 5390 |
vector. Ensure that the life time of this |
| 5391 |
memory matches that of the update vector. |
| 5392 |
@return the fts doc id used in the update vector */ |
| 5393 |
doc_id_t fts_update_doc_id(dict_table_t *table, upd_field_t *ufield, |
| 5394 |
doc_id_t *next_doc_id) { |
| 5395 |
doc_id_t doc_id; |
| 5396 |
dberr_t error = DB_SUCCESS; |
| 5397 |
|
| 5398 |
if (*next_doc_id) { |
| 5399 |
doc_id = *next_doc_id; |
| 5400 |
} else { |
| 5401 |
/* Get the new document id that will be added. */ |
| 5402 |
error = fts_get_next_doc_id(table, &doc_id); |
| 5403 |
} |
| 5404 |
|
| 5405 |
if (error == DB_SUCCESS) { |
| 5406 |
dict_index_t *clust_index; |
| 5407 |
dict_col_t *col = table->get_col(table->fts->doc_col); |
| 5408 |
|
| 5409 |
ufield->exp = nullptr; |
| 5410 |
|
| 5411 |
ufield->new_val.len = sizeof(doc_id); |
| 5412 |
|
| 5413 |
clust_index = table->first_index(); |
| 5414 |
|
| 5415 |
ufield->field_no = dict_col_get_clust_pos(col, clust_index); |
| 5416 |
col->copy_type(dfield_get_type(&ufield->new_val)); |
| 5417 |
|
| 5418 |
/* It is possible we update record that has |
| 5419 |
not yet be sync-ed from last crash. */ |
| 5420 |
|
| 5421 |
/* Convert to storage byte order. */ |
| 5422 |
ut_a(doc_id != FTS_NULL_DOC_ID); |
| 5423 |
fts_write_doc_id((byte *)next_doc_id, doc_id); |
| 5424 |
|
| 5425 |
ufield->new_val.data = next_doc_id; |
| 5426 |
ufield->new_val.ext = 0; |
| 5427 |
} |
| 5428 |
|
| 5429 |
return (doc_id); |
| 5430 |
} |
| 5431 |
|
| 5432 |
/** fts_t constructor. |
| 5433 |
@param[in] table table with FTS indexes |
| 5434 |
@param[in,out] heap memory heap where 'this' is stored */ |
| 5435 |
fts_t::fts_t(dict_table_t *table, mem_heap_t *heap) |
| 5436 |
: bg_threads(0), |
| 5437 |
fts_status(0), |
| 5438 |
add_wq(nullptr), |
| 5439 |
cache(nullptr), |
| 5440 |
doc_col(ULINT_UNDEFINED), |
| 5441 |
fts_heap(heap) { |
| 5442 |
ut_a(table->fts == nullptr); |
| 5443 |
|
| 5444 |
mutex_create(LATCH_ID_FTS_BG_THREADS, &bg_threads_mutex); |
| 5445 |
|
| 5446 |
ib_alloc_t *heap_alloc = ib_heap_allocator_create(fts_heap); |
| 5447 |
|
| 5448 |
indexes = ib_vector_create(heap_alloc, sizeof(dict_index_t *), 4); |
| 5449 |
|
| 5450 |
dict_table_get_all_fts_indexes(table, indexes); |
| 5451 |
} |
| 5452 |
|
| 5453 |
/** fts_t destructor. */ |
| 5454 |
fts_t::~fts_t() { |
| 5455 |
mutex_free(&bg_threads_mutex); |
| 5456 |
|
| 5457 |
ut_ad(add_wq == nullptr); |
| 5458 |
|
| 5459 |
if (cache != nullptr) { |
| 5460 |
fts_cache_clear(cache); |
| 5461 |
fts_cache_destroy(cache); |
| 5462 |
cache = nullptr; |
| 5463 |
} |
| 5464 |
|
| 5465 |
/* There is no need to call ib_vector_free() on this->indexes |
| 5466 |
because it is stored in this->fts_heap. */ |
| 5467 |
} |
| 5468 |
|
| 5469 |
/** Create an instance of fts_t. |
| 5470 |
@return instance of fts_t */ |
| 5471 |
fts_t *fts_create(dict_table_t *table) /*!< in/out: table with FTS indexes */ |
| 5472 |
{ |
| 5473 |
fts_t *fts; |
| 5474 |
mem_heap_t *heap; |
| 5475 |
|
| 5476 |
heap = mem_heap_create(512, UT_LOCATION_HERE); |
| 5477 |
|
| 5478 |
fts = static_cast<fts_t *>(mem_heap_alloc(heap, sizeof(*fts))); |
| 5479 |
|
| 5480 |
new (fts) fts_t(table, heap); |
| 5481 |
|
| 5482 |
return (fts); |
| 5483 |
} |
| 5484 |
|
| 5485 |
/** Free the FTS resources. */ |
| 5486 |
void fts_free(dict_table_t *table) /*!< in/out: table with FTS indexes */ |
| 5487 |
{ |
| 5488 |
fts_t *fts = table->fts; |
| 5489 |
|
| 5490 |
fts->~fts_t(); |
| 5491 |
|
| 5492 |
mem_heap_free(fts->fts_heap); |
| 5493 |
|
| 5494 |
table->fts = nullptr; |
| 5495 |
} |
| 5496 |
|
| 5497 |
#if 0 // TODO: Enable this in WL#6608 |
| 5498 |
/*********************************************************************//** |
| 5499 |
Signal FTS threads to initiate shutdown. */ |
| 5500 |
void |
| 5501 |
fts_start_shutdown( |
| 5502 |
dict_table_t* table, /*!< in: table with FTS indexes */ |
| 5503 |
fts_t* fts) /*!< in: fts instance that needs |
| 5504 |
to be informed about shutdown */ |
| 5505 |
{ |
| 5506 |
mutex_enter(&fts->bg_threads_mutex); |
| 5507 |
|
| 5508 |
fts->fts_status |= BG_THREAD_STOP; |
| 5509 |
|
| 5510 |
mutex_exit(&fts->bg_threads_mutex); |
| 5511 |
|
| 5512 |
} |
| 5513 |
|
| 5514 |
/*********************************************************************//** |
| 5515 |
Wait for FTS threads to shutdown. */ |
| 5516 |
void |
| 5517 |
fts_shutdown( |
| 5518 |
dict_table_t* table, /*!< in: table with FTS indexes */ |
| 5519 |
fts_t* fts) /*!< in: fts instance to shutdown */ |
| 5520 |
{ |
| 5521 |
mutex_enter(&fts->bg_threads_mutex); |
| 5522 |
|
| 5523 |
ut_a(fts->fts_status & BG_THREAD_STOP); |
| 5524 |
|
| 5525 |
dict_table_wait_for_bg_threads_to_exit(table, std::chrono::milliseconds{20}); |
| 5526 |
|
| 5527 |
mutex_exit(&fts->bg_threads_mutex); |
| 5528 |
} |
| 5529 |
#endif |
| 5530 |
|
| 5531 |
/** Take a FTS savepoint. */ |
| 5532 |
static inline void fts_savepoint_copy( |
| 5533 |
const fts_savepoint_t *src, /*!< in: source savepoint */ |
| 5534 |
fts_savepoint_t *dst) /*!< out: destination savepoint */ |
| 5535 |
{ |
| 5536 |
const ib_rbt_node_t *node; |
| 5537 |
const ib_rbt_t *tables; |
| 5538 |
|
| 5539 |
tables = src->tables; |
| 5540 |
|
| 5541 |
for (node = rbt_first(tables); node; node = rbt_next(tables, node)) { |
| 5542 |
fts_trx_table_t *ftt_dst; |
| 5543 |
const fts_trx_table_t **ftt_src; |
| 5544 |
|
| 5545 |
ftt_src = rbt_value(const fts_trx_table_t *, node); |
| 5546 |
|
| 5547 |
ftt_dst = fts_trx_table_clone(*ftt_src); |
| 5548 |
|
| 5549 |
rbt_insert(dst->tables, &ftt_dst, &ftt_dst); |
| 5550 |
} |
| 5551 |
} |
| 5552 |
|
| 5553 |
void fts_savepoint_take(fts_trx_t *fts_trx, const char *name) { |
| 5554 |
mem_heap_t *heap; |
| 5555 |
fts_savepoint_t *savepoint; |
| 5556 |
fts_savepoint_t *last_savepoint; |
| 5557 |
|
| 5558 |
ut_a(name != nullptr); |
| 5559 |
|
| 5560 |
heap = fts_trx->heap; |
| 5561 |
|
| 5562 |
/* The implied savepoint must exist. */ |
| 5563 |
ut_a(ib_vector_size(fts_trx->savepoints) > 0); |
| 5564 |
|
| 5565 |
last_savepoint = |
| 5566 |
static_cast<fts_savepoint_t *>(ib_vector_last(fts_trx->savepoints)); |
| 5567 |
savepoint = fts_savepoint_create(fts_trx->savepoints, name, heap); |
| 5568 |
|
| 5569 |
if (last_savepoint->tables != nullptr) { |
| 5570 |
fts_savepoint_copy(last_savepoint, savepoint); |
| 5571 |
} |
| 5572 |
} |
| 5573 |
|
| 5574 |
/** Lookup a savepoint instance by name. |
| 5575 |
@return ULINT_UNDEFINED if not found */ |
| 5576 |
static inline ulint fts_savepoint_lookup( |
| 5577 |
ib_vector_t *savepoints, /*!< in: savepoints */ |
| 5578 |
const char *name) /*!< in: savepoint name */ |
| 5579 |
{ |
| 5580 |
ulint i; |
| 5581 |
|
| 5582 |
ut_a(ib_vector_size(savepoints) > 0); |
| 5583 |
|
| 5584 |
for (i = 1; i < ib_vector_size(savepoints); ++i) { |
| 5585 |
fts_savepoint_t *savepoint; |
| 5586 |
|
| 5587 |
savepoint = static_cast<fts_savepoint_t *>(ib_vector_get(savepoints, i)); |
| 5588 |
|
| 5589 |
if (strcmp(name, savepoint->name) == 0) { |
| 5590 |
return (i); |
| 5591 |
} |
| 5592 |
} |
| 5593 |
|
| 5594 |
return (ULINT_UNDEFINED); |
| 5595 |
} |
| 5596 |
|
| 5597 |
/** Release the savepoint data identified by name. All savepoints created |
| 5598 |
after the named savepoint are kept. */ |
| 5599 |
void fts_savepoint_release(trx_t *trx, /*!< in: transaction */ |
| 5600 |
const char *name) /*!< in: savepoint name */ |
| 5601 |
{ |
| 5602 |
ut_a(name != nullptr); |
| 5603 |
|
| 5604 |
ib_vector_t *savepoints = trx->fts_trx->savepoints; |
| 5605 |
|
| 5606 |
ut_a(ib_vector_size(savepoints) > 0); |
| 5607 |
|
| 5608 |
ulint i = fts_savepoint_lookup(savepoints, name); |
| 5609 |
if (i != ULINT_UNDEFINED) { |
| 5610 |
ut_a(i >= 1); |
| 5611 |
|
| 5612 |
fts_savepoint_t *savepoint; |
| 5613 |
savepoint = static_cast<fts_savepoint_t *>(ib_vector_get(savepoints, i)); |
| 5614 |
|
| 5615 |
if (i == ib_vector_size(savepoints) - 1) { |
| 5616 |
/* If the savepoint is the last, we save its |
| 5617 |
tables to the previous savepoint. */ |
| 5618 |
fts_savepoint_t *prev_savepoint; |
| 5619 |
prev_savepoint = |
| 5620 |
static_cast<fts_savepoint_t *>(ib_vector_get(savepoints, i - 1)); |
| 5621 |
|
| 5622 |
ib_rbt_t *tables = savepoint->tables; |
| 5623 |
savepoint->tables = prev_savepoint->tables; |
| 5624 |
prev_savepoint->tables = tables; |
| 5625 |
} |
| 5626 |
|
| 5627 |
fts_savepoint_free(savepoint); |
| 5628 |
ib_vector_remove(savepoints, *(void **)savepoint); |
| 5629 |
|
| 5630 |
/* Make sure we don't delete the implied savepoint. */ |
| 5631 |
ut_a(ib_vector_size(savepoints) > 0); |
| 5632 |
} |
| 5633 |
} |
| 5634 |
|
| 5635 |
/** Refresh last statement savepoint. */ |
| 5636 |
void fts_savepoint_laststmt_refresh(trx_t *trx) /*!< in: transaction */ |
| 5637 |
{ |
| 5638 |
fts_trx_t *fts_trx; |
| 5639 |
fts_savepoint_t *savepoint; |
| 5640 |
|
| 5641 |
fts_trx = trx->fts_trx; |
| 5642 |
|
| 5643 |
savepoint = static_cast<fts_savepoint_t *>(ib_vector_pop(fts_trx->last_stmt)); |
| 5644 |
fts_savepoint_free(savepoint); |
| 5645 |
|
| 5646 |
ut_ad(ib_vector_is_empty(fts_trx->last_stmt)); |
| 5647 |
savepoint = fts_savepoint_create(fts_trx->last_stmt, nullptr, nullptr); |
| 5648 |
} |
| 5649 |
|
| 5650 |
/******************************************************************** |
| 5651 |
Undo the Doc ID add/delete operations in last stmt */ |
| 5652 |
static void fts_undo_last_stmt( |
| 5653 |
fts_trx_table_t *s_ftt, /*!< in: Transaction FTS table */ |
| 5654 |
fts_trx_table_t *l_ftt) /*!< in: last stmt FTS table */ |
| 5655 |
{ |
| 5656 |
ib_rbt_t *s_rows; |
| 5657 |
ib_rbt_t *l_rows; |
| 5658 |
const ib_rbt_node_t *node; |
| 5659 |
|
| 5660 |
l_rows = l_ftt->rows; |
| 5661 |
s_rows = s_ftt->rows; |
| 5662 |
|
| 5663 |
for (node = rbt_first(l_rows); node; node = rbt_next(l_rows, node)) { |
| 5664 |
fts_trx_row_t *l_row = rbt_value(fts_trx_row_t, node); |
| 5665 |
ib_rbt_bound_t parent; |
| 5666 |
|
| 5667 |
rbt_search(s_rows, &parent, &(l_row->doc_id)); |
| 5668 |
|
| 5669 |
if (parent.result == 0) { |
| 5670 |
fts_trx_row_t *s_row = rbt_value(fts_trx_row_t, parent.last); |
| 5671 |
|
| 5672 |
switch (l_row->state) { |
| 5673 |
case FTS_INSERT: |
| 5674 |
ut::free(rbt_remove_node(s_rows, parent.last)); |
| 5675 |
break; |
| 5676 |
|
| 5677 |
case FTS_DELETE: |
| 5678 |
if (s_row->state == FTS_NOTHING) { |
| 5679 |
s_row->state = FTS_INSERT; |
| 5680 |
} else if (s_row->state == FTS_DELETE) { |
| 5681 |
ut::free(rbt_remove_node(s_rows, parent.last)); |
| 5682 |
} |
| 5683 |
break; |
| 5684 |
|
| 5685 |
/* FIXME: Check if FTS_MODIFY need to be addressed */ |
| 5686 |
case FTS_MODIFY: |
| 5687 |
case FTS_NOTHING: |
| 5688 |
break; |
| 5689 |
default: |
| 5690 |
ut_error; |
| 5691 |
} |
| 5692 |
} |
| 5693 |
} |
| 5694 |
} |
| 5695 |
|
| 5696 |
/** Rollback to savepoint identified by name. */ |
| 5697 |
void fts_savepoint_rollback_last_stmt(trx_t *trx) /*!< in: transaction */ |
| 5698 |
{ |
| 5699 |
ib_vector_t *savepoints; |
| 5700 |
fts_savepoint_t *savepoint; |
| 5701 |
fts_savepoint_t *last_stmt; |
| 5702 |
fts_trx_t *fts_trx; |
| 5703 |
ib_rbt_bound_t parent; |
| 5704 |
const ib_rbt_node_t *node; |
| 5705 |
ib_rbt_t *l_tables; |
| 5706 |
ib_rbt_t *s_tables; |
| 5707 |
|
| 5708 |
fts_trx = trx->fts_trx; |
| 5709 |
savepoints = fts_trx->savepoints; |
| 5710 |
|
| 5711 |
savepoint = static_cast<fts_savepoint_t *>(ib_vector_last(savepoints)); |
| 5712 |
last_stmt = |
| 5713 |
static_cast<fts_savepoint_t *>(ib_vector_last(fts_trx->last_stmt)); |
| 5714 |
|
| 5715 |
l_tables = last_stmt->tables; |
| 5716 |
s_tables = savepoint->tables; |
| 5717 |
|
| 5718 |
for (node = rbt_first(l_tables); node; node = rbt_next(l_tables, node)) { |
| 5719 |
fts_trx_table_t **l_ftt; |
| 5720 |
|
| 5721 |
l_ftt = rbt_value(fts_trx_table_t *, node); |
| 5722 |
|
| 5723 |
rbt_search_cmp(s_tables, &parent, &(*l_ftt)->table->id, |
| 5724 |
fts_trx_table_id_cmp, nullptr); |
| 5725 |
|
| 5726 |
if (parent.result == 0) { |
| 5727 |
fts_trx_table_t **s_ftt; |
| 5728 |
|
| 5729 |
s_ftt = rbt_value(fts_trx_table_t *, parent.last); |
| 5730 |
|
| 5731 |
fts_undo_last_stmt(*s_ftt, *l_ftt); |
| 5732 |
} |
| 5733 |
} |
| 5734 |
} |
| 5735 |
|
| 5736 |
/** Rollback to savepoint identified by name. */ |
| 5737 |
void fts_savepoint_rollback(trx_t *trx, /*!< in: transaction */ |
| 5738 |
const char *name) /*!< in: savepoint name */ |
| 5739 |
{ |
| 5740 |
ulint i; |
| 5741 |
ib_vector_t *savepoints; |
| 5742 |
|
| 5743 |
ut_a(name != nullptr); |
| 5744 |
|
| 5745 |
savepoints = trx->fts_trx->savepoints; |
| 5746 |
|
| 5747 |
/* We pop all savepoints from the the top of the stack up to |
| 5748 |
and including the instance that was found. */ |
| 5749 |
i = fts_savepoint_lookup(savepoints, name); |
| 5750 |
|
| 5751 |
if (i != ULINT_UNDEFINED) { |
| 5752 |
fts_savepoint_t *savepoint; |
| 5753 |
|
| 5754 |
ut_a(i > 0); |
| 5755 |
|
| 5756 |
while (ib_vector_size(savepoints) > i) { |
| 5757 |
fts_savepoint_t *savepoint; |
| 5758 |
|
| 5759 |
savepoint = static_cast<fts_savepoint_t *>(ib_vector_pop(savepoints)); |
| 5760 |
|
| 5761 |
if (savepoint->name != nullptr) { |
| 5762 |
/* Since name was allocated on the heap, the |
| 5763 |
memory will be released when the transaction |
| 5764 |
completes. */ |
| 5765 |
savepoint->name = nullptr; |
| 5766 |
|
| 5767 |
fts_savepoint_free(savepoint); |
| 5768 |
} |
| 5769 |
} |
| 5770 |
|
| 5771 |
/* Pop all a elements from the top of the stack that may |
| 5772 |
have been released. We have to be careful that we don't |
| 5773 |
delete the implied savepoint. */ |
| 5774 |
|
| 5775 |
for (savepoint = static_cast<fts_savepoint_t *>(ib_vector_last(savepoints)); |
| 5776 |
ib_vector_size(savepoints) > 1 && savepoint->name == nullptr; |
| 5777 |
savepoint = |
| 5778 |
static_cast<fts_savepoint_t *>(ib_vector_last(savepoints))) { |
| 5779 |
ib_vector_pop(savepoints); |
| 5780 |
} |
| 5781 |
|
| 5782 |
/* Make sure we don't delete the implied savepoint. */ |
| 5783 |
ut_a(ib_vector_size(savepoints) > 0); |
| 5784 |
|
| 5785 |
/* Restore the savepoint. */ |
| 5786 |
fts_savepoint_take(trx->fts_trx, name); |
| 5787 |
} |
| 5788 |
} |
| 5789 |
|
| 5790 |
/** Check if a table is an FTS auxiliary table name. |
| 5791 |
@param[out] table FTS table info |
| 5792 |
@param[in] name Table name |
| 5793 |
@param[in] len Length of table name |
| 5794 |
@return true if the name matches an auxiliary table name pattern */ |
| 5795 |
bool fts_is_aux_table_name(fts_aux_table_t *table, const char *name, |
| 5796 |
ulint len) { |
| 5797 |
const char *ptr; |
| 5798 |
char *end; |
| 5799 |
char my_name[MAX_FULL_NAME_LEN + 1]; |
| 5800 |
|
| 5801 |
ut_ad(len <= MAX_FULL_NAME_LEN); |
| 5802 |
ut_memcpy(my_name, name, len); |
| 5803 |
my_name[len] = 0; |
| 5804 |
end = my_name + len; |
| 5805 |
|
| 5806 |
ptr = static_cast<const char *>(memchr(my_name, '/', len)); |
| 5807 |
|
| 5808 |
if (ptr != nullptr) { |
| 5809 |
/* We will start the match after the '/' */ |
| 5810 |
++ptr; |
| 5811 |
len = end - ptr; |
| 5812 |
} |
| 5813 |
|
| 5814 |
/* All auxiliary tables are prefixed with "FTS_" and the name |
| 5815 |
length will be at the very least greater than 20 bytes. */ |
| 5816 |
if (ptr != nullptr && len > 20 && |
| 5817 |
(strncmp(ptr, FTS_PREFIX, 4) == 0 || |
| 5818 |
strncmp(ptr, FTS_PREFIX_5_7, 4) == 0)) { |
| 5819 |
ulint i; |
| 5820 |
|
| 5821 |
/* Skip the prefix. */ |
| 5822 |
ptr += 4; |
| 5823 |
len -= 4; |
| 5824 |
|
| 5825 |
/* Try and read the table id. */ |
| 5826 |
if (!fts_read_object_id(&table->parent_id, ptr)) { |
| 5827 |
return (false); |
| 5828 |
} |
| 5829 |
|
| 5830 |
/* Skip the table id. */ |
| 5831 |
ptr = static_cast<const char *>(memchr(ptr, '_', len)); |
| 5832 |
|
| 5833 |
if (ptr == nullptr) { |
| 5834 |
return (false); |
| 5835 |
} |
| 5836 |
|
| 5837 |
/* Skip the underscore. */ |
| 5838 |
++ptr; |
| 5839 |
ut_a(end >= ptr); |
| 5840 |
len = end - ptr; |
| 5841 |
|
| 5842 |
/* It's not enough to be a FTS auxiliary table name */ |
| 5843 |
if (len == 0) { |
| 5844 |
return (false); |
| 5845 |
} |
| 5846 |
|
| 5847 |
/* First search the common table suffix array. */ |
| 5848 |
for (i = 0; fts_common_tables[i] != nullptr; ++i) { |
| 5849 |
if ((len == strlen(fts_common_tables[i])) && |
| 5850 |
(strncmp(ptr, fts_common_tables[i], len) == 0)) { |
| 5851 |
table->type = FTS_COMMON_TABLE; |
| 5852 |
return (true); |
| 5853 |
} |
| 5854 |
|
| 5855 |
if ((len == strlen(fts_common_tables_5_7[i])) && |
| 5856 |
(strncmp(ptr, fts_common_tables_5_7[i], len) == 0)) { |
| 5857 |
table->type = FTS_COMMON_TABLE; |
| 5858 |
return (true); |
| 5859 |
} |
| 5860 |
} |
| 5861 |
|
| 5862 |
/* Could be obsolete common tables. */ |
| 5863 |
if ((len == strlen("ADDED")) && |
| 5864 |
(native_strncasecmp(ptr, "ADDED", len) == 0)) { |
| 5865 |
table->type = FTS_OBSOLETED_TABLE; |
| 5866 |
return (true); |
| 5867 |
} |
| 5868 |
|
| 5869 |
if ((len == strlen("STOPWORDS")) && |
| 5870 |
(native_strncasecmp(ptr, "STOPWORDS", len) == 0)) { |
| 5871 |
table->type = FTS_OBSOLETED_TABLE; |
| 5872 |
return (true); |
| 5873 |
} |
| 5874 |
|
| 5875 |
/* Try and read the index id. */ |
| 5876 |
if (!fts_read_object_id(&table->index_id, ptr)) { |
| 5877 |
return (false); |
| 5878 |
} |
| 5879 |
|
| 5880 |
/* Skip the index id. */ |
| 5881 |
ptr = static_cast<const char *>(memchr(ptr, '_', len)); |
| 5882 |
|
| 5883 |
if (ptr == nullptr) { |
| 5884 |
return (false); |
| 5885 |
} |
| 5886 |
|
| 5887 |
/* Skip the underscore. */ |
| 5888 |
++ptr; |
| 5889 |
ut_a(end >= ptr); |
| 5890 |
len = end - ptr; |
| 5891 |
|
| 5892 |
/* It's not enough to be a FTS auxiliary table name */ |
| 5893 |
if (len == 0) { |
| 5894 |
return (false); |
| 5895 |
} |
| 5896 |
|
| 5897 |
/* Search the FT index specific array. */ |
| 5898 |
for (i = 0; i < FTS_NUM_AUX_INDEX; ++i) { |
| 5899 |
if ((len == strlen(fts_get_suffix(i))) && |
| 5900 |
(strncmp(ptr, fts_get_suffix(i), len) == 0)) { |
| 5901 |
table->type = FTS_INDEX_TABLE; |
| 5902 |
return (true); |
| 5903 |
} |
| 5904 |
if ((len == strlen(fts_get_suffix_5_7(i))) && |
| 5905 |
(strncmp(ptr, fts_get_suffix_5_7(i), len) == 0)) { |
| 5906 |
table->type = FTS_INDEX_TABLE; |
| 5907 |
return (true); |
| 5908 |
} |
| 5909 |
} |
| 5910 |
|
| 5911 |
/* Other FT index specific table(s). */ |
| 5912 |
if ((len == strlen("DOC_ID")) && |
| 5913 |
(native_strncasecmp(ptr, "DOC_ID", len) == 0)) { |
| 5914 |
table->type = FTS_OBSOLETED_TABLE; |
| 5915 |
return (true); |
| 5916 |
} |
| 5917 |
} |
| 5918 |
|
| 5919 |
return (false); |
| 5920 |
} |
| 5921 |
|
| 5922 |
/** Check whether user supplied stopword table is of the right format. |
| 5923 |
Caller is responsible to hold dictionary locks. |
| 5924 |
@return the stopword column charset if qualifies */ |
| 5925 |
CHARSET_INFO *fts_valid_stopword_table( |
| 5926 |
const char *stopword_table_name) /*!< in: Stopword table |
| 5927 |
name */ |
| 5928 |
{ |
| 5929 |
dict_table_t *table; |
| 5930 |
dict_col_t *col = nullptr; |
| 5931 |
|
| 5932 |
if (!stopword_table_name) { |
| 5933 |
return (nullptr); |
| 5934 |
} |
| 5935 |
|
| 5936 |
MDL_ticket *mdl = nullptr; |
| 5937 |
THD *thd = current_thd; |
| 5938 |
table = dd_table_open_on_name(thd, &mdl, stopword_table_name, false, |
| 5939 |
DICT_ERR_IGNORE_NONE); |
| 5940 |
|
| 5941 |
if (!table) { |
| 5942 |
ib::error(ER_IB_MSG_481) |
| 5943 |
<< "User stopword table " << stopword_table_name << " does not exist."; |
| 5944 |
|
| 5945 |
return (nullptr); |
| 5946 |
} else { |
| 5947 |
const char *col_name; |
| 5948 |
|
| 5949 |
dd_table_close(table, thd, &mdl, false); |
| 5950 |
|
| 5951 |
col_name = table->get_col_name(0); |
| 5952 |
|
| 5953 |
if (ut_strcmp(col_name, "value")) { |
| 5954 |
ib::error(ER_IB_MSG_482) << "Invalid column name for stopword" |
| 5955 |
" table " |
| 5956 |
<< stopword_table_name |
| 5957 |
<< ". Its" |
| 5958 |
" first column must be named as 'value'."; |
| 5959 |
|
| 5960 |
return (nullptr); |
| 5961 |
} |
| 5962 |
|
| 5963 |
col = table->get_col(0); |
| 5964 |
|
| 5965 |
if (col->mtype != DATA_VARCHAR && col->mtype != DATA_VARMYSQL) { |
| 5966 |
ib::error(ER_IB_MSG_483) << "Invalid column type for stopword" |
| 5967 |
" table " |
| 5968 |
<< stopword_table_name |
| 5969 |
<< ". Its" |
| 5970 |
" first column must be of varchar type"; |
| 5971 |
|
| 5972 |
return (nullptr); |
| 5973 |
} |
| 5974 |
} |
| 5975 |
|
| 5976 |
ut_ad(col); |
| 5977 |
|
| 5978 |
return (fts_get_charset(col->prtype)); |
| 5979 |
} |
| 5980 |
|
| 5981 |
/** This function loads the stopword into the FTS cache. It also |
| 5982 |
records/fetches stopword configuration to/from FTS configure |
| 5983 |
table, depending on whether we are creating or reloading the |
| 5984 |
FTS. |
| 5985 |
@return true if load operation is successful */ |
| 5986 |
bool fts_load_stopword( |
| 5987 |
const dict_table_t *table, /*!< in: Table with FTS */ |
| 5988 |
trx_t *trx, /*!< in: Transactions */ |
| 5989 |
const char *global_stopword_table, /*!< in: Global stopword table |
| 5990 |
name */ |
| 5991 |
const char *session_stopword_table, /*!< in: Session stopword table |
| 5992 |
name */ |
| 5993 |
bool stopword_is_on, /*!< in: Whether stopword |
| 5994 |
option is turned on/off */ |
| 5995 |
bool reload) /*!< in: Whether it is |
| 5996 |
for reloading FTS table */ |
| 5997 |
{ |
| 5998 |
fts_table_t fts_table; |
| 5999 |
fts_string_t str; |
| 6000 |
dberr_t error = DB_SUCCESS; |
| 6001 |
ulint use_stopword; |
| 6002 |
fts_cache_t *cache; |
| 6003 |
const char *stopword_to_use = nullptr; |
| 6004 |
bool new_trx = false; |
| 6005 |
byte str_buffer[MAX_FULL_NAME_LEN + 1]; |
| 6006 |
|
| 6007 |
FTS_INIT_FTS_TABLE(&fts_table, FTS_SUFFIX_CONFIG, FTS_COMMON_TABLE, table); |
| 6008 |
|
| 6009 |
cache = table->fts->cache; |
| 6010 |
|
| 6011 |
if (!reload && !(cache->stopword_info.status & STOPWORD_NOT_INIT)) { |
| 6012 |
return true; |
| 6013 |
} |
| 6014 |
|
| 6015 |
if (!trx) { |
| 6016 |
trx = trx_allocate_for_background(); |
| 6017 |
trx->op_info = "upload FTS stopword"; |
| 6018 |
new_trx = true; |
| 6019 |
} |
| 6020 |
|
| 6021 |
/* First check whether stopword filtering is turned off */ |
| 6022 |
if (reload) { |
| 6023 |
error = |
| 6024 |
fts_config_get_ulint(trx, &fts_table, FTS_USE_STOPWORD, &use_stopword); |
| 6025 |
} else { |
| 6026 |
use_stopword = (ulint)stopword_is_on; |
| 6027 |
|
| 6028 |
error = |
| 6029 |
fts_config_set_ulint(trx, &fts_table, FTS_USE_STOPWORD, use_stopword); |
| 6030 |
} |
| 6031 |
|
| 6032 |
if (error != DB_SUCCESS) { |
| 6033 |
goto cleanup; |
| 6034 |
} |
| 6035 |
|
| 6036 |
/* If stopword is turned off, no need to continue to load the |
| 6037 |
stopword into cache, but still need to do initialization */ |
| 6038 |
if (!use_stopword) { |
| 6039 |
cache->stopword_info.status = STOPWORD_OFF; |
| 6040 |
goto cleanup; |
| 6041 |
} |
| 6042 |
|
| 6043 |
if (reload) { |
| 6044 |
/* Fetch the stopword table name from FTS config |
| 6045 |
table */ |
| 6046 |
str.f_n_char = 0; |
| 6047 |
str.f_str = str_buffer; |
| 6048 |
str.f_len = sizeof(str_buffer) - 1; |
| 6049 |
|
| 6050 |
error = |
| 6051 |
fts_config_get_value(trx, &fts_table, FTS_STOPWORD_TABLE_NAME, &str); |
| 6052 |
|
| 6053 |
if (error != DB_SUCCESS) { |
| 6054 |
goto cleanup; |
| 6055 |
} |
| 6056 |
|
| 6057 |
if (strlen((char *)str.f_str) > 0) { |
| 6058 |
stopword_to_use = (const char *)str.f_str; |
| 6059 |
} |
| 6060 |
} else { |
| 6061 |
stopword_to_use = (session_stopword_table) ? session_stopword_table |
| 6062 |
: global_stopword_table; |
| 6063 |
} |
| 6064 |
|
| 6065 |
if (stopword_to_use && |
| 6066 |
fts_load_user_stopword(stopword_to_use, &cache->stopword_info)) { |
| 6067 |
/* Save the stopword table name to the configure |
| 6068 |
table */ |
| 6069 |
if (!reload) { |
| 6070 |
str.f_n_char = 0; |
| 6071 |
str.f_str = (byte *)stopword_to_use; |
| 6072 |
str.f_len = ut_strlen(stopword_to_use); |
| 6073 |
|
| 6074 |
error = |
| 6075 |
fts_config_set_value(trx, &fts_table, FTS_STOPWORD_TABLE_NAME, &str); |
| 6076 |
} |
| 6077 |
} else { |
| 6078 |
/* Load system default stopword list */ |
| 6079 |
fts_load_default_stopword(&cache->stopword_info); |
| 6080 |
} |
| 6081 |
|
| 6082 |
cleanup: |
| 6083 |
if (new_trx) { |
| 6084 |
if (error == DB_SUCCESS) { |
| 6085 |
DEBUG_SYNC_C("fts_before_stopword_commit"); |
| 6086 |
fts_sql_commit(trx); |
| 6087 |
} else { |
| 6088 |
fts_sql_rollback(trx); |
| 6089 |
} |
| 6090 |
|
| 6091 |
trx_free_for_background(trx); |
| 6092 |
} |
| 6093 |
|
| 6094 |
if (!cache->stopword_info.cached_stopword) { |
| 6095 |
cache->stopword_info.cached_stopword = |
| 6096 |
rbt_create_arg_cmp(sizeof(fts_tokenizer_word_t), innobase_fts_text_cmp, |
| 6097 |
&my_charset_latin1); |
| 6098 |
} |
| 6099 |
|
| 6100 |
return (error == DB_SUCCESS); |
| 6101 |
} |
| 6102 |
|
| 6103 |
/** Callback function when we initialize the FTS at the start up |
| 6104 |
time. It recovers the maximum Doc IDs presented in the current table. |
| 6105 |
@return: always returns true */ |
| 6106 |
static bool fts_init_get_doc_id(void *row, /*!< in: sel_node_t* */ |
| 6107 |
void *user_arg) /*!< in: fts cache */ |
| 6108 |
{ |
| 6109 |
doc_id_t doc_id = FTS_NULL_DOC_ID; |
| 6110 |
sel_node_t *node = static_cast<sel_node_t *>(row); |
| 6111 |
que_node_t *exp = node->select_list; |
| 6112 |
fts_cache_t *cache = static_cast<fts_cache_t *>(user_arg); |
| 6113 |
|
| 6114 |
ut_ad(ib_vector_is_empty(cache->get_docs)); |
| 6115 |
|
| 6116 |
/* Copy each indexed column content into doc->text.f_str */ |
| 6117 |
if (exp) { |
| 6118 |
dfield_t *dfield = que_node_get_val(exp); |
| 6119 |
dtype_t *type = dfield_get_type(dfield); |
| 6120 |
void *data = dfield_get_data(dfield); |
| 6121 |
|
| 6122 |
ut_a(dtype_get_mtype(type) == DATA_INT); |
| 6123 |
|
| 6124 |
doc_id = static_cast<doc_id_t>( |
| 6125 |
mach_read_from_8(static_cast<const byte *>(data))); |
| 6126 |
|
| 6127 |
if (doc_id >= cache->next_doc_id) { |
| 6128 |
cache->next_doc_id = doc_id + 1; |
| 6129 |
} |
| 6130 |
} |
| 6131 |
|
| 6132 |
return true; |
| 6133 |
} |
| 6134 |
|
| 6135 |
/** Callback function when we initialize the FTS at the start up |
| 6136 |
time. It recovers Doc IDs that have not sync-ed to the auxiliary |
| 6137 |
table, and require to bring them back into FTS index. |
| 6138 |
@return: always returns true */ |
| 6139 |
static bool fts_init_recover_doc(void *row, /*!< in: sel_node_t* */ |
| 6140 |
void *user_arg) /*!< in: fts cache */ |
| 6141 |
{ |
| 6142 |
fts_doc_t doc; |
| 6143 |
ulint doc_len = 0; |
| 6144 |
ulint field_no = 0; |
| 6145 |
fts_get_doc_t *get_doc = static_cast<fts_get_doc_t *>(user_arg); |
| 6146 |
doc_id_t doc_id = FTS_NULL_DOC_ID; |
| 6147 |
sel_node_t *node = static_cast<sel_node_t *>(row); |
| 6148 |
que_node_t *exp = node->select_list; |
| 6149 |
fts_cache_t *cache = get_doc->cache; |
| 6150 |
st_mysql_ftparser *parser = get_doc->index_cache->index->parser; |
| 6151 |
|
| 6152 |
fts_doc_init(&doc); |
| 6153 |
doc.found = true; |
| 6154 |
|
| 6155 |
ut_a(cache); |
| 6156 |
|
| 6157 |
/* Copy each indexed column content into doc->text.f_str */ |
| 6158 |
while (exp) { |
| 6159 |
dfield_t *dfield = que_node_get_val(exp); |
| 6160 |
ulint len = dfield_get_len(dfield); |
| 6161 |
|
| 6162 |
if (field_no == 0) { |
| 6163 |
dtype_t *type = dfield_get_type(dfield); |
| 6164 |
void *data = dfield_get_data(dfield); |
| 6165 |
|
| 6166 |
ut_a(dtype_get_mtype(type) == DATA_INT); |
| 6167 |
|
| 6168 |
doc_id = static_cast<doc_id_t>( |
| 6169 |
mach_read_from_8(static_cast<const byte *>(data))); |
| 6170 |
|
| 6171 |
field_no++; |
| 6172 |
exp = que_node_get_next(exp); |
| 6173 |
continue; |
| 6174 |
} |
| 6175 |
|
| 6176 |
if (len == UNIV_SQL_NULL) { |
| 6177 |
exp = que_node_get_next(exp); |
| 6178 |
continue; |
| 6179 |
} |
| 6180 |
|
| 6181 |
ut_a(get_doc); |
| 6182 |
|
| 6183 |
if (!get_doc->index_cache->charset) { |
| 6184 |
get_doc->index_cache->charset = fts_get_charset(dfield->type.prtype); |
| 6185 |
} |
| 6186 |
|
| 6187 |
doc.charset = get_doc->index_cache->charset; |
| 6188 |
doc.is_ngram = get_doc->index_cache->index->is_ngram; |
| 6189 |
|
| 6190 |
if (dfield_is_ext(dfield)) { |
| 6191 |
dict_table_t *table = cache->sync->table; |
| 6192 |
|
| 6193 |
/** When a nullptr is passed for trx, it means we will |
| 6194 |
fetch the latest LOB (and no MVCC will be done). */ |
| 6195 |
doc.text.f_str = lob::btr_copy_externally_stored_field( |
| 6196 |
nullptr, get_doc->index_cache->index, &doc.text.f_len, nullptr, |
| 6197 |
static_cast<byte *>(dfield_get_data(dfield)), |
| 6198 |
dict_table_page_size(table), len, false, |
| 6199 |
static_cast<mem_heap_t *>(doc.self_heap->arg)); |
| 6200 |
} else { |
| 6201 |
doc.text.f_str = static_cast<byte *>(dfield_get_data(dfield)); |
| 6202 |
|
| 6203 |
doc.text.f_len = len; |
| 6204 |
} |
| 6205 |
|
| 6206 |
if (field_no == 1) { |
| 6207 |
fts_tokenize_document(&doc, nullptr, parser); |
| 6208 |
} else { |
| 6209 |
fts_tokenize_document_next(&doc, doc_len, nullptr, parser); |
| 6210 |
} |
| 6211 |
|
| 6212 |
exp = que_node_get_next(exp); |
| 6213 |
|
| 6214 |
doc_len += (exp) ? len + 1 : len; |
| 6215 |
|
| 6216 |
field_no++; |
| 6217 |
} |
| 6218 |
|
| 6219 |
fts_cache_add_doc(cache, get_doc->index_cache, doc_id, doc.tokens); |
| 6220 |
|
| 6221 |
fts_doc_free(&doc); |
| 6222 |
|
| 6223 |
cache->added++; |
| 6224 |
|
| 6225 |
if (doc_id >= cache->next_doc_id) { |
| 6226 |
cache->next_doc_id = doc_id + 1; |
| 6227 |
} |
| 6228 |
|
| 6229 |
return true; |
| 6230 |
} |
| 6231 |
|
| 6232 |
/** This function brings FTS index in sync when FTS index is first |
| 6233 |
used. There are documents that have not yet sync-ed to auxiliary |
| 6234 |
tables from last server abnormally shutdown, we will need to bring |
| 6235 |
such document into FTS cache before any further operations |
| 6236 |
@return true if all OK */ |
| 6237 |
bool fts_init_index(dict_table_t *table, /*!< in: Table with FTS */ |
| 6238 |
bool has_cache_lock) /*!< in: Whether we already have |
| 6239 |
cache lock */ |
| 6240 |
{ |
| 6241 |
dict_index_t *index; |
| 6242 |
doc_id_t start_doc; |
| 6243 |
fts_get_doc_t *get_doc = nullptr; |
| 6244 |
fts_cache_t *cache = table->fts->cache; |
| 6245 |
bool need_init = false; |
| 6246 |
|
| 6247 |
ut_ad(!dict_sys_mutex_own()); |
| 6248 |
|
| 6249 |
/* First check cache->get_docs is initialized */ |
| 6250 |
if (!has_cache_lock) { |
| 6251 |
rw_lock_x_lock(&cache->lock, UT_LOCATION_HERE); |
| 6252 |
} |
| 6253 |
|
| 6254 |
rw_lock_x_lock(&cache->init_lock, UT_LOCATION_HERE); |
| 6255 |
if (cache->get_docs == nullptr) { |
| 6256 |
cache->get_docs = fts_get_docs_create(cache); |
| 6257 |
} |
| 6258 |
rw_lock_x_unlock(&cache->init_lock); |
| 6259 |
|
| 6260 |
if (table->fts->fts_status & ADDED_TABLE_SYNCED) { |
| 6261 |
goto func_exit; |
| 6262 |
} |
| 6263 |
|
| 6264 |
need_init = true; |
| 6265 |
|
| 6266 |
start_doc = cache->synced_doc_id; |
| 6267 |
|
| 6268 |
if (!start_doc) { |
| 6269 |
fts_cmp_set_sync_doc_id(table, 0, true, &start_doc); |
| 6270 |
cache->synced_doc_id = start_doc; |
| 6271 |
} |
| 6272 |
|
| 6273 |
/* No FTS index, this is the case when previous FTS index |
| 6274 |
dropped, and we re-initialize the Doc ID system for subsequent |
| 6275 |
insertion */ |
| 6276 |
if (ib_vector_is_empty(cache->get_docs)) { |
| 6277 |
index = table->fts_doc_id_index; |
| 6278 |
|
| 6279 |
ut_a(index); |
| 6280 |
|
| 6281 |
fts_doc_fetch_by_doc_id(nullptr, start_doc, index, |
| 6282 |
FTS_FETCH_DOC_BY_ID_LARGE, fts_init_get_doc_id, |
| 6283 |
cache); |
| 6284 |
} else { |
| 6285 |
if (table->fts->cache->stopword_info.status & STOPWORD_NOT_INIT) { |
| 6286 |
fts_load_stopword(table, nullptr, nullptr, nullptr, true, true); |
| 6287 |
} |
| 6288 |
|
| 6289 |
for (ulint i = 0; i < ib_vector_size(cache->get_docs); ++i) { |
| 6290 |
get_doc = static_cast<fts_get_doc_t *>(ib_vector_get(cache->get_docs, i)); |
| 6291 |
|
| 6292 |
index = get_doc->index_cache->index; |
| 6293 |
|
| 6294 |
fts_doc_fetch_by_doc_id(nullptr, start_doc, index, |
| 6295 |
FTS_FETCH_DOC_BY_ID_LARGE, fts_init_recover_doc, |
| 6296 |
get_doc); |
| 6297 |
} |
| 6298 |
} |
| 6299 |
|
| 6300 |
table->fts->fts_status |= ADDED_TABLE_SYNCED; |
| 6301 |
|
| 6302 |
fts_get_docs_clear(cache->get_docs); |
| 6303 |
|
| 6304 |
func_exit: |
| 6305 |
if (!has_cache_lock) { |
| 6306 |
rw_lock_x_unlock(&cache->lock); |
| 6307 |
} |
| 6308 |
|
| 6309 |
if (need_init) { |
| 6310 |
dict_sys_mutex_enter(); |
| 6311 |
/* Register the table with the optimize thread. */ |
| 6312 |
fts_optimize_add_table(table); |
| 6313 |
dict_sys_mutex_exit(); |
| 6314 |
} |
| 6315 |
|
| 6316 |
return true; |
| 6317 |
} |
| 6318 |
|
| 6319 |
/** Rename old FTS common and aux tables with the new table_id |
| 6320 |
@param[in] old_name old name of FTS AUX table |
| 6321 |
@param[in] new_name new name of FTS AUX table |
| 6322 |
@return new fts table if success, else nullptr on failure */ |
| 6323 |
static dict_table_t *fts_upgrade_rename_aux_table_low(const char *old_name, |
| 6324 |
const char *new_name) { |
| 6325 |
dict_sys_mutex_enter(); |
| 6326 |
|
| 6327 |
dict_table_t *old_aux_table = |
| 6328 |
dict_table_open_on_name(old_name, true, false, DICT_ERR_IGNORE_NONE); |
| 6329 |
|
| 6330 |
ut_ad(old_aux_table != nullptr); |
| 6331 |
dict_table_close(old_aux_table, true, false); |
| 6332 |
dberr_t err = dict_table_rename_in_cache(old_aux_table, new_name, false); |
| 6333 |
if (err != DB_SUCCESS) { |
| 6334 |
dict_sys_mutex_exit(); |
| 6335 |
return (nullptr); |
| 6336 |
} |
| 6337 |
|
| 6338 |
dict_table_t *new_aux_table = |
| 6339 |
dict_table_open_on_name(new_name, true, false, DICT_ERR_IGNORE_NONE); |
| 6340 |
ut_ad(new_aux_table != nullptr); |
| 6341 |
dict_sys_mutex_exit(); |
| 6342 |
|
| 6343 |
return (new_aux_table); |
| 6344 |
} |
| 6345 |
|
| 6346 |
/** Rename old FTS common and aux tables with the new table_id |
| 6347 |
@param[in] old_name old name of FTS AUX table |
| 6348 |
@param[in] new_name new name of FTS AUX table |
| 6349 |
@param[in] rollback if true, do the rename back |
| 6350 |
else mark original AUX tables |
| 6351 |
evictable */ |
| 6352 |
static void fts_upgrade_rename_aux_table(const char *old_name, |
| 6353 |
const char *new_name, bool rollback) { |
| 6354 |
dict_table_t *new_table = nullptr; |
| 6355 |
|
| 6356 |
if (rollback) { |
| 6357 |
new_table = fts_upgrade_rename_aux_table_low(old_name, new_name); |
| 6358 |
|
| 6359 |
} else { |
| 6360 |
new_table = |
| 6361 |
dict_table_open_on_name(old_name, false, false, DICT_ERR_IGNORE_NONE); |
| 6362 |
} |
| 6363 |
|
| 6364 |
if (new_table == nullptr) { |
| 6365 |
return; |
| 6366 |
} |
| 6367 |
|
| 6368 |
dict_sys_mutex_enter(); |
| 6369 |
dict_table_allow_eviction(new_table); |
| 6370 |
dict_table_close(new_table, true, false); |
| 6371 |
dict_sys_mutex_exit(); |
| 6372 |
} |
| 6373 |
|
| 6374 |
/** During upgrade, tables are moved by DICT_MAX_DD_TABLES |
| 6375 |
offset, remove this offset to get 5.7 fts aux table names |
| 6376 |
@param[in] table_id 8.0 table id */ |
| 6377 |
inline table_id_t fts_upgrade_get_5_7_table_id(table_id_t table_id) { |
| 6378 |
return (table_id - DICT_MAX_DD_TABLES); |
| 6379 |
} |
| 6380 |
|
| 6381 |
/** Upgrade FTS AUX Tables. The FTS common and aux tables are |
| 6382 |
renamed because they have table_id in their name. We move table_ids |
| 6383 |
by DICT_MAX_DD_TABLES offset. Aux tables are registered into DD |
| 6384 |
after rename. |
| 6385 |
@param[in] table InnoDB table object |
| 6386 |
@return DB_SUCCESS or error code */ |
| 6387 |
dberr_t fts_upgrade_aux_tables(dict_table_t *table) { |
| 6388 |
fts_table_t fts_old_table; |
| 6389 |
|
| 6390 |
ut_ad(srv_is_upgrade_mode); |
| 6391 |
|
| 6392 |
FTS_INIT_FTS_TABLE(&fts_old_table, nullptr, FTS_COMMON_TABLE, table); |
| 6393 |
fts_table_t fts_new_table = fts_old_table; |
| 6394 |
|
| 6395 |
fts_old_table.table_id = fts_upgrade_get_5_7_table_id(fts_old_table.table_id); |
| 6396 |
|
| 6397 |
/* Rename common auxiliary tables */ |
| 6398 |
for (ulint i = 0; fts_common_tables_5_7[i] != nullptr; ++i) { |
| 6399 |
fts_old_table.suffix = fts_common_tables_5_7[i]; |
| 6400 |
|
| 6401 |
bool is_config = fts_old_table.suffix == FTS_SUFFIX_CONFIG_5_7; |
| 6402 |
char old_name[MAX_FULL_NAME_LEN]; |
| 6403 |
char new_name[MAX_FULL_NAME_LEN]; |
| 6404 |
|
| 6405 |
fts_get_table_name_5_7(&fts_old_table, old_name); |
| 6406 |
|
| 6407 |
DBUG_EXECUTE_IF("dd_upgrade", ib::info(ER_IB_MSG_484) |
| 6408 |
<< "Old fts table name is " << old_name;); |
| 6409 |
|
| 6410 |
fts_new_table.suffix = fts_common_tables[i]; |
| 6411 |
fts_get_table_name(&fts_new_table, new_name); |
| 6412 |
|
| 6413 |
DBUG_EXECUTE_IF("dd_upgrade", ib::info(ER_IB_MSG_485) |
| 6414 |
<< "New fts table name is " << new_name;); |
| 6415 |
|
| 6416 |
dict_table_t *new_table = |
| 6417 |
fts_upgrade_rename_aux_table_low(old_name, new_name); |
| 6418 |
|
| 6419 |
if (new_table == nullptr) { |
| 6420 |
return (DB_ERROR); |
| 6421 |
} |
| 6422 |
|
| 6423 |
dict_sys_mutex_enter(); |
| 6424 |
dict_table_prevent_eviction(new_table); |
| 6425 |
dict_sys_mutex_exit(); |
| 6426 |
|
| 6427 |
if (!dd_create_fts_common_table(table, new_table, is_config)) { |
| 6428 |
dict_table_close(new_table, false, false); |
| 6429 |
return (DB_FAIL); |
| 6430 |
} |
| 6431 |
dict_table_close(new_table, false, false); |
| 6432 |
} |
| 6433 |
|
| 6434 |
fts_t *fts = table->fts; |
| 6435 |
|
| 6436 |
/* Rename index specific auxiliary tables */ |
| 6437 |
for (ulint i = 0; fts->indexes != nullptr && i < ib_vector_size(fts->indexes); |
| 6438 |
++i) { |
| 6439 |
dict_index_t *index; |
| 6440 |
|
| 6441 |
index = static_cast<dict_index_t *>(ib_vector_getp(fts->indexes, i)); |
| 6442 |
|
| 6443 |
FTS_INIT_INDEX_TABLE(&fts_old_table, nullptr, FTS_INDEX_TABLE, index); |
| 6444 |
fts_new_table = fts_old_table; |
| 6445 |
|
| 6446 |
fts_old_table.table_id = |
| 6447 |
fts_upgrade_get_5_7_table_id(fts_old_table.table_id); |
| 6448 |
|
| 6449 |
for (ulint j = 0; j < FTS_NUM_AUX_INDEX; ++j) { |
| 6450 |
fts_old_table.suffix = fts_get_suffix_5_7(j); |
| 6451 |
|
| 6452 |
char old_name[MAX_FULL_NAME_LEN]; |
| 6453 |
char new_name[MAX_FULL_NAME_LEN]; |
| 6454 |
|
| 6455 |
fts_get_table_name_5_7(&fts_old_table, old_name); |
| 6456 |
|
| 6457 |
fts_new_table.suffix = fts_get_suffix(j); |
| 6458 |
fts_get_table_name(&fts_new_table, new_name); |
| 6459 |
|
| 6460 |
dict_table_t *new_table = |
| 6461 |
fts_upgrade_rename_aux_table_low(old_name, new_name); |
| 6462 |
|
| 6463 |
if (new_table == nullptr) { |
| 6464 |
return (DB_ERROR); |
| 6465 |
} |
| 6466 |
|
| 6467 |
dict_sys_mutex_enter(); |
| 6468 |
dict_table_prevent_eviction(new_table); |
| 6469 |
dict_sys_mutex_exit(); |
| 6470 |
|
| 6471 |
CHARSET_INFO *charset = fts_get_charset(index->get_field(0)->col->prtype); |
| 6472 |
|
| 6473 |
if (!dd_create_fts_index_table(table, new_table, charset)) { |
| 6474 |
dict_table_close(new_table, false, false); |
| 6475 |
return (DB_FAIL); |
| 6476 |
} |
| 6477 |
dict_table_close(new_table, false, false); |
| 6478 |
} |
| 6479 |
} |
| 6480 |
|
| 6481 |
return (DB_SUCCESS); |
| 6482 |
} |
| 6483 |
|
| 6484 |
/** Rename FTS AUX tablespace name from 8.0 format to 5.7 format. |
| 6485 |
This will be done on upgrade failure |
| 6486 |
@param[in] table parent table |
| 6487 |
@param[in] rollback rollback the rename from 8.0 to 5.7 |
| 6488 |
if true, rename to 5.7 format |
| 6489 |
if false, mark the table as evictable |
| 6490 |
@return DB_SUCCESS on success, DB_ERROR on error */ |
| 6491 |
dberr_t fts_upgrade_rename(const dict_table_t *table, bool rollback) { |
| 6492 |
fts_table_t fts_old_table; |
| 6493 |
|
| 6494 |
ut_ad(srv_is_upgrade_mode); |
| 6495 |
|
| 6496 |
FTS_INIT_FTS_TABLE(&fts_old_table, nullptr, FTS_COMMON_TABLE, table); |
| 6497 |
|
| 6498 |
fts_table_t fts_new_table = fts_old_table; |
| 6499 |
|
| 6500 |
fts_new_table.table_id = fts_upgrade_get_5_7_table_id(fts_new_table.table_id); |
| 6501 |
|
| 6502 |
/* Rename common auxiliary tables */ |
| 6503 |
for (ulint i = 0; fts_common_tables[i] != nullptr; ++i) { |
| 6504 |
fts_old_table.suffix = fts_common_tables[i]; |
| 6505 |
|
| 6506 |
char old_name[MAX_FULL_NAME_LEN]; |
| 6507 |
char new_name[MAX_FULL_NAME_LEN]; |
| 6508 |
|
| 6509 |
fts_get_table_name(&fts_old_table, old_name); |
| 6510 |
|
| 6511 |
fts_new_table.suffix = fts_common_tables_5_7[i]; |
| 6512 |
fts_get_table_name_5_7(&fts_new_table, new_name); |
| 6513 |
|
| 6514 |
fts_upgrade_rename_aux_table(old_name, new_name, rollback); |
| 6515 |
} |
| 6516 |
|
| 6517 |
fts_t *fts = table->fts; |
| 6518 |
|
| 6519 |
/* Rename index specific auxiliary tables */ |
| 6520 |
for (ulint i = 0; fts->indexes != nullptr && i < ib_vector_size(fts->indexes); |
| 6521 |
++i) { |
| 6522 |
dict_index_t *index; |
| 6523 |
|
| 6524 |
index = static_cast<dict_index_t *>(ib_vector_getp(fts->indexes, i)); |
| 6525 |
|
| 6526 |
FTS_INIT_INDEX_TABLE(&fts_old_table, nullptr, FTS_INDEX_TABLE, index); |
| 6527 |
fts_new_table = fts_old_table; |
| 6528 |
|
| 6529 |
fts_new_table.table_id = |
| 6530 |
fts_upgrade_get_5_7_table_id(fts_new_table.table_id); |
| 6531 |
|
| 6532 |
for (ulint j = 0; j < FTS_NUM_AUX_INDEX; ++j) { |
| 6533 |
fts_old_table.suffix = fts_get_suffix(j); |
| 6534 |
|
| 6535 |
char old_name[MAX_FULL_NAME_LEN]; |
| 6536 |
char new_name[MAX_FULL_NAME_LEN]; |
| 6537 |
|
| 6538 |
fts_get_table_name(&fts_old_table, old_name); |
| 6539 |
|
| 6540 |
fts_new_table.suffix = fts_get_suffix_5_7(j); |
| 6541 |
fts_get_table_name_5_7(&fts_new_table, new_name); |
| 6542 |
|
| 6543 |
fts_upgrade_rename_aux_table(old_name, new_name, rollback); |
| 6544 |
} |
| 6545 |
} |
| 6546 |
return (DB_SUCCESS); |
| 6547 |
} |