MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
universal_id.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_REST_MRS_SRC_MRS_DATABASE_ENTRY_UNIVERSAL_ID_H_
27#define ROUTER_SRC_REST_MRS_SRC_MRS_DATABASE_ENTRY_UNIVERSAL_ID_H_
28
29#include <algorithm>
30#include <array>
31#include <cassert>
32#include <compare>
33#include <cstring>
34#include <optional>
35#include <utility>
36
38
39#include "my_compiler.h"
40
41namespace mrs {
42namespace database {
43namespace entry {
44
46 constexpr static uint64_t k_size = 16;
47 using Array = std::array<uint8_t, k_size>;
48
49 UniversalId() = default;
50
51 UniversalId(std::initializer_list<uint8_t> v) {
52 assert(v.size() <= raw.size());
53 std::copy_n(v.begin(), std::min(v.size(), raw.size()), std::begin(raw));
54 }
55
56 UniversalId(const Array &v) { raw = v; }
57
59
60 bool empty() const {
61 for (uint8_t v : raw) {
62 if (0 != v) return false;
63 }
64 return true;
65 }
66
67 auto begin() const { return std::begin(raw); }
68 auto end() const { return std::end(raw); }
69
71 MY_COMPILER_GCC_DIAGNOSTIC_IGNORE("-Wzero-as-null-pointer-constant")
72 MY_COMPILER_CLANG_DIAGNOSTIC_IGNORE("-Wzero-as-null-pointer-constant")
73 std::strong_ordering operator<=>(const UniversalId &rhs) const {
74 for (size_t ndx = raw.size() - 1; ndx > 0; --ndx) {
75 auto cmp_res = raw[ndx] <=> rhs.raw[ndx];
76
77 if (cmp_res != 0) return cmp_res;
78 }
79
80 return raw[0] <=> rhs.raw[0];
81 }
83
84 bool operator==(const UniversalId &rhs) const = default;
85
86 static UniversalId from_cstr(const char *p, uint32_t length) {
87 if (length != k_size) return {};
89 from_raw(&result, p);
90 return result;
91 }
92
93 const char *to_raw() const {
94 return reinterpret_cast<const char *>(raw.data());
95 }
96
97 static void from_raw(UniversalId *uid, const char *binray) {
98 memcpy(uid->raw.data(), binray, k_size);
99 }
100
101 static void from_raw_zero_on_null(UniversalId *uid, const char *binray) {
102 if (binray)
103 memcpy(uid->raw.data(), binray, k_size);
104 else
105 memset(uid->raw.data(), 0, k_size);
106 }
107
108 static void from_raw_optional(std::optional<UniversalId> *uid,
109 const char *binray) {
110 if (binray) {
112 from_raw(&result, binray);
113 *uid = std::move(result);
114 } else {
115 (*uid).reset();
116 }
117 }
118
119 std::string to_string() const {
120 // lower-case hex
121 constexpr std::array<char, 16> hex_chars = {
122 '0', '1', '2', '3', '4', '5', '6', '7', //
123 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
124 };
125
126 std::string out;
127 out.reserve(raw.size() * 2);
128
129 for (auto ch : raw) {
130 out += hex_chars[ch >> 4];
131 out += hex_chars[ch & 0xf];
132 }
133
134 return out;
135 }
136};
137
140 result << ud.to_string();
141 return result;
142}
143
144inline std::string to_string(const UniversalId &ud) { return ud.to_string(); }
145
147 const UniversalId &ud) {
148 sql << to_sqlstring(ud);
149
150 return sql;
151}
152
153} // namespace entry
154} // namespace database
155} // namespace mrs
156
157#endif // ROUTER_SRC_REST_MRS_SRC_MRS_DATABASE_ENTRY_UNIVERSAL_ID_H_
Definition: utils_sqlstring.h:67
const char * p
Definition: ctype-mb.cc:1227
Header for compiler-dependent features.
#define MY_COMPILER_GCC_DIAGNOSTIC_IGNORE(X)
Definition: my_compiler.h:242
#define MY_COMPILER_DIAGNOSTIC_PUSH()
save the compiler's diagnostic (enabled warnings, errors, ...) state
Definition: my_compiler.h:277
#define MY_COMPILER_DIAGNOSTIC_POP()
restore the compiler's diagnostic (enabled warnings, errors, ...) state
Definition: my_compiler.h:278
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
mysqlrouter::sqlstring to_sqlstring(const UniversalId &ud)
Definition: universal_id.h:138
helper::json::SerializerToText & operator<<(helper::json::SerializerToText &stt, const UniversalId &id)
Definition: auth_app.h:59
std::string to_string(const AuthApp &entry)
Definition: auth_app.h:66
Definition: authorize_manager.h:48
const char * begin(const char *const c)
Definition: base64.h:44
Definition: gcs_xcom_synode.h:64
struct result result
Definition: result.h:34
MY_COMPILER_CLANG_DIAGNOSTIC_IGNORE("-Winconsistent-missing-destructor-override") static Scope_guard static_guard([]()
Definition: protobuf_plugin.cc:33
Definition: completion_hash.h:35
Definition: universal_id.h:45
const char * to_raw() const
Definition: universal_id.h:93
auto end() const
Definition: universal_id.h:68
UniversalId(std::initializer_list< uint8_t > v)
Definition: universal_id.h:51
static UniversalId from_cstr(const char *p, uint32_t length)
Definition: universal_id.h:86
auto begin() const
Definition: universal_id.h:67
Array raw
Definition: universal_id.h:58
std::array< uint8_t, k_size > Array
Definition: universal_id.h:47
UniversalId(const Array &v)
Definition: universal_id.h:56
static void from_raw_zero_on_null(UniversalId *uid, const char *binray)
Definition: universal_id.h:101
bool empty() const
Definition: universal_id.h:60
std::string to_string() const
Definition: universal_id.h:119
constexpr static uint64_t k_size
Definition: universal_id.h:46
static void from_raw(UniversalId *uid, const char *binray)
Definition: universal_id.h:97
static void from_raw_optional(std::optional< UniversalId > *uid, const char *binray)
Definition: universal_id.h:108
Definition: result.h:30