1 /* $Id: tstisamb.c,v 1.21 2005-04-14 07:48:57 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include <yaz/xmalloc.h>
27 #include <idzebra/isamb.h>
30 static void log_item(int level, const void *b, const char *txt)
33 memcpy(&x, b, sizeof(int));
34 yaz_log(YLOG_DEBUG, "%s %d", txt, x);
37 static void log_pr(const char *txt)
39 yaz_log(YLOG_DEBUG, "%s", txt);
42 int compare_item(const void *a, const void *b)
46 memcpy(&ia, a, sizeof(int));
47 memcpy(&ib, b, sizeof(int));
60 void code_item(void *p, char **dst, const char **src)
62 memcpy (*dst, *src, sizeof(int));
63 (*dst) += sizeof(int);
64 (*src) += sizeof(int);
67 void code_reset(void *p)
70 void code_stop(void *p)
81 int code_read(void *vp, char **dst, int *insertMode)
83 struct read_info *ri = (struct read_info *)vp;
86 if (ri->no >= ri->max)
89 memcpy (*dst, &x, sizeof(int));
92 ri->no = ri->no + ri->step;
93 *insertMode = ri->insertMode;
97 void tst_insert(ISAMB isb, int n)
106 /* insert a number of entries */
112 isamc_i.clientData = &ri;
113 isamc_i.read_item = code_read;
115 isamc_p = 0; /* new list */
116 isamb_merge (isb, &isamc_p , &isamc_i);
118 /* read the entries */
119 pp = isamb_pp_open (isb, isamc_p, 1);
122 while(isamb_pp_read (pp, key_buf))
125 memcpy (&x, key_buf, sizeof(int));
128 yaz_log(YLOG_WARN, "isamb_pp_read. n=%d Got %d (expected %d)",
133 yaz_log(YLOG_LOG, "isamb_pp_read. n=%d Got %d",
140 yaz_log(YLOG_WARN, "ri.max != ri.max (%d != %d)", ri.no, ri.max);
143 isamb_dump(isb, isamc_p, log_pr);
148 /* delete a number of entries (even ones) */
154 isamc_i.clientData = &ri;
155 isamc_i.read_item = code_read;
157 isamb_merge (isb, &isamc_p , &isamc_i);
159 /* delete a number of entries (odd ones) */
165 isamc_i.clientData = &ri;
166 isamc_i.read_item = code_read;
168 isamb_merge (isb, &isamc_p, &isamc_i);
172 yaz_log(YLOG_WARN, "isamb_merge did not return empty list");
177 void tst_forward(ISAMB isb, int n)
185 /* insert a number of entries */
191 isamc_i.clientData = &ri;
192 isamc_i.read_item = code_read;
195 isamb_merge (isb, &isamc_p, &isamc_i);
197 /* read the entries */
198 pp = isamb_pp_open (isb, isamc_p, 1);
200 for (i = 0; i<ri.max; i +=2 )
204 isamb_pp_forward(pp, &x, &xu);
205 if (x != xu && xu != x+1)
207 yaz_log(YLOG_WARN, "isamb_pp_forward (1). Got %d (expected %d)",
215 pp = isamb_pp_open (isb, isamc_p, 1);
216 for (i = 0; i<ri.max; i += 100)
220 isamb_pp_forward(pp, &x, &xu);
221 if (x != xu && xu != x+1)
223 yaz_log(YLOG_WARN, "isamb_pp_forward (2). Got %d (expected %d)",
231 isamb_unlink(isb, isamc_p);
234 void tst_x(ISAMB isb)
240 isamc_i.clientData = &ri;
241 isamc_i.read_item = code_read;
247 isamb_merge (isb, &isamb_p , &isamc_i);
254 isamb_merge (isb, &isamb_p , &isamc_i);
257 void tst_append(ISAMB isb, int n)
265 for (i = 0; i < n; i += chunk)
267 /* insert a number of entries */
273 isamc_i.clientData = &ri;
274 isamc_i.read_item = code_read;
276 isamb_merge (isb, &isamb_p , &isamc_i);
280 int main(int argc, char **argv)
287 yaz_log_init_level(YLOG_ALL);
289 /* setup method (attributes) */
290 method.compare_item = compare_item;
291 method.log_item = log_item;
292 method.codec.start = code_start;
293 method.codec.encode = code_item;
294 method.codec.decode = code_item;
295 method.codec.reset = code_reset;
296 method.codec.stop = code_stop;
298 /* create block system */
299 bfs = bfs_create(0, 0);
302 yaz_log(YLOG_WARN, "bfs_create failed");
308 /* create isam handle */
309 isb = isamb_open (bfs, "isamb", 1, &method, 0);
312 yaz_log(YLOG_WARN, "isamb_open failed");
318 tst_insert(isb, 100);
319 tst_insert(isb, 500);
320 tst_insert(isb, 10000);
322 tst_forward(isb, 10000);
326 tst_append(isb, 1000);
327 /* close isam handle */
330 /* exit block system */