xref: /original-bsd/lib/libc/db/test/hash.tests/tdel.c (revision cc7f0fbb)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Margo Seltzer.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 char copyright[] =
13 "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
14  All rights reserved.\n";
15 #endif /* not lint */
16 
17 #ifndef lint
18 static char sccsid[] = "@(#)tdel.c	5.2 (Berkeley) 03/12/91";
19 #endif /* not lint */
20 
21 #include <sys/types.h>
22 #include <sys/file.h>
23 #include <db.h>
24 #include <stdio.h>
25 
26 #define INITIAL	25000
27 #define MAXWORDS    25000	       /* # of elements in search table */
28 
29 /* Usage: thash pagesize fillfactor file */
30 char	wp1[8192];
31 char	wp2[8192];
32 main(argc, argv)
33 char **argv;
34 {
35 	DBT item, key;
36 	DB	*dbp;
37 	HASHINFO ctl;
38 	FILE *fp;
39 	int	stat;
40 
41 	int i = 0;
42 
43 	argv++;
44 	ctl.nelem = INITIAL;
45 	ctl.hash = NULL;
46 	ctl.bsize = atoi(*argv++);
47 	ctl.ffactor = atoi(*argv++);
48 	ctl.cachesize = 1024 * 1024;	/* 1 MEG */
49 	ctl.lorder = 0;
50 	argc -= 2;
51 	if (!(dbp = hash_open( NULL, O_CREAT|O_RDWR, 0400, &ctl))) {
52 		/* create table */
53 		fprintf(stderr, "cannot create: hash table size %d)\n",
54 			INITIAL);
55 		exit(1);
56 	}
57 
58 	key.data = wp1;
59 	item.data = wp2;
60 	while ( fgets(wp1, 8192, stdin) &&
61 		fgets(wp2, 8192, stdin) &&
62 		i++ < MAXWORDS) {
63 /*
64 * put info in structure, and structure in the item
65 */
66 		key.size = strlen(wp1);
67 		item.size = strlen(wp2);
68 
69 /*
70  * enter key/data pair into the table
71  */
72 		if ((dbp->put)(dbp, &key, &item, R_NOOVERWRITE) != NULL) {
73 			fprintf(stderr, "cannot enter: key %s\n",
74 				item.data);
75 			exit(1);
76 		}
77 	}
78 
79 	if ( --argc ) {
80 		fp = fopen ( argv[0], "r");
81 		i = 0;
82 		while ( fgets(wp1, 8192, fp) &&
83 			fgets(wp2, 8192, fp) &&
84 			i++ < MAXWORDS) {
85 		    key.size = strlen(wp1);
86 		    stat = (dbp->delete)(dbp, &key);
87 		    if (stat) {
88 			fprintf ( stderr, "Error retrieving %s\n", key.data );
89 			exit(1);
90 		    }
91 		}
92 		fclose(fp);
93 	}
94 	(dbp->close)(dbp);
95 	exit(0);
96 }
97