Skip to content

Commit 97eec92

Browse files
committed
Refactor: CheckPrivilegeFunction & ServerCookieIface
Replace gsl::not_null<> with ref to the type This patch was created following these steps: 1. Use CLion's refactor signature functionality for each method in ServerCookieIface. 2. Update CheckPrivilegeFunction 3. Add/remove derefs or refs where needed, found by compiling the source code. 4. Update some methods of casting from CookieIface* to Cookie or MockCookie. Change-Id: I50bb6b07e5e708ecd917c3aa4f9082e5fce93620 Reviewed-on: http://review.couchbase.org/c/kv_engine/+/155741 Tested-by: Build Bot <[email protected]> Reviewed-by: Trond Norbye <[email protected]>
1 parent 5e768d6 commit 97eec92

File tree

23 files changed

+251
-306
lines changed

23 files changed

+251
-306
lines changed

auditd/src/audit.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ void AuditImpl::notify_event_state_changed(uint32_t id, bool enabled) const {
383383

384384
void AuditImpl::notify_io_complete(gsl::not_null<const CookieIface*> cookie,
385385
cb::engine_errc status) {
386-
cookie_api->notify_io_complete(cookie, status);
386+
cookie_api->notify_io_complete(*cookie, status);
387387
}
388388

389389
void AuditImpl::stats(const StatCollector& collector) {

auditd/tests/testauditd.cc

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,118 +42,110 @@ static bool ready = false;
4242

4343
class AuditMockServerCookieApi : public ServerCookieIface {
4444
public:
45-
void setDcpConnHandler(gsl::not_null<const CookieIface*> cookie,
45+
void setDcpConnHandler(const CookieIface& cookie,
4646
DcpConnHandlerIface* handler) override {
4747
throw std::runtime_error("Not implemented");
4848
}
49-
DcpConnHandlerIface* getDcpConnHandler(
50-
gsl::not_null<const CookieIface*> cookie) override {
49+
DcpConnHandlerIface* getDcpConnHandler(const CookieIface& cookie) override {
5150
throw std::runtime_error("Not implemented");
5251
}
53-
void setDcpFlowControlBufferSize(gsl::not_null<const CookieIface*> cookie,
52+
void setDcpFlowControlBufferSize(const CookieIface& cookie,
5453
std::size_t size) override {
5554
}
56-
void store_engine_specific(gsl::not_null<const CookieIface*> cookie,
55+
void store_engine_specific(const CookieIface& cookie,
5756
void* engine_data) override {
5857
throw std::runtime_error("Not implemented");
5958
}
60-
void* get_engine_specific(
61-
gsl::not_null<const CookieIface*> cookie) override {
59+
void* get_engine_specific(const CookieIface& cookie) override {
6260
throw std::runtime_error("Not implemented");
6361
}
64-
bool is_datatype_supported(gsl::not_null<const CookieIface*> cookie,
62+
bool is_datatype_supported(const CookieIface& cookie,
6563
protocol_binary_datatype_t datatype) override {
6664
throw std::runtime_error("Not implemented");
6765
}
68-
bool is_mutation_extras_supported(
69-
gsl::not_null<const CookieIface*> cookie) override {
66+
bool is_mutation_extras_supported(const CookieIface& cookie) override {
7067
throw std::runtime_error("Not implemented");
7168
}
72-
bool is_collections_supported(
73-
gsl::not_null<const CookieIface*> cookie) override {
69+
bool is_collections_supported(const CookieIface& cookie) override {
7470
throw std::runtime_error("Not implemented");
7571
}
7672
cb::mcbp::ClientOpcode get_opcode_if_ewouldblock_set(
77-
gsl::not_null<const CookieIface*> cookie) override {
73+
const CookieIface& cookie) override {
7874
throw std::runtime_error("Not implemented");
7975
}
80-
void notify_io_complete(gsl::not_null<const CookieIface*> cookie,
76+
void notify_io_complete(const CookieIface& cookie,
8177
cb::engine_errc status) override {
8278
std::lock_guard<std::mutex> lock(mutex);
8379
ready = true;
8480
cond.notify_one();
8581
}
86-
void scheduleDcpStep(gsl::not_null<const CookieIface*> cookie) override {
82+
void scheduleDcpStep(const CookieIface& cookie) override {
8783
throw std::runtime_error("Not implemented");
8884
}
8985

90-
void reserve(gsl::not_null<const CookieIface*> cookie) override {
86+
void reserve(const CookieIface& cookie) override {
9187
throw std::runtime_error("Not implemented");
9288
}
93-
void release(gsl::not_null<const CookieIface*> cookie) override {
89+
void release(const CookieIface& cookie) override {
9490
throw std::runtime_error("Not implemented");
9591
}
96-
void set_priority(gsl::not_null<const CookieIface*> cookie,
92+
void set_priority(const CookieIface& cookie,
9793
ConnectionPriority priority) override {
9894
throw std::runtime_error("Not implemented");
9995
}
100-
ConnectionPriority get_priority(
101-
gsl::not_null<const CookieIface*> cookie) override {
96+
ConnectionPriority get_priority(const CookieIface& cookie) override {
10297
throw std::runtime_error("Not implemented");
10398
}
104-
uint64_t get_connection_id(
105-
gsl::not_null<const CookieIface*> cookie) override {
99+
uint64_t get_connection_id(const CookieIface& cookie) override {
106100
throw std::runtime_error("Not implemented");
107101
}
108102
cb::rbac::PrivilegeAccess check_privilege(
109-
gsl::not_null<const CookieIface*> cookie,
103+
const CookieIface& cookie,
110104
cb::rbac::Privilege privilege,
111105
std::optional<ScopeID> sid,
112106
std::optional<CollectionID> cid) override {
113107
throw std::runtime_error("Not implemented");
114108
}
115109
cb::rbac::PrivilegeAccess test_privilege(
116-
gsl::not_null<const CookieIface*> cookie,
110+
const CookieIface& cookie,
117111
cb::rbac::Privilege privilege,
118112
std::optional<ScopeID> sid,
119113
std::optional<CollectionID> cid) override {
120114
throw std::runtime_error("Not implemented");
121115
}
122116
uint32_t get_privilege_context_revision(
123-
gsl::not_null<const CookieIface*> cookie) override {
117+
const CookieIface& cookie) override {
124118
throw std::runtime_error("Not implemented");
125119
}
126-
cb::mcbp::Status engine_error2mcbp(gsl::not_null<const CookieIface*> cookie,
120+
cb::mcbp::Status engine_error2mcbp(const CookieIface& cookie,
127121
cb::engine_errc code) override {
128122
throw std::runtime_error("Not implemented");
129123
}
130124
std::pair<uint32_t, std::string> get_log_info(
131-
gsl::not_null<const CookieIface*> cookie) override {
125+
const CookieIface& cookie) override {
132126
throw std::runtime_error("Not implemented");
133127
}
134-
std::string get_authenticated_user(
135-
gsl::not_null<const CookieIface*> cookie) override {
128+
std::string get_authenticated_user(const CookieIface& cookie) override {
136129
throw std::runtime_error("Not implemented");
137130
}
138-
in_port_t get_connected_port(
139-
gsl::not_null<const CookieIface*> cookie) override {
131+
in_port_t get_connected_port(const CookieIface& cookie) override {
140132
throw std::runtime_error("Not implemented");
141133
}
142-
void set_error_context(gsl::not_null<CookieIface*> cookie,
134+
void set_error_context(CookieIface& cookie,
143135
std::string_view message) override {
144136
throw std::runtime_error("Not implemented");
145137
}
146-
void set_error_json_extras(gsl::not_null<CookieIface*> cookie,
138+
void set_error_json_extras(CookieIface& cookie,
147139
const nlohmann::json& json) override {
148140
throw std::runtime_error("set_error_json_extras not implemented");
149141
}
150-
void set_unknown_collection_error_context(
151-
gsl::not_null<CookieIface*> cookie, uint64_t manifestUid) override {
142+
void set_unknown_collection_error_context(CookieIface& cookie,
143+
uint64_t manifestUid) override {
152144
throw std::runtime_error(
153145
"set_unknown_collection_error_context not implemented");
154146
}
155147
std::string_view get_inflated_payload(
156-
gsl::not_null<const CookieIface*> cookie,
148+
const CookieIface& cookie,
157149
const cb::mcbp::Request& request) override {
158150
throw std::runtime_error("get_inflated_payload not implemented");
159151
}

daemon/server_api.cc

Lines changed: 50 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
#include <phosphor/phosphor.h>
3131
#include <utilities/engine_errc_2_mcbp.h>
3232

33-
static Cookie& getCookie(gsl::not_null<const CookieIface*> void_cookie) {
34-
auto* ccookie = reinterpret_cast<const Cookie*>(void_cookie.get());
35-
return *const_cast<Cookie*>(ccookie);
33+
static Cookie& getCookie(const CookieIface& void_cookie) {
34+
auto& cookie = dynamic_cast<const Cookie&>(void_cookie);
35+
return const_cast<Cookie&>(cookie);
3636
}
3737

3838
struct ServerBucketApi : public ServerBucketIface {
@@ -109,82 +109,71 @@ struct ServerDocumentApi : public ServerDocumentIface {
109109
};
110110

111111
struct ServerCookieApi : public ServerCookieIface {
112-
void setDcpConnHandler(gsl::not_null<const CookieIface*> cookie,
112+
void setDcpConnHandler(const CookieIface& cookie,
113113
DcpConnHandlerIface* handler) override {
114114
getCookie(cookie).getConnection().setDcpConnHandlerIface(handler);
115115
}
116116

117-
DcpConnHandlerIface* getDcpConnHandler(
118-
gsl::not_null<const CookieIface*> cookie) override {
117+
DcpConnHandlerIface* getDcpConnHandler(const CookieIface& cookie) override {
119118
return getCookie(cookie).getConnection().getDcpConnHandlerIface();
120119
}
121120

122-
void setDcpFlowControlBufferSize(gsl::not_null<const CookieIface*> cookie,
121+
void setDcpFlowControlBufferSize(const CookieIface& cookie,
123122
std::size_t size) override {
124123
getCookie(cookie).getConnection().setDcpFlowControlBufferSize(size);
125124
}
126125

127-
void store_engine_specific(gsl::not_null<const CookieIface*> void_cookie,
126+
void store_engine_specific(const CookieIface& cookie,
128127
void* engine_data) override {
129-
const auto* cc = reinterpret_cast<const Cookie*>(void_cookie.get());
130-
auto* cookie = const_cast<Cookie*>(cc);
131-
cookie->setEngineStorage(engine_data);
128+
getCookie(cookie).setEngineStorage(engine_data);
132129
}
133130

134-
void* get_engine_specific(
135-
gsl::not_null<const CookieIface*> void_cookie) override {
136-
auto* cookie = reinterpret_cast<const Cookie*>(void_cookie.get());
137-
return cookie->getEngineStorage();
131+
void* get_engine_specific(const CookieIface& cookie) override {
132+
return getCookie(cookie).getEngineStorage();
138133
}
139134

140-
bool is_datatype_supported(gsl::not_null<const CookieIface*> void_cookie,
135+
bool is_datatype_supported(const CookieIface& cookie,
141136
protocol_binary_datatype_t datatype) override {
142-
auto* cookie = reinterpret_cast<const Cookie*>(void_cookie.get());
143-
return cookie->getConnection().isDatatypeEnabled(datatype);
137+
return getCookie(cookie).getConnection().isDatatypeEnabled(datatype);
144138
}
145139

146-
bool is_mutation_extras_supported(
147-
gsl::not_null<const CookieIface*> void_cookie) override {
148-
auto* cookie = reinterpret_cast<const Cookie*>(void_cookie.get());
149-
return cookie->getConnection().isSupportsMutationExtras();
140+
bool is_mutation_extras_supported(const CookieIface& cookie) override {
141+
return getCookie(cookie).getConnection().isSupportsMutationExtras();
150142
}
151143

152-
bool is_collections_supported(
153-
gsl::not_null<const CookieIface*> void_cookie) override {
154-
auto* cookie = reinterpret_cast<const Cookie*>(void_cookie.get());
155-
return cookie->getConnection().isCollectionsSupported();
144+
bool is_collections_supported(const CookieIface& cookie) override {
145+
return getCookie(cookie).getConnection().isCollectionsSupported();
156146
}
157147

158148
cb::mcbp::ClientOpcode get_opcode_if_ewouldblock_set(
159-
gsl::not_null<const CookieIface*> void_cookie) override {
160-
auto* cookie = reinterpret_cast<const Cookie*>(void_cookie.get());
149+
const CookieIface& void_cookie) override {
150+
auto& cookie = getCookie(void_cookie);
161151

162152
cb::mcbp::ClientOpcode opcode = cb::mcbp::ClientOpcode::Invalid;
163-
if (cookie->isEwouldblock()) {
153+
if (cookie.isEwouldblock()) {
164154
try {
165-
opcode =
166-
cb::mcbp::ClientOpcode(cookie->getHeader().getOpcode());
155+
opcode = cb::mcbp::ClientOpcode(cookie.getHeader().getOpcode());
167156
} catch (...) {
168157
// Don't barf out if the header isn't there
169158
}
170159
}
171160
return opcode;
172161
}
173162

174-
void notify_io_complete(gsl::not_null<const CookieIface*> cookie,
163+
void notify_io_complete(const CookieIface& cookie,
175164
cb::engine_errc status) override {
176165
notifyIoComplete(getCookie(cookie), status);
177166
}
178167

179-
void scheduleDcpStep(gsl::not_null<const CookieIface*> cookie) override {
168+
void scheduleDcpStep(const CookieIface& cookie) override {
180169
::scheduleDcpStep(getCookie(cookie));
181170
}
182171

183-
void reserve(gsl::not_null<const CookieIface*> void_cookie) override {
172+
void reserve(const CookieIface& void_cookie) override {
184173
getCookie(void_cookie).incrementRefcount();
185174
}
186175

187-
void release(gsl::not_null<const CookieIface*> void_cookie) override {
176+
void release(const CookieIface& void_cookie) override {
188177
auto& cookie = getCookie(void_cookie);
189178
auto& connection = cookie.getConnection();
190179
connection.getThread().eventBase.runInEventBaseThread([&cookie]() {
@@ -197,48 +186,45 @@ struct ServerCookieApi : public ServerCookieIface {
197186
});
198187
}
199188

200-
void set_priority(gsl::not_null<const CookieIface*> cookie,
189+
void set_priority(const CookieIface& cookie,
201190
ConnectionPriority priority) override {
202191
getCookie(cookie).getConnection().setPriority(priority);
203192
}
204193

205-
ConnectionPriority get_priority(
206-
gsl::not_null<const CookieIface*> void_cookie) override {
207-
auto* cookie = reinterpret_cast<const Cookie*>(void_cookie.get());
208-
return cookie->getConnection().getPriority();
194+
ConnectionPriority get_priority(const CookieIface& void_cookie) override {
195+
auto& cookie = dynamic_cast<const Cookie&>(void_cookie);
196+
return cookie.getConnection().getPriority();
209197
}
210198

211-
uint64_t get_connection_id(
212-
gsl::not_null<const CookieIface*> void_cookie) override {
213-
auto* cookie = reinterpret_cast<const Cookie*>(void_cookie.get());
214-
return uint64_t(&cookie->getConnection());
199+
uint64_t get_connection_id(const CookieIface& void_cookie) override {
200+
auto& cookie = dynamic_cast<const Cookie&>(void_cookie);
201+
return uint64_t(&cookie.getConnection());
215202
}
216203

217204
cb::rbac::PrivilegeAccess check_privilege(
218-
gsl::not_null<const CookieIface*> cookie,
205+
const CookieIface& cookie,
219206
cb::rbac::Privilege privilege,
220207
std::optional<ScopeID> sid,
221208
std::optional<CollectionID> cid) override {
222209
return getCookie(cookie).checkPrivilege(privilege, sid, cid);
223210
}
224211

225212
cb::rbac::PrivilegeAccess test_privilege(
226-
gsl::not_null<const CookieIface*> cookie,
213+
const CookieIface& cookie,
227214
cb::rbac::Privilege privilege,
228215
std::optional<ScopeID> sid,
229216
std::optional<CollectionID> cid) override {
230217
return getCookie(cookie).testPrivilege(privilege, sid, cid);
231218
}
232219

233220
uint32_t get_privilege_context_revision(
234-
gsl::not_null<const CookieIface*> cookie) override {
221+
const CookieIface& cookie) override {
235222
return getCookie(cookie).getPrivilegeContext().getGeneration();
236223
}
237-
cb::mcbp::Status engine_error2mcbp(
238-
gsl::not_null<const CookieIface*> void_cookie,
239-
cb::engine_errc code) override {
240-
const auto* cookie = reinterpret_cast<const Cookie*>(void_cookie.get());
241-
auto& connection = cookie->getConnection();
224+
cb::mcbp::Status engine_error2mcbp(const CookieIface& void_cookie,
225+
cb::engine_errc code) override {
226+
auto& cookie = dynamic_cast<const Cookie&>(void_cookie);
227+
auto& connection = cookie.getConnection();
242228

243229
code = connection.remapErrorCode(code);
244230
if (code == cb::engine_errc::disconnect) {
@@ -252,40 +238,37 @@ struct ServerCookieApi : public ServerCookieIface {
252238
}
253239

254240
std::pair<uint32_t, std::string> get_log_info(
255-
gsl::not_null<const CookieIface*> void_cookie) override {
256-
auto* cookie = reinterpret_cast<const Cookie*>(void_cookie.get());
257-
return std::make_pair(cookie->getConnection().getId(),
258-
cookie->getConnection().getDescription());
241+
const CookieIface& void_cookie) override {
242+
auto& cookie = dynamic_cast<const Cookie&>(void_cookie);
243+
return std::make_pair(cookie.getConnection().getId(),
244+
cookie.getConnection().getDescription());
259245
}
260246

261-
std::string get_authenticated_user(
262-
gsl::not_null<const CookieIface*> cookie) override {
247+
std::string get_authenticated_user(const CookieIface& cookie) override {
263248
return getCookie(cookie).getConnection().getUser().name;
264249
}
265250

266-
in_port_t get_connected_port(
267-
gsl::not_null<const CookieIface*> cookie) override {
251+
in_port_t get_connected_port(const CookieIface& cookie) override {
268252
return getCookie(cookie).getConnection().getParentPort();
269253
}
270254

271-
void set_error_context(gsl::not_null<CookieIface*> cookie,
255+
void set_error_context(CookieIface& cookie,
272256
std::string_view message) override {
273257
getCookie(cookie).setErrorContext(std::string{message});
274258
}
275259

276-
void set_error_json_extras(gsl::not_null<CookieIface*> cookie,
260+
void set_error_json_extras(CookieIface& cookie,
277261
const nlohmann::json& json) override {
278262
getCookie(cookie).setErrorJsonExtras(json);
279263
}
280264

281-
void set_unknown_collection_error_context(
282-
gsl::not_null<CookieIface*> cookie, uint64_t manifestUid) override {
265+
void set_unknown_collection_error_context(CookieIface& cookie,
266+
uint64_t manifestUid) override {
283267
getCookie(cookie).setUnknownCollectionErrorContext(manifestUid);
284268
}
285269

286-
std::string_view get_inflated_payload(
287-
gsl::not_null<const CookieIface*> cookie,
288-
const cb::mcbp::Request&) override {
270+
std::string_view get_inflated_payload(const CookieIface& cookie,
271+
const cb::mcbp::Request&) override {
289272
return getCookie(cookie).getInflatedInputPayload();
290273
}
291274
};

engines/default_engine/default_engine.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ void default_engine::generate_unknown_collection_response(
10561056
// Default engine does not support collection changes, so is always
10571057
// reporting 'unknown collection' against the epoch manifest (uid 0)
10581058
server.cookie->set_unknown_collection_error_context(
1059-
const_cast<CookieIface*>(cookie), 0);
1059+
const_cast<CookieIface&>(*cookie), 0);
10601060
}
10611061

10621062
std::pair<cb::engine_errc, vbucket_state_t> default_engine::getVBucket(

0 commit comments

Comments
 (0)