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. [leetcode]Binary Tree Maximum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...

  2. Substring with Concatenation of All Words leetcode java

    题目: You are given a string, S, and a list of words, L, that are all of the same length. Find all sta ...

  3. 4 Sum leetcode java

    题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  4. java类过滤器,防止页面SQL注入

    package com.tarena.dingdang.filter; import java.io.IOException; import java.util.Enumeration; import ...

  5. JavaScript快速检测浏览器对CSS3特性的支持情况

    项目中使用动画效果在IE9下不支持,所以写了个判断浏览器是否支持动画的函数,进而扩展到下面判断浏览器支持任一特定CSS3属性的函数. function supportAnimation(){ var ...

  6. POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新

    题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...

  7. shell more less cat

    cat 连续显示.查看文件内容 more 分页查看文件内容 less 分页可控制查看文件内容 通俗点说: cat一次性把文件内容全部显示出来,管你看不看得清,显示完了cat命令就返回了,不能进行交互式 ...

  8. nginx rewrite only specific servername to https

    需求: 把某个域名的80端口服务  ----> 重定向转到 这个域名的 443端口的服务.   server { listen 80; server_name xxx.abcd.com.cn; ...

  9. linux系统用户下的crontab任务不执行问题处理

    需求:需要每一天对数据库做一个备份,oracle数据库,linux系统. 备份命令采用最简单的导出\导入. 首先确认服务器是否开启任务计划服务,只有root用户才能对crond服务进行开启和关闭 [r ...

  10. win10 右键发送到 目录

    win10 右键发送到 目录 C:\Users\llskj\AppData\Roaming\Microsoft\Windows\SendTo C:\Users\llskj\AppData\Roamin ...