Исходный код СУБД 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "dgrid_logger.h"
#include "dgridd_datastore_table.h"
 
namespace DGrid {
 
#define HASHED_TABLE_SEED 0x136C936C
#define VACUUM_FACTOR 2
 
uint64_t TableRowKeyHasher::operator()(const TableRowKey& k) const {
    uint64_t hash(HASHED_TABLE_SEED);
    for(auto ki: k) {
        hash = ((hash << 1) | (hash >> 63)) ^ ki.val.size();
        uint8_t *c = (uint8_t*)ki.val.c_str();
        while(*c) {
            hash = ((hash << 1) | (hash >> 63)) ^ *c;
            c++;
        }
    }
    return hash;
}
 
TableData_Hashed::TableData_Hashed(const std::vector<std::string>& key_fields, const std::vector<std::string>& fields, const std::vector<uint64_t>& fields_len, const std::vector<FieldType>& fields_type)
    :TableData(key_fields.empty() ? fields : key_fields, fields, fields_len, fields_type)
{
 
}
 
TableData_Hashed::~TableData_Hashed() {}
TableType TableData_Hashed::type() { return TABLE_HASHED; }
 
HashedRows::iterator TableData_Hashed::sequentialScan(HashedRows::iterator begin, const std::vector<std::string>& keys, const std::vector<std::string>& search_values)
{
    SearchCondition condition;
    if(!conditionFromKV(keys, search_values, condition))
        return rows_pkey.end();
 
    for(auto it=begin; it!=rows_pkey.end(); it++) {
        if(getDataRow(it->second)->checkCondition(condition))
            return it;
    }
    return rows_pkey.end();
}
 
uint64_t TableData_Hashed::sequentialScan(const uint64_t begin, const std::vector<std::string>& keys, const std::vector<std::string>& search_values)
{
    SearchCondition condition;
    if(!conditionFromKV(keys, search_values, condition))
        return npos;
 
    uint64_t size = rows_data.size();
    for(auto it=begin; it < size; it++) {
        if(rows_data[it] != nullptr && rows_data[it]->checkCondition(condition))
            return it;
    }
    return npos;
}
 
HashedRows::iterator TableData_Hashed::sequentialScan(HashedRows::iterator begin, const ComplexSearchCondition__int& condition, uint64_t* index)
{
    writeLog(LOG_DEBUG, "sequentialScan...");
 
    for(auto it=begin; it!=rows_pkey.end(); it++) {
        if(index)
            (*index)++;
        if(getDataRow(it->second)->checkCondition(condition))
            return it;
    }
    return rows_pkey.end();
}
 
uint64_t TableData_Hashed::sequentialScan(const uint64_t begin, const ComplexSearchCondition__int& condition)
{
    writeLog(LOG_DEBUG, "sequentialScan...");
    uint64_t size = rows_data.size();
    for(auto it=begin; it < size; it++) {
        if(rows_data[it] != nullptr && rows_data[it]->checkCondition(condition))
            return it;
    }
    return npos;
}
 
std::shared_ptr<TableRowData> TableData_Hashed::getDataRow(const uint64_t idx)
{
    if(rows_data.size() > idx)
        return rows_data[idx];
    return nullptr;
}
 
bool TableData_Hashed::copyTo(std::shared_ptr<TableData>& other)
{
    other->clear();
    for(RowPointer& it: rows_data) {
        if(it != nullptr)
            other->appendDataRow(it);
    }
    return true;
}
 
bool TableData_Hashed::appendDataRow(const std::vector<std::string>& values, ModifyStatsData *stats)
{
    std::shared_ptr<TableRowData> row = std::make_shared<TableRowData>(this, values);
    TableRowKey key = row->getKey();
    if(rows_pkey.find(key) != rows_pkey.end()) {
        writeLog(LOG_WARNING, "%s: key already exists", __func__);
        return false;
    }
    uint64_t row_idx = rows_data.size();
    rows_pkey.emplace(key, row_idx);
    rows_data.push_back(row);
#ifdef HASH_TABLE_DEBUG
    TableRowKeyHasher hasher;
    hash_set.insert(hasher(key));
#endif
    registerRow(row, 0, row_idx);
    if(stats) {
#ifdef MODIFY_STATS_SHORT_RESULT
        if(stats->list.empty())
#endif
            stats->list.push_back(row->id_record);
    }
    return true;
}
 
bool TableData_Hashed::updateDataRowByID(const std::vector<std::string>& values, const RecordID id_record)
{
    RowPointer old_row = getDataRowByID(id_record);
    if(nullptr == old_row) {
        writeLog(LOG_DEBUG, "%s: invalid row id: %lu", __func__, id_record);
        return false;
    }
    std::shared_ptr<TableRowData> row = std::make_shared<TableRowData>(this, values);
    TableRowKey key = row->getKey();
    TableRowKey old_key = old_row->getKey();
    auto it = rows_pkey.find(key);
    if(old_key != key) {
        writeLog(LOG_INFO, "%s: key fields change not allowed", __func__);
        return false;
    }
    rows_data[it->second] = row;
    registerRow(row, old_row->id_record, it->second);
    return true;
}
 
bool TableData_Hashed::updateDataRow(const std::vector<std::string>& values, const uint64_t index)
{
    writeLog(LOG_ERR, "%s: Indexed access to hashed tables not allowed", __func__);
    return false;
}
 
bool TableData_Hashed::updateDataRow(const std::vector<std::string>& values, const std::vector<std::string>& old_key_values)
{
    std::shared_ptr<TableRowData> row = std::make_shared<TableRowData>(this, values);
    TableRowKey key = row->getKey();
    TableRowKey old_key = keyFromValues(old_key_values);
    if(rows_pkey.find(old_key) == rows_pkey.end()) {
        writeLog(LOG_DEBUG, "%s: no row found by key", __func__);
        return false;
    }
    auto it = rows_pkey.find(key);
    if(old_key != key) {
        writeLog(LOG_INFO, "%s: key fields change not allowed", __func__);
        return false;
    }
    RecordID id = rows_data[it->second]->id_record;
    rows_data[it->second] = row;
    registerRow(row, id, it->second);
    return true;
}
 
bool TableData_Hashed::insertDataRow(const std::vector<std::string>& values, const uint64_t index, ModifyStatsData *stats)
{
    writeLog(LOG_WARNING, "%s: Indexed access to hashed tables not allowed, ingoring index", __func__);
    return appendDataRow(values, stats);
}
 
bool TableData_Hashed::insertDataRows(const std::vector<std::vector<std::string> >& values, const uint64_t index, ModifyStatsData *stats)
{
    
    writeLog(LOG_ERR, "%s: Indexed access to hashed tables not allowed", __func__);
    return false;
}
 
bool TableData_Hashed::selectDataRowByID(const RecordID id_record, std::vector<std::string>& values, uint64_t& index)
{
    RowPointer row = getDataRowByID(id_record);
    if(nullptr == row)
        return false;
 
    row->getCellsData(values);
    return true;
}
 
bool TableData_Hashed::selectDataRow(const uint64_t index, std::vector<std::string>& values, RecordID& id_record)
{
    writeLog(LOG_ERR, "%s: Indexed access to hashed tables not allowed", __func__);
    return false;
}
 
bool TableData_Hashed::selectDataRow(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, uint16_t flags,
        std::vector<std::string>& values, RecordID& id_record, uint64_t& index)
{
    TableRowKey key;
    id_record = 0;
    index = 0;
    HashedRows::iterator it = rows_pkey.end();
    if(keyFromValues(keys, search_values, key)) {
        it = rows_pkey.find(key);
    } else {
        writeLog(LOG_DEBUG, "%s: key fields missmatch, performing sequential scan...", __func__);
        it = sequentialScan(rows_pkey.begin(), keys, search_values);
    }
    if(it == rows_pkey.end()) {
        writeLog(LOG_DEBUG, "%s: no row found by key", __func__);
        return false;
    }
    RowPointer row = rows_data[it->second];
    row->getCellsData(values);
    id_record = row->id_record;
    return true;
}
 
bool TableData_Hashed::selectNextDataRow(const ComplexSearchCondition__int& condition,
        const uint64_t index_from, const RecordID id_prev,
        std::vector<std::string>& values, RecordID& id_record, uint64_t& index)
{
    writeLog(LOG_ERR, "%s: Indexed access to hashed tables not allowed", __func__);
    return false;
}
 
bool TableData_Hashed::selectDataRows(std::vector<TableResponseRowData>& values)
{
    uint64_t count = rowsCount();
    values.resize(count);
    // HashedRows::iterator it = rows.begin();
    for(uint64_t i = 0, j = 0; i<count && j<rows_data.size(); i++, j++) {
        if(rows_data[j] == nullptr) {
            j++;
            continue;
        }
        // RowPointer& row = it->second;
        RowPointer& row = rows_data[j];
        row->getCellsData(values[i].data);
        values[i].id_record = row->id_record;
        values[i].index = 0;
    }
    return true;
}
 
bool TableData_Hashed::deleteDataRowByID(const RecordID id_record)
{
    RowPointer row = getDataRowByID(id_record);
    if(nullptr == row) {
        writeLog(LOG_DEBUG, "%s: invalid row id: %llu", __func__, id_record);
        return false;
    }
    TableRowKey key = row->getKey();
    auto it = rows_pkey.find(key);
    if(it == rows_pkey.end()) {
        writeLog(LOG_ERR, "%s: internal error, row %llu not found!", __func__);
        return false;
    }
    uint64_t idx = it->second;
    deregisterRow(row->id_record, 0);
    rows_pkey.erase(it);
    rows_data[idx] = nullptr;
    if(idx == rows_data.size() - 1)
        rows_data.resize(idx);
    return true;
}
 
bool TableData_Hashed::deleteDataRow(const uint64_t index)
{
    writeLog(LOG_ERR, "%s: Indexed access to hashed tables not allowed", __func__);
    return false;
}
 
bool TableData_Hashed::deleteDataRows(const uint64_t index, const uint64_t& count, uint64_t& real_count)
{
    writeLog(LOG_ERR, "%s: Indexed access to hashed tables not allowed", __func__);
    return false;
}
 
bool TableData_Hashed::deleteDataRows(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, uint64_t& real_count)
{
    TableRowKey key;
    real_count = 0;
    HashedRows::iterator it = rows_pkey.end();
    if(keyFromValues(keys, search_values, key)) {
        it = rows_pkey.find(key);
        if(it == rows_pkey.end()) {
            writeLog(LOG_DEBUG, "%s: no row found by key", __func__);
            return false;
        }
    } else {
        writeLog(LOG_DEBUG, "%s: key fields missmatch, performing sequential scan...", __func__);
        it = sequentialScan(rows_pkey.begin(), keys, search_values);
        if(it == rows_pkey.end()) {
            writeLog(LOG_DEBUG, "%s: no row found", __func__);
            return false;
        }
    }
    uint64_t idx = it->second;
    deregisterRow(rows_data[idx]->id_record, 0);
    rows_pkey.erase(it);
    rows_data[idx] = nullptr;
    if(idx == rows_data.size() - 1)
        rows_data.resize(idx);
    real_count = 1;
    return true;
}
 
bool TableData_Hashed::deleteAdjacentDuplicates(std::vector<uint16_t>& indexes, uint64_t& rows_deleted)
{
    rows_deleted = 0;
    auto it = rows_pkey.begin();
    auto it_next = it;
    it_next++;
    while(it_next != rows_pkey.end()) {
        RowPointer& row      = rows_data[it->second];
        RowPointer& row_next = rows_data[it_next->second];
        if(row->equal(row_next, indexes)) {
            deregisterRow(row_next->id_record, 0);
            rows_data[it_next->second] = nullptr;
            it_next = rows_pkey.erase(it_next);
            rows_deleted++;
        } else {
            it++;
            it_next = it;
            it_next++;
        }
    }
    return true;
}
 
void TableData_Hashed::clearData()
{
    rows_pkey.clear();
    rows_data.clear();
}
 
uint64_t TableData_Hashed::rowsCount()
{
#ifdef HASH_TABLE_DEBUG
    writeLog(LOG_INFO, "rows: %lld, uniq hashes: %lld", rows.size(), hash_set.size());
#endif
    return rows_pkey.size();
}
 
void TableData_Hashed::vacuum()
{
    if(rows_pkey.size() < rows_data.size() / VACUUM_FACTOR)
        return;
#warning TODO: VACUUM
}
 
// cursors
TableData_Hashed::TableCursor::TableCursor() :index(npos)
{}
TableData_Hashed::TableCursor::TableCursor(const ComplexSearchCondition__int&& cond) :index(npos), condition(std::move(cond))
{}
TableData_Hashed::TableCursor::~TableCursor() {}
 
CursorID TableData_Hashed::nextCursorID()
{
    while(cursors.find(last_cursor_id) != cursors.end())
        last_cursor_id++;
    return last_cursor_id++;
}
 
bool TableData_Hashed::cursorCreate(const ComplexSearchCondition__int&& cond, uint64_t& cursor_id, std::string& error_string)
{
    cursor_id = nextCursorID();
    cursors.emplace(cursor_id, TableCursor(std::move(cond)));
    return true;
}
 
bool TableData_Hashed::cursorCreate(const ComplexSearchCondition__int&& cond, const uint64_t index_from, const uint64_t index_to, uint64_t& cursor_id, std::string& error_string)
{
    error_string = "Indexed access to hashed tables not allowed";
    writeLog(LOG_ERR, "%s: %s", __func__, error_string.c_str());
    return false;
}
 
bool TableData_Hashed::cursorDelete(const uint64_t cursor_id, std::string& error_string)
{
    auto it = cursors.find(cursor_id);
    if(it == cursors.end())
        return false;
 
    cursors.erase(it);
    return true;
}
 
void TableData_Hashed::updateCursors(const uint64_t index, bool added)
{
    // writeLog(LOG_WARNING, "%s: Cursors not fully implemented for hashed tables", __func__);
}
 
void TableData_Hashed::resetCursors()
{
    for(auto& cursor: cursors) {
        cursor.second.index = npos;
    }
}
 
bool TableData_Hashed::selectNextDataRow_cursor(const uint64_t cursor_id, TableResponseRowData& values, std::string& error_string)
{
    auto cur = cursors.find(cursor_id);
    if(cur == cursors.end()) {
        error_string = "Invalid cursor ID";
        return false;
    }
    if(cur->second.index == npos) {
        cur->second.index = 0;
    }
    cur->second.index = sequentialScan(cur->second.index, cur->second.condition);
    if(cur->second.index != npos) {
        RowPointer& row = rows_data[cur->second.index];
        row->getCellsData(values.data);
        values.id_record = row->id_record;
        values.index = 0;
        cur->second.index++;
        return true;
    }
    return false;
}
 
} // namespace DGrid