HP-UX Reference (11i v2 04/09) - 3 Library Functions A-M (vol 6)

d
dbm(3C) dbm(3C)
NAME
dbminit, fetch, store, delete, firstkey, nextkey, dbmclose - database subroutines
SYNOPSIS
#include <dbm.h>
int dbminit(const char *file);
datum fetch(datum key);
int store(datum key, datum content);
int delete(datum key);
datum firstkey(void);
datum nextkey(datum key);
void dbmclose(void);
DESCRIPTION
These functions maintain key/content pairs in a database. They handle very large (a billion blocks (block
= 1024 bytes)) databases and can locate a keyed item in one or two file system accesses.
key and content parameters are described by the
datum type. A datum specifies a string of dsize bytes
pointed to by dptr. Arbitrary binary data, as well as normal ASCII strings, are allowed. 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.
Before a database can be accessed, it must be opened by
dbminit. At the time of this call, the files
file.dir and file.pag must exist. (An empty database is created by creating zero-length
.dir and
.pag files.)
Once open, data stored under a key is accessed by fetch
, and data is placed under a key by store
. Stor-
ing data on an existing key replaces the existing data. A key (and its associated contents) is deleted by
delete. A linear pass through all keys in a database can be made, in (apparently) random order by
using firstkey and nextkey. firstkey returns the first key in the database. With any key,
nextkey returns the next key in the database. The following code can be used to traverse the database:
for (key = firstkey(); key.dptr != NULL; key = nextkey(key))
A database can be closed by calling dbmclose. A currently open database must be closed before open-
ing a new one.
DIAGNOSTICS
All functions that return an
int indicate errors with negative values and success with zero. Functions
that return a datum indicate errors with a null dptr.
WARNINGS
The dbm functions provided in this library should not be confused in any way with those of a general-
purpose database management system such as ALLBASE/HP-UX SQL. 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 data base 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 sin-
gle key.
The .pag file will contain holes so that its apparent size is about four times its actual content. Some
older UNIX systems create real file blocks for these holes when touched. These files cannot be copied by
normal means (such as cp(1), cat(1), tar(1), or ar(1)) without expansion.
dptr pointers returned by these subroutines point into static storage that is changed by subsequent calls.
The sum of the sizes of a key/content pair must not exceed the internal block size (currently 1024 bytes).
Moreover, all key/content pairs that hash together must fit on a single block.
store returns an error if
a disk block fills with inseparable data.
delete does not physically reclaim file space, although it does make it available for reuse.
HP-UX 11i Version 2: September 2004 1 Hewlett-Packard Company Section 3183