/******************************************************************************* * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved * Unauthorized copying of this file, via any medium is strictly prohibited. * Proprietary and confidential. ******************************************************************************/ #include "ConfigNode.h" #include "utils/Error.h" #include "utils/CommonUtil.h" #include #include #include namespace zilliz { namespace vecwise { namespace server { 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){ sequences_[itr->first] = itr->second; } 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) { config_[key] = value; } std::string ConfigNode::GetValue(const std::string& param_key, const std::string& default_val) const { auto ref = config_.find(param_key); if(ref != config_.end()) { return ref->second; } //THROW_UNEXPECTED_ERROR("Can't find parameter key: " + param_key); return default_val; } bool ConfigNode::GetBoolValue(const std::string ¶m_key, bool default_val) const { std::string val = GetValue(param_key); if (!val.empty()) { std::transform(val.begin(), val.end(), val.begin(), ::tolower); return (val == "true" || val == "on" || val == "yes" || val == "1"); } else { return default_val; } } 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); } else { return default_val; } } int64_t ConfigNode::GetInt64Value(const std::string ¶m_key, int64_t default_val) const { std::string val = GetValue(param_key); if (!val.empty()) { return std::strtol(val.c_str(), nullptr, 10); } else { return default_val; } } float ConfigNode::GetFloatValue(const std::string ¶m_key, float default_val) const { std::string val = GetValue(param_key); if (!val.empty()) { return std::strtof(val.c_str(), nullptr); } else { return default_val; } } double ConfigNode::GetDoubleValue(const std::string ¶m_key, double default_val) const { std::string val = GetValue(param_key); if (!val.empty()) { return std::strtold(val.c_str(), nullptr); } else { return default_val; } } const std::map& ConfigNode::GetConfig() const { return config_; }; void ConfigNode::ClearConfig() { config_.clear(); } //key/object config void ConfigNode::AddChild(const std::string& type_name, const ConfigNode& config) { children_[type_name] = config; } ConfigNode ConfigNode::GetChild(const std::string& type_name) const { auto ref = children_.find(type_name); if(ref != children_.end()) { return ref->second; } ConfigNode nc; return nc; } ConfigNode& ConfigNode::GetChild(const std::string &type_name) { return children_[type_name]; } void ConfigNode::GetChildren(ConfigNodeArr& arr) const { arr.clear(); for(auto ref : children_){ arr.push_back(ref.second); } } const std::map& ConfigNode::GetChildren() const { return children_; } void ConfigNode::ClearChildren() { children_.clear(); } //key/sequence config void ConfigNode::AddSequenceItem(const std::string &key, const std::string &item) { sequences_[key].push_back(item); } std::vector ConfigNode::GetSequence(const std::string &key) const { auto itr = sequences_.find(key); if(itr != sequences_.end()) { return itr->second; } else { std::vector temp; return temp; } } const std::map >& ConfigNode::GetSequences() const { return sequences_; } void ConfigNode::ClearSequences() { sequences_.clear(); } void ConfigNode::PrintAll(const std::string& prefix) const { for(auto& elem : config_) { CommonUtil::PrintInfo(prefix + elem.first + ": " + elem.second); } for(auto& elem : sequences_) { CommonUtil::PrintInfo(prefix + elem.first + ": "); for(auto& str : elem.second) { CommonUtil::PrintInfo(prefix + " - " + str); } } for(auto& elem : children_) { CommonUtil::PrintInfo(prefix + elem.first + ": "); elem.second.PrintAll(prefix + " "); } } std::string ConfigNode::DumpString(const std::string &prefix) const { std::stringstream str_buffer; const std::string endl = "\n"; for(auto& elem : config_) { str_buffer << prefix << elem.first << ": " << elem.second << endl; } for(auto& elem : sequences_) { str_buffer << prefix << elem.first << ": " << endl; for(auto& str : elem.second) { str_buffer << prefix + " - " << str << endl; } } for(auto& elem : children_) { str_buffer << prefix << elem.first << ": " << endl; str_buffer << elem.second.DumpString(prefix + " ") << endl; } return str_buffer.str(); } } } }