Installation and use of leveldb
This article mainly explains how to install and use leveldb. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to install and use leveldb.
Brief introduction of leveldb installation and use of leveldb
Leveldb is a fast key-value pair storage database developed by Google with C++, providing sequential mapping from string keys to string values.
Leveldb installation downloads leveldbgit clone https://github.com/google/leveldb.git compiling leveldbcd leveldb/make
The compiled dynamic libraries and static libraries are respectively under out-shared,out-static:
Ls leveldb/out-shared/libleveldb.so.1.20ls leveldb/out-static/libleveldb.an install leveldb
Only dynamic libraries need to be installed, and static libraries can be linked directly when you compile
# cp leveldb header filesudo cp-r / leveldb/include/ / usr/include/# cp lib to / usr/lib/sudo cp / leveldb/out-shared/libleveldb.so.1.20 / usr/lib/# create linksudo ln-s / usr/lib/libleveldb.so.1.20 / usr/lib/libleveldb.so.1sudo ln-s / usr/lib/libleveldb.so.1.20 / usr/lib/libleveldb.so# update lib cachesudo ldconfig
Check to see if the installation is successful
Ls / usr/lib/libleveldb.so*# shows that the following three files are installed successfully / used by usr/lib/libleveldb.so.1.20/usr/lib/libleveldb.so.1/usr/lib/libleveldb.soleveldb
Let's write a hello_leveldb.cc to test our leveldb.
# include
# include
# include
# include
/ / include the necessary header files
# include
Using namespace std
Int main (void) {leveldb::DB * db = nullptr; leveldb::Options options; / / create the database options.create_if_missing = true; / / if the database does not exist in / tmp/testdb leveldb::Status status = leveldb::DB::Open (options, "/ tmp/testdb", & db); assert (status.ok ()); std::string key = "A" Std::string value = "a"; std::string get_value; / / write key1-> value1 leveldb::Status s = db- > Put (leveldb::WriteOptions (), key, value); / / read the value corresponding to key:people if the write is successful
If (s.ok ()) s = db- > Get (leveldb::ReadOptions (), "A", & get_value)
/ / output if (s.ok ()) if it is read successfully
Cout