'\"macro stdmacro
.if n .pH g3.dbm @(#)dbm	40.3 of 1/29/90
.\" Copyright 1989 AT&T
.\" @(#)dbm.3 1.6 89/09/28 SMI; from UCB 4.3
.\" Copyright (c) 1988 Sun Microsystems, Inc. - All Rights Reserved.
.\" Copyright (c) 1980 Regents of the University of California.
.\" All rights reserved.  The Berkeley software License Agreement
.\" specifies the terms and conditions for redistribution.
.nr X
.if \nX=0 .ds x} dbm 3 "" "\&"
.if \nX=1 .ds x} dbm 3 ""
.if \nX=2 .ds x} dbm 3 "" "\&"
.if \nX=3 .ds x} dbm "" "" "\&"
.TH \*(x}
.SH NAME
\f4dbm, dbminit, dbmclose, fetch, store, delete, firstkey, nextkey\fP \- database subroutines
.SH SYNOPSIS
.sp .25
.ft 4
#include <dbm.h>
.sp .25
.ft 4
.nf
typedef struct {
char *dptr;
int dsize;
} datum;
.fi
.sp .25
.ft 4
.nf
dbminit(file)
char *file;
.fi
.sp .25
.ft 4
dbmclose(\|)
.sp .25
.ft 4
.nf
datum fetch(key)
datum key;
.fi
.sp .25
.ft 4
.nf
store(key, content)
datum key, content;
.fi
.sp .25
.ft 4
.nf
delete(key)
datum key;
.fi
.sp .25
.ft 4
datum firstkey(\|)
.sp .25
.ft 4
.nf
datum nextkey(key)
datum key;
.fi
.ft 1
.SH DESCRIPTION
.P
These functions maintain key/content pairs in a database.
The functions will handle very large (a billion blocks)
databases and will access a keyed item in one or two file system accesses.
The functions are obtained with the loader option
\f4\-lyp\f1.
.P
\f2key\f1s
and
\f2content\f1s
are described by the
\f4datum\f1
typedef.  A
\f4datum\f1
specifies a string of
\f2dsize\f1
bytes pointed to by
\f2dptr\f1.
Arbitrary binary data, as well as normal
\s-1ASCII\s0
strings, are allowed.
The database is stored in two files.
One file is a directory containing a bit map and has
\f4\&.dir\f1
as its suffix.
The second file contains all data and has
\f4\&.pag\f1
as its suffix.
.P
Before a database can be accessed, it must be opened by
\f4dbminit\f1.
At the time of this call, the files
\f2file\f4\&.dir\f1
and
\f2file\f4\&.pag\f1
must exist.
An empty database is created by creating zero-length
\f4\&.dir\f1
and
\f4\&.pag\f1
files.
.P
A database may be closed by calling
\f4dbmclose\f1.
You must close a database before opening a new one.
.P
Once open, the data stored under a key is accessed by
\f4fetch(\|)\f1
and data is placed under a key by
\f4store\f1.
A key (and its associated contents) is deleted by
\f4delete\f1.
A linear pass through all keys in a database may be made,
in an (apparently) random order, by use of
\f4firstkey(\|)\f1
and
\f4nextkey\f1.
\f4firstkey(\|)\f1
will return the first key in the database.
With any key
\f4nextkey(\|)\f1
will return the next key in the database.
This code will traverse the database:
.sp .25
.RS
\f4for (key = firstkey(\|); key.dptr != \s-1NULL\s0; key = nextkey(key))\f1
.RE
.SH RETURN VALUE
All functions that return an
\f4int\f1
indicate errors with negative values.
A zero return indicates no error.
Routines that return a
\f4datum\f1
indicate errors with a
\s-1NULL\s0
(0)
\f2dptr\f1.
.br
.ne 5
.SH NOTES
.P
The
\f4\&.pag\f1
file will contain holes so that its apparent size is about
four times its actual content.
Older versions of the
\s-1UNIX\s0
operating system may create real
file blocks for these holes when touched.
These files cannot be copied
by normal means
(\f4cp\f1(1),
\f4cat\f1(1),
\f4tar\f1(1),
\f4ar\f1(1))
without filling in the holes.
.P
\f2dptr\f1
pointers returned by these subroutines point into
static storage that is changed by subsequent calls.
.P
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.
\f4store(\|)\f1
will return an error in the event that a
disk block fills with inseparable data.
.P
\f4delete(\|)\f1
does not physically reclaim file space,
although it does make it available for reuse.
.P
The order of keys presented by
\f4firstkey(\|)\f1
and
\f4nextkey(\|)\f1
depends on a hashing function,
not on anything interesting.
.P
There are no interlocks and no reliable cache flushing;
thus concurrent updating and reading is risky.
.SH FILES
.PD 0
.TP 20
\f4/usr/lib/libyp.a\f1
.PD
