MySQL 8.4.0
Source Code Documentation
serializer_default.h
Go to the documentation of this file.
1// Copyright (c) 2023, 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 MYSQL_SERIALIZATION_SERIALIZER_DEFAULT_H
25#define MYSQL_SERIALIZATION_SERIALIZER_DEFAULT_H
26
31
33
34/// @file
35/// Experimental API header
36
37/// @addtogroup GroupLibsMysqlSerialization
38/// @{
39
40namespace mysql::serialization {
41
42/// @brief Helper tag for vector types to help compiler pick the correct method
44/// @brief Helper tag for enum types to help compiler pick the correct method
46/// @brief Helper tag for map types to help compiler pick the correct method
48/// @brief Helper tag for array types to help compiler pick the correct method
50/// @brief Helper tag for set types to help compiler pick the correct method
52/// @brief Helper tag for serializable types to help compiler pick the correct
53/// method
55
56/// @brief Basic serializer that is implementing Serializer interface
57/// @details Serializes fields and appropriate metadata to ensure that
58/// messages are backward and forward compatible
59/// @tparam Archive_concrete_type Type of the archive
60template <class Archive_concrete_type>
62 : public Serializer<Serializer_default<Archive_concrete_type>,
63 Archive_concrete_type> {
64 public:
67
68 // template<typename ... Args>
69 // Serializer_default(Args && ... args) :
70 // Serializer(std::forward<Args>(args)...) {}
71
76 friend Base_type;
77
79
80 /// @copydoc Serializer::encode_field
81 template <class Field_type, Field_size field_size_defined>
82 void encode(
85
86 /// @copydoc Serializer::decode_field
87 template <class Field_type, Field_size field_size_defined>
88 void decode(
90 std::size_t serializable_end_pos,
92
93 // internal
94
95 /// @copydoc Serializer::get_max_size
96 template <
97 class Field_type, Field_size field_size_defined,
98 typename = std::enable_if_t<is_bounded_size_type<Field_type>() == true>>
99 static constexpr std::size_t get_max_size() {
100 return Archive_concrete_type::template get_max_size<uint64_t, 0>() +
101 Archive_concrete_type::template get_max_size<Field_type,
102 field_size_defined>();
103 }
104
105 /// @copydoc Serializer::get_max_size
106 template <class Serializable_concrete_type>
107 static constexpr std::size_t get_max_size() {
108 auto serializable_overhead_type =
109 Archive_concrete_type::template get_max_size<uint64_t, 0>();
110 auto serializable_overhead_size =
111 Archive_concrete_type::template get_max_size<uint64_t, 0>();
112 auto serializable_overhead_last_non_ignorable_field_id =
113 Archive_concrete_type::template get_max_size<uint64_t, 0>();
114 return serializable_overhead_type + serializable_overhead_size +
115 serializable_overhead_last_non_ignorable_field_id +
116 Serializable_concrete_type::template get_max_size_internal<
117 Base_type>();
118 }
119
120 /// @brief Function used internally to calculate size of
121 /// fields, version for basic types
122 /// @tparam Field_type Type of field
123 /// @param[in] field Field that will be saved
124 template <class Field_type, Field_size field_size_defined,
125 typename = std::enable_if_t<is_simple_type<Field_type>() == true>>
126 static std::size_t get_field_size(const Field_type &field);
127
128 /// @brief Function used internally to calculate size of
129 /// fields, version for enumeration types
130 /// @tparam Field_type Type of field
131 /// @param[in] field Field that will be saved
132 template <class Field_type, Field_size field_size_defined,
133 typename = std::enable_if_t<is_enum_type<Field_type>() == true>>
134 static std::size_t get_field_size(const Field_type &field,
136
137 /// @brief Function used internally to calculate size of
138 /// fields, version for vector
139 /// @tparam Field_type Type of field
140 /// @param[in] field Field that will be saved
141 template <
142 class Field_type, Field_size field_size_defined,
143 typename = std::enable_if_t<is_vector_list_type<Field_type>() == true>>
144 static std::size_t get_field_size(const Field_type &field,
145 Serializer_vector_list_tag = {});
146
147 /// @brief Function used internally to calculate size of
148 /// fields, version for serializable
149 /// @tparam Field_type Type of field
150 /// @param[in] field Field that will be saved
151 template <
152 class Field_type, Field_size field_size_defined,
153 typename = std::enable_if_t<is_serializable_type<Field_type>() == true>>
154 static std::size_t get_field_size(const Field_type &field,
155 Serializer_serializable_tag = {});
156
157 /// @brief Function used internally to calculate size of
158 /// fields, version for std::map
159 /// @tparam Field_type Type of field
160 /// @param[in] field Field that will be saved
161 template <class Field_type, Field_size field_size_defined,
162 typename = std::enable_if_t<is_map_type<Field_type>() == true>>
163 static std::size_t get_field_size(const Field_type &field,
164 Serializer_map_tag = {});
165
166 /// @brief Function used internally to calculate size of
167 /// fields, version for std::set
168 /// @tparam Field_type Type of field
169 /// @param[in] field Field that will be saved
170 template <class Field_type, Field_size field_size_defined,
171 typename = std::enable_if_t<is_set_type<Field_type>() == true>>
172 static std::size_t get_field_size(const Field_type &field,
173 Serializer_set_tag = {});
174
175 /// @brief Function used internally to calculate size of
176 /// fields, version for std::array
177 /// @tparam Field_type Type of field
178 /// @param[in] field Field that will be saved
179 template <class Field_type, Field_size field_size_defined,
180 typename = std::enable_if_t<is_array_type_v<Field_type>() == true>>
181 static std::size_t get_field_size(const Field_type &field,
182 Serializer_array_tag = {});
183
184 /// @brief Function returns size of serialized field_definition object
185 /// @tparam Field_type Type of field
186 /// @tparam field_size_defined Defined field size in archive
187 /// @param[in] field_id Field id
188 /// @param[in] field_definition Definition of the field
189 template <class Field_type, Field_size field_size_defined>
190 static std::size_t get_size_field_def(
192 const Field_definition<Field_type, field_size_defined> &field_definition);
193
194 /// @brief Function returns size of serialized field_definition object
195 /// @tparam Serializable_concrete_type Type of serializable obj
196 /// @param[in] field_id Field id
197 /// @param[in] serializable Serializable universal reference for which size
198 /// will be calculated
199 /// @param[in] skip_id Skip serializable id (for repeated fields)
200 template <class Serializable_concrete_type>
201 static std::size_t get_size_serializable(
202 Field_id_type field_id, const Serializable_concrete_type &serializable,
203 bool skip_id = false);
204
205 /// @brief This function saves serializable metadata
206 /// @tparam Serializable_type serializable data type
207 /// @param[in] level Level of the serializable tree, may be ignored, used
208 /// mainly for text formatting
209 /// @param[in] field_id Field id
210 /// @param[in] serializable Serializable object universal reference
211 /// @param[in] skip_id Skip encoding of serializable id (for repeated fields)
212 template <class Serializable_type>
214 const Serializable_type &serializable,
215 bool skip_id);
216
217 /// @brief This function loads serializable metadata
218 /// @tparam Serializable_type serializable data type
219 /// @param[in] level Level of the serializable tree, may be ignored, used
220 /// mainly for text formatting
221 /// @param[in] field_id Field id
222 /// @param[in] serializable Serializable object universal reference
223 /// @param[in] skip_id Skip encoding of serializable id (for repeated fields)
224 /// @returns Number of bytes decoded
225 template <class Serializable_type>
228 Serializable_type &serializable,
229 bool skip_id);
230
231 /// @brief Destructor
232 virtual ~Serializer_default() = default;
233
234 protected:
235 /// @brief Function used internally to decode field_id and handle optional
236 /// fields
237 /// @tparam Field_type Type of field
238 /// @tparam field_size_defined Defined field size in archive
239 /// @param[in] level Level of the serializable tree, may be ignored, used
240 /// mainly for text formatting
241 /// @param[in] field_id Field id
242 /// @param[in] field_definition Definition of the field
243 /// @return Answer to question Is field provided?
244 template <class Field_type, Field_size field_size_defined>
245 bool decode_field_id(
248
249 /// @brief Function used internally to encode field_id and handle optional
250 /// fields
251 /// @tparam Field_type Type of field
252 /// @tparam field_size_defined Defined field size in archive
253 /// @param[in] level Level of the serializable tree, may be ignored, used
254 /// mainly for text formatting
255 /// @param[in] field_id Field id
256 /// @param[in] field_definition Definition of the field
257 /// @return Answer to question Is field provided?
258 template <class Field_type, Field_size field_size_defined>
259 bool encode_field_id(
262
263 /// @brief Function used internally to encode field_id and handle optional
264 /// fields, version for basic types
265 /// @tparam Field_type Type of field
266 /// @param[in] field Field that will be saved
267 template <class Field_type, Field_size field_size_defined,
268 typename = std::enable_if_t<is_simple_type<Field_type>() == true>>
269 void encode_field(const Field_type &field);
270
271 /// @brief Function used internally to decode field_id and handle optional
272 /// fields, version for basic types
273 /// @tparam Field_type Type of field
274 /// @param[in] field Field that will be loaded
275 template <class Field_type, Field_size field_size_defined,
276 typename = std::enable_if_t<is_simple_type<Field_type>() == true>>
277 void decode_field(Field_type &field);
278
279 /// @brief Function used internally to encode field_id and handle optional
280 /// fields, version for enumeration types
281 /// @tparam Field_type Type of field
282 /// @param[in] field Field that will be saved
283 template <class Field_type, Field_size field_size_defined,
284 typename = std::enable_if_t<is_enum_type<Field_type>() == true>>
285 void encode_field(const Field_type &field, Serializer_enum_tag = {});
286
287 /// @brief Function used internally to decode field_id and handle optional
288 /// fields, version for enumeration types
289 /// @tparam Field_type Type of field
290 /// @param[in] field Field that will be loaded
291 template <class Field_type, Field_size field_size_defined,
292 typename = std::enable_if_t<is_enum_type<Field_type>() == true>>
294
295 /// @brief Function used internally to encode field_id and handle optional
296 /// fields, version for vector
297 /// @tparam Field_type Type of field
298 /// @param[in] field Field that will be saved
299 template <
300 class Field_type, Field_size field_size_defined,
301 typename = std::enable_if_t<is_vector_list_type<Field_type>() == true>>
302 void encode_field(const Field_type &field, Serializer_vector_list_tag = {});
303
304 /// @brief Function used internally to decode field_id and handle optional
305 /// fields, version for vector
306 /// @tparam Field_type Type of field
307 /// @param[in] field Field that will be loaded
308 template <
309 class Field_type, Field_size field_size_defined,
310 typename = std::enable_if_t<is_vector_list_type<Field_type>() == true>>
311 void decode_field(Field_type &field, Serializer_vector_list_tag = {});
312
313 /// @brief Function used internally to encode field_id and handle optional
314 /// fields, version for serializable
315 /// @tparam Field_type Type of field
316 /// @param[in] field Field that will be saved
317 template <
318 class Field_type, Field_size field_size_defined,
319 typename = std::enable_if_t<is_serializable_type<Field_type>() == true>>
320 void encode_field(const Field_type &field, Serializer_serializable_tag = {});
321
322 /// @brief Function used internally to decode field_id and handle optional
323 /// fields, version for serializable
324 /// @tparam Field_type Type of field
325 /// @param[in] field Field that will be loaded
326 template <
327 class Field_type, Field_size field_size_defined,
328 typename = std::enable_if_t<is_serializable_type<Field_type>() == true>>
329 void decode_field(Field_type &field, Serializer_serializable_tag = {});
330
331 /// @brief Function used internally to encode field_id and handle optional
332 /// fields, version for std::map
333 /// @tparam Field_type Type of field
334 /// @param[in] field Field that will be saved
335 template <class Field_type, Field_size field_size_defined,
336 typename = std::enable_if_t<is_map_type<Field_type>() == true>>
337 void encode_field(const Field_type &field, Serializer_map_tag = {});
338
339 /// @brief Function used internally to decode field_id and handle optional
340 /// fields, version for std::map
341 /// @tparam Field_type Type of field
342 /// @param[in] field Field that will be loaded
343 template <class Field_type, Field_size field_size_defined,
344 typename = std::enable_if_t<is_map_type<Field_type>() == true>>
345 void decode_field(Field_type &field, Serializer_map_tag = {});
346
347 /// @brief Function used internally to encode field_id and handle optional
348 /// fields, version for std::set
349 /// @tparam Field_type Type of field
350 /// @param[in] field Field that will be saved
351 template <class Field_type, Field_size field_size_defined,
352 typename = std::enable_if_t<is_set_type<Field_type>() == true>>
353 void encode_field(const Field_type &field, Serializer_set_tag = {});
354
355 /// @brief Function used internally to decode field_id and handle optional
356 /// fields, version for std::set
357 /// @tparam Field_type Type of field
358 /// @param[in] field Field that will be loaded
359 template <class Field_type, Field_size field_size_defined,
360 typename = std::enable_if_t<is_set_type<Field_type>() == true>>
361 void decode_field(Field_type &field, Serializer_set_tag = {});
362
363 /// @brief Function used internally to encode field_id and handle optional
364 /// fields, version for std::array
365 /// @tparam Field_type Type of field
366 /// @param[in] field Field that will be saved
367 template <class Field_type, Field_size field_size_defined,
368 typename = std::enable_if_t<is_array_type_v<Field_type>() == true>>
369 void encode_field(const Field_type &field, Serializer_array_tag = {});
370
371 /// @brief Function used internally to decode field_id and handle optional
372 /// fields, version for std::array
373 /// @tparam Field_type Type of field
374 /// @param[in] field Field that will be loaded
375 template <class Field_type, Field_size field_size_defined,
376 typename = std::enable_if_t<is_array_type_v<Field_type>() == true>>
377 void decode_field(Field_type &field, Serializer_array_tag = {});
378};
379
380} // namespace mysql::serialization
381
382/// @}
383
385
386#endif // MYSQL_SERIALIZATION_SERIALIZER_DEFAULT_H
Experimental API header.
Field definition provided by classes implementing Serializable interface.
Definition: field_definition.h:45
Basic serializer that is implementing Serializer interface.
Definition: serializer_default.h:63
static std::size_t get_size_field_def(Field_id_type field_id, const Field_definition< Field_type, field_size_defined > &field_definition)
Function returns size of serialized field_definition object.
Definition: serializer_default_impl.hpp:470
static constexpr std::size_t get_max_size()
Function returns maximum size of the field written to an archive, based on its type.
Definition: serializer_default.h:99
static std::size_t get_field_size(const Field_type &field)
Function used internally to calculate size of fields, version for basic types.
Definition: serializer_default_impl.hpp:234
virtual ~Serializer_default()=default
Destructor.
void encode(Level_type level, Field_id_type field_id, const Field_definition< Field_type, field_size_defined > &field_definition)
Function used to encode one field.
Definition: serializer_default_impl.hpp:361
bool encode_field_id(Level_type level, Field_id_type field_id, const Field_definition< Field_type, field_size_defined > &field_definition)
Function used internally to encode field_id and handle optional fields.
Definition: serializer_default_impl.hpp:317
bool decode_field_id(Level_type level, Field_id_type field_id, Field_definition< Field_type, field_size_defined > &field_definition)
Function used internally to decode field_id and handle optional fields.
Definition: serializer_default_impl.hpp:336
void decode_field(Field_type &field)
Function used internally to decode field_id and handle optional fields, version for basic types.
Definition: serializer_default_impl.hpp:41
static std::size_t get_size_serializable(Field_id_type field_id, const Serializable_concrete_type &serializable, bool skip_id=false)
Function returns size of serialized field_definition object.
Definition: serializer_default_impl.hpp:487
void decode(Level_type level, Field_id_type field_id, std::size_t serializable_end_pos, Field_definition< Field_type, field_size_defined > &field_definition)
Function used to decode one field.
Definition: serializer_default_impl.hpp:372
friend Archive_concrete_type
Definition: serializer_default.h:78
std::size_t decode_serializable_metadata(Level_type level, Field_id_type field_id, Serializable_type &serializable, bool skip_id)
This function loads serializable metadata.
Definition: serializer_default_impl.hpp:435
void encode_field(const Field_type &field)
Function used internally to encode field_id and handle optional fields, version for basic types.
Definition: serializer_default_impl.hpp:33
void encode_serializable_metadata(Level_type level, Field_id_type field_id, const Serializable_type &serializable, bool skip_id)
This function saves serializable metadata.
Definition: serializer_default_impl.hpp:411
friend Base_type
Definition: serializer_default.h:76
static constexpr std::size_t get_max_size()
Function returns maximum size of the field written to an archive, based on its type.
Definition: serializer_default.h:107
Interface for serializer.
Definition: serializer.h:62
Serialization_error m_error
Holds information about error.
Definition: serializer.h:251
Archive_concrete_type m_archive
Archive that stores the data.
Definition: serializer.h:250
Experimental API header.
Definition: archive.h:37
std::size_t Level_type
Type of the "level" used to indicate on which level of the nested message internal fields are defined...
Definition: serialization_types.h:40
uint64_t Field_id_type
Type for field_id assigned to each field in the.
Definition: serialization_types.h:42
std::size_t Field_size
Definition: serialization_types.h:43
Experimental API header.
Experimental API header.
Experimental API header.
Experimental API header.
static const LEX_CSTRING field_id
Definition: sql_show_processlist.cc:49
Definition: sql_resultset.h:36
Helper tag for array types to help compiler pick the correct method.
Definition: serializer_default.h:49
Helper tag for enum types to help compiler pick the correct method.
Definition: serializer_default.h:45
Helper tag for map types to help compiler pick the correct method.
Definition: serializer_default.h:47
Helper tag for serializable types to help compiler pick the correct method.
Definition: serializer_default.h:54
Helper tag for set types to help compiler pick the correct method.
Definition: serializer_default.h:51
Helper tag for vector types to help compiler pick the correct method.
Definition: serializer_default.h:43
mysql::serialization::Field_size Field_size
Definition: tag.cpp:34