@@ -1628,6 +1628,13 @@ Status Storage::StartBGThread() {
16281628
16291629Status Storage::AddBGTask (const BGTask& bg_task) {
16301630 bg_tasks_mutex_.lock ();
1631+
1632+ // 如果任务队列不为空且当前任务是 kCompactSST 类型,则直接返回,不添加到队列
1633+ if (!bg_tasks_queue_.empty () && bg_task.operation == kCompactSST ) {
1634+ bg_tasks_mutex_.unlock ();
1635+ return Status::OK ();
1636+ }
1637+
16311638 if (bg_task.type == kAll ) {
16321639 // if current task it is global compact,
16331640 // clear the bg_tasks_queue_;
@@ -1658,10 +1665,14 @@ Status Storage::RunBGTask() {
16581665
16591666 if (task.operation == kCleanAll ) {
16601667 DoCompact (task.type );
1668+ } else if (task.operation == kCompactOldSST ) {
1669+ DoCompactRange (task.type , " " , " " );
16611670 } else if (task.operation == kCompactRange ) {
16621671 if (task.argv .size () == 2 ) {
16631672 DoCompactRange (task.type , task.argv .front (), task.argv .back ());
16641673 }
1674+ } else if (task.operation == kCompactSST ) {
1675+ DoCompactOldFiles (task.type );
16651676 }
16661677 }
16671678 return Status::OK ();
@@ -1670,6 +1681,9 @@ Status Storage::RunBGTask() {
16701681Status Storage::Compact (const DataType& type, bool sync) {
16711682 if (sync) {
16721683 return DoCompact (type);
1684+ } else if (type == kSST ) {
1685+ LOG (WARNING) << " AddBGTask kCompactOldSST" ;
1686+ AddBGTask ({type, kCompactOldSST });
16731687 } else {
16741688 AddBGTask ({type, kCleanAll });
16751689 }
@@ -1722,6 +1736,48 @@ Status Storage::CompactRange(const DataType& type, const std::string& start, con
17221736 return Status::OK ();
17231737}
17241738
1739+ Status Storage::CompactOldFiles (const DataType& type, bool sync) {
1740+ if (sync) {
1741+ return DoCompactOldFiles (type);
1742+ } else {
1743+ AddBGTask ({type, kCompactSST });
1744+ }
1745+ return Status::OK ();
1746+ }
1747+
1748+ Status Storage::DoCompactOldFiles (const DataType& type) {
1749+ Status s;
1750+ if (type == kStrings ) {
1751+ current_task_type_ = Operation::kCompactSST ;
1752+ s = strings_db_->CompactOldFiles ();
1753+ } else if (type == kHashes ) {
1754+ current_task_type_ = Operation::kCompactSST ;
1755+ s = hashes_db_->CompactOldFiles ();
1756+ } else if (type == kSets ) {
1757+ current_task_type_ = Operation::kCompactSST ;
1758+ s = sets_db_->CompactOldFiles ();
1759+ } else if (type == kZSets ) {
1760+ current_task_type_ = Operation::kCompactSST ;
1761+ s = zsets_db_->CompactOldFiles ();
1762+ } else if (type == kLists ) {
1763+ current_task_type_ = Operation::kCompactSST ;
1764+ s = lists_db_->CompactOldFiles ();
1765+ } else if (type == kStreams ) {
1766+ current_task_type_ = Operation::kCompactSST ;
1767+ s = streams_db_->CompactOldFiles ();
1768+ } else {
1769+ current_task_type_ = Operation::kCompactSST ;
1770+ s = strings_db_->CompactOldFiles ();
1771+ s = hashes_db_->CompactOldFiles ();
1772+ s = sets_db_->CompactOldFiles ();
1773+ s = zsets_db_->CompactOldFiles ();
1774+ s = lists_db_->CompactOldFiles ();
1775+ s = streams_db_->CompactOldFiles ();
1776+ }
1777+ current_task_type_ = Operation::kNone ;
1778+ return s;
1779+ }
1780+
17251781Status Storage::DoCompactRange (const DataType& type, const std::string& start, const std::string& end) {
17261782 Status s;
17271783 if (type == kStrings ) {
0 commit comments