From d28fed9dd36169247f2ced5da34ae1355f74cfac Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 22 Mar 2019 10:49:26 +0800 Subject: [PATCH] common/config: use string_view as key when accessing settings Signed-off-by: Kefu Chai --- src/common/ConfUtils.cc | 9 ++++----- src/common/ConfUtils.h | 2 +- src/common/config.cc | 36 +++++++++++++++++------------------- src/common/config.h | 35 ++++++++++++++++++----------------- src/common/config_proxy.h | 20 ++++++++++---------- src/librbd/api/Config.cc | 8 ++++---- 6 files changed, 54 insertions(+), 56 deletions(-) diff --git a/src/common/ConfUtils.cc b/src/common/ConfUtils.cc index 7c8f1cc78b7..74f329bd04b 100644 --- a/src/common/ConfUtils.cc +++ b/src/common/ConfUtils.cc @@ -250,13 +250,12 @@ trim_whitespace(std::string &str, bool strip_internal) * the field names of md_config_t and get a key in normal form. */ std::string ConfFile:: -normalize_key_name(const std::string &key) +normalize_key_name(std::string_view key) { - if (key.find_first_of(" \t\r\n\f\v\xa0") == string::npos) { - return key; + std::string k{key}; + if (k.find_first_of(" \t\r\n\f\v\xa0") == k.npos) { + return k; } - - string k(key); ConfFile::trim_whitespace(k, true); std::replace(k.begin(), k.end(), ' ', '_'); return k; diff --git a/src/common/ConfUtils.h b/src/common/ConfUtils.h index 19ec188a711..a5170682805 100644 --- a/src/common/ConfUtils.h +++ b/src/common/ConfUtils.h @@ -71,7 +71,7 @@ public: const_section_iter_t sections_end() const; static void trim_whitespace(std::string &str, bool strip_internal); - static std::string normalize_key_name(const std::string &key); + static std::string normalize_key_name(std::string_view key); friend std::ostream &operator<<(std::ostream &oss, const ConfFile &cf); private: diff --git a/src/common/config.cc b/src/common/config.cc index aff3ff76a59..c4b472a5a94 100644 --- a/src/common/config.cc +++ b/src/common/config.cc @@ -240,7 +240,7 @@ void md_config_t::validate_schema() } } -const Option *md_config_t::find_option(const string& name) const +const Option *md_config_t::find_option(const std::string_view name) const { auto p = schema.find(name); if (p != schema.end()) { @@ -251,7 +251,7 @@ const Option *md_config_t::find_option(const string& name) const void md_config_t::set_val_default(ConfigValues& values, const ConfigTracker& tracker, - const string& name, const std::string& val) + const string_view name, const std::string& val) { const Option *o = find_option(name); ceph_assert(o); @@ -791,7 +791,7 @@ int md_config_t::injectargs(ConfigValues& values, void md_config_t::set_val_or_die(ConfigValues& values, const ConfigTracker& tracker, - const std::string &key, + const std::string_view key, const std::string &val) { std::stringstream err; @@ -804,7 +804,7 @@ void md_config_t::set_val_or_die(ConfigValues& values, int md_config_t::set_val(ConfigValues& values, const ConfigTracker& tracker, - const std::string &key, const char *val, + const std::string_view key, const char *val, std::stringstream *err_ss) { if (key.empty()) { @@ -837,7 +837,7 @@ int md_config_t::set_val(ConfigValues& values, return -ENOENT; } -int md_config_t::rm_val(ConfigValues& values, const std::string& key) +int md_config_t::rm_val(ConfigValues& values, const std::string_view key) { return _rm_val(values, key, CONF_OVERRIDE); } @@ -921,7 +921,7 @@ void md_config_t::get_config_bl( } int md_config_t::get_val(const ConfigValues& values, - const std::string &key, char **buf, int len) const + const std::string_view key, char **buf, int len) const { string k(ConfFile::normalize_key_name(key)); return _get_val_cstr(values, k, buf, len); @@ -929,7 +929,7 @@ int md_config_t::get_val(const ConfigValues& values, int md_config_t::get_val( const ConfigValues& values, - const std::string &key, + const std::string_view key, std::string *val) const { return conf_stringify(get_val_generic(values, key), val); @@ -937,7 +937,7 @@ int md_config_t::get_val( Option::value_t md_config_t::get_val_generic( const ConfigValues& values, - const std::string &key) const + const std::string_view key) const { string k(ConfFile::normalize_key_name(key)); return _get_val(values, k); @@ -945,7 +945,7 @@ Option::value_t md_config_t::get_val_generic( Option::value_t md_config_t::_get_val( const ConfigValues& values, - const std::string &key, + const std::string_view key, expand_stack_t *stack, std::ostream *err) const { @@ -1164,7 +1164,7 @@ Option::value_t md_config_t::_expand_meta( int md_config_t::_get_val_cstr( const ConfigValues& values, - const std::string &key, char **buf, int len) const + const std::string& key, char **buf, int len) const { if (key.empty()) return -EINVAL; @@ -1237,7 +1237,7 @@ int md_config_t::get_all_sections(std::vector §ions) const int md_config_t::get_val_from_conf_file( const ConfigValues& values, const std::vector §ions, - const std::string &key, + const std::string_view key, std::string &out, bool emeta) const { @@ -1255,13 +1255,11 @@ int md_config_t::get_val_from_conf_file( int md_config_t::_get_val_from_conf_file( const std::vector §ions, - const std::string &key, + const std::string_view key, std::string &out) const { - std::vector ::const_iterator s = sections.begin(); - std::vector ::const_iterator s_end = sections.end(); - for (; s != s_end; ++s) { - int ret = cf.read(s->c_str(), key, out); + for (auto &s : sections) { + int ret = cf.read(s.c_str(), std::string{key}, out); if (ret == 0) { return 0; } else if (ret != -ENOENT) { @@ -1332,13 +1330,13 @@ void md_config_t::_refresh(ConfigValues& values, const Option& opt) } int md_config_t::_rm_val(ConfigValues& values, - const std::string& key, + const std::string_view key, int level) { if (schema.count(key) == 0) { return -EINVAL; } - auto ret = values.rm_val(key, level); + auto ret = values.rm_val(std::string{key}, level); if (ret < 0) { return ret; } @@ -1447,7 +1445,7 @@ void md_config_t::diff( // we only have a default value; exclude from diff return; } - f->open_object_section(name.c_str()); + f->open_object_section(std::string{name}.c_str()); const Option *o = find_option(name); dump(f, CONF_DEFAULT, _get_val_default(*o)); for (auto& j : configs) { diff --git a/src/common/config.h b/src/common/config.h index 2944ce1120f..6d80d27f2f0 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -146,12 +146,12 @@ public: void _clear_safe_to_start_threads(); // this is only used by the unit test /// Look up an option in the schema - const Option *find_option(const string& name) const; + const Option *find_option(const std::string_view name) const; /// Set a default value void set_val_default(ConfigValues& values, const ConfigTracker& tracker, - const std::string& key, const std::string &val); + const std::string_view key, const std::string &val); /// Set a values from mon int set_mon_vals(CephContext *cct, @@ -169,21 +169,21 @@ public: // Set a configuration value, or crash // Metavariables will be expanded. void set_val_or_die(ConfigValues& values, const ConfigTracker& tracker, - const std::string &key, const std::string &val); + const std::string_view key, const std::string &val); // Set a configuration value. // Metavariables will be expanded. int set_val(ConfigValues& values, const ConfigTracker& tracker, - const std::string &key, const char *val, + const std::string_view key, const char *val, std::stringstream *err_ss=nullptr); int set_val(ConfigValues& values, const ConfigTracker& tracker, - const std::string &key, const string& s, + const std::string_view key, const std::string& s, std::stringstream *err_ss=nullptr) { return set_val(values, tracker, key, s.c_str(), err_ss); } /// clear override value - int rm_val(ConfigValues& values, const std::string& key); + int rm_val(ConfigValues& values, const std::string_view key); /// get encoded map> of entire config void get_config_bl(const ConfigValues& values, @@ -196,12 +196,11 @@ public: // Get a configuration value. // No metavariables will be returned (they will have already been expanded) - int get_val(const ConfigValues& values, const std::string &key, char **buf, int len) const; - int get_val(const ConfigValues& values, const std::string &key, std::string *val) const; - Option::value_t get_val_generic(const ConfigValues& values, const std::string &key) const; - template const T get_val(const ConfigValues& values, const std::string &key) const; + int get_val(const ConfigValues& values, const std::string_view key, char **buf, int len) const; + int get_val(const ConfigValues& values, const std::string_view key, std::string *val) const; + template const T get_val(const ConfigValues& values, const std::string_view key) const; template - auto with_val(const ConfigValues& values, const string& key, + auto with_val(const ConfigValues& values, const std::string_view key, Callback&& cb, Args&&... args) const -> std::result_of_t { return std::forward(cb)( @@ -222,7 +221,7 @@ public: // Metavariables will be expanded if emeta is true. int get_val_from_conf_file(const ConfigValues& values, const std::vector §ions, - std::string const &key, std::string &out, bool emeta) const; + const std::string_view key, std::string &out, bool emeta) const; /// dump all config values to a stream void show_config(const ConfigValues& values, std::ostream& out) const; @@ -249,10 +248,12 @@ private: void validate_schema(); void validate_default_settings(); + Option::value_t get_val_generic(const ConfigValues& values, + const std::string_view key) const; int _get_val_cstr(const ConfigValues& values, - const std::string &key, char **buf, int len) const; + const std::string& key, char **buf, int len) const; Option::value_t _get_val(const ConfigValues& values, - const std::string &key, + const std::string_view key, expand_stack_t *stack=0, std::ostream *err=0) const; Option::value_t _get_val(const ConfigValues& values, @@ -263,7 +264,7 @@ private: Option::value_t _get_val_nometa(const ConfigValues& values, const Option& o) const; - int _rm_val(ConfigValues& values, const std::string& key, int level); + int _rm_val(ConfigValues& values, const std::string_view key, int level); void _refresh(ConfigValues& values, const Option& opt); @@ -274,7 +275,7 @@ private: std::vector §ions) const; int _get_val_from_conf_file(const std::vector §ions, - const std::string &key, std::string &out) const; + const std::string_view key, std::string &out) const; int parse_option(ConfigValues& values, const ConfigTracker& tracker, @@ -356,7 +357,7 @@ public: template const T md_config_t::get_val(const ConfigValues& values, - const std::string &key) const { + const std::string_view key) const { return boost::get(this->get_val_generic(values, key)); } diff --git a/src/common/config_proxy.h b/src/common/config_proxy.h index 51aeaa842d7..92b0c91a373 100644 --- a/src/common/config_proxy.h +++ b/src/common/config_proxy.h @@ -118,21 +118,21 @@ public: ConfigValues* operator->() noexcept { return &values; } - int get_val(const std::string& key, char** buf, int len) const { + int get_val(const std::string_view key, char** buf, int len) const { std::lock_guard l{lock}; return config.get_val(values, key, buf, len); } - int get_val(const std::string &key, std::string *val) const { + int get_val(const std::string_view key, std::string *val) const { std::lock_guard l{lock}; return config.get_val(values, key, val); } template - const T get_val(const std::string& key) const { + const T get_val(const std::string_view key) const { std::lock_guard l{lock}; return config.template get_val(values, key); } template - auto with_val(const string& key, Callback&& cb, Args&&... args) const { + auto with_val(const std::string_view key, Callback&& cb, Args&&... args) const { std::lock_guard l{lock}; return config.template with_val(values, key, std::forward(cb), @@ -144,7 +144,7 @@ public: const decltype(md_config_t::schema)& get_schema() const { return config.schema; } - const Option* get_schema(const std::string& key) const { + const Option* get_schema(const std::string_view key) const { auto found = config.schema.find(key); if (found == config.schema.end()) { return nullptr; @@ -168,7 +168,7 @@ public: return config.get_all_sections(sections); } int get_val_from_conf_file(const std::vector& sections, - const std::string& key, std::string& out, + const std::string_view key, std::string& out, bool emeta) const { std::lock_guard l{lock}; return config.get_val_from_conf_file(values, @@ -236,7 +236,7 @@ public: std::lock_guard l{lock}; config.config_options(f); } - int rm_val(const std::string& key) { + int rm_val(const std::string_view key) { std::lock_guard l{lock}; return config.rm_val(values, key); } @@ -263,16 +263,16 @@ public: map_observer_changes(obs, key, rev_obs); }, oss); } - int set_val(const std::string& key, const std::string& s, + int set_val(const std::string_view key, const std::string& s, std::stringstream* err_ss=nullptr) { std::lock_guard l{lock}; return config.set_val(values, obs_mgr, key, s, err_ss); } - void set_val_default(const std::string& key, const std::string& val) { + void set_val_default(const std::string_view key, const std::string& val) { std::lock_guard l{lock}; config.set_val_default(values, obs_mgr, key, val); } - void set_val_or_die(const std::string& key, const std::string& val) { + void set_val_or_die(const std::string_view key, const std::string& val) { std::lock_guard l{lock}; config.set_val_or_die(values, obs_mgr, key, val); } diff --git a/src/librbd/api/Config.cc b/src/librbd/api/Config.cc index 3aa7573e8c2..55456b93e5c 100644 --- a/src/librbd/api/Config.cc +++ b/src/librbd/api/Config.cc @@ -79,10 +79,10 @@ struct Options : Parent { int init() { CephContext *cct = (CephContext *)io_ctx.cct(); - for (auto &it : *this) { - int r = cct->_conf.get_val(std::string(it.first), &it.second.first); + for (auto& [k,v] : *this) { + int r = cct->_conf.get_val(k, &v.first); ceph_assert(r == 0); - it.second.second = RBD_CONFIG_SOURCE_CONFIG; + v.second = RBD_CONFIG_SOURCE_CONFIG; } std::string last_key = ImageCtx::METADATA_CONF_PREFIX; @@ -222,7 +222,7 @@ void Config::apply_pool_overrides(librados::IoCtx& io_ctx, for (auto& [k,v] : opts) { if (v.second == RBD_CONFIG_SOURCE_POOL) { - r = config->set_val(std::string(k), v.first); + r = config->set_val(k, v.first); if (r < 0) { lderr(cct) << "failed to override pool config " << k << "=" << v.first << ": " << cpp_strerror(r) << dendl; -- 2.39.5