Исходный код СУБД 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "dgrid_logger.h"
#include "dgridd_datastore_table.h"
 
namespace DGrid {
 
TableData_Std::TableData_Std(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, fields, fields_len, fields_type), last_cursor_id(1)
{
 
}
 
TableData_Std::~TableData_Std() {}
TableType TableData_Std::type() { return TABLE_STANDARD; }
 
std::shared_ptr<TableRowData> TableData_Std::getDataRow(const uint64_t index)
{
    if(index >= rows_data.size()) {
        writeLog(LOG_WARNING, "%s: index %llu out of range (max = %llu)", __func__, index, rows_data.size() - 1);
        return nullptr;
    }
    return rows_data[index];
}
 
uint64_t TableData_Std::rowsCount() { return rows_data.size(); }
 
bool TableData_Std::getIndexByID(const RecordID id, uint64_t& index)
{
#warning !!! TODO: needs optimization !!!
    uint64_t count = rowsCount();
    for(uint64_t i=0; i<count; i++) {
        if(id == rows_data[i]->id_record) {
            index = i;
            return true;
        }
    }
    return false;
}
 
bool TableData_Std::sort(const SortKey& key, const bool stable)
{
    if(stable)
        std::stable_sort(rows_data.begin(), rows_data.end(), [&key](const RowPointer& a, const RowPointer& b) {
#ifdef TABLE_DEBUG
            if( a->compare(*b, key) && b->compare(*a, key) ) {
                writeLog(LOG_ERR, "COMPARATOR ERROR!!!");
                return false;
            }
#endif
            return a->compare(*b, key);
        });
    else
        std::sort(rows_data.begin(), rows_data.end(), [&key](const RowPointer& a, const RowPointer& b) {
#ifdef TABLE_DEBUG
            if( a->compare(*b, key) && b->compare(*a, key) ) {
                writeLog(LOG_ERR, "COMPARATOR ERROR!!!");
                return false;
            }
#endif
            return a->compare(*b, key);
    } );
    return true;
}
 
bool TableData_Std::copyFrom(TableData *other)
{
    uint64_t count = other->rowsCount();
    uint64_t index;
    clear();
    rows_data.resize(count);
    std::vector<std::string> values;
    for(uint64_t i = 0; i<count; i++) {
        other->selectDataRow(i, values, index);
        RowPointer row = std::make_shared<TableRowData>(this, values, other);
        rows_data[i] = row;
        registerRow(row, 0, 0);
    }
    return true;
}
 
bool TableData_Std::copyTo(std::shared_ptr<TableData>& other)
{
    if(TableData_Std *other_std = dynamic_cast<TableData_Std*>(other.get()))
        return other_std->copyFrom(this);
    if(TableData_Sorted *other_sorted = dynamic_cast<TableData_Sorted*>(other.get()))
        return other_sorted->copyFrom(this);
 
    other->clear();
    uint64_t count = rows_data.size();
    for(uint64_t i = 0; i<count; i++) {
        other->appendDataRow(rows_data[i]);
    }
    return true;
}
 
bool TableData_Std::appendDataRow(const std::vector<std::string>& values, ModifyStatsData* stats)
{
    std::shared_ptr<TableRowData> row = std::make_shared<TableRowData>(this, values);
    rows_data.push_back(row);
    registerRow(row,0,rows_data.size()-1);
    if(stats) {
        stats->last_index = rows_data.size() - 1;
#ifdef MODIFY_STATS_SHORT_RESULT
        if(stats->list.empty())
#endif
            stats->list.push_back(row->id_record);
    }
//    writeLog(LOG_DEBUG, "append: new table size: %d", rows.size());
    return true;
}
 
bool TableData_Std::updateDataRow(const std::vector<std::string>& values, const uint64_t index)
{
    if(index >= rows_data.size())
        return false;
 
    for(uint64_t i=0; i<values.size() && i<columns_count; i++) {
        rows_data[index]->setCellData(i, values[i]);
    }
    return true;
}
 
bool TableData_Std::updateDataRow(const std::vector<std::string>& values, const std::vector<std::string>& key_values)
{
    uint64_t index = 0;
    SearchCondition condition;
    conditionFromV(key_values, condition);
    if(sequentialScan(condition, 0 /* id_prev */, index)) {
        return updateDataRow(values, index);
    }
    return false;
}
 
bool TableData_Std::updateDataRowByID(const std::vector<std::string>& values, const RecordID id_record)
{
    RowPointer row = getDataRowByID(id_record);  
    if(nullptr == row) {
        writeLog(LOG_ERR, "%s: ID %llu not found", __func__, id_record);
        return false;
    }
    row->setCellsData(values);
    return true;
}
 
bool TableData_Std::insertDataRow(const std::vector<std::string>& values, const uint64_t index, ModifyStatsData *stats)
{
    std::vector<std::vector<std::string> > v = { values };
    return insertDataRows(v, index, stats);
}
 
bool TableData_Std::insertDataRows(const std::vector<std::vector<std::string> >& values, const uint64_t index, ModifyStatsData *stats)
{
    if(index > rows_data.size())
        return false;
 
    uint64_t count = values.size();
    uint64_t last_index = 0;
    rows_data.resize(rows_data.size()+count);
    uint64_t move_start = index + count;
    for(size_t i=rows_data.size()-1; i >= move_start; i--) {
        // writeLog(LOG_DEBUG, "move %d -> %d", i-count, i);
        rows_data[i] = rows_data[i-count];
    }
    for(size_t i=0; i<count; i++) {
        last_index = index + i;
        std::shared_ptr<TableRowData> row = std::make_shared<TableRowData>(this, values[i]);
        rows_data[last_index] = row;
        registerRow(row, 0, last_index);
        if(stats) {
#ifdef MODIFY_STATS_SHORT_RESULT
            if(!i)
#endif
                stats->list.push_back(row->id_record);
        }
    }
//    writeLog(LOG_DEBUG, "insert: new table size: %d", rows.size());
    if(stats) {
        stats->count = count;
        stats->last_index = last_index;
    }
    return true;
}
 
bool TableData_Std::selectDataRow(const uint64_t index, std::vector<std::string>& values, RecordID& id_record)
{
    if(index >= rows_data.size())
        return false;
    rows_data[index]->getCellsData(values);
    id_record = rows_data[index]->id_record;
    return true;
}
 
bool TableData_Std::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_Std::sequentialScan(const std::vector<std::string>& keys, const std::vector<std::string>& search_values,
        const RecordID id_prev, uint64_t& index, uint64_t index_upper)
{
#ifdef TABLE_DEBUG
    writeLog(LOG_DEBUG, "sequentialScan: ");
    std::string sl;
    for(auto s: keys) { sl += " " + s; }
    writeLog(LOG_DEBUG, "    keys:  %s", sl.c_str());
    sl.clear();
    for(auto s: search_values) { sl += " " + s; }
    writeLog(LOG_DEBUG, "    values: %s", sl.c_str());
#endif
    SearchCondition condition;
    conditionFromKV(keys, search_values, condition);
    return sequentialScan(condition, id_prev, index, index_upper);
}
 
bool TableData_Std::sequentialScan(const SearchCondition& condition,
        const RecordID id_prev, uint64_t& index, uint64_t index_upper)
{
    if(index_upper>rows_data.size())
        index_upper = rows_data.size();
 
    writeLog(LOG_DEBUG, "comparing from row %llu...", index);
    for(; index<index_upper; index++) {
        if(id_prev == rows_data[index]->id_record)
            continue;
        if(rows_data[index]->checkCondition(condition))
            return true;
    }
    return false;
}
 
bool TableData_Std::sequentialScan(const ComplexSearchCondition__int& condition,
        const RecordID id_prev, uint64_t& index, uint64_t index_upper)
{
    writeLog(LOG_DEBUG, "comparing from row %llu...", index);
    if(index_upper>rows_data.size())
        index_upper = rows_data.size();
 
    for(; index<index_upper; index++) {
        if(id_prev == rows_data[index]->id_record)
            continue;
        if(rows_data[index]->checkCondition(condition))
            return true;
    }
    return false;
}
 
bool TableData_Std::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)
{
    uint64_t index_upper = rows_data.size();
 
    if(flags & FL_READ_TABLE_BINARY_SEARCH) {
        writeLog(LOG_WARNING, "Using binary search in standard table: %d key field(s)", keys.size());
        TableRowFreeKey key;
        if(freeKeyFromValues(keys, search_values, key)) {
            if(bsearch_RowIndex(key, 0 /* id_prev */, index)) {
                return selectDataRow(index, values, id_record);
            }
            writeLog(LOG_DEBUG, "%s: no row found by binary search (expected index = %llu)", __func__, index);
            // values.clear();
            // values.resize(columns_count, "");
        }
    } else {
        if(sequentialScan(keys, search_values, 0 /* id_prev */, index, index_upper)) {
            return selectDataRow(index, values, id_record);
        }
    }
    return false;
}
 
bool TableData_Std::selectNextDataRow(const ComplexSearchCondition__int& condition,
        const RecordID id_prev, const uint64_t index_from,
        std::vector<std::string>& values, RecordID& id_record, uint64_t& index)
{
    index = index_from;
    writeLog(LOG_DEBUG, " look for next row from %llu, prev id %llu", index, id_prev);
    if(sequentialScan(condition, id_prev, index)) {
        return selectDataRow(index, values, id_record);
    }
    return false;
}
 
bool TableData_Std::selectDataRows(std::vector<TableResponseRowData>& values)
{
    uint64_t count = rowsCount();
    values.resize(count);
    for(uint64_t i = 0; i<count; i++) {
        selectDataRow(i, values[i].data, values[i].id_record);
        values[i].index = i + 1;
    }
    return true;
}
 
bool TableData_Std::deleteDataRowByID(const RecordID id_record)
{
    uint64_t index;
    if(getIndexByID(id_record, index) && deleteDataRow(index)) {
        deregisterRow(id_record, index);
        return true;
    }
    return false;
}
 
bool TableData_Std::deleteDataRow(const uint64_t index)
{
    uint64_t real_count;
    return deleteDataRows(index, 1, real_count);
}
 
bool TableData_Std::deleteDataRows(const uint64_t index, const uint64_t& count, uint64_t& real_count)
{
    real_count = 0;
    if(index >= rows_data.size())
        return false;
 
    real_count = std::min(count, rows_data.size() - index);
    for(size_t i=0; i<real_count; i++) {
        deregisterRow(rows_data[index+i]->id_record, index);
    }
    for(size_t i=index; i<rows_data.size()-real_count; i++) {
        rows_data[i] = rows_data[i+real_count];
    }
    rows_data.resize(rows_data.size()-real_count);
    return true;
}
 
bool TableData_Std::deleteDataRows(const std::vector<std::string>& keys, const std::vector<std::string>& values, uint64_t& real_count)
{
    real_count = 0;
    SearchCondition condition;
    conditionFromKV(keys, values, condition);
 
    for(auto it = rows_data.begin(); it != rows_data.end(); ) {
        if((*it)->checkCondition(condition)) {
            deregisterRow((*it)->id_record, it - rows_data.begin());
            real_count++;
            it = rows_data.erase(it);
        } else {
            ++it;
        }
    }
    return true;
}
 
bool TableData_Std::deleteAdjacentDuplicates(std::vector<uint16_t>& indexes, uint64_t& rows_deleted)
{
    rows_deleted = 0;
    uint64_t i=0;
    while(i+1 < rows_data.size()) {
        if(rows_data[i]->equal(*rows_data[i+1], indexes)) {
            deleteDataRow(i+1);
            rows_deleted++;
        } else {
            i++;
        }
    }
    return true;
}
 
void TableData_Std::clearData() {
    rows_data.clear();
}
 
// cursors
TableData_Std::TableCursor::TableCursor()
    :index(0),
    index_from(0), index_to(npos)
{}
 
TableData_Std::TableCursor::TableCursor(const TableCursor&& o)
    :index(o.index),
    index_from(o.index_from), index_to(o.index_to),
    condition(std::move(o.condition))
{}
 
TableData_Std::TableCursor::TableCursor(const ComplexSearchCondition__int&& cond)
    :index(0),
    index_from(0), index_to(npos),
    condition(std::move(cond))
{}
TableData_Std::TableCursor::TableCursor(const ComplexSearchCondition__int&& cond, const uint64_t _from, const uint64_t _to)
    :index(_from),
    index_from(_from), index_to(_to ? _to : npos),
    condition(std::move(cond))
{
    writeLog(LOG_DEBUG, "range cursor: %llu - %llu", index_from, index_to);
}
 
TableData_Std::TableCursor::~TableCursor() {}
 
CursorID TableData_Std::nextCursorID()
{
    while(cursors.find(last_cursor_id) != cursors.end())
        last_cursor_id++;
    return last_cursor_id++;
}
 
bool TableData_Std::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_Std::cursorCreate(const ComplexSearchCondition__int&& cond, const uint64_t index_from, const uint64_t index_to, uint64_t& cursor_id, std::string& error_string)
{
    cursor_id = nextCursorID();
    cursors.emplace(cursor_id, TableCursor(std::move(cond), index_from, index_to));
    return true;
}
 
bool TableData_Std::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_Std::updateCursors(const uint64_t index, bool added)
{
    for(auto& cursor: cursors) {
        if(added) {
            if(cursor.second.index > index)
                cursor.second.index++;
        } else {
            if(cursor.second.index > index)
                cursor.second.index--;
        }
    }
}
 
void TableData_Std::resetCursors()
{
    for(auto& cursor: cursors)
        cursor.second.index = cursor.second.index_from;
}
 
bool TableData_Std::selectNextDataRow_cursor(const uint64_t cursor_id, TableResponseRowData& values, std::string& error_string)
{
    auto it = cursors.find(cursor_id);
    if(it == cursors.end()) {
        error_string = "Invalid cursor ID";
        return false;
    }
    if(it->second.index > it->second.index_to) {
        writeLog(LOG_DEBUG, "range cursor [%llu - %llu] limit [%llu]", it->second.index_from, it->second.index_to, it->second.index);
        return false;
    }
    bool found = selectNextDataRow(it->second.condition, 0, it->second.index, values.data, values.id_record, values.index);
    it->second.index = values.index+1;
    return found;
}
 
} // namespace DGrid