Debian Codegraph @

storage/innobase/include/fts0types.ic

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.ic
29 Full text search types.
30
31 Created 2007-03-27 Sunny Bains
32 *******************************************************/
33
34 #ifndef INNOBASE_FTS0TYPES_IC
35 #define INNOBASE_FTS0TYPES_IC
36
37 #include "ha_prototypes.h"
38 #include "rem0cmp.h"
39
40 /** Duplicate a string. */
41 inline void fts_string_dup(fts_string_t *dst, /*!< in: dup to here */
42 const fts_string_t *src, /*!< in: src string */
43 mem_heap_t *heap) /*!< in: heap to use */
44 {
45 dst->f_str = (byte *)mem_heap_alloc(heap, src->f_len + 1);
46 memcpy(dst->f_str, src->f_str, src->f_len);
47
48 dst->f_len = src->f_len;
49 dst->f_str[src->f_len] = 0;
50 dst->f_n_char = src->f_n_char;
51 }
52
53 /** Compare two doc_ids.
54 @param[in] id1 1st doc_id to compare
55 @param[in] id2 2nd doc_id to compare
56 @return < 0 if id1 < id2, 0 if id1 == id2, > 0 if id1 > id2 */
57 inline int fts_doc_id_cmp(doc_id_t id1, doc_id_t id2) {
58 if (id1 < id2) {
59 return -1;
60 } else if (id1 > id2) {
61 return 1;
62 } else {
63 return 0;
64 }
65 }
66
67 /** Compare doc_ids of 2 objects.
68 @param[in] p1 Pointer to first instance o1 of T
69 @param[in] p2 Pointer to second instance o2 of T
70 @return sign(o1->doc_id - o2->doc_id) */
71 template <typename T>
72 int fts_doc_id_field_cmp(const void *p1, const void *p2) {
73 const T *o1 = static_cast<const T *>(p1);
74 const T *o2 = static_cast<const T *>(p2);
75
76 return fts_doc_id_cmp(o1->doc_id, o2->doc_id);
77 }
78
79 /** Get the first character's code position for FTS index partition
80 @param[in] cs Character set
81 @param[in] p2 string
82 @param[in] len2 string length
83 */
84 extern ulint innobase_strnxfrm(const CHARSET_INFO *cs, const uchar *p2,
85 const ulint len2);
86
87 /** Check if fts index charset is cjk
88 @param[in] cs charset
89 @retval true if the charset is cjk
90 @retval false if not. */
91 inline bool fts_is_charset_cjk(const CHARSET_INFO *cs) {
92 if (strcmp(cs->m_coll_name, "gb2312_chinese_ci") == 0 ||
93 strcmp(cs->m_coll_name, "gbk_chinese_ci") == 0 ||
94 strcmp(cs->m_coll_name, "big5_chinese_ci") == 0 ||
95 strcmp(cs->m_coll_name, "gb18030_chinese_ci") == 0 ||
96 strcmp(cs->m_coll_name, "ujis_japanese_ci") == 0 ||
97 strcmp(cs->m_coll_name, "sjis_japanese_ci") == 0 ||
98 strcmp(cs->m_coll_name, "cp932_japanese_ci") == 0 ||
99 strcmp(cs->m_coll_name, "eucjpms_japanese_ci") == 0 ||
100 strcmp(cs->m_coll_name, "euckr_korean_ci") == 0) {
101 return (true);
102 } else {
103 return (false);
104 }
105 }
106
107 /** Select the FTS auxiliary index for the given character by range.
108 @param[in] cs charset
109 @param[in] str string
110 @param[in] len string length
111 @retval the index to use for the string */
112 inline ulint fts_select_index_by_range(const CHARSET_INFO *cs, const byte *str,
113 ulint len) {
114 ulint selected = 0;
115 ulint value = innobase_strnxfrm(cs, str, len);
116
117 while (fts_index_selector[selected].value != 0) {
118 if (fts_index_selector[selected].value == value) {
119 return (selected);
120
121 } else if (fts_index_selector[selected].value > value) {
122 return (selected > 0 ? selected - 1 : 0);
123 }
124
125 ++selected;
126 }
127
128 ut_ad(selected > 1);
129
130 return (selected - 1);
131 }
132
133 /** Select the FTS auxiliary index for the given character by hash.
134 @param[in] cs charset
135 @param[in] str string
136 @param[in] len string length
137 @retval the index to use for the string */
138 inline ulint fts_select_index_by_hash(const CHARSET_INFO *cs, const byte *str,
139 ulint len) {
140 int char_len;
141
142 ut_ad(!(str == nullptr && len > 0));
143
144 if (str == nullptr || len == 0) {
145 return 0;
146 }
147
148 /* Get the first char */
149 char_len = my_mbcharlen_ptr(cs, reinterpret_cast<const char *>(str),
150 reinterpret_cast<const char *>(str + len));
151 ut_ad(static_cast<ulint>(char_len) <= len);
152
153 /*
154 Get collation hash code. Force truncation to ulong for legacy reasons;
155 it gives different results for Windows and Linux, but it needs to match
156 on-disk data.
157 */
158 uint64_t nr1 = 1;
159 uint64_t nr2 = 4;
160 cs->coll->hash_sort(cs, str, char_len, &nr1, &nr2);
161
162 return (static_cast<ulong>(nr1) % FTS_NUM_AUX_INDEX);
163 }
164
165 /** Select the FTS auxiliary table for the given character.
166 @param[in] cs charset
167 @param[in] str string
168 @param[in] len string length in bytes
169 @retval the auxiliary table number to use for the string, zero-based */
170 inline ulint fts_select_index(const CHARSET_INFO *cs, const byte *str,
171 ulint len) {
172 /* Words which compare equal using the character set's collation (have
173 the same sort order) MUST go into the same auxiliary table.
174 This is necessary as selecting a word using the equality operator will
175 select all words equal in the table's/field's collation order.
176 Other parts of FTS (e.g. index optimization) depend on this property.
177
178 This property is guaranteed by using collation-provided transforms,
179 which provide binary-equal values for collation-equal arguments:
180 weight string (strnxfrm) for alphabetic scripts, and collation hash
181 for non-alphabetic (CJK = Chinese, Korean and Japanese). */
182 ulint selected;
183
184 if (fts_is_charset_cjk(cs)) {
185 selected = fts_select_index_by_hash(cs, str, len);
186 } else {
187 selected = fts_select_index_by_range(cs, str, len);
188 }
189
190 return (selected);
191 }
192
193 /** Return the selected FTS aux index suffix. */
194 inline const char *fts_get_suffix(ulint selected) /*!< in: selected index */
195 {
196 return (fts_index_selector[selected].suffix);
197 }
198
199 /** Return the selected FTS aux index suffix in 5.7 compatible format
200 @param[in] selected selected index
201 @return the suffix name */
202 inline const char *fts_get_suffix_5_7(ulint selected) {
203 return (fts_index_selector_5_7[selected].suffix);
204 }
205
206 #endif /* INNOBASE_FTS0TYPES_IC */