1/*
2 * $Id: b_load.c,v 1.13 2007/12/21 13:58:30 bostic Exp $
3 */
4#include "bench.h"
5
6static int b_load_usage(void);
7
8int
9b_load(int argc, char *argv[])
10{
11	extern char *optarg;
12	extern int optind, __db_getopt_reset;
13	DB *dbp;
14	DBTYPE type;
15	DBT key, data;
16	db_recno_t recno;
17	u_int32_t cachesize;
18	int ch, i, count, duplicate;
19	char *ts, buf[32];
20
21	type = DB_BTREE;
22	cachesize = MEGABYTE;
23	count = 100000;
24	duplicate = 0;
25	ts = "Btree";
26	__db_getopt_reset = 1;
27	while ((ch = getopt(argc, argv, "C:c:dt:")) != EOF)
28		switch (ch) {
29		case 'C':
30			cachesize = (u_int32_t)atoi(optarg);
31			break;
32		case 'c':
33			count = atoi(optarg);
34			break;
35		case 'd':
36			duplicate = 1;
37			break;
38		case 't':
39			switch (optarg[0]) {
40			case 'B': case 'b':
41				ts = "Btree";
42				type = DB_BTREE;
43				break;
44			case 'H': case 'h':
45				if (b_util_have_hash())
46					return (0);
47				ts = "Hash";
48				type = DB_HASH;
49				break;
50			case 'Q': case 'q':
51				if (b_util_have_queue())
52					return (0);
53				ts = "Queue";
54				type = DB_QUEUE;
55				break;
56			case 'R': case 'r':
57				ts = "Recno";
58				type = DB_RECNO;
59				break;
60			default:
61				return (b_load_usage());
62			}
63			break;
64		case '?':
65		default:
66			return (b_load_usage());
67		}
68	argc -= optind;
69	argv += optind;
70	if (argc != 0)
71		return (b_load_usage());
72
73	/* Usage. */
74	if (duplicate && (type == DB_QUEUE || type == DB_RECNO)) {
75		fprintf(stderr,
76		    "b_load: Queue an Recno don't support duplicates\n");
77		return (b_load_usage());
78	}
79
80#if DB_VERSION_MAJOR < 3 || DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 1
81	/*
82	 * DB versions prior to 3.1.17 didn't have off-page duplicates, so
83	 * this test can run forever.
84	 */
85	if (duplicate)
86		return (0);
87#endif
88
89	/* Create the database. */
90	DB_BENCH_ASSERT(db_create(&dbp, NULL, 0) == 0);
91	DB_BENCH_ASSERT(dbp->set_cachesize(dbp, 0, cachesize, 0) == 0);
92	if (duplicate)
93		DB_BENCH_ASSERT(dbp->set_flags(dbp, DB_DUP) == 0);
94	dbp->set_errfile(dbp, stderr);
95
96	/* Set record length for Queue. */
97	if (type == DB_QUEUE)
98		DB_BENCH_ASSERT(dbp->set_re_len(dbp, 20) == 0);
99
100#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1
101	DB_BENCH_ASSERT(
102	    dbp->open(dbp, NULL, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
103#else
104	DB_BENCH_ASSERT(
105	    dbp->open(dbp, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
106#endif
107
108	/* Initialize the data. */
109	memset(&key, 0, sizeof(key));
110	memset(&data, 0, sizeof(data));
111
112	/* Insert count in-order key/data pairs. */
113	TIMER_START;
114	if (duplicate) {
115		key.size = 10;
116		key.data = "01234567890123456789";
117		data.data = buf;
118		data.size = 20;
119		for (i = 0; i < count; ++i) {
120			(void)snprintf(buf, sizeof(buf), "%020d", i);
121			DB_BENCH_ASSERT(
122			    dbp->put(dbp, NULL, &key, &data, 0) == 0);
123		}
124	} else {
125		data.data = buf;
126		data.size = 20;
127		if (type == DB_BTREE || type == DB_HASH) {
128			key.size = 10;
129			key.data = buf;
130			for (i = 0; i < count; ++i) {
131				(void)snprintf(buf, sizeof(buf), "%010d", i);
132				DB_BENCH_ASSERT(
133				    dbp->put(dbp, NULL, &key, &data, 0) == 0);
134			}
135		} else {
136			key.data = &recno;
137			key.size = sizeof(recno);
138			for (i = 0, recno = 1; i < count; ++i, ++recno)
139				DB_BENCH_ASSERT(
140				    dbp->put(dbp, NULL, &key, &data, 0) == 0);
141		}
142	}
143
144	TIMER_STOP;
145
146	printf("# %d %s database in-order put of 10/20 byte key/data %sitems\n",
147	    count, ts, duplicate ? "duplicate " : "");
148	TIMER_DISPLAY(count);
149
150	DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
151
152	return (0);
153}
154
155static int
156b_load_usage()
157{
158	(void)fprintf(stderr,
159	    "usage: b_load [-d] [-C cachesz] [-c count] [-t type]\n");
160	return (EXIT_FAILURE);
161}
162