码迷,mamicode.com
首页 > 数据库 > 详细

LevelDB场景分析2--Open

时间:2015-05-11 19:43:36      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

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()) {
10     uint64_t new_log_number = impl->versions_->NewFileNumber();
11     WritableFile* lfile;
12     s = options.env->NewWritableFile(LogFileName(dbname, new_log_number),
13                                      &lfile);
14     if (s.ok()) {
15       edit.SetLogNumber(new_log_number);
16       impl->logfile_ = lfile;
17       impl->logfile_number_ = new_log_number;
18       impl->log_ = new log::Writer(lfile);
19       s = impl->versions_->LogAndApply(&edit, &impl->mutex_);
20     }
21     if (s.ok()) {
22       impl->DeleteObsoleteFiles();
23       impl->MaybeScheduleCompaction();
24     }
25   }
26   impl->mutex_.Unlock();
27   if (s.ok()) {
28     *dbptr = impl;
29   } else {
30     delete impl;
31   }
32   return s;
33 }

 

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),
10       db_lock_(NULL),
11       shutting_down_(NULL),
12       bg_cv_(&mutex_),
13       mem_(new MemTable(internal_comparator_)),
14       imm_(NULL),
15       logfile_(NULL),
16       logfile_number_(0),
17       log_(NULL),
18       seed_(0),
19       tmp_batch_(new WriteBatch),
20       bg_compaction_scheduled_(false),
21       manual_compaction_(NULL) {
22   mem_->Ref();
23   has_imm_.Release_Store(NULL);
24 
25   // Reserve ten files or so for other uses and give the rest to TableCache.
26   const int table_cache_size = options_.max_open_files - kNumNonTableCacheFiles;
27   table_cache_ = new TableCache(dbname_, &options_, table_cache_size);
28 
29   versions_ = new VersionSet(dbname_, &options_, table_cache_,
30                              &internal_comparator_);
31 }

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

标签:

原文地址:http://www.cnblogs.com/onlyforcloud/p/4488202.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!