| 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 include/ddl0fts.h |
| 29 |
Create Full Text Index with (parallel) merge sort. |
| 30 |
Created 10/13/2010 Jimmy Yang */ |
| 31 |
|
| 32 |
#ifndef ddl0fts_h |
| 33 |
#define ddl0fts_h |
| 34 |
|
| 35 |
#include "btr0load.h" |
| 36 |
#include "data0data.h" |
| 37 |
#include "ddl0impl-buffer.h" |
| 38 |
#include "dict0types.h" |
| 39 |
#include "fts0priv.h" |
| 40 |
#include "fts0types.h" |
| 41 |
#include "ut0mpmcbq.h" |
| 42 |
|
| 43 |
/** The general architecture is that the work is done in two phases, |
| 44 |
roughly the read and write phase. The scanner pushes the document to |
| 45 |
a read handler queue for processing. |
| 46 |
|
| 47 |
Phase I: |
| 48 |
Start several parsing/tokenization threads that read the document from |
| 49 |
a queue, parse the document, tokenize the document, add them to a buffer, |
| 50 |
sort the rows in the buffer and then write the buffer to a temporary file. |
| 51 |
There is one file per auxiliary table per parser instance. So, if you |
| 52 |
have 2 parse threads you will end up with: |
| 53 |
|
| 54 |
2 x FTS_NUM_AUX_INDEX files. |
| 55 |
|
| 56 |
Phase 2: |
| 57 |
The temporary files generated during phase I are not closed but passed to |
| 58 |
the second (write) phase so that these temporary files can be merged and |
| 59 |
the rows inserted into the new FTS index. Using the example from above, |
| 60 |
create FTS_NUM_AUX_INDEX threads and each thread will merge 2 files. */ |
| 61 |
|
| 62 |
namespace ddl { |
| 63 |
// Forward declaration |
| 64 |
struct Builder; |
| 65 |
|
| 66 |
/** Full text search index builder. */ |
| 67 |
struct FTS { |
| 68 |
/** Information about temporary files used in merge sort. This structure |
| 69 |
defines information the scan thread will fetch and put to the linked list |
| 70 |
for parallel tokenization/sort threads to process */ |
| 71 |
struct Doc_item { |
| 72 |
/** Field which contains a document string to index */ |
| 73 |
dfield_t m_field{}; |
| 74 |
|
| 75 |
/** Document ID */ |
| 76 |
doc_id_t m_doc_id{}; |
| 77 |
}; |
| 78 |
|
| 79 |
/** Constructor. |
| 80 |
@param[in, out] ctx DDL context. |
| 81 |
@param[in, out] index DDL index. |
| 82 |
@param[in, out] table DDL table. */ |
| 83 |
FTS(Context &ctx, dict_index_t *index, dict_table_t *table) noexcept; |
| 84 |
|
| 85 |
/** ~Destructor. */ |
| 86 |
~FTS() noexcept; |
| 87 |
|
| 88 |
/** Create the internal data structures. |
| 89 |
@param[in] n_threads Number of parse threads to create. |
| 90 |
@return DB_SUCCESS or error code. */ |
| 91 |
dberr_t init(size_t n_threads) noexcept; |
| 92 |
|
| 93 |
/** @return the DDL index. */ |
| 94 |
dict_index_t *index() noexcept { return m_index; } |
| 95 |
|
| 96 |
/** @return the temporary sort index. */ |
| 97 |
dict_index_t *sort_index() noexcept { return m_dup.m_index; } |
| 98 |
|
| 99 |
/** Start the parsing, create the threads. |
| 100 |
@return DB_SUCCESS or error code. */ |
| 101 |
dberr_t start_parse_threads(Builder *builder) noexcept; |
| 102 |
|
| 103 |
/** For sending the documents to parse to the parsing threads. |
| 104 |
@param[in,out] doc_item Document to parse, takes ownership. |
| 105 |
@return DB_SUCCESS or error code, doc_item will be deleted either way. */ |
| 106 |
dberr_t enqueue(Doc_item *doc_item) noexcept; |
| 107 |
|
| 108 |
/** Check for error status after the parsing has finished. |
| 109 |
@return DB_SUCCESS or error code. */ |
| 110 |
dberr_t check_for_errors() noexcept; |
| 111 |
|
| 112 |
/** Start the merging and insert threads. |
| 113 |
@param[in,out] builder Builder instance to use. |
| 114 |
@return DB_SUCCESS or error code. */ |
| 115 |
dberr_t insert(Builder *builder) noexcept; |
| 116 |
|
| 117 |
/** Inform the parser threads that the scanning phase is complete so |
| 118 |
that they can shutdown after emptying the doc item queue. |
| 119 |
@param[in] err Error status of the scanning thread(s). |
| 120 |
@return DB_SUCCESS or error code. */ |
| 121 |
dberr_t scan_finished(dberr_t err) noexcept; |
| 122 |
|
| 123 |
private: |
| 124 |
/** Create the data structures required to build the FTS index. |
| 125 |
@param[in] n_threads Number of parser threads. |
| 126 |
@return DB_SUCCESS or error code. */ |
| 127 |
dberr_t create(size_t n_threads) noexcept; |
| 128 |
|
| 129 |
/** @return the number of parses. */ |
| 130 |
size_t get_n_parsers() const noexcept { return m_parsers.size(); } |
| 131 |
|
| 132 |
/** Destroy the data structures and clean up. */ |
| 133 |
void destroy() noexcept; |
| 134 |
|
| 135 |
/** Create a temporary "fts sort index" used to merge sort the |
| 136 |
tokenized doc string. The index has three "fields": |
| 137 |
|
| 138 |
1. Tokenized word, |
| 139 |
2. Doc ID |
| 140 |
3. Word's position in original 'doc'. |
| 141 |
|
| 142 |
@param[in,out] index Index to sort. |
| 143 |
@param[in,out] table Table that the FTS index is created on. |
| 144 |
@param[out] doc_id_32_bit Whether to use 4 bytes instead of 7 bytes |
| 145 |
integer to store the DOC ID during sort. |
| 146 |
@return dict_index_t structure for the fts sort index */ |
| 147 |
[[nodiscard]] static dict_index_t *create_index(dict_index_t *index, |
| 148 |
dict_table_t *table, |
| 149 |
bool *doc_id_32_bit) noexcept; |
| 150 |
|
| 151 |
/** Setup the insert phase inoput files generated by the parsers. |
| 152 |
@return DB_SUCCESS or error code. */ |
| 153 |
dberr_t setup_insert_phase() noexcept; |
| 154 |
|
| 155 |
private: |
| 156 |
// Forward declaration |
| 157 |
struct Parser; |
| 158 |
struct Inserter; |
| 159 |
|
| 160 |
using Threads = std::vector<std::thread>; |
| 161 |
using Parsers = std::vector<Parser *, ut::allocator<Parser *>>; |
| 162 |
|
| 163 |
/** For parsing the documents, there is one per thread. */ |
| 164 |
Parsers m_parsers{}; |
| 165 |
|
| 166 |
/** For inserting the rows parsed by the m_parsers. */ |
| 167 |
Inserter *m_inserter{}; |
| 168 |
|
| 169 |
/** DDL context. */ |
| 170 |
Context &m_ctx; |
| 171 |
|
| 172 |
/** Duplicate key reporting. */ |
| 173 |
Dup m_dup; |
| 174 |
|
| 175 |
/** true if document ID should be stored as a 32 bit instead of a 64 bit. */ |
| 176 |
bool m_doc_id_32_bit{}; |
| 177 |
|
| 178 |
/** DDL index instance. */ |
| 179 |
dict_index_t *m_index{}; |
| 180 |
|
| 181 |
/** DDL table instance. */ |
| 182 |
dict_table_t *m_table{}; |
| 183 |
|
| 184 |
/** Temporary index instance with relevant FTS columns. */ |
| 185 |
dict_index_t *m_sort_index{}; |
| 186 |
|
| 187 |
/** For tracking parser threads. */ |
| 188 |
Threads m_threads{}; |
| 189 |
}; |
| 190 |
|
| 191 |
} // namespace ddl |
| 192 |
#endif /* ddl0fts_h */ |