customizable.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 2011-present, Facebook, Inc. 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/customizable.h"
  6. #include <sstream>
  7. #include "options/options_helper.h"
  8. #include "port/port.h"
  9. #include "rocksdb/convenience.h"
  10. #include "rocksdb/status.h"
  11. #include "rocksdb/utilities/options_type.h"
  12. #include "util/string_util.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. std::string Customizable::GetOptionName(const std::string& long_name) const {
  15. const std::string& name = Name();
  16. size_t name_len = name.size();
  17. if (long_name.size() > name_len + 1 &&
  18. long_name.compare(0, name_len, name) == 0 &&
  19. long_name.at(name_len) == '.') {
  20. return long_name.substr(name_len + 1);
  21. } else {
  22. return Configurable::GetOptionName(long_name);
  23. }
  24. }
  25. std::string Customizable::GenerateIndividualId() const {
  26. std::ostringstream ostr;
  27. ostr << Name() << "@" << static_cast<const void*>(this) << "#"
  28. << port::GetProcessID();
  29. return ostr.str();
  30. }
  31. Status Customizable::GetOption(const ConfigOptions& config_options,
  32. const std::string& opt_name,
  33. std::string* value) const {
  34. if (opt_name == OptionTypeInfo::kIdPropName()) {
  35. *value = GetId();
  36. return Status::OK();
  37. } else {
  38. return Configurable::GetOption(config_options, opt_name, value);
  39. }
  40. }
  41. std::string Customizable::SerializeOptions(const ConfigOptions& config_options,
  42. const std::string& prefix) const {
  43. std::string result;
  44. std::string parent;
  45. std::string id = GetId();
  46. if (!config_options.IsShallow() && !id.empty()) {
  47. parent = Configurable::SerializeOptions(config_options, "");
  48. }
  49. if (parent.empty()) {
  50. result = id;
  51. } else {
  52. result.append(prefix);
  53. result.append(OptionTypeInfo::kIdPropName());
  54. result.append("=");
  55. result.append(id);
  56. result.append(config_options.delimiter);
  57. result.append(parent);
  58. }
  59. return result;
  60. }
  61. bool Customizable::AreEquivalent(const ConfigOptions& config_options,
  62. const Configurable* other,
  63. std::string* mismatch) const {
  64. if (config_options.sanity_level > ConfigOptions::kSanityLevelNone &&
  65. this != other) {
  66. const Customizable* custom = static_cast<const Customizable*>(other);
  67. if (custom == nullptr) { // Cast failed
  68. return false;
  69. } else if (GetId() != custom->GetId()) {
  70. *mismatch = OptionTypeInfo::kIdPropName();
  71. return false;
  72. } else if (config_options.sanity_level >
  73. ConfigOptions::kSanityLevelLooselyCompatible) {
  74. bool matches =
  75. Configurable::AreEquivalent(config_options, other, mismatch);
  76. return matches;
  77. }
  78. }
  79. return true;
  80. }
  81. Status Customizable::GetOptionsMap(
  82. const ConfigOptions& config_options, const Customizable* customizable,
  83. const std::string& value, std::string* id,
  84. std::unordered_map<std::string, std::string>* props) {
  85. Status status;
  86. if (value.empty() || value == kNullptrString) {
  87. *id = "";
  88. props->clear();
  89. } else if (customizable != nullptr) {
  90. status =
  91. Configurable::GetOptionsMap(value, customizable->GetId(), id, props);
  92. if (status.ok() && customizable->IsInstanceOf(*id)) {
  93. // The new ID and the old ID match, so the objects are the same type.
  94. // Try to get the existing options, ignoring any errors
  95. ConfigOptions embedded = config_options;
  96. embedded.delimiter = ";";
  97. std::string curr_opts;
  98. if (customizable->GetOptionString(embedded, &curr_opts).ok()) {
  99. std::unordered_map<std::string, std::string> curr_props;
  100. if (StringToMap(curr_opts, &curr_props).ok()) {
  101. props->insert(curr_props.begin(), curr_props.end());
  102. }
  103. }
  104. }
  105. } else {
  106. status = Configurable::GetOptionsMap(value, "", id, props);
  107. }
  108. return status;
  109. }
  110. Status Customizable::ConfigureNewObject(
  111. const ConfigOptions& config_options, Customizable* object,
  112. const std::unordered_map<std::string, std::string>& opt_map) {
  113. Status status;
  114. if (object != nullptr) {
  115. status = object->ConfigureFromMap(config_options, opt_map);
  116. } else if (!opt_map.empty()) {
  117. status = Status::InvalidArgument("Cannot configure null object ");
  118. }
  119. return status;
  120. }
  121. } // namespace ROCKSDB_NAMESPACE