1#include <sys/types.h>
2
3#include <errno.h>
4#include <fcntl.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
9#include "db_185.h"
10
11void	err(char *);
12int	mycmp(const DBT *, const DBT *);
13void	ops(DB *, int);
14
15int
16main()
17{
18	DB *dbp;
19	HASHINFO h_info;
20	BTREEINFO b_info;
21	RECNOINFO r_info;
22
23	printf("\tBtree...\n");
24	memset(&b_info, 0, sizeof(b_info));
25	b_info.flags = R_DUP;
26	b_info.cachesize = 100 * 1024;
27	b_info.psize = 512;
28	b_info.lorder = 4321;
29	b_info.compare = mycmp;
30	(void)remove("a.db");
31	if ((dbp =
32	   dbopen("a.db", O_CREAT | O_RDWR, 0664, DB_BTREE, &b_info)) == NULL)
33		err("dbopen: btree");
34	ops(dbp, DB_BTREE);
35
36	printf("\tHash...\n");
37	memset(&h_info, 0, sizeof(h_info));
38	h_info.bsize = 512;
39	h_info.ffactor = 6;
40	h_info.nelem = 1000;
41	h_info.cachesize = 100 * 1024;
42	h_info.lorder = 1234;
43	(void)remove("a.db");
44	if ((dbp =
45	    dbopen("a.db", O_CREAT | O_RDWR, 0664, DB_HASH, &h_info)) == NULL)
46		err("dbopen: hash");
47	ops(dbp, DB_HASH);
48
49	printf("\tRecno...\n");
50	memset(&r_info, 0, sizeof(r_info));
51	r_info.flags = R_FIXEDLEN;
52	r_info.cachesize = 100 * 1024;
53	r_info.psize = 1024;
54	r_info.reclen = 37;
55	(void)remove("a.db");
56	if ((dbp =
57	   dbopen("a.db", O_CREAT | O_RDWR, 0664, DB_RECNO, &r_info)) == NULL)
58		err("dbopen: recno");
59	ops(dbp, DB_RECNO);
60
61	return (0);
62}
63
64int
65mycmp(a, b)
66	const DBT *a, *b;
67{
68	size_t len;
69	u_int8_t *p1, *p2;
70
71	len = a->size > b->size ? b->size : a->size;
72	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
73		if (*p1 != *p2)
74			return ((long)*p1 - (long)*p2);
75	return ((long)a->size - (long)b->size);
76}
77
78void
79ops(dbp, type)
80	DB *dbp;
81	int type;
82{
83	FILE *outfp;
84	DBT key, data;
85	recno_t recno;
86	int i, ret;
87	char buf[64];
88
89	memset(&key, 0, sizeof(key));
90	memset(&data, 0, sizeof(data));
91
92	for (i = 1; i < 100; ++i) {		/* Test DB->put. */
93		sprintf(buf, "abc_%d_efg", i);
94		if (type == DB_RECNO) {
95			recno = i;
96			key.data = &recno;
97			key.size = sizeof(recno);
98		} else {
99			key.data = data.data = buf;
100			key.size = data.size = strlen(buf);
101		}
102
103		data.data = buf;
104		data.size = strlen(buf);
105		if (dbp->put(dbp, &key, &data, 0))
106			err("DB->put");
107	}
108
109	if (type == DB_RECNO) {			/* Test DB->get. */
110		recno = 97;
111		key.data = &recno;
112		key.size = sizeof(recno);
113	} else {
114		key.data = buf;
115		key.size = strlen(buf);
116	}
117	sprintf(buf, "abc_%d_efg", 97);
118	if (dbp->get(dbp, &key, &data, 0) != 0)
119		err("DB->get");
120	if (memcmp(data.data, buf, strlen(buf)))
121		err("DB->get: wrong data returned");
122
123	if (type == DB_RECNO) {			/* Test DB->put no-overwrite. */
124		recno = 42;
125		key.data = &recno;
126		key.size = sizeof(recno);
127	} else {
128		key.data = buf;
129		key.size = strlen(buf);
130	}
131	sprintf(buf, "abc_%d_efg", 42);
132	if (dbp->put(dbp, &key, &data, R_NOOVERWRITE) == 0)
133		err("DB->put: no-overwrite succeeded");
134
135	if (type == DB_RECNO) {			/* Test DB->del. */
136		recno = 35;
137		key.data = &recno;
138		key.size = sizeof(recno);
139	} else {
140		sprintf(buf, "abc_%d_efg", 35);
141		key.data = buf;
142		key.size = strlen(buf);
143	}
144	if (dbp->del(dbp, &key, 0))
145		err("DB->del");
146
147						/* Test DB->seq. */
148	if ((outfp = fopen("output", "w")) == NULL)
149		err("fopen: output");
150	while ((ret = dbp->seq(dbp, &key, &data, R_NEXT)) == 0) {
151		if (type == DB_RECNO)
152			fprintf(outfp, "%d\n", *(int *)key.data);
153		else
154			fprintf(outfp,
155			    "%.*s\n", (int)key.size, (char *)key.data);
156		fprintf(outfp, "%.*s\n", (int)data.size, (char *)data.data);
157	}
158	if (ret != 1)
159		err("DB->seq");
160	fclose(outfp);
161	switch (type) {
162	case DB_BTREE:
163		ret = system("cmp output O.BH");
164		break;
165	case DB_HASH:
166		ret = system("sort output | cmp - O.BH");
167		break;
168	case DB_RECNO:
169		ret = system("cmp output O.R");
170		break;
171	}
172	if (ret != 0)
173		err("output comparison failed");
174
175	if (dbp->sync(dbp, 0))			/* Test DB->sync. */
176		err("DB->sync");
177
178	if (dbp->close(dbp))			/* Test DB->close. */
179		err("DB->close");
180}
181
182void
183err(s)
184	char *s;
185{
186	fprintf(stderr, "\t%s: %s\n", s, strerror(errno));
187	exit(EXIT_FAILURE);
188}
189