ConfigNode.cpp 5.6 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/*******************************************************************************
 * 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 <sstream>
#include <string>
#include <algorithm>

namespace zilliz {
namespace vecwise {
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){
        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){
        sequences_[itr->first] = itr->second;
    }

    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) {
    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 &param_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 &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);
    } else {
        return default_val;
    }
}

int64_t
ConfigNode::GetInt64Value(const std::string &param_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 &param_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 &param_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<std::string, std::string>&
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<std::string, ConfigNode>&
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<std::string>
ConfigNode::GetSequence(const std::string &key) const {
    auto itr = sequences_.find(key);
    if(itr != sequences_.end()) {
        return itr->second;
    } else {
        std::vector<std::string> temp;
        return temp;
    }
}

const std::map<std::string, std::vector<std::string> >&
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();
}

}
}
}