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