ConfigNode.cpp 6.1 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

S
starlord 已提交
18
#include "config/ConfigNode.h"
G
groot 已提交
19
#include "utils/Error.h"
G
groot 已提交
20
#include "utils/Log.h"
G
groot 已提交
21

S
starlord 已提交
22
#include <algorithm>
G
groot 已提交
23 24 25
#include <sstream>
#include <string>

J
jinhai 已提交
26
namespace milvus {
G
groot 已提交
27 28
namespace server {

S
starlord 已提交
29
void
S
starlord 已提交
30 31
ConfigNode::Combine(const ConfigNode& target) {
    const std::map<std::string, std::string>& kv = target.GetConfig();
S
starlord 已提交
32
    for (auto itr = kv.begin(); itr != kv.end(); ++itr) {
G
groot 已提交
33 34 35
        config_[itr->first] = itr->second;
    }

S
starlord 已提交
36
    const std::map<std::string, std::vector<std::string> >& sequences = target.GetSequences();
S
starlord 已提交
37
    for (auto itr = sequences.begin(); itr != sequences.end(); ++itr) {
G
groot 已提交
38 39 40
        sequences_[itr->first] = itr->second;
    }

S
starlord 已提交
41
    const std::map<std::string, ConfigNode>& children = target.GetChildren();
S
starlord 已提交
42
    for (auto itr = children.begin(); itr != children.end(); ++itr) {
G
groot 已提交
43 44 45 46
        children_[itr->first] = itr->second;
    }
}

S
starlord 已提交
47
// key/value pair config
G
groot 已提交
48
void
S
starlord 已提交
49
ConfigNode::SetValue(const std::string& key, const std::string& value) {
G
groot 已提交
50 51 52 53
    config_[key] = value;
}

std::string
S
starlord 已提交
54
ConfigNode::GetValue(const std::string& param_key, const std::string& default_val) const {
G
groot 已提交
55
    auto ref = config_.find(param_key);
S
starlord 已提交
56
    if (ref != config_.end()) {
G
groot 已提交
57 58 59
        return ref->second;
    }

S
starlord 已提交
60
    // THROW_UNEXPECTED_ERROR("Can't find parameter key: " + param_key);
G
groot 已提交
61 62 63 64
    return default_val;
}

bool
S
starlord 已提交
65
ConfigNode::GetBoolValue(const std::string& param_key, bool default_val) const {
G
groot 已提交
66 67 68 69 70 71 72 73 74 75
    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
S
starlord 已提交
76
ConfigNode::GetInt32Value(const std::string& param_key, int32_t default_val) const {
G
groot 已提交
77 78
    std::string val = GetValue(param_key);
    if (!val.empty()) {
S
starlord 已提交
79
        return (int32_t)std::strtol(val.c_str(), nullptr, 10);
G
groot 已提交
80 81 82 83 84 85
    } else {
        return default_val;
    }
}

int64_t
S
starlord 已提交
86
ConfigNode::GetInt64Value(const std::string& param_key, int64_t default_val) const {
G
groot 已提交
87 88 89 90 91 92 93 94 95
    std::string val = GetValue(param_key);
    if (!val.empty()) {
        return std::strtol(val.c_str(), nullptr, 10);
    } else {
        return default_val;
    }
}

float
S
starlord 已提交
96
ConfigNode::GetFloatValue(const std::string& param_key, float default_val) const {
G
groot 已提交
97 98 99 100 101 102 103 104 105
    std::string val = GetValue(param_key);
    if (!val.empty()) {
        return std::strtof(val.c_str(), nullptr);
    } else {
        return default_val;
    }
}

double
S
starlord 已提交
106
ConfigNode::GetDoubleValue(const std::string& param_key, double default_val) const {
G
groot 已提交
107 108
    std::string val = GetValue(param_key);
    if (!val.empty()) {
J
jinhai 已提交
109
        return std::strtod(val.c_str(), nullptr);
G
groot 已提交
110 111 112 113 114
    } else {
        return default_val;
    }
}

S
starlord 已提交
115
const std::map<std::string, std::string>&
G
groot 已提交
116 117
ConfigNode::GetConfig() const {
    return config_;
S
starlord 已提交
118
}
G
groot 已提交
119

S
starlord 已提交
120 121
void
ConfigNode::ClearConfig() {
G
groot 已提交
122 123 124
    config_.clear();
}

S
starlord 已提交
125
// key/object config
G
groot 已提交
126
void
S
starlord 已提交
127
ConfigNode::AddChild(const std::string& type_name, const ConfigNode& config) {
G
groot 已提交
128 129 130 131
    children_[type_name] = config;
}

ConfigNode
S
starlord 已提交
132
ConfigNode::GetChild(const std::string& type_name) const {
G
groot 已提交
133
    auto ref = children_.find(type_name);
S
starlord 已提交
134
    if (ref != children_.end()) {
G
groot 已提交
135 136 137 138 139 140 141
        return ref->second;
    }

    ConfigNode nc;
    return nc;
}

S
starlord 已提交
142 143
ConfigNode&
ConfigNode::GetChild(const std::string& type_name) {
G
groot 已提交
144 145 146 147
    return children_[type_name];
}

void
S
starlord 已提交
148
ConfigNode::GetChildren(ConfigNodeArr& arr) const {
G
groot 已提交
149
    arr.clear();
S
starlord 已提交
150
    for (auto ref : children_) {
G
groot 已提交
151 152 153 154
        arr.push_back(ref.second);
    }
}

S
starlord 已提交
155
const std::map<std::string, ConfigNode>&
G
groot 已提交
156 157 158 159
ConfigNode::GetChildren() const {
    return children_;
}

S
starlord 已提交
160 161
void
ConfigNode::ClearChildren() {
G
groot 已提交
162 163 164
    children_.clear();
}

S
starlord 已提交
165
// key/sequence config
G
groot 已提交
166
void
S
starlord 已提交
167
ConfigNode::AddSequenceItem(const std::string& key, const std::string& item) {
G
groot 已提交
168 169 170 171
    sequences_[key].push_back(item);
}

std::vector<std::string>
S
starlord 已提交
172
ConfigNode::GetSequence(const std::string& key) const {
G
groot 已提交
173
    auto itr = sequences_.find(key);
S
starlord 已提交
174
    if (itr != sequences_.end()) {
G
groot 已提交
175 176 177 178 179 180 181
        return itr->second;
    } else {
        std::vector<std::string> temp;
        return temp;
    }
}

S
starlord 已提交
182
const std::map<std::string, std::vector<std::string> >&
G
groot 已提交
183 184 185 186
ConfigNode::GetSequences() const {
    return sequences_;
}

S
starlord 已提交
187 188
void
ConfigNode::ClearSequences() {
G
groot 已提交
189 190 191 192
    sequences_.clear();
}

void
S
starlord 已提交
193 194
ConfigNode::PrintAll(const std::string& prefix) const {
    for (auto& elem : config_) {
G
groot 已提交
195
        SERVER_LOG_INFO << prefix << elem.first + ": " << elem.second;
G
groot 已提交
196 197
    }

S
starlord 已提交
198
    for (auto& elem : sequences_) {
G
groot 已提交
199
        SERVER_LOG_INFO << prefix << elem.first << ": ";
S
starlord 已提交
200
        for (auto& str : elem.second) {
G
groot 已提交
201
            SERVER_LOG_INFO << prefix << "    - " << str;
G
groot 已提交
202 203 204
        }
    }

S
starlord 已提交
205
    for (auto& elem : children_) {
G
groot 已提交
206
        SERVER_LOG_INFO << prefix << elem.first << ": ";
G
groot 已提交
207 208 209 210 211
        elem.second.PrintAll(prefix + "    ");
    }
}

std::string
S
starlord 已提交
212
ConfigNode::DumpString(const std::string& prefix) const {
G
groot 已提交
213 214
    std::stringstream str_buffer;
    const std::string endl = "\n";
S
starlord 已提交
215
    for (auto& elem : config_) {
G
groot 已提交
216 217 218
        str_buffer << prefix << elem.first << ": " << elem.second << endl;
    }

S
starlord 已提交
219
    for (auto& elem : sequences_) {
G
groot 已提交
220
        str_buffer << prefix << elem.first << ": " << endl;
S
starlord 已提交
221
        for (auto& str : elem.second) {
G
groot 已提交
222 223 224 225
            str_buffer << prefix + "    - " << str << endl;
        }
    }

S
starlord 已提交
226
    for (auto& elem : children_) {
G
groot 已提交
227 228 229 230 231 232 233
        str_buffer << prefix << elem.first << ": " << endl;
        str_buffer << elem.second.DumpString(prefix + "    ") << endl;
    }

    return str_buffer.str();
}

S
starlord 已提交
234 235
}  // namespace server
}  // namespace milvus