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