MySQL  8.0.18
Source Code Documentation
uuid.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
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 
35 namespace 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 
60 struct 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  void print() const {
149  char buf[TEXT_LENGTH + 1];
150  to_string(buf);
151  printf("%s\n", buf);
152  }
153  /// The number of bytes in the textual representation of a Uuid.
154  static const size_t TEXT_LENGTH = 36;
155  /// The number of bits in the data of a Uuid.
156  static const size_t BIT_LENGTH = 128;
157  static const int NUMBER_OF_SECTIONS = 5;
159  static const int hex_to_byte[256];
160 };
161 
162 struct Hash_Uuid {
163  size_t operator()(const Uuid &uuid) const {
164  return std::hash<std::string>()(
165  std::string(pointer_cast<const char *>(uuid.bytes), Uuid::BYTE_LENGTH));
166  }
167 };
168 
169 inline bool operator==(const Uuid &a, const Uuid &b) { return a.equals(b); }
170 
171 } // namespace binary_log
172 
173 #endif // UUID_H_INCLUDED
static const size_t BYTE_LENGTH
The number of bytes in the data of a Uuid.
Definition: uuid.h:136
size_t operator()(const Uuid &uuid) const
Definition: uuid.h:163
static const int hex_to_byte[256]
Definition: uuid.h:159
static const int bytes_per_section[NUMBER_OF_SECTIONS]
Definition: uuid.h:158
static const int NUMBER_OF_SECTIONS
Definition: uuid.h:157
unsigned char bytes[BYTE_LENGTH]
The data for this Uuid.
Definition: uuid.h:138
bool operator==(const Uuid &a, const Uuid &b)
Definition: uuid.h:169
Definition: uuid.h:162
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
Definition: item_cmpfunc.h:1469
void copy_to(unsigned char *data) const
Copies the given UUID object to this UUID.
Definition: uuid.h:72
static const size_t TEXT_LENGTH
The number of bytes in the textual representation of a Uuid.
Definition: uuid.h:154
size_t to_string(char *buf) const
Generates a 36+1 character long representation of this UUID object in the given string buffer...
Definition: uuid.cpp:134
bool equals(const Uuid &other) const
Returns true if this UUID is equal the given UUID.
Definition: uuid.h:74
static bool is_valid(const char *string, size_t len)
Returns true if parse() would succeed, but doesn&#39;t store the result.
Definition: uuid.cpp:109
void copy_from(const unsigned char *data)
Copies the given 16-byte data to this UUID.
Definition: uuid.h:64
void clear()
Set to all zeros.
Definition: uuid.h:62
void print() const
Definition: uuid.h:148
static const size_t BIT_LENGTH
The number of bits in the data of a Uuid.
Definition: uuid.h:156
The namespace contains classes representing events that can occur in a replication stream...
This is a POD.
Definition: uuid.h:60
void copy_from(const Uuid &data)
Copies the given UUID object to this UUID.
Definition: uuid.h:68
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