LEVELDB will organize data in different versions (Level-0-Level-n, from new to old), which is stored on disk in sstable format. A sstable file can be viewed as a disk-based, read-only map that supports sequential scanning and can look for a key. This article explores the format of the sstable file and the creation process.
1. sstable file format
| - - - - - - - - - - - - - - - - |
| Data Blocks |
| - - - - - - - - - - - - - - - - |
| Filter Block |
| - - - - - - - - - - - - - - - - |
| Meta Index Block |
| - - - - - - - - - - - - - - - - |
| Index Block |
| - - - - - - - - - - - - - - - - |
| Footer |
| - - - - - - - - - - - - - - - - |
As above is the sstable file format, the entire file includes a series of Block,block is disk IO and the basic unit of memory cache, through block read and write can be halved each IO cost, the use of local principle. The size of the block is user configurable.
1) Data Blocks: Store kv pairs, which is the actual data. KV pair will be divided into blocks according to size, can not guarantee that all blocks of the same size, but basically close to the configured block size, but when the kv pair of data is greater than this value, will be a block. Additional metadata is included inside the block, which is described in more detail later in this article.
2) The Filter Block:filter is used to determine if the sstable contains a key, thereby avoiding disk IO when locating the key. The filter block is used to store the result after the filter is serialized.
3) Meta Index Block: Used for storing meta data, currently only the KV format of the filter block offset, key is Filter.${filter_name},value is the filter block in the file offset.
4) Index BLOCK: The index of the data block, preserving the minimum key in each block, thus determining the scope of the block's key, accelerating the search for a key.
5) Footer: Metadata metadata that contains the offset of index block and Meta index block. Footer in the end, because the file is generated in sequential append, and footer information depends on all the previous information, so only in the end. Because the metadata is included, the first thing to read sstable is to load the footer.
2. Data structure
Blockhandle: Points to a block in the file with two properties for the Block's offset (offset_) and size (size_).
Footer: Represents the Footer of the sstable file, fixed in size.
Both of these structures provide a way to serialize and deserialize a string.
Tablebuilder: Constructs the entrance of the sstable. A series of kv pairs are constructed into sstable.
Blockbuilder: Constructs a block to serialize the added KV pair.
Filterblockbuilder: Constructs the filter Block.
The above is the main data structure for generating sstable files.
3. Blockbuidler
The above mentioned block is the basic unit of data transmission, in the data block through the block to the continuous kv pair packaging processing, you can take advantage of the principle of locality. At the same time, KV is stored in key order, then the content of the duplicate of key in the same block increases, and the space utilization can be improved by compression.
1) block Format:
| --Block Content:var len--| --Block type:1byte--| --Crc:4byte--|
A block contains 3 parts:
(1) Block content:kv the serialized content, which is a variable-length byte array.
(2) Block Type: Specifies whether the block is compressed, 1 bytes.
(3) CRC:CRC check code, for data complete test, 4 bytes.
2) Block content format:
| --KV Entries:var len--| --Restart Point array:4nbytes--| --Num Restart point:4byte--|
(1) KV Entries: A series of KV content, the actual content of the block, is a variable-length byte array.
(2) Restart point Array:restart a point array, each element is an integer, 4n bytes. The role of restart point is described later in this article.
(3) Num Restart point: Specifies the length of the Restart point array, 4 bytes.
3) KV entry format:
Because kv pairs are stored in key order, the key is prefixed by a prefix compression to save space.
| --Shared:4byte--| --Non-shared:4byte--| --Val len:4byte--| --Key Delta:var len--| --Value:var len--|
(1) Shared: prefix length with the previous key.
(2) Non-shared:key non-common prefix length, shared + non-shared equals the length of key.
(3) The length of Val len:value.
(4) Key Delta: Stores the key that removes the common prefix.
(5) Value: Store value.
As you can tell by the above description, when a key is stored, it does not have a common prefix with the previous key, but only a different part. Then, when reading a key from a disk, you need to read the previous key first to get the complete can. There is a problem, if you read the last key, then you need to read [1, n-1] key to get the full content.
LEVELDB addresses the above problems by introducing restart point, each of which is equivalent to a restart of the prefix compression of restart points. Restart point points to a key, which stores the full content, which is shared equal to 0. By inserting multiple restart point averages in some kv, you can reduce the length of the prefix decompression reads. By default, the interval for placing restart point in Leveldb is 16, ensuring that the worst case scenario is to fetch the full contents of a key as long as it reads 15 keys.
At the same time, Restart point points to the key is also ordered, you can put the underlying KV sequence of two index, in the key search, first Restart point of the two points to find the frame range, and then within the specified key range of linear lookup.
4. Filterblock
Filter is used to speed up key searches and avoid invalid disk IO. The LEVELDB itself provides bloom Filter. You can simply think of filter as a collection that determines whether a key exists in the collection. The Filterblock is a collection of all the keys in the sstable file (the result of filter serialization based on key).
1) filterblock Format:
| --encoded Filter Array:var len--| --Filter Offset array:4nbyte--| --offset of offset array:4byte--| --Base lg:1byte--|
(1) encoded filter array: A byte array that is serialized by the filter, consisting of the information of N filter.
(2) Filter offset Array: Specifies the offset of each filter in the encoded filter Array.
(3) offset of the offsets of offset array:filter offset Array.
(4) Base lg:2^ (base LG) is the smallest size of the filter that is constructed on the data block. Leveldb default is 2KB.
2) Process:
Leveldb for the creation of the filter source data size requirements, can not be less than 2KB, this is to prevent the source data is too small, resulting in a small amount of filter particles, the unit block corresponding to the filter space usage is too large, will be more wasteful.
The correspondence between the encoded Filter array and the data block is slightly more complex, and when the block is less than 2KB (such as 1KB), then multiple blocks will use a filter. If the block is larger than 2KB (for example, 4KB), then a block corresponds to a filter.
Each time a new block is started, the Filterbuilder.startblock () method is called, which determines the corresponding relationship between the block and the filter, which is generated if the previous filter is completed. After all keys have been added, the Filterbuilder.finish () method is called for overall serialization and returns the serialized result.
5. Tablebuilder
Tablebuilder is externally generated sstable interface, through the Add method to receive a kv, relying on the underlying blockbuidler to create a block, if the current block size exceeds the preset value, The Blockbuilder's finish method is called to serialize and then appended to the file. After all KV has been added, call the Tablebuilder.finish method to append the metadata, including the filter Block,meta Index block,index Block and footer.
6. Summary
Described above is the sstable disk representation, the design is quite sophisticated, including but not limited to prefix compression, the introduction of Restart point, Filter source data block division and so on. The next article describes the sstable read, which is the memory representation.
Leveldb Source Reading (1)--sstable generation