MySQL 9.3.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 return helper::json::merge_objects(options, parent_options,
142 k_non_inherited_options);
143}
144
145inline std::optional<std::string> get_endpoint_options(
146 const std::shared_ptr<DbServiceEndpoint> &endpoint) {
147 return endpoint->get()->options;
148}
149
150inline std::optional<std::string> get_endpoint_options(
151 const std::shared_ptr<DbSchemaEndpoint> &endpoint) {
152 const auto &option = endpoint->get()->options;
153
154 auto parent =
155 std::dynamic_pointer_cast<DbServiceEndpoint>(endpoint->get_parent_ptr());
156 if (!parent) return option;
157
158 const auto &parent_options = get_endpoint_options(parent);
159 return merge_options(option, parent_options);
160}
161
162inline std::optional<std::string> get_endpoint_options(
163 const std::shared_ptr<DbObjectEndpoint> &endpoint) {
164 const auto &option = endpoint->get()->options;
165
166 auto parent =
167 std::dynamic_pointer_cast<DbSchemaEndpoint>(endpoint->get_parent_ptr());
168 if (!parent) return option;
169
170 const auto &parent_options = get_endpoint_options(parent);
171 return merge_options(option, parent_options);
172}
173
174inline std::optional<std::string> get_endpoint_options(
175 const std::shared_ptr<ContentSetEndpoint> &endpoint) {
176 const auto &option = endpoint->get()->options;
177
178 auto parent =
179 std::dynamic_pointer_cast<DbServiceEndpoint>(endpoint->get_parent_ptr());
180 if (!parent) return option;
181
182 const auto &parent_options = get_endpoint_options(parent);
183 return merge_options(option, parent_options);
184}
185
186inline std::optional<std::string> get_endpoint_options(
187 const std::shared_ptr<ContentFileEndpoint> &endpoint) {
188 const auto &option = endpoint->get()->options;
189
190 auto parent =
191 std::dynamic_pointer_cast<ContentSetEndpoint>(endpoint->get_parent_ptr());
192 if (!parent) return option;
193
194 const auto &parent_options = get_endpoint_options(parent);
195 return merge_options(option, parent_options);
196}
197
199 std::shared_ptr<DbServiceEndpoint> &endpoint) {
200 auto entry = endpoint->get();
201
202 return entry->url_protocols;
203}
204
205template <typename Endpoint>
206Protocols get_endpoint_protocol(std::shared_ptr<Endpoint> &endpoint) {
207 auto parent = lock_parent(endpoint);
208 if (!parent) return {};
209
210 return get_endpoint_protocol(parent);
211}
212
213} // namespace handler
214} // namespace endpoint
215} // namespace mrs
216
217#endif /* ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_ENDPOINT_HANDLER_UTILITIES_H_ \
218 */
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4618
Definition: content_file_endpoint.h:41
Definition: error.h:37
const EndpointBasePtr get_parent_ptr() const
Definition: endpoint_base.h:184
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
std::string merge_objects(const std::string &j1, const std::string &j2, const std::set< std::string > &skip_attributes)
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:145
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:198
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