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