You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by tw...@apache.org on 2022/06/26 15:16:31 UTC

[incubator-kvrocks] branch unstable updated: Use unique_ptr in Config (#672)

This is an automated email from the ASF dual-hosted git repository.

twice pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new df4a548  Use unique_ptr in Config (#672)
df4a548 is described below

commit df4a548789a2aa1ef58aaaa47e1d0f7b132e2522
Author: jakevin <30...@users.noreply.github.com>
AuthorDate: Sun Jun 26 23:16:26 2022 +0800

    Use unique_ptr in Config (#672)
    
    Co-authored-by: Twice <tw...@apache.org>
---
 src/config.cc | 26 ++++++++++++--------------
 src/config.h  |  9 +++++----
 2 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/src/config.cc b/src/config.cc
index 01bfea5..e3a7c57 100644
--- a/src/config.cc
+++ b/src/config.cc
@@ -84,11 +84,15 @@ const char *configEnumGetName(configEnum *ce, int val) {
 
 Config::Config() {
   struct FieldWrapper {
-    const char *name;
+    std::string name;
     bool readonly;
-    ConfigField *field;
+    std::unique_ptr<ConfigField> field;
+
+    FieldWrapper(std::string name, bool readonly,
+                 ConfigField* field)
+        : name(std::move(name)), readonly(readonly), field(field) {}
   };
-  std::vector<FieldWrapper> fields = {
+  FieldWrapper fields[] = {
       {"daemonize", true, new YesNoField(&daemonize, false)},
       {"bind", true, new StringField(&binds_, "127.0.0.1")},
       {"port", true, new IntField(&port, 6666, 1, 65535)},
@@ -181,10 +185,10 @@ Config::Config() {
       {"rocksdb.level_compaction_dynamic_level_bytes",
         false, new YesNoField(&RocksDB.level_compaction_dynamic_level_bytes, false)},
   };
-  for (const auto &wrapper : fields) {
-    auto field = wrapper.field;
+  for (auto &wrapper : fields) {
+    auto &field = wrapper.field;
     field->readonly = wrapper.readonly;
-    fields_.insert({wrapper.name, field});
+    fields_.emplace(std::move(wrapper.name), std::move(field));
   }
   initFieldValidator();
   initFieldCallback();
@@ -483,12 +487,6 @@ void Config::initFieldCallback() {
   }
 }
 
-Config::~Config() {
-  for (const auto &iter : fields_) {
-    delete iter.second;
-  }
-}
-
 void Config::SetMaster(const std::string &host, int port) {
   master_host = host;
   master_port = port;
@@ -526,7 +524,7 @@ Status Config::parseConfigFromString(std::string input, int line_number) {
   }
   auto iter = fields_.find(field_key);
   if (iter != fields_.end()) {
-    auto field = iter->second;
+    auto& field = iter->second;
     field->line_number = line_number;
     auto s = field->Set(kv[1]);
     if (!s.IsOK()) return s;
@@ -615,7 +613,7 @@ Status Config::Set(Server *svr, std::string key, const std::string &value) {
   if (iter == fields_.end() || iter->second->readonly) {
     return Status(Status::NotOK, "Unsupported CONFIG parameter: "+key);
   }
-  auto field = iter->second;
+  auto& field = iter->second;
   if (field->validate) {
     auto s = field->validate(key, value);
     if (!s.IsOK()) return s;
diff --git a/src/config.h b/src/config.h
index 8fb7d3b..793d2ce 100644
--- a/src/config.h
+++ b/src/config.h
@@ -21,10 +21,11 @@
 #pragma once
 #include <sys/resource.h>
 
-#include <string>
 #include <map>
-#include <vector>
+#include <memory>
 #include <set>
+#include <string>
+#include <vector>
 
 #include <rocksdb/options.h>
 
@@ -62,7 +63,7 @@ struct CompactionCheckerRange {
 struct Config{
  public:
   Config();
-  ~Config();
+  ~Config() = default;
   int port = 6666;
   int workers = 0;
   int timeout = 0;
@@ -178,7 +179,7 @@ struct Config{
   std::string bgsave_cron_;
   std::string compaction_checker_range_;
   std::string profiling_sample_commands_;
-  std::map<std::string, ConfigField*> fields_;
+  std::map<std::string, std::unique_ptr<ConfigField>> fields_;
   std::string rename_command_;
 
   void initFieldValidator();