提交 d5a825ed 编写于 作者: S starlord

format config code


Former-commit-id: 6d87e0d669e5f8029453a94cc1817372263ff30f
上级 4c91d328
......@@ -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
......@@ -20,6 +20,8 @@
#include "utils/Error.h"
#include "ConfigNode.h"
#include <string>
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
......@@ -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<std::string, std::string>& kv = target.GetConfig();
for(auto itr = kv.begin(); itr != kv.end(); ++itr){
void
ConfigNode::Combine(const ConfigNode &target) {
const std::map<std::string, std::string> &kv = target.GetConfig();
for (auto itr = kv.begin(); itr != kv.end(); ++itr) {
config_[itr->first] = itr->second;
}
const std::map<std::string, std::vector<std::string> >& sequences = target.GetSequences();
for(auto itr = sequences.begin(); itr != sequences.end(); ++itr){
const std::map<std::string, std::vector<std::string> > &sequences = target.GetSequences();
for (auto itr = sequences.begin(); itr != sequences.end(); ++itr) {
sequences_[itr->first] = itr->second;
}
const std::map<std::string, ConfigNode>& children = target.GetChildren();
for(auto itr = children.begin(); itr != children.end(); ++itr){
const std::map<std::string, ConfigNode> &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 &param_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 &param_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 &param_key, double default_val) con
}
}
const std::map<std::string, std::string>&
const std::map<std::string, std::string> &
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<std::string, ConfigNode>&
const std::map<std::string, ConfigNode> &
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<std::string>
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<std::string> temp;
......@@ -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 {
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
......@@ -30,7 +30,7 @@ typedef std::vector<ConfigNode> 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 &param_key, float 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();
//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<std::string, ConfigNode>& GetChildren() const;
const std::map<std::string, ConfigNode> &GetChildren() const;
void ClearChildren();
//key/sequence config
void AddSequenceItem(const std::string &key, const std::string &item);
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 PrintAll(const std::string &prefix = "") const;
......@@ -70,6 +70,6 @@ class ConfigNode {
std::map<std::string, std::vector<std::string> > sequences_;
};
}
}
}
} // namespace server
} // namespace milvus
} // namespace zilliz
......@@ -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 <sys/stat.h>
......@@ -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<std::string>());
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<std::string>());
}
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<std::string>();
}
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
......@@ -21,6 +21,7 @@
#include "ConfigNode.h"
#include "utils/Error.h"
#include <string>
#include <yaml-cpp/yaml.h>
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册