Berkeley DB Reference Guide: Programmer Notes
Полезная информация

Berkeley DB Reference Guide: Programmer Notes

Integrating version 1.85 applications

It is not difficult to upgrade Berkeley DB 1.85 applications to use the Berkeley DB version 2 library. The Berkeley DB version 2 library has a Berkeley DB 1.85 compatibility mode, which you can use by either recompiling your application's source code or by relinking its object files against the version 2 library.

The underlying databases must be converted, however, as the Berkeley DB version 2 library has a different underlying database format.

System Integration

  1. It is possible to maintain both the Berkeley DB 1.85 and Berkeley DB version 2 libraries on your system. However, the db.h include file that was distributed with Berkeley DB 1.85 is not compatible with the db.h file distributed with Berkeley DB version 2, so you will have to install them in different locations. In addition, both the Berkeley DB 1.85 and Berkeley DB version 2 libraries are named libdb.a.

    As the Berkeley DB 1.85 library did not have an installation target in the Makefile, there's no way to know exactly where it was installed on the system. In addition, many vendors included it in the C library instead of as a separate library, and so it may actually be part of libc and the db.h include file may be installed in /usr/include.

    For these reasons, the simplest way to maintain both libraries is to install Berkeley DB version 2 in a completely separate area of your system. The Berkeley DB version 2 installation process allows you to install into a standalone directory hierarchy on your system. See the Building for UNIX platforms documentation for more information and instructions on how to install the Berkeley DB version 2 library, include files and documentation into specific locations.

  2. Alternatively, you can replace Berkeley DB 1.85 on your system with Berkeley DB version 2. In this case, you'll probably want to install Berkeley DB version 2 in the normal place on your system, wherever that may be, and delete the Berkeley DB 1.85 include files, manual pages and libraries.

    To replace 1.85 with version 2, you must either convert your 1.85 applications to use the version 2 API or build the Berkeley DB version 2 library to include Berkeley DB 1.85 interface compatibility code. Whether converting your applications to use the version 2 interface or using the version 1.85 compatibility API, you will need to recompile or relink your 1.85 applications, and you must convert any persistent application databases to the Berkeley DB version 2 database formats.

    There is no requirement that you recompile your Berkeley DB 1.85 applications, as you can simply link their object files against the Berkeley DB version 2 library.

    If you want to recompile your Berkeley DB 1.85 applications, you will have to change them to include the file db_185.h instead of db.h. (The db_185.h file is automatically installed during the Berkeley DB version 2 installation process.) You can then recompile the applications, linking them against the Berkeley DB version 2 library.

    For more information on compiling the Berkeley DB 1.85 compatibility code into the Berkeley DB version 2 library, see Building for UNIX platforms.

    For more information on converting databases from the Berkeley DB 1.85 formats to the Berkeley DB version 2 formats, see the db_dump185 and db_load documentation.

  3. Finally, although we certainly do not recommend it, it is possible to load both Berkeley DB 1.85 and Berkeley DB version 2 into the same library. Similarly, it is possible to use both Berkeley DB 1.85 and Berkeley DB version 2 within a single application, although it's not possible to use them from within the same file.

    The name space in Berkeley DB version 2 has been changed from that of previous Berkeley DB versions, notably version 1.85, for portability and consistency reasons. The only name collisions in the two libraries are the names used by the historic dbm, ndbm and hsearch interfaces, and the Berkeley DB 1.85 compatibility interfaces in the Berkeley DB version 2 library.

    If you are loading both Berkeley DB 1.85 and Berkeley DB version 2 into a single library, remove the historic interfaces from one of the two library builds, and configure the Berkeley DB version 2 build to not include the Berkeley DB 1.85 compatibility API, otherwise you could have collisions and undefined behavior. This can be done by editing the library Makefiles and reconfiguring and rebuilding the Berkeley DB version 2 library. Obviously, if you use the historic interfaces, you will get the version in the library from which you did not remove it. Similarly, you will not be able to access Berkeley DB version 2 files using the Berkeley DB 1.85 compatibility interface, since you have removed that from the library as well.

Converting Applications

Mapping the Berkeley DB 1.85 functionality into Berkeley DB version 2 is almost always a simple, one-to-one mapping. The manual page db_open replaces the Berkeley DB 1.85 manual pages dbopen(3), btree(3), hash(3) and recno(3). You should be able to convert each 1.85 function call into a Berkeley DB version 2 function call using just the db_open documentation.

Some guidelines and things to watch out for:

  1. Most access method functions have exactly the same semantics as in Berkeley DB 1.85, although the arguments to the functions have changed in some cases. To get your code to compile, the most common change is to add the transaction ID as an argument (NULL, since Berkeley DB 1.85 did not support transactions).

  2. You must always initialize DBT structures to zero before using them with any Berkeley DB version 2 function. (They do not normally have to be reinitialized each time, only when they are first allocated. Do this by declaring the DBT structure external or static, or by calling the C library routine bzero(3) or memset(3).

  3. The error returns are completely different in the two versions. In Berkeley DB 1.85, < 0 meant an error, and > 0 meant a minor Berkeley DB exception. In Berkeley DB 2.0, > 0 means an error (the Berkeley DB version 2 functions return errno on error) and < 0 means a Berkeley DB exception. See Error Returns to Applications for more information.

  4. The Berkeley DB 1.85 seq() function has been replaced by cursors in Berkeley DB version 2. The semantics are approximately the same, but cursors require the creation of an extra object (the DBC * object), which is then used to access the database.

    Specifically, the partial key match and range search functionality of the R_CURSOR flag in db->seq() has been replaced by the DB_SET_RANGE flag in DBcursor->c_get.

  5. In version 2 of the Berkeley DB library, additions or deletions into Recno (fixed and variable-length record) databases no longer automatically logically renumber all records after the add/delete point, by default. The default behavior is that deleting records does not cause subsequent records to be renumbered, and it is an error to attempt to add new records between records already in the database. Applications wanting the historic recno access method semantics should specify the DB_RENUMBER flag in the DB_INFO structure.

  6. Opening a database in Berkeley DB version 2 is a much heavier-weight operation than it was in Berkeley DB 1.85. Therefore, if your historic applications were written to open a database, perform a single operation, and close the database, you may observe performance degradation. In most cases, this is due to the expense of creating the environment upon each open. While we encourage restructuring your application to avoid repeated opens and closes, you can probably recover most of the lost performance by simply using a persistent environment across invocations.

While simply converting Berkeley DB 1.85 function calls to Berkeley DB version 2 function calls will work, we recommend that you eventually reconsider your application's interface to the Berkeley DB database library in light of the additional functionality supplied by Berkeley DB version 2, as it is likely to result in enhanced application performance.

Converting Databases

For information on converting application databases from Berkeley DB 1.85 to Berkeley DB 2.0, see the db_dump185 and db_load documentation.