| 1 |
/**************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2010, 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 ddl/ddl0fts.cc |
| 29 |
Create Full Text Index with (parallel) merge sort. |
| 30 |
Created 10/13/2010 Jimmy Yang */ |
| 31 |
|
| 32 |
#include <sys/types.h> |
| 33 |
|
| 34 |
#include "univ.i" |
| 35 |
|
| 36 |
#include "ddl0ddl.h" |
| 37 |
#include "ddl0fts.h" |
| 38 |
#include "ddl0impl-builder.h" |
| 39 |
#include "ddl0impl-merge.h" |
| 40 |
#include "dict0dd.h" |
| 41 |
#include "fts0plugin.h" |
| 42 |
#include "lob0lob.h" |
| 43 |
#include "os0thread-create.h" |
| 44 |
#include "sql/sql_class.h" |
| 45 |
|
| 46 |
#include <current_thd.h> |
| 47 |
|
| 48 |
/** Parallel sort degree, must be a power of 2. */ |
| 49 |
ulong ddl::fts_parser_threads = 2; |
| 50 |
|
| 51 |
/** Maximum pending doc memory limit in bytes for a fts tokenization thread */ |
| 52 |
static constexpr size_t PENDING_DOC_MEMORY_LIMIT = 1000000; |
| 53 |
|
| 54 |
/** Must be a power of 2. */ |
| 55 |
static constexpr size_t DOC_ITEM_QUEUE_SIZE = 64; |
| 56 |
|
| 57 |
namespace ddl { |
| 58 |
|
| 59 |
/** status bit used for communication between parent and child thread */ |
| 60 |
enum class Thread_state : uint8_t { UNKNOWN, COMPLETE, EXITING, ABORT }; |
| 61 |
|
| 62 |
/** Row fts token for plugin parser */ |
| 63 |
struct Token { |
| 64 |
/** Token */ |
| 65 |
fts_string_t *m_text{}; |
| 66 |
|
| 67 |
/** Token position in the document */ |
| 68 |
size_t m_position{}; |
| 69 |
|
| 70 |
/** Next token link */ |
| 71 |
UT_LIST_NODE_T(Token) m_token_list; |
| 72 |
}; |
| 73 |
|
| 74 |
/** Structure stores information needed for the insertion phase of FTS |
| 75 |
parallel sort. */ |
| 76 |
struct Insert { |
| 77 |
/** Charset info */ |
| 78 |
CHARSET_INFO *m_charset{}; |
| 79 |
|
| 80 |
/** Heap */ |
| 81 |
mem_heap_t *m_heap{}; |
| 82 |
|
| 83 |
/** Whether to use smaller (4 bytes) integer for Doc ID */ |
| 84 |
bool m_doc_id_32_bit{}; |
| 85 |
|
| 86 |
/** Bulk load instance */ |
| 87 |
Btree_load *m_btr_bulk{}; |
| 88 |
|
| 89 |
/** Tuple to insert */ |
| 90 |
dtuple_t *m_tuple{}; |
| 91 |
|
| 92 |
#ifdef UNIV_DEBUG |
| 93 |
/** Auxiliary index id */ |
| 94 |
size_t m_handler_id{}; |
| 95 |
#endif /* UNIV_DEBUG */ |
| 96 |
}; |
| 97 |
|
| 98 |
/** Structure stores information from string tokenization operation */ |
| 99 |
struct Tokenize_ctx { |
| 100 |
using Token_list = UT_LIST_BASE_NODE_T(Token, m_token_list); |
| 101 |
|
| 102 |
/** Processed string length */ |
| 103 |
size_t m_processed_len{}; |
| 104 |
|
| 105 |
/** Doc start position */ |
| 106 |
size_t m_init_pos{}; |
| 107 |
|
| 108 |
/** The handler that triggered a buffer full event. */ |
| 109 |
size_t m_handler_id{}; |
| 110 |
|
| 111 |
/** Stopword list */ |
| 112 |
ib_rbt_t *m_cached_stopword{}; |
| 113 |
|
| 114 |
/** Token list. */ |
| 115 |
Token_list m_token_list{}; |
| 116 |
}; |
| 117 |
|
| 118 |
/** For parsing and sorting the documents. */ |
| 119 |
struct FTS::Parser { |
| 120 |
/** Constructor. |
| 121 |
@param[in] id Parser ID. |
| 122 |
@param[in,out] ctx DDL context. |
| 123 |
@param[in,out] dup Descriptor of FTS index being created. |
| 124 |
@param[in] doc_id_32_bit Size of the doc ID column to use for sort. */ |
| 125 |
Parser(size_t id, Context &ctx, Dup *dup, bool doc_id_32_bit) noexcept; |
| 126 |
|
| 127 |
/** Destructor. */ |
| 128 |
~Parser() noexcept; |
| 129 |
|
| 130 |
/** @return the parser ID. */ |
| 131 |
size_t id() const noexcept { return m_id; } |
| 132 |
|
| 133 |
/** Initialize the data structures. |
| 134 |
@param[in] n_threads Number of parsing threads. |
| 135 |
@return DB_SUCCESS or error code. */ |
| 136 |
dberr_t init(size_t n_threads) noexcept; |
| 137 |
|
| 138 |
/** Releases ownership of the i'th file used. |
| 139 |
@return the i'th file. */ |
| 140 |
file_t release_file(size_t id) noexcept { |
| 141 |
return std::move(m_handlers[id]->m_file); |
| 142 |
} |
| 143 |
|
| 144 |
/** Data structures for building an index. */ |
| 145 |
struct Handler { |
| 146 |
/** Constructor. |
| 147 |
@param[in,out] index Index to create. |
| 148 |
@param[in] size IO buffer size. */ |
| 149 |
explicit Handler(dict_index_t *index, size_t size) noexcept; |
| 150 |
|
| 151 |
/** Destructor. */ |
| 152 |
~Handler() noexcept; |
| 153 |
|
| 154 |
/** Aux index id. */ |
| 155 |
size_t m_id{}; |
| 156 |
|
| 157 |
/** Sort file */ |
| 158 |
file_t m_file{}; |
| 159 |
|
| 160 |
/** Sort buffer */ |
| 161 |
Key_sort_buffer m_key_buffer; |
| 162 |
|
| 163 |
/** Buffer to use for temporary file writes. */ |
| 164 |
ut::unique_ptr_aligned<byte[]> m_aligned_buffer; |
| 165 |
|
| 166 |
/** Buffer for IO to use for temporary file writes. */ |
| 167 |
IO_buffer m_io_buffer; |
| 168 |
|
| 169 |
/** Record list start offsets. */ |
| 170 |
Merge_offsets m_offsets{}; |
| 171 |
}; |
| 172 |
|
| 173 |
/** @return the parser error status. */ |
| 174 |
dberr_t get_error() const noexcept { return m_ctx.get_error(); } |
| 175 |
|
| 176 |
/** Set the error code. |
| 177 |
@param[in] err Error code to set. */ |
| 178 |
void set_error(dberr_t err) noexcept { m_ctx.set_error(err); } |
| 179 |
|
| 180 |
/** Enqueue a document to parse. |
| 181 |
@param[in,out] doc_item Document to parse. |
| 182 |
@return DB_SUCCESS or error code. */ |
| 183 |
dberr_t enqueue(FTS::Doc_item *doc_item) noexcept; |
| 184 |
|
| 185 |
/** Function performs parallel tokenization of the incoming doc strings. |
| 186 |
@param[in,out] builder Index builder instance. */ |
| 187 |
void parse(Builder *builder) noexcept; |
| 188 |
|
| 189 |
/** Set the parent thread state. |
| 190 |
@param[in] state The parent state. */ |
| 191 |
void set_parent_state(Thread_state state) noexcept { m_parent_state = state; } |
| 192 |
|
| 193 |
Diagnostics_area da{false}; |
| 194 |
|
| 195 |
private: |
| 196 |
/** Tokenize incoming text data and add to the sort buffer. |
| 197 |
@param[in] doc_id Doc ID. |
| 198 |
@param[in] doc Doc to be tokenized. |
| 199 |
@param[in] word_dtype Data structure for word col. |
| 200 |
@param[in,out] t_ctx Tokenize context. |
| 201 |
@return true if the record passed, false if out of space */ |
| 202 |
bool doc_tokenize(doc_id_t doc_id, fts_doc_t *doc, dtype_t *word_dtype, |
| 203 |
Tokenize_ctx *t_ctx) noexcept; |
| 204 |
|
| 205 |
/** Get next doc item from fts_doc_lis. |
| 206 |
@param[in,out] doc_item Doc item. */ |
| 207 |
void get_next_doc_item(FTS::Doc_item *&doc_item) noexcept; |
| 208 |
|
| 209 |
/** Tokenize by fts plugin parser. |
| 210 |
@param[in] doc To tokenize |
| 211 |
@param[in] parser Plugin parser instance. |
| 212 |
@param[in,out] t_ctx Tokenize ctx instance. */ |
| 213 |
void tokenize(fts_doc_t *doc, st_mysql_ftparser *parser, |
| 214 |
Tokenize_ctx *t_ctx) noexcept; |
| 215 |
|
| 216 |
/** FTS plugin parser 'myql_add_word' callback function for row merge. |
| 217 |
Refer to 'MYSQL_FTPARSER_PARAM' for more detail. |
| 218 |
@param[in] param Parser parameter. |
| 219 |
@param[in] word Token word. |
| 220 |
@param[in] word_len Word len. |
| 221 |
@param[in] boolean_info Boolean info. |
| 222 |
@return always 0 - plugin requirement. */ |
| 223 |
static int add_word(MYSQL_FTPARSER_PARAM *param, char *word, int word_len, |
| 224 |
MYSQL_FTPARSER_BOOLEAN_INFO *boolean_info) noexcept; |
| 225 |
|
| 226 |
private: |
| 227 |
using Docq = mpmc_bq<FTS::Doc_item *>; |
| 228 |
using Docq_ptr = std::unique_ptr<Docq, std::function<void(Docq *)>>; |
| 229 |
using Handler_ptr = std::unique_ptr<Handler, std::function<void(Handler *)>>; |
| 230 |
using Handlers = std::array<Handler_ptr, FTS_NUM_AUX_INDEX>; |
| 231 |
|
| 232 |
/** Parallel sort ID */ |
| 233 |
size_t m_id{}; |
| 234 |
|
| 235 |
/** Descriptor of FTS index. */ |
| 236 |
Dup *m_dup{}; |
| 237 |
|
| 238 |
/** DDL context. */ |
| 239 |
Context &m_ctx; |
| 240 |
|
| 241 |
/** Buffers etc. */ |
| 242 |
Handlers m_handlers{}; |
| 243 |
|
| 244 |
/** Whether to use 4 bytes instead of 8 bytes integer to store Doc ID during |
| 245 |
sort, if Doc ID will not be big enough to use 8 bytes value. */ |
| 246 |
bool m_doc_id_32_bit{}; |
| 247 |
|
| 248 |
/** Doc queue to process */ |
| 249 |
Docq_ptr m_docq{}; |
| 250 |
|
| 251 |
/** Memory used by fts_doc_list */ |
| 252 |
std::atomic_size_t m_memory_used{}; |
| 253 |
|
| 254 |
/** Parent thread state */ |
| 255 |
Thread_state m_parent_state{Thread_state::UNKNOWN}; |
| 256 |
}; |
| 257 |
|
| 258 |
struct FTS::Inserter { |
| 259 |
/** Data structures for building an index. */ |
| 260 |
struct Handler { |
| 261 |
/** Constructor. */ |
| 262 |
Handler() = default; |
| 263 |
|
| 264 |
/** Destructor. */ |
| 265 |
~Handler() = default; |
| 266 |
|
| 267 |
using Buffer = ut::unique_ptr_aligned<byte[]>; |
| 268 |
using Files = std::vector<file_t, ut::allocator<file_t>>; |
| 269 |
|
| 270 |
/** Aux index id. */ |
| 271 |
size_t m_id{}; |
| 272 |
|
| 273 |
/** Sort file */ |
| 274 |
Files m_files{}; |
| 275 |
}; |
| 276 |
|
| 277 |
/** Constructor. |
| 278 |
@param[in,out] ctx DDL context. |
| 279 |
@param[in,out] dup Descriptor of FTS index being created. |
| 280 |
@param[in] doc_id_32_bit Size of the doc ID column to use for sort. */ |
| 281 |
Inserter(Context &ctx, Dup *dup, bool doc_id_32_bit) noexcept; |
| 282 |
|
| 283 |
/** Destructor. */ |
| 284 |
~Inserter() noexcept {} |
| 285 |
|
| 286 |
/** Read sorted file(s) containing index data tuples and insert these data |
| 287 |
tuples to the index |
| 288 |
@param[in,out] builder Index builder. |
| 289 |
@param[in,out] handler Insert handler. |
| 290 |
@return DB_SUCCESS or error number */ |
| 291 |
[[nodiscard]] dberr_t insert(Builder *builder, Handler *handler) noexcept; |
| 292 |
|
| 293 |
/** Add a file to the handler for merging and inserting. |
| 294 |
@param[in] id Aux index ID. |
| 295 |
@param[in] file File to merge and insert. |
| 296 |
@return DB_SUCCESS or error code. */ |
| 297 |
dberr_t add_file(size_t id, file_t file) noexcept { |
| 298 |
auto &handler = m_handlers[id]; |
| 299 |
|
| 300 |
handler.m_files.push_back(std::move(file)); |
| 301 |
|
| 302 |
return DB_SUCCESS; |
| 303 |
} |
| 304 |
|
| 305 |
/** Write out a single word's data as new entry/entries in the INDEX table. |
| 306 |
@param[in] ins_ctx Insert context. |
| 307 |
@param[in] word Word string. |
| 308 |
@param[in] node Node columns. |
| 309 |
@return DB_SUCCUESS if insertion runs fine, otherwise error code */ |
| 310 |
dberr_t write_node(const Insert *ins_ctx, const fts_string_t *word, |
| 311 |
const fts_node_t *node) noexcept; |
| 312 |
|
| 313 |
/** Insert processed FTS data to auxiliary index tables. |
| 314 |
@param[in] ins_ctx Insert context. |
| 315 |
@param[in] word Sorted and tokenized word. |
| 316 |
@return DB_SUCCESS if insertion runs fine */ |
| 317 |
dberr_t write_word(Insert *ins_ctx, fts_tokenizer_word_t *word) noexcept; |
| 318 |
|
| 319 |
/** Read sorted FTS data files and insert data tuples to auxiliary tables. |
| 320 |
@param[in] ins_ctx Insert context. |
| 321 |
@param[in] word Last processed tokenized word. |
| 322 |
@param[in] positions Word position. |
| 323 |
@param[in] in_doc_id Last item doc id. |
| 324 |
@param[in] dtuple Entry to insert or nullptr on end. */ |
| 325 |
void insert_tuple(Insert *ins_ctx, fts_tokenizer_word_t *word, |
| 326 |
ib_vector_t *positions, doc_id_t *in_doc_id, |
| 327 |
const dtuple_t *dtuple) noexcept; |
| 328 |
|
| 329 |
using Handlers = std::array<Handler, FTS_NUM_AUX_INDEX>; |
| 330 |
|
| 331 |
/** For duplicate reporting. */ |
| 332 |
Dup *m_dup{}; |
| 333 |
|
| 334 |
/** DDL context. */ |
| 335 |
Context &m_ctx; |
| 336 |
|
| 337 |
/** 32 or 64 bit doc id. */ |
| 338 |
bool m_doc_id_32_bit{}; |
| 339 |
|
| 340 |
/** There is one handler per parser. */ |
| 341 |
Handlers m_handlers{}; |
| 342 |
}; |
| 343 |
|
| 344 |
FTS::Parser::Handler::Handler(dict_index_t *index, size_t size) noexcept |
| 345 |
: m_file(), m_key_buffer(index, size), m_aligned_buffer() {} |
| 346 |
|
| 347 |
FTS::Parser::Handler::~Handler() noexcept {} |
| 348 |
|
| 349 |
FTS::Parser::Parser(size_t id, Context &ctx, Dup *dup, |
| 350 |
bool doc_id_32_bit) noexcept |
| 351 |
: m_id(id), m_dup(dup), m_ctx(ctx), m_doc_id_32_bit(doc_id_32_bit) {} |
| 352 |
|
| 353 |
dberr_t FTS::Parser::init(size_t n_threads) noexcept { |
| 354 |
m_docq = Docq_ptr(ut::new_withkey<Docq>(ut::make_psi_memory_key(mem_key_ddl), |
| 355 |
DOC_ITEM_QUEUE_SIZE), |
| 356 |
[](Docq *docq) { ut::delete_(docq); }); |
| 357 |
|
| 358 |
if (m_docq == nullptr) { |
| 359 |
return DB_OUT_OF_MEMORY; |
| 360 |
} |
| 361 |
|
| 362 |
const auto path = thd_innodb_tmpdir(m_ctx.thd()); |
| 363 |
const auto buffer_size = m_ctx.scan_buffer_size(n_threads); |
| 364 |
|
| 365 |
for (size_t i = 0; i < FTS_NUM_AUX_INDEX; ++i) { |
| 366 |
m_handlers[i] = Handler_ptr( |
| 367 |
ut::new_withkey<Handler>(ut::make_psi_memory_key(mem_key_ddl), |
| 368 |
m_dup->m_index, buffer_size.first), |
| 369 |
[](Handler *handler) { ut::delete_(handler); }); |
| 370 |
|
| 371 |
auto &handler = m_handlers[i]; |
| 372 |
|
| 373 |
if (handler == nullptr) { |
| 374 |
return DB_OUT_OF_MEMORY; |
| 375 |
} |
| 376 |
|
| 377 |
handler->m_aligned_buffer = |
| 378 |
ut::make_unique_aligned<byte[]>(ut::make_psi_memory_key(mem_key_ddl), |
| 379 |
UNIV_SECTOR_SIZE, buffer_size.first); |
| 380 |
|
| 381 |
if (!handler->m_aligned_buffer) { |
| 382 |
return DB_OUT_OF_MEMORY; |
| 383 |
} |
| 384 |
|
| 385 |
handler->m_io_buffer = {handler->m_aligned_buffer.get(), buffer_size.first}; |
| 386 |
|
| 387 |
if (!file_create(&handler->m_file, path)) { |
| 388 |
return DB_OUT_OF_MEMORY; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
return DB_SUCCESS; |
| 393 |
} |
| 394 |
|
| 395 |
FTS::Parser::~Parser() noexcept {} |
| 396 |
|
| 397 |
dberr_t FTS::Parser::enqueue(FTS::Doc_item *doc_item) noexcept { |
| 398 |
auto err = get_error(); |
| 399 |
|
| 400 |
if (err != DB_SUCCESS) { |
| 401 |
ut::free(doc_item); |
| 402 |
return err; |
| 403 |
} |
| 404 |
|
| 405 |
const auto sz = sizeof(*doc_item) + doc_item->m_field.len; |
| 406 |
|
| 407 |
m_memory_used.fetch_add(sz, std::memory_order_relaxed); |
| 408 |
|
| 409 |
while (!m_docq->enqueue(doc_item)) { |
| 410 |
auto err = get_error(); |
| 411 |
|
| 412 |
if (err != DB_SUCCESS) { |
| 413 |
ut::free(doc_item); |
| 414 |
m_memory_used.fetch_sub(sz, std::memory_order_relaxed); |
| 415 |
return err; |
| 416 |
} |
| 417 |
|
| 418 |
std::this_thread::sleep_for(std::chrono::microseconds(1000)); |
| 419 |
} |
| 420 |
|
| 421 |
constexpr auto LIMIT = PENDING_DOC_MEMORY_LIMIT; |
| 422 |
|
| 423 |
while (m_memory_used.load(std::memory_order_relaxed) > LIMIT) { |
| 424 |
const auto err = get_error(); |
| 425 |
|
| 426 |
if (err != DB_SUCCESS) { |
| 427 |
return err; |
| 428 |
} |
| 429 |
|
| 430 |
std::this_thread::sleep_for(std::chrono::microseconds(1000)); |
| 431 |
} |
| 432 |
|
| 433 |
return DB_SUCCESS; |
| 434 |
} |
| 435 |
|
| 436 |
FTS::Inserter::Inserter(Context &ctx, Dup *dup, bool doc_id_32_bit) noexcept |
| 437 |
: m_dup(dup), m_ctx(ctx), m_doc_id_32_bit(doc_id_32_bit) { |
| 438 |
size_t i{}; |
| 439 |
for (auto &handler : m_handlers) { |
| 440 |
handler.m_id = i++; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
dict_index_t *FTS::create_index(dict_index_t *index, dict_table_t *table, |
| 445 |
bool *doc_id_32_bit) noexcept { |
| 446 |
// FIXME: This name shouldn't be hard coded here. |
| 447 |
auto new_index = dict_mem_index_create(index->table->name.m_name, |
| 448 |
"tmp_fts_idx", 0, DICT_FTS, 3); |
| 449 |
|
| 450 |
new_index->id = index->id; |
| 451 |
new_index->table = table; |
| 452 |
new_index->n_uniq = FTS_NUM_FIELDS_SORT; |
| 453 |
new_index->n_def = FTS_NUM_FIELDS_SORT; |
| 454 |
new_index->cached = true; |
| 455 |
new_index->parser = index->parser; |
| 456 |
new_index->is_ngram = index->is_ngram; |
| 457 |
|
| 458 |
auto idx_field = index->get_field(0); |
| 459 |
auto charset = fts_index_get_charset(index); |
| 460 |
|
| 461 |
/* The first field is on the Tokenized Word */ |
| 462 |
auto field = new_index->get_field(0); |
| 463 |
|
| 464 |
field->name = nullptr; |
| 465 |
field->prefix_len = 0; |
| 466 |
field->is_ascending = true; |
| 467 |
|
| 468 |
field->col = static_cast<dict_col_t *>( |
| 469 |
mem_heap_alloc(new_index->heap, sizeof(dict_col_t))); |
| 470 |
|
| 471 |
field->col->len = FTS_MAX_WORD_LEN; |
| 472 |
|
| 473 |
field->col->mtype = |
| 474 |
(charset == &my_charset_latin1) ? DATA_VARCHAR : DATA_VARMYSQL; |
| 475 |
|
| 476 |
field->col->prtype = idx_field->col->prtype | DATA_NOT_NULL; |
| 477 |
field->col->mbminmaxlen = idx_field->col->mbminmaxlen; |
| 478 |
field->fixed_len = 0; |
| 479 |
field->col->set_version_added(UINT8_UNDEFINED); |
| 480 |
field->col->set_version_dropped(UINT8_UNDEFINED); |
| 481 |
field->col->set_phy_pos(UINT32_UNDEFINED); |
| 482 |
|
| 483 |
/* Doc ID */ |
| 484 |
field = new_index->get_field(1); |
| 485 |
field->name = nullptr; |
| 486 |
field->prefix_len = 0; |
| 487 |
field->is_ascending = true; |
| 488 |
|
| 489 |
field->col = static_cast<dict_col_t *>( |
| 490 |
mem_heap_alloc(new_index->heap, sizeof(dict_col_t))); |
| 491 |
|
| 492 |
field->col->mtype = DATA_INT; |
| 493 |
*doc_id_32_bit = false; |
| 494 |
|
| 495 |
/* Check whether we can use 4 bytes instead of 8 bytes integer |
| 496 |
field to hold the Doc ID, thus reduce the overall sort size */ |
| 497 |
if (DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_ADD_DOC_ID)) { |
| 498 |
/* If Doc ID column is being added by this create |
| 499 |
index, then just check the number of rows in the table */ |
| 500 |
if (dict_table_get_n_rows(table) < MAX_DOC_ID_OPT_VAL) { |
| 501 |
*doc_id_32_bit = true; |
| 502 |
} |
| 503 |
} else { |
| 504 |
doc_id_t max_doc_id; |
| 505 |
|
| 506 |
/* If the Doc ID column is supplied by user, then |
| 507 |
check the maximum Doc ID in the table */ |
| 508 |
max_doc_id = fts_get_max_doc_id((dict_table_t *)table); |
| 509 |
|
| 510 |
if (max_doc_id && max_doc_id < MAX_DOC_ID_OPT_VAL) { |
| 511 |
*doc_id_32_bit = true; |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
if (*doc_id_32_bit) { |
| 516 |
field->col->len = sizeof(uint32_t); |
| 517 |
field->fixed_len = sizeof(uint32_t); |
| 518 |
} else { |
| 519 |
field->col->len = FTS_DOC_ID_LEN; |
| 520 |
field->fixed_len = FTS_DOC_ID_LEN; |
| 521 |
} |
| 522 |
|
| 523 |
field->col->prtype = DATA_NOT_NULL | DATA_BINARY_TYPE; |
| 524 |
|
| 525 |
field->col->mbminmaxlen = 0; |
| 526 |
field->col->set_version_added(UINT8_UNDEFINED); |
| 527 |
field->col->set_version_dropped(UINT8_UNDEFINED); |
| 528 |
field->col->set_phy_pos(UINT32_UNDEFINED); |
| 529 |
|
| 530 |
/* The third field is on the word's position in the original doc */ |
| 531 |
field = new_index->get_field(2); |
| 532 |
field->name = nullptr; |
| 533 |
field->prefix_len = 0; |
| 534 |
field->is_ascending = true; |
| 535 |
|
| 536 |
field->col = static_cast<dict_col_t *>( |
| 537 |
mem_heap_alloc(new_index->heap, sizeof(dict_col_t))); |
| 538 |
|
| 539 |
field->col->mtype = DATA_INT; |
| 540 |
field->col->len = 4; |
| 541 |
field->fixed_len = 4; |
| 542 |
field->col->prtype = DATA_NOT_NULL; |
| 543 |
field->col->mbminmaxlen = 0; |
| 544 |
field->col->set_version_added(UINT8_UNDEFINED); |
| 545 |
field->col->set_version_dropped(UINT8_UNDEFINED); |
| 546 |
field->col->set_phy_pos(UINT32_UNDEFINED); |
| 547 |
|
| 548 |
return new_index; |
| 549 |
} |
| 550 |
|
| 551 |
int FTS::Parser::add_word(MYSQL_FTPARSER_PARAM *param, char *word, int word_len, |
| 552 |
MYSQL_FTPARSER_BOOLEAN_INFO *boolean_info) noexcept { |
| 553 |
auto t_ctx = static_cast<Tokenize_ctx *>(param->mysql_ftparam); |
| 554 |
ut_a(t_ctx != nullptr); |
| 555 |
|
| 556 |
fts_string_t str; |
| 557 |
|
| 558 |
str.f_len = word_len; |
| 559 |
str.f_str = reinterpret_cast<byte *>(word); |
| 560 |
str.f_n_char = fts_get_token_size((CHARSET_INFO *)param->cs, word, word_len); |
| 561 |
|
| 562 |
ut_ad(boolean_info->position >= 0); |
| 563 |
|
| 564 |
auto ptr = static_cast<byte *>( |
| 565 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, |
| 566 |
sizeof(Token) + sizeof(fts_string_t) + str.f_len)); |
| 567 |
|
| 568 |
auto fts_token = reinterpret_cast<Token *>(ptr); |
| 569 |
|
| 570 |
fts_token->m_text = reinterpret_cast<fts_string_t *>(ptr + sizeof(Token)); |
| 571 |
|
| 572 |
fts_token->m_text->f_str = |
| 573 |
static_cast<byte *>(ptr + sizeof(Token) + sizeof(fts_string_t)); |
| 574 |
|
| 575 |
fts_token->m_text->f_len = str.f_len; |
| 576 |
|
| 577 |
fts_token->m_text->f_n_char = str.f_n_char; |
| 578 |
|
| 579 |
memcpy(fts_token->m_text->f_str, str.f_str, str.f_len); |
| 580 |
|
| 581 |
fts_token->m_position = boolean_info->position; |
| 582 |
|
| 583 |
/* Add token to list */ |
| 584 |
UT_LIST_ADD_LAST(t_ctx->m_token_list, fts_token); |
| 585 |
|
| 586 |
return 0; |
| 587 |
} |
| 588 |
|
| 589 |
void FTS::Parser::tokenize(fts_doc_t *doc, st_mysql_ftparser *parser, |
| 590 |
Tokenize_ctx *t_ctx) noexcept { |
| 591 |
MYSQL_FTPARSER_PARAM param; |
| 592 |
|
| 593 |
param.cs = doc->charset; |
| 594 |
param.mysql_ftparam = t_ctx; |
| 595 |
param.mysql_add_word = add_word; |
| 596 |
param.mode = MYSQL_FTPARSER_SIMPLE_MODE; |
| 597 |
param.length = static_cast<int>(doc->text.f_len); |
| 598 |
param.mysql_parse = fts_tokenize_document_internal; |
| 599 |
param.doc = reinterpret_cast<char *>(doc->text.f_str); |
| 600 |
|
| 601 |
int ret{}; |
| 602 |
|
| 603 |
if (parser->init != nullptr) { |
| 604 |
ret = parser->init(¶m); |
| 605 |
} |
| 606 |
|
| 607 |
if (ret == 0) { |
| 608 |
ret = parser->parse(¶m); |
| 609 |
if (ret != 0) { |
| 610 |
set_error(DB_UNSUPPORTED); |
| 611 |
} |
| 612 |
} else { |
| 613 |
set_error(DB_ERROR); |
| 614 |
} |
| 615 |
|
| 616 |
if (parser->deinit != nullptr) { |
| 617 |
ut_a(parser->init != nullptr); |
| 618 |
ret = parser->deinit(¶m); |
| 619 |
if (ret != 0) { |
| 620 |
set_error(DB_ERROR); |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
bool FTS::Parser::doc_tokenize(doc_id_t doc_id, fts_doc_t *doc, |
| 626 |
dtype_t *word_dtype, |
| 627 |
Tokenize_ctx *t_ctx) noexcept { |
| 628 |
size_t inc = 0; |
| 629 |
bool buf_full{}; |
| 630 |
fts_string_t str; |
| 631 |
fts_string_t t_str; |
| 632 |
std::array<byte, FTS_MAX_WORD_LEN + 1> str_buf; |
| 633 |
|
| 634 |
t_str.f_n_char = 0; |
| 635 |
t_ctx->m_handler_id = 0; |
| 636 |
|
| 637 |
auto parser = m_dup->m_index->parser; |
| 638 |
auto is_ngram = m_dup->m_index->is_ngram; |
| 639 |
|
| 640 |
/* When using a plug-in parser, the whole document is tokenized first |
| 641 |
by the plugin and written to t_ctx->m_token_list. The list is not |
| 642 |
empty at this point iff the buffer was filled without processing all |
| 643 |
tokens (function returned false on same document). In this case the |
| 644 |
list contains the remaining tokens to be processed. */ |
| 645 |
if (parser != nullptr) { |
| 646 |
ut_ad(t_ctx->m_processed_len == 0); |
| 647 |
|
| 648 |
if (UT_LIST_GET_LEN(t_ctx->m_token_list) == 0) { |
| 649 |
/* Parse the whole doc and cache tokens. */ |
| 650 |
tokenize(doc, parser, t_ctx); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
/* Iterate over each word string and add it with its corresponding |
| 655 |
doc id and position to sort buffer. In non-plugin mode |
| 656 |
t_ctx->m_processed_len indicates the position of the next unprocessed |
| 657 |
token. With a plugin parser it is only updated once all remaining tokens |
| 658 |
produced by the plugin are processed. */ |
| 659 |
while (t_ctx->m_processed_len < doc->text.f_len) { |
| 660 |
Token *fts_token{}; |
| 661 |
|
| 662 |
/* Get the next unprocessed token */ |
| 663 |
if (parser != nullptr) { |
| 664 |
fts_token = UT_LIST_GET_FIRST(t_ctx->m_token_list); |
| 665 |
|
| 666 |
if (fts_token != nullptr) { |
| 667 |
str.f_len = fts_token->m_text->f_len; |
| 668 |
str.f_n_char = fts_token->m_text->f_n_char; |
| 669 |
str.f_str = fts_token->m_text->f_str; |
| 670 |
} else { |
| 671 |
ut_a(UT_LIST_GET_LEN(t_ctx->m_token_list) == 0); |
| 672 |
/* Reach the end of the list */ |
| 673 |
t_ctx->m_processed_len = doc->text.f_len; |
| 674 |
break; |
| 675 |
} |
| 676 |
} else { |
| 677 |
inc = innobase_mysql_fts_get_token( |
| 678 |
doc->charset, doc->text.f_str + t_ctx->m_processed_len, |
| 679 |
doc->text.f_str + doc->text.f_len, &str); |
| 680 |
|
| 681 |
ut_a(inc > 0); |
| 682 |
} |
| 683 |
/* str now contains the token */ |
| 684 |
|
| 685 |
/* Ignore string whose character number is less than |
| 686 |
"fts_min_token_size" or more than "fts_max_token_size" */ |
| 687 |
if (!fts_check_token(&str, nullptr, is_ngram, nullptr)) { |
| 688 |
if (parser != nullptr) { |
| 689 |
UT_LIST_REMOVE(t_ctx->m_token_list, fts_token); |
| 690 |
ut::free(fts_token); |
| 691 |
} else { |
| 692 |
t_ctx->m_processed_len += inc; |
| 693 |
} |
| 694 |
continue; |
| 695 |
} |
| 696 |
|
| 697 |
t_str.f_len = |
| 698 |
innobase_fts_casedn_str(doc->charset, (char *)str.f_str, str.f_len, |
| 699 |
(char *)&str_buf, FTS_MAX_WORD_LEN + 1); |
| 700 |
|
| 701 |
t_str.f_str = (byte *)&str_buf; |
| 702 |
|
| 703 |
/* If "cached_stopword" is defined, ignore words in the stopword list */ |
| 704 |
if (!fts_check_token(&str, t_ctx->m_cached_stopword, is_ngram, |
| 705 |
doc->charset)) { |
| 706 |
if (parser != nullptr) { |
| 707 |
UT_LIST_REMOVE(t_ctx->m_token_list, fts_token); |
| 708 |
ut::free(fts_token); |
| 709 |
} else { |
| 710 |
t_ctx->m_processed_len += inc; |
| 711 |
} |
| 712 |
continue; |
| 713 |
} |
| 714 |
|
| 715 |
/* There are FTS_NUM_AUX_INDEX auxiliary tables, find |
| 716 |
out which sort buffer to put this word record in */ |
| 717 |
t_ctx->m_handler_id = |
| 718 |
fts_select_index(doc->charset, t_str.f_str, t_str.f_len); |
| 719 |
|
| 720 |
auto key_buffer = &m_handlers[t_ctx->m_handler_id]->m_key_buffer; |
| 721 |
|
| 722 |
ut_a(t_ctx->m_handler_id < FTS_NUM_AUX_INDEX); |
| 723 |
|
| 724 |
dfield_t *fields; |
| 725 |
auto field = fields = key_buffer->alloc(FTS_NUM_FIELDS_SORT); |
| 726 |
key_buffer->m_dtuples.push_back(fields); |
| 727 |
|
| 728 |
/* The first field is the tokenized word */ |
| 729 |
dfield_set_data(field, t_str.f_str, t_str.f_len); |
| 730 |
auto len = dfield_get_len(field); |
| 731 |
|
| 732 |
field->type.mtype = word_dtype->mtype; |
| 733 |
field->type.prtype = word_dtype->prtype | DATA_NOT_NULL; |
| 734 |
|
| 735 |
/* Variable length field, set to max size. */ |
| 736 |
field->type.len = FTS_MAX_WORD_LEN; |
| 737 |
field->type.mbminmaxlen = word_dtype->mbminmaxlen; |
| 738 |
|
| 739 |
auto cur_len = len; |
| 740 |
|
| 741 |
++field; |
| 742 |
|
| 743 |
/* The second field is the Doc ID */ |
| 744 |
|
| 745 |
doc_id_t write_doc_id; |
| 746 |
uint32_t doc_id_32_bit; |
| 747 |
|
| 748 |
if (!m_doc_id_32_bit) { |
| 749 |
fts_write_doc_id((byte *)&write_doc_id, doc_id); |
| 750 |
dfield_set_data(field, &write_doc_id, sizeof(write_doc_id)); |
| 751 |
} else { |
| 752 |
mach_write_to_4((byte *)&doc_id_32_bit, (uint32_t)doc_id); |
| 753 |
dfield_set_data(field, &doc_id_32_bit, sizeof(doc_id_32_bit)); |
| 754 |
} |
| 755 |
|
| 756 |
len = field->len; |
| 757 |
ut_a(len == FTS_DOC_ID_LEN || len == sizeof(uint32_t)); |
| 758 |
|
| 759 |
field->type.len = len; |
| 760 |
field->type.mbminmaxlen = 0; |
| 761 |
field->type.mtype = DATA_INT; |
| 762 |
field->type.prtype = DATA_NOT_NULL | DATA_BINARY_TYPE; |
| 763 |
|
| 764 |
cur_len += len; |
| 765 |
|
| 766 |
++field; |
| 767 |
|
| 768 |
uint32_t position; |
| 769 |
|
| 770 |
{ |
| 771 |
auto p = reinterpret_cast<byte *>(&position); |
| 772 |
/* The third field is the position. */ |
| 773 |
if (parser != nullptr) { |
| 774 |
mach_write_to_4(p, fts_token->m_position + t_ctx->m_init_pos); |
| 775 |
} else { |
| 776 |
const auto n = |
| 777 |
t_ctx->m_processed_len + inc - str.f_len + t_ctx->m_init_pos; |
| 778 |
mach_write_to_4(p, n); |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
dfield_set_data(field, &position, sizeof(position)); |
| 783 |
len = dfield_get_len(field); |
| 784 |
ut_a(len == sizeof(uint32_t)); |
| 785 |
|
| 786 |
field->type.len = len; |
| 787 |
field->type.mbminmaxlen = 0; |
| 788 |
field->type.mtype = DATA_INT; |
| 789 |
field->type.prtype = DATA_NOT_NULL; |
| 790 |
cur_len += len; |
| 791 |
|
| 792 |
/* One variable length column, word with its length less than |
| 793 |
fts_max_token_size, add one extra size and one extra byte. |
| 794 |
|
| 795 |
Since the max length for FTS token now is larger than 255, so we will |
| 796 |
need to signify length byte itself, so only 1 to 128 bytes can be used |
| 797 |
for 1 bytes, larger than that 2 bytes. */ |
| 798 |
cur_len += t_str.f_len < 128 ? 2 : 3; |
| 799 |
|
| 800 |
if (!key_buffer->will_fit(cur_len)) { |
| 801 |
key_buffer->pop_unfinished_tuple(); |
| 802 |
buf_full = true; |
| 803 |
break; |
| 804 |
} |
| 805 |
|
| 806 |
key_buffer->deep_copy(FTS_NUM_FIELDS_SORT, cur_len); |
| 807 |
|
| 808 |
if (parser != nullptr) { |
| 809 |
UT_LIST_REMOVE(t_ctx->m_token_list, fts_token); |
| 810 |
ut::free(fts_token); |
| 811 |
} else { |
| 812 |
t_ctx->m_processed_len += inc; |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
if (!buf_full) { |
| 817 |
/* we pad one byte between text across two fields */ |
| 818 |
t_ctx->m_init_pos += doc->text.f_len + 1; |
| 819 |
} |
| 820 |
|
| 821 |
return !buf_full; |
| 822 |
} |
| 823 |
|
| 824 |
void FTS::Parser::get_next_doc_item(FTS::Doc_item *&doc_item) noexcept { |
| 825 |
if (doc_item != nullptr) { |
| 826 |
ut::free(doc_item); |
| 827 |
doc_item = nullptr; |
| 828 |
} |
| 829 |
|
| 830 |
if (!m_docq->dequeue(doc_item)) { |
| 831 |
return; |
| 832 |
} |
| 833 |
|
| 834 |
if (doc_item != nullptr) { |
| 835 |
const auto sz = sizeof(FTS::Doc_item) + doc_item->m_field.len; |
| 836 |
ut_a(m_memory_used >= sz); |
| 837 |
|
| 838 |
m_memory_used.fetch_sub(sz, std::memory_order_relaxed); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
void FTS::Parser::parse(Builder *builder) noexcept { |
| 843 |
fts_doc_t doc; |
| 844 |
size_t retried{}; |
| 845 |
dtype_t word_dtype; |
| 846 |
uint64_t total_rec{}; |
| 847 |
dberr_t err{DB_SUCCESS}; |
| 848 |
Tokenize_ctx t_ctx{}; |
| 849 |
size_t n_doc_processed{}; |
| 850 |
FTS::Doc_item *doc_item{}; |
| 851 |
|
| 852 |
auto table = m_ctx.new_table(); |
| 853 |
auto old_table = m_ctx.old_table(); |
| 854 |
auto blob_heap = mem_heap_create(512, UT_LOCATION_HERE); |
| 855 |
|
| 856 |
memset(&doc, 0, sizeof(doc)); |
| 857 |
|
| 858 |
doc.charset = fts_index_get_charset(m_dup->m_index); |
| 859 |
|
| 860 |
auto idx_field = m_dup->m_index->get_field(0); |
| 861 |
|
| 862 |
word_dtype.prtype = idx_field->col->prtype; |
| 863 |
word_dtype.mbminmaxlen = idx_field->col->mbminmaxlen; |
| 864 |
|
| 865 |
word_dtype.mtype = |
| 866 |
(doc.charset == &my_charset_latin1) ? DATA_VARCHAR : DATA_VARMYSQL; |
| 867 |
|
| 868 |
const page_size_t &page_size = dict_table_page_size(table); |
| 869 |
|
| 870 |
get_next_doc_item(doc_item); |
| 871 |
|
| 872 |
t_ctx.m_cached_stopword = table->fts->cache->stopword_info.cached_stopword; |
| 873 |
|
| 874 |
auto processed{true}; |
| 875 |
|
| 876 |
auto clean_up = [&](dberr_t err) { |
| 877 |
mem_heap_free(blob_heap); |
| 878 |
|
| 879 |
#ifdef UNIV_DEBUG |
| 880 |
if (Sync_point::enabled(m_ctx.thd(), "ddl_fts_write_failure")) { |
| 881 |
err = DB_TEMP_FILE_WRITE_FAIL; |
| 882 |
}; |
| 883 |
#endif |
| 884 |
|
| 885 |
if (err != DB_SUCCESS) { |
| 886 |
builder->set_error(err); |
| 887 |
set_error(err); |
| 888 |
} |
| 889 |
|
| 890 |
if (!m_docq->empty()) { |
| 891 |
/* Child can exit either with error or told by parent. */ |
| 892 |
ut_a(err != DB_SUCCESS || m_parent_state == Thread_state::ABORT); |
| 893 |
} |
| 894 |
|
| 895 |
/* Free fts doc list in case of err. */ |
| 896 |
do { |
| 897 |
get_next_doc_item(doc_item); |
| 898 |
} while (doc_item != nullptr); |
| 899 |
}; |
| 900 |
|
| 901 |
auto handle_tail_end = [&]() { |
| 902 |
/* Do a final sort of the last (or latest) batch of records |
| 903 |
in block memory. Flush them to temp file if records cannot |
| 904 |
be held in one block of memory. */ |
| 905 |
for (size_t i = 0; i < FTS_NUM_AUX_INDEX; ++i) { |
| 906 |
auto &handler = m_handlers[i]; |
| 907 |
|
| 908 |
if (!handler->m_key_buffer.empty()) { |
| 909 |
auto key_buffer = &handler->m_key_buffer; |
| 910 |
auto io_buffer = handler->m_io_buffer; |
| 911 |
|
| 912 |
const auto n_tuples = key_buffer->size(); |
| 913 |
|
| 914 |
key_buffer->sort(nullptr); |
| 915 |
|
| 916 |
auto &file = handler->m_file; |
| 917 |
handler->m_offsets.push_back(file.m_size); |
| 918 |
|
| 919 |
auto persistor = [&](IO_buffer io_buffer) -> dberr_t { |
| 920 |
return builder->append(file, io_buffer); |
| 921 |
}; |
| 922 |
|
| 923 |
err = key_buffer->serialize(io_buffer, persistor); |
| 924 |
|
| 925 |
if (err != DB_SUCCESS) { |
| 926 |
clean_up(DB_TEMP_FILE_WRITE_FAIL); |
| 927 |
return; |
| 928 |
} |
| 929 |
|
| 930 |
key_buffer->clear(); |
| 931 |
|
| 932 |
file.m_n_recs += n_tuples; |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
for (size_t i = 0; i < FTS_NUM_AUX_INDEX; i++) { |
| 937 |
auto &handler = m_handlers[i]; |
| 938 |
|
| 939 |
if (handler->m_offsets.size() <= 1) { |
| 940 |
continue; |
| 941 |
} |
| 942 |
|
| 943 |
Merge_file_sort::Context merge_ctx; |
| 944 |
|
| 945 |
merge_ctx.m_dup = m_dup; |
| 946 |
merge_ctx.m_file = &handler->m_file; |
| 947 |
|
| 948 |
merge_ctx.m_n_threads = |
| 949 |
m_ctx.m_fts.m_ptr->get_n_parsers() * FTS_NUM_AUX_INDEX; |
| 950 |
|
| 951 |
Merge_file_sort merge_file_sort{&merge_ctx}; |
| 952 |
|
| 953 |
err = merge_file_sort.sort(builder, handler->m_offsets); |
| 954 |
|
| 955 |
if (err != DB_SUCCESS) { |
| 956 |
clean_up(err); |
| 957 |
return; |
| 958 |
} |
| 959 |
|
| 960 |
total_rec += handler->m_file.m_n_recs; |
| 961 |
} |
| 962 |
|
| 963 |
clean_up(DB_SUCCESS); |
| 964 |
}; |
| 965 |
|
| 966 |
/* Items provided by get_next_doc_item are individual fields |
| 967 |
of a potentially multi-field document. Subsequent fields in |
| 968 |
multi-field document must arrive consecutively, not |
| 969 |
interleaved by fields from other documents; last_doc_id |
| 970 |
is used to determine whether a new item is part of the same |
| 971 |
document as the previous one. */ |
| 972 |
doc_id_t last_doc_id{}; |
| 973 |
|
| 974 |
/* get_next_doc_item() reads items from a non-blocking queue. |
| 975 |
It may therefore yield a nullptr result even when there are more |
| 976 |
documents to be read. The inner loop reads doc items from the |
| 977 |
queue as long as they are available and there is space to store |
| 978 |
the item on the buffer. When either of these conditions is not |
| 979 |
met, control will break out to the outer loop, which handles |
| 980 |
buffer flushing and polling for more data. */ |
| 981 |
for (;;) { |
| 982 |
while (doc_item != nullptr) { |
| 983 |
const dfield_t *dfield = &doc_item->m_field; |
| 984 |
|
| 985 |
last_doc_id = doc_item->m_doc_id; |
| 986 |
|
| 987 |
ut_a(dfield->data != nullptr && dfield_get_len(dfield) != UNIV_SQL_NULL); |
| 988 |
|
| 989 |
/* If finish processing the last item, update "doc" with strings in the |
| 990 |
doc_item, otherwise continue processing last item. */ |
| 991 |
if (processed) { |
| 992 |
auto data = static_cast<byte *>(dfield_get_data(dfield)); |
| 993 |
auto data_len = dfield_get_len(dfield); |
| 994 |
|
| 995 |
if (dfield_is_ext(dfield)) { |
| 996 |
auto clust_index = old_table->first_index(); |
| 997 |
|
| 998 |
doc.text.f_str = lob::btr_copy_externally_stored_field( |
| 999 |
nullptr, clust_index, &doc.text.f_len, nullptr, data, page_size, |
| 1000 |
data_len, false, blob_heap); |
| 1001 |
} else { |
| 1002 |
doc.text.f_str = data; |
| 1003 |
doc.text.f_len = data_len; |
| 1004 |
} |
| 1005 |
|
| 1006 |
doc.tokens = nullptr; |
| 1007 |
t_ctx.m_processed_len = 0; |
| 1008 |
} else { |
| 1009 |
/* Finish processing the current "doc", continue processing it. */ |
| 1010 |
ut_a(doc.text.f_str != nullptr); |
| 1011 |
ut_a(t_ctx.m_processed_len < doc.text.f_len); |
| 1012 |
} |
| 1013 |
|
| 1014 |
processed = doc_tokenize(doc_item->m_doc_id, &doc, &word_dtype, &t_ctx); |
| 1015 |
|
| 1016 |
/* Current sort buffer full, need to recycle */ |
| 1017 |
if (!processed) { |
| 1018 |
ut_a(t_ctx.m_processed_len < doc.text.f_len); |
| 1019 |
break; |
| 1020 |
} |
| 1021 |
|
| 1022 |
++n_doc_processed; |
| 1023 |
|
| 1024 |
mem_heap_empty(blob_heap); |
| 1025 |
|
| 1026 |
get_next_doc_item(doc_item); |
| 1027 |
|
| 1028 |
if (doc_item != nullptr && last_doc_id != doc_item->m_doc_id) { |
| 1029 |
t_ctx.m_init_pos = 0; |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
auto &handler = m_handlers[t_ctx.m_handler_id]; |
| 1034 |
|
| 1035 |
/* If we run out of current sort buffer, need to sort and flush the |
| 1036 |
sort buffer to disk. */ |
| 1037 |
if (handler->m_key_buffer.size() > 0 && !processed) { |
| 1038 |
auto &file = handler->m_file; |
| 1039 |
auto key_buffer = &handler->m_key_buffer; |
| 1040 |
auto io_buffer = handler->m_io_buffer; |
| 1041 |
const auto n_tuples = key_buffer->size(); |
| 1042 |
|
| 1043 |
key_buffer->sort(nullptr); |
| 1044 |
|
| 1045 |
handler->m_offsets.push_back(file.m_size); |
| 1046 |
|
| 1047 |
auto persistor = [&](IO_buffer io_buffer) -> dberr_t { |
| 1048 |
return builder->append(file, io_buffer); |
| 1049 |
}; |
| 1050 |
|
| 1051 |
err = key_buffer->serialize(io_buffer, persistor); |
| 1052 |
|
| 1053 |
if (err != DB_SUCCESS) { |
| 1054 |
clean_up(DB_TEMP_FILE_WRITE_FAIL); |
| 1055 |
return; |
| 1056 |
} |
| 1057 |
|
| 1058 |
key_buffer->clear(); |
| 1059 |
|
| 1060 |
file.m_n_recs += n_tuples; |
| 1061 |
|
| 1062 |
ut_a(doc_item != nullptr); |
| 1063 |
continue; |
| 1064 |
} |
| 1065 |
|
| 1066 |
/* Parent done scanning, and if finish processing all the docs, exit. */ |
| 1067 |
if (m_parent_state == Thread_state::COMPLETE) { |
| 1068 |
if (m_docq->empty()) { |
| 1069 |
handle_tail_end(); |
| 1070 |
break; |
| 1071 |
} |
| 1072 |
|
| 1073 |
if (retried > 10000) { |
| 1074 |
ut_a(doc_item == nullptr); |
| 1075 |
/* retied too many times and cannot get new record */ |
| 1076 |
ib::error(ER_IB_MSG_930) |
| 1077 |
<< "FTS parallel sort processed " << n_doc_processed |
| 1078 |
<< " records, the sort queue is not empty but tokenizer" |
| 1079 |
<< " cannot dequeue records."; |
| 1080 |
handle_tail_end(); |
| 1081 |
break; |
| 1082 |
} |
| 1083 |
} else if (m_parent_state == Thread_state::ABORT) { |
| 1084 |
/* Parent abort. */ |
| 1085 |
clean_up(err); |
| 1086 |
break; |
| 1087 |
} |
| 1088 |
|
| 1089 |
if (doc_item == nullptr) { |
| 1090 |
std::this_thread::yield(); |
| 1091 |
} |
| 1092 |
|
| 1093 |
get_next_doc_item(doc_item); |
| 1094 |
|
| 1095 |
if (doc_item != nullptr) { |
| 1096 |
if (last_doc_id != doc_item->m_doc_id) { |
| 1097 |
t_ctx.m_init_pos = 0; |
| 1098 |
} |
| 1099 |
|
| 1100 |
retried = 0; |
| 1101 |
} else if (m_parent_state == Thread_state::COMPLETE) { |
| 1102 |
++retried; |
| 1103 |
} |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
dberr_t FTS::Inserter::write_node(const Insert *ins_ctx, |
| 1108 |
const fts_string_t *word, |
| 1109 |
const fts_node_t *node) noexcept { |
| 1110 |
auto tuple = ins_ctx->m_tuple; |
| 1111 |
|
| 1112 |
/* We don't do a deep copy. Be careful moving these around. */ |
| 1113 |
uint32_t doc_count{}; |
| 1114 |
doc_id_t last_doc_id{}; |
| 1115 |
doc_id_t first_doc_id{}; |
| 1116 |
|
| 1117 |
{ |
| 1118 |
/* The first field is the tokenized word */ |
| 1119 |
auto field = dtuple_get_nth_field(tuple, 0); |
| 1120 |
dfield_set_data(field, word->f_str, word->f_len); |
| 1121 |
} |
| 1122 |
|
| 1123 |
{ |
| 1124 |
/* The second field is first_doc_id */ |
| 1125 |
auto field = dtuple_get_nth_field(tuple, 1); |
| 1126 |
fts_write_doc_id((byte *)&first_doc_id, node->first_doc_id); |
| 1127 |
dfield_set_data(field, &first_doc_id, sizeof(first_doc_id)); |
| 1128 |
} |
| 1129 |
|
| 1130 |
{ |
| 1131 |
/* The third and fourth fields(TRX_ID, ROLL_PTR) are filled already.*/ |
| 1132 |
/* The fifth field is last_doc_id */ |
| 1133 |
auto field = dtuple_get_nth_field(tuple, 4); |
| 1134 |
fts_write_doc_id((byte *)&last_doc_id, node->last_doc_id); |
| 1135 |
dfield_set_data(field, &last_doc_id, sizeof(last_doc_id)); |
| 1136 |
} |
| 1137 |
|
| 1138 |
{ |
| 1139 |
/* The sixth field is doc_count */ |
| 1140 |
auto field = dtuple_get_nth_field(tuple, 5); |
| 1141 |
mach_write_to_4((byte *)&doc_count, (uint32_t)node->doc_count); |
| 1142 |
dfield_set_data(field, &doc_count, sizeof(doc_count)); |
| 1143 |
} |
| 1144 |
|
| 1145 |
{ |
| 1146 |
/* The seventh field is ilist */ |
| 1147 |
auto field = dtuple_get_nth_field(tuple, 6); |
| 1148 |
dfield_set_data(field, node->ilist, node->ilist_size); |
| 1149 |
} |
| 1150 |
|
| 1151 |
return ins_ctx->m_btr_bulk->insert(tuple, 0); |
| 1152 |
} |
| 1153 |
|
| 1154 |
dberr_t FTS::Inserter::write_word(Insert *ins_ctx, |
| 1155 |
fts_tokenizer_word_t *word) noexcept { |
| 1156 |
dberr_t ret{DB_SUCCESS}; |
| 1157 |
|
| 1158 |
ut_ad(ins_ctx->m_handler_id == fts_select_index(ins_ctx->m_charset, |
| 1159 |
word->text.f_str, |
| 1160 |
word->text.f_len)); |
| 1161 |
|
| 1162 |
/* Pop out each fts_node in word->nodes write them to auxiliary table */ |
| 1163 |
for (size_t i = 0; i < ib_vector_size(word->nodes); i++) { |
| 1164 |
auto fts_node = static_cast<fts_node_t *>(ib_vector_get(word->nodes, i)); |
| 1165 |
|
| 1166 |
auto err = write_node(ins_ctx, &word->text, fts_node); |
| 1167 |
|
| 1168 |
if (err != DB_SUCCESS) { |
| 1169 |
ib::error(ER_IB_MSG_931) << "Failed to write word " << word->text.f_str |
| 1170 |
<< " to FTS auxiliary" |
| 1171 |
" index table, error (" |
| 1172 |
<< ut_strerr(err) << ")"; |
| 1173 |
ret = err; |
| 1174 |
} else { |
| 1175 |
ut_ad(ins_ctx->m_btr_bulk->get_n_recs() > 0); |
| 1176 |
} |
| 1177 |
|
| 1178 |
ut::free(fts_node->ilist); |
| 1179 |
fts_node->ilist = nullptr; |
| 1180 |
} |
| 1181 |
|
| 1182 |
ib_vector_reset(word->nodes); |
| 1183 |
|
| 1184 |
return ret; |
| 1185 |
} |
| 1186 |
|
| 1187 |
void FTS::Inserter::insert_tuple(Insert *ins_ctx, fts_tokenizer_word_t *word, |
| 1188 |
ib_vector_t *positions, doc_id_t *in_doc_id, |
| 1189 |
const dtuple_t *dtuple) noexcept { |
| 1190 |
fts_node_t *fts_node; |
| 1191 |
|
| 1192 |
/* Get fts_node for the FTS auxiliary INDEX table */ |
| 1193 |
if (ib_vector_size(word->nodes) > 0) { |
| 1194 |
fts_node = static_cast<fts_node_t *>(ib_vector_last(word->nodes)); |
| 1195 |
} else { |
| 1196 |
fts_node = nullptr; |
| 1197 |
} |
| 1198 |
|
| 1199 |
if (fts_node == nullptr || fts_node->ilist_size > FTS_ILIST_MAX_SIZE) { |
| 1200 |
fts_node = static_cast<fts_node_t *>(ib_vector_push(word->nodes, nullptr)); |
| 1201 |
|
| 1202 |
memset(fts_node, 0x0, sizeof(*fts_node)); |
| 1203 |
} |
| 1204 |
|
| 1205 |
/* If dtuple == nullptr, this is the last word to be processed */ |
| 1206 |
if (dtuple == nullptr) { |
| 1207 |
if (fts_node != nullptr && ib_vector_size(positions) > 0) { |
| 1208 |
fts_cache_node_add_positions(nullptr, fts_node, *in_doc_id, positions); |
| 1209 |
|
| 1210 |
/* Write out the current word */ |
| 1211 |
write_word(ins_ctx, word); |
| 1212 |
} |
| 1213 |
|
| 1214 |
return; |
| 1215 |
} |
| 1216 |
|
| 1217 |
/* Get the first field for the tokenized word */ |
| 1218 |
auto dfield = dtuple_get_nth_field(dtuple, 0); |
| 1219 |
|
| 1220 |
fts_string_t token_word; |
| 1221 |
|
| 1222 |
token_word.f_n_char = 0; |
| 1223 |
token_word.f_len = dfield->len; |
| 1224 |
token_word.f_str = static_cast<byte *>(dfield_get_data(dfield)); |
| 1225 |
|
| 1226 |
if (word->text.f_str == nullptr) { |
| 1227 |
fts_string_dup(&word->text, &token_word, ins_ctx->m_heap); |
| 1228 |
} |
| 1229 |
|
| 1230 |
/* Compare to the last word, to see if they are the same word */ |
| 1231 |
if (innobase_fts_text_cmp(ins_ctx->m_charset, &word->text, &token_word) != |
| 1232 |
0) { |
| 1233 |
/* Getting a new word, flush the last position info |
| 1234 |
for the current word in fts_node */ |
| 1235 |
if (ib_vector_size(positions) > 0) { |
| 1236 |
fts_cache_node_add_positions(nullptr, fts_node, *in_doc_id, positions); |
| 1237 |
} |
| 1238 |
|
| 1239 |
/* Write out the current word */ |
| 1240 |
write_word(ins_ctx, word); |
| 1241 |
|
| 1242 |
/* Copy the new word */ |
| 1243 |
fts_string_dup(&word->text, &token_word, ins_ctx->m_heap); |
| 1244 |
|
| 1245 |
const auto n_item = ib_vector_size(positions); |
| 1246 |
|
| 1247 |
/* Clean up position queue */ |
| 1248 |
for (size_t i = 0; i < n_item; i++) { |
| 1249 |
ib_vector_pop(positions); |
| 1250 |
} |
| 1251 |
|
| 1252 |
/* Reset Doc ID */ |
| 1253 |
*in_doc_id = 0; |
| 1254 |
memset(fts_node, 0x0, sizeof(*fts_node)); |
| 1255 |
} |
| 1256 |
|
| 1257 |
/* Get the word's Doc ID */ |
| 1258 |
dfield = dtuple_get_nth_field(dtuple, 1); |
| 1259 |
|
| 1260 |
doc_id_t doc_id; |
| 1261 |
|
| 1262 |
{ |
| 1263 |
const auto ptr = static_cast<byte *>(dfield_get_data(dfield)); |
| 1264 |
|
| 1265 |
if (ins_ctx->m_doc_id_32_bit == 0) { |
| 1266 |
doc_id = fts_read_doc_id(ptr); |
| 1267 |
} else { |
| 1268 |
doc_id = mach_read_from_4(ptr); |
| 1269 |
} |
| 1270 |
} |
| 1271 |
|
| 1272 |
/* Get the word's position info. */ |
| 1273 |
dfield = dtuple_get_nth_field(dtuple, 2); |
| 1274 |
|
| 1275 |
const auto ptr = static_cast<byte *>(dfield_get_data(dfield)); |
| 1276 |
const doc_id_t position = mach_read_from_4(ptr); |
| 1277 |
|
| 1278 |
/* If this is the same word as the last word, and they |
| 1279 |
have the same Doc ID, we just need to add its position |
| 1280 |
info. Otherwise, we will flush position info to the |
| 1281 |
fts_node and initiate a new position vector */ |
| 1282 |
if (*in_doc_id == 0 || *in_doc_id == doc_id) { |
| 1283 |
ib_vector_push(positions, &position); |
| 1284 |
} else { |
| 1285 |
const auto n_pos = ib_vector_size(positions); |
| 1286 |
|
| 1287 |
fts_cache_node_add_positions(nullptr, fts_node, *in_doc_id, positions); |
| 1288 |
|
| 1289 |
for (size_t i = 0; i < n_pos; i++) { |
| 1290 |
ib_vector_pop(positions); |
| 1291 |
} |
| 1292 |
|
| 1293 |
ib_vector_push(positions, &position); |
| 1294 |
} |
| 1295 |
|
| 1296 |
/* Record the current Doc ID */ |
| 1297 |
*in_doc_id = doc_id; |
| 1298 |
} |
| 1299 |
|
| 1300 |
dberr_t FTS::Inserter::insert(Builder *builder, |
| 1301 |
Inserter::Handler *handler) noexcept { |
| 1302 |
ut_a(!handler->m_files.empty()); |
| 1303 |
|
| 1304 |
/* We use the insert query graph as the dummy graph needed in the |
| 1305 |
row module call */ |
| 1306 |
|
| 1307 |
auto trx = trx_allocate_for_background(); |
| 1308 |
|
| 1309 |
trx_start_if_not_started(trx, true, UT_LOCATION_HERE); |
| 1310 |
|
| 1311 |
trx->op_info = "inserting index entries"; |
| 1312 |
|
| 1313 |
Insert ins_ctx; |
| 1314 |
|
| 1315 |
ins_ctx.m_doc_id_32_bit = m_doc_id_32_bit; |
| 1316 |
|
| 1317 |
auto tuple_heap = mem_heap_create(512, UT_LOCATION_HERE); |
| 1318 |
|
| 1319 |
auto index = m_dup->m_index; |
| 1320 |
|
| 1321 |
ins_ctx.m_heap = tuple_heap; |
| 1322 |
ins_ctx.m_charset = fts_index_get_charset(index); |
| 1323 |
|
| 1324 |
/* Initialize related variables if creating FTS indexes */ |
| 1325 |
auto heap_alloc = ib_heap_allocator_create(tuple_heap); |
| 1326 |
|
| 1327 |
fts_tokenizer_word_t new_word; |
| 1328 |
|
| 1329 |
memset(&new_word, 0, sizeof(new_word)); |
| 1330 |
|
| 1331 |
new_word.nodes = ib_vector_create(heap_alloc, sizeof(fts_node_t), 4); |
| 1332 |
|
| 1333 |
dict_table_t *aux_table{}; |
| 1334 |
{ |
| 1335 |
fts_table_t fts_table; |
| 1336 |
|
| 1337 |
fts_table.index_id = index->id; |
| 1338 |
fts_table.table_id = m_ctx.new_table()->id; |
| 1339 |
fts_table.table = index->table; |
| 1340 |
fts_table.type = FTS_INDEX_TABLE; |
| 1341 |
fts_table.suffix = fts_get_suffix(handler->m_id); |
| 1342 |
fts_table.parent = index->table->name.m_name; |
| 1343 |
|
| 1344 |
std::array<char, MAX_FULL_NAME_LEN> aux_table_name; |
| 1345 |
|
| 1346 |
/* Get aux index */ |
| 1347 |
fts_get_table_name(&fts_table, aux_table_name.data()); |
| 1348 |
|
| 1349 |
aux_table = dd_table_open_on_name(nullptr, nullptr, aux_table_name.data(), |
| 1350 |
false, DICT_ERR_IGNORE_NONE); |
| 1351 |
} |
| 1352 |
|
| 1353 |
ut_ad(aux_table != nullptr); |
| 1354 |
dd_table_close(aux_table, nullptr, nullptr, false); |
| 1355 |
|
| 1356 |
auto observer = m_ctx.flush_observer(); |
| 1357 |
auto aux_index = aux_table->first_index(); |
| 1358 |
|
| 1359 |
auto func_exit = [&](dberr_t err) { |
| 1360 |
fts_sql_commit(trx); |
| 1361 |
|
| 1362 |
trx->op_info = ""; |
| 1363 |
|
| 1364 |
if (ins_ctx.m_btr_bulk != nullptr) { |
| 1365 |
err = ins_ctx.m_btr_bulk->finish(err); |
| 1366 |
ut::delete_(ins_ctx.m_btr_bulk); |
| 1367 |
} |
| 1368 |
|
| 1369 |
trx_free_for_background(trx); |
| 1370 |
|
| 1371 |
mem_heap_free(tuple_heap); |
| 1372 |
|
| 1373 |
return err; |
| 1374 |
}; |
| 1375 |
|
| 1376 |
/* Create bulk load instance */ |
| 1377 |
ins_ctx.m_btr_bulk = ut::new_withkey<Btree_load>( |
| 1378 |
ut::make_psi_memory_key(mem_key_ddl), aux_index, trx->id, observer); |
| 1379 |
|
| 1380 |
/* Create tuple for insert. */ |
| 1381 |
ins_ctx.m_tuple = |
| 1382 |
dtuple_create(tuple_heap, dict_index_get_n_fields(aux_index)); |
| 1383 |
|
| 1384 |
const auto n_fields = dict_index_get_n_fields(aux_index); |
| 1385 |
|
| 1386 |
dict_index_copy_types(ins_ctx.m_tuple, aux_index, n_fields); |
| 1387 |
|
| 1388 |
/* Set TRX_ID and ROLL_PTR */ |
| 1389 |
roll_ptr_t roll_ptr{}; |
| 1390 |
byte trx_id_buf[DATA_TRX_ID_LEN]; |
| 1391 |
|
| 1392 |
{ |
| 1393 |
auto field = dtuple_get_nth_field(ins_ctx.m_tuple, 2); |
| 1394 |
|
| 1395 |
trx_write_trx_id(trx_id_buf, trx->id); |
| 1396 |
dfield_set_data(field, &trx_id_buf, DATA_TRX_ID_LEN); |
| 1397 |
} |
| 1398 |
|
| 1399 |
{ |
| 1400 |
auto field = dtuple_get_nth_field(ins_ctx.m_tuple, 3); |
| 1401 |
|
| 1402 |
dfield_set_data(field, &roll_ptr, 7); |
| 1403 |
} |
| 1404 |
|
| 1405 |
ut_d(ins_ctx.m_handler_id = handler->m_id); |
| 1406 |
|
| 1407 |
size_t total_rows{}; |
| 1408 |
Merge_cursor cursor(builder, nullptr, nullptr); |
| 1409 |
|
| 1410 |
{ |
| 1411 |
const auto n_buffers = handler->m_files.size(); |
| 1412 |
const auto io_buffer_size = m_ctx.merge_io_buffer_size(n_buffers); |
| 1413 |
|
| 1414 |
for (auto &file : handler->m_files) { |
| 1415 |
ut_a(file.m_n_recs > 0); |
| 1416 |
|
| 1417 |
auto err = cursor.add_file(file, io_buffer_size); |
| 1418 |
|
| 1419 |
if (err != DB_SUCCESS) { |
| 1420 |
return err; |
| 1421 |
} |
| 1422 |
total_rows += file.m_n_recs; |
| 1423 |
} |
| 1424 |
} |
| 1425 |
|
| 1426 |
if (total_rows == 0) { |
| 1427 |
return func_exit(DB_SUCCESS); |
| 1428 |
} |
| 1429 |
|
| 1430 |
auto err = cursor.open(); |
| 1431 |
|
| 1432 |
if (err != DB_SUCCESS) { |
| 1433 |
return func_exit(err); |
| 1434 |
} |
| 1435 |
|
| 1436 |
/* Fetch sorted records from the run files and insert them into |
| 1437 |
corresponding FTS index auxiliary tables. */ |
| 1438 |
|
| 1439 |
doc_id_t doc_id{}; |
| 1440 |
dtuple_t *dtuple{}; |
| 1441 |
auto heap = mem_heap_create(1000, UT_LOCATION_HERE); |
| 1442 |
auto positions = ib_vector_create(heap_alloc, sizeof(doc_id_t), 32); |
| 1443 |
|
| 1444 |
while ((err = cursor.fetch(dtuple)) == DB_SUCCESS) { |
| 1445 |
mem_heap_empty(heap); |
| 1446 |
|
| 1447 |
insert_tuple(&ins_ctx, &new_word, positions, &doc_id, dtuple); |
| 1448 |
|
| 1449 |
--total_rows; |
| 1450 |
|
| 1451 |
err = cursor.next(); |
| 1452 |
|
| 1453 |
if (err != DB_SUCCESS) { |
| 1454 |
break; |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
if (err == DB_SUCCESS || err == DB_END_OF_INDEX) { |
| 1459 |
ut_a(total_rows == 0); |
| 1460 |
insert_tuple(&ins_ctx, &new_word, positions, &doc_id, nullptr); |
| 1461 |
} |
| 1462 |
|
| 1463 |
mem_heap_free(heap); |
| 1464 |
|
| 1465 |
return func_exit(err == DB_END_OF_INDEX ? DB_SUCCESS : err); |
| 1466 |
} |
| 1467 |
|
| 1468 |
FTS::FTS(Context &ctx, dict_index_t *index, dict_table_t *table) noexcept |
| 1469 |
: m_ctx(ctx), m_index(index), m_table(table) { |
| 1470 |
m_dup.m_n_dup = 0; |
| 1471 |
m_dup.m_index = nullptr; |
| 1472 |
m_dup.m_table = ctx.m_table; |
| 1473 |
m_dup.m_col_map = m_ctx.m_col_map; |
| 1474 |
} |
| 1475 |
|
| 1476 |
FTS::~FTS() noexcept { |
| 1477 |
destroy(); |
| 1478 |
|
| 1479 |
if (m_dup.m_index != nullptr) { |
| 1480 |
dict_mem_index_free(m_dup.m_index); |
| 1481 |
} |
| 1482 |
} |
| 1483 |
|
| 1484 |
dberr_t FTS::create(size_t n_threads) noexcept { |
| 1485 |
ut_a(m_parsers.empty()); |
| 1486 |
|
| 1487 |
for (size_t i = 0; i < n_threads; ++i) { |
| 1488 |
auto parser = ut::new_withkey<Parser>(ut::make_psi_memory_key(mem_key_ddl), |
| 1489 |
i, m_ctx, &m_dup, m_doc_id_32_bit); |
| 1490 |
|
| 1491 |
if (parser == nullptr) { |
| 1492 |
destroy(); |
| 1493 |
return DB_OUT_OF_MEMORY; |
| 1494 |
} |
| 1495 |
|
| 1496 |
m_parsers.push_back(parser); |
| 1497 |
|
| 1498 |
auto err = parser->init(n_threads); |
| 1499 |
|
| 1500 |
if (err != DB_SUCCESS) { |
| 1501 |
destroy(); |
| 1502 |
return err; |
| 1503 |
} |
| 1504 |
} |
| 1505 |
|
| 1506 |
m_inserter = ut::new_withkey<Inserter>(ut::make_psi_memory_key(mem_key_ddl), |
| 1507 |
m_ctx, &m_dup, m_doc_id_32_bit); |
| 1508 |
|
| 1509 |
if (m_inserter == nullptr) { |
| 1510 |
destroy(); |
| 1511 |
return DB_OUT_OF_MEMORY; |
| 1512 |
} |
| 1513 |
|
| 1514 |
return DB_SUCCESS; |
| 1515 |
} |
| 1516 |
|
| 1517 |
void FTS::destroy() noexcept { |
| 1518 |
for (auto parser : m_parsers) { |
| 1519 |
ut::delete_(parser); |
| 1520 |
} |
| 1521 |
|
| 1522 |
m_parsers.clear(); |
| 1523 |
|
| 1524 |
if (m_inserter != nullptr) { |
| 1525 |
ut::delete_(m_inserter); |
| 1526 |
m_inserter = nullptr; |
| 1527 |
} |
| 1528 |
} |
| 1529 |
|
| 1530 |
dberr_t FTS::init(size_t n_threads) noexcept { |
| 1531 |
ut_a(m_dup.m_index == nullptr); |
| 1532 |
|
| 1533 |
m_dup.m_index = create_index(m_index, m_table, &m_doc_id_32_bit); |
| 1534 |
|
| 1535 |
m_sort_index = m_dup.m_index; |
| 1536 |
|
| 1537 |
return create(n_threads); |
| 1538 |
} |
| 1539 |
|
| 1540 |
dberr_t FTS::start_parse_threads(Builder *builder) noexcept { |
| 1541 |
auto fn = [&](PSI_thread_seqnum seqnum, Parser *parser, Builder *builder) { |
| 1542 |
ut_a(seqnum > 0); |
| 1543 |
#ifdef UNIV_PFS_THREAD |
| 1544 |
Runnable runnable{fts_parallel_tokenization_thread_key, seqnum}; |
| 1545 |
#else |
| 1546 |
Runnable runnable{PSI_NOT_INSTRUMENTED, seqnum}; |
| 1547 |
#endif /* UNIV_PFS_THREAD */ |
| 1548 |
runnable([&]() { |
| 1549 |
auto thd = create_internal_thd(); |
| 1550 |
ut_ad(current_thd == thd); |
| 1551 |
|
| 1552 |
thd->push_diagnostics_area(&parser->da, false); |
| 1553 |
parser->parse(builder); |
| 1554 |
thd->pop_diagnostics_area(); |
| 1555 |
|
| 1556 |
destroy_internal_thd(current_thd); |
| 1557 |
/* Return value ignored but required for Runnable::operator() */ |
| 1558 |
return DB_SUCCESS; |
| 1559 |
}); |
| 1560 |
}; |
| 1561 |
|
| 1562 |
size_t seqnum{1}; |
| 1563 |
|
| 1564 |
for (auto parser : m_parsers) { |
| 1565 |
try { |
| 1566 |
m_threads.push_back(std::thread(fn, seqnum++, parser, builder)); |
| 1567 |
} catch (...) { |
| 1568 |
parser->set_error(DB_OUT_OF_RESOURCES); |
| 1569 |
return DB_OUT_OF_RESOURCES; |
| 1570 |
} |
| 1571 |
} |
| 1572 |
|
| 1573 |
return DB_SUCCESS; |
| 1574 |
} |
| 1575 |
|
| 1576 |
dberr_t FTS::enqueue(FTS::Doc_item *doc_item) noexcept { |
| 1577 |
auto parser = m_parsers[doc_item->m_doc_id % m_parsers.size()]; |
| 1578 |
|
| 1579 |
return parser->enqueue(doc_item); |
| 1580 |
} |
| 1581 |
|
| 1582 |
dberr_t FTS::check_for_errors() noexcept { |
| 1583 |
for (auto parser : m_parsers) { |
| 1584 |
auto da = &parser->da; |
| 1585 |
if (da->is_error() && !m_ctx.thd()->is_error()) { |
| 1586 |
m_ctx.thd()->get_stmt_da()->set_error_status( |
| 1587 |
da->mysql_errno(), da->message_text(), da->returned_sqlstate()); |
| 1588 |
} |
| 1589 |
m_ctx.thd()->get_stmt_da()->copy_sql_conditions_from_da(m_ctx.thd(), |
| 1590 |
&parser->da); |
| 1591 |
} |
| 1592 |
for (auto parser : m_parsers) { |
| 1593 |
auto err = parser->get_error(); |
| 1594 |
|
| 1595 |
if (err != DB_SUCCESS) { |
| 1596 |
m_ctx.m_trx->error_key_num = parser->id(); |
| 1597 |
return err; |
| 1598 |
} |
| 1599 |
} |
| 1600 |
|
| 1601 |
return DB_SUCCESS; |
| 1602 |
} |
| 1603 |
|
| 1604 |
dberr_t FTS::insert(Builder *builder) noexcept { |
| 1605 |
Threads threads{}; |
| 1606 |
std::vector<dberr_t, ut::allocator<dberr_t>> errs{}; |
| 1607 |
|
| 1608 |
errs.assign(FTS_NUM_AUX_INDEX, DB_SUCCESS); |
| 1609 |
|
| 1610 |
auto fn = [&](PSI_thread_seqnum seqnum, FTS::Inserter::Handler *handler, |
| 1611 |
dberr_t &err) { |
| 1612 |
ut_a(seqnum > 0); |
| 1613 |
#ifdef UNIV_PFS_THREAD |
| 1614 |
Runnable runnable{fts_parallel_merge_thread_key, seqnum}; |
| 1615 |
#else |
| 1616 |
Runnable runnable{PSI_NOT_INSTRUMENTED, seqnum}; |
| 1617 |
#endif /* UNIV_PFS_THREAD */ |
| 1618 |
|
| 1619 |
if (!handler->m_files.empty()) { |
| 1620 |
err = runnable([&]() { return m_inserter->insert(builder, handler); }); |
| 1621 |
} |
| 1622 |
}; |
| 1623 |
|
| 1624 |
dberr_t err{DB_SUCCESS}; |
| 1625 |
const auto last = FTS_NUM_AUX_INDEX - 1; |
| 1626 |
|
| 1627 |
for (size_t i = 0; i < last; ++i) { |
| 1628 |
auto handler = &m_inserter->m_handlers[i]; |
| 1629 |
try { |
| 1630 |
threads.push_back(std::thread{fn, i + 1, handler, std::ref(errs[i])}); |
| 1631 |
} catch (...) { |
| 1632 |
err = errs[i] = DB_OUT_OF_RESOURCES; |
| 1633 |
break; |
| 1634 |
} |
| 1635 |
} |
| 1636 |
|
| 1637 |
if (err == DB_SUCCESS) { |
| 1638 |
auto handler = &m_inserter->m_handlers[last]; |
| 1639 |
if (!handler->m_files.empty()) { |
| 1640 |
errs[last] = m_inserter->insert(builder, handler); |
| 1641 |
} |
| 1642 |
if (errs[last] != DB_SUCCESS) { |
| 1643 |
builder->set_error(errs[last]); |
| 1644 |
} |
| 1645 |
} |
| 1646 |
|
| 1647 |
{ |
| 1648 |
size_t i{}; |
| 1649 |
|
| 1650 |
for (auto &thread : threads) { |
| 1651 |
thread.join(); |
| 1652 |
if (errs[i] != DB_SUCCESS) { |
| 1653 |
builder->set_error(errs[i]); |
| 1654 |
} |
| 1655 |
++i; |
| 1656 |
} |
| 1657 |
} |
| 1658 |
|
| 1659 |
return builder->get_error(); |
| 1660 |
} |
| 1661 |
|
| 1662 |
dberr_t FTS::setup_insert_phase() noexcept { |
| 1663 |
for (auto parser : m_parsers) { |
| 1664 |
for (size_t i = 0; i < FTS_NUM_AUX_INDEX; ++i) { |
| 1665 |
auto file = parser->release_file(i); |
| 1666 |
|
| 1667 |
if (file.m_n_recs == 0) { |
| 1668 |
/* Ignore empty files. */ |
| 1669 |
continue; |
| 1670 |
} |
| 1671 |
|
| 1672 |
const auto err = m_inserter->add_file(i, std::move(file)); |
| 1673 |
|
| 1674 |
if (err != DB_SUCCESS) { |
| 1675 |
break; |
| 1676 |
} |
| 1677 |
} |
| 1678 |
} |
| 1679 |
|
| 1680 |
return DB_SUCCESS; |
| 1681 |
} |
| 1682 |
|
| 1683 |
dberr_t FTS::scan_finished(dberr_t err) noexcept { |
| 1684 |
for (auto parser : m_parsers) { |
| 1685 |
if (err == DB_SUCCESS) { |
| 1686 |
parser->set_parent_state(Thread_state::COMPLETE); |
| 1687 |
} else { |
| 1688 |
parser->set_parent_state(Thread_state::ABORT); |
| 1689 |
} |
| 1690 |
} |
| 1691 |
|
| 1692 |
for (auto &thread : m_threads) { |
| 1693 |
thread.join(); |
| 1694 |
} |
| 1695 |
|
| 1696 |
if (err == DB_SUCCESS) { |
| 1697 |
err = check_for_errors(); |
| 1698 |
} |
| 1699 |
|
| 1700 |
if (err != DB_SUCCESS) { |
| 1701 |
return err; |
| 1702 |
} |
| 1703 |
|
| 1704 |
auto &fts = m_ctx.m_fts; |
| 1705 |
|
| 1706 |
/* Update the next Doc ID we used. Table should be locked, so |
| 1707 |
no concurrent DML */ |
| 1708 |
if (fts.m_doc_id != nullptr && err == DB_SUCCESS) { |
| 1709 |
const auto generated = fts.m_doc_id->is_generated(); |
| 1710 |
|
| 1711 |
if ((generated && fts.m_doc_id->generated_count() > 0) || |
| 1712 |
(!generated && fts.m_doc_id->max_doc_id() > 0)) { |
| 1713 |
/* Sync fts cache for other fts indexes to keep all fts indexes |
| 1714 |
consistent in sync_doc_id. */ |
| 1715 |
auto table = const_cast<dict_table_t *>(m_ctx.m_new_table); |
| 1716 |
|
| 1717 |
err = fts_sync_table(table, false, true, false); |
| 1718 |
|
| 1719 |
if (err == DB_SUCCESS) { |
| 1720 |
auto name = m_ctx.m_old_table->name.m_name; |
| 1721 |
const auto max_doc_id{fts.m_doc_id->max_doc_id()}; |
| 1722 |
|
| 1723 |
fts_update_next_doc_id(nullptr, m_ctx.m_new_table, name, max_doc_id); |
| 1724 |
} |
| 1725 |
} |
| 1726 |
} |
| 1727 |
|
| 1728 |
if (err == DB_SUCCESS) { |
| 1729 |
err = setup_insert_phase(); |
| 1730 |
} |
| 1731 |
|
| 1732 |
for (auto parser : m_parsers) { |
| 1733 |
ut::delete_(parser); |
| 1734 |
} |
| 1735 |
|
| 1736 |
m_parsers.clear(); |
| 1737 |
|
| 1738 |
return err; |
| 1739 |
} |
| 1740 |
|
| 1741 |
} // namespace ddl |