-
Notifications
You must be signed in to change notification settings - Fork 6
/
side_plugin_tpl_inst.cc
451 lines (417 loc) · 15.5 KB
/
side_plugin_tpl_inst.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "side_plugin_factory.h"
#include "rocksdb/env.h"
#include "rocksdb/options.h"
#include "options/db_options.h"
#include "rocksdb/compaction_filter.h"
#include "rocksdb/concurrent_task_limiter.h"
#include "rocksdb/flush_block_policy.h"
#include "rocksdb/merge_operator.h"
#include "rocksdb/rate_limiter.h"
#include "rocksdb/slice_transform.h"
#include "rocksdb/sst_file_manager.h"
#include "rocksdb/utilities/transaction_db_mutex.h"
#include "rocksdb/utilities/write_batch_with_index.h"
#include "rocksdb/wal_filter.h"
#include "logging/logging.h"
#include "util/string_util.h"
namespace ROCKSDB_NAMESPACE {
template<class Ptr>
struct PluginFactory<Ptr>::Meta {
AcqFunc acq;
//std::string base_class; // chain to base
};
template<class Ptr>
struct PluginFactory<Ptr>::Reg::Impl {
Impl(const Impl&) = delete;
Impl& operator=(const Impl&) = delete;
Impl() = default;
std::map<Slice, Meta> func_map;
//std::map<std::string, Ptr> inst_map;
static Impl& s_singleton();
};
template<class Ptr>
typename
PluginFactory<Ptr>::Reg::Impl&
PluginFactory<Ptr>::Reg::Impl::s_singleton() { static Impl imp; return imp; }
template<class Ptr>
PluginFactory<Ptr>::Reg::Reg(const char* class_name, AcqFunc acq,
const char* file, int line)
noexcept {
auto& imp = Impl::s_singleton();
Meta meta{acq};
auto ib = imp.func_map.emplace(class_name, std::move(meta));
if (!ib.second) {
fprintf(stderr,
"%s: FATAL: %s:%d: PluginFactory<%s>::Reg: dup class = %s\n",
StrDateTimeNow(), RocksLogShorterFileName(file), line,
demangle(typeid(Ptr)).c_str(), class_name);
abort();
}
if (SidePluginRepo::DebugLevel() >= 2) {
fprintf(stderr, "%s: INFO: %s:%d: PluginFactory<%s>::Reg: class = %s\n",
StrDateTimeNow(), RocksLogShorterFileName(file), line,
demangle(typeid(Ptr)).c_str(), class_name);
}
this->ipos = ib.first;
}
template<class Ptr>
PluginFactory<Ptr>::Reg::~Reg() {
// static const char* env = getenv("SidePlugin_unknown_double_free");
// static const bool val = env ? bool(atoi(env)) : false;
// if (val) return; // skip erase
auto& imp = Impl::s_singleton();
imp.func_map.erase(ipos);
}
template<class Ptr> SidePluginRepo::Impl::ObjMap<Ptr>::ObjMap() {
name2p = std::make_shared<std::map<std::string, Ptr> >();
}
template<class Ptr> SidePluginRepo::Impl::ObjMap<Ptr>::~ObjMap() = default;
template<class Ptr> PluginFactory<Ptr>::PluginFactory() = default;
template<class Ptr> PluginFactory<Ptr>::~PluginFactory() = default;
template<class Ptr>
Ptr
PluginFactory<Ptr>::AcquirePlugin(const std::string& clazz, const json& js,
const SidePluginRepo& repo) {
auto& imp = Reg::Impl::s_singleton();
auto iter = imp.func_map.find(clazz);
if (imp.func_map.end() != iter) {
Ptr ptr = iter->second.acq(js, repo);
TERARK_VERIFY_S(nullptr != ptr, "class = %s, js = %s", clazz, js.dump());
return ptr;
}
else {
//return Ptr(nullptr);
THROW_NotFound("class = " + clazz + ", params = " + js.dump());
}
}
template<class Ptr>
Ptr
PluginFactory<Ptr>::NullablePlugin(const std::string& clazz, const json& js,
const SidePluginRepo& repo) {
auto& imp = Reg::Impl::s_singleton();
auto iter = imp.func_map.find(clazz);
if (imp.func_map.end() != iter) {
Ptr ptr = iter->second.acq(js, repo);
TERARK_VERIFY_S(nullptr != ptr, "class = %s, js = %s", clazz, js.dump());
return ptr;
}
return Ptr(nullptr);
}
#define do_not_instantiate(Func, Ptr) \
static void \
Func(const char* varname, const char* func_name, \
const json& js, const SidePluginRepo& repo, Ptr* pp) { \
ROCKSDB_DIE("Should not goes here"); \
}
do_not_instantiate(GetNoAcq, std::shared_ptr<CFPropertiesWebView>)
do_not_instantiate(GetOrAcq, std::shared_ptr<CFPropertiesWebView>)
do_not_instantiate(GetNoAcq, std::shared_ptr<TableReader>)
do_not_instantiate(GetOrAcq, std::shared_ptr<TableReader>)
template<class T> do_not_instantiate(GetNoAcq, const PluginManipFunc<T>*)
template<class T> do_not_instantiate(GetOrAcq, const PluginManipFunc<T>*)
template<class T> do_not_instantiate(GetNoAcq, std::shared_ptr<SerDeFunc<T> >)
template<class T> do_not_instantiate(GetOrAcq, std::shared_ptr<SerDeFunc<T> >)
template<class Ptr>
static void
GetNoAcq(const char* varname, const char* func_name,
const json& js, const SidePluginRepo& repo, Ptr* pp) {
if (js.is_string()) {
const auto& str_val = js.get_ref<const std::string&>();
if (str_val.empty()) {
throw Status::InvalidArgument(
func_name, std::string(varname) + " inst_id/class_name is empty");
}
bool ret = false;
if ('$' == str_val[0]) {
if (str_val.size() < 3) {
throw Status::InvalidArgument(func_name,
std::string(varname) + " inst_id is too short");
}
const auto inst_id = PluginParseInstID(str_val);
ret = repo.Get(inst_id, pp);
} else {
ret = repo.Get(str_val, pp); // the whole str_val is inst_id
}
if (!ret) {
throw Status::NotFound(func_name,
std::string(varname) + "inst_id = " + str_val);
}
ROCKSDB_VERIFY(nullptr != *pp);
}
else {
throw Status::InvalidArgument(func_name,
std::string(varname) + " must be a string for reference to object");
}
}
///@param varname just for error report
///@param func_name just for error report
//
// if json is a string ${inst_id} or $inst_id, then Get the plugin named
// inst_id in repo.
//
// if json is a string does not like ${inst_id} or $inst_id, then the string
// is treated as a class name to create the plugin with empty json params.
//
// if json is an object, it should be { class: class_name, params: ... }
template<class Ptr>
static void
GetOrAcq(const char* varname, const char* func_name,
const json& js, const SidePluginRepo& repo, Ptr* pp) {
if (js.is_string()) {
const auto& str_val = js.get_ref<const std::string&>();
if (str_val.empty()) {
#if 1
// treat empty string json as null, because json convert to yaml
// may convert json null into empty string, convert such yaml back
// to json yield an empty string ""
#else
throw Status::InvalidArgument(func_name, std::string(varname) +
" inst_id/class_name is empty");
#endif
}
else if ('$' == str_val[0]) {
if (str_val.size() < 3) {
throw Status::InvalidArgument(func_name, std::string(varname) +
" inst_id = \"" + str_val + "\" is too short");
}
const auto inst_id = PluginParseInstID(str_val);
if (!repo.Get(inst_id, pp)) {
throw Status::NotFound(func_name,
std::string(varname) + " inst_id = \"" + inst_id + "\"");
}
ROCKSDB_VERIFY(nullptr != *pp);
} else {
// string which does not like ${inst_id} or $inst_id
// try to treat str_val as inst_id to Get it
if (repo.Get(str_val, pp)) {
ROCKSDB_VERIFY(nullptr != *pp);
}
else {
// now treat str_val as class name, try to --
// AcquirePlugin with empty json params
const std::string& clazz_name = str_val;
*pp = PluginFactory<Ptr>::AcquirePlugin(clazz_name, json{}, repo);
}
}
} else if (js.is_null()) {
// do nothing
} else if (js.is_object()) {
auto iter = js.find("class");
if (js.end() == iter) {
throw Status::InvalidArgument(func_name, "sub obj class is required");
}
if (!iter.value().is_string()) {
throw Status::InvalidArgument(func_name, "sub obj class must be string");
}
const auto& clazz_name = iter.value().get_ref<const std::string&>();
const json& params = js.at("params");
*pp = PluginFactory<Ptr>::AcquirePlugin(clazz_name, params, repo);
}
else {
throw Status::InvalidArgument(func_name,
"js must be string, null, or object, but is: " + js.dump());
}
}
template<class Ptr>
Ptr
PluginFactory<Ptr>::
GetPlugin(const char* varname, const char* func_name,
const json& js, const SidePluginRepo& repo) {
Ptr p(nullptr);
GetNoAcq(varname, func_name, js, repo, &p);
return p;
}
template<class Ptr>
Ptr
PluginFactory<Ptr>::
ObtainPlugin(const char* varname, const char* func_name,
const json& js, const SidePluginRepo& repo) {
Ptr p(nullptr);
GetOrAcq(varname, func_name, js, repo, &p);
return p;
}
template<class Ptr>
Ptr
PluginFactory<Ptr>::
AcquirePlugin(const json& js, const SidePluginRepo& repo) {
if (js.is_string()) {
const auto& str_val = js.get_ref<const std::string&>();
if (str_val.empty()) {
THROW_InvalidArgument("jstr class_name is empty");
}
// now treat js as class name, try to --
// AcquirePlugin with empty json params
const std::string& clazz_name = str_val;
return AcquirePlugin(clazz_name, json{}, repo);
} else if (js.is_null()) {
return Ptr(nullptr);
} else if (js.is_object()) {
auto iter = js.find("class");
if (js.end() == iter) {
THROW_InvalidArgument("js[\"class\"] is required: " + js.dump());
}
if (!iter.value().is_string()) {
THROW_InvalidArgument("js[\"class\"] must be string: " + js.dump());
}
const auto& clazz_name = iter.value().get_ref<const std::string&>();
const json& params = js.at("params");
return AcquirePlugin(clazz_name, params, repo);
}
THROW_InvalidArgument(
"js must be string, null, or object, but is: " + js.dump());
}
template<class Ptr>
Ptr
PluginFactory<Ptr>::
NullablePlugin(const json& js, const SidePluginRepo& repo) {
if (js.is_string()) {
const auto& str_val = js.get_ref<const std::string&>();
if (str_val.empty()) {
THROW_InvalidArgument("jstr class_name is empty");
}
// now treat js as class name, try to --
// NullablePlugin with empty json params
const std::string& clazz_name = str_val;
return NullablePlugin(clazz_name, json{}, repo);
} else if (js.is_null()) {
return Ptr(nullptr);
} else if (js.is_object()) {
auto iter = js.find("class");
if (js.end() == iter) {
THROW_InvalidArgument("js[\"class\"] is required: " + js.dump());
}
if (!iter.value().is_string()) {
THROW_InvalidArgument("js[\"class\"] must be string: " + js.dump());
}
const auto& clazz_name = iter.value().get_ref<const std::string&>();
const json& params = js.at("params");
return NullablePlugin(clazz_name, params, repo);
}
THROW_InvalidArgument(
"js must be string, null, or object, but is: " + js.dump());
}
template<class Ptr>
bool PluginFactory<Ptr>::HasPlugin(const std::string& class_name) {
auto& imp = Reg::Impl::s_singleton();
return imp.func_map.count(class_name) != 0;
}
// plugin can have alias class name, this function check whether the two
// aliases are defined as a same plugin
template<class Ptr>
bool PluginFactory<Ptr>::SamePlugin(const std::string& clazz1,
const std::string& clazz2) {
if (clazz1 == clazz2) {
return true;
}
auto& imp = Reg::Impl::s_singleton();
auto i1 = imp.func_map.find(clazz1);
auto i2 = imp.func_map.find(clazz2);
if (imp.func_map.end() == i1) {
THROW_NotFound("clazz1 = " + clazz1);
}
if (imp.func_map.end() == i2) {
THROW_NotFound("clazz2 = " + clazz2);
}
return i1->second.acq == i2->second.acq;
}
template<class Ptr>
std::string
PluginToString(const Ptr& p, const SidePluginRepo::Impl::ObjMap<Ptr>& map,
const json& js, const SidePluginRepo& repo) {
using Object = RemovePtr<Ptr>;
auto iter = map.p2name.find(GetRawPtr(p));
if (map.p2name.end() != iter) {
auto manip = PluginManip<Object>::NullablePlugin(iter->second.spec, repo);
if (manip)
return manip->ToString(*p, js, repo);
json with_note = {
{"note", "This Is Default Show"},
{"spec", iter->second.spec}
};
return JsonToString(with_note, js);
}
THROW_NotFound("Ptr not found");
}
template<class Ptr>
void PluginUpdate(const Ptr& p, const SidePluginRepo::Impl::ObjMap<Ptr>& map,
const json& query, const json& body,
const SidePluginRepo& repo) {
using Object = RemovePtr<Ptr>;
auto iter = map.p2name.find(GetRawPtr(p));
if (map.p2name.end() != iter) {
auto manip = PluginManip<Object>::AcquirePlugin(iter->second.spec, repo);
manip->Update(GetRawPtr(p), query, body, repo);
}
}
#define explicit_instantiate_sp(Interface) \
template std::string PluginToString<std::shared_ptr<Interface> >( \
const std::shared_ptr<Interface>&, \
const SidePluginRepo::Impl::ObjMap<std::shared_ptr<Interface>>&, \
const json&, const SidePluginRepo&); \
template void PluginUpdate<std::shared_ptr<Interface> >( \
const std::shared_ptr<Interface>&, \
const SidePluginRepo::Impl::ObjMap<std::shared_ptr<Interface> >&, \
const json& query, const json& body, const SidePluginRepo& repo); \
template class SidePluginRepo::Impl::ObjMap<std::shared_ptr<Interface> >; \
template class PluginFactory<std::shared_ptr<Interface> >; \
template class PluginFactory<const PluginManipFunc<Interface>*>
#define explicit_instantiate_rp(Interface) \
template std::string PluginToString<Interface*>( \
Interface* const &, \
const SidePluginRepo::Impl::ObjMap<Interface*>&, \
const json&, const SidePluginRepo&); \
template void PluginUpdate<Interface*>( \
Interface* const &, \
const SidePluginRepo::Impl::ObjMap<Interface*>&, \
const json& query, const json& body, const SidePluginRepo& repo); \
template class SidePluginRepo::Impl::ObjMap<Interface*>; \
template class PluginFactory<Interface*>; \
template class PluginFactory<const PluginManipFunc<Interface>*>
#define explicit_instantiate_serde(Interface) \
template class PluginFactory<std::shared_ptr<SerDeFunc<Interface> > >
explicit_instantiate_sp(AnyPlugin);
explicit_instantiate_sp(Cache);
explicit_instantiate_sp(PersistentCache);
explicit_instantiate_sp(CompactionExecutorFactory);
explicit_instantiate_sp(CompactionFilterFactory);
explicit_instantiate_sp(ConcurrentTaskLimiter);
explicit_instantiate_sp(EventListener);
explicit_instantiate_sp(FileChecksumGenFactory);
explicit_instantiate_sp(FileSystem);
explicit_instantiate_sp(const FilterPolicy);
explicit_instantiate_sp(FlushBlockPolicyFactory);
explicit_instantiate_sp(Logger);
explicit_instantiate_sp(MemoryAllocator);
explicit_instantiate_sp(MemTableRepFactory);
explicit_instantiate_sp(MergeOperator);
explicit_instantiate_sp(RateLimiter);
explicit_instantiate_sp(SstFileManager);
explicit_instantiate_sp(SstPartitionerFactory);
explicit_instantiate_sp(Statistics);
explicit_instantiate_sp(TableFactory);
explicit_instantiate_sp(TablePropertiesCollectorFactory);
explicit_instantiate_sp(TransactionDBMutexFactory);
explicit_instantiate_sp(WriteBufferManager);
explicit_instantiate_sp(const SliceTransform);
explicit_instantiate_sp(WBWIFactory);
explicit_instantiate_sp(Options);
explicit_instantiate_sp(DBOptions);
explicit_instantiate_sp(ColumnFamilyOptions);
explicit_instantiate_sp(CFPropertiesWebView);
explicit_instantiate_sp(TableReader);
explicit_instantiate_rp(const Comparator);
explicit_instantiate_rp(Env);
explicit_instantiate_rp(DB_MultiCF);
explicit_instantiate_rp(DB);
template class SidePluginRepo::Impl::ObjMap<DB_Ptr>;
template class SidePluginRepo::Impl::ObjMap<ColumnFamilyHandle*>;
explicit_instantiate_serde(AnyPlugin);
explicit_instantiate_serde(CompactionFilterFactory);
explicit_instantiate_serde(Comparator);
explicit_instantiate_serde(SliceTransform);
explicit_instantiate_serde(MergeOperator);
explicit_instantiate_serde(SstPartitionerFactory);
explicit_instantiate_serde(TableFactory);
explicit_instantiate_serde(EventListener);
explicit_instantiate_serde(TablePropertiesCollectorFactory);
} // ROCKSDB_NAMESPACE