Ferenc Szontágh
2024-06-27 097a09efd9556b22917990ed37fbff3265b24260
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
#include <rocksdb/db.h>
#include <rocksdb/options.h>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <regex>
#include "tser/tser.hpp"
 
// Wrapper class for RocksDB
class RocksDBWrapper
{
public:
    RocksDBWrapper(const std::string &db_path, const std::string &index_path);
 
    ~RocksDBWrapper();
 
    template <typename T>
    void store(const std::string &key, const T &value)
    {
        tser::BinaryArchive archive(0);
        archive.save(value);
 
        rocksdb::Status status = db_->Put(rocksdb::WriteOptions(), serializeKey(key), archive.get_buffer().data());
        if (!status.ok())
        {
            std::cerr << "Error storing value in RocksDB database: " << status.ToString() << std::endl;
        }
 
        // Index the struct's members
        index_members(serializeKey(key), value);
    }
 
    template <typename T>
    bool get(const std::string &key, T &value)
    {
        std::string serialized_value;
        rocksdb::Status status = db_->Get(rocksdb::ReadOptions(), serializeKey(key), &serialized_value);
        if (!status.ok())
        {
            std::cerr << "Error getting value from RocksDB database: " << status.ToString() << std::endl;
            return false;
        }
 
        tser::BinaryArchive archive(0);
        archive.initialize(serialized_value);
        value = archive.load<T>();
        return true;
    }
 
    template <typename T>
    std::vector<std::string> search(const std::string &member_name, const std::string &member_value)
    {
        std::vector<std::string> keys;
        std::string prefix = member_name + ":" + serializeKey(member_value) + ":";
        rocksdb::Iterator *it = index_db_->NewIterator(rocksdb::ReadOptions());
        for (it->Seek(prefix); it->Valid() && it->key().ToString().find(prefix) == 0; it->Next())
        {
            keys.push_back(it->value().ToString());
        }
        delete it;
        return keys;
    }
 
    inline std::vector<std::string> split(const std::string &s, const std::string &delimiter)
    {
        size_t last = 0;
        size_t next = 0;
        std::vector<std::string> results;
        while ((next = s.find(delimiter, last)) != std::string::npos)
        {
            results.emplace_back(s.substr(last, next - last));
            last = next + delimiter.length();
        }
        results.emplace_back(s.substr(last));
        return results;
    }
 
    template <typename T>
    void search_text(const std::string &search_value, std::vector<T> &res)
    {
        std::vector<std::string> keys;
        std::vector<std::string> tmp;
        rocksdb::Iterator *it = index_db_->NewIterator(rocksdb::ReadOptions());
 
        for (it->SeekToFirst(); it->Valid(); it->Next())
        {
            std::string key = it->key().ToString();
            auto _split = this->split(key, ":");
            if (this->StringMatch(_split[1], search_value))
            {
                keys.push_back(_split[2]);
            }
        }
 
        std::vector<rocksdb::Slice> slices;
        for (const auto &key : keys)
        {
            slices.push_back(rocksdb::Slice(key));
        }
 
        std::vector<rocksdb::Status> statuses = db_->MultiGet(rocksdb::ReadOptions(), slices, &tmp);
 
        for (const auto &_v : tmp)
        {
            T _val;
            this->DeSerialize(_v, _val);
            res.emplace_back(_val);
        }
        delete it;
    }
 
    // New keyExists method
    bool keyExists(const std::string &key);
 
    // New conditional search method
    template <typename T>
    void search_conditional(std::vector<T> &res, std::function<bool(const T &)> condition)
    {
        rocksdb::Iterator *it = db_->NewIterator(rocksdb::ReadOptions());
 
        for (it->SeekToFirst(); it->Valid(); it->Next())
        {
            std::string key = it->key().ToString();
            std::string serialized_value = it->value().ToString();
 
            T _val;
            this->DeSerialize(serialized_value, _val);
 
            if (condition(_val))
            {
                res.push_back(_val);
            }
        }
        delete it;
    }
 
private:
    rocksdb::DB *db_;
    rocksdb::DB *index_db_;
 
    bool StringMatch(const std::string &text, const std::string &pattern);
 
    template <typename T>
    inline void DeSerialize(const std::string &data, T &value)
    {
        tser::BinaryArchive archive(0);
        archive.initialize(data);
        value = archive.load<T>();
    }
 
    template <typename T>
    inline void Serialize(const T &value, std::string &data)
    {
        tser::BinaryArchive archive(0);
        archive.save(value);
        data = archive.get_buffer().data();
    }
 
    template <typename T>
    void index_single_member(const std::string &key, const std::string &member_name, const T &member_value)
    {
        if constexpr (std::is_arithmetic_v<std::decay_t<T>>)
        {
            std::string member_value_str = std::to_string(member_value);
            std::string index_key = member_name + ":" + serializeKey(member_value_str) + ":" + key;
            rocksdb::Status status = index_db_->Put(rocksdb::WriteOptions(), index_key, key);
            if (!status.ok())
            {
                std::cerr << "Error indexing member in RocksDB index database: " << status.ToString() << std::endl;
            }
        }
        else if constexpr (std::is_same_v<std::decay_t<T>, std::string>)
        {
            std::string index_key = member_name + ":" + serializeKey(member_value) + ":" + key;
            rocksdb::Status status = index_db_->Put(rocksdb::WriteOptions(), index_key, key);
            if (!status.ok())
            {
                std::cerr << "Error indexing member in RocksDB index database: " << status.ToString() << std::endl;
            }
        }
        else if constexpr (tser::is_container_v<std::decay_t<T>>)
        {
            for (const auto &elem : member_value)
            {
                index_single_member(key, member_name, elem);
            }
        }
        else
        {
            // Handle other types if necessary
        }
    }
 
    template <typename T>
    void index_members(const std::string &key, const T &value)
    {
        if constexpr (tser::is_tser_t_v<T>)
        {
            auto member_names = T::_memberNames;
            size_t index = 0;
            std::apply([&](auto &&...args)
                       { ((index_single_member(key, member_names[index++], args)), ...); },
                       value.members());
        }
    }
 
    std::string serializeKey(const std::string &key)
    {
        // Implement a method to serialize keys to handle special characters
        std::string serialized_key;
        for (const char c : key)
        {
            if (c == ':')
            {
                serialized_key += "%3A"; // Percent-encoding for ':'
            }
            else
            {
                serialized_key += c;
            }
        }
        return serialized_key;
    }
};