1 /* This file is part of the Zebra server.
2 Copyright (C) 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
24 #include <sys/times.h>
33 #include <yaz/xmalloc.h>
34 #include <idzebra/isamb.h>
37 static int log_level = 0;
39 static void log_item(int level, const void *b, const char *txt)
42 memcpy(&x, b, sizeof(int));
43 yaz_log(log_level, "%s %d", txt, x);
46 static void log_pr(const char *txt)
48 yaz_log(log_level, "%s", txt);
51 int compare_item(const void *a, const void *b)
55 memcpy(&ia, a, sizeof(int));
56 memcpy(&ib, b, sizeof(int));
64 void *code_start(void)
69 void code_item(void *p, char **dst, const char **src)
71 memcpy (*dst, *src, sizeof(int));
72 (*dst) += sizeof(int);
73 (*src) += sizeof(int);
76 void code_reset(void *p)
79 void code_stop(void *p)
92 int code_read(void *vp, char **dst, int *insertMode)
94 struct read_info *ri = (struct read_info *)vp;
97 if (ri->no >= ri->max)
102 memcpy (*dst, &x, sizeof(int));
105 ri->val = ri->val + ri->step;
106 *insertMode = ri->insertMode;
109 yaz_log(log_level, "%d %5d", ri->insertMode, x);
114 void tst_insert(ISAMB isb, int n)
123 /* insert a number of entries */
131 isamc_i.clientData = &ri;
132 isamc_i.read_item = code_read;
134 isamc_p = 0; /* new list */
135 isamb_merge (isb, &isamc_p , &isamc_i);
137 /* read the entries */
138 pp = isamb_pp_open (isb, isamc_p, 1);
141 while(isamb_pp_read (pp, key_buf))
144 memcpy (&x, key_buf, sizeof(int));
147 yaz_log(YLOG_WARN, "isamb_pp_read. n=%d Got %d (expected %d)",
152 yaz_log(log_level, "isamb_pp_read. n=%d Got %d",
157 if (ri.val != ri.max)
159 yaz_log(YLOG_WARN, "ri.max != ri.max (%d != %d)", ri.val, ri.max);
162 isamb_dump(isb, isamc_p, log_pr);
167 /* delete a number of entries (even ones) */
175 isamc_i.clientData = &ri;
176 isamc_i.read_item = code_read;
178 isamb_merge (isb, &isamc_p , &isamc_i);
180 /* delete a number of entries (odd ones) */
188 isamc_i.clientData = &ri;
189 isamc_i.read_item = code_read;
191 isamb_merge (isb, &isamc_p, &isamc_i);
195 yaz_log(YLOG_WARN, "isamb_merge did not return empty list n=%d",
201 void tst_forward(ISAMB isb, int n)
209 /* insert a number of entries */
217 isamc_i.clientData = &ri;
218 isamc_i.read_item = code_read;
221 isamb_merge (isb, &isamc_p, &isamc_i);
223 /* read the entries */
224 pp = isamb_pp_open (isb, isamc_p, 1);
226 for (i = 0; i<ri.max; i +=2 )
230 isamb_pp_forward(pp, &x, &xu);
231 if (x != xu && xu != x+1)
233 yaz_log(YLOG_WARN, "isamb_pp_forward (1). Got %d (expected %d)",
241 pp = isamb_pp_open (isb, isamc_p, 1);
242 for (i = 0; i<ri.max; i += 100)
246 isamb_pp_forward(pp, &x, &xu);
247 if (x != xu && xu != x+1)
249 yaz_log(YLOG_WARN, "isamb_pp_forward (2). Got %d (expected %d)",
257 isamb_unlink(isb, isamc_p);
260 void tst_x(ISAMB isb)
266 isamc_i.clientData = &ri;
267 isamc_i.read_item = code_read;
275 isamb_merge (isb, &isamb_p , &isamc_i);
284 isamb_merge (isb, &isamb_p , &isamc_i);
287 void tst_append(ISAMB isb, int n)
295 for (i = 0; i < n; i += chunk)
297 /* insert a number of entries */
305 isamc_i.clientData = &ri;
306 isamc_i.read_item = code_read;
308 isamb_merge (isb, &isamb_p , &isamc_i);
313 struct random_read_info {
320 int tst_random_read(void *vp, char **dst, int *insertMode)
322 struct random_read_info *ri = (struct random_read_info *)vp;
325 while(ri->idx < ri->max && ri->delta[ri->idx] == ri->level)
330 if (ri->idx >= ri->max)
333 if (ri->delta[ri->idx] > 0)
344 memcpy (*dst, &x, sizeof(int));
347 yaz_log(YLOG_DEBUG, "%d %5d", *insertMode, x);
351 void tst_random(ISAMB isb, int n, int rounds, int max_dups)
355 int *freq = malloc(sizeof(int) * n);
356 int *delta = malloc(sizeof(int) * n);
358 for (i = 0; i<n; i++)
361 for (j = 0; j<rounds; j++)
363 yaz_log(YLOG_DEBUG, "round %d", j);
364 for (i = 0; i<n; i++)
367 delta[i] = (rand() % (1+max_dups)) - freq[i];
374 struct random_read_info ri;
381 isamc_i.clientData = &ri;
382 isamc_i.read_item = tst_random_read;
384 isamb_merge (isb, &isamb_p , &isamc_i);
387 yaz_log(YLOG_DEBUG, "dump %d", j);
388 isamb_dump(isb, isamb_p, log_pr);
390 yaz_log(YLOG_DEBUG, "----------------------------");
391 for (i = 0; i<n; i++)
396 for (i = 0; i<n; i++)
399 yaz_log(YLOG_WARN, "isamb_merge returned 0, but "
400 "freq is non-empty");
409 ISAMB_PP pp = isamb_pp_open (isb, isamb_p, 1);
411 yaz_log(YLOG_DEBUG, "test %d", j);
413 while(isamb_pp_read (pp, key_buf))
416 memcpy (&x, key_buf, sizeof(int));
417 yaz_log(YLOG_DEBUG, "Got %d", x);
418 while (idx < n && freq[idx] == level)
425 yaz_log(YLOG_WARN, "tst_random: Extra item: %d", x);
430 yaz_log(YLOG_WARN, "tst_random: Mismatch %d != %d",
436 while (idx < n && freq[idx] == level)
443 yaz_log(YLOG_WARN, "tst_random: Missing item: %d", idx);
453 /* \fn void tst_minsert(ISAMB isb, int n)
454 \brief insert inserts n identical keys, removes n/2, then n-n/2 ..
455 \param isb ISAMB handle
456 \param n number of keys
458 void tst_minsert(ISAMB isb, int n)
464 isamc_i.clientData = &ri;
466 /* all have same value = 1 */
470 isamc_i.read_item = code_read;
477 isamb_merge (isb, &isamb_p , &isamc_i);
479 isamb_dump(isb, isamb_p, log_pr);
486 isamb_merge (isb, &isamb_p , &isamc_i);
493 isamb_merge (isb, &isamb_p , &isamc_i);
496 yaz_log(YLOG_WARN, "tst_minsert: isamb_merge should be empty n=%d",
502 /* tests for identical keys.. ISAMB does not handle that, so some of the
505 static void identical_keys_tests(ISAMB isb)
508 tst_minsert(isb, 10);
511 tst_minsert(isb, 600); /* still fails */
514 tst_random(isb, 20, 200, 1);
517 tst_random(isb, 5, 200, 2);
521 tst_random(isb, 250, 10, 4);
524 /* fails if both are executed */
525 tst_random(isb, 20000, 10, 4);
526 tst_random(isb, 20000, 10, 10);
529 tst_random(isb, 250, 100, 10);
533 int main(int argc, char **argv)
540 yaz_log_init_level(YLOG_ALL);
542 /* setup method (attributes) */
543 method.compare_item = compare_item;
544 method.log_item = log_item;
545 method.codec.start = code_start;
546 method.codec.encode = code_item;
547 method.codec.decode = code_item;
548 method.codec.reset = code_reset;
549 method.codec.stop = code_stop;
551 /* create block system */
552 bfs = bfs_create(0, 0);
555 yaz_log(YLOG_WARN, "bfs_create failed");
561 /* create isam handle */
562 isb = isamb_open (bfs, "isamb", 1, &method, 0);
565 yaz_log(YLOG_WARN, "isamb_open failed");
571 tst_insert(isb, 100);
572 tst_insert(isb, 500);
573 tst_insert(isb, 10000);
575 tst_forward(isb, 10000);
579 tst_append(isb, 1000);
582 identical_keys_tests(isb);
586 /* exit block system */
594 * c-file-style: "Stroustrup"
595 * indent-tabs-mode: nil
597 * vim: shiftwidth=4 tabstop=8 expandtab