1.源码
1 Status DB::Open(const Options& options, const std::string& dbname,
2 DB** dbptr) {
3 *dbptr = NULL;
4
5 DBImpl* impl = new DBImpl(options, dbname);
6 impl->mutex_.Lock();
7 VersionEdit edit;
8 Status s = impl->Recover(&edit); // Handles create_if_missing, error_if_exists
9 if (s.ok()) {
10 uint64_t new_log_number = impl->versions_->NewFileNumber();
11 WritableFile* lfile;
12 s = options.env->NewWritableFile(LogFileName(dbname, new_log_number),
13 &lfile);
14 if (s.ok()) {
15 edit.SetLogNumber(new_log_number);
16 impl->logfile_ = lfile;
17 impl->logfile_number_ = new_log_number;
18 impl->log_ = new log::Writer(lfile);
19 s = impl->versions_->LogAndApply(&edit, &impl->mutex_);
20 }
21 if (s.ok()) {
22 impl->DeleteObsoleteFiles();
23 impl->MaybeScheduleCompaction();
24 }
25 }
26 impl->mutex_.Unlock();
27 if (s.ok()) {
28 *dbptr = impl;
29 } else {
30 delete impl;
31 }
32 return s;
33 }
2.DBImpl::DBImpl
1 DBImpl::DBImpl(const Options& raw_options, const std::string& dbname)
2 : env_(raw_options.env),
3 internal_comparator_(raw_options.comparator),
4 internal_filter_policy_(raw_options.filter_policy),
5 options_(SanitizeOptions(dbname, &internal_comparator_,
6 &internal_filter_policy_, raw_options)),
7 owns_info_log_(options_.info_log != raw_options.info_log),
8 owns_cache_(options_.block_cache != raw_options.block_cache),
9 dbname_(dbname),
10 db_lock_(NULL),
11 shutting_down_(NULL),
12 bg_cv_(&mutex_),
13 mem_(new MemTable(internal_comparator_)),
14 imm_(NULL),
15 logfile_(NULL),
16 logfile_number_(0),
17 log_(NULL),
18 seed_(0),
19 tmp_batch_(new WriteBatch),
20 bg_compaction_scheduled_(false),
21 manual_compaction_(NULL) {
22 mem_->Ref();
23 has_imm_.Release_Store(NULL);
24
25 // Reserve ten files or so for other uses and give the rest to TableCache.
26 const int table_cache_size = options_.max_open_files - kNumNonTableCacheFiles;
27 table_cache_ = new TableCache(dbname_, &options_, table_cache_size);
28
29 versions_ = new VersionSet(dbname_, &options_, table_cache_,
30 &internal_comparator_);
31 }
Env *env_
- 单例
- 创建random-read,sequential-read,common文件
- 文件目录增删查改,检测。
- 文件锁,锁进程。
- 启动线程功能
- 新增日志文件
InternalKeyComparator internal_comparator_
- internal key的compare()
InternalFilterPolicy internal_filter_policy_
- filter policy wrapper that converts from internal keys to user keys
Options options_
- overall:Options to control the behavior of a database (passed to DB::Open)
- Env *
- Logger *
- write_buffer_size
- max_open_files
- block_cache
- block_size
- CompressionType
- FilterPolicy
Table Cache *table_cache_
- Env *env_
- Options options_
- Cache *cache_
Memtable *mem_
- KeyComparator comparator_
- int refs_
- Arena arena_
- Table table_
MemTable *imm_
- KeyComparator comparator_
- int refs_
- Arena arena_
- Table table_
WriteableFile *log_file_
- A file abstraction for sequential writing.
- The implementationmust provide buffering since callers may append small fragments at a time to the file.
log::Writer *log_
- explicit Writer(WritableFile* dest);
- 写日志文件
std::deque<Writer*> writers_
- Status status;
- WriteBatch *batch;
- bool sync;
- bool done;
- port::CondVar cv;
- explicit Writer(port::Mutex* mu) : cv(mu)
WriteBatch *write_batch_
- 批量写入
- 实际是保存在buffer中,key--value,到达一定数量后,写入
SnapshotList snapshots_
- 双向链表,内容是SnapshotImpl list_;
- Oldest,Newest
std::set<uint64_t> pending_outputs_
- Set of table files to protect from deletion because they are part of ongoing compactions.
ManualCompaction manual_compaction_
- struct ManualCompaction {
int level;bool done;
const InternalKey* begin; // NULL means beginning of key range
const InternalKey* end; // NULL means end of key range
InternalKey tmp_storage; // Used to keep track of compaction progress
};
VersionSet *versions_
- LogAndApply()
- Recover()
- current()
- ManifestFileNumber()
- NewFileNumber()
- ReuseFileNumber()
- NumLevelFiles()
- NumLevelBytes()
- LastSequence()
- LogNumber()
- PrevLogNumber()
- PickCompaction()
- CompactRange()
- AddLiveFiles()
- LevelSummary()
- Env* const env_;
const std::string dbname_;const Options* const options_;
TableCache* const table_cache_;
const InternalKeyComparator icmp_;
uint64_t next_file_number_;
uint64_t manifest_file_number_;
uint64_t last_sequence_;
uint64_t log_number_;
uint64_t prev_log_number_; // 0 or backing store for memtable being compacted
// Opened lazily
WritableFile* descriptor_file_;
log::Writer* descriptor_log_;
Version dummy_versions_; // Head of circular doubly-linked list of versions.
Version* current_; // == dummy_versions_.prev_
// Per-level key at which the next compaction at that level should start.
// Either an empty string, or a valid InternalKey.
std::string compact_pointer_[config::kNumLevels];
CompactionState states_[config::kNumLevels]
- Per level compaction stats.
- stats_[level] stores the stats for compactions that produced data for the specified "level".