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