1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 
6 #include "rocksdb/utilities/object_registry.h"
7 
8 #include "logging/logging.h"
9 #include "rocksdb/env.h"
10 
11 namespace ROCKSDB_NAMESPACE {
12 #ifndef ROCKSDB_LITE
13 // Looks through the "type" factories for one that matches "name".
14 // If found, returns the pointer to the Entry matching this name.
15 // Otherwise, nullptr is returned
FindEntry(const std::string & type,const std::string & name) const16 const ObjectLibrary::Entry *ObjectLibrary::FindEntry(
17     const std::string &type, const std::string &name) const {
18   auto entries = entries_.find(type);
19   if (entries != entries_.end()) {
20     for (const auto &entry : entries->second) {
21       if (entry->matches(name)) {
22         return entry.get();
23       }
24     }
25   }
26   return nullptr;
27 }
28 
AddEntry(const std::string & type,std::unique_ptr<Entry> & entry)29 void ObjectLibrary::AddEntry(const std::string &type,
30                              std::unique_ptr<Entry> &entry) {
31   auto &entries = entries_[type];
32   entries.emplace_back(std::move(entry));
33 }
34 
Dump(Logger * logger) const35 void ObjectLibrary::Dump(Logger *logger) const {
36   for (const auto &iter : entries_) {
37     ROCKS_LOG_HEADER(logger, "    Registered factories for type[%s] ",
38                      iter.first.c_str());
39     bool printed_one = false;
40     for (const auto &e : iter.second) {
41       ROCKS_LOG_HEADER(logger, "%c %s", (printed_one) ? ',' : ':',
42                        e->Name().c_str());
43       printed_one = true;
44     }
45   }
46   ROCKS_LOG_HEADER(logger, "\n");
47 }
48 
49 // Returns the Default singleton instance of the ObjectLibrary
50 // This instance will contain most of the "standard" registered objects
Default()51 std::shared_ptr<ObjectLibrary> &ObjectLibrary::Default() {
52   static std::shared_ptr<ObjectLibrary> instance =
53       std::make_shared<ObjectLibrary>();
54   return instance;
55 }
56 
NewInstance()57 std::shared_ptr<ObjectRegistry> ObjectRegistry::NewInstance() {
58   std::shared_ptr<ObjectRegistry> instance = std::make_shared<ObjectRegistry>();
59   return instance;
60 }
61 
ObjectRegistry()62 ObjectRegistry::ObjectRegistry() {
63   libraries_.push_back(ObjectLibrary::Default());
64 }
65 
66 // Searches (from back to front) the libraries looking for the
67 // an entry that matches this pattern.
68 // Returns the entry if it is found, and nullptr otherwise
FindEntry(const std::string & type,const std::string & name) const69 const ObjectLibrary::Entry *ObjectRegistry::FindEntry(
70     const std::string &type, const std::string &name) const {
71   for (auto iter = libraries_.crbegin(); iter != libraries_.crend(); ++iter) {
72     const auto *entry = iter->get()->FindEntry(type, name);
73     if (entry != nullptr) {
74       return entry;
75     }
76   }
77   return nullptr;
78 }
79 
Dump(Logger * logger) const80 void ObjectRegistry::Dump(Logger *logger) const {
81   for (auto iter = libraries_.crbegin(); iter != libraries_.crend(); ++iter) {
82     iter->get()->Dump(logger);
83   }
84 }
85 
86 #endif  // ROCKSDB_LITE
87 }  // namespace ROCKSDB_NAMESPACE
88