-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tndb.c
397 lines (307 loc) · 8.76 KB
/
test_tndb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* $Id$ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <trurl/nmalloc.h>
#include "tndb.h"
#define NAME "/tmp/tndb"
#define NAMEZ "/tmp/tndb.gz"
void *timethis_begin(void)
{
struct timeval *tv;
tv = n_malloc(sizeof(*tv));
gettimeofday(tv, NULL);
return tv;
}
void timethis_end(void *tvp, const char *prefix)
{
struct timeval tv, *tv0 = (struct timeval *)tvp;
gettimeofday(&tv, NULL);
tv.tv_sec -= tv0->tv_sec;
tv.tv_usec -= tv0->tv_usec;
if (tv.tv_usec < 0) {
tv.tv_sec--;
tv.tv_usec = 1000000 + tv.tv_usec;
}
printf("time [%s] %ld.%ld\n", prefix, tv.tv_sec, tv.tv_usec);
free(tvp);
}
int test_creat(const char *name, int items)
{
int i;
struct tndb *db;
printf("\n\nCreating %s with %d records...", name, items);
fflush(stdout);
if ((db = tndb_creat(name, -1, TNDB_SIGN_DIGEST)) == NULL) {
perror("tndb_creat");
return -1;
}
for (i = 0; i < items; i++) {
char key[40], val[1024 * 32], *fmt = "val%%.%dd", valfmt[256];
int kn, vn;
kn = snprintf(key, sizeof(key), "key%.8d", i);
snprintf(valfmt, sizeof(valfmt), fmt, i);
vn = snprintf(val, sizeof(val), valfmt, i);
if (!tndb_put(db, key, kn, val, vn)) {
perror("tndb_add");
return -1;
}
if (i % (items / 5) == 0) {
printf("%d..", i);
fflush(stdout);
}
}
printf("%d\n", i);
if (!tndb_close(db))
perror("tndb_close");
return 0;
}
int test_creat_bigdata(const char *name, int items)
{
int i;
struct tndb *db;
void *tt;
printf("\n\nCreating %s with %d records...", name, items);
fflush(stdout);
if ((db = tndb_creat(name, -1, TNDB_SIGN_DIGEST)) == NULL) {
perror("tndb_creat");
return -1;
}
tt = timethis_begin();
for (i = 0; i < items; i++) {
char key[40], val[10 * 1024],
*fmt = "val%%.%dd", valfmt[256];
int kn;
kn = snprintf(key, sizeof(key), "key%.8d", i);
memset(val, 0, sizeof(val));
snprintf(valfmt, sizeof(valfmt), fmt, i);
snprintf(val, sizeof(val), valfmt, i);
if (!tndb_put(db, key, kn, val, sizeof(val))) {
perror("tndb_add");
return -1;
}
if (items > 5)
if (i % (items / 5) == 0) {
printf("%d..", i);
fflush(stdout);
}
}
printf("%d\n", i);
timethis_end(tt, "write-data");
tt = timethis_begin();
if (!tndb_close(db))
perror("tndb_close");
timethis_end(tt, "tndb_close");
return 0;
}
int test_lookup(const char *name, int items)
{
int i;
uint32_t vlen;
off_t voffs;
struct tndb *db;
if ((db = tndb_open(name)) == NULL) {
perror("Can't open the database");
return -1;
}
printf("Lookup %s...", name);
fflush(stdout);
for (i = 0; i < items + (items/2); i++) {
//for (i = items + (items/2); i > -1; i--) {
char key[40], val[1024 * 32], *fmt = "val%%.%dd", valfmt[256];
int kn, rc;
if (i % (items / 5) == 0) {
printf("%d..", i);
fflush(stdout);
}
kn = snprintf(key, sizeof(key), "key%.8d", i);
snprintf(valfmt, sizeof(valfmt), fmt, i);
snprintf(val, sizeof(val), valfmt, i);
if ((rc = tndb_get_voff(db, key, kn, &voffs, &vlen)) < 0) {
printf("Error while reading key %s (%d): %m\n", key, rc);
return -1;
} else if (rc == 0) {
if (i < items) {
printf("Key %s not found (%d)\n", key, rc);
return -1;
}
} else {
char buf[1024 * 32];
//printf("found %s %d %d -> ", str, retpos, retlen);
if (i >= items) {
printf("Ghost record '%s' detected!\n", key);
return -1;
}
if (tndb_read(db, voffs, buf, vlen) != (int)vlen) {
perror("Error while reading data\n");
return -1;
}
buf[vlen] = '\0';
//printf(" %s\n", buf);
if (strcmp(val, buf) != 0) {
printf("Wrong data %s %s!\n", val, buf);
return -1;
}
}
}
printf("%d\n", i);
return 0;
}
int test_walk(const char *name, int items, int with_keys)
{
uint32_t vlen;
off_t voffs;
struct tndb *db;
struct tndb_it it;
char key[TNDB_KEY_MAX + 1], val[1024 * 32], *fmt = "val%%.%dd", valfmt[256];
char *keyp = NULL;
int rc, nrec;
unsigned int kn;
if (with_keys)
keyp = key;
if ((db = tndb_open(name)) == NULL) {
perror("Can't open the database");
return -1;
}
if (!tndb_verify(db)) {
printf("%s: BROKEN", name);
return -1;
}
if (!tndb_it_start(db, &it)) {
perror("tndb_it_start");
return -1;
}
printf("Scanning %s...", name);
fflush(stdout);
nrec = 0;
while ((rc = tndb_it_get_voff(&it, keyp, &kn, &voffs, &vlen))) {
char buf[1024 * 32];
int i;
if (items > 5)
if (nrec % (items / 5) == 0) {
printf("%d..", nrec);
fflush(stdout);
}
nrec++;
if (rc < 0) {
perror("tndb_it_get");
return -1;
}
if (keyp == NULL)
continue;
if (kn < 3) {
printf("tndb_it_get key error (len %d): %m\n", kn);
return -1;
}
if (sscanf(key + 3, "%d", &i) != 1) {
printf("tndb_it_get key error '%s'\n", key);
return -1;
}
snprintf(valfmt, sizeof(valfmt), fmt, i);
snprintf(val, sizeof(val), valfmt, i);
if (tndb_read(db, voffs, buf, vlen) != (int)vlen) {
perror("Error while reading data\n");
return -1;
}
buf[vlen] = '\0';
if (strcmp(val, buf) != 0) {
printf("Wrong data %s %s!\n", val, buf);
return -1;
}
}
printf("%d\n", nrec);
return 0;
}
int test_filedb(const char *name)
{
FILE *stream;
char buf[1024];
int i = 0;
struct tndb *db;
stream = popen("rpm -qla", "r");
printf("\n\nCreating %s with rpm -qla...", name);
fflush(stdout);
if ((db = tndb_creat(name, -1, 0)) == NULL) {
perror("tndb_creat");
return -1;
}
while (fgets(buf, sizeof(buf), stream)) {
char key[40], val[1024 * 32];
int kn, vn;
kn = snprintf(key, sizeof(key), "%d", i);
vn = strlen(buf);
if (!tndb_put(db, key, kn, val, vn)) {
perror("tndb_add");
return -1;
}
i++;
if (i % 1000 == 0) {
printf("%d..", i);
fflush(stdout);
}
}
printf("%d\n", i);
if (!tndb_close(db))
perror("tndb_close");
return 0;
}
int main(void)
{
int i, n = 1000;
void *tt;
#if TEST_FILEDB
tt = timethis_begin();
test_filedb(NAMEZ);
timethis_end(tt, "creat");
tt = timethis_begin();
test_walk(NAMEZ, n, 0);
timethis_end(tt, "walk");
exit(0);
#endif
#if 0
n = 200;
tt = timethis_begin();
test_creat_bigdata(NAMEZ, n);
timethis_end(tt, "creat");
#endif
#if 0
tt = timethis_begin();
test_walk(NAMEZ, n, 0);
timethis_end(tt, "walk");
exit(0);
#endif
n = 200;
for (i=0; i<1 ; i++) {
n += (n * i);
if (i < 1) { /* too much space */
tt = timethis_begin();
test_creat(NAMEZ, n);
timethis_end(tt, "creat");
tt = timethis_begin();
test_lookup(NAMEZ, n);
timethis_end(tt, "lookup");
exit(0);
tt = timethis_begin();
test_walk(NAME, n, 1);
timethis_end(tt, "walk");
tt = timethis_begin();
test_walk(NAME, n, 0);
timethis_end(tt, "walk(no keys)");
}
exit(0);
tt = timethis_begin();
test_creat(NAMEZ, n);
timethis_end(tt, "creat");
tt = timethis_begin();
test_lookup(NAMEZ, n);
timethis_end(tt, "lookup");
tt = timethis_begin();
test_walk(NAMEZ, n, 1);
timethis_end(tt, "walk");
tt = timethis_begin();
test_walk(NAMEZ, n, 0);
timethis_end(tt, "walk(no keys)");
}
return 0;
}