zoukankan      html  css  js  c++  java
  • [Leveldb源码剖析疑问]-block_builder.cc之Add函数

    Add函数是给一个Data block中添加对应的key和value,函数源码如下,其中有一处不理解:

    L30~L34是更新last_key_的,不理解这里干嘛不直接last_key_ = key.ToString();

    写成

    // Update state
    last_key_.resize(shared);
    last_key_.append(key.data() + shared, non_shared);
    assert(Slice(last_key_) == key);

    是有什么其他原因吗?

     1 void BlockBuilder::Add(const Slice& key, const Slice& value) {
     2   Slice last_key_piece(last_key_);
     3   assert(!finished_);
     4   assert(counter_ <= options_->block_restart_interval);
     5   assert(buffer_.empty() // No values yet?
     6          || options_->comparator->Compare(key, last_key_piece) > 0);
     7   size_t shared = 0;
     8   if (counter_ < options_->block_restart_interval) {
     9     // See how much sharing to do with previous string
    10     const size_t min_length = std::min(last_key_piece.size(), key.size());
    11     while ((shared < min_length) && (last_key_piece[shared] == key[shared])) {
    12       shared++;
    13     }
    14   } else {
    15     // Restart compression
    16     restarts_.push_back(buffer_.size());
    17     counter_ = 0;
    18   }
    19   const size_t non_shared = key.size() - shared;
    20 
    21   // Add "<shared><non_shared><value_size>" to buffer_
    22   PutVarint32(&buffer_, shared);
    23   PutVarint32(&buffer_, non_shared);
    24   PutVarint32(&buffer_, value.size());
    25 
    26   // Add string delta to buffer_ followed by value
    27   buffer_.append(key.data() + shared, non_shared);
    28   buffer_.append(value.data(), value.size());
    29 
    30   // Update state
    31   last_key_.resize(shared);
    32   last_key_.append(key.data() + shared, non_shared);
    33   assert(Slice(last_key_) == key);
    34   counter_++;
    35 }
  • 相关阅读:
    SQL having 子句
    sqlserver2008 R2 创建作业(定时任务)
    3步完成chrome切换搜索引擎
    http模拟请求工具
    网页自动加载进度条插件
    span设为inline-block之后,未包含文字时下面会多出一条空白问题
    记一次特殊的下载字体方法
    团队冲刺第十三天
    团队冲刺第十二天
    人月神话03
  • 原文地址:https://www.cnblogs.com/i4oolish/p/4084007.html
Copyright © 2011-2022 走看看