forked from apache/kvrocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_db.h
99 lines (87 loc) · 3.62 KB
/
redis_db.h
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
#pragma once
#include <string>
#include <vector>
#include <utility>
#include <map>
#include "redis_metadata.h"
#include "storage.h"
namespace Redis {
class Database {
public:
explicit Database(Engine::Storage *storage, const std::string &ns = "");
rocksdb::Status GetMetadata(RedisType type, const Slice &ns_key, Metadata *metadata);
rocksdb::Status GetRawMetadata(const Slice &ns_key, std::string *bytes);
rocksdb::Status GetRawMetadataByUserKey(const Slice &user_key, std::string *bytes);
rocksdb::Status Expire(const Slice &user_key, int timestamp);
rocksdb::Status Del(const Slice &user_key);
rocksdb::Status Exists(const std::vector<Slice> &keys, int *ret);
rocksdb::Status TTL(const Slice &user_key, int *ttl);
rocksdb::Status Type(const Slice &user_key, RedisType *type);
rocksdb::Status Dump(const Slice &user_key, std::vector<std::string> *infos);
rocksdb::Status FlushDB();
rocksdb::Status FlushAll();
void GetKeyNumStats(const std::string &prefix, KeyNumStats *stats);
void Keys(std::string prefix, std::vector<std::string> *keys = nullptr, KeyNumStats *stats = nullptr);
rocksdb::Status Scan(const std::string &cursor,
uint64_t limit,
const std::string &prefix,
std::vector<std::string> *keys,
std::string *end_cursor = nullptr);
rocksdb::Status RandomKey(const std::string &cursor, std::string *key);
void AppendNamespacePrefix(const Slice &user_key, std::string *output);
rocksdb::Status FindKeyRangeWithPrefix(const std::string &prefix,
const std::string &prefix_end,
std::string *begin,
std::string *end,
rocksdb::ColumnFamilyHandle *cf_handle = nullptr);
rocksdb::Status ClearKeysOfSlot(const rocksdb::Slice &ns, int slot);
rocksdb::Status GetSlotKeysInfo(int slot,
std::map<int, uint64_t> *slotskeys,
std::vector<std::string> *keys,
int count);
protected:
Engine::Storage *storage_;
rocksdb::DB *db_;
rocksdb::ColumnFamilyHandle *metadata_cf_handle_;
std::string namespace_;
class LatestSnapShot {
public:
explicit LatestSnapShot(rocksdb::DB *db) : db_(db) {
snapshot_ = db_->GetSnapshot();
}
~LatestSnapShot() {
db_->ReleaseSnapshot(snapshot_);
}
const rocksdb::Snapshot *GetSnapShot() { return snapshot_; }
private:
rocksdb::DB *db_ = nullptr;
const rocksdb::Snapshot *snapshot_ = nullptr;
};
};
class SubKeyScanner : public Redis::Database {
public:
explicit SubKeyScanner(Engine::Storage *storage, const std::string &ns)
: Database(storage, ns) {}
rocksdb::Status Scan(RedisType type,
const Slice &user_key,
const std::string &cursor,
uint64_t limit,
const std::string &subkey_prefix,
std::vector<std::string> *keys,
std::vector<std::string> *values = nullptr);
};
class WriteBatchLogData {
public:
WriteBatchLogData() = default;
explicit WriteBatchLogData(RedisType type) : type_(type) {}
explicit WriteBatchLogData(RedisType type, std::vector<std::string> &&args) :
type_(type), args_(std::move(args)) {}
RedisType GetRedisType();
std::vector<std::string> *GetArguments();
std::string Encode();
Status Decode(const rocksdb::Slice &blob);
private:
RedisType type_ = kRedisNone;
std::vector<std::string> args_;
};
} // namespace Redis