dbm_clearerr()

NAME

dbm_clearerr, dbm_close, dbm_delete, dbm_error, dbm_fetch, dbm_firstkey, dbm_nextkey, dbm_open, dbm_store - database functions

SYNOPSIS

#include <ndbm.h>

int dbm_clearerr(DBM *db);
void dbm_close(DBM *db);
int dbm_delete(DBM *db, datum key);
int dbm_error(DBM *db);
datum dbm_fetch(DBM *db, datum key);
datum dbm_firstkey(DBM *db);
datum dbm_nextkey(DBM *db);
DBM *dbm_open(const char *file, int open_flags, mode_t file_mode);
int dbm_store(DBM *db, datum key, datum content, int store_mode);

DESCRIPTION

These functions create, access and modify a database.

A datum consists of at least two members, dptr and dsize. The dptr member points to an object that is dsize bytes in length. Arbitrary binary data, as well as character strings, may be stored in the object pointed to by dptr.

The database is stored in two files. One file is a directory containing a bit map of keys and has .dir as its suffix. The second file contains all data and has .pag as its suffix.

The dbm_open(3) function opens a database. The file argument to the function is the pathname of the database. The open_flags argument has the same meaning as the flags argument of open(2) except that a database opened for write-only access opens the files for read and write access and the behaviour of the O_APPEND flag is unspecified. The file_mode argument has the same meaning as the third argument of open(2).

The dbm_close(3) function closes a database. The argument db must be a pointer to a dbm structure that has been returned from a call to dbm_open(3).

The dbm_fetch(3) function reads a record from a database. The argument db is a pointer to a database structure that has been returned from a call to dbm_open(3). The argument key is a datum that has been initialised by the application program to the value of the key that matches the key of the record the program is fetching.

The dbm_store(3) function writes a record to a database. The argument db is a pointer to a database structure that has been returned from a call to dbm_open(3). The argument key is a datum that has been initialised by the application program to the value of the key that identifies (for subsequent reading, writing or deleting) the record the program is writing. The argument content is a datum that has been initialised by the application program to the value of the record the program is writing. The argument store_mode controls whether dbm_store(3) replaces any pre-existing record that has the same key that is specified by the key argument. The application program must set store_mode to either DBM_INSERT or DBM_REPLACE. If the database contains a record that matches the key argument and store_mode is DBM_REPLACE, the existing record is replaced with the new record. If the database contains a record that matches the key argument and store_mode is DBM_INSERT, the existing record is not replaced with the new record. If the database does not contain a record that matches the key argument and store_mode is either DBM_INSERT or DBM_REPLACE, the new record is inserted in the database.

The sum of the sizes of a key/content pair must not exceed the internal block size. Moreover, all key/content pairs that hash together must fit on a single block. The dbm_store(3) function returns an error in the event that a disk block fills with inseparable data.

The dbm_delete(3) function deletes a record and its key from the database. The argument db is a pointer to a database structure that has been returned from a call to dbm_open(3). The argument key is a datum that has been initialised by the application program to the value of the key that identifies the record the program is deleting.

The dbm_firstkey(3) function returns the first key in the database. The argument db is a pointer to a database structure that has been returned from a call to dbm_open(3).

The dbm_nextkey(3) function returns the next key in the database. The argument db is a pointer to a database structure that has been returned from a call to dbm_open(3). The dbm_firstkey(3) function must be called before calling dbm_nextkey(3). Subsequent calls to dbm_nextkey(3) return the next key until all of the keys in the database have been returned.

The dbm_error(3) function returns the error condition of the database. The argument db is a pointer to a database structure that has been returned from a call to dbm_open(3).

The dbm_clearerr(3) function clears the error condition of the database. The argument db is a pointer to a database structure that has been returned from a call to dbm_open(3).

These database functions support key/content pairs of at least 1023 bytes.

The dptr pointers returned by these functions may point into static storage that may be changed by subsequent calls.

These interfaces need not be reentrant.

RETURN VALUE

The dbm_store(3) and dbm_delete(3) functions return 0 when they succeed and a negative value when they fail.

The dbm_store(3) function returns 1 if it is called with a flags value of DBM_INSERT and the function finds an existing record with the same key.

The dbm_error(3) function returns 0 if the error condition is not set and returns a non-zero value if the error condition is set.

The return value of dbm_clearerr(3) is unspecified.

The dbm_firstkey(3) and dbm_nextkey(3) functions return a key datum. When the end of the database is reached, the dptr member of the key is a null pointer. If an error is detected, the dptr member of the key is a null pointer and the error condition of the database is set.

The dbm_fetch(3) function returns a content datum. If no record in the database matches the key or if an error condition has been detected in the database, the dptr member of the content is a null pointer.

The dbm_open(3) function returns a pointer to a database structure. If an error is detected during the operation, dbm_open(3) returns a (DBM *)0.

ERRORS

No errors are defined.

EXAMPLES

None.

APPLICATION USAGE

The following code can be used to traverse the database:

for(key = dbm_firstkey(db); key.dptr != NULL; key = dbm_nextkey(db))

The dbm_ functions provided in this library should not be confused in any way with those of a general-purpose database management system. These functions do not provide for multiple search keys per entry, they do not protect against multi-user access (in other words they do not lock records or files), and they do not provide the many other useful database functions that are found in more robust database management systems. Creating and updating databases by use of these functions is relatively slow because of data copies that occur upon hash collisions. These functions are useful for applications requiring fast lookup of relatively static information that is to be indexed by a single key.

The dbm_delete(3) function need not physically reclaim file space, although it does make it available for reuse by the database.

After calling dbm_store(3) or dbm_delete(3) during a pass through the keys by dbm_firstkey(3) and dbm_nextkey(3), the application should reset the database by calling dbm_firstkey(3) before again calling dbm_nextkey(3). The contents of these files are unspecified and may not be portable.

FUTURE DIRECTIONS

None.

SEE ALSO

open(2)