SSTable is a crucial part of Bigtable. It is also true for LevelDb. Understanding the SSTable Implementation Details of LevelDb also helps you understand some implementation details in Bigtable. This section describes the static layout structure of SSTable. We once said in the overall architecture that the SSTable file forms a hierarchical structure of different levels.
SSTable is a crucial part of Bigtable. It is also true for LevelDb. Understanding the SSTable Implementation Details of LevelDb also helps you understand some implementation details in Bigtable. This section describes the static layout structure of SSTable. We once said in the overall architecture that the SSTable file forms a hierarchical structure of different levels.
SSTable is a crucial part of Bigtable. It is also true for LevelDb. Understanding the SSTable Implementation Details of LevelDb also helps you understand some implementation details in Bigtable.
This section describes the static layout structure of SSTable. We once said in the "overall architecture" that the SSTable file forms a hierarchical structure of different levels, as for how this hierarchical structure is formed, we will discuss it in the Compaction section below. This section describes the physical layout and logical layout structure of a file in SSTable, which is helpful for understanding the running process of LevelDb.
Different levels of LevelDb have many SSTable files (characterized by. sst later). The internal layout of all. sst files is the same. In the previous section, we introduced that Log files are physically segmented, and SSTable will also divide the files into physical storage blocks of a fixed size, but the logical layout of the two is very different, the root cause is: the records in the Log file are unordered, that is, the Key size of the record is not explicitly related to the size,. the sst file is arranged from small to large according to the record Key. From the SSTable layout described below, we can see why Key order is designed. the key to the sst file structure.
. 1. Block Structure of the sst File
. 1 shows. the physical division structure of sst files, like Log files, is also a storage Block of fixed size. Each Block is divided into three parts, and the red part is the data storage area, the blue Type area is used to identify whether the data storage area adopts the Data Compression Algorithm (Snappy compression or no compression), and the CRC part is the data verification code, used to identify whether data is generated and transmitted incorrectly.
The above is. the physical layout of sst is described below. the logical layout of sst files, the so-called logical layout, that is to say, although everyone is a physical block, what content is stored in each block, what structure is there internally, and so on .. 2 shows the internal logic explanation of the. sst file.
. 2 logical layout
From. 2. We can see that in the big aspect. sst files are divided into data storage areas and data management areas. The data storage area stores the actual Key: Value data. The data management area provides some index pointers and other management data, the goal is to quickly and conveniently search for corresponding records. Both regions are based on the above blocks, that is, the first several blocks of the file actually store KV data, and the subsequent data management areas store and manage data. Management data is divided into four different types: Purple Meta Block, red MetaBlock index, blue data index Block, and a file tail Block.
LevelDb 1.2 is not actually used for Meta Block, but only retains an interface. It is estimated that content will be added to later versions. Let's look at the internal structure of the Data Index Area and the Footer at the end of the file.
. 3. Data Index
. 3 is the internal structure of the data index. Once again, The KV records in the Data Block are arranged in ascending order of keys. Each record in the Data index area is the index information created for a Data Block, each index contains three contents. for Index I of data block I shown in 3: the first field in the red part records the Key that is greater than or equal to the maximum Key value in data block I, the second field indicates that the data block I is in. start position in the sst file. The third field indicates the size of Data Block I (sometimes there is Data compression ). The following two fields are used to locate the position of the data block in the file. The first field needs to be explained in detail, the Key value stored in the index may not be the Key of a record. for example 3, assume that the minimum Key of block I is "samecity", the maximum Key is "the best", and the minimum Key of block I + 1 is "the fox ", the maximum Key is "zoo", so for Index I of data block I, the first field records are greater than or equal to the maximum Key ("the best") of data block I ") at the same time, it must be smaller than the minimum Key ("the fox") of the data block I + 1. Therefore, the first field of Index I in this example is "the c", which meets the requirements; the first field of Index I + 1 is "zoo", that is, the maximum Key of data block I + 1.
For the internal structure of the Footer block at the end of the file, see. 4. metaindex_handle indicates the start position and size of the metaindex block. inex_handle indicates the start address and size of the index Block. These two fields can be considered as the index, it is set up to read the index value correctly, followed by a filling area and magic number.
. 4 Footer
The above section mainly introduces the internal structure of the data management area. Let's take a look at how the data part of a Block in the data area is laid out internally (. 1 In red ),. 5 is its internal layout.
. 5 internal structure of the data Block
It can be seen that it is also divided into two parts, the first is a KV record, the order is based on the Key value from small to large, at the end of a Block is a Restart Point, which is actually a pointer pointing to some recorded locations in the Block content.
What is "Restarting? We have repeatedly stressed that the KV records in the Block content are sorted by the Key size. In this case, the adjacent two records may overlap the Key part, for example, key I = "the Car ", key I + 1 = "the color", there is an overlap between the two "the c". To reduce the storage of the Key, key I + 1 can only store the part "olor" that is different from the previous Key, and the common part of the two can be obtained from Key I. The record Key is stored in the Block content to reduce storage overhead. The "restart point" means that at the beginning of this record, all Key values are rerecorded instead of recording only different Key parts. Assume that Key I + 1 is a restart point, the "the color" is fully stored in the Key, instead of using the simple "olor" method. The end of the Block indicates which records are the key points.
. 6 record format