2019-07-24 00:08:26 +00:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
|
|
|
|
#include "rocksdb/utilities/object_registry.h"
|
|
|
|
|
2024-03-04 18:08:32 +00:00
|
|
|
#include <cctype>
|
2021-12-29 15:55:17 +00:00
|
|
|
|
2019-07-24 00:08:26 +00:00
|
|
|
#include "logging/logging.h"
|
2022-02-11 21:19:34 +00:00
|
|
|
#include "port/lang.h"
|
2021-09-10 12:19:47 +00:00
|
|
|
#include "rocksdb/customizable.h"
|
2019-07-24 00:08:26 +00:00
|
|
|
#include "rocksdb/env.h"
|
2021-09-10 12:19:47 +00:00
|
|
|
#include "util/string_util.h"
|
2019-07-24 00:08:26 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2022-02-16 19:11:55 +00:00
|
|
|
namespace {
|
|
|
|
bool MatchesInteger(const std::string &target, size_t start, size_t pos) {
|
|
|
|
// If it is numeric, everything up to the match must be a number
|
|
|
|
int digits = 0;
|
2022-02-18 20:23:48 +00:00
|
|
|
if (target[start] == '-') {
|
|
|
|
start++; // Allow negative numbers
|
|
|
|
}
|
2022-02-16 19:11:55 +00:00
|
|
|
while (start < pos) {
|
|
|
|
if (!isdigit(target[start++])) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
digits++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (digits > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MatchesDecimal(const std::string &target, size_t start, size_t pos) {
|
|
|
|
int digits = 0;
|
2022-02-18 20:23:48 +00:00
|
|
|
if (target[start] == '-') {
|
|
|
|
start++; // Allow negative numbers
|
|
|
|
}
|
2022-02-16 19:11:55 +00:00
|
|
|
for (bool point = false; start < pos; start++) {
|
|
|
|
if (target[start] == '.') {
|
|
|
|
if (point) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
point = true;
|
|
|
|
}
|
|
|
|
} else if (!isdigit(target[start])) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
digits++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (digits > 0);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2021-12-29 15:55:17 +00:00
|
|
|
size_t ObjectLibrary::PatternEntry::MatchSeparatorAt(
|
|
|
|
size_t start, Quantifier mode, const std::string &target, size_t tlen,
|
|
|
|
const std::string &separator) const {
|
|
|
|
size_t slen = separator.size();
|
|
|
|
// See if there is enough space. If so, find the separator
|
|
|
|
if (tlen < start + slen) {
|
|
|
|
return std::string::npos; // not enough space left
|
|
|
|
} else if (mode == kMatchExact) {
|
|
|
|
// Exact mode means the next thing we are looking for is the separator
|
|
|
|
if (target.compare(start, slen, separator) != 0) {
|
|
|
|
return std::string::npos;
|
|
|
|
} else {
|
|
|
|
return start + slen; // Found the separator, return where we found it
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto pos = start + 1;
|
|
|
|
if (!separator.empty()) {
|
|
|
|
pos = target.find(separator, pos);
|
|
|
|
}
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
return pos;
|
2022-02-16 19:11:55 +00:00
|
|
|
} else if (mode == kMatchInteger) {
|
|
|
|
if (!MatchesInteger(target, start, pos)) {
|
|
|
|
return std::string::npos;
|
|
|
|
}
|
|
|
|
} else if (mode == kMatchDecimal) {
|
|
|
|
if (!MatchesDecimal(target, start, pos)) {
|
|
|
|
return std::string::npos;
|
2019-07-24 00:08:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-29 15:55:17 +00:00
|
|
|
return pos + slen;
|
2019-07-24 00:08:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 15:55:17 +00:00
|
|
|
bool ObjectLibrary::PatternEntry::MatchesTarget(const std::string &name,
|
|
|
|
size_t nlen,
|
|
|
|
const std::string &target,
|
|
|
|
size_t tlen) const {
|
|
|
|
if (separators_.empty()) {
|
|
|
|
assert(optional_); // If there are no separators, it must be only a name
|
|
|
|
return nlen == tlen && name == target;
|
|
|
|
} else if (nlen == tlen) { // The lengths are the same
|
|
|
|
return optional_ && name == target;
|
|
|
|
} else if (tlen < nlen + slength_) {
|
|
|
|
// The target is not long enough
|
|
|
|
return false;
|
|
|
|
} else if (target.compare(0, nlen, name) != 0) {
|
|
|
|
return false; // Target does not start with name
|
|
|
|
} else {
|
|
|
|
// Loop through all of the separators one at a time matching them.
|
|
|
|
// Note that we first match the separator and then its quantifiers.
|
|
|
|
// Since we expect the separator first, we start with an exact match
|
|
|
|
// Subsequent matches will use the quantifier of the previous separator
|
|
|
|
size_t start = nlen;
|
|
|
|
auto mode = kMatchExact;
|
|
|
|
for (size_t idx = 0; idx < separators_.size(); ++idx) {
|
|
|
|
const auto &separator = separators_[idx];
|
|
|
|
start = MatchSeparatorAt(start, mode, target, tlen, separator.first);
|
|
|
|
if (start == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
mode = separator.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We have matched all of the separators. Now check that what is left
|
|
|
|
// unmatched in the target is acceptable.
|
|
|
|
if (mode == kMatchExact) {
|
|
|
|
return (start == tlen);
|
2022-01-11 14:32:42 +00:00
|
|
|
} else if (start > tlen || (start == tlen && mode != kMatchZeroOrMore)) {
|
2021-12-29 15:55:17 +00:00
|
|
|
return false;
|
2022-02-16 19:11:55 +00:00
|
|
|
} else if (mode == kMatchInteger) {
|
|
|
|
return MatchesInteger(target, start, tlen);
|
|
|
|
} else if (mode == kMatchDecimal) {
|
|
|
|
return MatchesDecimal(target, start, tlen);
|
2021-12-29 15:55:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjectLibrary::PatternEntry::Matches(const std::string &target) const {
|
|
|
|
auto tlen = target.size();
|
|
|
|
if (MatchesTarget(name_, nlength_, target, tlen)) {
|
|
|
|
return true;
|
|
|
|
} else if (!names_.empty()) {
|
|
|
|
for (const auto &alt : names_) {
|
|
|
|
if (MatchesTarget(alt, alt.size(), target, tlen)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2019-07-24 00:08:26 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 13:45:49 +00:00
|
|
|
size_t ObjectLibrary::GetFactoryCount(size_t *types) const {
|
2021-07-22 20:41:48 +00:00
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
2021-12-29 15:55:17 +00:00
|
|
|
*types = factories_.size();
|
2021-05-11 13:45:49 +00:00
|
|
|
size_t factories = 0;
|
2021-12-29 15:55:17 +00:00
|
|
|
for (const auto &e : factories_) {
|
2021-05-11 13:45:49 +00:00
|
|
|
factories += e.second.size();
|
|
|
|
}
|
|
|
|
return factories;
|
|
|
|
}
|
|
|
|
|
2022-05-16 16:44:43 +00:00
|
|
|
size_t ObjectLibrary::GetFactoryCount(const std::string &type) const {
|
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
|
|
|
auto iter = factories_.find(type);
|
|
|
|
if (iter != factories_.end()) {
|
|
|
|
return iter->second.size();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectLibrary::GetFactoryNames(const std::string &type,
|
|
|
|
std::vector<std::string> *names) const {
|
|
|
|
assert(names);
|
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
|
|
|
auto iter = factories_.find(type);
|
|
|
|
if (iter != factories_.end()) {
|
|
|
|
for (const auto &f : iter->second) {
|
|
|
|
names->push_back(f->Name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectLibrary::GetFactoryTypes(
|
|
|
|
std::unordered_set<std::string> *types) const {
|
|
|
|
assert(types);
|
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
|
|
|
for (const auto &iter : factories_) {
|
|
|
|
types->insert(iter.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:08:26 +00:00
|
|
|
void ObjectLibrary::Dump(Logger *logger) const {
|
2021-07-22 20:41:48 +00:00
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
2022-04-11 20:44:09 +00:00
|
|
|
if (logger != nullptr && !factories_.empty()) {
|
|
|
|
ROCKS_LOG_HEADER(logger, " Registered Library: %s\n", id_.c_str());
|
|
|
|
for (const auto &iter : factories_) {
|
|
|
|
ROCKS_LOG_HEADER(logger, " Registered factories for type[%s] ",
|
|
|
|
iter.first.c_str());
|
|
|
|
bool printed_one = false;
|
|
|
|
for (const auto &e : iter.second) {
|
|
|
|
ROCKS_LOG_HEADER(logger, "%c %s", (printed_one) ? ',' : ':', e->Name());
|
|
|
|
printed_one = true;
|
|
|
|
}
|
2019-07-24 00:08:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the Default singleton instance of the ObjectLibrary
|
|
|
|
// This instance will contain most of the "standard" registered objects
|
|
|
|
std::shared_ptr<ObjectLibrary> &ObjectLibrary::Default() {
|
2022-02-11 21:19:34 +00:00
|
|
|
// Use avoid destruction here so the default ObjectLibrary will not be
|
|
|
|
// statically destroyed and long-lived.
|
|
|
|
STATIC_AVOID_DESTRUCTION(std::shared_ptr<ObjectLibrary>, instance)
|
|
|
|
(std::make_shared<ObjectLibrary>("default"));
|
2019-07-24 00:08:26 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2022-04-11 20:44:09 +00:00
|
|
|
ObjectRegistry::ObjectRegistry(const std::shared_ptr<ObjectLibrary> &library) {
|
|
|
|
libraries_.push_back(library);
|
|
|
|
for (const auto &b : builtins_) {
|
|
|
|
RegisterPlugin(b.first, b.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 13:45:49 +00:00
|
|
|
std::shared_ptr<ObjectRegistry> ObjectRegistry::Default() {
|
2022-02-11 21:19:34 +00:00
|
|
|
// Use avoid destruction here so the default ObjectRegistry will not be
|
|
|
|
// statically destroyed and long-lived.
|
|
|
|
STATIC_AVOID_DESTRUCTION(std::shared_ptr<ObjectRegistry>, instance)
|
|
|
|
(std::make_shared<ObjectRegistry>(ObjectLibrary::Default()));
|
2019-07-24 00:08:26 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2021-05-11 13:45:49 +00:00
|
|
|
std::shared_ptr<ObjectRegistry> ObjectRegistry::NewInstance() {
|
|
|
|
return std::make_shared<ObjectRegistry>(Default());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<ObjectRegistry> ObjectRegistry::NewInstance(
|
|
|
|
const std::shared_ptr<ObjectRegistry> &parent) {
|
|
|
|
return std::make_shared<ObjectRegistry>(parent);
|
2019-07-24 00:08:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 12:19:47 +00:00
|
|
|
Status ObjectRegistry::SetManagedObject(
|
|
|
|
const std::string &type, const std::string &id,
|
|
|
|
const std::shared_ptr<Customizable> &object) {
|
|
|
|
std::string object_key = ToManagedObjectKey(type, id);
|
|
|
|
std::shared_ptr<Customizable> curr;
|
|
|
|
if (parent_ != nullptr) {
|
|
|
|
curr = parent_->GetManagedObject(type, id);
|
|
|
|
}
|
|
|
|
if (curr == nullptr) {
|
|
|
|
// We did not find the object in any parent. Update in the current
|
|
|
|
std::unique_lock<std::mutex> lock(objects_mutex_);
|
|
|
|
auto iter = managed_objects_.find(object_key);
|
|
|
|
if (iter != managed_objects_.end()) { // The object exists
|
|
|
|
curr = iter->second.lock();
|
|
|
|
if (curr != nullptr && curr != object) {
|
|
|
|
return Status::InvalidArgument("Object already exists: ", object_key);
|
|
|
|
} else {
|
|
|
|
iter->second = object;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The object does not exist. Add it
|
|
|
|
managed_objects_[object_key] = object;
|
|
|
|
}
|
|
|
|
} else if (curr != object) {
|
|
|
|
return Status::InvalidArgument("Object already exists: ", object_key);
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Customizable> ObjectRegistry::GetManagedObject(
|
|
|
|
const std::string &type, const std::string &id) const {
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(objects_mutex_);
|
|
|
|
auto iter = managed_objects_.find(ToManagedObjectKey(type, id));
|
|
|
|
if (iter != managed_objects_.end()) {
|
|
|
|
return iter->second.lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (parent_ != nullptr) {
|
|
|
|
return parent_->GetManagedObject(type, id);
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status ObjectRegistry::ListManagedObjects(
|
|
|
|
const std::string &type, const std::string &name,
|
|
|
|
std::vector<std::shared_ptr<Customizable>> *results) const {
|
|
|
|
{
|
|
|
|
std::string key = ToManagedObjectKey(type, name);
|
|
|
|
std::unique_lock<std::mutex> lock(objects_mutex_);
|
|
|
|
for (auto iter = managed_objects_.lower_bound(key);
|
|
|
|
iter != managed_objects_.end() && StartsWith(iter->first, key);
|
|
|
|
++iter) {
|
|
|
|
auto shared = iter->second.lock();
|
|
|
|
if (shared != nullptr) {
|
|
|
|
if (name.empty() || shared->IsInstanceOf(name)) {
|
|
|
|
results->emplace_back(shared);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (parent_ != nullptr) {
|
|
|
|
return parent_->ListManagedObjects(type, name, results);
|
|
|
|
} else {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
}
|
2019-07-24 00:08:26 +00:00
|
|
|
|
2022-05-16 16:44:43 +00:00
|
|
|
// Returns the number of registered types for this registry.
|
|
|
|
// If specified (not-null), types is updated to include the names of the
|
|
|
|
// registered types.
|
|
|
|
size_t ObjectRegistry::GetFactoryCount(const std::string &type) const {
|
|
|
|
size_t count = 0;
|
|
|
|
if (parent_ != nullptr) {
|
|
|
|
count = parent_->GetFactoryCount(type);
|
|
|
|
}
|
|
|
|
std::unique_lock<std::mutex> lock(library_mutex_);
|
|
|
|
for (const auto &library : libraries_) {
|
|
|
|
count += library->GetFactoryCount(type);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectRegistry::GetFactoryNames(const std::string &type,
|
|
|
|
std::vector<std::string> *names) const {
|
|
|
|
assert(names);
|
|
|
|
names->clear();
|
|
|
|
if (parent_ != nullptr) {
|
|
|
|
parent_->GetFactoryNames(type, names);
|
|
|
|
}
|
|
|
|
std::unique_lock<std::mutex> lock(library_mutex_);
|
|
|
|
for (const auto &library : libraries_) {
|
|
|
|
library->GetFactoryNames(type, names);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectRegistry::GetFactoryTypes(
|
|
|
|
std::unordered_set<std::string> *types) const {
|
|
|
|
assert(types);
|
|
|
|
if (parent_ != nullptr) {
|
|
|
|
parent_->GetFactoryTypes(types);
|
|
|
|
}
|
|
|
|
std::unique_lock<std::mutex> lock(library_mutex_);
|
|
|
|
for (const auto &library : libraries_) {
|
|
|
|
library->GetFactoryTypes(types);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:08:26 +00:00
|
|
|
void ObjectRegistry::Dump(Logger *logger) const {
|
2022-04-11 20:44:09 +00:00
|
|
|
if (logger != nullptr) {
|
2021-09-10 12:19:47 +00:00
|
|
|
std::unique_lock<std::mutex> lock(library_mutex_);
|
2022-04-11 20:44:09 +00:00
|
|
|
if (!plugins_.empty()) {
|
|
|
|
ROCKS_LOG_HEADER(logger, " Registered Plugins:");
|
|
|
|
bool printed_one = false;
|
|
|
|
for (const auto &plugin : plugins_) {
|
|
|
|
ROCKS_LOG_HEADER(logger, "%s%s", (printed_one) ? ", " : " ",
|
|
|
|
plugin.c_str());
|
|
|
|
printed_one = true;
|
|
|
|
}
|
|
|
|
ROCKS_LOG_HEADER(logger, "\n");
|
|
|
|
}
|
2021-09-10 12:19:47 +00:00
|
|
|
for (auto iter = libraries_.crbegin(); iter != libraries_.crend(); ++iter) {
|
|
|
|
iter->get()->Dump(logger);
|
|
|
|
}
|
2019-07-24 00:08:26 +00:00
|
|
|
}
|
2021-05-11 13:45:49 +00:00
|
|
|
if (parent_ != nullptr) {
|
|
|
|
parent_->Dump(logger);
|
|
|
|
}
|
2019-07-24 00:08:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 20:44:09 +00:00
|
|
|
int ObjectRegistry::RegisterPlugin(const std::string &name,
|
|
|
|
const RegistrarFunc &func) {
|
|
|
|
if (!name.empty() && func != nullptr) {
|
|
|
|
plugins_.push_back(name);
|
|
|
|
return AddLibrary(name)->Register(func, name);
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|