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