DB(3X) DB(3X) NAME DBopen, DBclose, DBget, DBput, DBdel, DBkey0, DBkeyn, DBlock, DBunlock, DBsync, DBapp, DBins, DBcopy - database subroutines SYNOPSIS #include <DB.h> datum DBkeyn(dp, key) DBFILE * dp; typedef struct { datum key; char * dptr; int dsize; void DBclose(dp) } datum; DBFILE * dp; int DBdebug; int DBlock(dp) DBFILE * dp; DBFILE * DBopen(file, flags) char * file; int DBunlock(dp) int flags; DBFILE * dp; datum DBget(dp, key) void DBsync(dp) DBFILE * dp; DBFILE * dp; datum key; int DBapp(dp, key, content) int DBput(dp, key, content) DBFILE * dp; DBFILE * dp; datum key, content; datum key, content; int DBins(dp, key, content) int DBdel(dp, key) DBFILE * dp; DBFILE * dp; datum key, content; datum key; int DBcopy(sdp, ddp) datum DBkey0(dp) DBFILE * sdp, ddp; DBFILE * dp; DESCRIPTION These functions maintain key/content pairs in a data base. The functions will handle databases as large as the filesys- tem can handle and will access a keyed item in one or two file system accesses. The functions are obtained with the loader option -lDB. Keys and contents are described by the datum typedef. A datum specifies a string of dsize bytes pointed to by dptr. Arbitrary binary data, as well as normal ASCII strings, are allowed as either keys or records. The data base is stored in two files. One file is a directory containing a bit map 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 DB(3X) DB(3X) DBopen. At the time of this call, the files file.dir and file.pag must exist unless the DB_CREATE flag is set in the flags argument. If this flag is specified and the files exist, they are truncated. The only other meaningful flag that may be specified is DB_RONLY, which indicates that the DB library may not write on the files. If the files are both not writable, this flag need not be specified and the d_flags member of the returned DBFILE pointer will have this bit set. Note that these flags are mutually exclusive, and if one must be ignored, it will be DB_RONLY. Once open, the data stored under a key is accessed by DBget and data is placed under a key or overwritten by DBput. The data denoted by a key may be appended to, or inserted before, by DBapp and DBins, respectively. A key (and its associated contents) is deleted by DBdel. A linear pass through all keys in a database may be made, in an (appar- ently) random order, by use of DBkey0 and DBkeyn. DBkey0 will return the first key in the database. With any key argument, DBkeyn will return the next key in the database. This code will traverse the data base: for (key = DBkey0(dp); key.dptr != NULL; key = DBkeyn(dp, key)) DBcopy copies the data indicated by keys in the database referred to by its first argument to the database indicated by its second argument using the same traversal. DBlock and DBunlock set and clear advisory locks (see ioctl(4)) on the database files. DBlock returns when the lock is set by the invoking process. DBsync flushes any modified buffers, and is called by DBunlock and DBclose. DIAGNOSTICS All functions that return an int indicate errors with nega- tive values. A zero return indicates success. Routines that return a datum indicate errors with a null (0) dptr. DBput will return an error in the event that a disk block fills with inseparable data. Debugging information will be printed out on stderr if DBdebug is set to one of DBDBCORE, DBDBERR, DBDBWARN, or DBDBINFO. These values provide increasing amounts of infor- mation, ranging from conditions that will cause abnormal termination of the program (DBDBCORE) to call traces of the user-accessible routines (DBDBINFO). BUGS DB(3X) DB(3X) The `.pag' file will contain holes so that its apparent size is about four times its actual content. Older UNIX systems may create real file blocks for these holes when touched. These files cannot be copied by normal means (cp(1), cat(1), tp(1), tar(1), ar(1)) without filling in the holes. A com- mand to permit copying of these files (with holes intact) is provided (DBcp(1)). 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 65536 (64K) bytes). Moreover, all key/content pairs that hash together must fit on a single block. DBdel does not physically reclaim file space, although it does make it available for reuse. Use DBcp(1) or DBcopy if file space must be reclaimed. The order of keys presented by DBkey0 and DBkeyn depends on a hashing function, not on anything interesting. Since these routines use binary data for internal housekeep- ing, databases created on a VAX cannot be directly manipu- lated on a 3B, and vice versa. DBcvt(1) converts a database generated on either of these machines to a form palatable on the other. SEE ALSO DB(1), DBcp(1), DBcvt(1).