This section describes the data structure of LEVELDB in memory memtable,memtable as its name is the KV Table in memory, the C0 Tree in Lsm-tree. We know that the KV data just inserted in the lsm-tree is stored in memory and then written to disk when the data stored in memory exceeds a certain amount. For LEVELDB, the process evolved into in-memory data is inserted into the memtable, and when the data in memtable exceeds a certain amount (options.write_buffer_size), memtable is converted to immutable Memtable waits for the dump to sstable in the disk. Immutable memtable is structurally and memtable is exactly the same, except that it is read-only, does not allow write operations, and Memtable is allowed to write and read, while Memtable is converted to immutable A memtable is generated when memtable.
To understand Memtable, let's first look at the member variables that he contains.
typedef skiplist<ConstChar*, keycomparator> Table; Keycomparator comparator_; // comparator, which is used to determine the order of key, which is described later int refs_; // reference count, a self-destruct when reference is 0 Arena Arena_; // a simple memory pool Table Table_; // Save the actual structure of the kv pair, which is a skiplist
As mentioned above, when the data in memory exceeds a certain amount, the memtable will be converted to immutable Table, and this amount of management and judgment is achieved through arena. Its work is very simple, the application to the memory block into the std::vector Blocks_, after the end of the life cycle of arena, the unified release of all the requested memory, internal structure:
Just when dealing with a small trick, arena will pre-allocate a piece of 4K of memory, if the subsequent memory request from which to obtain, if the remaining space is not enough to request the two processing methods: 1. Apply Size > 1K, then directly apply a new size and deposit in vector; 2. Request size <= 1K, request a piece of 4K of memory, and then allocate the appropriate size space to the requestor.
Arena mainly provides two application functions: one allocates memory directly and the other can request an aligned memory space. This design should say that this is related to leveldb specific scenarios, such as a memtable using a arena, and when Memtable is released, it is freed by Arena to release its memory uniformly. The way small memory is pre-allocated can effectively reduce the number of system calls to memory requests and reduce the corresponding overhead. Understanding the process, let's look at the code:
Char* ALLOC_PTR_; //Current pre-allocated 4K address pointer //Pre-allocated memory not yet used size std::vector<char*> blocks_; //Set of memory address pointers already allocated size_t Blocks_memory_; //Allocated memory size
Application process, only the application is not misaligned method, the application of aligned memory is just a memory alignment process, the logic is simple
Char* arena::allocate (size_t bytes) { if (bytes <= alloc_bytes_remaining_) { // the remaining memory is greater than the request, taking the corresponding size directly from the pre-allocated memory char* result = alloc_ptr_; + = bytes ; -= bytes ; return result; } return allocatefallback (bytes);}
Char*arena::allocatefallback (size_t bytes) {//if > 1K directly allocates a piece of memory and returns it to the requester if(Bytes > Kblocksize/4) { Char* result =allocatenewblock (bytes); returnresult; } //Otherwise, request a piece of 4K of memory and then return the corresponding size to the requestor.Alloc_ptr_ =Allocatenewblock (kblocksize); Alloc_bytes_remaining_=kblocksize; Char* result =alloc_ptr_; Alloc_ptr_+=bytes; Alloc_bytes_remaining_-=bytes; returnresult;}
Here the use of the principle of arena is completed, when the destruction of the time will release all memory.
As for Skiplist Leveldb is the alignment has a specific implementation, the alignment of the detailed introduction and description there is an article "Skiplist Jump Table" has a detailed introduction. In the following article we introduce memtable iterators and comparator.