Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
#include <fstream>
#include <uuid/uuid.h>
 
#include "dgrid_logger.h"
#include "dgrid_config.h"
 
namespace DGrid {
 
Config* Config::_instance = nullptr;
NodeID Config::_node_id;
 
Config::Config() {}
Config::~Config() {}
 
Config* Config::instance()
{
    if (nullptr == _instance) {
        _instance = new Config();
    }
    return _instance;
}
 
void Config::destroy_instance()
{
    if(nullptr != _instance) {
        delete _instance;
        _instance = nullptr;
    }
}
 
NodeID Config::node_id()
{
    if(_node_id.empty()) {
        _node_id.resize(16);
        uuid_generate(reinterpret_cast<unsigned char*>(_node_id.data()));
        writeLog(LOG_INFO, "Current node ID: %s", raw_to_hexstring(_node_id).c_str() );
    }
    return _node_id;
}
 
bool Config::load(const std::string& fname)
{
    int line_num(0);
    int node_idx(-1);
    std::ifstream cfile(fname);
    std::string group;
    std::string::size_type pos, first, last;
    std::string key, val;
 
    if(!cfile.is_open()) {
        writeLog(LOG_WARNING, "Can't open config file '%s'", fname.c_str());
        return false;
    }
    writeLog(LOG_INFO, "Reading config '%s'", fname.c_str());
 
    for(std::string line; std::getline(cfile, line); line_num++) {
        if(std::string::npos != (pos = line.find("#"))) {
            line = line.substr(0, pos);
        }
        line = strtrim(line);
        if(line.empty())
            continue;
//        writeLog(LOG_DEBUG, "%s", line.c_str());
 
        if(std::string::npos != (first = line.find_first_of("[")) &&
            std::string::npos != (last = line.find_last_of("]"))) {
            group = to_lower_case(strtrim(line.substr(first+1, last - first - 1)));
            if("node" == group) {
                node_idx++;
                nodes.resize(node_idx+1);
            }
        }
 
        if(std::string::npos != (pos = line.find("="))) {
            key = strtrim(line.substr(0, pos));
            val = strtrim(line.substr(pos + 1));
 
            if(key.empty()) {
                writeLog(LOG_WARNING, "Error in config file at line %d", line_num);
                continue;
            }
 
            if("common" == group) {
                common[key] = val;
            } else if("node" == group) {
                nodes[node_idx][key] = val;
            }
        }
    }
 
    writeLog(LOG_DEBUG, "Local settings:");
    for(auto p: common) {
        writeLog(LOG_DEBUG, "\t'%s' = '%s'", p.first.c_str(), p.second.c_str());
    }
    writeLog(LOG_DEBUG, "Known nodes:");
    for(auto p: nodes) {
        writeLog(LOG_DEBUG, "\t'%s'@%s:%d", p["name"].c_str(), p["addr"].c_str(), std::stoi(p["port"]));
    }
    cfile.close();
    return true;
}
 
std::string Config::get_cluster_key() const
{
    return get("cluster_key");
}
 
std::string Config::get(const std::string& key, const std::string& default_value) const
{
    auto it = common.find(key);
    if(it == common.end())
        return default_value;
    return it->second;
}
 
uint16_t Config::get(const std::string& key, const uint16_t value) const
{
    return atoi(get(key, std::to_string(value)).c_str());
}
 
const std::vector<stringmap>& Config::getnodes() const
{
    return nodes;
}
 
void Config::clear() {
    common.clear();
    nodes.clear();
}
 
} // namespace DGrid