1 Introduction
First, we need to understand some basic features of Berkeley DB. There is an article on the IBM development website that gives a clear introduction to it;
This article describes BDB's design ideas, core data structures, and data access algorithms. It also provides examples of common functions. It is a good entry to BDB;
Read the following url:
Http://www.ibm.com/developerworks/cn/linux/l-embdb/index.html
My current problem is that if we need to use BDB, we need to solve the following problems:
How to use common operations?
Data Access Algorithm selection?
What is the performance? A performance test is required;
How can I create multiple indexes when BDB supports multiple indexes?
How can we create appropriate indexes for specific environments?
How can we encapsulate it for our convenience?
How can I port SQL to NOSQL?
In the next study, I will focus on solving the above problems;
In this article, we will learn common BDB operations.
2. Download and installDownload Berkeley DB
Official oracle Berkeley DB Website:
Http://www.oracle.com/us/products/database/berkeley-db/overview/index.html
Http://www.oracle.com/technetwork/products/berkeleydb/downloads/index.html? SsSourceSiteId = ocomen
Download version: Berkeley DB 6.0.20.tar.gz
Install
Go to the installation directory of the corresponding environment:
Cd build_unix
../Dist/configure -- prefix =/opt/app/todeav1/soft/bdb/
Environment Variable deployment
BDB_HOME =/opt/app/todeav1/colin/db; export BDB_HOME
LIBPATH = $ BDB_HOME/bin: $ LIBPATH; export LIBPATH
LD_LIBRARY_PATH = $ BDB_HOME/lib: $ LD_LIBRARY_PATH; export LD_LIBRARY_PATH
Compile
G ++-o test demo. cpp-I $ BDB_HOME/include/-L $ BDB_HOME/lib/-ldb-lpthread
3. Common Operations
Open Database
First, call the db_create () function to create an instance of the DB structure, and then call the DB-> open () function to complete the real open operation.
Berkeley DB encapsulates all database operations in a structure named DB. The function db_create () is used to create a structure.
Opening a file stored on the disk as a database is completed by the DB-> open () function. Its prototype is as follows:
?
The "DATABASE" parameter indicates that the corresponding disk file name is demo. db; the parameter "DB_BTREE" indicates that the underlying data structure of the database is B. The parameters "DB_CREATE" and "0664" indicate that a new database file is created when the database file does not exist, set the property value of the file to 0664.
Error handling is a routine check required when you open the database. This can be done by calling the DB-> err () function. The "ret" parameter is the error code returned after the Berkeley DB function is called. Other parameters are used to display structured error messages.
Insert
To add data to the Berkeley DB database, you can call the DB-> put () function. Its prototype is as follows:
Int DB-> put (DB * db, DB_TXN * txnid, DBT * key, DBT * data,
U_int32_t flags );
When adding data to a database, if the given keyword already exists, most applications will have existing data.
The coverage principle is adopted. That is to say, if a "sport/basketball" pair has been saved in the database, call the DB-> put () function again to add a "sport/football" pair, the previously stored data will be overwritten. However, Berkeley DB allows you to specify the parameter "DB_NOOVERWRITE" when calling the DB-> put () function. It declares that existing data in the database is not overwritten. Once the "DB_NOOVERWRITE" mark is given, if the DB-> put () function finds that the given keyword already exists in the database during execution, the Key/Data pair cannot be successfully added to the database, therefore, the error code "DB_KEYEXIST" is returned ".
Retrieve Data
You can call the DB-> get () function to retrieve data from the Berkeley DB database. Its prototype is as follows:
Int DB-> get (DB * db, DB_TXN * txnid, DBT * key, DBT * data,
U_int32_t flags );
When performing data retrieval, it is essential to process the return values of the DB-> get () function, because it carries information such as whether the retrieval operation is successful or not. The returned values of the DB-> get () function are listed below:
◆ The 0 function is successfully called and the specified keyword is found;
◆ The DB_NOTFOUND function is successfully called, but the specified keyword is not found;
◆ Failed to call a function greater than 0. A system error may occur.
Delete
You can call the DB-> del () function to delete data from the Berkeley DB database. Its prototype is as follows:
Int DB-> del (DB * db, DB_TXN * txnid, DBT * key, u_int32_t flags );
To delete data, you only need to provide the corresponding keywords, and do not need to specify the corresponding data.
?
Close Database
Closing a database is an indispensable part of a complete database operation process. This is because Berkeley DB depends on the system's underlying buffer mechanism. That is to say, only when the database is shut down normally can all the modified data be written to the disk, at the same time, the resources it occupies can be truly released. The database closure operation is completed by calling the DB-> close () function. Its prototype is as follows:
Int DB-> close (DB * db, u_int32_t flags );
Refresh cache to Hard Disk
All Berkeley DB operations are performed in the memory. When the database handle is disabled, all data is written to the hard disk. If data needs to be synchronized to the hard disk, you can call the sync () function:
Note: This function has no parameters in versions earlier than 4.3, and parameters are added in later versions. Note that this function is compatible in earlier versions;
Dbp-> sync (dbp, 0 );
More
This article provides a detailed description of each operation during insertion, and provides a link to the document for further study;
Detailed description of the Berkeley DB sample program (1)
Http://www.bdbchina.com/2009/02/berkeley-db%E7%A4%BA%E4%BE%8B%E7%A8%8B%E5%BA%8F%E8%AF%A6%E8%A7%A3-1/
4. More learning materials
Official Berkeley DB developer documentation
Http://docs.oracle.com/cd/E17076_03/html/index.html
Official homepage: http://www.oracle.com/database/berkeley-db/db/index.html
Product: http://www.oracle.com/technology/software/products/berkeley-db/index.html
Berkeley DB China R & D team blog:
Http://www.bdbchina.com/
Documentation Center: http://www.oracle.com/technology/documentation/berkeley-db/db/index.html
Berkeley DB official forum seems to have been unmaintained ):
Https://forums.oracle.com/community/developer/english/berkeley_db_family
This article is from the "colin" blog, please be sure to keep this source http://me115.blog.51cto.com/86104/1298134