@@ -89,6 +89,7 @@ struct DBImpl::CompactionState {
89
89
};
90
90
91
91
// Fix user-supplied options to be reasonable
92
+ // 统一用户提供的 options
92
93
template <class T ,class V >
93
94
static void ClipToRange (T* ptr, V minvalue, V maxvalue) {
94
95
if (static_cast <V>(*ptr) > maxvalue) *ptr = maxvalue;
@@ -180,13 +181,15 @@ DBImpl::~DBImpl() {
180
181
}
181
182
}
182
183
184
+ // 产生新的 db 目录
183
185
Status DBImpl::NewDB () {
184
186
VersionEdit new_db;
185
187
new_db.SetComparatorName (user_comparator ()->Name ());
186
188
new_db.SetLogNumber (0 );
187
189
new_db.SetNextFile (2 );
188
190
new_db.SetLastSequence (0 );
189
191
192
+ // 新的 manifest 文件
190
193
const std::string manifest = DescriptorFileName (dbname_, 1 );
191
194
WritableFile* file;
192
195
Status s = env_->NewWritableFile (manifest, &file);
@@ -205,6 +208,7 @@ Status DBImpl::NewDB() {
205
208
delete file;
206
209
if (s.ok ()) {
207
210
// Make "CURRENT" file that points to the new manifest file.
211
+ // 设置 CURRENT 文件
208
212
s = SetCurrentFile (env_, dbname_, 1 );
209
213
} else {
210
214
env_->DeleteFile (manifest);
@@ -221,7 +225,7 @@ void DBImpl::MaybeIgnoreError(Status* s) const {
221
225
}
222
226
}
223
227
224
- // 删除一些不必要的文件和内存中不新鲜的数据
228
+ // 删除一些过期的文件
225
229
void DBImpl::DeleteObsoleteFiles () {
226
230
if (!bg_error_.ok ()) {
227
231
// After a background error, we don't know whether a new version may
@@ -286,7 +290,7 @@ void DBImpl::DeleteObsoleteFiles() {
286
290
}
287
291
}
288
292
289
- // cheng: recover
293
+ // 恢复 db
290
294
Status DBImpl::Recover (VersionEdit* edit, bool *save_manifest) {
291
295
mutex_.AssertHeld ();
292
296
@@ -1279,6 +1283,7 @@ const Snapshot* DBImpl::GetSnapshot() {
1279
1283
return snapshots_.New (versions_->LastSequence ());
1280
1284
}
1281
1285
1286
+ // 从 snapshot 列表中删除某个 snapshot
1282
1287
void DBImpl::ReleaseSnapshot (const Snapshot* s) {
1283
1288
MutexLock l (&mutex_);
1284
1289
snapshots_.Delete (reinterpret_cast <const SnapshotImpl*>(s));
@@ -1618,7 +1623,7 @@ Status DB::Open(const Options& options, const std::string& dbname,
1618
1623
VersionEdit edit;
1619
1624
// Recover handles create_if_missing, error_if_exists
1620
1625
bool save_manifest = false ;
1621
- // 尝试恢复 db
1626
+ // 尝试恢复 db 或创建 db
1622
1627
Status s = impl->Recover (&edit, &save_manifest);
1623
1628
if (s.ok () && impl->mem_ == NULL ) {
1624
1629
// Create new log and a corresponding memtable.
0 commit comments