<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    小明思考

    Just a software engineer
    posts - 124, comments - 36, trackbacks - 0, articles - 0
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
    leveldb 使用 version 來(lái)保存數(shù)據(jù)庫(kù)的狀態(tài)。

    先看看一個(gè)重要的數(shù)據(jù)結(jié)果,sst file的META info
    <db/version_edit.h>

    struct FileMetaData {
      
    int refs; //引用計(jì)數(shù)
      int allowed_seeks; //允許的seeks次數(shù)
      uint64_t number;//文件編號(hào)
      uint64_t file_size;  //文件大小
      InternalKey smallest;    //最小的key
      InternalKey largest;      //最大的key 

      FileMetaData() : refs(
    0), allowed_seeks(1 << 30), file_size(0) { }
    };

    這里面有一個(gè)很有意思的字段: allowed_seeks,代表了可以seek的次數(shù),為0的時(shí)候表示這個(gè)文件需要被compaction.如何設(shè)置seeks次數(shù)呢?文件大小除以16k,不到100算100。

          f->allowed_seeks = (f->file_size / 16384);
          
    if (f->allowed_seeks < 100) f->allowed_seeks = 100;

    原因,請(qǐng)看leveldb的注釋:

    // We arrange to automatically compact this file after a certain number of seeks.  Let's assume:
          //   (1) One seek costs 10ms
          //   (2) Writing or reading 1MB costs 10ms (100MB/s)
          //   (3) A compaction of 1MB does 25MB of IO:
          //         1MB read from this level
          //         10-12MB read from next level (boundaries may be misaligned)
          //         10-12MB written to next level
          // This implies that 25 seeks cost the same as the compaction
          // of 1MB of data.  I.e., one seek costs approximately the
          // same as the compaction of 40KB of data.  We are a little
          // conservative and allow approximately one seek for every 16KB
          // of data before triggering a compaction.


    接下來(lái)看Version的定義,version其實(shí)就是一系列的SST file的集合。
    class Version {
     
    public:
      
    //生成iterator用于遍歷
      void AddIterators(const ReadOptions&, std::vector<Iterator*>* iters);

      
    //根據(jù)key來(lái)查詢,若沒(méi)有查到,更新GetStats
      struct GetStats {
        FileMetaData
    * seek_file;
        
    int seek_file_level;
      };
      Status Get(
    const ReadOptions&const LookupKey& key, std::string* val,
                 GetStats
    * stats);

      
    //是否需要進(jìn)行compaction
      bool UpdateStats(const GetStats& stats);

      
    //引用計(jì)算,避免在被引用時(shí)候刪除
      void Ref();
      
    void Unref();

      
    //查詢和key range有關(guān)的files
      void GetOverlappingInputs(
          
    int level,
          
    const InternalKey* begin,         // NULL means before all keys
          const InternalKey* end,           // NULL means after all keys
          std::vector<FileMetaData*>* inputs);

      
    //計(jì)算是否level對(duì)某個(gè)key range是否有overlap
      bool OverlapInLevel(int level,
                          
    const Slice* smallest_user_key,
                          
    const Slice* largest_user_key);

      
    //memtable output應(yīng)該放到哪個(gè)level
      int PickLevelForMemTableOutput(const Slice& smallest_user_key,
                                     
    const Slice& largest_user_key);

      
    //某個(gè)level的文件個(gè)數(shù)
       int NumFiles(int level) const { return files_[level].size(); }

      
    // Return a human readable string that describes this version's contents.
      std::string DebugString() const;

     
    private:
      friend 
    class Compaction;
      friend 
    class VersionSet;

      
    class LevelFileNumIterator;
      Iterator
    * NewConcatenatingIterator(const ReadOptions&int level) const;

      VersionSet
    * vset_;            // VersionSet to which this Version belongs
      Version* next_;               // Next version in linked list
      Version* prev_;               // Previous version in linked list
      int refs_;                    // Number of live refs to this version

      
    //sst files 
      std::vector<FileMetaData*> files_[config::kNumLevels];

      
    //下一個(gè)要被compaction的文件
      FileMetaData* file_to_compact_;
      
    int file_to_compact_level_;

      
    //compaction score:>1表示要compaction
      double compaction_score_;
      
    int compaction_level_;

      
    explicit Version(VersionSet* vset)
          : vset_(vset), next_(
    this), prev_(this), refs_(0),
            file_to_compact_(NULL),
            file_to_compact_level_(
    -1),
            compaction_score_(
    -1),
            compaction_level_(
    -1) {
      }

      
    ~Version();

      
    // No copying allowed
      Version(const Version&);
      
    void operator=(const Version&);
    };


    那VersionSet呢?VersionSet 是version組成一個(gè)雙向循環(huán)鏈表。

    class VersionSet{
    //. . .
    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_;

      WritableFile
    * descriptor_file_;
      log::Writer
    * descriptor_log_;
      Version dummy_versions_;  
    // Head of circular doubly-linked list of versions.
      Version* current_;        // == dummy_versions_.prev_

      
    //每層都有一個(gè)compact pointer用于指示下次從哪里開始compact,以用于實(shí)現(xiàn)循環(huán)compact
      std::string compact_pointer_[config::kNumLevels];
    //. . .
    }


    VersionEdit是version對(duì)象的變更記錄,用于寫入manifest.這樣通過(guò)原始的version加上一系列的versionedit的記錄,就可以恢復(fù)到最新狀態(tài)。

    class VersionEdit {
     
    public:
      VersionEdit() { Clear(); }
      
    ~VersionEdit() { }

      
    void Clear();

      
    void SetComparatorName(const Slice& name) {
        has_comparator_ 
    = true;
        comparator_ 
    = name.ToString();
      }
      
    void SetLogNumber(uint64_t num) {
        has_log_number_ 
    = true;
        log_number_ 
    = num;
      }
      
    void SetPrevLogNumber(uint64_t num) {
        has_prev_log_number_ 
    = true;
        prev_log_number_ 
    = num;
      }
      
    void SetNextFile(uint64_t num) {
        has_next_file_number_ 
    = true;
        next_file_number_ 
    = num;
      }
      
    void SetLastSequence(SequenceNumber seq) {
        has_last_sequence_ 
    = true;
        last_sequence_ 
    = seq;
      }
      
    void SetCompactPointer(int level, const InternalKey& key) {
        compact_pointers_.push_back(std::make_pair(level, key));
      }

      
    //添加meta file
      void AddFile(int level, uint64_t file,
                   uint64_t file_size,
                   
    const InternalKey& smallest,
                   
    const InternalKey& largest) {
        FileMetaData f;
        f.number 
    = file;
        f.file_size 
    = file_size;
        f.smallest 
    = smallest;
        f.largest 
    = largest;
        new_files_.push_back(std::make_pair(level, f));
      }

      
    //刪除特定的文件
      void DeleteFile(int level, uint64_t file) {
        deleted_files_.insert(std::make_pair(level, file));
      }

      
    //編碼,解碼:用于寫入manifest
      void EncodeTo(std::string* dst) const;
      Status DecodeFrom(
    const Slice& src);

      std::
    string DebugString() const;

     
    private:
      friend 
    class VersionSet;

      typedef std::
    set< std::pair<int, uint64_t> > DeletedFileSet;

      std::
    string comparator_;
      uint64_t log_number_;
      uint64_t prev_log_number_;
      uint64_t next_file_number_;
      SequenceNumber last_sequence_;
      
    bool has_comparator_;
      
    bool has_log_number_;
      
    bool has_prev_log_number_;
      
    bool has_next_file_number_;
      
    bool has_last_sequence_;

      std::vector
    < std::pair<int, InternalKey> > compact_pointers_;
      DeletedFileSet deleted_files_;
      std::vector
    < std::pair<int, FileMetaData> > new_files_;
    };


    主站蜘蛛池模板: 亚洲欧洲精品视频在线观看| 中文字幕免费在线看线人| 妞干网免费视频观看| 亚洲区视频在线观看| 国产乱子精品免费视观看片| 亚洲国产美女视频| 狼群影院在线观看免费观看直播 | 天天综合亚洲色在线精品| 最新中文字幕免费视频| 亚洲日韩精品无码专区加勒比 | www视频在线观看免费| 亚洲乱码一二三四区麻豆| 91在线视频免费播放| 亚洲精品国产首次亮相| 免费一看一级毛片全播放| 国产精品小视频免费无限app| 亚洲精品乱码久久久久久蜜桃不卡| a级片免费观看视频| 亚洲三级电影网站| 免费福利网站在线观看| 亚洲av无码专区在线电影天堂 | 国产一级在线免费观看| 亚洲熟女少妇一区二区| 精品国产污污免费网站入口| 久久久亚洲精品视频| 久久久久国色AV免费观看性色| 亚洲中文无码亚洲人成影院| 亚洲国产成人VA在线观看| 久久久久久免费一区二区三区| 亚洲中字慕日产2021| 无码欧精品亚洲日韩一区夜夜嗨 | 久久免费国产精品一区二区| 亚洲美女视频一区二区三区| 午夜毛片不卡高清免费| 免费一级做a爰片久久毛片潮| 午夜亚洲www湿好大| 四虎www成人影院免费观看| 中文无码日韩欧免费视频| 亚洲国产高清视频在线观看| 亚洲福利精品一区二区三区| 午夜影院免费观看|