Debian Codegraph @

storage/innobase/include/fts0priv.h

raw
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 include/fts0priv.h
29 Full text search internal header file
30
31 Created 2011/09/02 Sunny Bains
32 ***********************************************************************/
33
34 #ifndef INNOBASE_FTS0PRIV_H
35 #define INNOBASE_FTS0PRIV_H
36
37 #include "dict0dict.h"
38 #include "fts0types.h"
39 #include "pars0pars.h"
40 #include "que0que.h"
41 #include "que0types.h"
42 #include "univ.i"
43
44 /* The various states of the FTS sub system pertaining to a table with
45 FTS indexes defined on it. */
46 enum fts_table_state_enum {
47 /* !<This must be 0 since we insert
48 a hard coded '0' at create time
49 to the config table */
50
51 FTS_TABLE_STATE_RUNNING = 0, /*!< Auxiliary tables created OK */
52
53 FTS_TABLE_STATE_OPTIMIZING, /*!< This is a substate of RUNNING */
54
55 FTS_TABLE_STATE_DELETED /*!< All aux tables to be dropped when
56 it's safe to do so */
57 };
58
59 typedef enum fts_table_state_enum fts_table_state_t;
60
61 /** The default time to wait for the background thread. */
62 constexpr std::chrono::milliseconds FTS_MAX_BACKGROUND_THREAD_WAIT{10};
63
64 /** Maximum number of iterations to wait before we complain */
65 #define FTS_BACKGROUND_THREAD_WAIT_COUNT 1000
66
67 /** The maximum length of the config table's value column in bytes */
68 #define FTS_MAX_CONFIG_NAME_LEN 64
69
70 /** The maximum length of the config table's value column in bytes */
71 #define FTS_MAX_CONFIG_VALUE_LEN 1024
72
73 /** Approx. upper limit of ilist length in bytes. */
74 #define FTS_ILIST_MAX_SIZE (64 * 1024)
75
76 /** FTS config table name parameters */
77
78 /** The number of seconds after which an OPTIMIZE run will stop */
79 #define FTS_OPTIMIZE_LIMIT_IN_SECS "optimize_checkpoint_limit"
80
81 /** The next doc id */
82 #define FTS_SYNCED_DOC_ID "synced_doc_id"
83
84 /** The last word that was OPTIMIZED */
85 #define FTS_LAST_OPTIMIZED_WORD "last_optimized_word"
86
87 /** Total number of documents that have been deleted. The next_doc_id
88 minus this count gives us the total number of documents. */
89 #define FTS_TOTAL_DELETED_COUNT "deleted_doc_count"
90
91 /** Total number of words parsed from all documents */
92 #define FTS_TOTAL_WORD_COUNT "total_word_count"
93
94 /** Start of optimize of an FTS index */
95 #define FTS_OPTIMIZE_START_TIME "optimize_start_time"
96
97 /** End of optimize for an FTS index */
98 #define FTS_OPTIMIZE_END_TIME "optimize_end_time"
99
100 /** User specified stopword table name */
101 #define FTS_STOPWORD_TABLE_NAME "stopword_table_name"
102
103 /** Whether to use (turn on/off) stopword */
104 #define FTS_USE_STOPWORD "use_stopword"
105
106 /** State of the FTS system for this table. It can be one of
107 RUNNING, OPTIMIZING, DELETED. */
108 #define FTS_TABLE_STATE "table_state"
109
110 /** The minimum length of an FTS auxiliary table names's id component
111 e.g., For an auxiliary table name
112
113 "FTS_@<TABLE_ID@>_SUFFIX"
114
115 This constant is for the minimum length required to store the @<TABLE_ID@>
116 component.
117 */
118 #define FTS_AUX_MIN_TABLE_ID_LENGTH 48
119
120 /** Maximum length of an integer stored in the config table value column. */
121 #define FTS_MAX_INT_LEN 32
122
123 /** Parse an SQL string. %s is replaced with the table's id.
124 @return query graph */
125 [[nodiscard]] que_t *fts_parse_sql(
126 fts_table_t *fts_table, /*!< in: FTS aux table */
127 pars_info_t *info, /*!< in: info struct, or NULL */
128 const char *sql); /*!< in: SQL string to evaluate */
129
130 /** Evaluate a parsed SQL statement
131 @return DB_SUCCESS or error code */
132 [[nodiscard]] dberr_t fts_eval_sql(trx_t *trx, /*!< in: transaction */
133 que_t *graph); /*!< in: Parsed statement */
134
135 /** Construct the name of an ancillary FTS table for the given table.
136 Caller must allocate enough memory(usually size of MAX_FULL_NAME_LEN)
137 for param 'table_name'. */
138 void fts_get_table_name(
139 const fts_table_t *fts_table, /*!< in: FTS aux table info */
140 char *table_name); /*!< in/out: aux table name */
141
142 /** Construct the name of an ancillary FTS table for the given table in
143 5.7 compatible format. Caller must allocate enough memory(usually size
144 of MAX_FULL_NAME_LEN) for param 'table_name'
145 @param[in] fts_table Auxiliary table object
146 @param[in,out] table_name aux table name */
147 void fts_get_table_name_5_7(const fts_table_t *fts_table, char *table_name);
148
149 /** Construct the column specification part of the SQL string for selecting the
150 indexed FTS columns for the given table. Adds the necessary bound
151 ids to the given 'info' and returns the SQL string. Examples:
152
153 One indexed column named "text":
154
155 "$sel0",
156 info/ids: sel0 -> "text"
157
158 Two indexed columns named "subject" and "content":
159
160 "$sel0, $sel1",
161 info/ids: sel0 -> "subject", sel1 -> "content",
162 @return heap-allocated WHERE string */
163 [[nodiscard]] const char *fts_get_select_columns_str(
164 dict_index_t *index, /*!< in: FTS index */
165 pars_info_t *info, /*!< in/out: parser info */
166 mem_heap_t *heap); /*!< in: memory heap */
167
168 /** define for fts_doc_fetch_by_doc_id() "option" value, defines whether
169 we want to get Doc whose ID is equal to or greater or smaller than supplied
170 ID */
171 #define FTS_FETCH_DOC_BY_ID_EQUAL 1
172 #define FTS_FETCH_DOC_BY_ID_LARGE 2
173
174 /** Fetch document (= a single row's indexed text) with the given
175 document id.
176 @return: DB_SUCCESS if fetch is successful, else error */
177 dberr_t fts_doc_fetch_by_doc_id(
178 fts_get_doc_t *get_doc, /*!< in: state */
179 doc_id_t doc_id, /*!< in: id of document to fetch */
180 dict_index_t *index_to_use, /*!< in: caller supplied FTS index,
181 or NULL */
182 ulint option, /*!< in: search option, if it is
183 greater than doc_id or equal */
184 fts_sql_callback callback, /*!< in: callback to read
185 records */
186 void *arg); /*!< in: callback arg */
187
188 /** Callback function for fetch that stores the text of an FTS document,
189 converting each column to UTF-16.
190 @return always false */
191 bool fts_query_expansion_fetch_doc(void *row, /*!< in: sel_node_t* */
192 void *user_arg); /*!< in: fts_doc_t* */
193
194 /********************************************************************
195 Write out a single word's data as new entry/entries in the INDEX table.
196 @return DB_SUCCESS if all OK. */
197 [[nodiscard]] dberr_t fts_write_node(
198 trx_t *trx, /*!< in: transaction */
199 que_t **graph, /*!< in: query graph */
200 fts_table_t *fts_table, /*!< in: the FTS aux index */
201 fts_string_t *word, /*!< in: word in UTF-8 */
202 fts_node_t *node); /*!< in: node columns */
203
204 /** Check fts token
205 1. for ngram token, check whether the token contains any words in stopwords
206 2. for non-ngram token, check if it's stopword or less than fts_min_token_size
207 or greater than fts_max_token_size.
208 @param[in] token token string
209 @param[in] stopwords stopwords rb tree
210 @param[in] is_ngram is ngram parser
211 @param[in] cs token charset
212 @retval true if it is not stopword and length in range
213 @retval false if it is stopword or length not in range */
214 bool fts_check_token(const fts_string_t *token, const ib_rbt_t *stopwords,
215 bool is_ngram, const CHARSET_INFO *cs);
216
217 /** Initialize a document. */
218 void fts_doc_init(fts_doc_t *doc); /*!< in: doc to initialize */
219
220 /** Do a binary search for a doc id in the array
221 @return +ve index if found -ve index where it should be
222 inserted if not found */
223 [[nodiscard]] int fts_bsearch(fts_update_t *array, /*!< in: array to sort */
224 int lower, /*!< in: lower bound of array*/
225 int upper, /*!< in: upper bound of array*/
226 doc_id_t doc_id); /*!< in: doc id to lookup */
227 /** Free document. */
228 void fts_doc_free(fts_doc_t *doc); /*!< in: document */
229
230 /** Free fts_optimizer_word_t instanace.*/
231 void fts_word_free(fts_word_t *word); /*!< in: instance to free.*/
232
233 /** Read the rows from the FTS inde
234 @return DB_SUCCESS or error code */
235 dberr_t fts_index_fetch_nodes(
236 trx_t *trx, /*!< in: transaction */
237 que_t **graph, /*!< in: prepared statement */
238 fts_table_t *fts_table, /*!< in: FTS aux table */
239 const fts_string_t *word, /*!< in: the word to fetch */
240 fts_fetch_t *fetch); /*!< in: fetch callback.*/
241
242 /** Compare two fts_trx_table_t instances, we actually compare the
243 table id's here.
244 @param[in] v1 id1
245 @param[in] v2 id2
246 @return < 0 if n1 < n2, 0 if n1 == n2, > 0 if n1 > n2 */
247 static inline int fts_trx_table_cmp(const void *v1, const void *v2);
248
249 /** Compare a table id with a trx_table_t table id.
250 @param[in] p1 id1
251 @param[in] p2 id2
252 @return < 0 if n1 < n2, 0 if n1 == n2, > 0 if n1 > n2 */
253 static inline int fts_trx_table_id_cmp(const void *p1, const void *p2);
254
255 /** Commit a transaction.
256 @return DB_SUCCESS if all OK */
257 dberr_t fts_sql_commit(trx_t *trx); /*!< in: transaction */
258
259 /** Rollback a transaction.
260 @return DB_SUCCESS if all OK */
261 dberr_t fts_sql_rollback(trx_t *trx); /*!< in: transaction */
262
263 /** Get value from config table. The caller must ensure that enough
264 space is allocated for value to hold the column contents
265 @return DB_SUCCESS or error code */
266 dberr_t fts_config_get_value(
267 trx_t *trx, /* transaction */
268 fts_table_t *fts_table, /*!< in: the indexed FTS table */
269 const char *name, /*!< in: get config value for
270 this parameter name */
271 fts_string_t *value); /*!< out: value read from
272 config table */
273 /** Get value specific to an FTS index from the config table. The caller
274 must ensure that enough space is allocated for value to hold the
275 column contents.
276 @return DB_SUCCESS or error code */
277 [[nodiscard]] dberr_t fts_config_get_index_value(
278 trx_t *trx, /*!< transaction */
279 dict_index_t *index, /*!< in: index */
280 const char *param, /*!< in: get config value
281 for this parameter name */
282 fts_string_t *value); /*!< out: value read
283 from config table */
284
285 /** Set the value in the config table for name.
286 @return DB_SUCCESS or error code */
287 dberr_t fts_config_set_value(
288 trx_t *trx, /*!< transaction */
289 fts_table_t *fts_table, /*!< in: the indexed FTS table */
290 const char *name, /*!< in: get config value for
291 this parameter name */
292 const fts_string_t *value); /*!< in: value to update */
293
294 /** Set an ulint value in the config table.
295 @return DB_SUCCESS if all OK else error code */
296 [[nodiscard]] dberr_t fts_config_set_ulint(
297 trx_t *trx, /*!< in: transaction */
298 fts_table_t *fts_table, /*!< in: the indexed FTS table */
299 const char *name, /*!< in: param name */
300 ulint int_value); /*!< in: value */
301
302 /** Set the value specific to an FTS index in the config table.
303 @return DB_SUCCESS or error code */
304 [[nodiscard]] dberr_t fts_config_set_index_value(
305 trx_t *trx, /*!< transaction */
306 dict_index_t *index, /*!< in: index */
307 const char *param, /*!< in: get config value
308 for this parameter name */
309 fts_string_t *value); /*!< out: value read
310 from config table */
311
312 #ifdef FTS_OPTIMIZE_DEBUG
313 /** Get an ulint value from the config table.
314 @return DB_SUCCESS or error code */
315 [[nodiscard]] dberr_t fts_config_get_index_ulint(
316 trx_t *trx, /*!< in: transaction */
317 dict_index_t *index, /*!< in: FTS index */
318 const char *name, /*!< in: param name */
319 ulint *int_value); /*!< out: value */
320
321 /** Set an ulint value int the config table.
322 @return DB_SUCCESS or error code */
323 [[nodiscard]] dberr_t fts_config_set_index_ulint(
324 trx_t *trx, /*!< in: transaction */
325 dict_index_t *index, /*!< in: FTS index */
326 const char *name, /*!< in: param name */
327 ulint int_value); /*!< in: value */
328 #endif /* FTS_OPTIMIZE_DEBUG */
329
330 /** Get an ulint value from the config table.
331 @return DB_SUCCESS or error code */
332 dberr_t fts_config_get_ulint(
333 trx_t *trx, /*!< in: transaction */
334 fts_table_t *fts_table, /*!< in: the indexed FTS table */
335 const char *name, /*!< in: param name */
336 ulint *int_value); /*!< out: value */
337
338 /** Search cache for word.
339 @return the word node vector if found else NULL */
340 [[nodiscard]] const ib_vector_t *fts_cache_find_word(
341 const fts_index_cache_t *index_cache, /*!< in: cache to search */
342 const fts_string_t *text); /*!< in: word to search for */
343
344 /** Append deleted doc ids to vector and sort the vector. */
345 void fts_cache_append_deleted_doc_ids(
346 const fts_cache_t *cache, /*!< in: cache to use */
347 ib_vector_t *vector); /*!< in: append to this vector */
348 /** Wait for the background thread to start. We poll to detect change
349 of state, which is acceptable, since the wait should happen only
350 once during startup.
351 @param[in] table table to which the thread is attached
352 @param[in] max_wait Time to wait. If set to 0 then it disables timeout checking
353 @return true if the thread started else false (i.e timed out) */
354 bool fts_wait_for_background_thread_to_start(
355 dict_table_t *table, std::chrono::microseconds max_wait);
356 /** Search the index specific cache for a particular FTS index.
357 @return the index specific cache else NULL */
358 [[nodiscard]] fts_index_cache_t *fts_find_index_cache(
359 const fts_cache_t *cache, /*!< in: cache to search */
360 const dict_index_t *index); /*!< in: index to search for */
361
362 /** Write the table id to the given buffer (including final NUL). Buffer must
363 be at least FTS_AUX_MIN_TABLE_ID_LENGTH bytes long.
364 @param[in] id a table/index id
365 @param[in] str buffer to write the id to
366 @return number of bytes written */
367 static inline int fts_write_object_id(ib_id_t id, char *str);
368
369 /** Read the table id from the string generated by fts_write_object_id().
370 @param[out] id Table ID.
371 @param[in] str Buffer to read from.
372 @return true if parse successful */
373 [[nodiscard]] static inline bool fts_read_object_id(ib_id_t *id,
374 const char *str);
375
376 /** Get the table id.
377 @return number of bytes written */
378 [[nodiscard]] int fts_get_table_id(
379 const fts_table_t *fts_table, /*!< in: FTS Auxiliary table */
380 char *table_id); /*!< out: table id, must be at least
381 FTS_AUX_MIN_TABLE_ID_LENGTH bytes
382 long */
383
384 /** Add the table to add to the OPTIMIZER's list. */
385 void fts_optimize_add_table(dict_table_t *table); /*!< in: table to add */
386
387 /** Construct the prefix name of an FTS table.
388 @return own: table name, must be freed with ut::free() */
389 [[nodiscard]] char *fts_get_table_name_prefix(
390 const fts_table_t *fts_table); /*!< in: Auxiliary table type */
391
392 /** Add node positions. */
393 void fts_cache_node_add_positions(
394 fts_cache_t *cache, /*!< in: cache */
395 fts_node_t *node, /*!< in: word node */
396 doc_id_t doc_id, /*!< in: doc id */
397 ib_vector_t *positions); /*!< in: fts_token_t::positions */
398
399 /** Create the config table name for retrieving index specific value.
400 @return index config parameter name */
401 [[nodiscard]] char *fts_config_create_index_param_name(
402 const char *param, /*!< in: base name of param */
403 const dict_index_t *index); /*!< in: index for config */
404
405 #include "fts0priv.ic"
406
407 #endif /* INNOBASE_FTS0PRIV_H */