-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathleveldb_impl.cpp
313 lines (251 loc) · 9.63 KB
/
leveldb_impl.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "leveldb_impl.h"
#include "leveldb/db.h"
#include "leveldb/env.h"
#include <stdlib.h>
#include <sys/stat.h>
#include <fstream>
#include <sstream>
#include <iterator>
#include <string>
#define OVERRIDE override
// #define OVERRIDE
// A wrapper for SequentialFile that forwards the data read information to
// LevelDBImpl.
class LevelDBSequentialFile : public leveldb::SequentialFile {
public:
LevelDBSequentialFile(LevelDBImpl* leveldb_impl, leveldb::SequentialFile* t)
: leveldb::SequentialFile(), leveldb_impl_(leveldb_impl), target_(t) {}
virtual ~LevelDBSequentialFile() OVERRIDE { delete target_; }
virtual leveldb::Status Read(size_t n, leveldb::Slice* result,
char* scratch) OVERRIDE {
leveldb_impl_->Read(n);
return target_->Read(n, result, scratch);
}
virtual leveldb::Status Skip(uint64_t n) OVERRIDE { return target_->Skip(n); }
private:
class LevelDBImpl* leveldb_impl_;
leveldb::SequentialFile* target_;
};
// A wrapper for RandomAccessFile that forwards the data read information to
// LevelDBImpl.
class LevelDBRandomAccessFile : public leveldb::RandomAccessFile {
public:
LevelDBRandomAccessFile(LevelDBImpl* leveldb_impl,
leveldb::RandomAccessFile* t)
: leveldb::RandomAccessFile(), leveldb_impl_(leveldb_impl), target_(t) {}
virtual ~LevelDBRandomAccessFile() OVERRIDE { delete target_; }
virtual leveldb::Status Read(uint64_t offset, size_t n,
leveldb::Slice* result,
char* scratch) const OVERRIDE {
leveldb_impl_->Read(n);
return target_->Read(offset, n, result, scratch);
}
private:
class LevelDBImpl* leveldb_impl_;
leveldb::RandomAccessFile* target_;
};
// A wrapper for WritableFile that forwards the data append information to
// LevelDBImpl.
class LevelDBWritableFile : public leveldb::WritableFile {
public:
LevelDBWritableFile(LevelDBImpl* leveldb_impl, leveldb::WritableFile* t)
: leveldb::WritableFile(), leveldb_impl_(leveldb_impl), target_(t) {}
virtual ~LevelDBWritableFile() OVERRIDE { delete target_; }
virtual leveldb::Status Append(const leveldb::Slice& data) OVERRIDE {
leveldb_impl_->Append(data.size());
return target_->Append(data);
}
virtual leveldb::Status Close() OVERRIDE { return target_->Close(); }
virtual leveldb::Status Flush() OVERRIDE { return target_->Flush(); }
virtual leveldb::Status Sync() OVERRIDE {
if (leveldb_impl_->params_.enable_fsync)
return target_->Sync();
else {
// Let's ignore Sync() for faster experiments.
return leveldb::Status::OK();
}
}
private:
class LevelDBImpl* leveldb_impl_;
leveldb::WritableFile* target_;
};
// A wrapper for Env that forwards the file deletion information to LevelDBImpl.
class LevelDBEnv : public leveldb::EnvWrapper {
public:
LevelDBEnv(LevelDBImpl* leveldb_impl)
: leveldb::EnvWrapper(leveldb::Env::Default()),
leveldb_impl_(leveldb_impl) {}
virtual ~LevelDBEnv() OVERRIDE {}
virtual leveldb::Status NewSequentialFile(
const std::string& f, leveldb::SequentialFile** r) OVERRIDE {
leveldb::Status status = target()->NewSequentialFile(f, r);
if (*r != NULL) *r = new LevelDBSequentialFile(leveldb_impl_, *r);
return status;
}
virtual leveldb::Status NewRandomAccessFile(
const std::string& f, leveldb::RandomAccessFile** r) OVERRIDE {
leveldb::Status status = target()->NewRandomAccessFile(f, r);
if (*r != NULL) *r = new LevelDBRandomAccessFile(leveldb_impl_, *r);
return status;
}
virtual leveldb::Status NewWritableFile(const std::string& f,
leveldb::WritableFile** r) OVERRIDE {
leveldb::Status status = target()->NewWritableFile(f, r);
if (*r != NULL) *r = new LevelDBWritableFile(leveldb_impl_, *r);
return status;
}
virtual leveldb::Status DeleteFile(const std::string& f) OVERRIDE {
struct stat st;
memset(&st, 0, sizeof(st));
// XXX: The file length *might* not be as large as its actual content
// because the directory metadata can be updated later than the appends.
int ret = stat(f.c_str(), &st);
if (ret == 0) leveldb_impl_->Delete(static_cast<uint64_t>(st.st_size));
return target()->DeleteFile(f);
}
private:
class LevelDBImpl* leveldb_impl_;
};
LevelDBImpl::LevelDBImpl(const LevelDBParams& params, std::vector<Stat>& stats)
: params_(params), stats_(stats) {
stats_.push_back(Stat());
pthread_mutex_init(&stats_mutex_, NULL);
read_ = 0;
appended_ = 0;
// Clean up old files.
leveldb::DestroyDB("leveldb_files", leveldb::Options());
options_ = new leveldb::Options();
options_->create_if_missing = true;
// Turn off Snappy.
options_->compression = leveldb::CompressionType::kNoCompression;
// Use our Env to gather statistics.
options_->env = new LevelDBEnv(this);
// Limit the max open file count.
options_->max_open_files = 900;
// Configure the write buffer size.
options_->write_buffer_size = params.log_size_threshold;
// Do not overload insert.
// These are hardcoded in leveldb/db/dbformat.h
// options_->level0_file_num_compaction_trigger = 4;
// options_->level0_slowdown_writes_trigger = 4;
// options_->level0_stop_writes_trigger = 4;
// Use custom level sizes
if (params_.use_custom_sizes) {
std::size_t* custom_level_sizes = new std::size_t[20];
std::ifstream ifs("output_sensitivity.txt");
while (!ifs.eof()) {
std::string line;
std::getline(ifs, line);
std::istringstream iss(line);
std::vector<std::string> tokens{std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>{}};
if (tokens.size() < 5) continue;
if (tokens[0] != "sensitivity_item_count_leveldb_best_sizes" &&
tokens[0] != "sensitivity_log_size_leveldb_best_sizes")
continue;
if (static_cast<uint64_t>(atol(tokens[1].c_str())) !=
params_.hint_num_unique_keys)
continue;
if (atof(tokens[2].c_str()) != params_.hint_theta) continue;
if (static_cast<uint64_t>(atol(tokens[3].c_str())) !=
params_.log_size_threshold)
continue;
options_->custom_level_size_count = tokens.size() - 5 + 1;
custom_level_sizes[0] = 0;
std::size_t level;
for (level = 1; level < options_->custom_level_size_count; level++) {
custom_level_sizes[level] = static_cast<size_t>(
atof(tokens[5 + level - 1].c_str()) * 1000. + 0.5);
printf("level-%zu: %zu\n", level, custom_level_sizes[level]);
}
// Make the last level very large and not spill.
level--;
custom_level_sizes[level] = 1000000000000000LU;
printf("level-%zu: %zu (expanded)\n", level, custom_level_sizes[level]);
printf("\n");
break;
}
assert(options_->custom_level_size_count != 0);
options_->custom_level_sizes = custom_level_sizes;
}
leveldb::Status status = leveldb::DB::Open(*options_, "leveldb_files", &db_);
if (!status.ok()) {
printf("%s\n", status.ToString().c_str());
assert(false);
}
memset(value_buf_, 0, sizeof(value_buf_));
}
LevelDBImpl::~LevelDBImpl() {
delete db_;
delete options_->env;
if (params_.use_custom_sizes) delete[] options_->custom_level_sizes;
delete options_;
pthread_mutex_destroy(&stats_mutex_);
}
void LevelDBImpl::print_status() const {
// Force updating stats.
const_cast<LevelDBImpl*>(this)->Delete(0);
}
void LevelDBImpl::dump_state(FILE* fp) const {
// TODO: Implement.
(void)fp;
}
void LevelDBImpl::put(LevelDBKey key, uint32_t item_size) {
// LevelDB includes the full SSTable file size during calculating the level
// size;
// we consider the average space overhead per item in LevelDB so that the
// average stored size becomes similar to item_size.
const uint32_t overhead = 18;
leveldb::Slice s_key(reinterpret_cast<const char*>(&key), sizeof(key));
uint32_t value_size =
static_cast<uint32_t>(static_cast<std::size_t>(item_size) - sizeof(key)) -
overhead;
assert(value_size < sizeof(value_buf_));
leveldb::Slice s_value(value_buf_, value_size);
leveldb::Status status = db_->Put(leveldb::WriteOptions(), s_key, s_value);
if (!status.ok()) {
printf("%s\n", status.ToString().c_str());
assert(false);
}
}
void LevelDBImpl::del(LevelDBKey key) {
leveldb::Slice s_key(reinterpret_cast<const char*>(&key), sizeof(key));
leveldb::Status status = db_->Delete(leveldb::WriteOptions(), s_key);
if (!status.ok()) {
printf("%s\n", status.ToString().c_str());
assert(false);
}
}
uint64_t LevelDBImpl::get(LevelDBKey key) {
leveldb::Slice s_key(reinterpret_cast<const char*>(&key), sizeof(key));
std::string s_value;
uint64_t value;
leveldb::Status status = db_->Get(leveldb::ReadOptions(), s_key, &s_value);
if (!status.ok()) {
printf("%s\n", status.ToString().c_str());
assert(false);
}
assert(s_value.size() >= sizeof(uint64_t));
value = *reinterpret_cast<const uint64_t*>(s_value.data());
return value;
}
void LevelDBImpl::force_compact() {
db_->CompactRange(NULL, NULL);
// Force stat update.
Delete(0);
}
void LevelDBImpl::Read(std::size_t len) { __sync_fetch_and_add(&read_, len); }
void LevelDBImpl::Append(std::size_t len) {
__sync_fetch_and_add(&appended_, len);
}
void LevelDBImpl::Delete(std::size_t len) {
uint64_t read = read_;
__sync_fetch_and_sub(&read_, read);
uint64_t appended = appended_;
__sync_fetch_and_sub(&appended_, appended);
pthread_mutex_lock(&stats_mutex_);
if (read != 0) stats_.back().read(read);
if (appended != 0) stats_.back().write(appended);
if (len != 0) stats_.back().del(len);
pthread_mutex_unlock(&stats_mutex_);
}