当前位置:网站首页>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;
}边栏推荐
- The RTSP video image intelligent analysis platform easynvr cascades to the superior platform through the national standard for playback optimization
- AFG EDI requirements details
- See through the new financial report of Tencent music, online music needs b+c
- Will the easycvr video channel of the urban intelligent video monitoring image analysis platform occupy bandwidth after stopping playing?
- Pagoda activities, team members can enjoy a lightweight server 1 core 2g5m 28 yuan for two years
- VBA Daniel used the nested loop
- Go kit microservice integrates Promtheus to solve monitoring alarm problems
- Can you remember the code of a programming boss? Can you hit it out without Baidu?
- Yiwen teaches you to understand the stack operation in go
- Implement typescript runtime type checking
猜你喜欢

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

Why do you develop middleware when you are young? "You can choose your own way"
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)

MySQL learning -- table structure of SQL test questions
随机推荐
AFG EDI requirements details
Cloud native monitoring practice (2) monitoring and collection of components outside the TKE cluster
CentOS 7 installing SQL server2017 (Linux)
Research on clock synchronization performance monitoring system based on 1588v2 Technology
FPGA systematic learning notes serialization_ Day10 [sequential logic, competitive adventure, synchronous reset, asynchronous reset]
5g brings opportunities and challenges. Are you ready to defend against DDoS?
Markdown syntax -- Formula
New MySQL 8.0 feature - enhanced logical backup recovery
Devops in digital transformation digital risk
Solution to the problem that kibana's map cannot render longitude and latitude coordinate data
TRCT test cloud + article online speed
TVP experts talk about geese factory middleware: innovating forward and meeting the future
To redefine the storage architecture, Huawei has used more than five "cores"
FPGA systematic learning notes serialization_ Day8 [design of 4-bit multiplier and 4-bit divider]
How does the easynvr/easygbs live video platform use Wireshark to capture and analyze data locally?
Install Clickhouse client code 210 connection referred (localhost:9000)
Radiology: contralateral preoperative resting state MRI functional network integration is related to the surgical results of temporal lobe epilepsy
What is the reason for the worse website SEO ranking?
VBA Daniel used the nested loop
Management system permission design