Ferenc Szontágh
2024-06-27 3ac954922108b07fb3f7a7ce1c727bfcfad8b263
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
#include "RocksDBWrapper.h"
#include "tser/tser.hpp"
#include <iostream>
#include <string>
 
struct TestStruct {
    int id;
    std::string name;
    double value;
 
    DEFINE_SERIALIZABLE(id, name, value);
};
 
int main() {
    RocksDBWrapper db("/tmp/testdb", "/tmp/testindex");
 
    TestStruct ts1{1, "Alpha", 1.1};
    TestStruct ts2{2, "Beta", 2.2};
    TestStruct ts3{3, "Gamma", 3.3};
 
    // Store data
    db.store("key1", ts1);
    db.store("key2", ts2);
    db.store("key3", ts3);
 
    // Retrieve data
    TestStruct retrieved;
    if (db.get("key1", retrieved)) {
        std::cout << "Retrieved: " << retrieved.name << std::endl;
    }
 
    // Check key existence
    if (db.keyExists("key2")) {
        std::cout << "Key 'key2' exists in the database." << std::endl;
    }
 
    // Search by member
    auto keys = db.search<TestStruct>("name", "Beta");
    for (const auto& key : keys) {
        std::cout << "Found key for Beta: " << key << std::endl;
    }
 
    // Text search
    std::vector<TestStruct> results;
    db.search_text("Gam", results);
    for (const auto& result : results) {
        std::cout << "Text search found: " << result.name << std::endl;
    }
 
    // Conditional search
    std::vector<TestStruct> cond_results;
    db.search_conditional<TestStruct>(cond_results, [](const TestStruct& s) {
        return s.value > 2.0;
    });
    for (const auto& result : cond_results) {
        std::cout << "Conditional search found: " << result.name << std::endl;
    }
 
    return 0;
}