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. python内置函数和魔法函数

    内置方法:Python中声明每一个类系统都会加上一些默认内置方法,提供给系统调用该类的对象时使用.比如需要实例化一个对象时,需要调用该类的init方法:使用print去打印一个类时,其实调用的是str ...

  2. js常用的事件对象

    事件源对象  event.srcElement.tagName  event.srcElement.type 捕获释放  event.srcElement.setCapture();   event. ...

  3. HTML-<td> 标签的 valign 属性

    <html> <body> <table border="1" style="height:200px"> <tr&g ...

  4. 关于json与protobuf的材料

    1. https://solicomo.com/network-dev/protobuf-proto3-vs-proto2.html 2.

  5. GeSHi Documentation

    GeSHi Documentation Version 1.0.8.11 Authors: © 2004 - 2007 Nigel McNie © 2007 - 2012 Benny Baumann ...

  6. [Javascript] Prototype, hasOwnProperty(), valueOf() and toString() methods.

    Sometime, use can rewrite the toString , valueOf method to make those function more useful: For exma ...

  7. Android 色彩设计理念

    色彩 色彩从当代建筑.路标.人行横道以及运动场馆中获取灵感.由此引发出大胆的颜色表达激活了色彩,与单调乏味的周边环境形成鲜明的对照. 强调大胆的阴影和高光.引出意想不到且充满活力的颜色. 色样 – 0 ...

  8. Android 的一些提示框

    1.在测试时,如何实现一个提示 可以使用 Toast.makeText(this, "这是一个提示", Toast.LENGTH_SHORT).show(); //从资源文件str ...

  9. leetcode Wildcard Matching greedy algrithm

    The recursive program will result in TLE like this: class Solution { public: bool isMatch(const char ...

  10. 设定StatusBar的文字成不同的颜色

    设定StatusBar上的文字,该文字以StatusBar所在Form的字型设定为准,并以form的ForeColor为字的颜色,文字过长时,自动会截除这个程式的实质意义不太大,因为当文字被盖掉後需自 ...