|
@ -138,7 +138,7 @@ bool SomeFileOverlapsRange(const InternalKeyComparator& icmp, |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Binary search over file list
|
|
|
|
|
|
|
|
|
// Binary search over file list, 保证SSTable有序
|
|
|
uint32_t index = 0; |
|
|
uint32_t index = 0; |
|
|
if (smallest_user_key != nullptr) { |
|
|
if (smallest_user_key != nullptr) { |
|
|
// Find the earliest possible internal key for smallest_user_key
|
|
|
// Find the earliest possible internal key for smallest_user_key
|
|
@ -351,6 +351,7 @@ Status Version::Get(const ReadOptions& options, const LookupKey& k, |
|
|
state->last_file_read = f; |
|
|
state->last_file_read = f; |
|
|
state->last_file_read_level = level; |
|
|
state->last_file_read_level = level; |
|
|
|
|
|
|
|
|
|
|
|
// if(state->stats->now_ts > f->newer_ts)return false;
|
|
|
state->s = state->vset->table_cache_->Get(*state->options, f->number, |
|
|
state->s = state->vset->table_cache_->Get(*state->options, f->number, |
|
|
f->file_size, state->ikey, |
|
|
f->file_size, state->ikey, |
|
|
&state->saver, SaveValue); |
|
|
&state->saver, SaveValue); |
|
@ -460,7 +461,7 @@ void Version::Unref() { |
|
|
delete this; |
|
|
delete this; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//files_[level] 中存在至少一个文件的键范围与 [smallest_user_key, largest_user_key] 有重叠,那么 OverlapInLevel 就会返回 true。
|
|
|
bool Version::OverlapInLevel(int level, const Slice* smallest_user_key, |
|
|
bool Version::OverlapInLevel(int level, const Slice* smallest_user_key, |
|
|
const Slice* largest_user_key) { |
|
|
const Slice* largest_user_key) { |
|
|
return SomeFileOverlapsRange(vset_->icmp_, (level > 0), files_[level], |
|
|
return SomeFileOverlapsRange(vset_->icmp_, (level > 0), files_[level], |
|
@ -470,7 +471,7 @@ bool Version::OverlapInLevel(int level, const Slice* smallest_user_key, |
|
|
int Version::PickLevelForMemTableOutput(const Slice& smallest_user_key, |
|
|
int Version::PickLevelForMemTableOutput(const Slice& smallest_user_key, |
|
|
const Slice& largest_user_key) { |
|
|
const Slice& largest_user_key) { |
|
|
int level = 0; |
|
|
int level = 0; |
|
|
if (!OverlapInLevel(0, &smallest_user_key, &largest_user_key)) { |
|
|
|
|
|
|
|
|
if (!OverlapInLevel(0, &smallest_user_key, &largest_user_key)) {//当与level0没有重叠时,直接选择压入其他层
|
|
|
// Push to next level if there is no overlap in next level,
|
|
|
// Push to next level if there is no overlap in next level,
|
|
|
// and the #bytes overlapping in the level after that are limited.
|
|
|
// and the #bytes overlapping in the level after that are limited.
|
|
|
InternalKey start(smallest_user_key, kMaxSequenceNumber, kValueTypeForSeek); |
|
|
InternalKey start(smallest_user_key, kMaxSequenceNumber, kValueTypeForSeek); |
|
@ -664,7 +665,7 @@ class VersionSet::Builder { |
|
|
if (f->allowed_seeks < 100) f->allowed_seeks = 100; |
|
|
if (f->allowed_seeks < 100) f->allowed_seeks = 100; |
|
|
|
|
|
|
|
|
levels_[level].deleted_files.erase(f->number); |
|
|
levels_[level].deleted_files.erase(f->number); |
|
|
levels_[level].added_files->insert(f); |
|
|
|
|
|
|
|
|
levels_[level].added_files->insert(f);//按照最小key排序
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -795,7 +796,7 @@ Status VersionSet::LogAndApply(VersionEdit* edit, port::Mutex* mu) { |
|
|
builder.Apply(edit); |
|
|
builder.Apply(edit); |
|
|
builder.SaveTo(v); |
|
|
builder.SaveTo(v); |
|
|
} |
|
|
} |
|
|
Finalize(v); |
|
|
|
|
|
|
|
|
Finalize(v);//对于每层compact打分
|
|
|
|
|
|
|
|
|
// Initialize new descriptor log file if necessary by creating
|
|
|
// Initialize new descriptor log file if necessary by creating
|
|
|
// a temporary file that contains a snapshot of the current version.
|
|
|
// a temporary file that contains a snapshot of the current version.
|
|
@ -820,7 +821,7 @@ Status VersionSet::LogAndApply(VersionEdit* edit, port::Mutex* mu) { |
|
|
// Write new record to MANIFEST log
|
|
|
// Write new record to MANIFEST log
|
|
|
if (s.ok()) { |
|
|
if (s.ok()) { |
|
|
std::string record; |
|
|
std::string record; |
|
|
edit->EncodeTo(&record); |
|
|
|
|
|
|
|
|
edit->EncodeTo(&record);// TODO:修改
|
|
|
s = descriptor_log_->AddRecord(record); |
|
|
s = descriptor_log_->AddRecord(record); |
|
|
if (s.ok()) { |
|
|
if (s.ok()) { |
|
|
s = descriptor_file_->Sync(); |
|
|
s = descriptor_file_->Sync(); |
|
@ -1386,7 +1387,7 @@ void VersionSet::SetupOtherInputs(Compaction* c) { |
|
|
const int level = c->level(); |
|
|
const int level = c->level(); |
|
|
InternalKey smallest, largest; |
|
|
InternalKey smallest, largest; |
|
|
|
|
|
|
|
|
AddBoundaryInputs(icmp_, current_->files_[level], &c->inputs_[0]); |
|
|
|
|
|
|
|
|
AddBoundaryInputs(icmp_, current_->files_[level], &c->inputs_[0]);//增加边界值,userkey相等的情况
|
|
|
GetRange(c->inputs_[0], &smallest, &largest); |
|
|
GetRange(c->inputs_[0], &smallest, &largest); |
|
|
|
|
|
|
|
|
current_->GetOverlappingInputs(level + 1, &smallest, &largest, |
|
|
current_->GetOverlappingInputs(level + 1, &smallest, &largest, |
|
@ -1444,11 +1445,11 @@ void VersionSet::SetupOtherInputs(Compaction* c) { |
|
|
compact_pointer_[level] = largest.Encode().ToString(); |
|
|
compact_pointer_[level] = largest.Encode().ToString(); |
|
|
c->edit_.SetCompactPointer(level, largest); |
|
|
c->edit_.SetCompactPointer(level, largest); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//得到基于当前level的所涉及的需要compact的文件,(level0可能会涉及到level1等)
|
|
|
Compaction* VersionSet::CompactRange(int level, const InternalKey* begin, |
|
|
Compaction* VersionSet::CompactRange(int level, const InternalKey* begin, |
|
|
const InternalKey* end) { |
|
|
const InternalKey* end) { |
|
|
std::vector<FileMetaData*> inputs; |
|
|
std::vector<FileMetaData*> inputs; |
|
|
current_->GetOverlappingInputs(level, begin, end, &inputs); |
|
|
|
|
|
|
|
|
current_->GetOverlappingInputs(level, begin, end, &inputs);//得到一层的input
|
|
|
if (inputs.empty()) { |
|
|
if (inputs.empty()) { |
|
|
return nullptr; |
|
|
return nullptr; |
|
|
} |
|
|
} |
|
|