Isssue 178 patch:Compaction causes previously deleted value to reappear
- Dominant language
- C++
- Stars
- 39.4k
- Forks
- 8.2k
- PR merge metrics
- No merged PRs in 30d
Description
Original [issue 199](https://code.google.com/p/leveldb/issues/detail?id=199) created by zju.zhengmh on 2013-08-21T06:39:43.000Z:
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.
``` patch
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;
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.