Skip to content

Commit 8708e54

Browse files
committed
comment more leveldb
1 parent 25ec4a5 commit 8708e54

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

leveldb/db/builder.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace leveldb {
1616

17+
// 函数作用见 builder.h
1718
Status BuildTable(const std::string& dbname,
1819
Env* env,
1920
const Options& options,
@@ -34,7 +35,7 @@ Status BuildTable(const std::string& dbname,
3435
return s;
3536
}
3637

37-
// 用 TableBuilder 来处理
38+
// 用 TableBuilder 来处理,迭代添加 kv 数据
3839
TableBuilder* builder = new TableBuilder(options, file);
3940
meta->smallest.DecodeFrom(iter->key());
4041
for (; iter->Valid(); iter->Next()) {
@@ -57,6 +58,7 @@ Status BuildTable(const std::string& dbname,
5758
delete builder;
5859

5960
// Finish and check for file errors
61+
// 同步并关闭文件
6062
if (s.ok()) {
6163
s = file->Sync();
6264
}
@@ -68,7 +70,7 @@ Status BuildTable(const std::string& dbname,
6870

6971
if (s.ok()) {
7072
// Verify that the table is usable
71-
// 验证可用
73+
// 验证可用,加入 cache
7274
Iterator* it = table_cache->NewIterator(ReadOptions(),
7375
meta->number,
7476
meta->file_size);

leveldb/db/builder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class VersionEdit;
2222
// *meta will be filled with metadata about the generated table.
2323
// If no data is present in *iter, meta->file_size will be set to
2424
// zero, and no Table file will be produced.
25+
// 以 iter 中的数据生成一个 sstable, 函数名以 meta->number 为基准
26+
// 成功会把 sstable 的元信息填入 meta
27+
// iter 中无数据时,meta->file_size 会置为 0,不会有 sstable 文件产生
2528
extern Status BuildTable(const std::string& dbname,
2629
Env* env,
2730
const Options& options,

leveldb/db/db_impl.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct DBImpl::CompactionState {
8989
};
9090

9191
// Fix user-supplied options to be reasonable
92+
// 统一用户提供的 options
9293
template <class T,class V>
9394
static void ClipToRange(T* ptr, V minvalue, V maxvalue) {
9495
if (static_cast<V>(*ptr) > maxvalue) *ptr = maxvalue;
@@ -180,13 +181,15 @@ DBImpl::~DBImpl() {
180181
}
181182
}
182183

184+
// 产生新的 db 目录
183185
Status DBImpl::NewDB() {
184186
VersionEdit new_db;
185187
new_db.SetComparatorName(user_comparator()->Name());
186188
new_db.SetLogNumber(0);
187189
new_db.SetNextFile(2);
188190
new_db.SetLastSequence(0);
189191

192+
// 新的 manifest 文件
190193
const std::string manifest = DescriptorFileName(dbname_, 1);
191194
WritableFile* file;
192195
Status s = env_->NewWritableFile(manifest, &file);
@@ -205,6 +208,7 @@ Status DBImpl::NewDB() {
205208
delete file;
206209
if (s.ok()) {
207210
// Make "CURRENT" file that points to the new manifest file.
211+
// 设置 CURRENT 文件
208212
s = SetCurrentFile(env_, dbname_, 1);
209213
} else {
210214
env_->DeleteFile(manifest);
@@ -221,7 +225,7 @@ void DBImpl::MaybeIgnoreError(Status* s) const {
221225
}
222226
}
223227

224-
// 删除一些不必要的文件和内存中不新鲜的数据
228+
// 删除一些过期的文件
225229
void DBImpl::DeleteObsoleteFiles() {
226230
if (!bg_error_.ok()) {
227231
// After a background error, we don't know whether a new version may
@@ -286,7 +290,7 @@ void DBImpl::DeleteObsoleteFiles() {
286290
}
287291
}
288292

289-
// cheng: recover
293+
// 恢复 db
290294
Status DBImpl::Recover(VersionEdit* edit, bool *save_manifest) {
291295
mutex_.AssertHeld();
292296

@@ -1279,6 +1283,7 @@ const Snapshot* DBImpl::GetSnapshot() {
12791283
return snapshots_.New(versions_->LastSequence());
12801284
}
12811285

1286+
// 从 snapshot 列表中删除某个 snapshot
12821287
void DBImpl::ReleaseSnapshot(const Snapshot* s) {
12831288
MutexLock l(&mutex_);
12841289
snapshots_.Delete(reinterpret_cast<const SnapshotImpl*>(s));
@@ -1618,7 +1623,7 @@ Status DB::Open(const Options& options, const std::string& dbname,
16181623
VersionEdit edit;
16191624
// Recover handles create_if_missing, error_if_exists
16201625
bool save_manifest = false;
1621-
// 尝试恢复 db
1626+
// 尝试恢复 db 或创建 db
16221627
Status s = impl->Recover(&edit, &save_manifest);
16231628
if (s.ok() && impl->mem_ == NULL) {
16241629
// Create new log and a corresponding memtable.

0 commit comments

Comments
 (0)