MySQL 8.0.39
Source Code Documentation
uuid.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 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 UUID_H_INCLUDED
25#define UUID_H_INCLUDED 1
26
27#include <stddef.h>
28#include <stdio.h>
29#include <string.h>
30
31#include <string>
32#include <utility>
33
34#include "template_utils.h"
35
36namespace binary_log {
37
38/**
39 @struct Uuid
40
41 This is a POD. It has to be a POD because it is a member of
42 Sid_map::Node which is stored in HASH in mysql-server code.
43 The structure contains the following components.
44 <table>
45 <caption>Structure gtid_info</caption>
46
47 <tr>
48 <th>Name</th>
49 <th>Format</th>
50 <th>Description</th>
51 </tr>
52 <tr>
53 <td>byte</td>
54 <td>unsigned char array</td>
55 <td>This stores the Uuid of the server on which transaction
56 is originated</td>
57 </tr>
58 </table>
59*/
60
61struct Uuid {
62 /// Set to all zeros.
63 void clear() { memset(bytes, 0, BYTE_LENGTH); }
64 /// Copies the given 16-byte data to this UUID.
65 void copy_from(const unsigned char *data) {
66 memcpy(bytes, data, BYTE_LENGTH);
67 }
68 /// Copies the given UUID object to this UUID.
69 void copy_from(const Uuid &data) {
70 copy_from(pointer_cast<const unsigned char *>(data.bytes));
71 }
72 /// Copies the given UUID object to this UUID.
73 void copy_to(unsigned char *data) const { memcpy(data, bytes, BYTE_LENGTH); }
74 /// Returns true if this UUID is equal the given UUID.
75 bool equals(const Uuid &other) const {
76 return memcmp(bytes, other.bytes, BYTE_LENGTH) == 0;
77 }
78 /**
79 Returns true if parse() would succeed, but doesn't store the result.
80
81 @param string String that needs to be checked.
82 @param len Length of that string.
83
84 @retval true valid string.
85 @retval false invalid string.
86 */
87 static bool is_valid(const char *string, size_t len);
88
89 /**
90 Stores the UUID represented by a string of the form
91 XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX or
92 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX or
93 {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
94 in this object.
95
96 @param string String to be parsed and stored.
97 @param len Length of that string.
98
99 @retval 0 success.
100 @retval >0 failure.
101 */
102 int parse(const char *string, size_t len);
103
104 /**
105 Parses the UUID passed as argument in in_string and functions and writes
106 the binary representation in out_binary_string.
107 Depends on UUID's read_section method and the constants for text length.
108
109 @param[in] in_string String to be parsed.
110 @param[in] len Length of that string.
111 @param[out] out_binary_string String where the binary UUID will be stored
112
113 @retval 0 success.
114 @retval >0 failure.
115 */
116 static int parse(const char *in_string, size_t len,
117 const unsigned char *out_binary_string);
118 /**
119 Helper method used to validate and parse one section of a uuid.
120 If the last parameter, out_binary_str, is NULL then the function will
121 just validate the section.
122
123 @param[in] section_len Length of the section to be parsed.
124 @param[in,out] section_str Pointer to a string containing the
125 section. It will be updated during the
126 execution as the string is parsed.
127 @param[out] out_binary_str String where the section will be stored
128 in binary format. If null, the function
129 will just validate the input string.
130
131 @retval false success.
132 @retval true failure.
133 */
134 static bool read_section(int section_len, const char **section_str,
135 const unsigned char **out_binary_str);
136 /** The number of bytes in the data of a Uuid. */
137 static const size_t BYTE_LENGTH = 16;
138 /** The data for this Uuid. */
139 unsigned char bytes[BYTE_LENGTH];
140 /**
141 Generates a 36+1 character long representation of this UUID object
142 in the given string buffer.
143
144 @retval 36 - the length of the resulting string.
145 */
146 size_t to_string(char *buf) const;
147 /// Convert the given binary buffer to a UUID
148 static size_t to_string(const unsigned char *bytes_arg, char *buf);
149 std::string to_string() const {
150 char buf[TEXT_LENGTH + 1];
151 to_string(buf);
152 return buf;
153 }
154 void print() const {
155 char buf[TEXT_LENGTH + 1];
156 to_string(buf);
157 printf("%s\n", buf);
158 }
159 /// The number of bytes in the textual representation of a Uuid.
160 static const size_t TEXT_LENGTH = 36;
161 /// The number of bits in the data of a Uuid.
162 static const size_t BIT_LENGTH = 128;
163 static const int NUMBER_OF_SECTIONS = 5;
165 static const int hex_to_byte[256];
166};
167
168struct Hash_Uuid {
169 size_t operator()(const Uuid &uuid) const {
170 return std::hash<std::string>()(
171 std::string(pointer_cast<const char *>(uuid.bytes), Uuid::BYTE_LENGTH));
172 }
173};
174
175inline bool operator==(const Uuid &a, const Uuid &b) { return a.equals(b); }
176
177} // namespace binary_log
178
179#endif // UUID_H_INCLUDED
Definition: item_cmpfunc.h:1644
The namespace contains classes representing events that can occur in a replication stream.
bool operator==(const Uuid &a, const Uuid &b)
Definition: uuid.h:175
Definition: buf0block_hint.cc:30
Definition: uuid.h:168
size_t operator()(const Uuid &uuid) const
Definition: uuid.h:169
This is a POD.
Definition: uuid.h:61
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:90
unsigned char bytes[BYTE_LENGTH]
The data for this Uuid.
Definition: uuid.h:139
void copy_from(const unsigned char *data)
Copies the given 16-byte data to this UUID.
Definition: uuid.h:65
static const int NUMBER_OF_SECTIONS
Definition: uuid.h:163
static const size_t BIT_LENGTH
The number of bits in the data of a Uuid.
Definition: uuid.h:162
void copy_from(const Uuid &data)
Copies the given UUID object to this UUID.
Definition: uuid.h:69
static const size_t BYTE_LENGTH
The number of bytes in the data of a Uuid.
Definition: uuid.h:137
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:53
bool equals(const Uuid &other) const
Returns true if this UUID is equal the given UUID.
Definition: uuid.h:75
void print() const
Definition: uuid.h:154
static const size_t TEXT_LENGTH
The number of bytes in the textual representation of a Uuid.
Definition: uuid.h:160
static const int bytes_per_section[NUMBER_OF_SECTIONS]
Definition: uuid.h:164
static const int hex_to_byte[256]
Definition: uuid.h:165
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:110
void copy_to(unsigned char *data) const
Copies the given UUID object to this UUID.
Definition: uuid.h:73
void clear()
Set to all zeros.
Definition: uuid.h:63
std::string to_string() const
Definition: uuid.h:149