source: main/tags/2.21/gsdl/packages/wingdbm/testgdbm.c@ 26544

Last change on this file since 26544 was 18, checked in by sjboddie, 26 years ago

Added windows gdbm and mg versions

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