Introduction to Database Leveldb used by bitcoin clients
Brief Introduction
The bitcoin core client uses Google's LEVELDB database to store block-chain metadata. LEVELDB is a highly efficient KV database implemented by Google and is open source.
One of its main features is that the performance of the write is very good, applicable to the Bitcoin client this application scenario.
The following figure is a snapshot of the storage model after leveldb runs for a period of time.
In-memory memtable and immutable memtable and several main files on disk: Current files, manifest files, log files, and sstable files.
Log file is almost all the database software necessary mechanism, mainly for data abnormal recovery and data synchronization.
The sstable file is the core storage structure on disk, which is tiered storage. This is also the origin of Leveldb name. using the sample
LEVELDB is a database, but it is not like other mainstream databases (MySQL, Oracle, etc.) that can provide client connection services. It is only a Lib library based on C + +, so we use it is very simple, directly in the Project Link Lib library file, and then the code contains headers can be used.
Here are the steps I used to write leveldb using vs2010 in Windows 10 environments .
The first step is of course to get the Leveldb library and header files. Since LEVELDB is open source, we can download the source code, and then compile the build library file in the Windows environment.
LEVELDB Open Source Address
I am here to compile leveldb do not do too much explanation, there are many tutorials on the internet. (Note that the compilation process requires boost) You can download my compiled library file directly from the address below.
Leveldb Windows library file download
After downloading, configure the project to VS2010 (the configuration process is omitted), the code is as follows:
#include <assert.h> #include <string.h> #include <iostream> #include "leveldb/db.h" using Namesp
Ace STD;
int _tmain (int argc, _tchar* argv[]) {leveldb::D b *db;
Leveldb::options Options;
Options.create_if_missing = true;
Open database Leveldb::status Status = leveldb::D b::open (Options, "/tmp/testdb", &db);
ASSERT (Status.ok ());
string key = "Name";
String value = "Pony";
Write status = Db->put (Leveldb::writeoptions (), key, value);
ASSERT (Status.ok ());
Read status = Db->get (Leveldb::readoptions (), key, &value);
ASSERT (Status.ok ());
cout << value << Endl;
Delete status = Db->delete (Leveldb::writeoptions (), key);
ASSERT (Status.ok ());
Status = Db->get (Leveldb::readoptions (), key, &value); if (!status.ok ()) {Cerr << key << "" << status.
ToString () << Endl; else {cout << key << == = "<< value << Endl;
//Close database Delete db;
return 0;
}
After the operation is successful, the production database files can be seen below the/TMP/TESTDB under the engineering directory.
Reference
1. How to operate LEVELDB database, to realize the deletion of LEVELDB library introduction