Исходный код СУБД 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <set>
#include "dgrid_config.h"
#include "dgrid_logger.h"
#include "dgridd_datastore_simple.h"
#include "dgridd_node_arbiter.h"
 
namespace DGrid {
 
DGriddDataStoreSimple::DGriddDataStoreSimple(std::string session, DGriddNodeArbiter* a)
    :_node_arbiter(a), _session(session)
{
}
 
DGriddDataStoreSimple::~DGriddDataStoreSimple()
{
}
 
void DGriddDataStoreSimple::setNodeArbiter(DGriddNodeArbiter* a)
{
    _node_arbiter = a;
}
 
void DGriddDataStoreSimple::showStats()
{
    writeLog(LOG_INFO, "> Simple datastote stats:");
    writeLog(LOG_INFO, "  Themes total: %d", getThemesCount());
    writeLog(LOG_INFO, "  Keys total: %d", getKeysCount());
}
 
uint32_t DGriddDataStoreSimple::getThemesCount()
{
    std::lock_guard<std::mutex> lock(io_mutex);
    return internal_data.size();
}
 
uint64_t DGriddDataStoreSimple::getKeysCount()
{
    uint64_t cnt(0);
    std::lock_guard<std::mutex> lock(io_mutex);
    for(auto theme: internal_data) {
        cnt += theme.second.size();
    }
    return cnt;
}
 
/*
client-controlled methods: stats
*/
 
DGridResult DGriddDataStoreSimple::get_stats(const std::string& theme, std::vector<std::string>& list, std::string& error_string)
{
    DGridResult r(DGRID_OK);
    list.clear();
 
    io_mutex.lock();
    if(theme.empty()) {
        for(auto it: internal_data) {
            list.push_back(it.first);
        }
    } else {
        auto theme_it = internal_data.find(theme);
        if(theme_it == internal_data.end()) {
            error_string = "Unknown theme '" + theme + "'";
            r = DGRID_NOT_FOUND;
        } else {
            for(auto it: internal_data[theme]) {
                list.push_back(it.first);
            }
        }
    }
    io_mutex.unlock();
    return r;
}
 
/*
 client-controlled methods
*/
 
DGridResult DGriddDataStoreSimple::set_value(const std::string& theme, const std::string& key, /*const*/ SimpleValueData& value, std::string& error_string)
{
    DGridResult r(DGRID_OK);
    bool replicate(false);
    NodeID old_source;
    error_string.clear();
    value.source_node = Config::node_id();
 
    io_mutex.lock();
    {
        auto it = internal_data[theme].find(key);
        if(it != internal_data[theme].end()) {
            if(it->second.ts > value.ts) {
                writeLog(LOG_NOTICE, "\E[33;60m older record received (%s/%s), ignoring", theme.c_str(), key.c_str());
                r = DGRID_TOO_OLD;
            } else {
                if(!it->second.source_node.empty() && it->second.source_node != value.source_node) {
                    writeLog(LOG_DEBUG, "\E[33;60m value '%s/%s' source changed (was '%s'), have to notify old owner",
                        theme.c_str(), key.c_str(), raw_to_hexstring(it->second.source_node).c_str());
                }
                old_source = it->second.source_node;
                it->second = value;
                replicate = true;
            }
        } else {
            internal_data[theme][key] = value;
            replicate = true;
        }
    }
    io_mutex.unlock();
    if(replicate) {
        // send new data to other nodes
        writeLog(LOG_DEBUG, "\E[33;60m send new key (%s/%s) to other nodes...", theme.c_str(), key.c_str());
        if(_node_arbiter) {
            SimpleValueMap data;
            data[key] = value;
            _node_arbiter->append_theme_simple(_session, theme, data, old_source);
        }
    }
    return r;
}
 
DGridResult DGriddDataStoreSimple::get_value(const std::string& theme, const std::string& key, SimpleValueData& value, std::string& error_string)
{
    DGridResult r(DGRID_NOT_FOUND);
    error_string.clear();
    NodeID old_source;
 
    io_mutex.lock();
    {
        auto theme_it = internal_data.find(theme);
        if(theme_it != internal_data.end()) {
            auto key_it = theme_it->second.find(key);
            if(key_it != theme_it->second.end()) {
                value = key_it->second;
                if(Config::node_id() != value.source_node) {
                    old_source = value.source_node;
                }
                r = DGRID_OK;
            }
        }
    }
    io_mutex.unlock();
    // check data in nodes
    if(_node_arbiter != nullptr && (DGRID_NOT_FOUND == r || !old_source.empty())) {
        SimpleValueMap data;
        data[key] = SimpleValueData();
        if(old_source.empty())
            writeLog(LOG_DEBUG, "\E[33;60m key (%s/%s) not found, checking other nodes...", theme.c_str(), key.c_str());
        else
            writeLog(LOG_DEBUG, "\E[33;60m key (%s/%s) found, but owner differs, check source...", theme.c_str(), key.c_str());
        if (DGRID_OK == _node_arbiter->get_theme_simple(_session, theme, data, old_source)) {
            io_mutex.lock();
            for(auto& it: data) {
                internal_data[theme][it.first] = it.second;
            }
            io_mutex.unlock();
            auto key_it = data.find(key);
            if(key_it != data.end()) {
                value = key_it->second;
                r = DGRID_OK;
            }
        }
    }
    if (DGRID_NOT_FOUND == r) {
        error_string = "key (" + theme + "/" + key + ") not found";
    }
    return r;
}
 
DGridResult DGriddDataStoreSimple::del_value(const std::string& theme, const std::string& key, std::string& error_string)
{
    DGridResult r(DGRID_NOT_FOUND);
    error_string.clear();
    std::lock_guard<std::mutex> lock(io_mutex);
 
    auto theme_it = internal_data.find(theme);
    if(theme_it != internal_data.end()) {
        if(theme_it->second.erase(key) > 0)
            r = DGRID_OK;
    }
    // TODO _node_arbiter->erase()
 
    if (DGRID_NOT_FOUND == r) {
        error_string = "key (" + theme + "/" + key + ") not found";
    }
    return r;
}
 
/*
    node interaction methods
*/
 
// ответ на запрос темы (в data можно указать ключи, если нужна не вся тема)
DGridResult DGriddDataStoreSimple::get_theme_simple(const std::string& name, SimpleValueMap& data)
{
    std::lock_guard<std::mutex> lock(io_mutex);
    auto theme_it = internal_data.find(name);
    if(theme_it != internal_data.end()) {
        if(data.empty()) {
            data = theme_it->second;
            return DGRID_OK;
        } else {
            std::set<std::string> requested_keys;
            // collect requested keys
            for(auto& it: data) {
                requested_keys.insert(it.first);
            }
            data.clear();
            // search internals & fill data for requested keys
            for(auto& key: requested_keys) {
                auto it = theme_it->second.find(key);
                if(it != theme_it->second.end()) {
                    data[it->first] = it->second;
                }
            }
            if(!data.empty())
                return DGRID_OK;
        }
    }
    return DGRID_NOT_FOUND;
}
 
// сохраняем копию в локальное хранилище
DGridResult DGriddDataStoreSimple::append_theme_simple(const std::string& name, const SimpleValueMap& data, NodeID& source_node)
{
    std::lock_guard<std::mutex> lock(io_mutex);
    bool source_checked(false);
    for(auto& it: data) {
        if (internal_data[name][it.first].ts > it.second.ts) {
            writeLog(LOG_NOTICE, "\E[33;60m older record received (%s/%s), ignoring", name.c_str(), it.first.c_str());
        } else {
            if(!source_checked) {
                auto key_it = internal_data[name].find(it.first);
                if(key_it != internal_data[name].end())
                    source_node = key_it->second.source_node;
                source_checked = true;
            }
            internal_data[name][it.first] = it.second;
        }
    }
    return DGRID_OK;
}
 
// дописываем в локальное хранилище и возвращаем разницу
DGridResult DGriddDataStoreSimple::sync_theme_simple(const std::string& name, SimpleValueMap& data)
{
    std::lock_guard<std::mutex> lock(io_mutex);
    auto theme_it = internal_data.find(name);
    if(theme_it == internal_data.end()) {
        internal_data[name] = data;
        data.clear();
    } else {
        std::set<std::string> ext_keys;
        // append local storage
        for(auto& it: data) {
            if (internal_data[name][it.first].ts < it.second.ts) {
                // save new version
                theme_it->second[it.first] = it.second;
                ext_keys.insert(it.first);
            } else if (internal_data[name][it.first].ts == it.second.ts) {
                // just mark to shorten result
                ext_keys.insert(it.first);
            }
            // return new version if older received
        }
        // return diff
        data.clear();
        for(auto& it: theme_it->second) {
            if(ext_keys.find(it.first) == ext_keys.end())
                data[it.first] = it.second;
        }
    }
    return DGRID_OK;
}
 
} // namespace DGrid