MySQL 9.6.0
Source Code Documentation
utilities.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, 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_MYSQL_REST_SERVICE_SRC_MRS_ENDPOINT_HANDLER_UTILITIES_H_
27#define ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_ENDPOINT_HANDLER_UTILITIES_H_
28
29#include <cassert>
30#include <memory>
31#include <optional>
32#include <set>
33#include <string>
34
35#include "helper/json/merge.h"
41#include "mrs/http/error.h"
42
43namespace mrs {
44namespace endpoint {
45namespace handler {
46
47using Protocols = std::set<std::string>;
48
49const uint64_t k_default_items_on_page = 25;
50
51template <typename Type>
52std::shared_ptr<Type> lock_or_throw_unavail(std::weak_ptr<Type> &endpoint) {
53 auto result = endpoint.lock();
54
56
57 return result;
58}
59
60template <typename Type>
61std::shared_ptr<Type> lock(const std::weak_ptr<Type> &endpoint) {
62 auto result = endpoint.lock();
63
64 assert(result &&
65 "The weak_ptr should be not expired, when calling any Handler "
66 "constructor.");
67
68 return result;
69}
70
71template <typename Type>
72const std::shared_ptr<Type> &lock(std::shared_ptr<Type> &endpoint) {
73 // Nothing to lock
74 return endpoint;
75}
76
78 auto result = url.get_host();
79 if (!result.empty()) {
80 auto port = url.get_port();
81 if (-1 != port) {
82 return result + ":" + std::to_string(port);
83 }
84 }
85 return result;
86}
87
88inline std::string get_endpoint_host(
89 std::weak_ptr<mrs::interface::EndpointBase> wp) {
90 auto endpoint = lock(wp);
91 if (!endpoint) return {};
92
93 return get_endpoint_host(endpoint->get_url());
94}
95
96inline std::shared_ptr<DbSchemaEndpoint> lock_parent(
97 const std::shared_ptr<DbObjectEndpoint> &endpoint) {
98 auto parent = endpoint->get_parent_ptr();
99 if (!parent) return {};
100
101 return std::dynamic_pointer_cast<DbSchemaEndpoint>(parent);
102}
103
104inline std::shared_ptr<DbServiceEndpoint> lock_parent(
105 const std::shared_ptr<DbSchemaEndpoint> &endpoint) {
106 auto parent = endpoint->get_parent_ptr();
107 if (!parent) return {};
108
109 return std::dynamic_pointer_cast<DbServiceEndpoint>(parent);
110}
111
112inline std::shared_ptr<ContentSetEndpoint> lock_parent(
113 ContentFileEndpoint *endpoint) {
114 auto parent = endpoint->get_parent_ptr();
115 if (!parent) return {};
116
117 return std::dynamic_pointer_cast<ContentSetEndpoint>(parent);
118}
119
120inline std::shared_ptr<ContentSetEndpoint> lock_parent(
121 const std::shared_ptr<ContentFileEndpoint> &endpoint) {
122 auto parent = endpoint->get_parent_ptr();
123 if (!parent) return {};
124
125 return std::dynamic_pointer_cast<ContentSetEndpoint>(parent);
126}
127
128inline std::shared_ptr<DbServiceEndpoint> lock_parent(
129 const std::shared_ptr<ContentSetEndpoint> &endpoint) {
130 auto parent = endpoint->get_parent_ptr();
131 if (!parent) return {};
132
133 return std::dynamic_pointer_cast<DbServiceEndpoint>(parent);
134}
135
136inline std::optional<std::string> merge_options(
137 std::optional<std::string> options,
138 std::optional<std::string> parent_options) {
139 static const std::set<std::string> k_non_inherited_options{
140 "directoryIndexDirective", "defaultStaticContent", "defaultRedirects"};
141 static const std::set<std::string> k_always_inherited_options{
142 "passthroughDbUser"};
143 return helper::json::merge_objects(options, parent_options,
144 k_non_inherited_options,
145 k_always_inherited_options);
146}
147
148inline std::optional<std::string> get_endpoint_options(
149 const std::shared_ptr<DbServiceEndpoint> &endpoint) {
150 return endpoint->get()->options;
151}
152
153inline std::optional<std::string> get_endpoint_options(
154 const std::shared_ptr<DbSchemaEndpoint> &endpoint) {
155 const auto &option = endpoint->get()->options;
156
157 auto parent =
158 std::dynamic_pointer_cast<DbServiceEndpoint>(endpoint->get_parent_ptr());
159 if (!parent) return option;
160
161 const auto &parent_options = get_endpoint_options(parent);
162 return merge_options(option, parent_options);
163}
164
165inline std::optional<std::string> get_endpoint_options(
166 const std::shared_ptr<DbObjectEndpoint> &endpoint) {
167 const auto &option = endpoint->get()->options;
168
169 auto parent =
170 std::dynamic_pointer_cast<DbSchemaEndpoint>(endpoint->get_parent_ptr());
171 if (!parent) return option;
172
173 const auto &parent_options = get_endpoint_options(parent);
174 return merge_options(option, parent_options);
175}
176
177inline std::optional<std::string> get_endpoint_options(
178 const std::shared_ptr<ContentSetEndpoint> &endpoint) {
179 const auto &option = endpoint->get()->options;
180
181 auto parent =
182 std::dynamic_pointer_cast<DbServiceEndpoint>(endpoint->get_parent_ptr());
183 if (!parent) return option;
184
185 const auto &parent_options = get_endpoint_options(parent);
186 return merge_options(option, parent_options);
187}
188
189inline std::optional<std::string> get_endpoint_options(
190 const std::shared_ptr<ContentFileEndpoint> &endpoint) {
191 const auto &option = endpoint->get()->options;
192
193 auto parent =
194 std::dynamic_pointer_cast<ContentSetEndpoint>(endpoint->get_parent_ptr());
195 if (!parent) return option;
196
197 const auto &parent_options = get_endpoint_options(parent);
198 return merge_options(option, parent_options);
199}
200
202 std::shared_ptr<DbServiceEndpoint> &endpoint) {
203 auto entry = endpoint->get();
204
205 return entry->url_protocols;
206}
207
208template <typename Endpoint>
209Protocols get_endpoint_protocol(std::shared_ptr<Endpoint> &endpoint) {
210 auto parent = lock_parent(endpoint);
211 if (!parent) return {};
212
213 return get_endpoint_protocol(parent);
214}
215
216} // namespace handler
217} // namespace endpoint
218} // namespace mrs
219
220#endif /* ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_ENDPOINT_HANDLER_UTILITIES_H_ \
221 */
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4754
Definition: content_file_endpoint.h:41
Definition: error.h:37
const EndpointBasePtr get_parent_ptr() const
Definition: endpoint_base.h:185
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:51
std::string merge_objects(const std::string &j1, const std::string &j2, const std::set< std::string > &skip_attributes, const std::set< std::string > &overwrite_attributes)
Merge fields from j2 into j1, which must be JSON objects.
Definition: merge.cc:34
constexpr key_type ServiceUnavailable
Definition: status_code.h:96
Request::Uri Uri
Definition: request.cc:36
std::shared_ptr< Type > lock_or_throw_unavail(std::weak_ptr< Type > &endpoint)
Definition: utilities.h:52
std::optional< std::string > get_endpoint_options(const std::shared_ptr< DbServiceEndpoint > &endpoint)
Definition: utilities.h:148
std::shared_ptr< DbSchemaEndpoint > lock_parent(const std::shared_ptr< DbObjectEndpoint > &endpoint)
Definition: utilities.h:96
const uint64_t k_default_items_on_page
Definition: utilities.h:49
std::optional< std::string > merge_options(std::optional< std::string > options, std::optional< std::string > parent_options)
Definition: utilities.h:136
std::string get_endpoint_host(const ::http::base::Uri &url)
Definition: utilities.h:77
std::shared_ptr< Type > lock(const std::weak_ptr< Type > &endpoint)
Definition: utilities.h:61
Protocols get_endpoint_protocol(std::shared_ptr< DbServiceEndpoint > &endpoint)
Definition: utilities.h:201
std::set< std::string > Protocols
Definition: utilities.h:47
Definition: authorize_manager.h:48
Definition: options.cc:57
struct result result
Definition: result.h:34
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
Definition: completion_hash.h:35
Definition: result.h:30