The Add function is to add the corresponding key and value to a data block, the function source code is as follows, one of which does not understand:
L30~L34 is updated last_key_, do not understand here why not directly last_key_ = key. ToString ();
Written
Update State
Last_key_.resize (shared);
Last_key_.append (Key.data () + GKFX, non_shared);
ASSERT (Slice (last_key_) = = key);
Is there any other reason?
1 voidBlockbuilder::add (Constslice& Key,Constslice&value) {2 Slice last_key_piece (LAST_KEY_);3ASSERT (!finished_);4ASSERT (Counter_ <= options_->block_restart_interval);5ASSERT (Buffer_.empty ()//No values yet?6|| Options_->comparator->compare (Key, last_key_piece) >0);7size_t shared =0;8 if(Counter_ < options_->block_restart_interval) {9 //See how much sharing to does with previous stringTen Constsize_t min_length =std::min (Last_key_piece.size (), key.size ()); One while(Shared < Min_length) && (last_key_piece[shared] = =key[shared])) { Ashared++; - } -}Else { the //Restart Compression - Restarts_.push_back (Buffer_.size ()); -Counter_ =0; - } + Constsize_t non_shared = key.size ()-Gkfx; - + //Add "<shared><non_shared><value_size>" to Buffer_ APutVarint32 (&Buffer_, GKFX); atPutVarint32 (&Buffer_, non_shared); -PutVarint32 (&Buffer_, Value.size ()); - - //Add string Delta to Buffer_ followed by value -Buffer_.append (Key.data () +gkfx, non_shared); - Buffer_.append (Value.data (), value.size ()); in - //Update State to last_key_.resize (shared); +Last_key_.append (Key.data () +gkfx, non_shared); -ASSERT (Slice (last_key_) = =key); thecounter_++; *}
[Leveldb Source Analysis question]-block_builder.cc's add function