Use same sorting for all level 0 files

Summary:
We decided that one of the long term goals is to unify level and universal compaction.

As a small first step, I'm unifying level 0 sorting methods.

Previously, we used to sort level 0 files in level compaction by file number and in universal compaction by sequence number.

But it turns out that in level compaction, sorting by file number is exactly the same as sorting by sequence number.

Test Plan:
Ran make check with bunch of asserts to verify the sorting order is exactly the same.
Also, make check with this patch

Reviewers: haobo, yhchiang, ljin, dhruba, sdong

Reviewed By: sdong

Subscribers: leveldb

Differential Revision: https://reviews.facebook.net/D19131
This commit is contained in:
Igor Canadi 2014-06-20 09:12:14 +02:00
parent 7a9dd5f214
commit 107e08baa7

View File

@ -445,9 +445,6 @@ static bool SaveValue(void* arg, const ParsedInternalKey& parsed_key,
} }
namespace { namespace {
bool NewestFirst(FileMetaData* a, FileMetaData* b) {
return a->fd.GetNumber() > b->fd.GetNumber();
}
bool NewestFirstBySeqNo(FileMetaData* a, FileMetaData* b) { bool NewestFirstBySeqNo(FileMetaData* a, FileMetaData* b) {
if (a->smallest_seqno != b->smallest_seqno) { if (a->smallest_seqno != b->smallest_seqno) {
return a->smallest_seqno > b->smallest_seqno; return a->smallest_seqno > b->smallest_seqno;
@ -456,7 +453,7 @@ bool NewestFirstBySeqNo(FileMetaData* a, FileMetaData* b) {
return a->largest_seqno > b->largest_seqno; return a->largest_seqno > b->largest_seqno;
} }
// Break ties by file number // Break ties by file number
return NewestFirst(a, b); return a->fd.GetNumber() > b->fd.GetNumber();
} }
bool BySmallestKey(FileMetaData* a, FileMetaData* b, bool BySmallestKey(FileMetaData* a, FileMetaData* b,
const InternalKeyComparator* cmp) { const InternalKeyComparator* cmp) {
@ -655,11 +652,7 @@ void Version::Get(const ReadOptions& options,
assert(comp_sign < 0); assert(comp_sign < 0);
} else { } else {
// level == 0, the current file cannot be newer than the previous one. // level == 0, the current file cannot be newer than the previous one.
if (cfd_->options()->compaction_style == kCompactionStyleUniversal) { assert(!NewestFirstBySeqNo(f, prev_file));
assert(!NewestFirstBySeqNo(f, prev_file));
} else {
assert(!NewestFirst(f, prev_file));
}
} }
} }
prev_file = f; prev_file = f;
@ -1318,22 +1311,18 @@ struct VersionSet::ManifestWriter {
class VersionSet::Builder { class VersionSet::Builder {
private: private:
// Helper to sort v->files_ // Helper to sort v->files_
// kLevel0LevelCompaction -- NewestFirst (also used for FIFO compaction) // kLevel0 -- NewestFirstBySeqNo
// kLevel0UniversalCompaction -- NewestFirstBySeqNo
// kLevelNon0 -- BySmallestKey // kLevelNon0 -- BySmallestKey
struct FileComparator { struct FileComparator {
enum SortMethod { enum SortMethod {
kLevel0LevelCompaction = 0, kLevel0 = 0,
kLevel0UniversalCompaction = 1, kLevelNon0 = 1,
kLevelNon0 = 2,
} sort_method; } sort_method;
const InternalKeyComparator* internal_comparator; const InternalKeyComparator* internal_comparator;
bool operator()(FileMetaData* f1, FileMetaData* f2) const { bool operator()(FileMetaData* f1, FileMetaData* f2) const {
switch (sort_method) { switch (sort_method) {
case kLevel0LevelCompaction: case kLevel0:
return NewestFirst(f1, f2);
case kLevel0UniversalCompaction:
return NewestFirstBySeqNo(f1, f2); return NewestFirstBySeqNo(f1, f2);
case kLevelNon0: case kLevelNon0:
return BySmallestKey(f1, f2, internal_comparator); return BySmallestKey(f1, f2, internal_comparator);
@ -1359,10 +1348,7 @@ class VersionSet::Builder {
Builder(ColumnFamilyData* cfd) : cfd_(cfd), base_(cfd->current()) { Builder(ColumnFamilyData* cfd) : cfd_(cfd), base_(cfd->current()) {
base_->Ref(); base_->Ref();
levels_ = new LevelState[base_->NumberLevels()]; levels_ = new LevelState[base_->NumberLevels()];
level_zero_cmp_.sort_method = level_zero_cmp_.sort_method = FileComparator::kLevel0;
(cfd_->options()->compaction_style == kCompactionStyleUniversal)
? FileComparator::kLevel0UniversalCompaction
: FileComparator::kLevel0LevelCompaction;
level_nonzero_cmp_.sort_method = FileComparator::kLevelNon0; level_nonzero_cmp_.sort_method = FileComparator::kLevelNon0;
level_nonzero_cmp_.internal_comparator = &cfd->internal_comparator(); level_nonzero_cmp_.internal_comparator = &cfd->internal_comparator();
@ -1408,9 +1394,7 @@ class VersionSet::Builder {
auto f2 = v->files_[level][i]; auto f2 = v->files_[level][i];
if (level == 0) { if (level == 0) {
assert(level_zero_cmp_(f1, f2)); assert(level_zero_cmp_(f1, f2));
if (cfd_->options()->compaction_style == kCompactionStyleUniversal) { assert(f1->largest_seqno > f2->largest_seqno);
assert(f1->largest_seqno > f2->largest_seqno);
}
} else { } else {
assert(level_nonzero_cmp_(f1, f2)); assert(level_nonzero_cmp_(f1, f2));