Debian Codegraph @

build_vxworks/test/micro/b_curwalk.c

raw
1 /*
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2005, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 * $Id$
7 */
8 #include "bench.h"
9
10 static int b_curwalk_usage(void);
11
12 int
13 b_curwalk(int argc, char *argv[])
14 {
15 extern char *optarg;
16 extern int optind, __db_getopt_reset;
17 DB *dbp;
18 DBTYPE type;
19 DBC *dbc;
20 DBT key, data;
21 #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
22 DB_HEAP_RID rid;
23 #endif
24 db_recno_t recno;
25 u_int32_t cachesize, pagesize, walkflags;
26 int ch, i, count, dupcount, j;
27 int prev, ret, skipdupwalk, sorted, walkcount;
28 char *ts, dbuf[32], kbuf[32];
29
30 type = DB_BTREE;
31 cachesize = 10 * MEGABYTE;
32 pagesize = 16 * 1024;
33 count = 100000;
34 dupcount = prev = skipdupwalk = sorted = 0;
35 walkcount = 1000;
36 ts = "Btree";
37 __db_getopt_reset = 1;
38 while ((ch = getopt(argc, argv, "C:c:d:P:pSst:w:")) != EOF)
39 switch (ch) {
40 case 'C':
41 cachesize = (u_int32_t)atoi(optarg);
42 break;
43 case 'c':
44 count = atoi(optarg);
45 break;
46 case 'd':
47 dupcount = atoi(optarg);
48 break;
49 case 'P':
50 pagesize = (u_int32_t)atoi(optarg);
51 break;
52 case 'p':
53 prev = 1;
54 break;
55 case 'S':
56 skipdupwalk = 1;
57 break;
58 case 's':
59 sorted = 1;
60 break;
61 case 't':
62 switch (optarg[0]) {
63 case 'B': case 'b':
64 ts = "Btree";
65 type = DB_BTREE;
66 break;
67 case 'H': case 'h':
68 if (optarg[1] == 'E' || optarg[1] == 'e') {
69 #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
70 if (b_util_have_heap())
71 return (0);
72 ts = "Heap";
73 type = DB_HEAP;
74 #else
75 fprintf(stderr,
76 "b_curwalk: Heap is not supported! \n");
77 return (EXIT_SUCCESS);
78 #endif
79 } else {
80 if (b_util_have_hash())
81 return (0);
82 ts = "Hash";
83 type = DB_HASH;
84 }
85 break;
86 case 'Q': case 'q':
87 if (b_util_have_queue())
88 return (0);
89 ts = "Queue";
90 type = DB_QUEUE;
91 break;
92 case 'R': case 'r':
93 ts = "Recno";
94 type = DB_RECNO;
95 break;
96 default:
97 return (b_curwalk_usage());
98 }
99 break;
100 case 'w':
101 walkcount = atoi(optarg);
102 break;
103 case '?':
104 default:
105 return (b_curwalk_usage());
106 }
107 argc -= optind;
108 argv += optind;
109 if (argc != 0)
110 return (b_curwalk_usage());
111
112 /*
113 * Queue and Recno don't support duplicates.
114 */
115 #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
116 if (dupcount != 0 &&
117 (type == DB_QUEUE || type == DB_RECNO || type == DB_HEAP)) {
118 fprintf(stderr,
119 "b_curwalk: Queue, Recno and Heap don't support duplicates\n");
120 return (b_curwalk_usage());
121 }
122 #else
123 if (dupcount != 0 && (type == DB_QUEUE || type == DB_RECNO)) {
124 fprintf(stderr,
125 "b_curwalk: Queue and Recno don't support duplicates\n");
126 return (b_curwalk_usage());
127 }
128 #endif
129
130 #if DB_VERSION_MAJOR < 3 || DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0
131 #define DB_PREV_NODUP 0
132 /*
133 * DB_PREV_NODUP wasn't available until after 3.0.55.
134 *
135 * For some reason, testing sorted duplicates doesn't work either.
136 * I don't really care about 3.0.55 any more, just ignore it.
137 */
138 return (0);
139 #endif
140 /* Create the database. */
141 DB_BENCH_ASSERT(db_create(&dbp, NULL, 0) == 0);
142 DB_BENCH_ASSERT(dbp->set_cachesize(dbp, 0, cachesize, 0) == 0);
143 DB_BENCH_ASSERT(dbp->set_pagesize(dbp, pagesize) == 0);
144 dbp->set_errfile(dbp, stderr);
145
146 /* Set record length for Queue. */
147 if (type == DB_QUEUE)
148 DB_BENCH_ASSERT(dbp->set_re_len(dbp, 20) == 0);
149
150 /* Set duplicates flag. */
151 if (dupcount != 0)
152 DB_BENCH_ASSERT(
153 dbp->set_flags(dbp, sorted ? DB_DUPSORT : DB_DUP) == 0);
154
155 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
156 DB_BENCH_ASSERT(dbp->open(
157 dbp, NULL, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
158 #else
159 DB_BENCH_ASSERT(dbp->open(
160 dbp, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
161 #endif
162
163 /* Initialize the data. */
164 memset(&key, 0, sizeof(key));
165 memset(&data, 0, sizeof(data));
166
167 /* Insert count in-order key/data pairs. */
168 data.data = dbuf;
169 data.size = 20;
170 if (type == DB_BTREE || type == DB_HASH) {
171 key.size = 10;
172 key.data = kbuf;
173 for (i = 0; i < count; ++i) {
174 (void)snprintf(kbuf, sizeof(kbuf), "%010d", i);
175 for (j = 0; j <= dupcount; ++j) {
176 (void)snprintf(dbuf, sizeof(dbuf), "%020d", j);
177 DB_BENCH_ASSERT(
178 dbp->put(dbp, NULL, &key, &data, 0) == 0);
179 }
180 }
181 #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
182 } else if (type == DB_HEAP) {
183 key.data = &rid;
184 key.size = sizeof(rid);
185 for (i = 0; i < count; ++i)
186 DB_BENCH_ASSERT(
187 dbp->put(dbp, NULL, &key, &data, DB_APPEND) == 0);
188 #endif
189 } else {
190 key.data = &recno;
191 key.size = sizeof(recno);
192 for (i = 0, recno = 1; i < count; ++i, ++recno)
193 DB_BENCH_ASSERT(
194 dbp->put(dbp, NULL, &key, &data, 0) == 0);
195 }
196
197 walkflags = prev ?
198 (skipdupwalk ? DB_PREV_NODUP : DB_PREV) :
199 (skipdupwalk ? DB_NEXT_NODUP : DB_NEXT);
200
201 /* Walk the cursor through the tree N times. */
202 TIMER_START;
203 for (i = 0; i < walkcount; ++i) {
204 DB_BENCH_ASSERT(dbp->cursor(dbp, NULL, &dbc, 0) == 0);
205 while ((ret = dbc->c_get(dbc, &key, &data, walkflags)) == 0)
206 ;
207 DB_BENCH_ASSERT(ret == DB_NOTFOUND);
208 DB_BENCH_ASSERT(dbc->c_close(dbc) == 0);
209 }
210 TIMER_STOP;
211
212 printf("# %d %s %s cursor of %d 10/20 byte key/data items",
213 walkcount, ts, prev ?
214 (skipdupwalk ? "DB_PREV_NODUP" : "DB_PREV") :
215 (skipdupwalk ? "DB_NEXT_NODUP" : "DB_NEXT"),
216 count);
217 if (dupcount != 0)
218 printf(" with %d dups", dupcount);
219 printf("\n");
220
221 /*
222 * An "operation" is traversal of a single key/data pair -- not a
223 * return of the key/data pair, since some versions of this test
224 * skip duplicate key/data pairs.
225 *
226 * Use a "double" so we don't overflow.
227 */
228 TIMER_DISPLAY((double)count * walkcount);
229
230 DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
231
232 return (EXIT_SUCCESS);
233 }
234
235 static int
236 b_curwalk_usage()
237 {
238 (void)fprintf(stderr, "%s\n\t%s\n",
239 "usage: b_curwalk [-pSs] [-C cachesz]",
240 "[-c cnt] [-d dupcnt] [-P pagesz] [-t type] [-w walkcnt]");
241 return (EXIT_FAILURE);
242 }