Add Book to My BookshelfPurchase This Book Online

Chapter 16 - Miscellaneous Routines

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Database Management
Most versions of UNIX provide a library to maintain a rudimentary database. This database is basically an on-disk hash table (see above), designed for efficiency. The routines can handle very large databases (up to a billion blocks), and require only one or two filesystem accesses to retrieve an item.
Although not necessary on most versions of SVR4, HP-UX 10.x requires linking with the -lndbm library to use these functions.
    #include <ndbm.h>
    DBM *dbm_open(char *file, int flags, int mode);
    void dbm_close(DBM *db);
    int dbm_store(DBM *db, datum key, datum content, int flags);
    datum dbm_fetch(DBM *db, datum key);
    int dbm_delete (DBM *db, datum key);
    datum dbm_firstkey(DBM *db);
    datum dbm_nextkey(DBM *db);
    int dbm_clearerr(DBM *db);
    int dbm_error(DBM *db);
Before using the other functions, the database must be opened with dbm_open. The database is stored in two files, one with a “.dir” suffix and the other with a “.pag” suffix. The root name of the file (without the suffixes) should be passed to dbm_open in the file parameter. The flags and mode arguments are given as for the open function. On success, dbm_open returns a pointer to type DBM; otherwise it returns NULL. A database can be closed with dbm_close.
Keys and contents are described with objects of type datum:
    typedef struct {
        char    *dptr;
        int      dsize;
    } datum;
The dptr field points to the data, and dsize indicates the size of the data. Note that both keys and contents may be arbitrary data types.
An item is stored in the database by calling dbm_store. The db argument is a pointer to an open database. The key parameter is the key under which the data in the content parameter is to be stored. The flags argument may be one of the following:
DBM_INSERT
Insert an item into the database. If an item with this key is already in the database, do not replace it with the new value. If an existing entry is found, dbm_store returns 1, otherwise it returns 0.
DBM_REPLACE
Insert an item into the database. If an item with this key is already in the database, replace it with the new value.
To retrieve an item from the database, the dbm_fetch function is used. The db parameter specifies an open database, and the key for the item is given in key. The content for that key is returned as a datum type; note that the structure itself is returned, not a pointer to the structure. If no item was found for the key, then the dptr field of the datum structure will be null.
To delete an item with key key from the database referred to by db, the dbm_delete function is used.
The dbm_firstkey and dbm_nextkey functions can be used to make a linear pass through all keys in the database as follows:
    #include <ndbm.h>
    ·
    ·
    ·
    for (key = dbm_firstkey(db); key.dptr != NULL; key = dbm_nextkey(db)) {
        ...
        content = dbm_fetch(db, key);
        ...
    }
    ·
    ·
    ·
The dbm_error function returns non-zero when an error has occurred in reading or writing the database referenced by db; the dbm_clearerr function clears the error condition.
Porting Notes
Some particularly old versions of UNIX may offer the predecessor to the -lndbm library, called the -ldbm library. This version of the library uses functions with the same names, except without the leading dbm_. They do not accept a db argument, and handle only one open database at a time. Replacing these functions with the newer ones is straightforward.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback