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_

  1. 单例
  2. 创建random-read,sequential-read,common文件
  3. 文件目录增删查改,检测。
  4. 文件锁,锁进程。
  5. 启动线程功能
  6. 新增日志文件

InternalKeyComparator internal_comparator_

  1. internal key的compare()

InternalFilterPolicy internal_filter_policy_

  1. filter policy wrapper that converts from internal keys to user keys

Options options_

  1. overall:Options to control the behavior of a database (passed to DB::Open)
  2. Env *
  3. Logger *
  4. write_buffer_size
  5. max_open_files
  6. block_cache
  7. block_size
  8. CompressionType
  9. FilterPolicy

Table Cache *table_cache_

  1. Env *env_
  2. Options options_
  3. Cache *cache_

Memtable *mem_

  1. KeyComparator comparator_
  2. int refs_
  3. Arena arena_
  4. Table table_

MemTable *imm_

  1. KeyComparator comparator_
  2. int refs_
  3. Arena arena_
  4. Table table_

WriteableFile *log_file_

  1. A file abstraction for sequential writing.
  2. The implementationmust provide buffering since callers may append small fragments at a time to the file.

log::Writer *log_

  1. explicit Writer(WritableFile* dest);
  2. 写日志文件

std::deque<Writer*> writers_

  1. Status status;
  2. WriteBatch *batch;
  3. bool sync;
  4. bool done;
  5. port::CondVar cv;
  6. explicit Writer(port::Mutex* mu) : cv(mu)

WriteBatch *write_batch_

  1. 批量写入
  2. 实际是保存在buffer中,key--value,到达一定数量后,写入

SnapshotList snapshots_

  1. 双向链表,内容是SnapshotImpl list_;
  2. Oldest,Newest

std::set<uint64_t> pending_outputs_

  1. Set of table files to protect from deletion because they are part of ongoing compactions.

ManualCompaction manual_compaction_

  1. 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_

  1. LogAndApply()
  2. Recover()
  3. current()
  4. ManifestFileNumber()
  5. NewFileNumber()
  6. ReuseFileNumber()
  7. NumLevelFiles()
  8. NumLevelBytes()
  9. LastSequence()
  10. LogNumber()
  11. PrevLogNumber()
  12. PickCompaction()
  13. CompactRange()
  14. AddLiveFiles()
  15. LevelSummary()
  16. 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]

  1. Per level compaction stats.
  2. stats_[level] stores the stats for compactions that produced data for the specified "level".
 

LevelDB场景分析2--Open的更多相关文章

  1. LevelDB场景分析1--整体结构分析

    基本用法 数据结构 class DBImpl : public DB { private:     struct CompactionState;     struct Writer;// Infor ...

  2. LevelDB场景分析4--BackgroundCompaction

    1.DBImpl::Open      uint64_t new_log_number = impl->versions_->NewFileNumber();      WritableF ...

  3. TYPESDK手游聚合SDK服务端设计思路与架构之一:应用场景分析

    TYPESDK 服务端设计思路与架构之一:应用场景分析 作为一个渠道SDK统一接入框架,TYPESDK从一开始,所面对的需求场景就是多款游戏,通过一个统一的SDK服务端,能够同时接入几十个甚至几百个各 ...

  4. Oracle dbms_lock.sleep()存储过程使用技巧-场景-分析-实例

    <Oracle dbms_lock.sleep()存储过程使用技巧>-场景-分析-实例 摘要:今天是2014年3月10日,北京,雾霾,下午组织相关部门开会.会议的结尾一名开发工程师找到了我 ...

  5. 理解 python metaclass使用技巧与应用场景分析

    理解python metaclass使用技巧与应用场景分析       参考: decorator与metaclass:http://jfine-python-classes.readthedocs. ...

  6. 数据结构之链表C语言实现以及使用场景分析

    牢骚:本篇博客两个星期前已经存为草稿,鉴于发生一些糟糕的事情,今天才基本完成.本人6月份应届毕业生一枚,毕业后当天来到帝都,之后也非常顺利,面试了俩家公司都成功了.一家做C++方面电商ERP,一家做w ...

  7. mariadb 10 多源复制(Multi-source replication) 业务使用场景分析,及使用方法

    mariadb 10 多源复制(Multi-source replication) 业务使用场景分析,及使用方法 官方mysql一个slave只能对应一个master,mariadb 10开始支持多源 ...

  8. ThreadLocal的理解与应用场景分析

    对于Java ThreadLocal的理解与应用场景分析 一.对ThreadLocal理解 ThreadLocal提供一个方便的方式,可以根据不同的线程存放一些不同的特征属性,可以方便的在线程中进行存 ...

  9. Java 常用List集合使用场景分析

    Java 常用List集合使用场景分析 过年前的最后一篇,本章通过介绍ArrayList,LinkedList,Vector,CopyOnWriteArrayList 底层实现原理和四个集合的区别.让 ...

随机推荐

  1. unity 静态合批

    想做这样一个优化 因为cmd drawcall太多 materials太多导致 实际上只是贴图不一样 想用texture2DArray把他们合起来 texArray这步功能倒是很快就好了 但是从fra ...

  2. IOS开发之新浪微博OAuth2

    说明:微博开放接口的调用,如发微博.关注等,都是需要获取用户身份认证的.目前微博开放平台用户身份鉴权主要采用的是OAuth2.0.为了方便开发者开发.测试自己的应用. OAuth2.0较1.0相比,整 ...

  3. GO语言基础语法

    1. Go项目的目录结构 一般的,一个Go项目在GOPATH下,会有如下三个目录: project   --- bin   --- pkg   --- src 其中,bin 存放编译后的可执行文件:p ...

  4. linux安装scikit-learn

    原文:http://www.cnblogs.com/cyttina/archive/2013/06/08/3127345.html ubuntu的看官方的文档就好了. http://scikit-le ...

  5. [Algorithm] Meeting hour optimization (Kanpsack problem) and Dynamic programming

    For example we have array of meeting objects: const data = [ { name: }, { name: }, { name: }, { name ...

  6. windows 用户变量和系统变量的差别

    点击"我的电脑→属性→高级系统设置"标签的"环境变量"button,出现"环境变量"对话框,假设当前是以Administrator登录系统的 ...

  7. 整数划分问题--DFS

    单点时限:1000ms 内存限制:256MB 描写叙述 Given two positive integers N and M, please divide N into several intege ...

  8. c语言訪问excel

    直接通过格式化读取文件就可实现,见附件

  9. 深夜闲聊节目:华为 Mate7的指纹识别安全么?

    许久没有写过不论什么东西,近期非常忙并且还要准备找工作之类的,唉... ....今天的文章也不说技术,仅仅是闲聊. 一.手机指纹识别一揽 打开非常多站点.论坛的科技栏目,充斥着各种手机讯息!仿佛手机已 ...

  10. Codeforces#86D Powerful array(分块暴力)

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...