Debian Codegraph @

storage/innobase/include/fts0types.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2007, 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/fts0types.h
29 Full text search types file
30
31 Created 2007-03-27 Sunny Bains
32 *******************************************************/
33
34 #ifndef INNOBASE_FTS0TYPES_H
35 #define INNOBASE_FTS0TYPES_H
36
37 #include <cstdint>
38 #include "fts0fts.h"
39 #include "fut0fut.h"
40 #include "pars0pars.h"
41 #include "que0types.h"
42 #include "univ.i"
43 #include "ut0byte.h"
44 #include "ut0rbt.h"
45
46 /** Types used within FTS. */
47 struct fts_que_t;
48 struct fts_node_t;
49
50 /** Callbacks used within FTS. */
51 typedef pars_user_func_cb_t fts_sql_callback;
52 typedef void (*fts_filter)(void *, fts_node_t *, void *, ulint len);
53
54 /** Statistics relevant to a particular document, used during retrieval. */
55 struct fts_doc_stats_t {
56 doc_id_t doc_id; /*!< Document id */
57 ulint word_count; /*!< Total words in the document */
58 };
59
60 /** It's main purpose is to store the SQL prepared statements that
61 are required to retrieve a document from the database. */
62 struct fts_get_doc_t {
63 fts_index_cache_t *index_cache; /*!< The index cache instance */
64
65 /*!< Parsed sql statement */
66 que_t *get_document_graph;
67 fts_cache_t *cache; /*!< The parent cache */
68 };
69
70 /** Since we can have multiple FTS indexes on a table, we keep a
71 per index cache of words etc. */
72 struct fts_index_cache_t {
73 dict_index_t *index; /*!< The FTS index instance */
74
75 ib_rbt_t *words; /*!< Nodes; indexed by fts_string_t*,
76 cells are fts_tokenizer_word_t*.*/
77
78 ib_vector_t *doc_stats; /*!< Array of the fts_doc_stats_t
79 contained in the memory buffer.
80 Must be in sorted order (ascending).
81 The ideal choice is an rb tree but
82 the rb tree imposes a space overhead
83 that we can do without */
84
85 que_t **ins_graph; /*!< Insert query graphs */
86
87 que_t **sel_graph; /*!< Select query graphs */
88 CHARSET_INFO *charset; /*!< charset */
89 };
90
91 /** For supporting the tracking of updates on multiple FTS indexes we need
92 to track which FTS indexes need to be updated. For INSERT and DELETE we
93 update all fts indexes. */
94 struct fts_update_t {
95 doc_id_t doc_id; /*!< The doc id affected */
96
97 ib_vector_t *fts_indexes; /*!< The FTS indexes that need to be
98 updated. A NULL value means all
99 indexes need to be updated. This
100 vector is not allocated on the heap
101 and so must be freed explicitly,
102 when we are done with it */
103 };
104
105 /** Stop word control infotmation. */
106 struct fts_stopword_t {
107 ulint status; /*!< Status of the stopword tree */
108 ib_alloc_t *heap; /*!< The memory allocator to use */
109 ib_rbt_t *cached_stopword; /*!< This stores all active stopwords */
110 CHARSET_INFO *charset; /*!< charset for stopword */
111 };
112
113 /** The SYNC state of the cache. There is one instance of this struct
114 associated with each ADD thread. */
115 struct fts_sync_t {
116 trx_t *trx; /*!< The transaction used for SYNCing
117 the cache to disk */
118 dict_table_t *table; /*!< Table with FTS index(es) */
119 ulint max_cache_size; /*!< Max size in bytes of the cache */
120 bool cache_full; /*!< flag, when true it indicates that
121 we need to sync the cache to disk */
122 ulint lower_index; /*!< the start index of the doc id
123 vector from where to start adding
124 documents to the FTS cache */
125 ulint upper_index; /*!< max index of the doc id vector to
126 add to the FTS cache */
127 bool interrupted; /*!< true if SYNC was interrupted */
128 doc_id_t min_doc_id; /*!< The smallest doc id added to the
129 cache. It should equal to
130 doc_ids[lower_index] */
131 doc_id_t max_doc_id; /*!< The doc id at which the cache was
132 noted as being full, we use this to
133 set the upper_limit field */
134 std::chrono::steady_clock::time_point start_time;
135 /*!< SYNC start time */
136 bool in_progress; /*!< flag whether sync is in progress.*/
137 bool unlock_cache; /*!< flag whether unlock cache when
138 write fts node */
139 os_event_t event; /*!< sync finish event */
140 };
141
142 /** The cache for the FTS system. It is a memory-based inverted index
143 that new entries are added to, until it grows over the configured maximum
144 size, at which time its contents are written to the INDEX table. */
145 struct fts_cache_t {
146 #ifndef UNIV_HOTBACKUP
147 rw_lock_t lock; /*!< lock protecting all access to the
148 memory buffer. FIXME: this needs to
149 be our new upgrade-capable rw-lock */
150
151 rw_lock_t init_lock; /*!< lock used for the cache
152 initialization, it has different
153 SYNC level as above cache lock */
154 #endif /* !UNIV_HOTBACKUP */
155
156 ib_mutex_t optimize_lock; /*!< Lock for OPTIMIZE */
157
158 ib_mutex_t deleted_lock; /*!< Lock covering deleted_doc_ids */
159
160 ib_mutex_t doc_id_lock; /*!< Lock covering Doc ID */
161
162 ib_vector_t *deleted_doc_ids; /*!< Array of deleted doc ids, each
163 element is of type fts_update_t */
164
165 ib_vector_t *indexes; /*!< We store the stats and inverted
166 index for the individual FTS indexes
167 in this vector. Each element is
168 an instance of fts_index_cache_t */
169
170 ib_vector_t *get_docs; /*!< information required to read
171 the document from the table. Each
172 element is of type fts_doc_t */
173
174 ulint total_size; /*!< total size consumed by the ilist
175 field of all nodes. SYNC is run
176 whenever this gets too big */
177 uint64_t total_size_before_sync; /*!< total size of fts cache,
178 when last SYNC request was sent */
179 fts_sync_t *sync; /*!< sync structure to sync data to
180 disk */
181 ib_alloc_t *sync_heap; /*!< The heap allocator, for indexes
182 and deleted_doc_ids, ie. transient
183 objects, they are recreated after
184 a SYNC is completed */
185
186 ib_alloc_t *self_heap; /*!< This heap is the heap out of
187 which an instance of the cache itself
188 was created. Objects created using
189 this heap will last for the lifetime
190 of the cache */
191
192 doc_id_t next_doc_id; /*!< Next doc id */
193
194 doc_id_t synced_doc_id; /*!< Doc ID sync-ed to CONFIG table */
195
196 doc_id_t first_doc_id; /*!< first doc id since this table
197 was opened */
198
199 ulint deleted; /*!< Number of doc ids deleted since
200 last optimized. This variable is
201 covered by deleted_lock */
202
203 ulint added; /*!< Number of doc ids added since last
204 optimized. This variable is covered by
205 the deleted lock */
206
207 fts_stopword_t stopword_info; /*!< Cached stopwords for the FTS */
208 mem_heap_t *cache_heap; /*!< Cache Heap */
209 };
210
211 /** Columns of the FTS auxiliary INDEX table */
212 struct fts_node_t {
213 doc_id_t first_doc_id; /*!< First document id in ilist. */
214
215 doc_id_t last_doc_id; /*!< Last document id in ilist. */
216
217 byte *ilist; /*!< Binary list of documents & word
218 positions the token appears in.
219 TODO: For now, these are simply
220 ut_malloc'd, but if testing shows
221 that they waste memory unacceptably, a
222 special memory allocator will have
223 to be written */
224
225 ulint doc_count; /*!< Number of doc ids in ilist */
226
227 ulint ilist_size; /*!< Used size of ilist in bytes. */
228
229 ulint ilist_size_alloc;
230 /*!< Allocated size of ilist in
231 bytes */
232 bool synced; /*!< flag whether the node is synced */
233 };
234
235 /** A tokenizer word. Contains information about one word. */
236 struct fts_tokenizer_word_t {
237 fts_string_t text; /*!< Token text. */
238
239 ib_vector_t *nodes; /*!< Word node ilists, each element is
240 of type fts_node_t */
241 };
242
243 /** Word text plus it's array of nodes as on disk in FTS index */
244 struct fts_word_t {
245 fts_string_t text; /*!< Word value in UTF-8 */
246 ib_vector_t *nodes; /*!< Nodes read from disk */
247
248 ib_alloc_t *heap_alloc; /*!< For handling all allocations */
249 };
250
251 /** Callback for reading and filtering nodes that are read from FTS index */
252 struct fts_fetch_t {
253 void *read_arg; /*!< Arg for the sql_callback */
254
255 fts_sql_callback read_record; /*!< Callback for reading index
256 record */
257 ulint total_memory; /*!< Total memory used */
258 };
259
260 /** For horizontally splitting an FTS auxiliary index */
261 struct fts_index_selector_t {
262 ulint value; /*!< Character value at which
263 to split */
264
265 const char *suffix; /*!< FTS aux index suffix */
266 };
267
268 /** This type represents a single document field.
269 When fulltext index spans multiple columns, the
270 entire document (all indexed text in a row)
271 is comprised of multiple fields, one per indexed
272 column. */
273 struct fts_doc_t {
274 fts_string_t text; /*!< document text */
275
276 bool found; /*!< true if the document was found
277 successfully in the database */
278
279 ib_rbt_t *tokens; /*!< This is filled when the document
280 is tokenized. Tokens; indexed by
281 fts_string_t*, cells are of type
282 fts_token_t* */
283
284 ib_alloc_t *self_heap; /*!< An instance of this type is
285 allocated from this heap along
286 with any objects that have the
287 same lifespan, most notably
288 the vector of token positions */
289 CHARSET_INFO *charset; /*!< Document's charset info */
290
291 st_mysql_ftparser *parser; /*!< fts plugin parser */
292
293 bool is_ngram; /*!< Whether it is a ngram parser */
294
295 ib_rbt_t *stopwords; /*!< Stopwords */
296 };
297
298 /** A token and its positions within a document. */
299 struct fts_token_t {
300 fts_string_t text; /*!< token text */
301
302 ib_vector_t *positions; /*!< an array of the positions the
303 token is found in; each item is
304 actually an ulint. */
305 };
306
307 /** It's defined in fts/fts0fts.c */
308 extern const fts_index_selector_t fts_index_selector[];
309
310 /** It's defined in fts/fts0fts.c */
311 extern const fts_index_selector_t fts_index_selector_5_7[];
312
313 /** Decode and return the integer that was encoded using our VLC scheme.
314 @param[in,out] ptr ptr to decode from, this ptr is incremented
315 by the number of bytes decoded
316 @return value decoded */
317 inline uint64_t fts_decode_vlc(byte **ptr);
318
319 /** Duplicate a string.
320 @param[in] dst dup to here
321 @param[in] src src string
322 @param[in] heap heap to use
323 */
324 inline void fts_string_dup(fts_string_t *dst, const fts_string_t *src,
325 mem_heap_t *heap);
326
327 /** Return length of val if it were encoded using our VLC scheme.
328 @param[in] val value to encode
329 @return length of value encoded, in bytes */
330 inline unsigned int fts_get_encoded_len(uint64_t val);
331
332 /** Encode an integer using our VLC scheme and return the length in bytes.
333 @param[in] val value to encode
334 @param[in] buf buffer, must have enough space
335 @return length of value encoded, in bytes */
336 inline unsigned int fts_encode_int(uint64_t val, byte *buf);
337
338 /** Get the selected FTS aux INDEX suffix. */
339 inline const char *fts_get_suffix(ulint selected); /*!< in: selected index */
340
341 /** Return the selected FTS aux index suffix in 5.7 compatible format
342 @param[in] selected selected index
343 @return the suffix name */
344 inline const char *fts_get_suffix_5_7(ulint selected);
345
346 /** Select the FTS auxiliary table for the given character.
347 @param[in] cs charset
348 @param[in] str string
349 @param[in] len string length in bytes
350 @retval the auxiliary table number to use for the string, zero-based */
351 inline ulint fts_select_index(const CHARSET_INFO *cs, const byte *str,
352 ulint len);
353
354 #include "fts0types.ic"
355 #include "fts0vlc.ic"
356
357 #endif /* INNOBASE_FTS0TYPES_H */