© 2009 Mitch Richling
You will find several simple examples of programs to get you started with the traditional UNIX database libraries NDBM, GDBM or Berkeley DB. See the notes below for background information. The makefile is here.
DBM, originally developed at UC Berkeley, is a VERY high performance, key-value pair database library. Modern programmers might think of the key-value pair database as an associative container backed with a persistent disk store. DBM doesn't support transactional operations, any form of concurrency, or general query (SQL) capabilities. DBM only allows a program to have ONE database open at any time, and data payloads are limited to 1K (i.e. the key and value must each be less than 1K). Later, NDBM, which stands for New-DBD, was developed as an improved version including simple access to multiple databases at the same time. Later versions of NDBM removed the limits on the size of key and value data. Unfortunately, NDBM uses a different file format than DBM. While DBM and NDBM are BSDisms, the interfaces are widely available on modern System V derived systems like Solaris and Linux. NDBM appeared in the Single UNIX Specification version 2, and is thus available on a wide array of systems.
Even with NDBM widely available, an improved GNU-DBM, called GDBM, was has been developed. As part of the GNU project, GDBM is even more widely available and may be a useful option if portability to odd hardware or software platforms is a must. GDBM has many improvements, but it uses yet another file format. GDBM has compatibility modes for both DBM/NDBM file formats and source code. While it is tempting to use the compatibility modes and simply stick with the older interfaces, it is important to note that the compatibility modes provide compatibility with the limitations of the older libraries too! GDMB has several notable improvements over standard NDBM. It supports multiple readers into a DB, has no size limits on records, much better tuning options, and better error reporting. GDBM provides all of this while still providing an almost identical API -- an NDBM program can be converted into GDBM mostly via search and replace. The removal of size limitations on what can be stored has come at a cost. This cost is increased memory allocation and deallocation overhead, and a requirement for users of GDBM to free up memory allocated by the library. This is one of the most common sources of memory leaks in newly converted GDBM programs -- one must free the memory allocated by functions like gdbm_fetch(). I highly recommend stepping up to the better capabilities of GDBM if you are going to use it -- why not, GDBM compiles on most any platform worth using!
An even more sophisticated library called Berkeley DB is commercially available. An older version of Berkeley DB, around v1.85, is included with many BSD variants and the NDBM interface is implemented using this version of Berkeley DB. As a result, the NDBM implementations on BSD systems have no size limitations on what can be stored and no user required memory management! Modern versions of Berkeley DB support concurrency, transactions, and a variety of sophisticated features. All of this wonderful stuff is implemented while still supporting the same design principles of the other DBMs -- simple, clean, and easy to use. The API is quite different from the other DBMs -- more consistent and less difficult to use in my opinion. I HIGHLY recommend the Berkeley DB package if you are looking for uncompromising performance, ease of use, and powerful features! (Hmm, that sounded like a commercial. I represent Sleepycat in no way, I just really like the product)