Utilizing Gdbm, GNU's Easy-to-Use Database Management Library
Lately, a number of open source relational databases have received favorable press. Indeed, MySQL and PostgreSQL have been touted as capable alternatives to Oracle and Sybase, the traditional heavyweights of the commercial RDBMS world.
But let's say you, the programmer, need something simpler. You may have outgrown plain-text "flatfiles," but you might not be quite ready for the planning and maintenance that goes along with a full-fledged RDBMS.
Further, as you likely run Linux or some version of BSD, Microsoft Access isn't aviable option, either. Isn't there an intermediate data storage solution, somewhere between clunky text files and full-blown relational databases?
Fortunately, the answer is yes: Gdbm!
Gdbm, or the GNU Database Manager, is a fully-capable, yet simple means of storing and retrieving data. There aren't 1,001 features and procedures to memorize, making for a gentle learning curve. Tuning is not an issue, and you don't need sophisticated algorithms to get the most out of your data store. Yet, you can still use gdbm-powered databases for customer files, back ends to Websites, and anywhere else that an RDBMS would be appropriate.
On the downside, the speed isn't as great as it might be with other systems. Databases created with gdbm aren't indexed, meaning that you certainly don't want to use it for a multi-terabyte data warehouse. But, for small to mid-sized data collections, the time differential is negligible.
Best of all, you don't need to worry about mounting and unmounting anything created with gdbm. Nor is there any server to run. Your data is simply stored as a regular file on disk.
Now, how do we use it?
The first step is to make sure that you have gdbm. Most Linux distributions include it, but you can make a quick check by looking in your /lib directory. If you see something along the lines of libgdbm.so.Now that you have the gdbm library, we'll jump right in and do some coding.
You access the gdbm library by creating C programs that use functions found in the gdbm.h header file. As with other libraries, you link with a simple command line flag -- in this case, -lgdbm.
Gdbm Functions
Creating a new database and opening an existing one are both accomplished with the same function: gdbm_open(). Here's how it works:GDBM_FILE dbf; dbf = gdbm_open(name, block_size, flags, mode, fatal_func); |
char *name -- The name of your database. This will be the name of the binary data file that's written to disk.int block_size -- A block is a group of bits that's transferred as a single unit. In this case, 512 is both the minimum and the default. Once the database is created, the block size cannot be changed.int flags -- GDBM_READER Opens the database in read-only mode. Multiple programs can access the database while in GDBM_READER mode. GDBM_WRITER Allows both reading and writing. Only one program can access the database in GDBM_WRITER mode. GDBM_WRCREAT Creates a new database if none previously exists; otherwise the same as GDBM_WRITER. GDBM_NEWDB Creates a new database regardless of whether another one with the same name exists (i.e., it will overwrite old databases); otherwise, it's the same as GDBM_WRITER.int mode -- This sets the file permissions of the database, just as you would with chmod. A sample value would be 0644. void (*fatal_func) () -- A function for gdbm to call if it detects a fatal error. The only parameter of this is a string. If the value of NULL is provided,gdbm will use a default function. |
gdbm_close(dbf); |
Page 1 of 3
This article was originally published on January 4, 2001