MySQL 8.4.0
Source Code Documentation
tag.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_GTID_TAG_H
25#define MYSQL_GTID_TAG_H
26
27#include <array>
28#include <cstring>
29#include <memory>
30#include <string>
31
36
37/// @addtogroup GroupLibsMysqlGtid
38/// @{
39
40namespace mysql::gtid {
41
42struct Tag_plain;
43
44/// @class Tag
45/// @brief Representation of the GTID tag
46/// @details Tag format: [a-z_]{a-z0-9_}{0,31}
47/// Tag may be created from a given text. Text may contain leading and
48/// trailing spaces which will be omitted during Tag creation. Text may also
49/// contain uppercase characters. Acceptable format for text is as follows:
50/// [:space:][a-zA-Z_][a-zA-Z0-9_]{0,31}[:space:]
51class Tag {
52 public:
53 using Tag_data = std::string;
54
55 /// @brief Constructs an empty, valid tag
56 Tag() = default;
57
58 /// @brief Construct from Tag_plain object
59 /// @param tag pattern
60 explicit Tag(const Tag_plain &tag);
61
62 /// @brief Constructs tag from a given text. If passed text is not a valid
63 /// tag representation, object remains empty (empty tag)
64 /// @param[in] text Textual representation of a tag
65 /// @see from_cstring
66 Tag(const std::string &text);
67
68 /// @brief Obtains textual representation of internal tag
69 /// @returns tag string
70 std::string to_string() const;
71
72 /// @brief Obtains textual representation of internal tag and writes it to out
73 /// @param [out] out Output string
74 /// @returns Number of characters written to out
75 std::size_t to_string(char *out) const;
76
77 /// @brief Creates Tag object from a given text. If passed text is not a valid
78 /// tag representation, object remains empty (empty tag). Text must end
79 /// with a null character or a gtid separator
80 /// @param[in] text Textual representation of a tag
81 /// @returns Number of characters read, 0 means that either tag is empty or
82 /// invalid
83 [[NODISCARD]] std::size_t from_string(const std::string &text);
84
85 /// @brief Creates Tag object from a given text. If passed text is not a valid
86 /// tag representation, object remains empty (empty tag)
87 /// Since length of text is unknown, it is expected to be null terminated
88 /// @param[in] text Textual representation of a tag
89 /// @returns Number of characters read, 0 means that either tag is empty or
90 /// invalid
91 [[NODISCARD]] std::size_t from_cstring(const char *text);
92
93 /// @brief Indicates whether transaction tag is empty
94 /// @returns Answer to question: is tag empty?
95 bool is_empty() const { return m_data.empty(); }
96
97 /// @brief Indicates whether transaction tag is defined (is not empty)
98 /// @returns Answer to question: is tag defined?
99 bool is_defined() const { return !is_empty(); }
100
101 /// @brief Operator ==
102 /// @param other pattern to compare against
103 /// @return Result of comparison ==
104 bool operator==(const Tag &other) const;
105
106 /// @brief Operator !=
107 /// @param other pattern to compare against
108 /// @return Result of comparison !=
109 bool operator!=(const Tag &other) const;
110
111 /// @brief Compares this tag with other, <
112 /// @retval true This tag is before other (alphabetical order)
113 /// @retval false Other tag is before this (alphabetical order)
114 bool operator<(const Tag &other) const {
115 return m_data.compare(other.m_data) < 0;
116 }
117
118 /// @brief stores Tag in buffer
119 /// @param buf Buffer to store bytes, must contain at least
120 /// get_encoded_length() bytes
121 /// @param gtid_format Format of encoded GTID. If tag is not defined for this
122 /// GTID and tagged format is used, 0 will be encoded as length of the string.
123 /// In case "untagged" format is requested, function won't encode additional
124 /// tag information for untagged GTIDs. When using
125 /// untagged, tag is required to be empty.
126 /// @return the number of bytes written into the buf
127 [[NODISCARD]] std::size_t encode_tag(unsigned char *buf,
128 const Gtid_format &gtid_format) const;
129
130 /// @brief returns length of encoded tag, based on defined format
131 /// @param gtid_format Format of encoded GTID. If tag is not defined for this
132 /// GTID and tagged format is used, 0 will be encoded as length of the string.
133 /// In case "untagged" format is requested, function won't encode additional
134 /// tag information for untagged GTIDs. When using
135 /// untagged, tag is required to be empty.
136 /// @return length of encoded tag in bytes
137 /// @details Note that in case encoded format is "tagged" and tag is empty,
138 /// we still to encode tag length (1 byte for length equal to 0).
139 std::size_t get_encoded_length(const Gtid_format &gtid_format) const;
140
141 /// @brief returns length of tag
142 /// @return tag length
143 std::size_t get_length() const;
144
145 /// @brief Obtains maximum length of encoded tag (compile time)
146 /// @return Maximum length of encoded tag in bytes
147 static constexpr std::size_t get_max_encoded_length() {
148 // we fix maximum size encoding to 1, since maximum tag length is 32
149 return 1 + tag_max_length;
150 }
151
152 /// @brief Reads Tag data from the buffer
153 /// @param buf Buffer to read tag from
154 /// @param buf_len Number of bytes in the buffer
155 /// @param gtid_format Gtid format read from stream, if untagged, tag
156 /// information is assumed to be empty. If tagged, gtid tag length will be
157 /// read from the stream both for untagged and tagged GTIDs
158 /// @return The number of bytes read or 0. For Gtid_format::untagged,
159 /// function will read bytes. For Gtid_format::tagged, 0 means
160 /// that an error occurred (e.g. not enough bytes in the buffer to read the
161 /// tag - corrupted bytes in the buffer).
162 [[NODISCARD]] std::size_t decode_tag(const unsigned char *buf,
163 std::size_t buf_len,
164 const Gtid_format &gtid_format);
165
166 /// @brief Structure to compute hash function of a given Tag object
167 struct Hash {
168 /// @brief Computes hash of a given Tag object
169 /// @param arg Object handle for which hash will be calculated
170 size_t operator()(const Tag &arg) const;
171 };
172
173 /// @brief Internal data accessor
174 /// @return Const reference to internal tag data
175 const Tag_data &get_data() const { return m_data; }
176
177 /// @brief Internal data accessor, non const (serialization)
178 /// @return Reference to internal tag data
179 Tag_data &get_data() { return m_data; }
180
181 protected:
182 /// @brief Checks whether current character is a valid ending of a Tag string
183 /// @param[in] character A character under test
184 /// @returns Answer to question: is character a valid tag ending?
185 static bool is_valid_end_char(const char &character);
186
187 /// @brief Checks whether current character is a valid Tag character
188 /// @param[in] character A character under test
189 /// @param[in] pos Position within a tag
190 /// @returns Answer to question: is character a valid tag character?
191 static bool is_character_valid(const char &character, std::size_t pos);
192
193 /// @brief Replaces internal tag data with a given text, includes text
194 /// normalization
195 /// @param[in] text Pattern to copy from, valid tag string
196 /// @param[in] len Number of characters to copy
197 void replace(const char *text, std::size_t len);
198
199 Tag_data m_data = ""; ///< internal tag representation
200};
201
202} // namespace mysql::gtid
203
204/// @}
205
206#endif // MYSQL_GTID_TAG_H
Representation of the GTID tag.
Definition: tag.h:51
bool operator!=(const Tag &other) const
Operator !=.
Definition: tag.cpp:142
const Tag_data & get_data() const
Internal data accessor.
Definition: tag.h:175
static bool is_character_valid(const char &character, std::size_t pos)
Checks whether current character is a valid Tag character.
Definition: tag.cpp:112
std::size_t encode_tag(unsigned char *buf, const Gtid_format &gtid_format) const
stores Tag in buffer
Definition: tag.cpp:65
bool is_defined() const
Indicates whether transaction tag is defined (is not empty)
Definition: tag.h:99
bool is_empty() const
Indicates whether transaction tag is empty.
Definition: tag.h:95
bool operator<(const Tag &other) const
Compares this tag with other, <.
Definition: tag.h:114
std::size_t get_encoded_length(const Gtid_format &gtid_format) const
returns length of encoded tag, based on defined format
Definition: tag.cpp:92
Tag_data m_data
internal tag representation
Definition: tag.h:199
static constexpr std::size_t get_max_encoded_length()
Obtains maximum length of encoded tag (compile time)
Definition: tag.h:147
void replace(const char *text, std::size_t len)
Replaces internal tag data with a given text, includes text normalization.
Definition: tag.cpp:44
bool operator==(const Tag &other) const
Operator ==.
Definition: tag.cpp:140
Tag_data & get_data()
Internal data accessor, non const (serialization)
Definition: tag.h:179
std::size_t from_string(const std::string &text)
Creates Tag object from a given text.
Definition: tag.cpp:108
std::string Tag_data
Definition: tag.h:53
Tag()=default
Constructs an empty, valid tag.
static bool is_valid_end_char(const char &character)
Checks whether current character is a valid ending of a Tag string.
Definition: tag.cpp:103
std::size_t get_length() const
returns length of tag
Definition: tag.cpp:101
std::size_t from_cstring(const char *text)
Creates Tag object from a given text.
Definition: tag.cpp:117
std::string to_string() const
Obtains textual representation of internal tag.
Definition: tag.cpp:54
std::size_t decode_tag(const unsigned char *buf, std::size_t buf_len, const Gtid_format &gtid_format)
Reads Tag data from the buffer.
Definition: tag.cpp:75
Definition: buf0block_hint.cc:30
Definition: global.h:35
Gtid_format
Gtid binary format indicator.
Definition: gtid_format.h:38
constexpr std::size_t tag_max_length
Maximal number of characters in a tag.
Definition: gtid_constants.h:44
#define NODISCARD
The function attribute [[NODISCARD]] is a replacement for [[nodiscard]] to workaround a gcc bug.
Definition: nodiscard.h:47
Structure to compute hash function of a given Tag object.
Definition: tag.h:167
size_t operator()(const Tag &arg) const
Computes hash of a given Tag object.
Definition: tag.cpp:151
Tag representation so that:
Definition: tag_plain.h:48