Исходный код СУБД 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#ifndef DGRIDD_DATASTORE_TABLE_H
#define DGRIDD_DATASTORE_TABLE_H
 
#include <string>
#include <map>
#include <vector>
 
#include <memory>
#include <mutex>
#include "dgrid.h"
#include "dgrid_req_structs.h"
#include "dgridd_datastore_table_index.h"
 
// #define TABLE_DEBUG
//#define HASH_TABLE_DEBUG
#define MODIFY_STATS_SHORT_RESULT
 
namespace DGrid {
 
class TableData;
class DGriddNodeArbiter;
 
class TableDescription {
public:
    TableDescription(const std::string& session, const std::string& space, const std::string& tname, const NodeID& master_node);
    ~TableDescription();
    TableDescription& operator=(const TableDescription& o);
    bool compare(const std::string& session, const std::string& space, const std::string& tname);
 
    std::string session;
    std::string space;
    std::string tname;
    NodeID master_node;
    std::vector<NodeID> fallback_nodes;
};
 
class TableFieldParams {
public:
    TableFieldParams();
    ~TableFieldParams();
 
    uint64_t max_length;
    FieldType data_type;
    SortOrder sort_order;
    std::string name;
};
 
class TableCellData {
public:
    TableCellData() = delete;
    TableCellData(const FieldType data_type);
    TableCellData(const FieldType data_type, const std::string& s);
    ~TableCellData();
 
    void set_type(const FieldType data_type);
    void from_string(const std::string& s);
    const std::string& to_string() const;
    bool operator==(const TableCellData& o) const;
    bool operator!=(const TableCellData& o) const;
    inline bool equal(const std::string& s) const { return compare(s, COMPARE_EQ, true); };
    bool compare(const TableCellData& s, const ComparisionType op) const;
    bool compare(const std::string& s, const ComparisionType op, bool s_is_plain) const;
    static bool compare(const std::string& str /*field data*/, const std::string& s /*condition data*/, const ComparisionType op, const FieldType type);
 
private:
    FieldType   type;
    std::string str;
};
 
typedef std::map<int16_t, std::string> SearchCondition;
 
struct SortKeyItem {
    int16_t     field_idx;
    uint16_t    flags;
    FieldType   data_type;
};
typedef std::vector<SortKeyItem> SortKey;
 
class TableRowData {
    friend class TableData;
 
public:
    TableRowData(TableData*);
    TableRowData(TableData*, const std::vector<std::string>& fields, TableData* src_table = nullptr);
    ~TableRowData();
    uint32_t columnsCount() const;
    void setCellsData(const std::vector<std::string>& fields);
    inline void moveCellsFrom(TableRowData& o) { cells = std::move(o.cells); key_valid = false; };
    inline void setCellData(const uint16_t i, const std::string& s) { if(i<cells.size()) cells[i].from_string(s); key_valid = false; };
    bool checkCondition(const SearchCondition& condition);
    bool checkCondition(const ComplexSearchCondition__int& condition);
    bool equal(const std::shared_ptr<TableRowData> other, const std::vector<uint16_t>& findexes);
    bool equal(const TableRowData& other, const std::vector<uint16_t>& findexes);
    TableRowKey& getKey();
    TableRowKey getKey(const std::vector<std::string>& components);
    std::vector<std::string> getCellsData();
    inline const std::string getCellData(int16_t i) const { return (i>=0 && (uint16_t)i<cells.size()) ? cells[i].to_string() : ""; };
    void getCellsData(std::vector<std::string>&);
    void dump();
    bool compare(const TableRowData& o, const SortKey& key) const;
 
    RecordID id_record;
 
private:
    std::vector<TableCellData> cells;
    TableData*  table;
    TableRowKey key;
    bool key_valid;
};
ISerializer& operator>>(ISerializer& ds, TableRowData& o);
OSerializer& operator<<(OSerializer& ds, const TableRowData& o);
 
/*
    Base table classes
*/
 
typedef std::pair<uint16_t, TableRowKeyItem> TableRowFreeKeyItem;
typedef std::vector<TableRowFreeKeyItem> TableRowFreeKey;
typedef std::shared_ptr<TableRowData> RowPointer;
typedef std::vector<RowPointer> RowsVector;
typedef std::unordered_map<RecordID, RowPointer> RecordIDmap;
 
bool operator>(const TableRowKey& a, const TableRowKey& b);
bool operator==(const TableRowKey& a, const TableRowKey& b);
 
inline bool operator>(const TableRowKey& a, const RowPointer& b)  { return a > b->getKey(); };
inline bool operator==(const TableRowKey& a, const RowPointer& b) { return a == b->getKey(); };
inline bool operator>(const RowPointer& a, const TableRowKey& b)  { return a->getKey() > b; };
inline bool operator==(const RowPointer& a, const TableRowKey& b) { return a->getKey() == b; };
 
bool operator>(const TableRowFreeKey& a, const RowPointer& b);
bool operator==(const TableRowFreeKey& a, const RowPointer& b);
 
#define BSEARCH_TEMPLATE
 
class TableData
{
    friend class TableRowData;
    friend class DataIndex_sorted;
    friend class DataIndex_hashed;
 
public:
    static const uint64_t npos = __UINT_LEAST64_MAX__;
 
    TableData(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);
    virtual ~TableData();
    inline uint32_t columnsCount() const { return columns_count; };
    int16_t columnIndex(const std::string& fname) const;
    std::vector<int16_t> keyColumns() const;
    std::vector<int16_t> keyColumns(const std::vector<std::string>& components) const;
    RowPointer getHeaderLine();
    virtual TableType type();
    const std::vector<std::string> getKeyFields() const;
    const std::vector<std::string> getFields() const;
    const std::vector<uint64_t> getFieldsLen() const;
    const std::vector<FieldType> getFieldsType() const;
    const FieldType getFieldType(const int16_t idx) const;
 
    virtual void setSecondaryKeys(const std::vector<TableKeyInfo>& keys);
    const std::vector<TableKeyInfo> getSecondaryKeys() const;
 
    bool resetSecondaryIndex(const std::string& name = "");
    bool resetSecondaryIndex(const std::string& name, std::string& error_string);
    bool rebuildSecondaryIndex(const std::string& name, std::string& error_string);
    DataIndex* findSecondaryIndex(const std::string& name);
    DataIndex* findSecondaryIndex(const std::vector<std::string>& components);
 
    virtual bool sort(const SortKey& sort_key, const bool stable) { return false; };
    virtual RowPointer getDataRow(const uint64_t idx) = 0;
    virtual TableRowData* getDataRow_raw(const uint64_t idx) const;
    virtual bool copyTo(std::shared_ptr<TableData>& other) = 0;
    bool appendDataRow(const RowPointer, ModifyStatsData *stats = nullptr);
    virtual bool appendDataRow(const std::vector<std::string>& values, ModifyStatsData *stats) = 0;
    virtual bool updateDataRowByID(const std::vector<std::string>& values, const RecordID id_record) = 0;
    virtual bool updateDataRow(const std::vector<std::string>& values, const uint64_t index) = 0;
    virtual bool updateDataRow(const std::vector<std::string>& values, const std::vector<std::string>& key_values) = 0;
    virtual bool insertDataRow(const std::vector<std::string>& values, const uint64_t index, ModifyStatsData *stats) = 0;
    virtual bool insertDataRows(const std::vector<std::vector<std::string> >& values, const uint64_t index, ModifyStatsData *stats) = 0;
    virtual bool selectDataRowByID(const RecordID id_record, std::vector<std::string>& values, uint64_t& index) = 0;
    virtual bool selectDataRow(const uint64_t index, std::vector<std::string>& values, RecordID& id_record) = 0;
    virtual bool 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) = 0;
    virtual bool 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) = 0;
    virtual bool selectDataRows(std::vector<TableResponseRowData>& values) = 0;
    virtual bool deleteDataRowByID(const RecordID id_record) = 0;
    virtual bool deleteDataRow(const uint64_t index) = 0;
    virtual bool deleteDataRows(const uint64_t index, const uint64_t& count, uint64_t& real_count) = 0;
    virtual bool deleteDataRows(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, uint64_t& real_count) = 0;
    virtual bool deleteAdjacentDuplicates(std::vector<uint16_t>& indexes, uint64_t& rows_deleted) = 0;
    virtual void clear();
    virtual uint64_t rowsCount() = 0;
 
    static void convertRowStructure(const TableData* const src_table, const std::vector<std::string>& src_values,
                                    const TableData* const dst_table, std::vector<std::string>& dst_values);
    void pack_row(const std::vector<std::string>& src_values, std::string& packed_row) const;
    void unpack_row(const std::string& packed_row, std::vector<std::string>& dst_values) const;
 
    virtual bool cursorCreate(const ComplexSearchCondition__int&& cond, uint64_t& cursor_id, std::string& error_string) = 0;
    virtual bool cursorCreate(const ComplexSearchCondition__int&& cond, const uint64_t index_from, const uint64_t index_to, uint64_t& cursor_id, std::string& error_string) = 0;
    virtual bool cursorDelete(const uint64_t cursor_id, std::string& error_string) = 0;
    virtual bool selectNextDataRow_cursor(const uint64_t cursor_id, TableResponseRowData& values, std::string& error_string) = 0;
 
protected:
    virtual void updateCursors(const uint64_t index, bool added) = 0;
    virtual void resetCursors() = 0;
 
    const RecordID registerRow(RowPointer row, const RecordID id, const uint64_t index);
    bool deregisterRow(const RecordID id, const uint64_t index);
    RowPointer getDataRowByID(const RecordID id);
    void clearRegisteredRows();
    virtual void clearData() = 0;
 
    bool conditionFromKV(const std::vector<std::string>& keys, const std::vector<std::string>& values, 
        SearchCondition& condition);
    bool conditionFromV(const std::vector<std::string>& key_values,
        SearchCondition& condition);
    TableRowKey keyFromValues(const std::vector<std::string>& values, bool may_be_partial = false);
    bool keyFromValues(const std::vector<std::string>& values, TableRowKey& key, bool may_be_partial = false);
    bool keyFromValues(const std::vector<std::string>& keys, const std::vector<std::string>& values,
        TableRowKey& key, bool may_be_partial = false, bool force_components = false);
    bool freeKeyFromValues(const std::vector<std::string>& keys, const std::vector<std::string>& values,
        TableRowFreeKey& key);
    bool keyFromCondition(const ComplexSearchCondition__int& condition, TableRowKey& key, bool may_be_partial = false);
 
    uint32_t columns_count;
    std::vector<std::string> key_fields;
    std::vector<TableFieldParams> fields;
    RowPointer header_line;
    std::vector<TableKeyInfo> sec_keys;
    std::map<std::string, std::shared_ptr<DataIndex> > sec_index_map;
    std::map<std::string, std::shared_ptr<DataIndex> > sec_index_map_alias;
 
protected:
    RowsVector rows_data;
 
private:
    virtual RecordID nextRecordID();
    RecordID last_record_id;
    RecordIDmap rows_id_map;
};
 
using TableDataPtr = std::shared_ptr<TableData>;
 
/*
    Standard table
*/
class TableData_Std: public TableData
{
    class TableCursor
    {
    public:
        TableCursor();
        TableCursor(const TableCursor& o) = delete;
        TableCursor(const TableCursor&& o);
        TableCursor(const ComplexSearchCondition__int&&);
        TableCursor(const ComplexSearchCondition__int&&, const uint64_t index_from, const uint64_t index_to);
        virtual ~TableCursor();
 
        uint64_t index;
        uint64_t index_from;
        uint64_t index_to;
        ComplexSearchCondition__int condition;
    };
 
public:
    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);
    virtual ~TableData_Std();
    TableType type() override;
 
    bool sort(const SortKey& sort_key, const bool stable) override;
    RowPointer getDataRow(const uint64_t idx) override;
    bool copyTo(std::shared_ptr<TableData>& other) override;
    bool appendDataRow(const std::vector<std::string>& values, ModifyStatsData *stats) override;
    bool updateDataRowByID(const std::vector<std::string>& values, const RecordID id_record) override;
    bool updateDataRow(const std::vector<std::string>& values, const uint64_t index) override;
    bool updateDataRow(const std::vector<std::string>& values, const std::vector<std::string>& key_values) override;
    bool insertDataRow(const std::vector<std::string>& values, const uint64_t index, ModifyStatsData *stats) override;
    bool insertDataRows(const std::vector<std::vector<std::string> >& values, const uint64_t index, ModifyStatsData *stats) override;
    bool selectDataRowByID(const RecordID id_record, std::vector<std::string>& values, uint64_t& index) override;
    bool selectDataRow(const uint64_t index, std::vector<std::string>& values, RecordID& id_record) override;
    bool selectDataRow(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, const uint16_t flags,
                                std::vector<std::string>& values, RecordID& id_record, uint64_t& index) override;
    bool 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) override;
    bool selectDataRows(std::vector<TableResponseRowData>& values) override;
    bool deleteDataRowByID(const RecordID id_record) override;
    bool deleteDataRow(const uint64_t index) override;
    bool deleteDataRows(const uint64_t index, const uint64_t& count, uint64_t& real_count) override;
    bool deleteDataRows(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, uint64_t& real_count);
    bool deleteAdjacentDuplicates(std::vector<uint16_t>& indexes, uint64_t& rows_deleted) override;
    void clearData() override;
    uint64_t rowsCount() override;
 
    bool copyFrom(TableData *other); // fast copy from STD/SORTED
 
    virtual bool cursorCreate(const ComplexSearchCondition__int&& cond, uint64_t& cursor_id, std::string& error_string) override;
    virtual bool cursorCreate(const ComplexSearchCondition__int&& cond, const uint64_t index_from, const uint64_t index_to, uint64_t& cursor_id, std::string& error_string) override;
    virtual bool cursorDelete(const uint64_t cursor_id, std::string& error_string) override;
    virtual bool selectNextDataRow_cursor(const uint64_t cursor_id, TableResponseRowData& values, std::string& error_string) override;
 
protected:
    virtual void updateCursors(const uint64_t index, bool added) override;
    virtual void resetCursors() override;
 
    std::unordered_map<CursorID, TableCursor> cursors;
    CursorID nextCursorID();
    CursorID last_cursor_id = 1;
 
    bool getIndexByID(const RecordID id, uint64_t& index);
    bool sequentialScan(const ComplexSearchCondition__int& condition, const RecordID id_prev, uint64_t& index, uint64_t index_upper = npos);
    bool sequentialScan(const SearchCondition& condition, const RecordID id_prev, uint64_t& index, uint64_t index_upper = npos);
    bool 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 = npos);
 
    template<class KEY>
    uint64_t bsearch_NextIndex(const KEY& key, const uint64_t index_from = 0) {
        if(index_from>=rows_data.size())
            return rows_data.size();
 
        int64_t l(index_from);
        int64_t count = rows_data.size() - l;
        int64_t step;
        while(count > 0) {
            step = count / 2;
            int64_t mid = l + step;
#ifdef TABLE_DEBUG
            writeLog(LOG_DEBUG, "n comparing %lld", mid);
#endif
            if(!(rows_data[mid] > key)) {
                l = mid + 1;
                count -= step + 1;
            } else
                count = step;
        }
        return l;
    }
 
    template<class KEY>
    bool bsearch_RowIndex(const KEY& key, const RecordID id_prev, uint64_t& index) {
        if(index>=rows_data.size())
            return false;
 
        uint64_t l((id_prev == rows_data[index]->id_record) ? index+1 : index);
        uint64_t count = rows_data.size() - l;
        uint64_t step;
        while(count > 0) {
            step = count / 2;
            uint64_t mid = l + step;
#ifdef TABLE_DEBUG
            writeLog(LOG_DEBUG, "r comparing %lld", mid);
#endif
            if(key > rows_data[mid]) {
                l = mid + 1;
                count -= step + 1;
            } else
                count = step;
        }
        index = l;
        return (l<rows_data.size() && key == rows_data[l]);
    }
};
 
/*
    Hashed table
*/
 
class TableData_Hashed: public TableData
{
    class TableCursor
    {
    public:
        TableCursor();
        TableCursor(const ComplexSearchCondition__int&&);
        virtual ~TableCursor();
 
        uint64_t index;
        ComplexSearchCondition__int condition;
        // HashedRows::iterator it;
    };
 
public:
    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);
    virtual ~TableData_Hashed();
    TableType type() override;
 
    RowPointer getDataRow(const uint64_t idx) override;
    bool copyTo(std::shared_ptr<TableData>& other) override;
    bool appendDataRow(const std::vector<std::string>& values, ModifyStatsData *stats) override;
    bool updateDataRowByID(const std::vector<std::string>& values, const RecordID id_record) override;
    bool updateDataRow(const std::vector<std::string>& values, const uint64_t index) override;
    bool updateDataRow(const std::vector<std::string>& values, const std::vector<std::string>& old_key) override;
    bool insertDataRow(const std::vector<std::string>& values, const uint64_t index, ModifyStatsData *stats) override;
    bool insertDataRows(const std::vector<std::vector<std::string> >& values, const uint64_t index, ModifyStatsData *stats) override;
    bool selectDataRowByID(const RecordID id_record, std::vector<std::string>& values, uint64_t& index) override;
    bool selectDataRow(const uint64_t index, std::vector<std::string>& values, RecordID& id_record) override;
    bool selectDataRow(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, const uint16_t flags,
                                std::vector<std::string>& values, RecordID& id_record, uint64_t& index) override;
    bool 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) override;
    bool selectDataRows(std::vector<TableResponseRowData>& values) override;
    bool deleteDataRowByID(const RecordID id_record) override;
    bool deleteDataRow(const uint64_t index) override;
    bool deleteDataRows(const uint64_t index, const uint64_t& count, uint64_t& real_count) override;
    bool deleteDataRows(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, uint64_t& real_count) override;
    bool deleteAdjacentDuplicates(std::vector<uint16_t>& indexes, uint64_t& rows_deleted) override;
    void clearData() override;
    uint64_t rowsCount() override;
 
    virtual bool cursorCreate(const ComplexSearchCondition__int&& cond, uint64_t& cursor_id, std::string& error_string) override;
    virtual bool cursorCreate(const ComplexSearchCondition__int&& cond, const uint64_t index_from, const uint64_t index_to, uint64_t& cursor_id, std::string& error_string) override;
    virtual bool cursorDelete(const uint64_t cursor_id, std::string& error_string) override;
    virtual bool selectNextDataRow_cursor(const uint64_t cursor_id, TableResponseRowData& values, std::string& error_string) override;
 
protected:
    virtual void updateCursors(const uint64_t index, bool added) override;
    virtual void resetCursors() override;
 
    std::unordered_map<CursorID, TableCursor> cursors;
    CursorID nextCursorID();
    CursorID last_cursor_id = 1;
 
private:
    HashedRows::iterator sequentialScan(HashedRows::iterator begin, const std::vector<std::string>& keys, const std::vector<std::string>& search_values);
    HashedRows::iterator sequentialScan(HashedRows::iterator begin, const ComplexSearchCondition__int& condition, uint64_t* index);
    uint64_t sequentialScan(const uint64_t begin, const std::vector<std::string>& keys, const std::vector<std::string>& search_values);
    uint64_t sequentialScan(const uint64_t begin, const ComplexSearchCondition__int& condition);
    void vacuum();
 
    HashedRows rows_pkey;
 
#ifdef HASH_TABLE_DEBUG
    std::set<uint64_t> hash_set;
#endif
};
 
/*
    Sorted table
*/
 
class TableData_Sorted: public TableData_Std
{
public:
    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);
    virtual ~TableData_Sorted();
    TableType type() override;
 
    bool copyTo(std::shared_ptr<TableData>& other) override;
    bool appendDataRow(const std::vector<std::string>& values, ModifyStatsData *stats) override;
    bool updateDataRowByID(const std::vector<std::string>& values, const RecordID id_record) override;
    bool updateDataRow(const std::vector<std::string>& values, const uint64_t index) override;
    bool updateDataRow(const std::vector<std::string>& values, const std::vector<std::string>& old_key) override;
    bool insertDataRow(const std::vector<std::string>& values, const uint64_t index, ModifyStatsData *stats) override;
    bool insertDataRows(const std::vector<std::vector<std::string> >& values, const uint64_t index, ModifyStatsData *stats) override;
    bool selectDataRowByID(const RecordID id_record, std::vector<std::string>& values, uint64_t& index) override;
    bool selectDataRow(const uint64_t index, std::vector<std::string>& values, RecordID& id_record) override;
    bool selectDataRow(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, const uint16_t flags,
                                std::vector<std::string>& values, RecordID& id_record, uint64_t& index) override;
    bool 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) override;
    bool selectDataRows(std::vector<TableResponseRowData>& values) override;
    bool deleteDataRowByID(const RecordID id_record) override;
    bool deleteDataRow(const uint64_t index) override;
    bool deleteDataRows(const uint64_t index, const uint64_t& count, uint64_t& real_count) override;
    bool deleteDataRows(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, uint64_t& real_count) override;
    bool deleteAdjacentDuplicates(std::vector<uint16_t>& indexes, uint64_t& rows_deleted) override;
    void clearData() override;
    uint64_t rowsCount() override;
 
private:
    bool insertDataRow_internal(RowPointer& row, const uint64_t index, const RecordID old_id = 0);
    // void ensure_sorted();
 
    bool uniq_key;
};
 
/*
    Table store
*/
 
class DGriddDataStoreTable
{
public:
    DGriddDataStoreTable(std::string session = "", DGriddNodeArbiter* a = nullptr);
    virtual ~DGriddDataStoreTable();
    void setNodeArbiter(DGriddNodeArbiter*);
    void showStats();
 
/*
client-controlled methods
*/
 
// table requests
    uint64_t getTablesCount();
    std::vector<std::string> getTablesList();
    DGridResult getTableSize(const std::string& tname, uint64_t& count, std::string& error_string);
    DGridResult createTable(const std::string& tname, const TableType type,
            const std::vector<std::string>& fields,
            const std::vector<uint64_t>& fields_len, const std::vector<FieldType>& fields_type,
            const std::vector<std::string>& pri_key,
            const std::vector<TableKeyInfo>& sec_keys,
            std::string& error_string);
    DGridResult getStructure(const std::string& tname, TableType& type,
            std::vector<std::string>& fields,
            std::vector<uint64_t>& fields_len, std::vector<FieldType>& fields_type,
            std::vector<std::string>& pri_key,
            std::vector<TableKeyInfo>& sec_keys,
            std::string& error_string);
    DGridResult dropTable(const std::string& tname, std::string& error_string);
    DGridResult sortTable(const std::string& tname, const SortOrderList& sort_order, const uint16_t flags, std::string& error_string);
 
    DGridResult resetSecondaryIndex(const std::string& tname, const std::vector<std::string>& key_names, std::string& error_string);
    DGridResult rebuildSecondaryIndex(const std::string& tname, const std::vector<std::string>& key_names, std::string& error_string);
 
    DGridResult appendRow(const std::string& tname, const std::vector<std::string>& values, ModifyStatsData& stats, std::string& error_string);
    DGridResult appendRows(const std::string& tname, const std::vector<std::vector<std::string> >& rows, ModifyStatsData& stats, std::string& error_string);
 
    DGridResult updateRowByID(const std::string& tname, const std::vector<std::string>& values, const RecordID id_record, std::string& error_string);
    DGridResult updateRow(const std::string& tname, const std::vector<std::string>& values, const uint64_t index, std::string& error_string);
    DGridResult updateRow(const std::string& tname, const std::vector<std::string>& values, const std::vector<std::string>& key_values, std::string& error_string);
 
    DGridResult insertRow(const std::string& tname, const std::vector<std::string>& values, const uint64_t index, ModifyStatsData& stats, std::string& error_string);
    DGridResult insertRows(const std::string& tname, const std::vector<std::vector<std::string> >& values, const uint64_t index, ModifyStatsData& stats, std::string& error_string);
 
    DGridResult selectRowByID(const std::string& tname, const RecordID id_record, TableResponseRowData& values, std::string& error_string);
    DGridResult selectRow(const std::string& tname, const uint64_t index, TableResponseRowData& values, std::string& error_string);
    DGridResult selectRows(const std::string& tname, const uint64_t index_from, const uint64_t count, std::vector<TableResponseRowData>& values, std::string& error_string);
    DGridResult selectRows(const std::string& tname, std::vector<TableResponseRowData>& values, std::string& error_string);
    DGridResult selectRow(const std::string& tname, const std::vector<std::string>& keys, const std::vector<std::string>& search_values, const uint16_t flags, TableResponseRowData& values, std::string& error_string);
    DGridResult selectNextRow(const std::string& tname, const ComplexSearchCondition& condition,
                            const RecordID id_prev, const uint64_t index_from,
                            TableResponseRowData& values, std::string& error_string);
 
    DGridResult deleteRowByID(const std::string& tname, const RecordID id_record, std::string& error_string);
    DGridResult deleteRow(const std::string& tname, const uint64_t index, std::string& error_string);
    DGridResult deleteRows(const std::string& tname, std::vector<uint64_t>& indexes, uint64_t& rows_affected, std::string& error_string);
    DGridResult deleteRows(const std::string& tname, const uint64_t index, const uint64_t& count, uint64_t& rows_affected, std::string& error_string);
    DGridResult deleteRows(const std::string& tname, const std::vector<std::string>& keys, const std::vector<std::string>& search_values, uint64_t& rows_affected, std::string& error_string);
    DGridResult deleteAdjacentDuplicates(const std::string& tname, std::vector<uint16_t>& indexes, uint64_t& rows_affected, std::string& error_string);
 
    DGridResult clearTable(const std::string& tname, std::string& error_string);
    DGridResult copyTableData(const std::string& tname_src, const std::string& tname_dst, std::string& error_string);
    DGridResult copyTableData(const std::string& tname_src, const std::string& tname_dst, DGriddDataStoreTable* tstore_dst, std::string& error_string);
 
    DGridResult cursorCreate(const std::string& tname, const ComplexSearchCondition& condition, TableCursorID& cursor, std::string& error_string);
    DGridResult cursorCreate(const std::string& tname, const ComplexSearchCondition& condition, const uint64_t index_from, const uint64_t index_to, TableCursorID& cursor, std::string& error_string);
    DGridResult cursorDelete(const std::string& tname, const TableCursorID& cursor, std::string& error_string);
    DGridResult cursorSelectNextRow(const std::string& tname, const TableCursorID& cursor, TableResponseRowData& values, std::string& error_string);
 
protected:
    TableDataPtr getTable(const std::string& tname, std::string& error_string);
 
/*
node interaction methods
*/
 
private:
    bool fillFieldsIndexes(ComplexSearchCondition__int& condition, TableDataPtr& table, std::string& error_string);
    bool createSortKey(TableDataPtr& table, const SortOrderList& order, SortKey& key, std::string& error_string);
 
    DGriddNodeArbiter*     _node_arbiter;
    std::string        _session;
    std::unordered_map<std::string, TableDataPtr> tables_std;
};
 
} // namespace DGrid
 
#endif // DGRIDD_DATASTORE_TABLE_H