LevelDB场景分析2--Open
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()) {
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);
s = impl->versions_->LogAndApply(&edit, &impl->mutex_);
}
if (s.ok()) {
impl->DeleteObsoleteFiles();
impl->MaybeScheduleCompaction();
}
}
impl->mutex_.Unlock();
if (s.ok()) {
*dbptr = impl;
} else {
delete impl;
}
return s;
}
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),
db_lock_(NULL),
shutting_down_(NULL),
bg_cv_(&mutex_),
mem_(new MemTable(internal_comparator_)),
imm_(NULL),
logfile_(NULL),
logfile_number_(),
log_(NULL),
seed_(),
tmp_batch_(new WriteBatch),
bg_compaction_scheduled_(false),
manual_compaction_(NULL) {
mem_->Ref();
has_imm_.Release_Store(NULL);
// Reserve ten files or so for other uses and give the rest to TableCache.
const int table_cache_size = options_.max_open_files - kNumNonTableCacheFiles;
table_cache_ = new TableCache(dbname_, &options_, table_cache_size);
versions_ = new VersionSet(dbname_, &options_, table_cache_,
&internal_comparator_);
}
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 rangeconst InternalKey* end; // NULL means end of key rangeInternalKey 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 lazilyWritableFile* 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".
LevelDB场景分析2--Open的更多相关文章
- LevelDB场景分析1--整体结构分析
基本用法 数据结构 class DBImpl : public DB { private: struct CompactionState; struct Writer;// Infor ...
- LevelDB场景分析4--BackgroundCompaction
1.DBImpl::Open uint64_t new_log_number = impl->versions_->NewFileNumber(); WritableF ...
- TYPESDK手游聚合SDK服务端设计思路与架构之一:应用场景分析
TYPESDK 服务端设计思路与架构之一:应用场景分析 作为一个渠道SDK统一接入框架,TYPESDK从一开始,所面对的需求场景就是多款游戏,通过一个统一的SDK服务端,能够同时接入几十个甚至几百个各 ...
- Oracle dbms_lock.sleep()存储过程使用技巧-场景-分析-实例
<Oracle dbms_lock.sleep()存储过程使用技巧>-场景-分析-实例 摘要:今天是2014年3月10日,北京,雾霾,下午组织相关部门开会.会议的结尾一名开发工程师找到了我 ...
- 理解 python metaclass使用技巧与应用场景分析
理解python metaclass使用技巧与应用场景分析 参考: decorator与metaclass:http://jfine-python-classes.readthedocs. ...
- 数据结构之链表C语言实现以及使用场景分析
牢骚:本篇博客两个星期前已经存为草稿,鉴于发生一些糟糕的事情,今天才基本完成.本人6月份应届毕业生一枚,毕业后当天来到帝都,之后也非常顺利,面试了俩家公司都成功了.一家做C++方面电商ERP,一家做w ...
- mariadb 10 多源复制(Multi-source replication) 业务使用场景分析,及使用方法
mariadb 10 多源复制(Multi-source replication) 业务使用场景分析,及使用方法 官方mysql一个slave只能对应一个master,mariadb 10开始支持多源 ...
- ThreadLocal的理解与应用场景分析
对于Java ThreadLocal的理解与应用场景分析 一.对ThreadLocal理解 ThreadLocal提供一个方便的方式,可以根据不同的线程存放一些不同的特征属性,可以方便的在线程中进行存 ...
- Java 常用List集合使用场景分析
Java 常用List集合使用场景分析 过年前的最后一篇,本章通过介绍ArrayList,LinkedList,Vector,CopyOnWriteArrayList 底层实现原理和四个集合的区别.让 ...
随机推荐
- Best Time to Buy and Sell Stock III leetcode java
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- Jump Game II leetcode java
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 微软 WCF的几种寄宿方式,寄宿IIS、寄宿winform、寄宿控制台、寄宿Windows服务
WCF寄宿方式是一种非常灵活的操作,可以在IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为调用者方便.高效提供服务调用.本文分别对这几种方式进行详 ...
- 超级简单的jquery操作表格(添加/删除行、添加/删除列)
利用jquery给指定的table添加一行.删除一行 <script language="javascript" src="./jquery.js"> ...
- VB.NET与C# 语法区别展示
在学习VB.NET后发现,VB.NET与C#的语法主要的不同在两个部分,这两部分搞通了,那就游刃有余,迎刃而解了.现将其对比总结如下: 一.实体部分 (与VB相比,在C#和VB.NET中,实体的使用很 ...
- EasyUI-Tooltip(提示框)学习
引子: if($("#BLUETOOTH_a")){ $("#BLUETOOTH_a").tooltip({ position: 'right', conten ...
- PS使用技巧
1. 3.缩放工具:工具栏上放大镜图标:<1>上面图标栏可以选择激活放大还是缩小.<2>按住alt键可以切换,可以点击,也可以滚动滚轮<3>ctrl加"+ ...
- eclipse启动优化文章集合
1. eclipse启动优化,终于不那么卡了! http://www.cfei.net/archives/445
- Linux下面kettle的部署
一直以来服务器是linux系统,但是感觉linux图形化不强,于是从接触kettle以来都是在windows系统操作ETL的设计和处理.现在需要在linux中查看一下kettle资源库是否连接正常,以 ...
- Angular报错
报错: Module 'App' is not available! You either misspelled the module name or forgot to load it. If re ...