-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathRocksEngine.h
223 lines (160 loc) · 6.85 KB
/
RocksEngine.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
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
/* Copyright (c) 2018 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/
#ifndef KVSTORE_ROCKSENGINE_H_
#define KVSTORE_ROCKSENGINE_H_
#include <gtest/gtest_prod.h>
#include <rocksdb/db.h>
#include <rocksdb/utilities/backupable_db.h>
#include <rocksdb/utilities/checkpoint.h>
#include "common/base/Base.h"
#include "kvstore/KVEngine.h"
#include "kvstore/KVIterator.h"
namespace nebula {
namespace kvstore {
class RocksRangeIter : public KVIterator {
public:
RocksRangeIter(rocksdb::Iterator* iter, rocksdb::Slice start, rocksdb::Slice end)
: iter_(iter), start_(start), end_(end) {}
~RocksRangeIter() = default;
bool valid() const override {
return !!iter_ && iter_->Valid() && (iter_->key().compare(end_) < 0);
}
void next() override { iter_->Next(); }
void prev() override { iter_->Prev(); }
folly::StringPiece key() const override {
return folly::StringPiece(iter_->key().data(), iter_->key().size());
}
folly::StringPiece val() const override {
return folly::StringPiece(iter_->value().data(), iter_->value().size());
}
private:
std::unique_ptr<rocksdb::Iterator> iter_;
rocksdb::Slice start_;
rocksdb::Slice end_;
};
class RocksPrefixIter : public KVIterator {
public:
RocksPrefixIter(rocksdb::Iterator* iter, rocksdb::Slice prefix) : iter_(iter), prefix_(prefix) {}
~RocksPrefixIter() = default;
bool valid() const override {
return !!iter_ && iter_->Valid() && (iter_->key().starts_with(prefix_));
}
void next() override { iter_->Next(); }
void prev() override { iter_->Prev(); }
folly::StringPiece key() const override {
return folly::StringPiece(iter_->key().data(), iter_->key().size());
}
folly::StringPiece val() const override {
return folly::StringPiece(iter_->value().data(), iter_->value().size());
}
protected:
std::unique_ptr<rocksdb::Iterator> iter_;
rocksdb::Slice prefix_;
};
class RocksCommonIter : public KVIterator {
public:
explicit RocksCommonIter(rocksdb::Iterator* iter) : iter_(iter) {}
~RocksCommonIter() = default;
bool valid() const override { return !!iter_ && iter_->Valid(); }
void next() override { iter_->Next(); }
void prev() override { iter_->Prev(); }
folly::StringPiece key() const override {
return folly::StringPiece(iter_->key().data(), iter_->key().size());
}
folly::StringPiece val() const override {
return folly::StringPiece(iter_->value().data(), iter_->value().size());
}
protected:
std::unique_ptr<rocksdb::Iterator> iter_;
};
/**************************************************************************
*
* An implementation of KVEngine based on Rocksdb
*
*************************************************************************/
class RocksEngine : public KVEngine {
FRIEND_TEST(RocksEngineTest, SimpleTest);
public:
RocksEngine(GraphSpaceID spaceId,
int32_t vIdLen,
const std::string& dataPath,
const std::string& walPath = "",
std::shared_ptr<rocksdb::MergeOperator> mergeOp = nullptr,
std::shared_ptr<rocksdb::CompactionFilterFactory> cfFactory = nullptr,
bool readonly = false);
~RocksEngine() { LOG(INFO) << "Release rocksdb on " << dataPath_; }
void stop() override;
// return path to a spaceId, e.g. "/DataPath/nebula/spaceId", usally it should
// contain two subdir: data and wal.
const char* getDataRoot() const override { return dataPath_.c_str(); }
const char* getWalRoot() const override { return walPath_.c_str(); }
std::unique_ptr<WriteBatch> startBatchWrite() override;
nebula::cpp2::ErrorCode commitBatchWrite(std::unique_ptr<WriteBatch> batch,
bool disableWAL,
bool sync,
bool wait) override;
/*********************
* Data retrieval
********************/
nebula::cpp2::ErrorCode get(const std::string& key, std::string* value) override;
std::vector<Status> multiGet(const std::vector<std::string>& keys,
std::vector<std::string>* values) override;
nebula::cpp2::ErrorCode range(const std::string& start,
const std::string& end,
std::unique_ptr<KVIterator>* iter) override;
nebula::cpp2::ErrorCode prefix(const std::string& prefix,
std::unique_ptr<KVIterator>* iter) override;
nebula::cpp2::ErrorCode rangeWithPrefix(const std::string& start,
const std::string& prefix,
std::unique_ptr<KVIterator>* iter) override;
nebula::cpp2::ErrorCode scan(std::unique_ptr<KVIterator>* storageIter) override;
/*********************
* Data modification
********************/
nebula::cpp2::ErrorCode put(std::string key, std::string value) override;
nebula::cpp2::ErrorCode multiPut(std::vector<KV> keyValues) override;
nebula::cpp2::ErrorCode remove(const std::string& key) override;
nebula::cpp2::ErrorCode multiRemove(std::vector<std::string> keys) override;
nebula::cpp2::ErrorCode removeRange(const std::string& start, const std::string& end) override;
/*********************
* Non-data operation
********************/
void addPart(PartitionID partId) override;
void removePart(PartitionID partId) override;
std::vector<PartitionID> allParts() override;
int32_t totalPartsNum() override;
nebula::cpp2::ErrorCode ingest(const std::vector<std::string>& files,
bool verifyFileChecksum = false) override;
nebula::cpp2::ErrorCode setOption(const std::string& configKey,
const std::string& configValue) override;
nebula::cpp2::ErrorCode setDBOption(const std::string& configKey,
const std::string& configValue) override;
nebula::cpp2::ErrorCode compact() override;
nebula::cpp2::ErrorCode flush() override;
nebula::cpp2::ErrorCode backup() override;
/*********************
* Checkpoint operation
********************/
nebula::cpp2::ErrorCode createCheckpoint(const std::string& path) override;
ErrorOr<nebula::cpp2::ErrorCode, std::string> backupTable(
const std::string& path,
const std::string& tablePrefix,
std::function<bool(const folly::StringPiece& key)> filter) override;
private:
std::string partKey(PartitionID partId);
void openBackupEngine(GraphSpaceID spaceId);
private:
GraphSpaceID spaceId_;
std::string dataPath_;
std::string walPath_;
std::unique_ptr<rocksdb::DB> db_{nullptr};
std::string backupPath_;
std::unique_ptr<rocksdb::BackupEngine> backupDb_{nullptr};
int32_t partsNum_ = -1;
};
} // namespace kvstore
} // namespace nebula
#endif // KVSTORE_ROCKSENGINE_H_