1. Introduction to leveldb
Leveldb is a key-value storage engine developed by Google and announced open source code with the BSD license.
2. leveldb download and Installation
Leveldb is hosted on Google code. You can use git to download the source code:
Plain code
- Git clone https://code.google.com/p/leveldb/
Compile leveldb after the download is complete.
Plain code
- CD leveldb
- Make all
The libleveldb. A library file is generated. Copy the header file of leveldb to the plain code under/usr/include.
- CP-R./include/leveldb/usr/include/
This completes leveldb installation.
3. leveldb client program example
The test procedure is as follows:
CPP Code
- # Include <assert. h>
- # Include <string>
- # Include <leveldb/DB. h>
- # Include <iostream>
- Int main ()
- {
- // Open a database
- Leveldb: DB * dB;
- Leveldb: Options options;
- Options. create_if_missing = true;
- Leveldb: Status status = leveldb: DB: open (options, "/tmp/testdb", & dB );
- Assert (status. OK ());
- // Write <key1, value1>
- Const int write_times = 10000;
- Int I = 0;
- STD: String key = "key ";
- STD: string value = "value ";
- Status = DB-> put (leveldb: writeoptions (), key, value );
- Assert (status. OK ());
- // Read value1 by key1
- Status = DB-> get (leveldb: readoptions (), key, & value );
- Assert (status. OK ());
- STD: cout <value <STD: Endl;
- // Delete Databse
- Delete dB;
- Return 0;
- }
Copy the generated libleveldb. A to the same directory of the source file and compile the program:
Plain code
- G ++-O test. cpp libleveldb. A-lpthread
The test executable file is generated. The test result is as follows:
Plain code
- [Root @ mdss33 test] #./test
- Value