1/*
2 * $Id: b_get.c,v 1.14 2008/01/31 17:01:22 bostic Exp $
3 */
4#include "bench.h"
5
6static int b_get_usage(void);
7
8int
9b_get(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;
19	char *ts;
20
21	type = DB_BTREE;
22	cachesize = MEGABYTE;
23	count = 100000;
24	ts = "Btree";
25	__db_getopt_reset = 1;
26	while ((ch = getopt(argc, argv, "C:c:t:")) != EOF)
27		switch (ch) {
28		case 'C':
29			cachesize = (u_int32_t)atoi(optarg);
30			break;
31		case 'c':
32			count = atoi(optarg);
33			break;
34		case 't':
35			switch (optarg[0]) {
36			case 'B': case 'b':
37				ts = "Btree";
38				type = DB_BTREE;
39				break;
40			case 'H': case 'h':
41				if (b_util_have_hash())
42					return (0);
43				ts = "Hash";
44				type = DB_HASH;
45				break;
46			case 'Q': case 'q':
47				if (b_util_have_queue())
48					return (0);
49				ts = "Queue";
50				type = DB_QUEUE;
51				break;
52			case 'R': case 'r':
53				ts = "Recno";
54				type = DB_RECNO;
55				break;
56			default:
57				return (b_get_usage());
58			}
59			break;
60		case '?':
61		default:
62			return (b_get_usage());
63		}
64	argc -= optind;
65	argv += optind;
66	if (argc != 0)
67		return (b_get_usage());
68
69	/* Create the database. */
70	DB_BENCH_ASSERT(db_create(&dbp, NULL, 0) == 0);
71	DB_BENCH_ASSERT(dbp->set_cachesize(dbp, 0, cachesize, 0) == 0);
72	dbp->set_errfile(dbp, stderr);
73
74	/* Set record length for Queue. */
75	if (type == DB_QUEUE)
76		DB_BENCH_ASSERT(dbp->set_re_len(dbp, 10) == 0);
77
78#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1
79	DB_BENCH_ASSERT(
80	    dbp->open(dbp, NULL, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
81#else
82	DB_BENCH_ASSERT(
83	    dbp->open(dbp, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
84#endif
85
86	/* Store a key/data pair. */
87	memset(&key, 0, sizeof(key));
88	memset(&data, 0, sizeof(data));
89	switch (type) {
90	case DB_BTREE:
91	case DB_HASH:
92		key.data = "aaaaa";
93		key.size = 5;
94		break;
95	case DB_QUEUE:
96	case DB_RECNO:
97		recno = 1;
98		key.data = &recno;
99		key.size = sizeof(recno);
100		break;
101	case DB_UNKNOWN:
102		b_util_abort();
103		break;
104	}
105	data.data = "bbbbb";
106	data.size = 5;
107
108	DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0);
109
110	/* Retrieve the key/data pair count times. */
111	TIMER_START;
112	for (i = 0; i < count; ++i)
113		DB_BENCH_ASSERT(dbp->get(dbp, NULL, &key, &data, 0) == 0);
114	TIMER_STOP;
115
116	printf("# %d %s database get of cached key/data item\n", count, ts);
117	TIMER_DISPLAY(count);
118
119	DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
120
121	return (0);
122}
123
124static int
125b_get_usage()
126{
127	(void)fprintf(stderr,
128	    "usage: b_get [-C cachesz] [-c count] [-t type]\n");
129	return (EXIT_FAILURE);
130}
131