leveldb 學習記錄(八) compact
阿新 • • 發佈:2018-11-11
隨著執行時間的增加,memtable會慢慢 轉化成 sstable。
sstable會越來越多 我們就需要進行整合 compact
程式碼會在寫入查詢key值 db寫入時等多出位置呼叫MaybeScheduleCompaction ()
檢測是否需要進行compact
1 void DBImpl::MaybeScheduleCompaction() { 2 mutex_.AssertHeld(); 3 if (bg_compaction_scheduled_) { 4 // Already scheduled 5 } else if (shutting_down_.Acquire_Load()) {6 // DB is being deleted; no more background compactions 7 } else if (imm_ == NULL && 8 manual_compaction_ == NULL && 9 !versions_->NeedsCompaction()) { 10 // No work to be done 11 } else { 12 bg_compaction_scheduled_ = true; 13 env_->Schedule(&DBImpl::BGWork, this); 14 } 15 } 16 17 void DBImpl::BGWork(void* db) { 18 reinterpret_cast<DBImpl*>(db)->BackgroundCall(); 19 } 20 21 void DBImpl::BackgroundCall() { 22 MutexLock l(&mutex_); 23 assert(bg_compaction_scheduled_); 24 if (!shutting_down_.Acquire_Load()) { 25 BackgroundCompaction();26 } 27 bg_compaction_scheduled_ = false; 28 29 // Previous compaction may have produced too many files in a level, 30 // so reschedule another compaction if needed. 31 MaybeScheduleCompaction(); 32 bg_cv_.SignalAll(); 33 }
實際進行compact的函式是 void DBImpl::BackgroundCompaction()