MySQL 9.6.0
Source Code Documentation
uuid.h
Go to the documentation of this file.
1// Copyright (c) 2025, 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_UUIDS_UUID_H
25#define MYSQL_UUIDS_UUID_H
26
27/// @file
28/// Experimental API header
29
30#include <array> // array
31#include <cstring> // memcpy, size_t
32#include <string_view> // string_view
33#include <type_traits> // is_trivially_default_constructible_v
34#include <utility> // ignore
35#include "mysql/strconv/strconv.h" // Parser
36
37/// @addtogroup GroupLibsMysqlUuids
38/// @{
39
40namespace mysql::uuids {
41
42/// Holds data for a UUID
43///
44/// This satisfies std::is_trivially_default_constructible,
45/// std::is_trivially_copyable, and std::is_standard_layout.
46///
47/// Use mysql::strconv::encode_text / mysql::strconv::decode_text to convert to
48/// and from text format.
50 public:
51 /// The number of bytes in the data of a Uuid.
52 static constexpr std::size_t byte_size = 16;
53 static constexpr std::size_t text_size = 36;
54 static constexpr std::size_t section_count = 5;
55 static constexpr std::array<std::size_t, section_count> section_sizes = {
56 4, 2, 2, 2, 6};
57
58 /// Return the size
59 [[nodiscard]] constexpr std::size_t size() const { return byte_size; }
60
61 /// Return the data buffer as `char *`.
62 [[nodiscard]] char *data() { return m_data.data(); }
63
64 /// Return the data buffer as `const char *`.
65 [[nodiscard]] const char *data() const { return m_data.data(); }
66
67 /// Copy `other` to this.
68 void assign(const Uuid &other) {
69 if (&other == this) return;
70 memcpy(data(), other.data(), size());
71 }
72
73 /// Copy `other`, which is represented as binary, to this.
75 const std::string_view &other) {
76 if (other.size() != byte_size) return mysql::utils::Return_status::error;
77 memcpy(data(), other.data(), size());
79 }
80
81 private:
82 /// The data for this Uuid.
83 std::array<char, byte_size> m_data;
84};
85
86static_assert(std::is_trivially_default_constructible_v<Uuid>);
87static_assert(std::is_trivially_copyable_v<Uuid>);
88static_assert(std::is_standard_layout_v<Uuid>);
89
90} // namespace mysql::uuids
91
92namespace mysql::strconv {
93
94/// Enable encode_text(Uuid).
95template <Is_string_target Target_t>
96void encode_impl(const Text_format &format [[maybe_unused]], Target_t &target,
97 const mysql::uuids::Uuid &uuid) {
98 if constexpr (Target_t::target_type == Target_type::counter) {
99 target.advance(mysql::uuids::Uuid::text_size);
100 } else {
101 bool first = true;
102 const auto *data = uuid.data();
103 const auto *data_pos = data;
104 [[maybe_unused]] auto *target_start_pos = target.pos();
106 if (!first) {
107 target.write_char('-');
108 }
109 first = false;
110 target.write(Hex_format{}, {data_pos, size});
111 data_pos += size;
112 }
113 assert(data_pos == uuid.end());
114 assert(target.pos() - target_start_pos == mysql::uuids::Uuid::text_size);
115 }
116}
117
118/// Enable decode(Text_format, string, Uuid).
120 mysql::uuids::Uuid &uuid) {
121 using Uuid_t = mysql::uuids::Uuid;
122 static constexpr auto return_ok = mysql::utils::Return_status::ok;
123
124 // Cursor to output.
125 auto *out = uuid.data();
126
127 // Decode `2*size` hex digits from input into `size` bytes of output, and
128 // advance the cursors into input and output.
129 auto read_hex = [&](std::size_t size) {
131 out_str_fixed_nz(out, size));
132 out += size;
133 return ret;
134 };
135
136 // Check for brace-enclosed format.
138 bool brace_format = parser.is_found();
139
140 // Whether we use hyphens or not. With braces, hyphens are mandatory;
141 // without braces, hyphens are optional.
142 // NOLINTNEXTLINE(performance-enum-size): silence clang-tidy's pointless hint
143 enum { yes, no, unknown } hyphen_format = brace_format ? yes : unknown;
144
145 bool first = true;
146 for (const auto &section_size : Uuid_t::section_sizes) {
147 if (!first) {
148 if (hyphen_format == unknown) {
150 hyphen_format = parser.is_found() ? yes : no;
151 } else if (hyphen_format == yes) {
152 if (parser.skip(format, "-") != return_ok) return;
153 }
154 }
155 first = false;
156 if (read_hex(section_size) != return_ok) return;
157 }
158
159 if (brace_format) {
160 std::ignore = parser.skip(format, "}"); // return anyways
161 }
162}
163
164/// Enable encode(Binary_format, Uuid).
165void encode_impl(const Binary_format &, Is_string_target auto &target,
166 const mysql::uuids::Uuid &uuid) {
167 target.write_raw(uuid.string_view());
168}
169
170/// Enable decode(Binary_format, Uuid).
172 mysql::uuids::Uuid &uuid) {
175 out_str_fixed_nz(uuid.data(), size)) !=
177 return;
179}
180
181} // namespace mysql::strconv
182
183// addtogroup GroupLibsMysqlUuids
184/// @}
185
186#endif // ifndef MYSQL_UUIDS_UUID_H
CRTP base class that provides a rich API for classes that behave like byte buffers.
Definition: buffer_interface.h:102
Object used to parse strings.
Definition: parser.h:69
Return_status_t read_to_out_str(const Is_parse_options_nocheck auto &opt, const Is_out_str auto &out_str)
Read from this object to the given Output String Wrapper, using a decode_impl function that takes an ...
Definition: parser.h:317
void skip(const Is_parse_options_optional auto &opt, const std::string_view &sv)
Skip occurrences of the literal string sv, if found.
Definition: parser.h:125
static Repeat exact(std::integral auto count)
Return a Repeat object representing exactly the given number of repetitions.
Definition: repeat.h:163
bool is_found() const
Return true if found_count() != 0.
Definition: parse_result.h:118
static Repeat_optional optional()
Return a Repeat_optional object representing zero or one instances.
Definition: repeat.h:52
Holds data for a UUID.
Definition: uuid.h:49
static constexpr std::array< std::size_t, section_count > section_sizes
Definition: uuid.h:55
static constexpr std::size_t text_size
Definition: uuid.h:53
std::array< char, byte_size > m_data
The data for this Uuid.
Definition: uuid.h:83
static constexpr std::size_t byte_size
The number of bytes in the data of a Uuid.
Definition: uuid.h:52
void assign(const Uuid &other)
Copy other to this.
Definition: uuid.h:68
mysql::utils::Return_status assign(const std::string_view &other)
Copy other, which is represented as binary, to this.
Definition: uuid.h:74
static constexpr std::size_t section_count
Definition: uuid.h:54
char * data()
Return the data buffer as char *.
Definition: uuid.h:62
const char * data() const
Return the data buffer as const char *.
Definition: uuid.h:65
constexpr std::size_t size() const
Return the size.
Definition: uuid.h:59
Concept that holds for String_counter and String_writer.
Definition: string_target.h:111
Experimental API header.
struct Parser parser
std::string format(const routing_guidelines::Session_info &session_info, bool extended_session_info)
Definition: dest_metadata_cache.cc:170
mysql::gtid::Uuid Uuid
Definition: global.h:38
Definition: gtid_binary_format.h:41
auto out_str_fixed_nz(mysql::meta::Is_pointer_to_charlike auto first, Size_t &length, Size_t capacity=0)
Return a wrapper around a non-growable, non-null-terminated output buffer, represented using a raw po...
Definition: out_str.h:818
void decode_impl(const Gtid_binary_format &format, Parser &parser, mysql::gtids::Is_tag auto &tag)
Definition: gtid_binary_format_conv.h:63
void encode_impl(const Gtid_binary_format &format, Is_string_target auto &target, const mysql::gtids::Is_tag auto &tag)
Definition: gtid_binary_format_conv.h:48
Return_status
Simple, strongly-typed enumeration to indicate internal status: ok, error.
Definition: return_status.h:40
@ ok
operation succeeded
@ error
operation failed
Definition: uuid.h:40
size_t size(const char *const c)
Definition: base64.h:46
Format tag to identify binary format.
Definition: binary_format.h:38
Format tag to identify fixed-length-string binary format.
Definition: fixstr_binary_format.h:38
Format tag to identify hex format when encoding and decoding strings.
Definition: hex_format.h:123
Format tag to identify text format.
Definition: text_format.h:38