[Leveldb源码剖析疑问]-block_builder.cc之Add函数
2014-11-08 21:04
435 查看
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);
是有什么其他原因吗?
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);
是有什么其他原因吗?
void BlockBuilder::Add(const Slice& key, const Slice& value) { Slice last_key_piece(last_key_); assert(!finished_); assert(counter_ <= options_->block_restart_interval); assert(buffer_.empty() // No values yet? || options_->comparator->Compare(key, last_key_piece) > 0); size_t shared = 0; if (counter_ < options_->block_restart_interval) { // See how much sharing to do with previous string const size_t min_length = std::min(last_key_piece.size(), key.size()); while ((shared < min_length) && (last_key_piece[shared] == key[shared])) { shared++; } } else { // Restart compression restarts_.push_back(buffer_.size()); counter_ = 0; } const size_t non_shared = key.size() - shared; // Add "<shared><non_shared><value_size>" to buffer_ PutVarint32(&buffer_, shared); PutVarint32(&buffer_, non_shared); PutVarint32(&buffer_, value.size()); // Add string delta to buffer_ followed by value buffer_.append(key.data() + shared, non_shared); buffer_.append(value.data(), value.size()); // Update state last_key_.resize(shared); last_key_.append(key.data() + shared, non_shared); assert(Slice(last_key_) == key); counter_++; }
相关文章推荐
- LevelDB源码剖析之SSTable_1(Block的创建与读取)
- boost源码剖析之:泛型函数指针类boost::function(rev#3)
- boost源码剖析之:泛型函数指针类boost::function(rev#3)
- 转帖:boost源码剖析(1):泛型函数指针类boost::function
- C++ Standard Stl -- SGI STL源码学习笔记(07) stl_vector 与 一些问题的细化 3 resize函数剖析
- levelDB源码分析-SSTable:Block
- boost源码剖析之:泛型函数指针类boost::function(rev#3)
- libevent源码分析--epoll_add()函数
- Linux进程间通信源码剖析,共享内存(shmget函数详解)
- jQuery.API源码深入剖析以及应用实现(1) - 核心函数篇(1)
- libevent源码分析--event_add()函数
- jQuery.API源码深入剖析以及应用实现(1) - 核心函数篇
- STL 源码剖析 笔记 1: 函数指针和类型
- jQuery.API源码深入剖析以及应用实现(1) - 核心函数篇
- Boost源码剖析:泛型函数指针类boost::function(修订版)
- boost源码剖析之:泛型函数指针类boost::function(rev#3)
- Boost源码剖析:C++泛型函数指针类function
- jQuery 源码剖析-4 init 函数分析
- Linux进程间通信源码剖析,共享内存(shmget函数详解)
- libevent源码分析--epoll中的几个函数 epoll_init epoll_add epoll_dispatch