Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ dbm(S) — OpenDesktop Software Development System 3.0.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought


 dbm(S)                         6 January 1993                         dbm(S)


 Name

    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


    ________________________________________________
      0 #include <stdio.h>
      1 #include <dbm.h>
      2 #define KEYSIZE       5
      3 #define DATASIZE     25
      4 datum key, data, testdata;
      5 FILE *fp, *fopen();
      6 char     keybuf[KEYSIZE];
      7 char     keybuf2[KEYSIZE];
      8 char     databuf1[DATASIZE];
      9 main()
     10 {
     11 char c;
     12 dbminit("testfile");
     13 fp = fopen("datafile", "r");
     14 while (( c= getc(fp)) != '\n') {
     15         key.dptr = keybuf;
     16         *key.dptr++  = c;
     17         key.dsize = 1;
     18         while(( c= getc(fp)) != '\n') {
     19                 *key.dptr++ = c;
     20                 key.dsize++;
     21         }
     22         data.dsize = 0;
     23         data.dptr = databuf1;
     24         while((c = getc(fp)) != '\n') {
     25                 *data.dptr++ = c;
     26                 data.dsize++;
     27         }
     28         *data.dptr = '\0';
     29         data.dsize++;
     30         data.dptr = databuf1;
     31         key.dptr  = keybuf;
     32         printf("datadsize %d keydsize %d\n", data.dsize, key.dsize);
     33         printf("datadptr %d keydptr %d\n", data.dptr, key.dptr);
     34         store(key,data);
     35 }
     36 printf("\nData base loaded and now going for the read\n\n");
     37 key.dptr = keybuf2;
     38  while ((*key.dptr++ = getc(fp)) != EOF){
     39         key.dsize =1;
     40         while ((c=getc(fp)) != '\n') {
     41                 *key.dptr++ = c;
     42 }
     43 key.dptr = keybuf2;
     44 testdata = fetch(key);
     45 printf("Key: %s Data: %s\n", key.dptr, testdata.dptr);
     46 delete(key);
     47 testdata = fetch(key);
     48 printf("Deleted Key : %s Data : %s\n", key.dptr, testdata.dptr);
     49 }
     50 }
    ________________________________________________


    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.


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