byzhang / leveldb

Automatically exported from code.google.com/p/leveldb
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Isssue 178 patch:Compaction causes previously deleted value to reappear #199

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
In Leveldb 1.11, it fixed isssue 178 by not pick level-0 files when do 
CompactRange.
But we can pick level-0 oldest files for CompactRange, which we ca sort files 
if it is level0. It will be more reasonable, since it can avoid too much 
level-0 files.

diff --git a/db/version_set.cc b/db/version_set.cc
index 4fd1dde..21b76ee 100644
--- a/db/version_set.cc
+++ b/db/version_set.cc
@@ -289,6 +289,11 @@ static bool NewestFirst(FileMetaData* a, FileMetaData* b) {
   return a->number > b->number;
 }

+static bool OldestFirst(FileMetaData* a, FileMetaData* b) {
+  return a->number < b->number;
+}
+
+
 Status Version::Get(const ReadOptions& options,
                     const LookupKey& k,
                     std::string* value,
@@ -1333,17 +1338,19 @@ Compaction* VersionSet::CompactRange(
   // Avoid compacting too much in one shot in case the range is large.
   // But we cannot do this for level-0 since level-0 files can overlap
   // and we must not pick one file and drop another older file if the
-  // two files overlap.
-  if (level > 0) {
-    const uint64_t limit = MaxFileSizeForLevel(level);
-    uint64_t total = 0;
-    for (size_t i = 0; i < inputs.size(); i++) {
-      uint64_t s = inputs[i]->file_size;
-      total += s;
-      if (total >= limit) {
-        inputs.resize(i + 1);
-        break;
-      }
+  // two files overlap. For level-0, we can pick oldest files.
+  if (level == 0) {
+    std::sort(inputs.begin(), inputs.end(), OldestFirst);
+  }
+
+  const uint64_t limit = MaxFileSizeForLevel(level);
+  uint64_t total = 0;
+  for (size_t i = 0; i < inputs.size(); i++) {
+    uint64_t s = inputs[i]->file_size;
+    total += s;
+    if (total >= limit) {
+      inputs.resize(i + 1);
+      break;
     }
   }

Original issue reported on code.google.com by zju.zhen...@gmail.com on 21 Aug 2013 at 6:39

Attachments: