DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T t

⟦115e59f40⟧ TextFile

    Length: 10673 (0x29b1)
    Types: TextFile
    Names: »testgdbm.c«

Derivation

└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89
    └─⟦847972ed9⟧ »./gdbm0.9.tar.Z« 
        └─⟦e41d67701⟧ 
            └─⟦this⟧ »gdbm/testgdbm.c« 

TextFile

/* testgdbm.c - Driver program to test the database routines and to
   help debug gdbm.  Uses inside information to show "system" information */

/*  GNU DBM  - DataBase Manager (database subroutines) by Philip A. Nelson
    Copyright (C) 1989  Free Software Foundation, Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    You may contact the author by:
       e-mail:  phil@wwu.edu
      us-mail:  Philip A. Nelson
                Computer Science Department
                Western Washington University
                Bellingham, WA 98226
        phone:  (206) 676-3035
       
*************************************************************************/


#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include "gdbmdefs.h"
#include "systems.h"
#include "gdbmerrno.h"
#include "extern.h"

extern gdbm_error gdbm_errno;

extern char * gdbm_version;

gdbm_file_info *gdbm_file;

/* Debug procedure to print the contents of the current hash bucket. */
print_bucket (bucket, mesg)
     hash_bucket *bucket;
     char *mesg;
{
  int  index;

  printf("******* %s **********\n\nbits = %d\ncount= %d\nHash Table:\n",
	 mesg, bucket->bucket_bits, bucket->count);
  printf("     #    hash value     key size    data size     data adr  home\n");
  for (index = 0; index < gdbm_file->header->bucket_elems; index++)
    printf("  %4d  %12x  %11d  %11d  %11d %5d\n", index,
	   bucket->h_table[index].hash_value,
	   bucket->h_table[index].key_size,
	   bucket->h_table[index].data_size,
	   bucket->h_table[index].data_pointer,
	   bucket->h_table[index].hash_value % gdbm_file->header->bucket_elems);

  printf ("\nAvail count = %1d\n", bucket->av_count);
  printf ("Avail  adr     size\n");
  for (index = 0; index < bucket->av_count; index++)
    printf ("%9d%9d\n", bucket->bucket_avail[index].av_adr,
	                bucket->bucket_avail[index].av_size);
}


_gdbm_print_avail_list (dbf)
     gdbm_file_info *dbf;
{
  int temp;
  int size;
  avail_block *av_stk;
 
  /* Print the the header avail block.  */
  printf ("\nheader block\nsize  = %d\ncount = %d\n",
	  dbf->header->avail.size, dbf->header->avail.count);
  for (temp = 0; temp < dbf->header->avail.count; temp++)
    {
      printf ("  %15d   %10d \n", dbf->header->avail.av_table[temp].av_size,
	      dbf->header->avail.av_table[temp].av_adr);
    }

  /* Initialize the variables for a pass throught the avail stack. */
  temp = dbf->header->avail.next_block;
  size = (dbf->header->avail.size * sizeof(avail_elem)) >> 1
            + sizeof(avail_block);
  av_stk = (avail_block *) alloca (size);

  /* Print the stack. */
  while ( FALSE )
    {
      lseek (dbf->desc, temp, L_SET);
      read  (dbf->desc, av_stk, size);

      /* Print the block! */
      printf ("\nblock = %d\nsize  = %d\ncount = %d\n", temp,
	      av_stk->size, av_stk->count);
      for (temp = 0; temp < av_stk->count; temp++)
	{
	  printf ("  %15d   %10d \n", av_stk->av_table[temp].av_size,
	    av_stk->av_table[temp].av_adr);
	}
      temp = av_stk->next_block;
    }
}



/* The test program allows one to call all the routines plus the hash function.
   The commands are single letter commands.  The user is prompted for all other
   information.  See the help command (?) for a list of all commands. */

main ()
{

  char  cmd_ch;

  datum key_data;
  datum data_data;
  datum return_data;

  char key_line[500];
  char data_line[1000];


  char done = FALSE;
  key_data.dptr = key_line;
  data_data.dptr = data_line;

  gdbm_file = gdbm_open ("junk.dbm", 512, GDBM_WRCREAT, 00664, NULL);
  if (gdbm_file == NULL)
    {
      if (gdbm_errno != CANT_BE_WRITER)
	printf("gdbm_open failed.\n");
      else
	printf("Can't open as a writer. \n");
      exit(2);
    }

  /* Welcome message. */
  printf ("\nWelcome to the gdbm test program.  Type ? for help.\n\n");
  
  while (!done)
    {
      printf("com -> ");
      cmd_ch = getchar();
      if (cmd_ch != '\n')
	{
	  char temp;
	  do
	      temp = getchar();
	  while (temp != '\n' && temp != EOF);
	}
      if (cmd_ch == EOF) cmd_ch = 'q';
      switch (cmd_ch)
	{
	case '\n':
	  printf ("\n");
	  break;

        case 'h':
	  printf ("key -> ");
	  gets(key_line);
	  key_data.dsize = strlen (key_line)+1;
	  printf ("hash value = %x. \n\n", _gdbm_hash (key_data));
	  break;

	case 'q':
	  done = TRUE;
	  break;

	case 'f':
	  if (return_data.dptr != NULL) free (return_data.dptr);
	  printf ("key -> ");
	  gets (key_line);
	  key_data.dsize = strlen (key_line)+1;
	  return_data = gdbm_fetch (gdbm_file, key_data);
	  if (return_data.dptr != NULL)
	      printf ("data is ->%s\n\n", return_data.dptr);
	  else
	    printf ("No such item found.\n\n");
	  break;

	case 's':
	  printf ("key -> ");
	  gets(key_line);
	  key_data.dsize = strlen (key_line)+1;
	  printf ("data -> ");
	  gets(data_line);
	  data_data.dsize = strlen (data_line)+1;
	  if (gdbm_store (gdbm_file, key_data, data_data, GDBM_REPLACE) != 0)
	    printf("Item not inserted. \n");
	  printf("\n");
	  break;

	case 'd':
	  printf ("key -> ");
	  gets(key_line);
	  key_data.dsize = strlen (key_line)+1;
	  if (gdbm_delete (gdbm_file, key_data) != 0)
	    printf("Item not found or deleted\n");
	  printf("\n");
	  break;

	case '1':
	  if (return_data.dptr != NULL) free (return_data.dptr);
	  return_data = gdbm_firstkey(gdbm_file);
	  if (return_data.dptr != NULL)
	      printf ("data is ->%s\n\n", return_data.dptr);
	  else
	    printf ("No such item found.\n\n");
	  break;

	case 'n':
	  if (return_data.dptr != NULL) free (return_data.dptr);
	  printf ("key -> ");
	  gets (key_line);
	  key_data.dsize = strlen (key_line)+1;
	  return_data = gdbm_nextkey (gdbm_file, key_data);
	  if (return_data.dptr != NULL)
	      printf ("data is ->%s\n\n", return_data.dptr);
	  else
	    printf ("No such item found.\n\n");
	  break;

	case '2':
	  key_data = return_data;
	  return_data = gdbm_nextkey (gdbm_file, key_data);
	  if (return_data.dptr != NULL)
	      printf ("data is ->%s\n\n", return_data.dptr);
	  else
	    printf ("No such item found.\n\n");
	  if (key_data.dptr != NULL) free (key_data.dptr);
	  key_data.dptr = key_line;
	  break;

	case 'b':
	  {
	    int temp;
	    char number[80];

	    printf ("bucket? ");
	    gets (number);
	    sscanf (number,"%d",&temp);

	    if (temp >= gdbm_file->header->dir_size /4)
	      {
		printf ("Not a bucket. \n\n");
		break;
	      }
	    _gdbm_get_bucket (gdbm_file, temp);
	  }
	  printf ("Your bucket is now ");

	case 'p':
	  print_bucket (gdbm_file->bucket, "Current bucket");
	  printf("\n current directory entry = %d.\n",   gdbm_file->bucket_dir);
	  printf(" current bucket address  = %d.\n\n",
		 gdbm_file->cache_entry->ca_adr);
	  break;

	case 'l':
	  {
	    int temp;

	    printf ("\nFile Header: \n\n");
	    printf ("  table        = %d\n", gdbm_file->header->dir);
	    printf ("  table size   = %d\n", gdbm_file->header->dir_size);
	    printf ("  table bits   = %d\n", gdbm_file->header->dir_bits);
	    printf ("  block size   = %d\n", gdbm_file->header->block_size);
	    printf ("  bucket elems = %d\n", gdbm_file->header->bucket_elems);
	    printf ("  bucket size  = %d\n", gdbm_file->header->bucket_size);
	    printf ("  header magic = %x\n", gdbm_file->header->header_magic);
	    printf ("  next block   = %d\n", gdbm_file->header->next_block);
	    printf ("  avail size   = %d\n", gdbm_file->header->avail.size);
	    printf ("  avail count  = %d\n", gdbm_file->header->avail.count);
	    printf ("  avail nx blk = %d\n", gdbm_file->header->avail.next_block);
	    printf ("\n");
	  }
	  break;

	case 'a':
	  _gdbm_print_avail_list(gdbm_file);
	  printf ("\n");
	  break;

	case 'r':
	  printf ("Hash table directory.\n");
	  printf ("  Size =  %d.  Bits = %d. \n\n",gdbm_file->header->dir_size,
		  gdbm_file->header->dir_bits);
	  {
	    int temp;

	    for (temp = 0; temp < gdbm_file->header->dir_size / 4; temp++)
	      {
		printf ("  %10d:  %12d\n", temp, gdbm_file->dir[temp]);
		if ( (temp+1) % 20 == 0 && isatty (0))
		  {
		    printf ("*** CR to continue: ");
		    while (getchar() != '\n') /* Do nothing. */;
		  }
	      }
	  }
	  printf ("\n");
	  break;

	case 'c':
	  {
	    int temp;
	    temp = 0;
	    if (return_data.dptr != NULL) free (return_data.dptr);
	    return_data = gdbm_firstkey (gdbm_file);
	    while (return_data.dptr != NULL)
	      {
		temp++;
		key_data = return_data;
		return_data = gdbm_nextkey (gdbm_file, key_data);
		free (key_data.dptr);
	      }
	    printf ("There are %d items in the database.\n\n", temp);
	  }
	  key_data.dptr = key_line;
	  break;

	case 'H':
	  {
	    int temp;
	    temp = 0;
	    if (return_data.dptr != NULL) free (return_data.dptr);
	    return_data = gdbm_firstkey (gdbm_file);
	    while (return_data.dptr != NULL)
	      {
		printf ("%10x\n", gdbm_file->bucket->h_table
			[gdbm_file->cache_entry->ca_data.elem_loc].hash_value);
		temp++;
		key_data = return_data;
		return_data = gdbm_nextkey (gdbm_file, key_data);
		free (key_data.dptr);
	      }
	    printf ("There are %d items in the database.\n\n", temp);
	  }
	  key_data.dptr = key_line;
	  break;

	case 'x':
	  {
	    if (gdbm_reorganize(gdbm_file))
	      printf ("Reorganization failed. \n\n");
	    else
	      printf ("Reorganization succeeded. \n\n");
	  }
	  break;

	case 'v':
	  printf ("%s\n\n", gdbm_version);
	  break;

	case '?':
	  printf ("a - print avail list\n");
	  printf ("b - get and print current bucket n\n");
	  printf ("c - count (number of entries)\n");
	  printf ("d - delete\n");
	  printf ("f - fetch\n");
	  printf ("h - hash value of key\n");
	  printf ("l - print file header\n");
	  printf ("n - nextkey\n");
	  printf ("p - print current bucket\n");
	  printf ("q - quit\n");
	  printf ("r - print hash directory\n");
	  printf ("s - store\n");
	  printf ("v - print version of gdbm\n");
	  printf ("x - reorganize\n");
	  printf ("1 - firstkey\n");
	  printf ("2 - nextkey on last return value\n\n");
	  break;

	default:
	  printf("What? \n\n");
	  break;

	}
    }

  /* Quit normally. */
  exit(0);

}