Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought



     DBM(S)                    UNIX System V                    DBM(S)



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

     Syntax
          #include <dbm.h>

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

          int dbminit(file)
          char *file;

          datum fetch(key)
          datum key;

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

          int delete(key)
          datum key;

          datum firstkey();

          datum nextkey(key);
          datum key;

     Description
          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 -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 will return the first key in the database.  With
          any key nextkey will return the next key in the database.
          This code will traverse the database:

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

     Example
          The example program below uses dbm's 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

               #include <stdio.h>
               #include <dbm.h>
               #define KEYSIZE 5
               #define DATASIZE 25

               typedef struct adatum
                 {
                    char *ptr;
                    int size;
                 }datum;

               datum key, data, testdata;

               FILE *fp, *fopen();

               char keybuf[KEYSIZE];
               char keybuf2[KEYSIZE];
               char databuf1[DATASIZE];

               main()
               {
                 datum fetch();
                 datum store();
                 char c;
               /* Initialize the database */
                 dbminit("testfile");
                 fp = fopen("datafile","r");
               /* Read in Keys and Data until a newline */
                 while ((c = getc(fp)) != '\n') {
                 /* Read in a key */
                    key.ptr = keybuf;
                    *key.ptr++ = c;
                    key.size = 1;
                    while ((c = getc(fp)) != '\n') {
                      *key.ptr++ = c;
                      key.size++;
                    }

                 /* Read in a data field */
                    data.size = 0;
                    data.ptr = databuf1;
                    while ((c= getc(fp)) != '\n') {
                      *data.ptr++ = c;
                      data.size++;
                    }
                    *data.ptr = '\0';
                    data.size++;

                 /* Store a record in the testfile database */
                    data.ptr = databuf1;
                    key.ptr = keybuf;
                 printf("datasize %d keysize %d\n",data.size,key.size);
                 printf("dataptr %s keyptr %s\n",data.ptr,key.ptr);
                    store(key,data);
                 }
                 key.ptr = keybuf2;

               /* Read in keys from datafile,
                 and use them to fetch records */
                 while ((*key.ptr++ = getc(fp)) != EOF) {
                    key.size = 1;
                    while ((c = getc(fp)) != '\n') {
                      *key.ptr++ =c;
                      key.size++;
                    }
                    key.ptr = keybuf2;

                 /* Fetch record specified by key */
                    testdata = fetch(key);
                    printf("Key: %s Data: %s\n",key.ptr,testdata.ptr);
                 /* Delete the record */
                    delete(key);
                 /* Attempt to retreive deleted record */
                    testdata = fetch(key);
                 /* printf to show data is now null */
                    printf("Deleted Key: %s Data: %s\n",key.ptr,testdata.ptr);
                 }
               }

     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 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, 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 will return 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.

     Credit
          This utility was developed at the University of California
          at
          Berkeley and is used with permission.

                                                (printed 6/20/89)



Typewritten Software • bear@typewritten.org • Edmonds, WA 98026