Source Download
git clone https://github.com/google/leveldb.git
Project Structure
- db/, Database logic
- doc/, MD Documentation
- helpers/, LEVELDB memory version, covered by namespace
- port/, platform-related code
- table/, LSM-related.
Main modulesLog file
The client's write request is append to the Log file before it is written to memtable. If the outage is possible, the Log file can be used to recover memtable.
Memtable and immutable memtable
Memory data structure, based on the hop table. Read and write requests from clients are handled by memtable. When the memory occupied by memtable reaches a certain threshold, the new memtable processing client request is regenerated. The original memtable turned into immutable memtable, waiting to be merged into the SST file.
SST file
A storage file that lands to disk. SST is divided into different level, specific reference documents.
Manifest file
Manifest records the SST files at different level, including the key range, size, and so on for each SST file metadata.
Current file
Current records the latest Manifest files.
Lsmtree's core ideas and questions
In the LSM tree, all data is written directly to memtable and log, and when the memtable is large enough, it becomes immemtable, moving to the hard disk and becoming a sstable. You can use any reasonable data structure to represent memtable, immemtable, and sstable. Leveldb chooses to implement Memtable and immemtable with a jumping table (skiplist), and implements Sstable with ordered row groups.
The LSM tree has the following problems:
1. It is suitable for inserting many and finding fewer cases. when looking for a key, the worst-case scenario is read from memtable to Immemtable, and then to all sstable.
2.SSTable How to effectively merge (major compaction)? If there is only one sstable, I want to merge the new immemtable, I will rewrite this sstable. How big the data is, how big the sstable will be. so divide the sstable into several parts, each 2MB? In the worst case, for example, the current immemtable has exactly one key to repeat with at least one key in any sstable, returning to the case where the whole library was just rewritten.
in response to the above issues, LEVELDB played two enhancement patches:
1. Add Bloomfilter, this will increase the speed of the whole library scan, skip the sstable without this key.
2. Leveled compaction, divide the sstable into different grades. In addition to level 0, the sstable of the remaining levels will not have duplicate keys.
Leveldb's approach makes every compaction ripple to the extent predictable. The official document says "The compaction picks a file from level L and all overlapping files from the next level l+1". Only by rank delay merge, no random read and write operation, simple mechanism, and do not need bookkeeping, can gracefully release the space of deleted records.
It is important to note that because subordinates may also have data of the same key, compaction does not necessarily empty all deletion maker.
Reference documents:
1.https://zhuanlan.zhihu.com/p/27329248
2.http://masutangu.com/2017/06/leveldb_1/
Leveldb source Reading (i)