DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

dbm(S)


dbm: dbminit, delete, fetch, firstkey, nextkey, store -- performs database functions

Syntax

cc . . . -ldbm

#include <dbm.h>

typedef struct { char *dptr; int dsize; } datum;

int dbminit(file) char *file;

int delete(key) datum key;

datum fetch(key) datum key;

datum firstkey();

datum nextkey(key); datum key;

int store(key, content) datum key, content;

Description

These functions maintain key/content pairs in a database. The functions handle very large (a billion blocks) databases and access a keyed item in one or two file system accesses. The functions are obtained with the loader option -ldbm.

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. The database 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 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, the data stored under a key is accessed by fetch and data is placed under a key by store. A key (and its associated contents) is deleted by delete. A linear pass through all keys in a database may be made, in an (apparently) random order, by use of firstkey and nextkey. firstkey returns the first key in the database. With any key nextkey returns the next key in the database. This code traverses the database:

   for(key=firstkey(); key.dptr!=NULL; key=nextkey(key))

Example

The example program below uses dbm, dbminit, store, fetch and delete functions. It reads in keys and data from a datafile (shown below) and stores them in a database called testfile. Newlines (``\n'') are used for delimiters. It then reads the keys from "datafile" and uses them to fetch the data out, print the data, and delete the record.

To run this program the files testfile.dir and testfile.pag must exist and be empty (0 bytes). The datafile and test program are as follows:

   101
   UNIX Programming
   105
   System Administration
   234
   Intro to UNIX

101 105 234

  1 #include <stdio.h>
  2 #include <dbm.h>
  3 #define KEYSIZE       5
  4 #define DATASIZE     25

5 datum key, data, testdata;

6 FILE *fp, *fopen();

7 char keybuf[KEYSIZE]; 8 char keybuf2[KEYSIZE]; 9 char databuf1[DATASIZE];

10 main() 11 { 12 char c;

13 dbminit("testfile"); 14 fp = fopen("datafile", "r"); 15 while (( c= getc(fp)) != '\n') { 16 key.dptr = keybuf; 17 *key.dptr++ = c; 18 key.dsize = 1; 19 while(( c= getc(fp)) != '\n') { 20 *key.dptr++ = c; 21 key.dsize++; 22 }

23 data.dsize = 0; 24 data.dptr = databuf1; 25 while((c = getc(fp)) != '\n') { 26 *data.dptr++ = c; 27 data.dsize++; 28 } 29 *data.dptr = '\0'; 30 data.dsize++; 31 data.dptr = databuf1; 32 key.dptr = keybuf; 33 printf("datadsize %d keydsize %d\n", data.dsize, key.dsize); 34 printf("datadptr %d keydptr %d\n", data.dptr, key.dptr); 35 store(key,data); 36 }

37 printf("\nData base loaded and now going for the read\n\n"); 38 key.dptr = keybuf2; 39 while ((*key.dptr++ = getc(fp)) != EOF){ 40 key.dsize =1; 41 while ((c=getc(fp)) != '\n') { 42 *key.dptr++ = c; 43 } 44 key.dptr = keybuf2; 45 testdata = fetch(key); 46 printf("Key: %s Data: %s\n", key.dptr, testdata.dptr); 47 delete(key); 48 testdata = fetch(key); 49 printf("Deleted Key : %s Data : %s\n", key.dptr, testdata.dptr); 50 } 51 }


Lines 12-13
Initialize the database

Lines 14-15
Read in keys and data until a new line

Lines 16-20
Read in a key

Lines 22-26
Read in a data field

Lines 28-34
Store a record in a testfile database

Lines 38-41
Read in keys from the datafile and use

Lines 43-44
Fetch record specified by key

Line 46
Delete the record

Lines 47-49
Printf to to show data is now null

Diagnostics

All functions that return an int indicate errors with negative values. A zero return indicates ok. Routines that return a datum indicate errors with a null (0) dptr.

Notes

The .pag file contains holes so that its apparent size is several times its actual content. Older version of some systems may create real file blocks for these holes when touched. These files cannot be copied by normal means (cp, cat, tp, tar, ar) without filling in the holes.

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 in the event that a disk block fills with inseparable data.

delete does not physically reclaim file space, although it does make it available for reuse.

The order of keys presented by firstkey and nextkey depends on a hashing function.

These routines are not reentrant, so they should not be used on more than one database at a time.

Standards conformance

dbm is not part of any currently supported standard; it was developed at the University of California at Berkeley and is used by permission.
© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003