MySQL 9.0.0
Source Code Documentation
collations_internal.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef STRINGS_COLLATIONS_INTERNAL_H_
25#define STRINGS_COLLATIONS_INTERNAL_H_
26
27#include <cstddef>
28#include <functional>
29#include <mutex>
30#include <string>
31#include <unordered_map>
32#include <utility>
33
35
36constexpr char MY_CHARSET_INDEX[]{"Index.xml"};
37
38typedef int myf;
39
40namespace mysql {
41
42namespace collation {
43class Name;
44} // namespace collation
45
46namespace collation_internals {
47
48/**
49 Helper class: implementation of character set/collation library
50
51 @see mysql::collation_internals::entry.
52*/
53class Collations final {
54 public:
55 Collations(const Collations &) = delete;
56 Collations &operator=(const Collations &) = delete;
57
58 /**
59 Constructor
60
61 @param charset_dir Optional "/\0"-terminated path to the directory
62 containing Index.xml
63 @param loader Optional user-specified hooks to the character
64 set/collation parser/initializer.
65 */
66 explicit Collations(const char *charset_dir,
67 MY_CHARSET_LOADER *loader = nullptr);
68
70
71 /**
72 Finds collation by its name
73
74 @note Forces collation parsing/initialization if not done yet.
75
76 @param name Collation name
77
78 @param flags Optional mysys-specific flags
79
80 @param [out] errmsg Optional buffer to return error message from
81 collation parser/initializer
82
83 @returns pointer to a collation object on success, nullptr if not found
84 */
86 MY_CHARSET_ERRMSG *errmsg = nullptr);
87
88 /**
89 Finds collation by its number
90
91 @note Forces collation parsing/initialization if not done yet.
92
93 @param id Collation id (hardcoded in library sources or
94 specified in Index.xml)
95
96 @param flags Optional mysys-specific flags
97
98 @param [out] errmsg Optional buffer to return error message from
99 collation parser/initializer
100
101 @returns pointer to a collation object on success, nullptr if not found
102 */
103 CHARSET_INFO *find_by_id(unsigned id, myf flags = 0,
104 MY_CHARSET_ERRMSG *errmsg = nullptr);
105
106 /**
107 Finds primary collation by its character set name
108
109 @note Forces collation parsing/initialization if not done yet.
110
111 @param cs_name Character set name
112
113 @param flags Optional mysys-specific flags
114
115 @param [out] errmsg Optional buffer to return error message from
116 collation parser/initializer
117
118 @returns pointer to a collation object on success, nullptr if not found
119 */
121 myf flags = 0,
122 MY_CHARSET_ERRMSG *errmsg = nullptr);
123
124 /**
125 Finds binary collation by its character set name
126
127 @note Forces collation parsing/initialization if not done yet.
128
129 @param cs_name Character set name
130
131 @param flags Optional mysys-specific flags
132
133 @param [out] errmsg Optional buffer to return error message from
134 collation parser/initializer
135
136 @returns pointer to a collation object on success, nullptr if not found
137 */
139 myf flags = 0,
140 MY_CHARSET_ERRMSG *errmsg = nullptr);
141
142 /**
143 Finds collation by its name and returns its id
144
145 @param name Collation name
146
147 @returns collation id
148 */
149 unsigned get_collation_id(const mysql::collation::Name &name) const;
150
151 /**
152 Finds character set by its name and returns an id of its primary collation
153
154 @param name Collation name
155
156 @returns primary collation id
157 */
159
160 /**
161 Finds character set by its name and returns an id of its default binary
162 collation
163
164 @param name Collation name
165
166 @returns default binary collation id
167 */
169 const mysql::collation::Name &name) const;
170
171 /**
172 If not done yet, force collation parsing/initialization under m_mutex lock
173
174 @param cs Pointer to collation object
175
176 @param flags Optional mysys-specific flags
177
178 @param [out] errmsg Optional buffer to return error message from
179 collation parser/initializer
180
181 @returns @p cs on success, otherwise nullptr
182 */
184 MY_CHARSET_ERRMSG *errmsg = nullptr);
185
186 /**
187 Like find_by_name but without initialization of return value
188
189 @param name Collation name
190
191 @returns Pointer to CHARSET_INFO object on success, nullptr if not found.
192 The resulting value can point to a half-initialized object.
193 Moreover, further initialization of that object or parsing
194 of its collation XML can fail.
195 */
197
198 /**
199 For registering compile-time collations
200
201 @param cs Collation object
202
203 @returns false on success, otherwise true.
204 */
206
207 /**
208 Iterate over all collation objects known to the library
209
210 @param f Closure to execute on each collation object known to the library
211 */
212 void iterate(const std::function<void(const CHARSET_INFO *)> &f) {
213 for (const auto &i : m_all_by_collation_name) {
214 f(i.second);
215 }
216 }
217
218 protected:
219 /**
220 Internals of safe_init_when_necessary()
221
222 This function is similar to safe_init_when_necessary, but, unlike
223 safe_init_when_necessary(), it doesn't acquire locks.
224
225 @param cs Pointer to collation object
226
227 @param flags Optional mysys-specific flags
228
229 @param [out] errmsg Optional buffer to return error message from
230 collation parser/initializer
231
232 @returns @p cs on success, otherwise nullptr
233 */
235 MY_CHARSET_ERRMSG *errmsg);
236
237 /**
238 Optional '/'-terminated path to the directory containing Index.xml
239 */
240 const std::string m_charset_dir;
241
242 /**
243 Common parametric type to map character set/collation names or their ids
244 to CHARSET_INFO object pointers
245
246 @tparam Key Name or id type (std::string or unsigned respectively)
247
248 TODO(gleb): it would be good to use mysql::collation::Name instead of
249 std::string for Key.
250 */
251 template <typename Key>
252 using Hash = std::unordered_map<Key, CHARSET_INFO *>;
253
254 /**
255 Maps collation ids to CHARSET_INFO object pointers
256 */
258
259 /**
260 Maps normalized strings of all known character set names, collation names,
261 and their aliases to CHARSET_INFO object pointers
262
263 @note see old_conv and get_old_charset_by_name() for exclusions
264 @see old_conv(), get_old_charset_by_name()
265 */
267
268 /**
269 Maps normalized strings of character set names to CHARSET_INFO object
270 pointers
271
272 @note In MySQL, CHARSET_INFO object of character set is also an object
273 of its primary collation.
274 */
276
277 /**
278 Maps normalized strings of character set names to CHARSET_INFO objects
279 of preferred binary collations
280
281 @note utf8mb4 has two separate binary collations, so m_binary_by_cs_name
282 contains a reference to utf8mb4_bin only.
283 */
285
286 /**
287 False if m_loader references external MY_CHARSET_LOADER, otherwise true.
288 */
289 const bool m_owns_loader;
290
291 /**
292 Shared MY_CHARSET_LOADER implementation for use in collation parser and
293 initializer
294
295 By default references an instance of mysql::collation_internals::Loader.
296 */
298
299 private:
300 /**
301 Collation parser/initializer mutex
302
303 The library parses collations and initializes CHARSET_INFO objects in
304 depth on demand, so m_mutex is necessary to guarantee a safety of
305 concurrent find_... function calls.
306 */
307 std::mutex m_mutex;
308};
309
310/**
311 Global entry point to character set/collation library internals
312*/
313extern Collations *entry;
314
315} // namespace collation_internals
316} // namespace mysql
317
318#endif // STRINGS_COLLATIONS_INTERNAL_H_
static Mysys_charset_loader * loader
Definition: charset.cc:185
User-specified callback interface for collation parser/initializer.
Definition: m_ctype.h:189
Normalizes character set/collation names.
Definition: collations.h:63
Helper class: implementation of character set/collation library.
Definition: collations_internal.h:53
Hash< std::string > m_all_by_collation_name
Maps normalized strings of all known character set names, collation names, and their aliases to CHARS...
Definition: collations_internal.h:266
unsigned get_default_binary_collation_id(const mysql::collation::Name &name) const
Finds character set by its name and returns an id of its default binary collation.
Definition: collations_internal.cc:702
MY_CHARSET_LOADER * m_loader
Shared MY_CHARSET_LOADER implementation for use in collation parser and initializer.
Definition: collations_internal.h:297
CHARSET_INFO * find_by_name_unsafe(const mysql::collation::Name &name)
Like find_by_name but without initialization of return value.
Definition: collations_internal.cc:774
CHARSET_INFO * find_primary(const mysql::collation::Name &cs_name, myf flags=0, MY_CHARSET_ERRMSG *errmsg=nullptr)
Finds primary collation by its character set name.
Definition: collations_internal.cc:677
Hash< std::string > m_binary_by_cs_name
Maps normalized strings of character set names to CHARSET_INFO objects of preferred binary collations...
Definition: collations_internal.h:284
void iterate(const std::function< void(const CHARSET_INFO *)> &f)
Iterate over all collation objects known to the library.
Definition: collations_internal.h:212
CHARSET_INFO * find_by_name(const mysql::collation::Name &name, myf flags=0, MY_CHARSET_ERRMSG *errmsg=nullptr)
Finds collation by its name.
Definition: collations_internal.cc:666
Hash< unsigned > m_all_by_id
Maps collation ids to CHARSET_INFO object pointers.
Definition: collations_internal.h:257
unsigned get_primary_collation_id(const mysql::collation::Name &name) const
Finds character set by its name and returns an id of its primary collation.
Definition: collations_internal.cc:696
Collations(const Collations &)=delete
const std::string m_charset_dir
Optional '/'-terminated path to the directory containing Index.xml.
Definition: collations_internal.h:240
~Collations()
Definition: collations_internal.cc:654
CHARSET_INFO * unsafe_init(CHARSET_INFO *cs, myf flags, MY_CHARSET_ERRMSG *errmsg)
Internals of safe_init_when_necessary()
Definition: collations_internal.cc:724
Collations & operator=(const Collations &)=delete
Hash< std::string > m_primary_by_cs_name
Maps normalized strings of character set names to CHARSET_INFO object pointers.
Definition: collations_internal.h:275
std::mutex m_mutex
Collation parser/initializer mutex.
Definition: collations_internal.h:307
unsigned get_collation_id(const mysql::collation::Name &name) const
Finds collation by its name and returns its id.
Definition: collations_internal.cc:690
std::unordered_map< Key, CHARSET_INFO * > Hash
Common parametric type to map character set/collation names or their ids to CHARSET_INFO object point...
Definition: collations_internal.h:252
bool add_internal_collation(CHARSET_INFO *cs)
For registering compile-time collations.
Definition: collations_internal.cc:746
CHARSET_INFO * find_default_binary(const mysql::collation::Name &cs_name, myf flags=0, MY_CHARSET_ERRMSG *errmsg=nullptr)
Finds binary collation by its character set name.
Definition: collations_internal.cc:683
CHARSET_INFO * find_by_id(unsigned id, myf flags=0, MY_CHARSET_ERRMSG *errmsg=nullptr)
Finds collation by its number.
Definition: collations_internal.cc:672
const bool m_owns_loader
False if m_loader references external MY_CHARSET_LOADER, otherwise true.
Definition: collations_internal.h:289
CHARSET_INFO * safe_init_when_necessary(CHARSET_INFO *cs, myf flags=0, MY_CHARSET_ERRMSG *errmsg=nullptr)
If not done yet, force collation parsing/initialization under m_mutex lock.
Definition: collations_internal.cc:708
int myf
Definition: collations_internal.h:38
constexpr char MY_CHARSET_INDEX[]
Definition: collations_internal.h:36
static int flags[50]
Definition: hp_test1.cc:40
A better implementation of the UNIX ctype(3) library.
int myf
Definition: my_inttypes.h:94
const char * collation
Definition: audit_api_message_emit.cc:184
Definition: commit_order_queue.h:34
Collations * entry
Global entry point to character set/collation library internals.
Definition: collations_internal.cc:41
Definition: instrumented_condition_variable.h:32
case opt name
Definition: sslopt-case.h:29
Definition: m_ctype.h:421
Helper structure to return error messages from collation parser/initializer.
Definition: m_ctype.h:180