This commit is contained in:
baiqiubai 2025-03-05 04:46:06 +00:00 committed by GitHub
commit 10aa226e00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 31 deletions

View File

@ -85,20 +85,31 @@ Version::~Version() {
} }
int FindFile(const InternalKeyComparator& icmp, int FindFile(const InternalKeyComparator& icmp,
const std::vector<FileMetaData*>& files, const Slice& key) { const std::vector<FileMetaData*>& files, const Slice& key,
bool use_user_comparator) {
uint32_t left = 0; uint32_t left = 0;
uint32_t right = files.size(); uint32_t right = files.size();
const Comparator* user_cmp = icmp.user_comparator();
while (left < right) { while (left < right) {
uint32_t mid = (left + right) / 2; uint32_t mid = (left + right) / 2;
const FileMetaData* f = files[mid]; const FileMetaData* f = files[mid];
if (icmp.InternalKeyComparator::Compare(f->largest.Encode(), key) < 0) { if (use_user_comparator) {
// Key at "mid.largest" is < "target". Therefore all if (user_cmp->Compare(f->largest.user_key(), key) < 0) {
// files at or before "mid" are uninteresting. left = mid + 1;
left = mid + 1; } else {
right = mid;
}
} else { } else {
// Key at "mid.largest" is >= "target". Therefore all files if (icmp.InternalKeyComparator::Compare(f->largest.Encode(), key) < 0) {
// after "mid" are uninteresting. // Key at "mid.largest" is < "target". Therefore all
right = mid; // files at or before "mid" are uninteresting.
left = mid + 1;
} else {
// Key at "mid.largest" is >= "target". Therefore all files
// after "mid" are uninteresting.
right = mid;
}
} }
} }
return right; return right;
@ -1485,9 +1496,6 @@ Compaction::Compaction(const Options* options, int level)
grandparent_index_(0), grandparent_index_(0),
seen_key_(false), seen_key_(false),
overlapped_bytes_(0) { overlapped_bytes_(0) {
for (int i = 0; i < config::kNumLevels; i++) {
level_ptrs_[i] = 0;
}
} }
Compaction::~Compaction() { Compaction::~Compaction() {
@ -1519,17 +1527,10 @@ bool Compaction::IsBaseLevelForKey(const Slice& user_key) {
const Comparator* user_cmp = input_version_->vset_->icmp_.user_comparator(); const Comparator* user_cmp = input_version_->vset_->icmp_.user_comparator();
for (int lvl = level_ + 2; lvl < config::kNumLevels; lvl++) { for (int lvl = level_ + 2; lvl < config::kNumLevels; lvl++) {
const std::vector<FileMetaData*>& files = input_version_->files_[lvl]; const std::vector<FileMetaData*>& files = input_version_->files_[lvl];
while (level_ptrs_[lvl] < files.size()) { int num = FindFile(input_version_->vset_->icmp_, files, user_key, true);
FileMetaData* f = files[level_ptrs_[lvl]]; if (num < files.size()) {
if (user_cmp->Compare(user_key, f->largest.user_key()) <= 0) { if (user_cmp->Compare(user_key, files[num]->smallest.user_key()) >= 0)
// We've advanced far enough return false;
if (user_cmp->Compare(user_key, f->smallest.user_key()) >= 0) {
// Key falls in this file's range, so definitely not base level
return false;
}
break;
}
level_ptrs_[lvl]++;
} }
} }
return true; return true;

View File

@ -43,7 +43,8 @@ class WritableFile;
// Return files.size() if there is no such file. // Return files.size() if there is no such file.
// REQUIRES: "files" contains a sorted list of non-overlapping files. // REQUIRES: "files" contains a sorted list of non-overlapping files.
int FindFile(const InternalKeyComparator& icmp, int FindFile(const InternalKeyComparator& icmp,
const std::vector<FileMetaData*>& files, const Slice& key); const std::vector<FileMetaData*>& files, const Slice& key,
bool use_user_comparator = false);
// Returns true iff some file in "files" overlaps the user key range // Returns true iff some file in "files" overlaps the user key range
// [*smallest,*largest]. // [*smallest,*largest].
@ -378,14 +379,6 @@ class Compaction {
bool seen_key_; // Some output key has been seen bool seen_key_; // Some output key has been seen
int64_t overlapped_bytes_; // Bytes of overlap between current output int64_t overlapped_bytes_; // Bytes of overlap between current output
// and grandparent files // and grandparent files
// State for implementing IsBaseLevelForKey
// level_ptrs_ holds indices into input_version_->levels_: our state
// is that we are positioned at one of the file ranges for each
// higher level than the ones involved in this compaction (i.e. for
// all L >= level_ + 2).
size_t level_ptrs_[config::kNumLevels];
}; };
} // namespace leveldb } // namespace leveldb