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 底层实现原理和四个集合的区别.让 ...
随机推荐
- magento upsell from cur_category
<?php /** * Magento * * NOTICE OF LICENSE * * This source file is subject to the Academic Free Li ...
- Metronic V1.5.2 Responsive Admin Dashboard Template build with Twitter Bootstrap 3.0
Template Name: Metronic - Responsive Admin Dashboard Template build with Twitter Bootstrap 3.0 Versi ...
- IOS NSString 用法详解
[cpp] view plain copy //NSString 操作均不改变自身值 //构建字符串 NSString *szTmp = @"A string"; ...
- 火狐浏览器FireFox 如何将整个网页保存为图片
使用Friefox的Pearl Cresent Page Saver插件 如图所示网页有很长的滚动条 点击右下角的该插件选项,将整个页面保存为图片 在桌面上得到了这样一个文件,大小是1263×6083 ...
- STL - 容器 - Array
Array是C++ 11给STL新增加的容器 ArrayTest.cpp #include <array> #include <algorithm> #include < ...
- svg translate 操作
function dragElement(evt) { var target = evt.target; var id = target.id; var dx = evt.dx, dy = evt.d ...
- Spring boot 与quart集成并在Job中注入服务
1:AutowiringSpringBeanJobFactory.java package com.microwisdom.grgzpt.jobs; import org.quartz.spi.Tri ...
- PACS系统简易
PACS系统 http://baike.baidu.com/link?url=prHBMbyu5W98ET1UGQ0PXXxLebxAeljckFH0pfO_2aODe1UgsrWgRd4Unbopt ...
- POJ - 1325 Machine Schedule 二分图 最小点覆盖
题目大意:有两个机器,A机器有n种工作模式,B机器有m种工作模式,刚開始两个机器都是0模式.假设要切换模式的话,机器就必须的重新启动 有k个任务,每一个任务都能够交给A机器的i模式或者B机器的j模式完 ...
- java正则表达式简介
Java的正则表达式讲解:(为了能看清,本文正则表达式用中文的句号代替英文句点) 1 英文句点符号:匹配单个任意字符. eg: 表达式”t.o 可以匹配:tno,t#o,teo等等.不可以匹配:tn ...