1
0
mirror of https://github.com/google/leveldb.git synced 2025-04-25 14:00:27 +08:00
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,
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 right = files.size();
const Comparator* user_cmp = icmp.user_comparator();
while (left < right) {
uint32_t mid = (left + right) / 2;
const FileMetaData* f = files[mid];
if (icmp.InternalKeyComparator::Compare(f->largest.Encode(), key) < 0) {
// Key at "mid.largest" is < "target". Therefore all
// files at or before "mid" are uninteresting.
left = mid + 1;
if (use_user_comparator) {
if (user_cmp->Compare(f->largest.user_key(), key) < 0) {
left = mid + 1;
} else {
right = mid;
}
} else {
// Key at "mid.largest" is >= "target". Therefore all files
// after "mid" are uninteresting.
right = mid;
if (icmp.InternalKeyComparator::Compare(f->largest.Encode(), key) < 0) {
// Key at "mid.largest" is < "target". Therefore all
// 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;
@ -1485,9 +1496,6 @@ Compaction::Compaction(const Options* options, int level)
grandparent_index_(0),
seen_key_(false),
overlapped_bytes_(0) {
for (int i = 0; i < config::kNumLevels; i++) {
level_ptrs_[i] = 0;
}
}
Compaction::~Compaction() {
@ -1519,17 +1527,10 @@ bool Compaction::IsBaseLevelForKey(const Slice& user_key) {
const Comparator* user_cmp = input_version_->vset_->icmp_.user_comparator();
for (int lvl = level_ + 2; lvl < config::kNumLevels; lvl++) {
const std::vector<FileMetaData*>& files = input_version_->files_[lvl];
while (level_ptrs_[lvl] < files.size()) {
FileMetaData* f = files[level_ptrs_[lvl]];
if (user_cmp->Compare(user_key, f->largest.user_key()) <= 0) {
// We've advanced far enough
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]++;
int num = FindFile(input_version_->vset_->icmp_, files, user_key, true);
if (num < files.size()) {
if (user_cmp->Compare(user_key, files[num]->smallest.user_key()) >= 0)
return false;
}
}
return true;

View File

@ -43,7 +43,8 @@ class WritableFile;
// Return files.size() if there is no such file.
// REQUIRES: "files" contains a sorted list of non-overlapping files.
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
// [*smallest,*largest].
@ -378,14 +379,6 @@ class Compaction {
bool seen_key_; // Some output key has been seen
int64_t overlapped_bytes_; // Bytes of overlap between current output
// 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