From 086354f9908af2bd6c1e2ee48ec0e019f79df9db Mon Sep 17 00:00:00 2001 From: cyq <1056374449@qq.com> Date: Wed, 30 Oct 2024 01:22:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=85=83=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0deadtime=E7=9A=84=E8=8C=83=E5=9B=B4=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E6=A0=B9=E6=8D=AE=E8=8C=83=E5=9B=B4=E5=AF=B9=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=9C=A8=E5=90=88=E5=B9=B6=E6=97=B6=E5=80=99=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E6=B8=85=E9=99=A4=EF=BC=8C=E5=8F=AF=E4=BB=A5=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E6=89=8B=E5=8A=A8=E5=90=88=E5=B9=B6=E4=B8=8D=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/db_impl.cc | 31 ++++++++++++++++++++++++++++++- db/db_impl.h | 1 + db/version_set.h | 2 ++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 28ef225..5d2d9e7 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -591,7 +591,7 @@ void DBImpl::CompactRange(const Slice* begin, const Slice* end) { } } } - max_level_with_files = config::kNumLevels - 1; //TODO:强制合并所有level中的sst,但是这么做不是很优雅 + // max_level_with_files = config::kNumLevels - 1; //TODO:强制合并所有level中的sst,但是这么做不是很优雅 TEST_CompactMemTable(); // TODO(sanjay): Skip if memtable does not overlap for (int level = 0; level < max_level_with_files; level++) { TEST_CompactRange(level, begin, end); @@ -706,6 +706,31 @@ void DBImpl::BackgroundCall() { background_work_finished_signal_.SignalAll(); } +bool DBImpl::RemoveExpireTable() { + bool remove = false; + VersionEdit edit; + time_t nowTime; + time(&nowTime); + Version *base = versions_->current(); + base->Ref(); + for(int level = 0; level < config::kNumLevels; level ++) { + const std::vector &files = versions_->current()->Files(level); + for(auto meta:files) { + if(meta->largest_deadtime < nowTime) { + remove = true; + edit.RemoveFile(level,meta->number); + std::cout<<"remove file : "<number<<" from level : "<LogAndApply(&edit,&mutex_); + RemoveObsoleteFiles(); + } + base->Unref(); + return remove; +} + void DBImpl::BackgroundCompaction() { mutex_.AssertHeld(); @@ -714,6 +739,10 @@ void DBImpl::BackgroundCompaction() { return; } + if(RemoveExpireTable()) { + return; + } + Compaction* c; bool is_manual = (manual_compaction_ != nullptr); InternalKey manual_end; diff --git a/db/db_impl.h b/db/db_impl.h index ef81411..92ea869 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -145,6 +145,7 @@ class DBImpl : public DB { EXCLUSIVE_LOCKS_REQUIRED(mutex_); Status DoCompactionWork(CompactionState* compact) EXCLUSIVE_LOCKS_REQUIRED(mutex_); + bool RemoveExpireTable(); //if remove some table thne return true otherwise return false Status OpenCompactionOutputFile(CompactionState* compact); Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input); diff --git a/db/version_set.h b/db/version_set.h index ea0c925..6b804f5 100644 --- a/db/version_set.h +++ b/db/version_set.h @@ -111,6 +111,8 @@ class Version { int NumFiles(int level) const { return files_[level].size(); } + const std::vector& Files(int level) const {return files_[level]; } + // Return a human readable string that describes this version's contents. std::string DebugString() const;