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
#include "RocksDBWrapper.h"
 
RocksDBWrapper::RocksDBWrapper(const std::string &db_path, const std::string &index_path)
{
    rocksdb::Options options;
    options.create_if_missing = true;
 
    // Open the main RocksDB database
    rocksdb::Status status = rocksdb::DB::Open(options, db_path, &db_);
    if (!status.ok())
    {
        std::cerr << "Error opening RocksDB database: " << status.ToString() << std::endl;
    }
 
    // Open the index RocksDB database
    status = rocksdb::DB::Open(options, index_path, &index_db_);
    if (!status.ok())
    {
        std::cerr << "Error opening RocksDB index database: " << status.ToString() << std::endl;
    }
}
 
RocksDBWrapper::~RocksDBWrapper()
{
    delete db_;
    delete index_db_;
}
 
bool RocksDBWrapper::keyExists(const std::string &key)
{
    std::string value;
    rocksdb::Status status = db_->Get(rocksdb::ReadOptions(), serializeKey(key), &value);
    return status.ok();
}
 
bool RocksDBWrapper::StringMatch(const std::string &text, const std::string &pattern)
{
    int n = text.length();
    int m = pattern.length();
    int i = 0, j = 0, startIndex = -1, match = 0;
 
    while (i < n)
    {
        if (j < m && (pattern[j] == '?' || pattern[j] == text[i]))
        {
            i++;
            j++;
        }
        else if (j < m && pattern[j] == '*')
        {
            startIndex = j;
            match = i;
            j++;
        }
        else if (startIndex != -1)
        {
            j = startIndex + 1;
            match++;
            i = match;
        }
        else
        {
            return false;
        }
    }
 
    while (j < m && pattern[j] == '*')
    {
        j++;
    }
 
    return j == m;
}