object_registry.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include "rocksdb/utilities/object_registry.h"
  6. #include "logging/logging.h"
  7. #include "rocksdb/env.h"
  8. namespace ROCKSDB_NAMESPACE {
  9. #ifndef ROCKSDB_LITE
  10. // Looks through the "type" factories for one that matches "name".
  11. // If found, returns the pointer to the Entry matching this name.
  12. // Otherwise, nullptr is returned
  13. const ObjectLibrary::Entry *ObjectLibrary::FindEntry(
  14. const std::string &type, const std::string &name) const {
  15. auto entries = entries_.find(type);
  16. if (entries != entries_.end()) {
  17. for (const auto &entry : entries->second) {
  18. if (entry->matches(name)) {
  19. return entry.get();
  20. }
  21. }
  22. }
  23. return nullptr;
  24. }
  25. void ObjectLibrary::AddEntry(const std::string &type,
  26. std::unique_ptr<Entry> &entry) {
  27. auto &entries = entries_[type];
  28. entries.emplace_back(std::move(entry));
  29. }
  30. void ObjectLibrary::Dump(Logger *logger) const {
  31. for (const auto &iter : entries_) {
  32. ROCKS_LOG_HEADER(logger, " Registered factories for type[%s] ",
  33. iter.first.c_str());
  34. bool printed_one = false;
  35. for (const auto &e : iter.second) {
  36. ROCKS_LOG_HEADER(logger, "%c %s", (printed_one) ? ',' : ':',
  37. e->Name().c_str());
  38. printed_one = true;
  39. }
  40. }
  41. ROCKS_LOG_HEADER(logger, "\n");
  42. }
  43. // Returns the Default singleton instance of the ObjectLibrary
  44. // This instance will contain most of the "standard" registered objects
  45. std::shared_ptr<ObjectLibrary> &ObjectLibrary::Default() {
  46. static std::shared_ptr<ObjectLibrary> instance =
  47. std::make_shared<ObjectLibrary>();
  48. return instance;
  49. }
  50. std::shared_ptr<ObjectRegistry> ObjectRegistry::NewInstance() {
  51. std::shared_ptr<ObjectRegistry> instance = std::make_shared<ObjectRegistry>();
  52. return instance;
  53. }
  54. ObjectRegistry::ObjectRegistry() {
  55. libraries_.push_back(ObjectLibrary::Default());
  56. }
  57. // Searches (from back to front) the libraries looking for the
  58. // an entry that matches this pattern.
  59. // Returns the entry if it is found, and nullptr otherwise
  60. const ObjectLibrary::Entry *ObjectRegistry::FindEntry(
  61. const std::string &type, const std::string &name) const {
  62. for (auto iter = libraries_.crbegin(); iter != libraries_.crend(); ++iter) {
  63. const auto *entry = iter->get()->FindEntry(type, name);
  64. if (entry != nullptr) {
  65. return entry;
  66. }
  67. }
  68. return nullptr;
  69. }
  70. void ObjectRegistry::Dump(Logger *logger) const {
  71. for (auto iter = libraries_.crbegin(); iter != libraries_.crend(); ++iter) {
  72. iter->get()->Dump(logger);
  73. }
  74. }
  75. #endif // ROCKSDB_LITE
  76. } // namespace ROCKSDB_NAMESPACE