Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / build_vxworks / test / micro / b_put.c
1 /*
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2005, 2012 Oracle and/or its affiliates.  All rights reserved.
5  *
6  * $Id$
7  */
8 #include "bench.h"
9
10 static int b_put_usage(void);
11 static int b_put_secondary(DB *, const DBT *, const DBT *, DBT *);
12
13 int
14 b_put(int argc, char *argv[])
15 {
16         extern char *optarg;
17         extern int optind, __db_getopt_reset;
18         DB_ENV *dbenv;
19         DB *dbp, **second;
20         DBTYPE type;
21         DBT key, data;
22 #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
23         DB_HEAP_RID rid;
24 #endif
25         db_recno_t recno;
26         u_int32_t cachesize, dsize;
27         int ch, i, count, secondaries;
28         char *ts, buf[64];
29
30         second = NULL;
31         type = DB_BTREE;
32         cachesize = MEGABYTE;
33         dsize = 20;
34         count = 100000;
35         secondaries = 0;
36         ts = "Btree";
37         __db_getopt_reset = 1;
38         while ((ch = getopt(argc, argv, "C:c:d:s:t:")) != 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                         dsize = (u_int32_t)atoi(optarg);
48                         break;
49                 case 's':
50                         secondaries = atoi(optarg);
51                         break;
52                 case 't':
53                         switch (optarg[0]) {
54                         case 'B': case 'b':
55                                 ts = "Btree";
56                                 type = DB_BTREE;
57                                 break;
58                         case 'H': case 'h':
59                                 if (optarg[1] == 'E' || optarg[1] == 'e') {
60 #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
61                                         if (b_util_have_heap())
62                                                 return (0);
63                                         ts = "Heap";
64                                         type = DB_HEAP;
65 #else
66                                         fprintf(stderr,
67                                 "b_curwalk: Heap is not supported! \n");
68                                         return (EXIT_SUCCESS);
69 #endif
70                                 } else {
71                                         if (b_util_have_hash())
72                                                 return (0);
73                                         ts = "Hash";
74                                         type = DB_HASH;
75                                 }
76                                 break;
77                         case 'Q': case 'q':
78                                 if (b_util_have_queue())
79                                         return (0);
80                                 ts = "Queue";
81                                 type = DB_QUEUE;
82                                 break;
83                         case 'R': case 'r':
84                                 ts = "Recno";
85                                 type = DB_RECNO;
86                                 break;
87                         default:
88                                 return (b_put_usage());
89                         }
90                         break;
91                 case '?':
92                 default:
93                         return (b_put_usage());
94                 }
95         argc -= optind;
96         argv += optind;
97         if (argc != 0)
98                 return (b_put_usage());
99
100 #if DB_VERSION_MAJOR < 3 || DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
101         /*
102          * Secondaries were added after DB 3.2.9.
103          */
104         if (secondaries)
105                 return (0);
106 #endif
107
108         /* Create the environment. */
109         DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
110         dbenv->set_errfile(dbenv, stderr);
111         DB_BENCH_ASSERT(dbenv->set_cachesize(dbenv, 0, cachesize, 0) == 0);
112 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 1
113         DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR,
114             NULL, DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE, 0666) == 0);
115 #else
116         DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR,
117             DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE, 0666) == 0);
118 #endif
119
120         /*
121          * Create the database.
122          * Optionally set the record length for Queue.
123          */
124         DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0);
125         if (type == DB_QUEUE)
126                 DB_BENCH_ASSERT(dbp->set_re_len(dbp, dsize) == 0);
127 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
128         DB_BENCH_ASSERT(
129             dbp->open(dbp, NULL, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
130 #else
131         DB_BENCH_ASSERT(
132             dbp->open(dbp, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
133 #endif
134
135         /* Optionally create the secondaries. */
136         if (secondaries != 0) {
137                 DB_BENCH_ASSERT((second =
138                     calloc(sizeof(DB *), (size_t)secondaries)) != NULL);
139                 for (i = 0; i < secondaries; ++i) {
140                         DB_BENCH_ASSERT(db_create(&second[i], dbenv, 0) == 0);
141                         (void)snprintf(buf, sizeof(buf), "%d.db", i);
142 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
143                         DB_BENCH_ASSERT(second[i]->open(second[i], NULL,
144                             buf, NULL, DB_BTREE, DB_CREATE, 0600) == 0);
145 #else
146                         DB_BENCH_ASSERT(second[i]->open(second[i],
147                             buf, NULL, DB_BTREE, DB_CREATE, 0600) == 0);
148 #endif
149 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
150                         /*
151                          * The DB_TXN argument to Db.associate was added in
152                          * 4.1.25.
153                          */
154                         DB_BENCH_ASSERT(dbp->associate(
155                             dbp, NULL, second[i], b_put_secondary, 0) == 0);
156 #else
157                         DB_BENCH_ASSERT(dbp->associate(
158                             dbp, second[i], b_put_secondary, 0) == 0);
159 #endif
160                 }
161         }
162
163         /* Store a key/data pair. */
164         memset(&key, 0, sizeof(key));
165         memset(&data, 0, sizeof(data));
166         switch (type) {
167         case DB_BTREE:
168         case DB_HASH:
169                 key.data = "01234567890123456789";
170                 key.size = 20;
171                 break;
172 #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
173         case DB_HEAP:
174                 key.data = &rid;
175                 key.size = sizeof(rid);
176                 break;
177 #endif
178         case DB_QUEUE:
179         case DB_RECNO:
180                 recno = 1;
181                 key.data = &recno;
182                 key.size = sizeof(recno);
183                 break;
184         case DB_UNKNOWN:
185                 b_util_abort();
186                 break;
187         }
188
189         data.size = dsize;
190         DB_BENCH_ASSERT(
191             (data.data = malloc((size_t)dsize)) != NULL);
192
193         /* Store the key/data pair count times. */
194         TIMER_START;
195         for (i = 0; i < count; ++i) {
196                 /* Change data value so the secondaries are updated. */
197                 (void)snprintf(data.data, data.size, "%10lu", (u_long)i);
198 #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
199                 DB_BENCH_ASSERT(dbp->put(dbp,
200                     NULL, &key, &data, type == DB_HEAP ? DB_APPEND : 0) == 0);
201 #else
202                 DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0);
203 #endif
204         }
205         TIMER_STOP;
206
207         if (type == DB_BTREE || type == DB_HASH)
208                 printf(
209                     "# %d %s database put of 10 byte key, %lu byte data",
210                     count, ts, (u_long)dsize);
211         else
212                 printf("# %d %s database put of key, %lu byte data",
213                     count, ts, (u_long)dsize);
214         if (secondaries)
215                 printf(" with %d secondaries", secondaries);
216         printf("\n");
217         TIMER_DISPLAY(count);
218
219         if (second != NULL) {
220                 for (i = 0; i < secondaries; ++i)
221                         DB_BENCH_ASSERT(second[i]->close(second[i], 0) == 0);
222                 free(second);
223         }
224
225         DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
226         DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);
227
228         return (0);
229 }
230
231 static int
232 b_put_secondary(dbp, pkey, pdata, skey)
233         DB *dbp;
234         const DBT *pkey, *pdata;
235         DBT *skey;
236 {
237         skey->data = pdata->data;
238         skey->size = pdata->size;
239
240         COMPQUIET(dbp, NULL);
241         COMPQUIET(pkey, NULL);
242         return (0);
243 }
244
245 static int
246 b_put_usage()
247 {
248         (void)fprintf(stderr, "usage: b_put %s\n",
249             "[-C cachesz] [-c count] [-d bytes] [-s secondaries] [-t type]");
250         return (EXIT_FAILURE);
251 }