[Leveldb源碼剖析疑問]-block_builder.cc之Add函數

來源:互聯網
上載者:User

標籤:style   blog   io   color   ar   os   sp   div   on   

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 string10     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 compression16     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 value27   buffer_.append(key.data() + shared, non_shared);28   buffer_.append(value.data(), value.size());29 30   // Update state31   last_key_.resize(shared);32   last_key_.append(key.data() + shared, non_shared);33   assert(Slice(last_key_) == key);34   counter_++;35 }

 

[Leveldb源碼剖析疑問]-block_builder.cc之Add函數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.