Исходный код СУБД 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <uuid/uuid.h>
 
#include "dgrid_logger.h"
#include "dgridd_datastore.h"
#include "dgridd_datastore_simple.h"
#include "dgridd_datastore_table.h"
 
namespace DGrid {
 
#define TABLEID_HASH_SEED 0x136C936C
 
uint64_t TableIDhasher::operator()(const TableID& k) const
{
    uint64_t hash(TABLEID_HASH_SEED);
    for(const uint8_t c: k) {
        hash = ((hash << 1) | (hash >> 63)) ^ c;
    }
    return hash;
}
 
DGriddDataStore* DGriddDataStore::_instance = nullptr;
 
DGriddDataStore::DGriddDataStore()
{
    _simple_default = std::make_shared<DGriddDataStoreSimple>();
    _table_default  = std::make_shared<DGriddDataStoreTable>();
}
DGriddDataStore::~DGriddDataStore() {}
 
DGriddDataStore* DGriddDataStore::instance()
{
    if (_instance == nullptr) {
        _instance = new DGriddDataStore();
    }
    return _instance;
}
void DGriddDataStore::destroy()
{
    if (_instance != nullptr) {
        delete _instance;
        _instance = nullptr;
    }
}
 
void DGriddDataStore::setNodeArbiter(DGriddNodeArbiter* a)
{
    _node_arbiter = a;
    _simple_default->setNodeArbiter(_node_arbiter);
}
 
DGriddNodeArbiter* DGriddDataStore::getNodeArbiter()
{
    return _node_arbiter;
}
 
void DGriddDataStore::showStats(const uint8_t detail_level)
{
    uint64_t tables_total(0);
    std::string err_details;
    writeLog(LOG_INFO, "> DataStote stats (default): %d themes, %d keys, %lu tables",
            _simple_default->getThemesCount(),
            _simple_default->getKeysCount(),
            _table_default->getTablesCount());
 
    simple_session_mutex.lock();
    writeLog(LOG_INFO, "> DataStote stats (sessions): %d K-V, %d T", _simple_session.size(), _table_session.size());
    if(detail_level>1) {
        for(auto s: _simple_session) {
            writeLog(LOG_INFO, " > %s: %d themes, %d keys", s.first.c_str(), s.second->getThemesCount(), s.second->getKeysCount());
        }
    }
    simple_session_mutex.unlock();
    table_session_mutex.lock();
    if(detail_level>1) {
        for(auto ts: _table_session) {
            uint64_t cnt(0);
            for(auto t: ts.second) {
                cnt += t.second->getTablesCount();
            }
            tables_total += cnt;
            writeLog(LOG_INFO, " > %s: %d tablespaces, %d tables", ts.first.c_str(), ts.second.size(), cnt);
            /*
            for(auto t: ts.second) {
                writeLog(LOG_INFO, "  > %s: %lu tables", t.first.c_str(), t.second->getTablesCount());
                for(std::string tname: t.second->getTablesList()) {
                    t.second->getTableSize(tname, cnt, err_details);
                    writeLog(LOG_INFO, "   - %s (%d rows) '%s'", raw_to_hexstring(getTableID(ts.first, t.first, tname)).c_str(), cnt, tname.c_str());
                }
            }
            */
        }
        writeLog(LOG_INFO, "> Tables total: %lu", tables_total);
    }
    writeLog(LOG_INFO, "> Tables registered: %lu", registered_tables.size());
    table_session_mutex.unlock();
}
 
void DGriddDataStore::DGriddDataStore::clearSession(std::string session)
{
    simple_session_mutex.lock();
    _simple_session.erase(session);
    simple_session_mutex.unlock();
 
    table_session_mutex.lock();
    _table_session.erase(session);
    table_session_mutex.unlock();
 
    table_reg_mutex.lock();
    deregisterSessionTables_int(session);
    table_reg_mutex.unlock();
}
 
void DGriddDataStore::list_sessions_simple(std::vector<std::string>& list)
{
    list.clear();
 
    simple_session_mutex.lock();
    list.reserve(_simple_session.size());
    for(auto s: _simple_session)
        list.push_back(s.first);
    simple_session_mutex.unlock();
}
 
void DGriddDataStore::list_sessions_table(std::vector<std::string>& list)
{
    list.clear();
 
    table_session_mutex.lock();
    list.reserve(_table_session.size());
    for(auto s: _table_session)
        list.push_back(s.first);
    table_session_mutex.unlock();
}
 
void DGriddDataStore::list_tablespaces(const std::string& session, std::vector<std::string>& list)
{
    list.clear();
    if(session.empty())
        return;
 
    table_session_mutex.lock();
    auto its = _table_session.find(session);
    if(its != _table_session.end()) {
        for(auto it: its->second) {
            list.push_back(it.first);
        }
    }
    table_session_mutex.unlock();
}
 
std::shared_ptr<DGriddDataStoreSimple> DGriddDataStore::simple(const std::string& session)
{
    if(session.empty())
        return _simple_default;
 
    simple_session_mutex.lock();
    std::shared_ptr<DGriddDataStoreSimple> store = nullptr;
    auto it = _simple_session.find(session);
    if(it != _simple_session.end()) {
        store = it->second;
    } else {
        store = std::make_shared<DGriddDataStoreSimple>(session, _node_arbiter);
        _simple_session[session] = store;
    }
    simple_session_mutex.unlock();
    return store;
}
 
std::shared_ptr<DGriddDataStoreTable> DGriddDataStore::table(const std::string& session, const std::string& space)
{
    if(session.empty())
        return _table_default;
 
    table_session_mutex.lock();
    std::shared_ptr<DGriddDataStoreTable>  store;
    auto it = _table_session[session].find(space);
    if(it != _table_session[session].end()) {
        store = it->second;
    } else {
        store = std::make_shared<DGriddDataStoreTable>(session, _node_arbiter);
        _table_session[session][space] = store;
    }
    table_session_mutex.unlock();
    return store;
}
 
std::shared_ptr<DGriddDataStoreTable> DGriddDataStore::table(const TableID& table_id, std::string& tname)
{
    std::shared_ptr<TableDescription> descr = getTableDescription(table_id);
    if(nullptr == descr)
        return nullptr;
    tname = descr->tname;
    return table(descr->session, descr->space);
}
 
bool DGriddDataStore::hasTable(const TableID& table_id)
{
    std::shared_ptr<TableDescription> descr = getTableDescription(table_id);
    return descr != nullptr;
}
 
void DGriddDataStore::registerTable(const NodeID& master_node, TableFullID& id_table)
{
    std::lock_guard<std::mutex> lock(table_reg_mutex);
    if(id_table.id.empty())
        id_table.id = createTableID();
    registered_tables[id_table.id] = std::make_shared<TableDescription>(id_table.session, id_table.space, id_table.name, master_node);
}
 
void DGriddDataStore::setMasterNode(const TableID& id_table, const NodeID& id_node)
{
    std::lock_guard<std::mutex> lock(table_reg_mutex);
    auto it = registered_tables.find(id_table);
    if(it != registered_tables.end())
        it->second->master_node = id_node;
}
 
NodeID DGriddDataStore::getMasterNode(const TableID& id_table)
{
    if(!id_table.empty()) {
        std::lock_guard<std::mutex> lock(table_reg_mutex);
        auto it = registered_tables.find(id_table);
        if(it != registered_tables.end())
            return it->second->master_node;
    }
    return NodeID();
}
 
void DGriddDataStore::setFallbackNodes(const TableID& id_table, const std::vector<NodeID>& nodes)
{
    std::lock_guard<std::mutex> lock(table_reg_mutex);
    auto it = registered_tables.find(id_table);
    if(it != registered_tables.end())
        it->second->fallback_nodes = nodes;
}
 
std::vector<NodeID> DGriddDataStore::getFallbackNodes(const TableID& id_table)
{
    std::lock_guard<std::mutex> lock(table_reg_mutex);
    auto it = registered_tables.find(id_table);
    if(it != registered_tables.end())
        return it->second->fallback_nodes;
    return std::vector<NodeID>();
}
 
const TableID DGriddDataStore::getTableID(const std::string& session, const std::string& space, const std::string& tname)
{
    std::lock_guard<std::mutex> lock(table_reg_mutex);
    return getTableID_int(session, space, tname);
}
 
bool DGriddDataStore::deregisterTable(const TableID& id)
{
    std::lock_guard<std::mutex> lock(table_reg_mutex);
    if(registered_tables.find(id) != registered_tables.end()) {
        registered_tables.erase(id);
        return true;
    }
    writeLog(LOG_ERR, "Can't deregister table id=%lu: not found", id);
    return false;
}
 
bool DGriddDataStore::deregisterTable(const std::string& session, const std::string& space, const std::string& tname)
{
    std::lock_guard<std::mutex> lock(table_reg_mutex);
    TableID id = getTableID_int(session, space, tname);
    if(!id.empty()) {
        registered_tables.erase(id);
        return true;
    }
    writeLog(LOG_ERR, "Can't deregister table '%s/%s/%s': not found", session.c_str(), space.c_str(), tname.c_str());
    return false;
}
 
const TableID DGriddDataStore::createTableID()
{
    TableID id;
    id.resize(16);
    uuid_generate(reinterpret_cast<unsigned char*>(id.data()));
    return id;
}
 
std::shared_ptr<TableDescription> DGriddDataStore::getTableDescription(const TableID& id)
{
    std::lock_guard<std::mutex> lock(table_reg_mutex);
    auto it = registered_tables.find(id);
    if(it != registered_tables.end())
        return it->second;
    return nullptr;
}
 
void DGriddDataStore::deregisterSessionTables_int(const std::string& session)
{
    for(auto it = registered_tables.begin(); it != registered_tables.end(); ) {
        if(session == it->second->session) {
            it = registered_tables.erase(it);
        } else {
            ++it;
        }
    }
}
 
const TableID DGriddDataStore::getTableID_int(const std::string& session, const std::string& space, const std::string& tname)
{
    for(auto it = registered_tables.begin(); it != registered_tables.end(); it++) {
        if(it->second->compare(session, space, tname)) 
            return it->first;
    }
    return TableID();
}
 
} // namespace DGrid