From the "LEVELDB Source analysis-Insert Delete process" and writebatch its name we can easily know, this is a leveldb internal bulk write structure, in the leveldb in order to improve the efficiency of insertion and deletion, Many write requests that have the same synchronization settings adjacent to the batch collection are written in bulk in the process of inserting them.
Its member variable contains only a rep_ variable of type std::string, and its put and delete (which is actually an insert delete operation, rather than deleting the put in data, or you can interpret it as a put delete Operation) will be encode after the corresponding operation is credited. Let's take a look at the format of its encode
Writebatch::rep_: = [SEQUENCE:FIXED64 | count:fixed32 | Data:record[count] record:= [Ktypevalue | key (Varint32 | data) | value (Varint32 | data)]
Let's take a look at some of the structures and members within Writebatch.
classHandler { Public: Virtual~Handler (); Virtual voidPut (Constslice& Key,Constslice& value) =0; Virtual voidDelete (Constslice& key) =0; }; Status Iterate (Handler* Handler)Const;friendclasswritebatchinternal;
The writebatchinternal is mainly the encapsulation of the internal decoding code of Writebatch, simplifying the structure of writebatch. And the handler interface we can see Memtableinserter become an implementation, this handler is in the writebatch of the internal decoding traversal process calls handler corresponding put, delete to memtable. So go back and analyze the Dbimpl::buildbatchgroup method.
// Requires:first writer must has a non-null batch writebatch* Dbimpl::buildbatchgroup (writer** last_writer) { Writer* first = Writers_.front (); Writebatch* result = first->batch;
= Writebatchinternal::bytesize (first->batch); / * This is primarily to set the maximum amount of data to be written per batch, preventing too much data being inserted at one time to allow for too long to write Because from the logic analysis of write we know that only the write thread at the top of the queue will go to bulk assemble and then perform a real insert,
Other threads are waiting for this bulk write to complete. */
size_t max_size =1<< -; if(Size <= ( -<<Ten) ) {max_size= Size + ( -<<Ten); } *last_writer =First ; std::d eque<writer*>::iterator iter =Writers_.begin (); ++iter;//Skip "First" for(; ITER! = Writers_.end (); + +ITER) {Writer* W = *ITER; if(W->sync &&!first->sync) { //only combining adjacent sync sets the same operation to a batch for processing. Break; } if(W->batch! =NULL) {Size+ = Writebatchinternal::bytesize (w->batch); if(Size >max_size) { //if the total size exceeds the current size of the setting, no longer continues the assembly process, jumping out of the loop to execute the assembled request Break; } //For the first time into the loop, assign the Tmp_batch_ to result and put the first into it, and the successor is append to Tmp_batch_ .
and the last return to operate is this tmp_batch_. if (Result = = first->Batch) { result = Tmp_batch_ ; 0 ); Writebatchinternal::append (result,first-batch); } Writebatchinternal::append (Result, W,batch); } *last_writer = w; } return result;}
The next operation for this batch is Writebatchinternal::insertinto (updates, mem_), which is the previous reference to decoding the data in batch using a iterator way.
Status Writebatchinternal::insertinto (const writebatch* B, memtable* memtable) { Memtableinserter Inserter; = Writebatchinternal::sequence (b); = memtable; return b->iterate (&Inserter);}
Const {
while (! switch case Ktypevalue: handler ->put (key, value); break ; case ktypedeletion: handler ->delete (key);
break; } } return Status::ok ();}
Here, let's review the Write
while (!w.done && &w! = Writers_.front ()) {
The statement is that the thread waits until the current thread's request is completed or requests to the front of the queue, so why does leveldb use one of these threads to bulk operations and other threads to wait? We know that leveldb in the actual insertion process will have a series of judgments, and write log to disk operations. And first of all these judgments are held in the case of keeping the lock, and here in fact is destined to be only serial; Secondly, for writing disks, merging multiple writes into one write can significantly improve efficiency, equivalent to N * disk addressing time + write time sum becomes a disk addressing time + write time sum, which saves (N- 1) secondary disk addressing time.