• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt/router/db-4.8.30/build_vxworks/test_micro/
1/*
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2005-2009 Oracle.  All rights reserved.
5 *
6 * $Id$
7 */
8#include "bench.h"
9
10static int b_del_usage(void);
11
12int
13b_del(int argc, char *argv[])
14{
15	extern char *optarg;
16	extern int optind, __db_getopt_reset;
17	DB *dbp;
18	DBC *dbc;
19	DBT key, data;
20	DBTYPE type;
21	db_recno_t recno;
22	u_int32_t cachesize;
23	int ch, i, count, ret, use_cursor;
24	char *ts, buf[32];
25
26	type = DB_BTREE;
27	cachesize = MEGABYTE;
28	count = 100000;
29	use_cursor = 0;
30	ts = "Btree";
31	__db_getopt_reset = 1;
32	while ((ch = getopt(argc, argv, "C:c:t:w")) != EOF)
33		switch (ch) {
34		case 'C':
35			cachesize = (u_int32_t)atoi(optarg);
36			break;
37		case 'c':
38			count = atoi(optarg);
39			break;
40		case 't':
41			switch (optarg[0]) {
42			case 'B': case 'b':
43				ts = "Btree";
44				type = DB_BTREE;
45				break;
46			case 'H': case 'h':
47				if (b_util_have_hash())
48					return (0);
49				ts = "Hash";
50				type = DB_HASH;
51				break;
52			case 'Q': case 'q':
53				if (b_util_have_queue())
54					return (0);
55				ts = "Queue";
56				type = DB_QUEUE;
57				break;
58			case 'R': case 'r':
59				ts = "Recno";
60				type = DB_RECNO;
61				break;
62			default:
63				return (b_del_usage());
64			}
65			break;
66		case 'w':
67			use_cursor = 1;
68			break;
69		case '?':
70		default:
71			return (b_del_usage());
72		}
73	argc -= optind;
74	argv += optind;
75	if (argc != 0)
76		return (b_del_usage());
77
78	/* Create the database. */
79	DB_BENCH_ASSERT(db_create(&dbp, NULL, 0) == 0);
80	DB_BENCH_ASSERT(dbp->set_cachesize(dbp, 0, cachesize, 0) == 0);
81	dbp->set_errfile(dbp, stderr);
82
83	/* Set record length for Queue. */
84	if (type == DB_QUEUE)
85		DB_BENCH_ASSERT(dbp->set_re_len(dbp, 20) == 0);
86
87#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1
88	DB_BENCH_ASSERT(
89	    dbp->open(dbp, NULL, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
90#else
91	DB_BENCH_ASSERT(
92	    dbp->open(dbp, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
93#endif
94
95	/* Initialize the data. */
96	memset(&key, 0, sizeof(key));
97	memset(&data, 0, sizeof(data));
98	data.data = "01234567890123456789";
99	data.size = 20;
100
101	/* Store a key/data pair. */
102	switch (type) {
103	case DB_BTREE:
104	case DB_HASH:
105		key.data = buf;
106		key.size = 10;
107		break;
108	case DB_QUEUE:
109	case DB_RECNO:
110		key.data = &recno;
111		key.size = sizeof(recno);
112		break;
113	case DB_UNKNOWN:
114		b_util_abort();
115		break;
116	}
117
118	/* Insert count in-order key/data pairs. */
119	if (type == DB_BTREE || type == DB_HASH)
120		for (i = 0; i < count; ++i) {
121			(void)snprintf(buf, sizeof(buf), "%010d", i);
122			DB_BENCH_ASSERT(
123			    dbp->put(dbp, NULL, &key, &data, 0) == 0);
124		}
125	else
126		for (i = 0, recno = 1; i < count; ++i, ++recno)
127			DB_BENCH_ASSERT(
128			    dbp->put(dbp, NULL, &key, &data, 0) == 0);
129
130	/* Delete the records. */
131	TIMER_START;
132	if (use_cursor) {
133		DB_BENCH_ASSERT(dbp->cursor(dbp, NULL, &dbc, 0) == 0);
134		while ((ret = dbc->c_get(dbc, &key, &data, DB_NEXT)) == 0)
135			DB_BENCH_ASSERT(dbc->c_del(dbc, 0) == 0);
136		DB_BENCH_ASSERT (ret == DB_NOTFOUND);
137	} else
138		if (type == DB_BTREE || type == DB_HASH)
139			for (i = 0; i < count; ++i) {
140				(void)snprintf(buf, sizeof(buf), "%010d", i);
141				DB_BENCH_ASSERT(
142				    dbp->del(dbp, NULL, &key, 0) == 0);
143			}
144		else
145			for (i = 0, recno = 1; i < count; ++i, ++recno)
146				DB_BENCH_ASSERT(
147				    dbp->del(dbp, NULL, &key, 0) == 0);
148
149	TIMER_STOP;
150
151	printf(
152    "# %d %s database in-order delete of 10/20 byte key/data pairs using %s\n",
153	    count, ts, use_cursor ? "a cursor" : "the key");
154	TIMER_DISPLAY(count);
155
156	DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
157
158	return (0);
159}
160
161static int
162b_del_usage()
163{
164	(void)fprintf(stderr,
165	    "usage: b_del [-w] [-C cachesz] [-c count] [-t type]\n");
166	return (EXIT_FAILURE);
167}
168