Article title: open-source embedded database BerkeleyDB (1 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Although a C/S-based relational database system like MySQL represents the mainstream of database applications, it cannot meet the needs of all application scenarios. Sometimes we only need a simple database system based on disk files. This not only avoids the installation of a large database server, but also simplifies the design of database applications. Berkeley DB is based on this idea.
Introduction to Berkeley DB Berkeley DB is an open-source embedded database management system that provides high-performance data management services for applications. Application programmers only need to call some simple APIs to access and manage data. Unlike common database management systems (such as MySQL and Oracle), Berkeley DB does not have a database server concept. The application does not need to establish a network connection with the database service in advance, but stores, queries, modifies, and deletes data through the Berkeley DB function library embedded in the program.
Berkeley DB provides practical API interfaces for many programming languages, including C, C ++, Java, Perl, Tcl, Python, and PHP. All database-related operations are performed in a unified manner by the Berkeley DB function library. In this way, no matter multiple processes in the system or multiple threads in the same process, you can call the function to access the database at the same time. The underlying data locks, transaction logs, and storage management are all implemented in the Berkeley DB function library. They are completely transparent to applications. As the saying goes: "The Sparrow is small and dirty ." Although the Berkeley DB function library is only about KB, it can be used to manage up to TB of data, and its performance in many aspects can compete with commercial database systems. For concurrent data operations, Berkeley DB can easily cope with the situation where thousands of users access the same database at the same time. In addition, if you want to manage the database on an embedded system with limited resources, Berkeley DB may be the only correct choice.
As an embedded database system, Berkeley DB has unique advantages in many aspects. First, because its applications and database management systems run in the same process space, data operations can avoid tedious inter-process communication, therefore, the communication overhead is naturally reduced to a very low level. Second, Berkeley DB uses simple function call interfaces to complete all database operations, rather than SQL languages that are frequently used in database systems. This avoids the overhead required to parse and process the structured query language.
Basic concepts Berkeley DB simplifies the database operation mode and introduces some new basic concepts, making it easier to access and manage databases. Before using the function library provided by Berkeley DB to write database applications, you need to understand the following basic concepts.
Keywords and data Key and Data are the basis for Berkeley DB for database management. Key/Data pairs composed of the two are shown in Table 1) it constitutes a basic structural unit in the database, and the whole database is actually composed of many such structural units. By using this method, developers only need to provide keywords to access the corresponding data when using the API provided by Berkeley DB to access the database.
Table 1 Key/Data pairs
To save "sport" and "football" in the first line to the Berkeley DB database, you can call the data storage interface provided by the Berkeley DB function library. In this case, "sport" and "football" are treated as keywords and data respectively. If you need to retrieve the data from the database, you can use "sport" as the keyword for query. In this case, the interface function provided by Berkeley DB returns the corresponding data "football ".
Keywords and data are represented by a simple structure named DBT in Berkeley DB. In fact, both of them can be binary data of any length, while DBT mainly stores the corresponding memory address and its length. its structure is as follows:
When Berkeley DB is used for data management, a keyword corresponds to one data by default. However, you can configure a database as a keyword to correspond to multiple data.
Object handle Most functions defined in the Berkeley DB function library follow the same calling principle: first create a structure and then call some methods in the structure. From the perspective of program design, this is very similar to the object-oriented design principle, that is, to create an instance of an object and then call some methods of the instance. For this reason, Berkeley DB introduces the object handle concept to represent the instantiated structure, and calls the member functions in the structure the method of the handle.
The introduction of object handles allows programmers to access and operate the Berkeley DB database with the idea of object-oriented, even if they are using a structured language like C. For example, for database opening operations, you can call the open function provided by the DB object handle. its prototype is as follows:
Error handling For any function library, how to handle errors in a unified manner is a matter of consideration. All functions provided by Berkeley DB follow the same error handling principle, that is, after the function is successfully executed, zero is returned; otherwise, a non-zero value is returned.
For system errors (such as insufficient disk space and insufficient access permissions), a standard value is returned. for non-system errors, a specific error code is returned. For example, if there is no data corresponding to a specific keyword in the database, an error will occur when retrieving data using this keyword. The return value of the function is DB_NOTFOUND, indicating that the requested keyword does not appear in the database. All standard errno values are greater than zero, while the special error codes defined by Berkeley DB are smaller than zero.
It is neither realistic nor practical to ask programmers to remember all the error codes, because Berkeley DB provides corresponding functions to get the error description corresponding to the error codes. Once an error occurs, you only need to call the db_strerror () function to obtain the error description, and then call DB-> err () or DB-> errx () you can easily output formatted error messages.
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.