Исходный код СУБД 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
#include <algorithm>
#include <experimental/random>
 
#include "dgrid_logger.h"
#include "dgridd_datastore_table.h"
 
namespace DGrid {
 
TableData_Sorted::TableData_Sorted(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, bool uniq_key)
    :TableData_Std(key_fields.empty() ? fields : key_fields, fields, fields_len, fields_type), uniq_key(uniq_key)
{
}
 
TableData_Sorted::~TableData_Sorted() {}
TableType TableData_Sorted::type() { return uniq_key ? TABLE_SORTED_UNIQ : TABLE_SORTED; }
 
bool TableData_Sorted::copyTo(std::shared_ptr<TableData>& other)
{
    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_Sorted::insertDataRow_internal(std::shared_ptr<TableRowData>& row, const uint64_t index, const RecordID old_id)
{
    if(index > rows_data.size()) {
        writeLog(LOG_ERR, "index > rows size (%d > %d)", index, rows_data.size());
        return false;
    }
    // writeLog(LOG_DEBUG, "insert row at %d, tot %d", index, rows_data.size());
    rows_data.resize(rows_data.size()+1);
 
    for(size_t i=rows_data.size()-1; i>index; i--) {
        rows_data[i] = rows_data[i-1];
    }
    rows_data[index] = row;
    registerRow(row, old_id, index);
    return true;
}
 
bool TableData_Sorted::appendDataRow(const std::vector<std::string>& values, ModifyStatsData *stats)
{
    RowPointer row = std::make_shared<TableRowData>(this, values);
    uint64_t index = bsearch_NextIndex(row->getKey());
    if(uniq_key && index>0 && row->getKey() == rows_data[index-1]->getKey()) {
        writeLog(LOG_INFO, "%s: key already exists", __func__);
        return false;
    }
    insertDataRow_internal(row, index);
    // writeLog(LOG_DEBUG, "row inseted at %llu, new size: %llu", index, rows.size());
    if(stats) {
        stats->last_index = index;
#ifdef MODIFY_STATS_SHORT_RESULT
        if(stats->list.empty())
#endif
            stats->list.push_back(row->id_record);
    }
    return true;
}
 
bool TableData_Sorted::updateDataRowByID(const std::vector<std::string>& values, const RecordID id_record)
{
#ifdef TABLE_DEBUG
    writeLog(LOG_DEBUG, "%s %d id %llu", __FILE__, __LINE__, id_record);
#endif
    RowPointer old_row = getDataRowByID(id_record);
    if(nullptr == old_row) {
        writeLog(LOG_ERR, "%s: ID %llu not found", __func__, id_record);
        return false;
    }
    RowPointer new_row = std::make_shared<TableRowData>(this, values);
    bool key_changed = old_row->getKey() != new_row->getKey();
 
    if(uniq_key && key_changed) {
        writeLog(LOG_ERR, "%s: sorted table UNIQUE key can't be changed via update operation", __func__);
        return false;
    }
    if(!key_changed) {
        writeLog(LOG_DEBUG, "key not changed, just move data > id %llu", id_record);
        old_row->moveCellsFrom(*new_row);
    } else {
        writeLog(LOG_DEBUG, "key changed, look for index for id %llu", id_record);
        uint64_t old_index = 0;
        if (!bsearch_RowIndex(old_row->getKey(), 0, old_index)) {
            writeLog(LOG_WARNING, "no start index found for old record");
            return false;
        }
        uint64_t old_index_high = bsearch_NextIndex(old_row->getKey(), old_index);
#ifdef TABLE_DEBUG
        writeLog(LOG_DEBUG, "old index interval: %llu - %llu", old_index, old_index_high - 1);
#endif
        while(id_record != rows_data[old_index]->id_record && old_index < old_index_high) {
            old_index++;
        }
        if(old_index == old_index_high) {
            writeLog(LOG_WARNING, "no actual index found for old record");
            return false;
        }
        uint64_t new_index = bsearch_NextIndex(new_row->getKey());
        if (old_index < new_index)
            new_index--;
        writeLog(LOG_DEBUG, "%s %d update %llu at index %llu -> %llu", __FILE__, __LINE__, id_record, old_index, new_index);
        deleteDataRow(old_index);
        insertDataRow_internal(new_row, new_index, id_record);
    }
    return true;
}
 
bool TableData_Sorted::updateDataRow(const std::vector<std::string>& values, const uint64_t index)
{
#ifdef TABLE_DEBUG
    writeLog(LOG_DEBUG, "%s %d index %llu", __FILE__, __LINE__, index);
#endif
    if(index >= rows_data.size()) {
        writeLog(LOG_ERR, "%s: Index %d out of range", __func__, index);
        return false;
    }
    RowPointer new_row = std::make_shared<TableRowData>(this, values);
    bool key_changed = rows_data[index]->getKey() != new_row->getKey();
 
    if(uniq_key && key_changed) {
        writeLog(LOG_INFO, "%s: sorted table UNIQUE key can't be changed via update operation", __func__);
        return false;
    }
    if(!key_changed) {
        writeLog(LOG_DEBUG, "key not changed, just move data [%llu]", index);
        rows_data[index]->moveCellsFrom(*new_row);
    } else {
        writeLog(LOG_DEBUG, "key changed, look for new index");
        uint64_t new_index = bsearch_NextIndex(new_row->getKey());
        RecordID id = rows_data[index]->id_record;
        if (index < new_index)
            new_index--;
        writeLog(LOG_DEBUG, "%s %d update at index %llu -> %llu (id = %llu)", __FILE__, __LINE__, index, new_index, id);
        deleteDataRow(index);
        insertDataRow_internal(new_row, new_index, id);
    }
    return true;
}
 
bool TableData_Sorted::updateDataRow(const std::vector<std::string>& values, const std::vector<std::string>& old_key_data)
{
#ifdef TABLE_DEBUG
    writeLog(LOG_DEBUG, "%s %d key", __FILE__, __LINE__);
#endif
    std::shared_ptr<TableRowData> new_row = std::make_shared<TableRowData>(this, values);
    TableRowKey new_key = new_row->getKey();
    TableRowKey old_key = keyFromValues(old_key_data, true);
    if(old_key.empty()) {
        writeLog(LOG_ERR, "%s: empty key created!", __func__);
        return false;
    }
 
    bool key_changed = old_key != new_row->getKey();
 
    if(uniq_key && key_changed) {
        writeLog(LOG_INFO, "%s: sorted table UNIQUE key can't be changed via update operation", __func__);
        return false;
    }
    uint64_t old_index = 0;
    if(!bsearch_RowIndex(old_key, 0 /* id_prev */, old_index)) {
        writeLog(LOG_DEBUG, "%s: no row found by key", __func__);
        return false;
    }
    if(!key_changed) {
        rows_data[old_index]->moveCellsFrom(*new_row);
    } else {
        uint64_t new_index = bsearch_NextIndex(new_row->getKey());
        RecordID id = rows_data[old_index]->id_record;
        if (old_index < new_index)
            new_index--;
        writeLog(LOG_DEBUG, "%s %d update at index %llu -> %llu", __FILE__, __LINE__, old_index, new_index);
        deleteDataRow(old_index);
        insertDataRow_internal(new_row, new_index, id);
    }
    return true;
}
 
bool TableData_Sorted::insertDataRow(const std::vector<std::string>& values, const uint64_t index, ModifyStatsData *stats)
{
    writeLog(LOG_WARNING, "%s: Indexed insert to sorted tables not allowed, ignoring index", __func__);
    return appendDataRow(values, stats);
}
 
bool TableData_Sorted::insertDataRows(const std::vector<std::vector<std::string> >& values, const uint64_t index, ModifyStatsData *stats)
{
    writeLog(LOG_WARNING, "%s: Indexed insert to sorted tables not allowed, ignoring index", __func__);
    for(auto row: values) {
        if (!appendDataRow(row, stats))
            return false;
    }
    return true;
}
 
bool TableData_Sorted::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_Sorted::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_Sorted::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();
    TableRowKey key;
    if(keyFromValues(keys, search_values, key, true)) {
        if(!bsearch_RowIndex(key, 0 /* id_prev */, index)) {
            writeLog(LOG_DEBUG, "%s: no row found by key (expected index = %llu)", __func__, index);
            // values.clear();
            // values.resize(columns_count, "");
            return false;
        }
        index_upper = bsearch_NextIndex(key, index);
    } else {
        writeLog(LOG_DEBUG, "%s: key fields missmatch, performing sequential scan...", __func__);
    }
    // compare all fields after key match
    if(!sequentialScan(keys, search_values, 0 /* id_prev */, index, index_upper)) {
        writeLog(LOG_DEBUG, "%s: no row found", __func__);
        return false;
    }
    rows_data[index]->getCellsData(values);
    id_record = rows_data[index]->id_record;
    return true;
}
 
bool TableData_Sorted::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;
    uint64_t index_upper = rows_data.size();
    TableRowKey key;
    writeLog(LOG_DEBUG, " look for next row from %llu, prev id %llu", index, id_prev);
    if(!condition.field_conditions.empty() && keyFromCondition(condition, key, true)) {
        if(!bsearch_RowIndex(key, id_prev, index)) {
            writeLog(LOG_DEBUG, "%s: no more rows found by key", __func__);
            return false;
        }
        index_upper = bsearch_NextIndex(key, index);
    } else {
        writeLog(LOG_DEBUG, "%s: key fields missmatch, performing sequential scan...", __func__);
    }
    // compare all fields after key match
    if(!sequentialScan(condition, id_prev, index, index_upper)) {
        writeLog(LOG_DEBUG, "%s: no more rows found", __func__);
        return false;
    }
    rows_data[index]->getCellsData(values);
    id_record = rows_data[index]->id_record;
    return true;
}
 
bool TableData_Sorted::selectDataRows(std::vector<TableResponseRowData>& values)
{
    return TableData_Std::selectDataRows(values);
}
 
bool TableData_Sorted::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_Sorted::deleteDataRow(const uint64_t index)
{
    uint64_t real_count;
    return deleteDataRows(index, 1, real_count);
}
 
bool TableData_Sorted::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++) {
        // writeLog(LOG_DEBUG, "[%d] id %llu", index+i, rows[index+i]->id_record);
        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_Sorted::deleteDataRows(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, uint64_t& real_count)
{
    uint64_t index=0;
    TableRowKey key;
    if(keyFromValues(keys, search_values, key)) {
        if(!bsearch_RowIndex(key, 0 /* id_prev */, index)) {
            writeLog(LOG_DEBUG, "%s: no row found by key", __func__);
            return false;
        }
    } else {
        writeLog(LOG_DEBUG, "%s: key fields missmatch, performing sequential scan...", __func__);
        // compare all fields after key match
        uint64_t index_upper = rows_data.size();
        if(!sequentialScan(keys, search_values, 0 /* id_prev */, index, index_upper)) {
            writeLog(LOG_DEBUG, "%s: no row found", __func__);
            return false;
        }
    }
    return deleteDataRows(index, 1, real_count);
}
 
bool TableData_Sorted::deleteAdjacentDuplicates(std::vector<uint16_t>& indexes, uint64_t& rows_deleted)
{
    return TableData_Std::deleteAdjacentDuplicates(indexes, rows_deleted);
}
 
void TableData_Sorted::clearData()
{
    rows_data.clear();
}
 
uint64_t TableData_Sorted::rowsCount()
{
    return rows_data.size();
}
 
} // namespace DGrid