diff --git a/cpp/src/config/ConfigMgr.cpp b/cpp/src/config/ConfigMgr.cpp index f39679fcddab5426f6af4e83616adc96caae62c7..a7cfdc9fdaa9f796e39c2d0412a472b8b812d4f0 100644 --- a/cpp/src/config/ConfigMgr.cpp +++ b/cpp/src/config/ConfigMgr.cpp @@ -15,18 +15,19 @@ // specific language governing permissions and limitations // under the License. -#include "ConfigMgr.h" +#include "config/ConfigMgr.h" #include "YamlConfigMgr.h" namespace zilliz { namespace milvus { namespace server { -ConfigMgr * ConfigMgr::GetInstance() { +ConfigMgr * +ConfigMgr::GetInstance() { static YamlConfigMgr mgr; return &mgr; } -} -} -} +} // namespace server +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/config/ConfigMgr.h b/cpp/src/config/ConfigMgr.h index 3e546103b6376043d8e677ecd0fc977d94a6327c..96a63aa2c8e1a784bf828161a7a5447cba08b280 100644 --- a/cpp/src/config/ConfigMgr.h +++ b/cpp/src/config/ConfigMgr.h @@ -20,6 +20,8 @@ #include "utils/Error.h" #include "ConfigNode.h" +#include + namespace zilliz { namespace milvus { namespace server { @@ -40,16 +42,16 @@ namespace server { class ConfigMgr { public: - static ConfigMgr* GetInstance(); + static ConfigMgr *GetInstance(); virtual ErrorCode LoadConfigFile(const std::string &filename) = 0; virtual void Print() const = 0;//will be deleted virtual std::string DumpString() const = 0; - virtual const ConfigNode& GetRootNode() const = 0; - virtual ConfigNode& GetRootNode() = 0; + virtual const ConfigNode &GetRootNode() const = 0; + virtual ConfigNode &GetRootNode() = 0; }; -} -} -} +} // namespace server +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/config/ConfigNode.cpp b/cpp/src/config/ConfigNode.cpp index 88954c1bdf22ee253b7b0434c69c934b546d7a1f..87b080d5722e9276dc48359faa4862fbd771d795 100644 --- a/cpp/src/config/ConfigNode.cpp +++ b/cpp/src/config/ConfigNode.cpp @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -#include "ConfigNode.h" +#include "config/ConfigNode.h" #include "utils/Error.h" #include "utils/Log.h" @@ -27,33 +27,34 @@ namespace zilliz { namespace milvus { namespace server { -void ConfigNode::Combine(const ConfigNode& target) { - const std::map& kv = target.GetConfig(); - for(auto itr = kv.begin(); itr != kv.end(); ++itr){ +void +ConfigNode::Combine(const ConfigNode &target) { + const std::map &kv = target.GetConfig(); + for (auto itr = kv.begin(); itr != kv.end(); ++itr) { config_[itr->first] = itr->second; } - const std::map >& sequences = target.GetSequences(); - for(auto itr = sequences.begin(); itr != sequences.end(); ++itr){ + const std::map > &sequences = target.GetSequences(); + for (auto itr = sequences.begin(); itr != sequences.end(); ++itr) { sequences_[itr->first] = itr->second; } - const std::map& children = target.GetChildren(); - for(auto itr = children.begin(); itr != children.end(); ++itr){ + const std::map &children = target.GetChildren(); + for (auto itr = children.begin(); itr != children.end(); ++itr) { children_[itr->first] = itr->second; } } //key/value pair config void -ConfigNode::SetValue(const std::string& key, const std::string& value) { +ConfigNode::SetValue(const std::string &key, const std::string &value) { config_[key] = value; } std::string -ConfigNode::GetValue(const std::string& param_key, const std::string& default_val) const { +ConfigNode::GetValue(const std::string ¶m_key, const std::string &default_val) const { auto ref = config_.find(param_key); - if(ref != config_.end()) { + if (ref != config_.end()) { return ref->second; } @@ -76,7 +77,7 @@ int32_t ConfigNode::GetInt32Value(const std::string ¶m_key, int32_t default_val) const { std::string val = GetValue(param_key); if (!val.empty()) { - return (int32_t)std::strtol(val.c_str(), nullptr, 10); + return (int32_t) std::strtol(val.c_str(), nullptr, 10); } else { return default_val; } @@ -112,25 +113,26 @@ ConfigNode::GetDoubleValue(const std::string ¶m_key, double default_val) con } } -const std::map& +const std::map & ConfigNode::GetConfig() const { return config_; -}; +} -void ConfigNode::ClearConfig() { +void +ConfigNode::ClearConfig() { config_.clear(); } //key/object config void -ConfigNode::AddChild(const std::string& type_name, const ConfigNode& config) { +ConfigNode::AddChild(const std::string &type_name, const ConfigNode &config) { children_[type_name] = config; } ConfigNode -ConfigNode::GetChild(const std::string& type_name) const { +ConfigNode::GetChild(const std::string &type_name) const { auto ref = children_.find(type_name); - if(ref != children_.end()) { + if (ref != children_.end()) { return ref->second; } @@ -138,25 +140,26 @@ ConfigNode::GetChild(const std::string& type_name) const { return nc; } -ConfigNode& +ConfigNode & ConfigNode::GetChild(const std::string &type_name) { return children_[type_name]; } void -ConfigNode::GetChildren(ConfigNodeArr& arr) const { +ConfigNode::GetChildren(ConfigNodeArr &arr) const { arr.clear(); - for(auto ref : children_){ + for (auto ref : children_) { arr.push_back(ref.second); } } -const std::map& +const std::map & ConfigNode::GetChildren() const { return children_; } -void ConfigNode::ClearChildren() { +void +ConfigNode::ClearChildren() { children_.clear(); } @@ -169,7 +172,7 @@ ConfigNode::AddSequenceItem(const std::string &key, const std::string &item) { std::vector ConfigNode::GetSequence(const std::string &key) const { auto itr = sequences_.find(key); - if(itr != sequences_.end()) { + if (itr != sequences_.end()) { return itr->second; } else { std::vector temp; @@ -177,29 +180,30 @@ ConfigNode::GetSequence(const std::string &key) const { } } -const std::map >& +const std::map > & ConfigNode::GetSequences() const { return sequences_; } -void ConfigNode::ClearSequences() { +void +ConfigNode::ClearSequences() { sequences_.clear(); } void -ConfigNode::PrintAll(const std::string& prefix) const { - for(auto& elem : config_) { +ConfigNode::PrintAll(const std::string &prefix) const { + for (auto &elem : config_) { SERVER_LOG_INFO << prefix << elem.first + ": " << elem.second; } - for(auto& elem : sequences_) { + for (auto &elem : sequences_) { SERVER_LOG_INFO << prefix << elem.first << ": "; - for(auto& str : elem.second) { + for (auto &str : elem.second) { SERVER_LOG_INFO << prefix << " - " << str; } } - for(auto& elem : children_) { + for (auto &elem : children_) { SERVER_LOG_INFO << prefix << elem.first << ": "; elem.second.PrintAll(prefix + " "); } @@ -209,18 +213,18 @@ std::string ConfigNode::DumpString(const std::string &prefix) const { std::stringstream str_buffer; const std::string endl = "\n"; - for(auto& elem : config_) { + for (auto &elem : config_) { str_buffer << prefix << elem.first << ": " << elem.second << endl; } - for(auto& elem : sequences_) { + for (auto &elem : sequences_) { str_buffer << prefix << elem.first << ": " << endl; - for(auto& str : elem.second) { + for (auto &str : elem.second) { str_buffer << prefix + " - " << str << endl; } } - for(auto& elem : children_) { + for (auto &elem : children_) { str_buffer << prefix << elem.first << ": " << endl; str_buffer << elem.second.DumpString(prefix + " ") << endl; } @@ -228,6 +232,6 @@ ConfigNode::DumpString(const std::string &prefix) const { return str_buffer.str(); } -} -} -} +} // namespace server +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/config/ConfigNode.h b/cpp/src/config/ConfigNode.h index 606e090d4b490b87e828a7156edbbe3a5b0721b5..d3fabc06550295dfeed2afdd582c1f46d9ef1621 100644 --- a/cpp/src/config/ConfigNode.h +++ b/cpp/src/config/ConfigNode.h @@ -30,7 +30,7 @@ typedef std::vector ConfigNodeArr; class ConfigNode { public: - void Combine(const ConfigNode& target); + void Combine(const ConfigNode &target); //key/value pair config void SetValue(const std::string &key, const std::string &value); @@ -42,23 +42,23 @@ class ConfigNode { float GetFloatValue(const std::string ¶m_key, float default_val = 0.0) const; double GetDoubleValue(const std::string ¶m_key, double default_val = 0.0) const; - const std::map& GetConfig() const; + const std::map &GetConfig() const; void ClearConfig(); //key/object config void AddChild(const std::string &type_name, const ConfigNode &config); ConfigNode GetChild(const std::string &type_name) const; - ConfigNode& GetChild(const std::string &type_name); + ConfigNode &GetChild(const std::string &type_name); void GetChildren(ConfigNodeArr &arr) const; - const std::map& GetChildren() const; + const std::map &GetChildren() const; void ClearChildren(); //key/sequence config void AddSequenceItem(const std::string &key, const std::string &item); std::vector GetSequence(const std::string &key) const; - const std::map >& GetSequences() const; + const std::map > &GetSequences() const; void ClearSequences(); void PrintAll(const std::string &prefix = "") const; @@ -70,6 +70,6 @@ class ConfigNode { std::map > sequences_; }; -} -} -} +} // namespace server +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/config/YamlConfigMgr.cpp b/cpp/src/config/YamlConfigMgr.cpp index bddbc322459ad6ac0080ae00ff235f9693ee5b61..1dd8a3d9435554e692f8ea0aeb2da341b43e5b70 100644 --- a/cpp/src/config/YamlConfigMgr.cpp +++ b/cpp/src/config/YamlConfigMgr.cpp @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -#include "YamlConfigMgr.h" +#include "config/YamlConfigMgr.h" #include "utils/Log.h" #include @@ -24,7 +24,8 @@ namespace zilliz { namespace milvus { namespace server { -ErrorCode YamlConfigMgr::LoadConfigFile(const std::string &filename) { +ErrorCode +YamlConfigMgr::LoadConfigFile(const std::string &filename) { struct stat directoryStat; int statOK = stat(filename.c_str(), &directoryStat); if (statOK != 0) { @@ -36,36 +37,40 @@ ErrorCode YamlConfigMgr::LoadConfigFile(const std::string &filename) { node_ = YAML::LoadFile(filename); LoadConfigNode(node_, config_); } - catch (YAML::Exception& e) { - SERVER_LOG_ERROR << "Failed to load config file: " << std::string(e.what ()); + catch (YAML::Exception &e) { + SERVER_LOG_ERROR << "Failed to load config file: " << std::string(e.what()); return SERVER_UNEXPECTED_ERROR; } return SERVER_SUCCESS; } -void YamlConfigMgr::Print() const { +void +YamlConfigMgr::Print() const { SERVER_LOG_INFO << "System config content:"; config_.PrintAll(); } -std::string YamlConfigMgr::DumpString() const { +std::string +YamlConfigMgr::DumpString() const { return config_.DumpString(""); } -const ConfigNode& YamlConfigMgr::GetRootNode() const { +const ConfigNode & +YamlConfigMgr::GetRootNode() const { return config_; } -ConfigNode& YamlConfigMgr::GetRootNode() { +ConfigNode & +YamlConfigMgr::GetRootNode() { return config_; } bool -YamlConfigMgr::SetConfigValue(const YAML::Node& node, - const std::string& key, - ConfigNode& config) { - if(node[key].IsDefined ()) { +YamlConfigMgr::SetConfigValue(const YAML::Node &node, + const std::string &key, + ConfigNode &config) { + if (node[key].IsDefined()) { config.SetValue(key, node[key].as()); return true; } @@ -73,10 +78,10 @@ YamlConfigMgr::SetConfigValue(const YAML::Node& node, } bool -YamlConfigMgr::SetChildConfig(const YAML::Node& node, - const std::string& child_name, - ConfigNode& config) { - if(node[child_name].IsDefined ()) { +YamlConfigMgr::SetChildConfig(const YAML::Node &node, + const std::string &child_name, + ConfigNode &config) { + if (node[child_name].IsDefined()) { ConfigNode sub_config; LoadConfigNode(node[child_name], sub_config); config.AddChild(child_name, sub_config); @@ -89,9 +94,9 @@ bool YamlConfigMgr::SetSequence(const YAML::Node &node, const std::string &child_name, ConfigNode &config) { - if(node[child_name].IsDefined ()) { + if (node[child_name].IsDefined()) { size_t cnt = node[child_name].size(); - for(size_t i = 0; i < cnt; i++){ + for (size_t i = 0; i < cnt; i++) { config.AddSequenceItem(child_name, node[child_name][i].as()); } return true; @@ -100,22 +105,22 @@ YamlConfigMgr::SetSequence(const YAML::Node &node, } void -YamlConfigMgr::LoadConfigNode(const YAML::Node& node, ConfigNode& config) { +YamlConfigMgr::LoadConfigNode(const YAML::Node &node, ConfigNode &config) { std::string key; for (YAML::const_iterator it = node.begin(); it != node.end(); ++it) { - if(!it->first.IsNull()){ + if (!it->first.IsNull()) { key = it->first.as(); } - if(node[key].IsScalar()) { + if (node[key].IsScalar()) { SetConfigValue(node, key, config); - } else if(node[key].IsMap()){ + } else if (node[key].IsMap()) { SetChildConfig(node, key, config); - } else if(node[key].IsSequence()){ + } else if (node[key].IsSequence()) { SetSequence(node, key, config); } } } -} -} -} +} // namespace server +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/config/YamlConfigMgr.h b/cpp/src/config/YamlConfigMgr.h index ef4d02c3ce7782b6d98d7089aa0ad22dc244b564..6b84943bf808d81e4de875eea5fb5af92e625cfc 100644 --- a/cpp/src/config/YamlConfigMgr.h +++ b/cpp/src/config/YamlConfigMgr.h @@ -21,6 +21,7 @@ #include "ConfigNode.h" #include "utils/Error.h" +#include #include namespace zilliz { @@ -33,15 +34,15 @@ class YamlConfigMgr : public ConfigMgr { virtual void Print() const; virtual std::string DumpString() const; - virtual const ConfigNode& GetRootNode() const; - virtual ConfigNode& GetRootNode(); + virtual const ConfigNode &GetRootNode() const; + virtual ConfigNode &GetRootNode(); private: - bool SetConfigValue(const YAML::Node& node, - const std::string& key, - ConfigNode& config); + bool SetConfigValue(const YAML::Node &node, + const std::string &key, + ConfigNode &config); - bool SetChildConfig(const YAML::Node& node, + bool SetChildConfig(const YAML::Node &node, const std::string &name, ConfigNode &config); @@ -50,15 +51,13 @@ class YamlConfigMgr : public ConfigMgr { const std::string &child_name, ConfigNode &config); - void LoadConfigNode(const YAML::Node& node, ConfigNode& config); + void LoadConfigNode(const YAML::Node &node, ConfigNode &config); private: YAML::Node node_; ConfigNode config_; }; -} -} -} - - +} // namespace server +} // namespace milvus +} // namespace zilliz