I. Introduction of LEVELDB
LEVELDB is the Google open-source persistent kv stand-alone database, with high random write, sequential read/write performance, but random read performance is very general, that is to say, LEVELDB is very suitable for application in the query less, and write a lot of scenes. LEVELDB applies the LSM (Log structured Merge) policy, Lsm_tree delay and batch processing of index changes, and efficiently migrates updates to disk in a way that is similar to a merge sort, reducing the index insertion overhead, about LSM, This article will also be mentioned briefly later.
According to the description of the official website of Leveldb, the features and limitations of LEVELDB are as follows:
Characteristics:
1, key and value are byte arrays of any length;
2, entry (that is, a k-v record) by default is stored in the dictionary order of key, of course, developers can also overload the sorting function;
3, provides the basic operation interface: Put (), Delete (), Get (), Batch ();
4, support the batch operation with atomic operation;
5, you can create snapshot (snapshot) of the data panorama, and allow to find data in the snapshot;
6, the data can be traversed by the forward (or back) iterator (the iterator creates a snapshot);
7, automatically use snappy compressed data;
8, portability;
Limit:
1, non-relational data Model (NoSQL), SQL statements are not supported, and indexes are not supported;
2. Only one process is allowed to access a particular database at a time;
3, there is no built-in C/s architecture, but developers can use the LEVELDB library to encapsulate a server;
Second, download the compilation
Leveldb source is hosted on GitHub and downloaded as follows:
1 git clone https://github.com/google/leveldb.git2cd leveldb3 make
Without error during compilation, you will see two new folders in the root directory, out-shared and out-static, corresponding to the dynamic library and the static library version, respectively. LEVELDB, although called DB, is actually a library.
Start using the LEVELDB library below. Create file Leveldb_test.cpp:
1#include"Leveldb/db.h"2#include <cassert>3#include <iostream>4 5 using namespacestd;6 using namespaceleveldb;7 8 intMain ()9 {TenLEVELDB::D b *db; One leveldb::options Options; AOptions.create_if_missing =true; -Leveldb::status Status = leveldb::D b::open (Options,"TestDB", &db); - assert (Status.ok ()); the -Status = Db->put (Writeoptions (),"YM65536","Hello ym65536!"); - assert (Status.ok ()); - stringRes; +Status = Db->get (Readoptions (),"YM65536", &res); - assert (Status.ok ()); +cout << Res <<Endl; A at Deletedb; - return 0; -}
Compile run
1 [email protected]:test# g++-g-o leveldb_test leveldb_test. cpp .. /leveldb/out-static/libleveldb.a-lpthread-i. /leveldb/include/2 [email protected]:test#./leveldb_test3 Hello ym6536!
Three, LevelDB code structure analysis
The project structure of LEVELDB is quite flat, and the catalogue is not more than 3 levels, as if C + + programmers like it all. Redis is also the source of a bunch of files are under SRC. I am a bit of Java, to be divided into many levels. This can also be learned, the code between the original is highly reusable, forcibly divided into a variety of "beautiful" small pieces, but reduced the quality.
- db/, Database logic
- doc/, MD Documentation
- helpers/, LEVELDB memory version, covered by namespace
- port/, platform-related code
- table/, LSM-related.
Document, compile, source code in Trinity, neat.
[Leveldb] 0. Source code compilation and use