Debian Codegraph @

build_vxworks/test/micro/b_del.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_del_usage(void);
11
12 int
13 b_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 #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;
26 int ch, i, count, ret, use_cursor;
27 char *ts, buf[32];
28
29 type = DB_BTREE;
30 cachesize = MEGABYTE;
31 count = 100000;
32 use_cursor = 0;
33 ts = "Btree";
34 __db_getopt_reset = 1;
35 while ((ch = getopt(argc, argv, "C:c:t:w")) != EOF)
36 switch (ch) {
37 case 'C':
38 cachesize = (u_int32_t)atoi(optarg);
39 break;
40 case 'c':
41 count = atoi(optarg);
42 break;
43 case 't':
44 switch (optarg[0]) {
45 case 'B': case 'b':
46 ts = "Btree";
47 type = DB_BTREE;
48 break;
49 case 'H': case 'h':
50 if (optarg[1] == 'E' || optarg[1] == 'e') {
51 #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
52 if (b_util_have_heap())
53 return (0);
54 ts = "Heap";
55 type = DB_HEAP;
56 #else
57 fprintf(stderr,
58 "b_curwalk: Heap is not supported! \n");
59 return (EXIT_SUCCESS);
60 #endif
61 } else {
62 if (b_util_have_hash())
63 return (0);
64 ts = "Hash";
65 type = DB_HASH;
66 }
67 break;
68 case 'Q': case 'q':
69 if (b_util_have_queue())
70 return (0);
71 ts = "Queue";
72 type = DB_QUEUE;
73 break;
74 case 'R': case 'r':
75 ts = "Recno";
76 type = DB_RECNO;
77 break;
78 default:
79 return (b_del_usage());
80 }
81 break;
82 case 'w':
83 use_cursor = 1;
84 break;
85 case '?':
86 default:
87 return (b_del_usage());
88 }
89 argc -= optind;
90 argv += optind;
91 if (argc != 0)
92 return (b_del_usage());
93
94 /* Create the database. */
95 DB_BENCH_ASSERT(db_create(&dbp, NULL, 0) == 0);
96 DB_BENCH_ASSERT(dbp->set_cachesize(dbp, 0, cachesize, 0) == 0);
97 dbp->set_errfile(dbp, stderr);
98
99 #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
100 /* Need a cursor if using Heap. */
101 if (type == DB_HEAP && !use_cursor) {
102 printf("Heap databases require the -w flag.\n");
103 return (-1);
104 }
105 #endif
106
107 /* Set record length for Queue. */
108 if (type == DB_QUEUE)
109 DB_BENCH_ASSERT(dbp->set_re_len(dbp, 20) == 0);
110
111 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
112 DB_BENCH_ASSERT(
113 dbp->open(dbp, NULL, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
114 #else
115 DB_BENCH_ASSERT(
116 dbp->open(dbp, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
117 #endif
118
119 /* Initialize the data. */
120 memset(&key, 0, sizeof(key));
121 memset(&data, 0, sizeof(data));
122 data.data = "01234567890123456789";
123 data.size = 20;
124
125 /* Store a key/data pair. */
126 switch (type) {
127 case DB_BTREE:
128 case DB_HASH:
129 key.data = buf;
130 key.size = 10;
131 break;
132 #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
133 case DB_HEAP:
134 key.data = &rid;
135 key.size = sizeof(rid);
136 break;
137 #endif
138 case DB_QUEUE:
139 case DB_RECNO:
140 key.data = &recno;
141 key.size = sizeof(recno);
142 break;
143 case DB_UNKNOWN:
144 b_util_abort();
145 break;
146 }
147
148 /* Insert count in-order key/data pairs. */
149 if (type == DB_BTREE || type == DB_HASH)
150 for (i = 0; i < count; ++i) {
151 (void)snprintf(buf, sizeof(buf), "%010d", i);
152 DB_BENCH_ASSERT(
153 dbp->put(dbp, NULL, &key, &data, 0) == 0);
154 }
155 #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
156 else if (type == DB_HEAP)
157 for (i = 0; i < count; i++)
158 DB_BENCH_ASSERT(
159 dbp->put(dbp, NULL, &key, &data, DB_APPEND) == 0);
160 #endif
161 else
162 for (i = 0, recno = 1; i < count; ++i, ++recno)
163 DB_BENCH_ASSERT(
164 dbp->put(dbp, NULL, &key, &data, 0) == 0);
165
166 /* Delete the records. */
167 TIMER_START;
168 if (use_cursor) {
169 DB_BENCH_ASSERT(dbp->cursor(dbp, NULL, &dbc, 0) == 0);
170 while ((ret = dbc->c_get(dbc, &key, &data, DB_NEXT)) == 0)
171 DB_BENCH_ASSERT(dbc->c_del(dbc, 0) == 0);
172 DB_BENCH_ASSERT (ret == DB_NOTFOUND);
173 } else
174 if (type == DB_BTREE || type == DB_HASH)
175 for (i = 0; i < count; ++i) {
176 (void)snprintf(buf, sizeof(buf), "%010d", i);
177 DB_BENCH_ASSERT(
178 dbp->del(dbp, NULL, &key, 0) == 0);
179 }
180 else
181 for (i = 0, recno = 1; i < count; ++i, ++recno)
182 DB_BENCH_ASSERT(
183 dbp->del(dbp, NULL, &key, 0) == 0);
184
185 TIMER_STOP;
186
187 printf(
188 "# %d %s database in-order delete of 10/20 byte key/data pairs using %s\n",
189 count, ts, use_cursor ? "a cursor" : "the key");
190 TIMER_DISPLAY(count);
191
192 DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
193
194 return (0);
195 }
196
197 static int
198 b_del_usage()
199 {
200 (void)fprintf(stderr,
201 "usage: b_del [-w] [-C cachesz] [-c count] [-t type]\n");
202 return (EXIT_FAILURE);
203 }