Leveldb source code analysis 1

Source: Internet
Author: User
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
  1. Git clone https://code.google.com/p/leveldb/

Compile leveldb after the download is complete.

Plain code
  1. CD leveldb
  2. Make all

The libleveldb. A library file is generated. Copy the header file of leveldb to the plain code under/usr/include.

  1. CP-R./include/leveldb/usr/include/

This completes leveldb installation.

3. leveldb client program example

The test procedure is as follows:

CPP Code
  1. # Include <assert. h>
  2. # Include <string>
  3. # Include <leveldb/DB. h>
  4. # Include <iostream>
  5. Int main ()
  6. {
  7. // Open a database
  8. Leveldb: DB * dB;
  9. Leveldb: Options options;
  10. Options. create_if_missing = true;
  11. Leveldb: Status status = leveldb: DB: open (options, "/tmp/testdb", & dB );
  12. Assert (status. OK ());
  13. // Write <key1, value1>
  14. Const int write_times = 10000;
  15. Int I = 0;
  16. STD: String key = "key ";
  17. STD: string value = "value ";
  18. Status = DB-> put (leveldb: writeoptions (), key, value );
  19. Assert (status. OK ());
  20. // Read value1 by key1
  21. Status = DB-> get (leveldb: readoptions (), key, & value );
  22. Assert (status. OK ());
  23. STD: cout <value <STD: Endl;
  24. // Delete Databse
  25. Delete dB;
  26. Return 0;
  27. }

Copy the generated libleveldb. A to the same directory of the source file and compile the program:

Plain code
  1. G ++-O test. cpp libleveldb. A-lpthread

The test executable file is generated. The test result is as follows:

Plain code
  1. [Root @ mdss33 test] #./test
  2. Value

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.