Blob Blame Raw
From: Hiram Chirino <hiram@hiramchirino.com>
Date: Tue, 30 Oct 2012 16:56:52 -0400
Subject: [PATCH] Added a DB:SuspendCompations() and DB:ResumeCompactions()
 methods. Fixes issue #184

https://code.google.com/p/leveldb/issues/detail?id=184

diff --git a/db/db_impl.cc b/db/db_impl.cc
index 3468862..a3358d1 100644
--- a/db/db_impl.cc
+++ b/db/db_impl.cc
@@ -134,6 +134,9 @@ DBImpl::DBImpl(const Options& raw_options, const std::string& dbname)
       table_cache_(new TableCache(dbname_, options_, TableCacheSize(options_))),
       db_lock_(nullptr),
       shutting_down_(false),
+      suspend_cv(&suspend_mutex),
+      suspend_count(0),
+      suspended(false),
       background_work_finished_signal_(&mutex_),
       mem_(nullptr),
       imm_(nullptr),
@@ -1482,6 +1485,39 @@ void DBImpl::GetApproximateSizes(
   }
 }
 
+void DBImpl::SuspendCompactions() {
+  MutexLock l(& suspend_mutex);
+  env_->Schedule(&SuspendWork, this);
+  suspend_count++;
+  while( !suspended ) {
+    suspend_cv.Wait();
+  }  
+}
+void DBImpl::SuspendWork(void* db) {
+  reinterpret_cast<DBImpl*>(db)->SuspendCallback();
+}
+void DBImpl::SuspendCallback() {
+    MutexLock l(&suspend_mutex);
+    Log(options_.info_log, "Compactions suspended");
+    suspended = true;
+    suspend_cv.SignalAll();
+    while( suspend_count > 0 ) {
+        suspend_cv.Wait();
+    }
+    suspended = false;
+    suspend_cv.SignalAll();
+    Log(options_.info_log, "Compactions resumed");
+}
+void DBImpl::ResumeCompactions() {
+    MutexLock l(&suspend_mutex);
+    suspend_count--;
+    suspend_cv.SignalAll();
+    while( suspended ) {
+      suspend_cv.Wait();
+    }  
+}
+
+
 // Default implementations of convenience methods that subclasses of DB
 // can call if they wish
 Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value) {
diff --git a/db/db_impl.h b/db/db_impl.h
index ca00d42..07df200 100644
--- a/db/db_impl.h
+++ b/db/db_impl.h
@@ -44,6 +44,8 @@ class DBImpl : public DB {
   virtual bool GetProperty(const Slice& property, std::string* value);
   virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes);
   virtual void CompactRange(const Slice* begin, const Slice* end);
+  virtual void SuspendCompactions();
+  virtual void ResumeCompactions();
 
   // Extra methods (for testing) that are not in the public DB interface
 
@@ -137,6 +139,13 @@ class DBImpl : public DB {
   // Lock over the persistent DB state.  Non-null iff successfully acquired.
   FileLock* db_lock_;
 
+  port::Mutex suspend_mutex;
+  port::CondVar suspend_cv;
+  int suspend_count;
+  bool suspended;
+  static void SuspendWork(void* db);
+  void SuspendCallback();
+
   // State below is protected by mutex_
   port::Mutex mutex_;
   std::atomic<bool> shutting_down_;
diff --git a/db/db_test.cc b/db/db_test.cc
index 3ab4aee..8730e4f 100644
--- a/db/db_test.cc
+++ b/db/db_test.cc
@@ -2042,6 +2042,10 @@ class ModelDB: public DB {
 
   explicit ModelDB(const Options& options): options_(options) { }
   ~ModelDB() { }
+  
+  virtual void SuspendCompactions() {}
+  virtual void ResumeCompactions() {}
+  
   virtual Status Put(const WriteOptions& o, const Slice& k, const Slice& v) {
     return DB::Put(o, k, v);
   }
diff --git a/include/leveldb/db.h b/include/leveldb/db.h
index 0239593..78abfb0 100644
--- a/include/leveldb/db.h
+++ b/include/leveldb/db.h
@@ -146,6 +146,12 @@ class LEVELDB_EXPORT DB {
   // Therefore the following call will compact the entire database:
   //    db->CompactRange(nullptr, nullptr);
   virtual void CompactRange(const Slice* begin, const Slice* end) = 0;
+
+  // Suspends the background compaction thread.  This methods
+  // returns once suspended.
+  virtual void SuspendCompactions() = 0;
+  // Resumes a suspended background compation thread.
+  virtual void ResumeCompactions() = 0;
 };
 
 // Destroy the contents of the specified database.