home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdbm-1.7.1.tar.gz / gdbm-1.7.1.tar / gdbm-1.7.1 / testgdbm.c < prev    next >
C/C++ Source or Header  |  1993-11-13  |  13KB  |  497 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, 1993  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 2, 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.        
  28. *************************************************************************/
  29.  
  30.  
  31. /* AIX demands this be the very first thing in the file. */
  32. #if !defined(__GNUC__) && defined(_AIX)
  33.  #pragma alloca
  34. #endif
  35.  
  36. /* include system configuration before all else. */
  37. #include "autoconf.h"
  38.  
  39. #include "gdbmdefs.h"
  40. #include "gdbmerrno.h"
  41. #include "extern.h"
  42.  
  43. #include "getopt.h"
  44.  
  45. extern char * gdbm_version;
  46.  
  47. extern char *gdbm_strerror _ARGS((gdbm_error));
  48.  
  49. gdbm_file_info *gdbm_file;
  50.  
  51. /* Debug procedure to print the contents of the current hash bucket. */
  52. void print_bucket (bucket, mesg)
  53.      hash_bucket *bucket;
  54.      char *mesg;
  55. {
  56.   int  index;
  57.  
  58.   printf ("******* %s **********\n\nbits = %d\ncount= %d\nHash Table:\n",
  59.      mesg, bucket->bucket_bits, bucket->count);
  60.   printf ("     #    hash value     key size    data size     data adr  home\n");
  61.   for (index = 0; index < gdbm_file->header->bucket_elems; index++)
  62.     printf ("  %4d  %12x  %11d  %11d  %11d %5d\n", index,
  63.        bucket->h_table[index].hash_value,
  64.        bucket->h_table[index].key_size,
  65.        bucket->h_table[index].data_size,
  66.        bucket->h_table[index].data_pointer,
  67.        bucket->h_table[index].hash_value % gdbm_file->header->bucket_elems);
  68.  
  69.   printf ("\nAvail count = %1d\n", bucket->av_count);
  70.   printf ("Avail  adr     size\n");
  71.   for (index = 0; index < bucket->av_count; index++)
  72.     printf ("%9d%9d\n", bucket->bucket_avail[index].av_adr,
  73.                     bucket->bucket_avail[index].av_size);
  74. }
  75.  
  76.  
  77. void _gdbm_print_avail_list (dbf)
  78.      gdbm_file_info *dbf;
  79. {
  80.   int temp;
  81.   int size;
  82.   avail_block *av_stk;
  83.  
  84.   /* Print the the header avail block.  */
  85.   printf ("\nheader block\nsize  = %d\ncount = %d\n",
  86.       dbf->header->avail.size, dbf->header->avail.count);
  87.   for (temp = 0; temp < dbf->header->avail.count; temp++)
  88.     {
  89.       printf ("  %15d   %10d \n", dbf->header->avail.av_table[temp].av_size,
  90.           dbf->header->avail.av_table[temp].av_adr);
  91.     }
  92.  
  93.   /* Initialize the variables for a pass throught the avail stack. */
  94.   temp = dbf->header->avail.next_block;
  95.   size = ( ( (dbf->header->avail.size * sizeof (avail_elem)) >> 1)
  96.       + sizeof (avail_block));
  97.   av_stk = (avail_block *) alloca (size);
  98.  
  99.   /* Print the stack. */
  100.   while (FALSE)
  101.     {
  102.       lseek (dbf->desc, temp, L_SET);
  103.       read  (dbf->desc, av_stk, size);
  104.  
  105.       /* Print the block! */
  106.       printf ("\nblock = %d\nsize  = %d\ncount = %d\n", temp,
  107.           av_stk->size, av_stk->count);
  108.       for (temp = 0; temp < av_stk->count; temp++)
  109.     {
  110.       printf ("  %15d   %10d \n", av_stk->av_table[temp].av_size,
  111.         av_stk->av_table[temp].av_adr);
  112.     }
  113.       temp = av_stk->next_block;
  114.     }
  115. }
  116.  
  117. void _gdbm_print_bucket_cache (dbf)
  118.      gdbm_file_info *dbf;
  119. {
  120.   register int index;
  121.   char changed;
  122.  
  123.   if (dbf->bucket_cache != NULL) {
  124.     printf(
  125.       "Bucket Cache (size %d):\n  Index:  Address  Changed  Data_Hash \n",
  126.       dbf->cache_size);
  127.     for (index=0; index < dbf->cache_size; index++) {
  128.       changed = dbf->bucket_cache[index].ca_changed;
  129.       printf ("  %5d:  %7d  %7s  %x\n",
  130.           index,
  131.           dbf->bucket_cache[index].ca_adr,
  132.           (changed ? "True" : "False"),
  133.           dbf->bucket_cache[index].ca_data.hash_val);
  134.     }
  135.   } else
  136.     printf("Bucket cache has not been initialized.\n");
  137. }
  138.  
  139. void usage (s)
  140.      char *s;
  141. {
  142.   printf(
  143.       "Usage: %s [-r or -nf] [-b block-size] [-c cache-size] [-g gdbm-file]\n",
  144.       s);
  145.   exit (2);
  146. }
  147.  
  148.  
  149. /* The test program allows one to call all the routines plus the hash function.
  150.    The commands are single letter commands.  The user is prompted for all other
  151.    information.  See the help command (?) for a list of all commands. */
  152.  
  153. int main (argc, argv)
  154.      int argc;
  155.      char *argv[];
  156.  
  157. {
  158.  
  159.   char  cmd_ch;
  160.  
  161.   datum key_data;
  162.   datum data_data;
  163.   datum return_data;
  164.  
  165.   char key_line[500];
  166.   char data_line[1000];
  167.  
  168.   char done = FALSE;
  169.   int  opt;
  170.   char reader = FALSE;
  171.   char newdb = FALSE;
  172.   int  fast  = 0;
  173.  
  174.   int  cache_size = DEFAULT_CACHESIZE;
  175.   int  block_size = 0;
  176.  
  177.   char *file_name = NULL;
  178.  
  179.  
  180.   /* Argument checking. */
  181.   opterr = 0;
  182.   while ((opt = getopt (argc, argv, "frnc:b:g:")) != -1)
  183.     switch (opt) {
  184.     case 'f':  fast = GDBM_FAST;
  185.                if (reader) usage (argv[0]);
  186.                break;
  187.     case 'r':  reader = TRUE;
  188.                if (newdb) usage (argv[0]);
  189.                break;
  190.     case 'n':  newdb = TRUE;
  191.                if (reader) usage (argv[0]);
  192.                break;
  193.     case 'c':  cache_size = atoi(optarg);
  194.                break;
  195.     case 'b':  block_size = atoi(optarg);
  196.                break;
  197.     case 'g':  file_name = optarg;
  198.                break;
  199.     default:  usage(argv[0]);
  200.     }
  201.  
  202.   if(file_name == NULL) 
  203.     file_name = "junk.gdbm";
  204.  
  205.   /* Initialize variables. */
  206.   key_data.dptr = NULL;
  207.   data_data.dptr = data_line;
  208.  
  209.   if (reader) {
  210.     gdbm_file = gdbm_open (file_name, block_size, GDBM_READER, 00664, NULL);
  211.   } else if (newdb) {
  212.     gdbm_file =
  213.         gdbm_open (file_name, block_size, GDBM_NEWDB | fast, 00664, NULL);
  214.   } else {
  215.     gdbm_file =
  216.         gdbm_open (file_name, block_size, GDBM_WRCREAT | fast, 00664, NULL);
  217.   }
  218.   if (gdbm_file == NULL) {
  219.     printf("gdbm_open failed, %s\n", gdbm_strerror(gdbm_errno));
  220.     exit (2);
  221.   }
  222.  
  223.   if (gdbm_setopt(gdbm_file, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1) {
  224.     printf("gdbm_setopt failed, %s\n", gdbm_strerror(gdbm_errno));
  225.     exit(2);
  226.   }
  227.  
  228.   /* Welcome message. */
  229.   printf ("\nWelcome to the gdbm test program.  Type ? for help.\n\n");
  230.   
  231.   while (!done)
  232.     {
  233.       printf ("com -> ");
  234.       cmd_ch = getchar ();
  235.       if (cmd_ch != '\n')
  236.     {
  237.       char temp;
  238.       do
  239.           temp = getchar ();
  240.       while (temp != '\n' && temp != EOF);
  241.     }
  242.       if (cmd_ch == EOF) cmd_ch = 'q';
  243.       switch (cmd_ch)
  244.     {
  245.     
  246.     /* Standard cases found in all test{dbm,ndbm,gdbm} programs. */
  247.     case '\n':
  248.       printf ("\n");
  249.       break;
  250.  
  251.     case 'c':
  252.       {
  253.         int temp;
  254.         temp = 0;
  255.         if (key_data.dptr != NULL) free (key_data.dptr);
  256.         return_data = gdbm_firstkey (gdbm_file);
  257.         while (return_data.dptr != NULL)
  258.           {
  259.         temp++;
  260.         key_data = return_data;
  261.         return_data = gdbm_nextkey (gdbm_file, key_data);
  262.         free (key_data.dptr);
  263.           }
  264.         printf ("There are %d items in the database.\n\n", temp);
  265.         key_data.dptr = NULL;
  266.       }
  267.       break;
  268.  
  269.     case 'd':
  270.       if (key_data.dptr != NULL) free (key_data.dptr);
  271.       printf ("key -> ");
  272.       gets (key_line);
  273.       key_data.dptr = key_line;
  274.       key_data.dsize = strlen (key_line)+1;
  275.       if (gdbm_delete (gdbm_file, key_data) != 0)
  276.         printf ("Item not found or deleted\n");
  277.       printf ("\n");
  278.       key_data.dptr = NULL;
  279.       break;
  280.  
  281.     case 'f':
  282.       if (key_data.dptr != NULL) free (key_data.dptr);
  283.       printf ("key -> ");
  284.       gets (key_line);
  285.       key_data.dptr = key_line;
  286.       key_data.dsize = strlen (key_line)+1;
  287.       return_data = gdbm_fetch (gdbm_file, key_data);
  288.       if (return_data.dptr != NULL)
  289.         {
  290.           printf ("data is ->%s\n\n", return_data.dptr);
  291.           free (return_data.dptr);
  292.         }
  293.       else
  294.         printf ("No such item found.\n\n");
  295.       key_data.dptr = NULL;
  296.       break;
  297.  
  298.     case 'n':
  299.       if (key_data.dptr != NULL) free (key_data.dptr);
  300.       printf ("key -> ");
  301.       gets (key_line);
  302.       key_data.dptr = key_line;
  303.       key_data.dsize = strlen (key_line)+1;
  304.       return_data = gdbm_nextkey (gdbm_file, key_data);
  305.       if (return_data.dptr != NULL)
  306.         {
  307.           key_data = return_data;
  308.           printf ("key is  ->%s\n", key_data.dptr);
  309.           return_data = gdbm_fetch (gdbm_file, key_data);
  310.           printf ("data is ->%s\n\n", return_data.dptr);
  311.           free (return_data.dptr);
  312.         }
  313.       else
  314.         {
  315.           printf ("No such item found.\n\n");
  316.           key_data.dptr = NULL;
  317.         }
  318.       break;
  319.  
  320.     case 'q':
  321.       done = TRUE;
  322.       break;
  323.  
  324.     case 's':
  325.       if (key_data.dptr != NULL) free (key_data.dptr);
  326.       printf ("key -> ");
  327.       gets (key_line);
  328.       key_data.dptr = key_line;
  329.       key_data.dsize = strlen (key_line)+1;
  330.       printf ("data -> ");
  331.       gets (data_line);
  332.       data_data.dsize = strlen (data_line)+1;
  333.       if (gdbm_store (gdbm_file, key_data, data_data, GDBM_REPLACE) != 0)
  334.         printf ("Item not inserted. \n");
  335.       printf ("\n");
  336.       key_data.dptr = NULL;
  337.       break;
  338.  
  339.     case '1':
  340.       if (key_data.dptr != NULL) free (key_data.dptr);
  341.       key_data = gdbm_firstkey (gdbm_file);
  342.       if (key_data.dptr != NULL)
  343.         {
  344.           printf ("key is  ->%s\n", key_data.dptr);
  345.           return_data = gdbm_fetch (gdbm_file, key_data);
  346.           printf ("data is ->%s\n\n", return_data.dptr);
  347.           free (return_data.dptr);
  348.         }
  349.       else
  350.         printf ("No such item found.\n\n");
  351.       break;
  352.  
  353.     case '2':
  354.       return_data = gdbm_nextkey (gdbm_file, key_data);
  355.       if (return_data.dptr != NULL)
  356.         {
  357.           free (key_data.dptr);
  358.           key_data = return_data;
  359.           printf ("key is  ->%s\n", key_data.dptr);
  360.           return_data = gdbm_fetch (gdbm_file, key_data);
  361.           printf ("data is ->%s\n\n", return_data.dptr);
  362.           free (return_data.dptr);
  363.         }
  364.       else
  365.         printf ("No such item found.\n\n");
  366.       break;
  367.  
  368.  
  369.     /* Special cases for the testgdbm program. */
  370.     case 'r':
  371.       {
  372.         if (gdbm_reorganize (gdbm_file))
  373.           printf ("Reorganization failed. \n\n");
  374.         else
  375.           printf ("Reorganization succeeded. \n\n");
  376.       }
  377.       break;
  378.  
  379.     case 'A':
  380.       _gdbm_print_avail_list (gdbm_file);
  381.       printf ("\n");
  382.       break;
  383.  
  384.     case 'B':
  385.       {
  386.         int temp;
  387.         char number[80];
  388.  
  389.         printf ("bucket? ");
  390.         gets (number);
  391.         sscanf (number,"%d",&temp);
  392.  
  393.         if (temp >= gdbm_file->header->dir_size /4)
  394.           {
  395.         printf ("Not a bucket. \n\n");
  396.         break;
  397.           }
  398.         _gdbm_get_bucket (gdbm_file, temp);
  399.       }
  400.       printf ("Your bucket is now ");
  401.  
  402.     case 'C':
  403.       print_bucket (gdbm_file->bucket, "Current bucket");
  404.       printf ("\n current directory entry = %d.\n", gdbm_file->bucket_dir);
  405.       printf (" current bucket address  = %d.\n\n",
  406.           gdbm_file->cache_entry->ca_adr);
  407.       break;
  408.  
  409.     case 'D':
  410.       printf ("Hash table directory.\n");
  411.       printf ("  Size =  %d.  Bits = %d. \n\n",gdbm_file->header->dir_size,
  412.           gdbm_file->header->dir_bits);
  413.       {
  414.         int temp;
  415.  
  416.         for (temp = 0; temp < gdbm_file->header->dir_size / 4; temp++)
  417.           {
  418.         printf ("  %10d:  %12d\n", temp, gdbm_file->dir[temp]);
  419.         if ( (temp+1) % 20 == 0 && isatty (0))
  420.           {
  421.             printf ("*** CR to continue: ");
  422.             while (getchar () != '\n') /* Do nothing. */;
  423.           }
  424.           }
  425.       }
  426.       printf ("\n");
  427.       break;
  428.  
  429.     case 'F':
  430.       {
  431.         printf ("\nFile Header: \n\n");
  432.         printf ("  table        = %d\n", gdbm_file->header->dir);
  433.         printf ("  table size   = %d\n", gdbm_file->header->dir_size);
  434.         printf ("  table bits   = %d\n", gdbm_file->header->dir_bits);
  435.         printf ("  block size   = %d\n", gdbm_file->header->block_size);
  436.         printf ("  bucket elems = %d\n", gdbm_file->header->bucket_elems);
  437.         printf ("  bucket size  = %d\n", gdbm_file->header->bucket_size);
  438.         printf ("  header magic = %x\n", gdbm_file->header->header_magic);
  439.         printf ("  next block   = %d\n", gdbm_file->header->next_block);
  440.         printf ("  avail size   = %d\n", gdbm_file->header->avail.size);
  441.         printf ("  avail count  = %d\n", gdbm_file->header->avail.count);
  442.         printf ("  avail nx blk = %d\n", gdbm_file->header->avail.next_block);
  443.         printf ("\n");
  444.       }
  445.       break;
  446.  
  447.         case 'H':
  448.       if (key_data.dptr != NULL) free (key_data.dptr);
  449.       printf ("key -> ");
  450.       gets (key_line);
  451.       key_data.dptr = key_line;
  452.       key_data.dsize = strlen (key_line)+1;
  453.       printf ("hash value = %x. \n\n", _gdbm_hash (key_data));
  454.       key_data.dptr = NULL;
  455.       break;
  456.  
  457.     case 'K':
  458.       _gdbm_print_bucket_cache (gdbm_file);
  459.       break;
  460.  
  461.     case 'V':
  462.       printf ("%s\n\n", gdbm_version);
  463.       break;
  464.  
  465.     case '?':
  466.       printf ("c - count (number of entries)\n");
  467.       printf ("d - delete\n");
  468.       printf ("f - fetch\n");
  469.       printf ("n - nextkey\n");
  470.       printf ("q - quit\n");
  471.       printf ("s - store\n");
  472.       printf ("1 - firstkey\n");
  473.       printf ("2 - nextkey on last key (from n, 1 or 2)\n\n");
  474.  
  475.       printf ("r - reorganize\n");
  476.       printf ("A - print avail list\n");
  477.       printf ("B - get and print current bucket n\n");
  478.       printf ("C - print current bucket\n");
  479.       printf ("D - print hash directory\n");
  480.       printf ("F - print file header\n");
  481.       printf ("H - hash value of key\n");
  482.       printf ("K - print the bucket cache\n");
  483.       printf ("V - print version of gdbm\n");
  484.       break;
  485.  
  486.     default:
  487.       printf ("What? \n\n");
  488.       break;
  489.  
  490.     }
  491.     }
  492.  
  493.   /* Quit normally. */
  494.   exit (0);
  495.  
  496. }
  497.