提交 44f5677c 编写于 作者: G groot

format config code


Former-commit-id: 8794370edb6dfab3fe94023ae482197e9a694394
上级 f50dca75
...@@ -15,18 +15,19 @@ ...@@ -15,18 +15,19 @@
// specific language governing permissions and limitations // specific language governing permissions and limitations
// under the License. // under the License.
#include "ConfigMgr.h" #include "config/ConfigMgr.h"
#include "YamlConfigMgr.h" #include "YamlConfigMgr.h"
namespace zilliz { namespace zilliz {
namespace milvus { namespace milvus {
namespace server { namespace server {
ConfigMgr * ConfigMgr::GetInstance() { ConfigMgr *
ConfigMgr::GetInstance() {
static YamlConfigMgr mgr; static YamlConfigMgr mgr;
return &mgr; return &mgr;
} }
} } // namespace server
} } // namespace milvus
} } // namespace zilliz
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include "utils/Error.h" #include "utils/Error.h"
#include "ConfigNode.h" #include "ConfigNode.h"
#include <string>
namespace zilliz { namespace zilliz {
namespace milvus { namespace milvus {
namespace server { namespace server {
...@@ -40,16 +42,16 @@ namespace server { ...@@ -40,16 +42,16 @@ namespace server {
class ConfigMgr { class ConfigMgr {
public: public:
static ConfigMgr* GetInstance(); static ConfigMgr *GetInstance();
virtual ErrorCode LoadConfigFile(const std::string &filename) = 0; virtual ErrorCode LoadConfigFile(const std::string &filename) = 0;
virtual void Print() const = 0;//will be deleted virtual void Print() const = 0;//will be deleted
virtual std::string DumpString() const = 0; virtual std::string DumpString() const = 0;
virtual const ConfigNode& GetRootNode() const = 0; virtual const ConfigNode &GetRootNode() const = 0;
virtual ConfigNode& GetRootNode() = 0; virtual ConfigNode &GetRootNode() = 0;
}; };
} } // namespace server
} } // namespace milvus
} } // namespace zilliz
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
// specific language governing permissions and limitations // specific language governing permissions and limitations
// under the License. // under the License.
#include "ConfigNode.h" #include "config/ConfigNode.h"
#include "utils/Error.h" #include "utils/Error.h"
#include "utils/Log.h" #include "utils/Log.h"
...@@ -27,33 +27,34 @@ namespace zilliz { ...@@ -27,33 +27,34 @@ namespace zilliz {
namespace milvus { namespace milvus {
namespace server { namespace server {
void ConfigNode::Combine(const ConfigNode& target) { void
const std::map<std::string, std::string>& kv = target.GetConfig(); ConfigNode::Combine(const ConfigNode &target) {
for(auto itr = kv.begin(); itr != kv.end(); ++itr){ const std::map<std::string, std::string> &kv = target.GetConfig();
for (auto itr = kv.begin(); itr != kv.end(); ++itr) {
config_[itr->first] = itr->second; config_[itr->first] = itr->second;
} }
const std::map<std::string, std::vector<std::string> >& sequences = target.GetSequences(); const std::map<std::string, std::vector<std::string> > &sequences = target.GetSequences();
for(auto itr = sequences.begin(); itr != sequences.end(); ++itr){ for (auto itr = sequences.begin(); itr != sequences.end(); ++itr) {
sequences_[itr->first] = itr->second; sequences_[itr->first] = itr->second;
} }
const std::map<std::string, ConfigNode>& children = target.GetChildren(); const std::map<std::string, ConfigNode> &children = target.GetChildren();
for(auto itr = children.begin(); itr != children.end(); ++itr){ for (auto itr = children.begin(); itr != children.end(); ++itr) {
children_[itr->first] = itr->second; children_[itr->first] = itr->second;
} }
} }
//key/value pair config //key/value pair config
void void
ConfigNode::SetValue(const std::string& key, const std::string& value) { ConfigNode::SetValue(const std::string &key, const std::string &value) {
config_[key] = value; config_[key] = value;
} }
std::string std::string
ConfigNode::GetValue(const std::string& param_key, const std::string& default_val) const { ConfigNode::GetValue(const std::string &param_key, const std::string &default_val) const {
auto ref = config_.find(param_key); auto ref = config_.find(param_key);
if(ref != config_.end()) { if (ref != config_.end()) {
return ref->second; return ref->second;
} }
...@@ -76,7 +77,7 @@ int32_t ...@@ -76,7 +77,7 @@ int32_t
ConfigNode::GetInt32Value(const std::string &param_key, int32_t default_val) const { ConfigNode::GetInt32Value(const std::string &param_key, int32_t default_val) const {
std::string val = GetValue(param_key); std::string val = GetValue(param_key);
if (!val.empty()) { if (!val.empty()) {
return (int32_t)std::strtol(val.c_str(), nullptr, 10); return (int32_t) std::strtol(val.c_str(), nullptr, 10);
} else { } else {
return default_val; return default_val;
} }
...@@ -112,25 +113,26 @@ ConfigNode::GetDoubleValue(const std::string &param_key, double default_val) con ...@@ -112,25 +113,26 @@ ConfigNode::GetDoubleValue(const std::string &param_key, double default_val) con
} }
} }
const std::map<std::string, std::string>& const std::map<std::string, std::string> &
ConfigNode::GetConfig() const { ConfigNode::GetConfig() const {
return config_; return config_;
}; }
void ConfigNode::ClearConfig() { void
ConfigNode::ClearConfig() {
config_.clear(); config_.clear();
} }
//key/object config //key/object config
void 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; children_[type_name] = config;
} }
ConfigNode ConfigNode
ConfigNode::GetChild(const std::string& type_name) const { ConfigNode::GetChild(const std::string &type_name) const {
auto ref = children_.find(type_name); auto ref = children_.find(type_name);
if(ref != children_.end()) { if (ref != children_.end()) {
return ref->second; return ref->second;
} }
...@@ -138,25 +140,26 @@ ConfigNode::GetChild(const std::string& type_name) const { ...@@ -138,25 +140,26 @@ ConfigNode::GetChild(const std::string& type_name) const {
return nc; return nc;
} }
ConfigNode& ConfigNode &
ConfigNode::GetChild(const std::string &type_name) { ConfigNode::GetChild(const std::string &type_name) {
return children_[type_name]; return children_[type_name];
} }
void void
ConfigNode::GetChildren(ConfigNodeArr& arr) const { ConfigNode::GetChildren(ConfigNodeArr &arr) const {
arr.clear(); arr.clear();
for(auto ref : children_){ for (auto ref : children_) {
arr.push_back(ref.second); arr.push_back(ref.second);
} }
} }
const std::map<std::string, ConfigNode>& const std::map<std::string, ConfigNode> &
ConfigNode::GetChildren() const { ConfigNode::GetChildren() const {
return children_; return children_;
} }
void ConfigNode::ClearChildren() { void
ConfigNode::ClearChildren() {
children_.clear(); children_.clear();
} }
...@@ -169,7 +172,7 @@ ConfigNode::AddSequenceItem(const std::string &key, const std::string &item) { ...@@ -169,7 +172,7 @@ ConfigNode::AddSequenceItem(const std::string &key, const std::string &item) {
std::vector<std::string> std::vector<std::string>
ConfigNode::GetSequence(const std::string &key) const { ConfigNode::GetSequence(const std::string &key) const {
auto itr = sequences_.find(key); auto itr = sequences_.find(key);
if(itr != sequences_.end()) { if (itr != sequences_.end()) {
return itr->second; return itr->second;
} else { } else {
std::vector<std::string> temp; std::vector<std::string> temp;
...@@ -177,29 +180,30 @@ ConfigNode::GetSequence(const std::string &key) const { ...@@ -177,29 +180,30 @@ ConfigNode::GetSequence(const std::string &key) const {
} }
} }
const std::map<std::string, std::vector<std::string> >& const std::map<std::string, std::vector<std::string> > &
ConfigNode::GetSequences() const { ConfigNode::GetSequences() const {
return sequences_; return sequences_;
} }
void ConfigNode::ClearSequences() { void
ConfigNode::ClearSequences() {
sequences_.clear(); sequences_.clear();
} }
void void
ConfigNode::PrintAll(const std::string& prefix) const { ConfigNode::PrintAll(const std::string &prefix) const {
for(auto& elem : config_) { for (auto &elem : config_) {
SERVER_LOG_INFO << prefix << elem.first + ": " << elem.second; SERVER_LOG_INFO << prefix << elem.first + ": " << elem.second;
} }
for(auto& elem : sequences_) { for (auto &elem : sequences_) {
SERVER_LOG_INFO << prefix << elem.first << ": "; SERVER_LOG_INFO << prefix << elem.first << ": ";
for(auto& str : elem.second) { for (auto &str : elem.second) {
SERVER_LOG_INFO << prefix << " - " << str; SERVER_LOG_INFO << prefix << " - " << str;
} }
} }
for(auto& elem : children_) { for (auto &elem : children_) {
SERVER_LOG_INFO << prefix << elem.first << ": "; SERVER_LOG_INFO << prefix << elem.first << ": ";
elem.second.PrintAll(prefix + " "); elem.second.PrintAll(prefix + " ");
} }
...@@ -209,18 +213,18 @@ std::string ...@@ -209,18 +213,18 @@ std::string
ConfigNode::DumpString(const std::string &prefix) const { ConfigNode::DumpString(const std::string &prefix) const {
std::stringstream str_buffer; std::stringstream str_buffer;
const std::string endl = "\n"; const std::string endl = "\n";
for(auto& elem : config_) { for (auto &elem : config_) {
str_buffer << prefix << elem.first << ": " << elem.second << endl; str_buffer << prefix << elem.first << ": " << elem.second << endl;
} }
for(auto& elem : sequences_) { for (auto &elem : sequences_) {
str_buffer << prefix << elem.first << ": " << endl; str_buffer << prefix << elem.first << ": " << endl;
for(auto& str : elem.second) { for (auto &str : elem.second) {
str_buffer << prefix + " - " << str << endl; str_buffer << prefix + " - " << str << endl;
} }
} }
for(auto& elem : children_) { for (auto &elem : children_) {
str_buffer << prefix << elem.first << ": " << endl; str_buffer << prefix << elem.first << ": " << endl;
str_buffer << elem.second.DumpString(prefix + " ") << endl; str_buffer << elem.second.DumpString(prefix + " ") << endl;
} }
...@@ -228,6 +232,6 @@ ConfigNode::DumpString(const std::string &prefix) const { ...@@ -228,6 +232,6 @@ ConfigNode::DumpString(const std::string &prefix) const {
return str_buffer.str(); return str_buffer.str();
} }
} } // namespace server
} } // namespace milvus
} } // namespace zilliz
...@@ -30,7 +30,7 @@ typedef std::vector<ConfigNode> ConfigNodeArr; ...@@ -30,7 +30,7 @@ typedef std::vector<ConfigNode> ConfigNodeArr;
class ConfigNode { class ConfigNode {
public: public:
void Combine(const ConfigNode& target); void Combine(const ConfigNode &target);
//key/value pair config //key/value pair config
void SetValue(const std::string &key, const std::string &value); void SetValue(const std::string &key, const std::string &value);
...@@ -42,23 +42,23 @@ class ConfigNode { ...@@ -42,23 +42,23 @@ class ConfigNode {
float GetFloatValue(const std::string &param_key, float default_val = 0.0) const; float GetFloatValue(const std::string &param_key, float default_val = 0.0) const;
double GetDoubleValue(const std::string &param_key, double default_val = 0.0) const; double GetDoubleValue(const std::string &param_key, double default_val = 0.0) const;
const std::map<std::string, std::string>& GetConfig() const; const std::map<std::string, std::string> &GetConfig() const;
void ClearConfig(); void ClearConfig();
//key/object config //key/object config
void AddChild(const std::string &type_name, const ConfigNode &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) const;
ConfigNode& GetChild(const std::string &type_name); ConfigNode &GetChild(const std::string &type_name);
void GetChildren(ConfigNodeArr &arr) const; void GetChildren(ConfigNodeArr &arr) const;
const std::map<std::string, ConfigNode>& GetChildren() const; const std::map<std::string, ConfigNode> &GetChildren() const;
void ClearChildren(); void ClearChildren();
//key/sequence config //key/sequence config
void AddSequenceItem(const std::string &key, const std::string &item); void AddSequenceItem(const std::string &key, const std::string &item);
std::vector<std::string> GetSequence(const std::string &key) const; std::vector<std::string> GetSequence(const std::string &key) const;
const std::map<std::string, std::vector<std::string> >& GetSequences() const; const std::map<std::string, std::vector<std::string> > &GetSequences() const;
void ClearSequences(); void ClearSequences();
void PrintAll(const std::string &prefix = "") const; void PrintAll(const std::string &prefix = "") const;
...@@ -70,6 +70,6 @@ class ConfigNode { ...@@ -70,6 +70,6 @@ class ConfigNode {
std::map<std::string, std::vector<std::string> > sequences_; std::map<std::string, std::vector<std::string> > sequences_;
}; };
} } // namespace server
} } // namespace milvus
} } // namespace zilliz
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
// specific language governing permissions and limitations // specific language governing permissions and limitations
// under the License. // under the License.
#include "YamlConfigMgr.h" #include "config/YamlConfigMgr.h"
#include "utils/Log.h" #include "utils/Log.h"
#include <sys/stat.h> #include <sys/stat.h>
...@@ -24,7 +24,8 @@ namespace zilliz { ...@@ -24,7 +24,8 @@ namespace zilliz {
namespace milvus { namespace milvus {
namespace server { namespace server {
ErrorCode YamlConfigMgr::LoadConfigFile(const std::string &filename) { ErrorCode
YamlConfigMgr::LoadConfigFile(const std::string &filename) {
struct stat directoryStat; struct stat directoryStat;
int statOK = stat(filename.c_str(), &directoryStat); int statOK = stat(filename.c_str(), &directoryStat);
if (statOK != 0) { if (statOK != 0) {
...@@ -36,36 +37,40 @@ ErrorCode YamlConfigMgr::LoadConfigFile(const std::string &filename) { ...@@ -36,36 +37,40 @@ ErrorCode YamlConfigMgr::LoadConfigFile(const std::string &filename) {
node_ = YAML::LoadFile(filename); node_ = YAML::LoadFile(filename);
LoadConfigNode(node_, config_); LoadConfigNode(node_, config_);
} }
catch (YAML::Exception& e) { catch (YAML::Exception &e) {
SERVER_LOG_ERROR << "Failed to load config file: " << std::string(e.what ()); SERVER_LOG_ERROR << "Failed to load config file: " << std::string(e.what());
return SERVER_UNEXPECTED_ERROR; return SERVER_UNEXPECTED_ERROR;
} }
return SERVER_SUCCESS; return SERVER_SUCCESS;
} }
void YamlConfigMgr::Print() const { void
YamlConfigMgr::Print() const {
SERVER_LOG_INFO << "System config content:"; SERVER_LOG_INFO << "System config content:";
config_.PrintAll(); config_.PrintAll();
} }
std::string YamlConfigMgr::DumpString() const { std::string
YamlConfigMgr::DumpString() const {
return config_.DumpString(""); return config_.DumpString("");
} }
const ConfigNode& YamlConfigMgr::GetRootNode() const { const ConfigNode &
YamlConfigMgr::GetRootNode() const {
return config_; return config_;
} }
ConfigNode& YamlConfigMgr::GetRootNode() { ConfigNode &
YamlConfigMgr::GetRootNode() {
return config_; return config_;
} }
bool bool
YamlConfigMgr::SetConfigValue(const YAML::Node& node, YamlConfigMgr::SetConfigValue(const YAML::Node &node,
const std::string& key, const std::string &key,
ConfigNode& config) { ConfigNode &config) {
if(node[key].IsDefined ()) { if (node[key].IsDefined()) {
config.SetValue(key, node[key].as<std::string>()); config.SetValue(key, node[key].as<std::string>());
return true; return true;
} }
...@@ -73,10 +78,10 @@ YamlConfigMgr::SetConfigValue(const YAML::Node& node, ...@@ -73,10 +78,10 @@ YamlConfigMgr::SetConfigValue(const YAML::Node& node,
} }
bool bool
YamlConfigMgr::SetChildConfig(const YAML::Node& node, YamlConfigMgr::SetChildConfig(const YAML::Node &node,
const std::string& child_name, const std::string &child_name,
ConfigNode& config) { ConfigNode &config) {
if(node[child_name].IsDefined ()) { if (node[child_name].IsDefined()) {
ConfigNode sub_config; ConfigNode sub_config;
LoadConfigNode(node[child_name], sub_config); LoadConfigNode(node[child_name], sub_config);
config.AddChild(child_name, sub_config); config.AddChild(child_name, sub_config);
...@@ -89,9 +94,9 @@ bool ...@@ -89,9 +94,9 @@ bool
YamlConfigMgr::SetSequence(const YAML::Node &node, YamlConfigMgr::SetSequence(const YAML::Node &node,
const std::string &child_name, const std::string &child_name,
ConfigNode &config) { ConfigNode &config) {
if(node[child_name].IsDefined ()) { if (node[child_name].IsDefined()) {
size_t cnt = node[child_name].size(); 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<std::string>()); config.AddSequenceItem(child_name, node[child_name][i].as<std::string>());
} }
return true; return true;
...@@ -100,22 +105,22 @@ YamlConfigMgr::SetSequence(const YAML::Node &node, ...@@ -100,22 +105,22 @@ YamlConfigMgr::SetSequence(const YAML::Node &node,
} }
void void
YamlConfigMgr::LoadConfigNode(const YAML::Node& node, ConfigNode& config) { YamlConfigMgr::LoadConfigNode(const YAML::Node &node, ConfigNode &config) {
std::string key; std::string key;
for (YAML::const_iterator it = node.begin(); it != node.end(); ++it) { for (YAML::const_iterator it = node.begin(); it != node.end(); ++it) {
if(!it->first.IsNull()){ if (!it->first.IsNull()) {
key = it->first.as<std::string>(); key = it->first.as<std::string>();
} }
if(node[key].IsScalar()) { if (node[key].IsScalar()) {
SetConfigValue(node, key, config); SetConfigValue(node, key, config);
} else if(node[key].IsMap()){ } else if (node[key].IsMap()) {
SetChildConfig(node, key, config); SetChildConfig(node, key, config);
} else if(node[key].IsSequence()){ } else if (node[key].IsSequence()) {
SetSequence(node, key, config); SetSequence(node, key, config);
} }
} }
} }
} } // namespace server
} } // namespace milvus
} } // namespace zilliz
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "ConfigNode.h" #include "ConfigNode.h"
#include "utils/Error.h" #include "utils/Error.h"
#include <string>
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
namespace zilliz { namespace zilliz {
...@@ -33,15 +34,15 @@ class YamlConfigMgr : public ConfigMgr { ...@@ -33,15 +34,15 @@ class YamlConfigMgr : public ConfigMgr {
virtual void Print() const; virtual void Print() const;
virtual std::string DumpString() const; virtual std::string DumpString() const;
virtual const ConfigNode& GetRootNode() const; virtual const ConfigNode &GetRootNode() const;
virtual ConfigNode& GetRootNode(); virtual ConfigNode &GetRootNode();
private: private:
bool SetConfigValue(const YAML::Node& node, bool SetConfigValue(const YAML::Node &node,
const std::string& key, const std::string &key,
ConfigNode& config); ConfigNode &config);
bool SetChildConfig(const YAML::Node& node, bool SetChildConfig(const YAML::Node &node,
const std::string &name, const std::string &name,
ConfigNode &config); ConfigNode &config);
...@@ -50,15 +51,13 @@ class YamlConfigMgr : public ConfigMgr { ...@@ -50,15 +51,13 @@ class YamlConfigMgr : public ConfigMgr {
const std::string &child_name, const std::string &child_name,
ConfigNode &config); ConfigNode &config);
void LoadConfigNode(const YAML::Node& node, ConfigNode& config); void LoadConfigNode(const YAML::Node &node, ConfigNode &config);
private: private:
YAML::Node node_; YAML::Node node_;
ConfigNode config_; ConfigNode config_;
}; };
} } // namespace server
} } // namespace milvus
} } // namespace zilliz
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册