Leveldb source code analysis 2.2-the mem cache of the core design

introduction

Whether it is MemTable or immutable MemTable, they are all MemTable types in the leveldb source code implementation. Its core data structure is the jump table.

1 Principle of Jump Table

1.1 Data structure

The jump table is mainly to optimize the operation time complexity of the traditional ordered linked list, which is similar to the idea of ​​a binary search algorithm. Its structure is as shown in the figure below: The
Insert picture description here
figure shows a jump meter with a maximum height of 3. The table header has 3 pointer fields, and the data node has 1 to 3 pointer fields. All pointer fields point to the base address of the subsequent node. For example, the level ptr0 of the data field 1 and the level ptr1 of the head node both point to the base address of the corresponding node in the data field 2.

1.2 Data query

The core idea of ​​data query is to query nodes that are greater than or equal to the search data. Take query data field 4 as an example, the steps are as follows:

  • First, at level 2, traverse the query node greater than or equal to data field 4 from the head of the table to get the node where data field 5 is located. At the same time, record the index relationship between level 2 and the node where the data domain 3 is located;
  • Then, at level 1, starting from the node where the data domain 3 is located, traverse and query the nodes greater than or equal to the data domain 4 to obtain the node where the data domain 5 is located. At the same time, record the index relationship between level 1 and the node where the data domain 3 is located;
  • Continuing, at level 0, starting from the node where the data domain 3 is located, it is traversed to query the nodes greater than or equal to the data domain 4 to obtain the node where the data domain 4 is located. At the same time, record the index relationship between level 0 and the node where the data domain 3 is located;
  • Finally, compare whether the node where the data field 4 is located is equal to the data field 4, and the query is successful because it is equal.

1.3 Insert node

Insert nodes in the following order:

  • First, generate a node according to the algorithm and fill in the data field. For example, in leveldb, nodes with heights ranging from 1 to 12 are randomly generated ;
  • Then, the query is greater than or equal to the node where the data is inserted, and a predecessor node index of level 0 to 2 is obtained. In leveldb, an array is used instead of an index, that is, a subscript is used to indicate level ;
  • Traverse each level of the inserted node and append the inserted node to the back of the corresponding level ptr in the index of the predecessor node.

1.4 Delete node

Note: Deleting nodes is similar to inserting. It also uses queries to remove nodes and rebuild the pointing relationship of the linked list.

2 Source code interpretation

The entire MemTable class consists of two parts, namely the Area class and the SkipList template class. The former is the realization of the memory pool, and the latter is the template realization of the jump table.
Insert picture description here
Note: Refer to the previous section for the realization principle of Area.
In SkipList, only insert, query, and iterative functions are provided, and the delete operation is not provided. The reason why the delete operation is not provided is that MemTable only provides an append data operation. When we delete key, we just append the operation type to the database.

Guess you like

Origin blog.csdn.net/fs3296/article/details/108115956