]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/config: use string_view as key when accessing settings
authorKefu Chai <kchai@redhat.com>
Fri, 22 Mar 2019 02:49:26 +0000 (10:49 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 22 Mar 2019 05:03:38 +0000 (13:03 +0800)
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/ConfUtils.cc
src/common/ConfUtils.h
src/common/config.cc
src/common/config.h
src/common/config_proxy.h
src/librbd/api/Config.cc

index 7c8f1cc78b7f02cdfea3dd26d49a31a9298216a8..74f329bd04ba6d05d9e21d8f7e70aa5239a0539f 100644 (file)
@@ -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;
index 19ec188a7112262ee502273616632291885f0d7b..a5170682805b1a59fbf423e18ac458babc90d42a 100644 (file)
@@ -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:
index aff3ff76a59116cb7ca2db04ad3a9b7c33ce7045..c4b472a5a94dd7fe3afb1b9b838f7d155da4bac7 100644 (file)
@@ -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::stringkey, char **buf, int len) const
 {
   if (key.empty())
     return -EINVAL;
@@ -1237,7 +1237,7 @@ int md_config_t::get_all_sections(std::vector <std::string> &sections) const
 int md_config_t::get_val_from_conf_file(
   const ConfigValues& values,
   const std::vector <std::string> &sections,
-  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 <std::string> &sections,
-  const std::string &key,
+  const std::string_view key,
   std::string &out) const
 {
-  std::vector <std::string>::const_iterator s = sections.begin();
-  std::vector <std::string>::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) {
index 2944ce1120fb1c239b93723a4edc3b6fbc75b876..6d80d27f2f081a78cda3618975380d57414e4f86 100644 (file)
@@ -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<string,map<int32_t,string>> 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<typename T> 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<typename T> const T get_val(const ConfigValues& values, const std::string_view key) const;
   template<typename T, typename Callback, typename...Args>
-  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<Callback(const T&, Args...)> {
     return std::forward<Callback>(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 <std::string> &sections,
-                  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::stringkey, 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 <std::string> &sections) const;
 
   int _get_val_from_conf_file(const std::vector <std::string> &sections,
-                             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<typename T>
 const T md_config_t::get_val(const ConfigValues& values,
-                            const std::string &key) const {
+                            const std::string_view key) const {
   return boost::get<T>(this->get_val_generic(values, key));
 }
 
index 51aeaa842d74e00570d960fbd7bc5bc1dd937e2b..92b0c91a37359304bacecc3379de3ff78eab5b44 100644 (file)
@@ -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<typename T>
-  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<T>(values, key);
   }
   template<typename T, typename Callback, typename...Args>
-  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<T>(values, key,
                                       std::forward<Callback>(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<std::string>& 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);
   }
index 3aa7573e8c22e1eef241bf8c615f5ce399251794..55456b93e5cdbe2e0b646d9026946fb22772ea83 100644 (file)
@@ -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<I>::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;