Исходный код СУБД 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
#ifndef DGRIDD_DATASTORE_TABLE_INDEX_H
#define DGRIDD_DATASTORE_TABLE_INDEX_H
 
#include <string>
#include <map>
#include <vector>
 
#include "dgrid.h"
#include "dgrid_req_structs.h"
 
namespace DGrid {
 
class TableData;
class TableRowData;
 
class TableRowKeyItem {
public:
    TableRowKeyItem(const TableRowKeyItem& o);
    TableRowKeyItem(const std::string& val, const FieldType data_type, const SortOrder sort_order);
    ~TableRowKeyItem();
    std::string val;
    FieldType   data_type;
    SortOrder   sort_order;
 
    static bool equal(const FieldType data_type, const std::string& a, const std::string& b);
    static bool compare(const FieldType data_type, const std::string& a, const std::string& b);
 
    inline bool operator==(const TableRowKeyItem& b) const { return equal(data_type, val, b.val); };
    inline bool operator==(const std::string& b) const { return equal(data_type, val, b); };
    inline bool operator>(const TableRowKeyItem& b) const { return compare(data_type, val, b.val); };
    inline bool operator>(const std::string& b) const { return compare(data_type, val, b); };
    inline bool operator<(const TableRowKeyItem& b) const { return compare(data_type, b.val, val); };
    inline bool operator<(const std::string& b) const { return compare(data_type, b, val); };
 
    template<class ITEM>
    inline bool operator!=(const ITEM& b) const { return !(*this == b); }
};
typedef std::vector<TableRowKeyItem> TableRowKey;
 
struct TableRowKeyHasher
{
    uint64_t operator()(const TableRowKey& k) const;
};
 
// hashed map for row indexing
typedef std::unordered_map<TableRowKey, uint64_t, TableRowKeyHasher> HashedRows;
 
 
class DataIndex {
public:
    DataIndex(TableData* table, const TableKeyInfo&);
    ~DataIndex();
    virtual const std::string& type() const = 0;
    const std::string& name() const;
    bool checkOrRebuild();
    virtual bool rebuild() = 0;
    virtual void reset() = 0;
    virtual void removeRow(TableRowData*) = 0;
    virtual bool search(const std::vector<std::string>& values, uint64_t& index) = 0;
    bool isValid();
    bool checkComponents(const std::vector<std::string>& components) const;
 
protected:
    TableData* table;
    TableKeyInfo key_info;
    bool valid;
};
 
typedef std::pair<TableRowKey, uint64_t> sortedKeyItem;
 
class DataIndex_sorted : public DataIndex {
public:
    DataIndex_sorted(TableData* table, const TableKeyInfo&);
    ~DataIndex_sorted();
    const std::string& type() const override { return _type; };
    bool rebuild() override;
    void reset() override;
    void removeRow(TableRowData* row) override;
    bool search(const std::vector<std::string>& values, uint64_t& index) override;
 
private:
    std::vector<sortedKeyItem> rows_index;
    bool bsearch(const TableRowKey& key, uint64_t& index);
 
private:
    static const std::string _type;
};
 
class DataIndex_hashed : public DataIndex {
public:
    DataIndex_hashed(TableData* table, const TableKeyInfo&);
    ~DataIndex_hashed();
    const std::string& type() const override { return _type; };
    bool rebuild() override;
    void reset() override;
    void removeRow(TableRowData* row) override;
    bool search(const std::vector<std::string>& values, uint64_t& index) override;
 
private:
    HashedRows rows_index;
 
private:
    static const std::string _type;
};
 
} // namespace DGrid
 
#endif // DGRIDD_DATASTORE_TABLE_INDEX_H