MySQL 8.2.0
Source Code Documentation
uuid.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef MYSQL_GTID_UUID_H
24#define MYSQL_GTID_UUID_H
25
26#include <stddef.h>
27#include <stdio.h>
28#include <string.h>
29
30#include <string>
31#include <utility>
32
33#include "template_utils.h"
34
35/// @addtogroup GroupLibsMysqlGtid
36/// @{
37
38namespace mysql::gtid {
39
40/**
41 @struct Uuid
42
43 This is a POD. It has to be a POD because it is a member of
44 Sid_map::Node which is stored in HASH in mysql-server code.
45 The structure contains the following components.
46 <table>
47 <caption>Structure gtid_info</caption>
48
49 <tr>
50 <th>Name</th>
51 <th>Format</th>
52 <th>Description</th>
53 </tr>
54 <tr>
55 <td>byte</td>
56 <td>unsigned char array</td>
57 <td>This stores the Uuid of the server on which transaction
58 is originated</td>
59 </tr>
60 </table>
61*/
62
63struct Uuid {
64 /// Set to all zeros.
65 void clear() { memset(bytes, 0, BYTE_LENGTH); }
66 /// Copies the given 16-byte data to this UUID.
67 void copy_from(const unsigned char *data) {
68 memcpy(bytes, data, BYTE_LENGTH);
69 }
70 /// Copies the given UUID object to this UUID.
71 void copy_from(const Uuid &data) {
72 copy_from(pointer_cast<const unsigned char *>(data.bytes));
73 }
74 /// Copies the given UUID object to this UUID.
75 void copy_to(unsigned char *data) const { memcpy(data, bytes, BYTE_LENGTH); }
76 /// Returns true if this UUID is equal the given UUID.
77 bool equals(const Uuid &other) const {
78 return memcmp(bytes, other.bytes, BYTE_LENGTH) == 0;
79 }
80 /**
81 Returns true if parse() would succeed, but doesn't store the result.
82
83 @param string String that needs to be checked.
84 @param len Length of that string.
85
86 @retval true valid string.
87 @retval false invalid string.
88 */
89 static bool is_valid(const char *string, size_t len);
90
91 /**
92 Stores the UUID represented by a string of the form
93 XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX or
94 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX or
95 {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
96 in this object.
97
98 @param string String to be parsed and stored.
99 @param len Length of that string.
100
101 @retval 0 success.
102 @retval >0 failure.
103 */
104 int parse(const char *string, size_t len);
105
106 /**
107 Parses the UUID passed as argument in in_string and functions and writes
108 the binary representation in out_binary_string.
109 Depends on UUID's read_section method and the constants for text length.
110
111 @param[in] in_string String to be parsed.
112 @param[in] len Length of that string.
113 @param[out] out_binary_string String where the binary UUID will be stored
114
115 @retval 0 success.
116 @retval >0 failure.
117 */
118 static int parse(const char *in_string, size_t len,
119 const unsigned char *out_binary_string);
120 /**
121 Helper method used to validate and parse one section of a uuid.
122 If the last parameter, out_binary_str, is NULL then the function will
123 just validate the section.
124
125 @param[in] section_len Length of the section to be parsed.
126 @param[in,out] section_str Pointer to a string containing the
127 section. It will be updated during the
128 execution as the string is parsed.
129 @param[out] out_binary_str String where the section will be stored
130 in binary format. If null, the function
131 will just validate the input string.
132
133 @retval false success.
134 @retval true failure.
135 */
136 static bool read_section(int section_len, const char **section_str,
137 const unsigned char **out_binary_str);
138 /** The number of bytes in the data of a Uuid. */
139 static const size_t BYTE_LENGTH = 16;
140 /** The data for this Uuid. */
141 unsigned char bytes[BYTE_LENGTH];
142 /**
143 Generates a 36+1 character long representation of this UUID object
144 in the given string buffer.
145
146 @retval 36 - the length of the resulting string.
147 */
148 size_t to_string(char *buf) const;
149 /// Convert the given binary buffer to a UUID
150 static size_t to_string(const unsigned char *bytes_arg, char *buf);
151 std::string to_string() const {
152 char buf[TEXT_LENGTH + 1];
153 to_string(buf);
154 return buf;
155 }
156 void print() const {
157 char buf[TEXT_LENGTH + 1];
158 to_string(buf);
159 printf("%s\n", buf);
160 }
161 /// The number of bytes in the textual representation of a Uuid.
162 static const size_t TEXT_LENGTH = 36;
163 /// The number of bits in the data of a Uuid.
164 static const size_t BIT_LENGTH = 128;
165 static const int NUMBER_OF_SECTIONS = 5;
166 static constexpr int bytes_per_section[NUMBER_OF_SECTIONS] = {4, 2, 2, 2, 6};
167 static constexpr int hex_to_byte[256] = {
168 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
169 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
170 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5,
171 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1,
172 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
173 -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1,
174 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
175 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
176 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
177 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
178 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
179 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
180 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
181 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
182 -1, -1, -1, -1,
183 };
184};
185
186struct Hash_Uuid {
187 size_t operator()(const Uuid &uuid) const {
188 return std::hash<std::string>()(
189 std::string(pointer_cast<const char *>(uuid.bytes), Uuid::BYTE_LENGTH));
190 }
191};
192
193inline bool operator==(const Uuid &a, const Uuid &b) { return a.equals(b); }
194
195} // namespace mysql::gtid
196
197namespace [[deprecated]] binary_log {
198namespace [[deprecated]] gtids {
199using namespace mysql::gtid;
200} // namespace gtids
201} // namespace binary_log
202
203/// @}
204
205#endif // MYSQL_GTID_UUID_H
Definition: item_cmpfunc.h:1640
static char buf[MAX_BUF]
Definition: conf_to_src.cc:72
Definition: base.h:74
Definition: buf0block_hint.cc:29
Definition: global.h:34
bool operator==(const Uuid &a, const Uuid &b)
Definition: uuid.h:193
Definition: uuid.h:186
size_t operator()(const Uuid &uuid) const
Definition: uuid.h:187
This is a POD.
Definition: uuid.h:63
void copy_from(const Uuid &data)
Copies the given UUID object to this UUID.
Definition: uuid.h:71
static const size_t TEXT_LENGTH
The number of bytes in the textual representation of a Uuid.
Definition: uuid.h:162
static constexpr int bytes_per_section[NUMBER_OF_SECTIONS]
Definition: uuid.h:166
bool equals(const Uuid &other) const
Returns true if this UUID is equal the given UUID.
Definition: uuid.h:77
void copy_from(const unsigned char *data)
Copies the given 16-byte data to this UUID.
Definition: uuid.h:67
static const int NUMBER_OF_SECTIONS
Definition: uuid.h:165
void print() const
Definition: uuid.h:156
void copy_to(unsigned char *data) const
Copies the given UUID object to this UUID.
Definition: uuid.h:75
unsigned char bytes[BYTE_LENGTH]
The data for this Uuid.
Definition: uuid.h:141
static bool read_section(int section_len, const char **section_str, const unsigned char **out_binary_str)
Helper method used to validate and parse one section of a uuid.
Definition: uuid.cpp:71
static const size_t BYTE_LENGTH
The number of bytes in the data of a Uuid.
Definition: uuid.h:139
static constexpr int hex_to_byte[256]
Definition: uuid.h:167
std::string to_string() const
Definition: uuid.h:151
int parse(const char *string, size_t len)
Stores the UUID represented by a string of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX or XXXXXXXXX...
Definition: uuid.cpp:34
static bool is_valid(const char *string, size_t len)
Returns true if parse() would succeed, but doesn't store the result.
Definition: uuid.cpp:91
void clear()
Set to all zeros.
Definition: uuid.h:65
static const size_t BIT_LENGTH
The number of bits in the data of a Uuid.
Definition: uuid.h:164