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