|
|
#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;
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))
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 UNIX101 105 234
1 #include <stdio.h> 2 #include <dbm.h> 3 #define KEYSIZE 5 4 #define DATASIZE 255 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 }
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.