(reprint + Finishing) Leveldb installation and examples

Source: Internet
Author: User
Tags assert

1 : Introduction

Leveldb is a very efficient KV database implemented by Google and can be stored in the order of string key values. The current version 1.2 can support the billion level of data volume. It also has very high performance at this number level.

Leveldb is a C + + library that can be used in many situations. For example, for a Web browser to store the cache of recent access pages, or for the operating system storage installation package list, or to apply storage user settings parameters. In fact, the INDEXEDDB HTML5 API deployed in the new version of the Chrome browser is built on LEVELDB. Google's own database bigtable in charge of millions of data sheets is also used leveldb.

Today I compiled it under Linux (FEDRA14) and it feels good. There are a lot of problems in the middle, recorded. For the purpose of learning.

Here I want to vent a bit of depressed feeling. I have learned the programming under Linux, but not very deep, most of the time I groped, there is no communication around the people, and then most of the surrounding in Java, C #,. NET no one in the study of c\c++, especially Linux. Some graduate students don't yet know what Ubuntu is. I always like technology, especially Linux, anyway is a pursuit of it. The road of his own groping is very bumpy, know a person to explore the difficulties, so encounter problems will write down their own blog, to oneself is a kind of learning, but also hope that other people have common interests to help, if you can let no basic people can understand, and according to the example of their own operation success, is also a very fulfilling thing.

2 : Compiling source code

I use the version of Release 1.2 2011-05-16, this need to use SVN download, the process is not much to say.

2.1 Unzip the file, my path is/HOME/LYC/LEVELDB/LEVELDB

2.2 Enter the unzipped path, CD/HOME/LYC/LEVELDB/LEVELDB

2.3 Compile, this is a simple command makefile, note that this compilation requires g++ support

Put in the Linux system to extract and compile, in fact, compiled leveldb is very simple, unzip the download package into the directory to directly execute make. Several files are generated in the current directory after the compilation is complete:

LIBLEVELDB.A libleveldb.so libleveldb.so.1 libleveldb.so.1.13 (I am currently downloading the 1.13.0 version), where libleveldb.so.1,libleveldb.so are libleveldb.so.1.13 's soft connection

2.4 After compiling a library file will appear under the/HOME/LYC/LEVELDB/LEVELDB path LIBLEVELDB.A, which can be used in your own project

3 : Leveldb Performance Testing

The default compile Makefile command does not generate a test program, and if you want to generate these auxiliary programs, you can use the command makefile test

As a result, test programs such as executable db_bench,arena_test,db_test are generated under the/HOME/LYC/LEVELDB/LEVELDB path. This is not enough, my goal is to use LEVELDB in their own programs, so focus on the use of library file LIBLEVELDB.A.

The source package comes with several performance test programs, one is LEVELDB's own test program: Db_bench, and two are Db_bench_sqlite3, db_bench_tree_db, respectively compiled make Db_bench make db_ Bench_sqlite3 Mke db_bench_tree_db, the back two because the dependency relationship does not necessarily compile and pass, the specific problem needs to be solved concretely.

4 : Leveldb header File Preparation

The problem with the header files took a lot of time to solve, in fact, very simple, to/home/lyc/leveldb/leveldb path (this specific to their own file path), with the command Cp-r Include/leveldb/usr/local/include. Copy the contents of the./include/leveldb folder to the/usr/local/include path.

Note that you want to cut to the root user, or you do not have permission to execute.

Copy the Leveldb directory under include to/usr/include/: Cp-r include/leveldb/usr/include/

5 : Sample Program

Copy the LIBLEVELDB.A and db.h to the same path of the program.

The compile command is: g++-o sa Main.cpp libleveldb.a-lpthread

Perform:

[Email protected] test]$./sa

Results:

Open DB OK

[Email protected]

SOURCE Main.cpp:

#include <assert.h>

#include <iostream>

#include "Db.h"

using namespace Std;

int main (int argc,char * argv[])

{

LEVELDB::D b* DB;

Leveldb::options Options;

Options.create_if_missing = true;

std::string dbpath = "TestDB";

Leveldb::status Status = leveldb::D b::open (Options, DBPath, &db);

ASSERT (Status.ok ());

std::string key1 = "LYC";

std::string key2 = "[email protected]";

cout<< "Open db OK" <<std::endl;

std::string value;

Leveldb::status s;

s = Db->put (Leveldb::writeoptions (), Key1, Key2),/*key1 and Key2 as a pair of key-value pairs inserted */

s = Db->get (Leveldb::readoptions (), Key1, &value);/* Returns the corresponding value value based on key */

cout<<value<<std::endl;

Delete db;/* Deleting database */

return 0;

}


6 : Precautions

6.1 Compile in addition to the path (LIBLEVELDB.A) of the library file and the line libraries flag (-lpthread), after execution in the current file produces a folder TestDB saved the inserted data.

6.2 Leveldb is used for some single-cell data such as name-value pairs, and the amount of data is much larger than memory and needs to be persisted. Very suitable for large-scale language model file storage.

6.3 The inserted data is two string pairs of one key, and the other is value, the query can be based on the key to obtain value, instead of.

Simple test Cases

Main.cpp

#include <assert>

#include <iostream>

#include <leveldb/db.h>

Intmain (int Argc,char * argv[])

{

LEVELDB::D b* DB;

Leveldb::options Options;

Options.create_if_missing = true;

std::string dbpath = "TestDB";

Leveldb::status Status =leveldb::D b::open (Options, DBPath, &db);

ASSERT (Status.ok ());

std::string key = "Test";

std::string val = "Test_value";

s = Db->put (Leveldb::writeoptions (), Key, Val);

Val.clear ();

s = Db->get (Leveldb::readoptions (), key, &val);

Std::cout << key << ":" << val << Std::endl;

}


Compile: Copy the previously compiled LIBLEVELDB.A to the same directory as the above code and g++-O leveldbtest main.cpp libleveldb.a–lpthread

#include <iostream>
#include "Leveldb/db.h"

using namespace Std;
using namespace Leveldb;

int main () {
DB *db;
Options op;
Op.create_if_missing = true;
Status s = Db::open (OP, "/tmp/testdb", &db);

if (S.ok ()) {
cout << "Create successfully" << Endl;
s = Db->put (Writeoptions (), "ABCD", "1234");
if (S.ok ()) {
cout << "put successfully" << Endl;
String value;
s = Db->get (Readoptions (), "ABCD", &value);
if (S.ok ()) {
cout << "Get Successfully,value:" << value << Endl;
}
else{
cout << "Get Failed" << Endl;
}
}
else{
cout << "put failed" << Endl;
}
}
else{
cout << "Create failed" << Endl;
}
Delete db;
return 0;
}

For you to add:
The compile command is g++ ldbtest.cpp-o ldbtest-l.-i./include-lpthread-lleveldb

(The working directory is the LEVELDB directory) include is the LEVELDB header file, the-L search the library in order to LIBLEVELDB.A can be called.

In addition, the compile time can be said in the makefile snappy compression also with, and Google Perfecttools, this optional. Compression is still necessary.

(reprint + Finishing) Leveldb installation and examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.