当前位置:网站首页>Leveldb source code analysis -- open the database
Leveldb source code analysis -- open the database
2022-06-24 17:23:00 【Xiao Lin Gang】
principle
Open in analysis leveldb Before the database , Let's discuss some similar sub problems first :
- How to restore the running state of a process ?
- How to solve the problem of slow data indexing ?
Main completed items :
- Build... In storage MemTable data structure ;
- load SSTable File search related index information ;
- Replay unfinished WAL journal ;
Open database
Status DB::Open(const Options& options, const std::string& dbname, DB** dbptr) {
*dbptr = nullptr;
DBImpl* impl = new DBImpl(options, dbname);
impl->mutex_.Lock();
VersionEdit edit;
// Recover handles create_if_missing, error_if_exists
bool save_manifest = false;
Status s = impl->Recover(&edit, &save_manifest);
if (s.ok() && impl->mem_ == nullptr) {
// Create new log and a corresponding memtable.
uint64_t new_log_number = impl->versions_->NewFileNumber();
WritableFile* lfile;
s = options.env->NewWritableFile(LogFileName(dbname, new_log_number),
&lfile);
if (s.ok()) {
edit.SetLogNumber(new_log_number);
impl->logfile_ = lfile;
impl->logfile_number_ = new_log_number;
impl->log_ = new log::Writer(lfile);
impl->mem_ = new MemTable(impl->internal_comparator_);
impl->mem_->Ref();
}
}
if (s.ok() && save_manifest) {
edit.SetPrevLogNumber(0); // No older logs needed after recovery.
edit.SetLogNumber(impl->logfile_number_);
s = impl->versions_->LogAndApply(&edit, &impl->mutex_);
}
if (s.ok()) {
impl->RemoveObsoleteFiles();
impl->MaybeScheduleCompaction();
}
impl->mutex_.Unlock();
if (s.ok()) {
assert(impl->mem_ != nullptr);
*dbptr = impl;
} else {
delete impl;
}
return s;
}Recover database
Status DBImpl::Recover(VersionEdit* edit, bool* save_manifest) {
// Create database directory
env_->CreateDir(dbname_);
// Get file lock , Prohibit multiple processes from accessing the database at the same time
Status s = env_->LockFile(LockFileName(dbname_), &db_lock_);
if (!env_->FileExists(CurrentFileName(dbname_))) {
if (options_.create_if_missing) {
// Such as CURRENT file does not exist , Then initialize the database file
s = NewDB();
}
}
// Load database version management information
s = versions_->Recover(save_manifest);
SequenceNumber max_sequence(0);
const uint64_t min_log = versions_->LogNumber();
const uint64_t prev_log = versions_->PrevLogNumber();
std::vector<std::string> filenames;
s = env_->GetChildren(dbname_, &filenames);
std::set<uint64_t> expected;
versions_->AddLiveFiles(&expected);
uint64_t number;
FileType type;
std::vector<uint64_t> logs;
for (size_t i = 0; i < filenames.size(); i++) {
if (ParseFileName(filenames[i], &number, &type)) {
expected.erase(number);
if (type == kLogFile && ((number >= min_log) || (number == prev_log)))
logs.push_back(number);
}
}
// Recover in the order in which the logs were generated
std::sort(logs.begin(), logs.end());
for (size_t i = 0; i < logs.size(); i++) {
s = RecoverLogFile(logs[i], (i == logs.size() - 1), save_manifest, edit,
&max_sequence);
// The previous incarnation may not have written any MANIFEST
// records after allocating this log number. So we manually
// update the file number allocation counter in VersionSet.
versions_->MarkFileNumberUsed(logs[i]);
}
if (versions_->LastSequence() < max_sequence) {
versions_->SetLastSequence(max_sequence);
}
return Status::OK();
}Initialize database file ( Called the first time data is created )
Status DBImpl::NewDB() {
VersionEdit new_db;
// Save the name of the comparator
new_db.SetComparatorName(user_comparator()->Name());
// The assigned log file number is 0
new_db.SetLogNumber(0);
// Set the next document number to be assigned as 2(1 Assigned to the MANIFEST file )
new_db.SetNextFile(2);
// Set last sequence The version number is 0
new_db.SetLastSequence(0);
const std::string manifest = DescriptorFileName(dbname_, 1);
WritableFile* file;
Status s = env_->NewWritableFile(manifest, &file);
{
log::Writer log(file);
std::string record;
new_db.EncodeTo(&record);
s = log.AddRecord(record);
if (s.ok()) {
s = file->Sync();
}
}
if (s.ok()) {
// to update CURRENT file , Keep the latest MANIFEST File name
s = SetCurrentFile(env_, dbname_, 1);
}
return s;
}边栏推荐
- Analysis of software supply chain attack package preemption low cost phishing
- Contributed code to famous projects for the first time, a little nervous
- Cloud native monitoring practice (2) monitoring and collection of components outside the TKE cluster
- zblog系统实现前台调用当天发布文章数量的教程
- 网站SEO排名越做越差是什么原因造成的?
- 主链系统发展解析
- Future banks need to think about today's structure with tomorrow's thinking
- Can you remember the code of a programming boss? Can you hit it out without Baidu?
- What securities dealers recommend? Is it safe to open an account online now?
- Several schemes of traffic exposure in kubernetes cluster
猜你喜欢

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)

MySQL learning -- table structure of SQL test questions
Using consistent hash algorithm in Presto to enhance the data cache locality of dynamic clusters
![[leetcode108] convert an ordered array into a binary search tree (medium order traversal)](/img/e1/0fac59a531040d74fd7531e2840eb5.jpg)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)

Why do you develop middleware when you are young? "You can choose your own way"
随机推荐
How to compile and debug go runtime source code
Cloud native monitoring practice (2) monitoring and collection of components outside the TKE cluster
Yupi made an AI programming nickname generator!
Why do you develop middleware when you are young? "You can choose your own way"
2021-04-02: given a square or rectangular matrix, zigzag printing can be realized.
The RTSP video image intelligent analysis platform easynvr cascades to the superior platform through the national standard for playback optimization
How to customize the log output format of zap?
A tutorial on how the zblog system obtains user related information based on user ID
[MySQL practice] binlog, a sharp tool for problem analysis
How Tencent cloud es achieves cross cluster data copy & lt through reindex; Lower & gt;
Elastic searchable snapshot function (frozen Tier 3)
This time, talk about the dry goods of industrial Internet | TVP technology closed door meeting
Try catch finally implementation mechanism
On N handshakes and M waves of TCP
Live broadcast Preview - on April 1, I made an appointment with you to explore tcapulusdb with Tencent cloud
Will the easycvr video channel of the urban intelligent video monitoring image analysis platform occupy bandwidth after stopping playing?
C language | logical operators
Zblog determines whether a plug-in installs the enabled built-in function code
Go kit microservice integrates Promtheus to solve monitoring alarm problems
Customizing security groups using BPF