home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / testgdbm.c < prev    next >
C/C++ Source or Header  |  1992-01-19  |  15KB  |  567 lines

  1. /* testgdbm.c - Driver program to test the database routines and to
  2.    help debug gdbm.  Uses inside information to show "system" information */
  3.  
  4. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  5.     Copyright (C) 1990, 1991  Free Software Foundation, Inc.
  6.  
  7.     GDBM is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 1, or (at your option)
  10.     any later version.
  11.  
  12.     GDBM is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with GDBM; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.     You may contact the author by:
  22.        e-mail:  phil@cs.wwu.edu
  23.       us-mail:  Philip A. Nelson
  24.                 Computer Science Department
  25.                 Western Washington University
  26.                 Bellingham, WA 98226
  27.         phone:  (206) 676-3035
  28.  
  29. *************************************************************************/
  30.  
  31.  
  32. #include "gdbmdefs.h"
  33. #include "gdbmerrno.h"
  34. #include "extern.h"
  35.  
  36. extern gdbm_error gdbm_errno;
  37.  
  38. extern char * gdbm_version;
  39.  
  40. /* For getopt. */
  41. extern char *optarg;
  42. extern int optind, opterr;
  43.  
  44. gdbm_file_info *gdbm_file;
  45.  
  46. #ifdef __STDC__
  47. void _gdbm_print_bucket_cache (gdbm_file_info *dbf);
  48. static void print_bucket (hash_bucket *bucket, char *mesg);
  49. static void _gdbm_print_avail_list (gdbm_file_info *dbf);
  50. void main (int argc, char **argv);
  51. void usage (char *s);
  52. void gdbm_perror (char *msg);
  53. extern int getopt (int argc, char **argv, const char *shortopts);
  54. #endif /* __STDC__ */
  55.  
  56.  
  57. /* access GDBM_ERRNO.  [tho]  */
  58.  
  59. void
  60. gdbm_perror (char *msg)
  61. {
  62.   char *err_msg;
  63.  
  64.   switch (gdbm_errno)
  65.     {
  66.     case GDBM_NO_ERROR:
  67.       err_msg = "no error";
  68.       break;
  69.     case GDBM_MALLOC_ERROR:
  70.       err_msg = "can't malloc";
  71.       break;
  72.     case GDBM_BLOCK_SIZE_ERROR:
  73.       err_msg = "bad block size";
  74.       break;
  75.     case GDBM_FILE_OPEN_ERROR:
  76.       err_msg = "can't open file";
  77.       break;
  78.     case GDBM_FILE_WRITE_ERROR:
  79.       err_msg = "can't write file";
  80.       break;
  81.     case GDBM_FILE_SEEK_ERROR:
  82.       err_msg = "can't seek file";
  83.       break;
  84.     case GDBM_FILE_READ_ERROR:
  85.       err_msg = "can't read file";
  86.       break;
  87.     case GDBM_BAD_MAGIC_NUMBER:
  88.       err_msg = "bad magic number";
  89.       break;
  90.     case GDBM_EMPTY_DATABASE:
  91.       err_msg = "empty database";
  92.       break;
  93.     case GDBM_CANT_BE_READER:
  94.       err_msg = "can't be reader";
  95.       break;
  96.     case GDBM_CANT_BE_WRITER:
  97.       err_msg = "can't be writer";
  98.       break;
  99.     case GDBM_READER_CANT_DELETE:
  100.       err_msg = "can't delete";
  101.       break;
  102.     case GDBM_READER_CANT_STORE:
  103.       err_msg = "can't store";
  104.       break;
  105.     case GDBM_READER_CANT_REORGANIZE:
  106.       err_msg = "can't reorganize";
  107.       break;
  108.     case GDBM_UNKNOWN_UPDATE:
  109.       err_msg = "unkown update";
  110.       break;
  111.     case GDBM_ITEM_NOT_FOUND:
  112.       err_msg = "item not found";
  113.       break;
  114.     case GDBM_REORGANIZE_FAILED:
  115.       err_msg = "reorganization failed";
  116.       break;
  117.     case GDBM_CANNOT_REPLACE:
  118.       err_msg = "can't replace";
  119.       break;
  120.     default:
  121.       err_msg = "unknown error";
  122.     }
  123.  
  124.   fprintf (stderr, "%s (GDBM error: %s)\n", msg, err_msg);
  125. }
  126.  
  127.  
  128. /* Debug procedure to print the contents of the current hash bucket. */
  129. void
  130. print_bucket (bucket, mesg)
  131.      hash_bucket *bucket;
  132.      char *mesg;
  133. {
  134.   int  index;
  135.  
  136.   printf ("******* %s **********\n\nbits = %d\ncount= %d\nHash Table:\n",
  137.      mesg, bucket->bucket_bits, bucket->count);
  138.   printf ("     #    hash value     key size    data size     data adr  home\n");
  139.   for (index = 0; index < gdbm_file->header->bucket_elems; index++)
  140.     printf ("  %4d  %12lx  %11d  %11d  %11ld %5d\n", index,
  141.        bucket->h_table[index].hash_value,
  142.        bucket->h_table[index].key_size,
  143.        bucket->h_table[index].data_size,
  144.        bucket->h_table[index].data_pointer,
  145.        (int) (bucket->h_table[index].hash_value % gdbm_file->header->bucket_elems));
  146.  
  147.   printf ("\nAvail count = %1d\n", bucket->av_count);
  148.   printf ("Avail  adr     size\n");
  149.   for (index = 0; index < bucket->av_count; index++)
  150.     printf ("%9ld%9d\n", bucket->bucket_avail[index].av_adr,
  151.                     bucket->bucket_avail[index].av_size);
  152. }
  153.  
  154.  
  155. void
  156. _gdbm_print_avail_list (dbf)
  157.      gdbm_file_info *dbf;
  158. {
  159.   long temp;
  160.   int size;
  161.   avail_block *av_stk;
  162.  
  163.   /* Print the the header avail block.  */
  164.   printf ("\nheader block\nsize  = %d\ncount = %d\n",
  165.       dbf->header->avail.size, dbf->header->avail.count);
  166.   for (temp = 0; temp < dbf->header->avail.count; temp++)
  167.     {
  168.       printf ("  %15d   %10ld \n", dbf->header->avail.av_table[temp].av_size,
  169.           dbf->header->avail.av_table[temp].av_adr);
  170.     }
  171.  
  172.   /* Initialize the variables for a pass throught the avail stack. */
  173.   temp = dbf->header->avail.next_block;
  174.   size = ( ( (dbf->header->avail.size * sizeof (avail_elem)) >> 1)
  175.       + sizeof (avail_block));
  176.   av_stk = (avail_block *) alloca (size);
  177. #ifdef OS2
  178.   if (av_stk == (avail_block *) 0)
  179.     {
  180.       fprintf (stderr, "Error: alloca() failed in gdbm (%s).\n", __FILE__);
  181.       exit (-2);
  182.     }
  183. #endif /* OS2 */
  184.  
  185.   /* Print the stack. */
  186.   while (FALSE)
  187.     {
  188.       lseek (dbf->desc, temp, L_SET);
  189.       read  (dbf->desc, av_stk, size);
  190.  
  191.       /* Print the block! */
  192.       printf ("\nblock = %d\nsize  = %d\ncount = %d\n", temp,
  193.           av_stk->size, av_stk->count);
  194.       for (temp = 0; temp < av_stk->count; temp++)
  195.     {
  196.       printf ("  %15d   %10ld \n", av_stk->av_table[temp].av_size,
  197.         av_stk->av_table[temp].av_adr);
  198.     }
  199.       temp = av_stk->next_block;
  200.     }
  201. }
  202.  
  203. void _gdbm_print_bucket_cache (dbf)
  204.      gdbm_file_info *dbf;
  205. {
  206.   int index;
  207.   char changed;
  208.  
  209.   printf ("Bucket Cache:\n  Index:  Address  Changed  Data_Hash \n");
  210.   for (index=0; index < CACHE_SIZE; index++)
  211.     {
  212.       changed = dbf->bucket_cache[index].ca_changed;
  213.       printf ("  %5d:  %7ld  %7s  %lx\n",
  214.           index,
  215.           dbf->bucket_cache[index].ca_adr,
  216.           (changed ? "True" : "False"),
  217.           dbf->bucket_cache[index].ca_data.hash_val);
  218.     }
  219. }
  220.  
  221. void usage (s)
  222.      char *s;
  223. {
  224.   printf ("Usage: %s [-r or -n] [gdbm-file] \n",s);
  225.   exit (2);
  226. }
  227.  
  228.  
  229. /* The test program allows one to call all the routines plus the hash function.
  230.    The commands are single letter commands.  The user is prompted for all other
  231.    information.  See the help command (?) for a list of all commands. */
  232.  
  233. void
  234. main (argc, argv)
  235.      int argc;
  236.      char *argv[];
  237.  
  238. {
  239.  
  240.   int  cmd_ch;
  241.   int  opt;
  242.  
  243.   datum key_data;
  244.   datum data_data;
  245.   datum return_data;
  246.  
  247.   char key_line[500];
  248.   char data_line[1000];
  249.  
  250.   char done = FALSE;
  251.   char reader = FALSE;
  252.   char newdb = FALSE;
  253.  
  254.   char *file_name;
  255.  
  256.  
  257.   /* Argument checking. */
  258.   while ((opt = getopt (argc, argv, "rn")) != -1)
  259.     switch (opt) {
  260.     case 'r':  reader = TRUE;
  261.                if (newdb) usage (argv[0]);
  262.                break;
  263.     case 'n':  newdb = TRUE;
  264.                if (reader) usage (argv[0]);
  265.                break;
  266.     }
  267.   if (argc > optind+1) usage (argv[0]);
  268.  
  269.   if (optind < argc)
  270.     {
  271.       file_name = argv[optind];
  272.     }
  273.   else
  274.     {
  275. #ifdef OS2
  276.       file_name = "junk.gdb";
  277. #else
  278.       file_name = "junk.gdbm";
  279. #endif
  280.     }
  281.  
  282.   /* Initialize variables. */
  283.   key_data.dptr = NULL;
  284.   data_data.dptr = data_line;
  285.  
  286.   if (reader)
  287.     gdbm_file = gdbm_open (file_name, 512, GDBM_READER, 00664, NULL);
  288.   else if (newdb)
  289.     gdbm_file = gdbm_open (file_name, 512, GDBM_NEWDB, 00664, NULL);
  290.   else
  291.     gdbm_file = gdbm_open (file_name, 512, GDBM_WRCREAT, 00664, NULL);
  292.   if (gdbm_file == NULL)
  293.     {
  294.       gdbm_perror ("gdbm_open failed");
  295.       exit (2);
  296.     }
  297.  
  298.   /* Welcome message. */
  299.   printf ("\nWelcome to the gdbm test program.  Type ? for help.\n\n");
  300.  
  301.   while (!done)
  302.     {
  303.       printf ("com -> ");
  304.       cmd_ch = (char) getchar ();
  305.       if (cmd_ch != '\n')
  306.     {
  307.       char temp;
  308.       do
  309.           temp = (char) getchar ();
  310.       while (temp != '\n' && temp != EOF);
  311.     }
  312.       if (cmd_ch == EOF) cmd_ch = 'q';
  313.       switch (cmd_ch)
  314.     {
  315.  
  316.     /* Standard cases found in all test{dbm,ndbm,gdbm} programs. */
  317.     case '\n':
  318.       printf ("\n");
  319.       break;
  320.  
  321.     case 'c':
  322.       {
  323.         int temp;
  324.         temp = 0;
  325.         if (key_data.dptr != NULL) free (key_data.dptr);
  326.         return_data = gdbm_firstkey (gdbm_file);
  327.         while (return_data.dptr != NULL)
  328.           {
  329.         temp++;
  330.         key_data = return_data;
  331.         return_data = gdbm_nextkey (gdbm_file, key_data);
  332.         free (key_data.dptr);
  333.           }
  334.         printf ("There are %d items in the database.\n\n", temp);
  335.         key_data.dptr = NULL;
  336.       }
  337.       break;
  338.  
  339.     case 'd':
  340.       if (key_data.dptr != NULL) free (key_data.dptr);
  341.       printf ("key -> ");
  342.       gets (key_line);
  343.       key_data.dptr = key_line;
  344.       key_data.dsize = strlen (key_line)+1;
  345.       if (gdbm_delete (gdbm_file, key_data) != 0)
  346.         gdbm_perror ("Item not found or deleted");
  347.       printf ("\n");
  348.       key_data.dptr = NULL;
  349.       break;
  350.  
  351.     case 'f':
  352.       if (key_data.dptr != NULL) free (key_data.dptr);
  353.       printf ("key -> ");
  354.       gets (key_line);
  355.       key_data.dptr = key_line;
  356.       key_data.dsize = strlen (key_line)+1;
  357.       return_data = gdbm_fetch (gdbm_file, key_data);
  358.       if (return_data.dptr != NULL)
  359.         {
  360.           printf ("data is ->%s\n\n", return_data.dptr);
  361.           free (return_data.dptr);
  362.         }
  363.       else
  364.         printf ("No such item found.\n\n");
  365.       key_data.dptr = NULL;
  366.       break;
  367.  
  368.     case 'n':
  369.       if (key_data.dptr != NULL) free (key_data.dptr);
  370.       printf ("key -> ");
  371.       gets (key_line);
  372.       key_data.dptr = key_line;
  373.       key_data.dsize = strlen (key_line)+1;
  374.       return_data = gdbm_nextkey (gdbm_file, key_data);
  375.       if (return_data.dptr != NULL)
  376.         {
  377.           key_data = return_data;
  378.           printf ("key is  ->%s\n", key_data.dptr);
  379.           return_data = gdbm_fetch (gdbm_file, key_data);
  380.           printf ("data is ->%s\n\n", return_data.dptr);
  381.           free (return_data.dptr);
  382.         }
  383.       else
  384.         {
  385.           gdbm_perror ("No such item found");
  386.           key_data.dptr = NULL;
  387.         }
  388.       break;
  389.  
  390.     case 'q':
  391.       done = TRUE;
  392.       break;
  393.  
  394.     case 's':
  395.       if (key_data.dptr != NULL) free (key_data.dptr);
  396.       printf ("key -> ");
  397.       gets (key_line);
  398.       key_data.dptr = key_line;
  399.       key_data.dsize = strlen (key_line)+1;
  400.       printf ("data -> ");
  401.       gets (data_line);
  402.       data_data.dsize = strlen (data_line)+1;
  403.       if (gdbm_store (gdbm_file, key_data, data_data, GDBM_REPLACE) != 0)
  404.         gdbm_perror ("Item not inserted");
  405.       printf ("\n");
  406.       key_data.dptr = NULL;
  407.       break;
  408.  
  409.     case '1':
  410.       if (key_data.dptr != NULL) free (key_data.dptr);
  411.       key_data = gdbm_firstkey (gdbm_file);
  412.       if (key_data.dptr != NULL)
  413.         {
  414.           printf ("key is  ->%s\n", key_data.dptr);
  415.           return_data = gdbm_fetch (gdbm_file, key_data);
  416.           printf ("data is ->%s\n\n", return_data.dptr);
  417.           free (return_data.dptr);
  418.         }
  419.       else
  420.         gdbm_perror ("No such item found");
  421.       break;
  422.  
  423.     case '2':
  424.       return_data = gdbm_nextkey (gdbm_file, key_data);
  425.       if (return_data.dptr != NULL)
  426.         {
  427.           free (key_data.dptr);
  428.           key_data = return_data;
  429.           printf ("key is  ->%s\n", key_data.dptr);
  430.           return_data = gdbm_fetch (gdbm_file, key_data);
  431.           printf ("data is ->%s\n\n", return_data.dptr);
  432.           free (return_data.dptr);
  433.         }
  434.       else
  435.         gdbm_perror ("No such item found");
  436.       break;
  437.  
  438.  
  439.     /* Special cases for the testgdbm program. */
  440.     case 'r':
  441.       {
  442.         if (gdbm_reorganize (gdbm_file))
  443.           gdbm_perror ("Reorganization failed");
  444.         else
  445.           printf ("Reorganization succeeded. \n\n");
  446.       }
  447.       break;
  448.  
  449.     case 'A':
  450.       _gdbm_print_avail_list (gdbm_file);
  451.       printf ("\n");
  452.       break;
  453.  
  454.     case 'B':
  455.       {
  456.         int temp;
  457.         char number[80];
  458.  
  459.         printf ("bucket? ");
  460.         gets (number);
  461.         sscanf (number,"%d",&temp);
  462.  
  463.         if (temp >= gdbm_file->header->dir_size /4)
  464.           {
  465.         gdbm_perror ("Not a bucket");
  466.         break;
  467.           }
  468.         _gdbm_get_bucket (gdbm_file, temp);
  469.       }
  470.       printf ("Your bucket is now ");
  471.  
  472.     case 'C':
  473.       print_bucket (gdbm_file->bucket, "Current bucket");
  474.       printf ("\n current directory entry = %d.\n", gdbm_file->bucket_dir);
  475.       printf (" current bucket address  = %ld.\n\n",
  476.           gdbm_file->cache_entry->ca_adr);
  477.       break;
  478.  
  479.     case 'D':
  480.       printf ("Hash table directory.\n");
  481.       printf ("  Size =  %d.  Bits = %d. \n\n",gdbm_file->header->dir_size,
  482.           gdbm_file->header->dir_bits);
  483.       {
  484.         int temp;
  485.  
  486.         for (temp = 0; temp < gdbm_file->header->dir_size / 4; temp++)
  487.           {
  488.         printf ("  %10d:  %12ld\n", temp, gdbm_file->dir[temp]);
  489.         if ( (temp+1) % 20 == 0 && isatty (0))
  490.           {
  491.             printf ("*** CR to continue: ");
  492.             while (getchar () != '\n') /* Do nothing. */;
  493.           }
  494.           }
  495.       }
  496.       printf ("\n");
  497.       break;
  498.  
  499.     case 'F':
  500.       {
  501.         printf ("\nFile Header: \n\n");
  502.         printf ("  table        = %ld\n", gdbm_file->header->dir);
  503.         printf ("  table size   = %d\n", gdbm_file->header->dir_size);
  504.         printf ("  table bits   = %d\n", gdbm_file->header->dir_bits);
  505.         printf ("  block size   = %d\n", gdbm_file->header->block_size);
  506.         printf ("  bucket elems = %d\n", gdbm_file->header->bucket_elems);
  507.         printf ("  bucket size  = %d\n", gdbm_file->header->bucket_size);
  508.         printf ("  header magic = %lx\n", gdbm_file->header->header_magic);
  509.         printf ("  next block   = %d\n", gdbm_file->header->next_block);
  510.         printf ("  avail size   = %d\n", gdbm_file->header->avail.size);
  511.         printf ("  avail count  = %d\n", gdbm_file->header->avail.count);
  512.         printf ("  avail nx blk = %d\n", gdbm_file->header->avail.next_block);
  513.         printf ("\n");
  514.       }
  515.       break;
  516.  
  517.         case 'H':
  518.       if (key_data.dptr != NULL) free (key_data.dptr);
  519.       printf ("key -> ");
  520.       gets (key_line);
  521.       key_data.dptr = key_line;
  522.       key_data.dsize = strlen (key_line)+1;
  523.       printf ("hash value = %lx. \n\n", _gdbm_hash (key_data));
  524.       key_data.dptr = NULL;
  525.       break;
  526.  
  527.     case 'K':
  528.       _gdbm_print_bucket_cache (gdbm_file);
  529.       break;
  530.  
  531.     case 'V':
  532.       printf ("%s\n\n", gdbm_version);
  533.       break;
  534.  
  535.     case '?':
  536.       printf ("c - count (number of entries)\n");
  537.       printf ("d - delete\n");
  538.       printf ("f - fetch\n");
  539.       printf ("n - nextkey\n");
  540.       printf ("q - quit\n");
  541.       printf ("s - store\n");
  542.       printf ("1 - firstkey\n");
  543.       printf ("2 - nextkey on last key (from n, 1 or 2)\n\n");
  544.  
  545.       printf ("r - reorganize\n");
  546.       printf ("A - print avail list\n");
  547.       printf ("B - get and print current bucket n\n");
  548.       printf ("C - print current bucket\n");
  549.       printf ("D - print hash directory\n");
  550.       printf ("F - print file header\n");
  551.       printf ("H - hash value of key\n");
  552.       printf ("K - print the bucket cache\n");
  553.       printf ("V - print version of gdbm\n");
  554.       break;
  555.  
  556.     default:
  557.       printf ("What? \n\n");
  558.       break;
  559.  
  560.     }
  561.     }
  562.  
  563.   /* Quit normally. */
  564.   exit (0);
  565.  
  566. }
  567.