1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <sys/times.h>
30 #include <yaz/xmalloc.h>
31 #include <idzebra/isamb.h>
34 static int log_level = 0;
36 static void log_item(int level, const void *b, const char *txt)
39 memcpy(&x, b, sizeof(int));
40 yaz_log(log_level, "%s %d", txt, x);
43 static void log_pr(const char *txt)
45 yaz_log(log_level, "%s", txt);
48 int compare_item(const void *a, const void *b)
52 memcpy(&ia, a, sizeof(int));
53 memcpy(&ib, b, sizeof(int));
61 void *code_start(void)
66 void code_item(void *p, char **dst, const char **src)
68 memcpy (*dst, *src, sizeof(int));
69 (*dst) += sizeof(int);
70 (*src) += sizeof(int);
73 void code_reset(void *p)
76 void code_stop(void *p)
89 int code_read(void *vp, char **dst, int *insertMode)
91 struct read_info *ri = (struct read_info *)vp;
94 if (ri->no >= ri->max)
99 memcpy (*dst, &x, sizeof(int));
102 ri->val = ri->val + ri->step;
103 *insertMode = ri->insertMode;
106 yaz_log(log_level, "%d %5d", ri->insertMode, x);
111 void tst_insert(ISAMB isb, int n)
120 /* insert a number of entries */
128 isamc_i.clientData = &ri;
129 isamc_i.read_item = code_read;
131 isamc_p = 0; /* new list */
132 isamb_merge (isb, &isamc_p , &isamc_i);
134 /* read the entries */
135 pp = isamb_pp_open (isb, isamc_p, 1);
138 while(isamb_pp_read (pp, key_buf))
141 memcpy (&x, key_buf, sizeof(int));
144 yaz_log(YLOG_WARN, "isamb_pp_read. n=%d Got %d (expected %d)",
149 yaz_log(log_level, "isamb_pp_read. n=%d Got %d",
154 if (ri.val != ri.max)
156 yaz_log(YLOG_WARN, "ri.max != ri.max (%d != %d)", ri.val, ri.max);
159 isamb_dump(isb, isamc_p, log_pr);
164 /* delete a number of entries (even ones) */
172 isamc_i.clientData = &ri;
173 isamc_i.read_item = code_read;
175 isamb_merge (isb, &isamc_p , &isamc_i);
177 /* delete a number of entries (odd ones) */
185 isamc_i.clientData = &ri;
186 isamc_i.read_item = code_read;
188 isamb_merge (isb, &isamc_p, &isamc_i);
192 yaz_log(YLOG_WARN, "isamb_merge did not return empty list n=%d",
198 void tst_forward(ISAMB isb, int n)
206 /* insert a number of entries */
214 isamc_i.clientData = &ri;
215 isamc_i.read_item = code_read;
218 isamb_merge (isb, &isamc_p, &isamc_i);
220 /* read the entries */
221 pp = isamb_pp_open (isb, isamc_p, 1);
223 for (i = 0; i<ri.max; i +=2 )
227 isamb_pp_forward(pp, &x, &xu);
228 if (x != xu && xu != x+1)
230 yaz_log(YLOG_WARN, "isamb_pp_forward (1). Got %d (expected %d)",
238 pp = isamb_pp_open (isb, isamc_p, 1);
239 for (i = 0; i<ri.max; i += 100)
243 isamb_pp_forward(pp, &x, &xu);
244 if (x != xu && xu != x+1)
246 yaz_log(YLOG_WARN, "isamb_pp_forward (2). Got %d (expected %d)",
254 isamb_unlink(isb, isamc_p);
257 void tst_x(ISAMB isb)
263 isamc_i.clientData = &ri;
264 isamc_i.read_item = code_read;
272 isamb_merge (isb, &isamb_p , &isamc_i);
281 isamb_merge (isb, &isamb_p , &isamc_i);
284 void tst_append(ISAMB isb, int n)
292 for (i = 0; i < n; i += chunk)
294 /* insert a number of entries */
302 isamc_i.clientData = &ri;
303 isamc_i.read_item = code_read;
305 isamb_merge (isb, &isamb_p , &isamc_i);
310 struct random_read_info {
317 int tst_random_read(void *vp, char **dst, int *insertMode)
319 struct random_read_info *ri = (struct random_read_info *)vp;
322 while(ri->idx < ri->max && ri->delta[ri->idx] == ri->level)
327 if (ri->idx >= ri->max)
330 if (ri->delta[ri->idx] > 0)
341 memcpy (*dst, &x, sizeof(int));
344 yaz_log(YLOG_DEBUG, "%d %5d", *insertMode, x);
348 void tst_random(ISAMB isb, int n, int rounds, int max_dups)
352 int *freq = malloc(sizeof(int) * n);
353 int *delta = malloc(sizeof(int) * n);
355 for (i = 0; i<n; i++)
358 for (j = 0; j<rounds; j++)
360 yaz_log(YLOG_DEBUG, "round %d", j);
361 for (i = 0; i<n; i++)
364 delta[i] = (rand() % (1+max_dups)) - freq[i];
371 struct random_read_info ri;
378 isamc_i.clientData = &ri;
379 isamc_i.read_item = tst_random_read;
381 isamb_merge (isb, &isamb_p , &isamc_i);
384 yaz_log(YLOG_DEBUG, "dump %d", j);
385 isamb_dump(isb, isamb_p, log_pr);
387 yaz_log(YLOG_DEBUG, "----------------------------");
388 for (i = 0; i<n; i++)
393 for (i = 0; i<n; i++)
396 yaz_log(YLOG_WARN, "isamb_merge returned 0, but "
397 "freq is non-empty");
406 ISAMB_PP pp = isamb_pp_open (isb, isamb_p, 1);
408 yaz_log(YLOG_DEBUG, "test %d", j);
410 while(isamb_pp_read (pp, key_buf))
413 memcpy (&x, key_buf, sizeof(int));
414 yaz_log(YLOG_DEBUG, "Got %d", x);
415 while (idx < n && freq[idx] == level)
422 yaz_log(YLOG_WARN, "tst_random: Extra item: %d", x);
427 yaz_log(YLOG_WARN, "tst_random: Mismatch %d != %d",
433 while (idx < n && freq[idx] == level)
440 yaz_log(YLOG_WARN, "tst_random: Missing item: %d", idx);
450 /* \fn void tst_minsert(ISAMB isb, int n)
451 \brief insert inserts n identical keys, removes n/2, then n-n/2 ..
452 \param isb ISAMB handle
453 \param n number of keys
455 void tst_minsert(ISAMB isb, int n)
461 isamc_i.clientData = &ri;
463 /* all have same value = 1 */
467 isamc_i.read_item = code_read;
474 isamb_merge (isb, &isamb_p , &isamc_i);
476 isamb_dump(isb, isamb_p, log_pr);
483 isamb_merge (isb, &isamb_p , &isamc_i);
490 isamb_merge (isb, &isamb_p , &isamc_i);
493 yaz_log(YLOG_WARN, "tst_minsert: isamb_merge should be empty n=%d",
499 /* tests for identical keys.. ISAMB does not handle that, so some of the
502 static void identical_keys_tests(ISAMB isb)
505 tst_minsert(isb, 10);
508 tst_minsert(isb, 600); /* still fails */
511 tst_random(isb, 20, 200, 1);
514 tst_random(isb, 5, 200, 2);
518 tst_random(isb, 250, 10, 4);
521 /* fails if both are executed */
522 tst_random(isb, 20000, 10, 4);
523 tst_random(isb, 20000, 10, 10);
526 tst_random(isb, 250, 100, 10);
530 int main(int argc, char **argv)
537 yaz_log_init_level(YLOG_ALL);
539 /* setup method (attributes) */
540 method.compare_item = compare_item;
541 method.log_item = log_item;
542 method.codec.start = code_start;
543 method.codec.encode = code_item;
544 method.codec.decode = code_item;
545 method.codec.reset = code_reset;
546 method.codec.stop = code_stop;
548 /* create block system */
549 bfs = bfs_create(0, 0);
552 yaz_log(YLOG_WARN, "bfs_create failed");
558 /* create isam handle */
559 isb = isamb_open (bfs, "isamb", 1, &method, 0);
562 yaz_log(YLOG_WARN, "isamb_open failed");
568 tst_insert(isb, 100);
569 tst_insert(isb, 500);
570 tst_insert(isb, 10000);
572 tst_forward(isb, 10000);
576 tst_append(isb, 1000);
579 identical_keys_tests(isb);
583 /* exit block system */
591 * c-file-style: "Stroustrup"
592 * indent-tabs-mode: nil
594 * vim: shiftwidth=4 tabstop=8 expandtab