1 /* $Id: tstisamb.c,v 1.16 2005-01-15 19:38:31 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
25 #include <yaz/xmalloc.h>
26 #include <idzebra/isamb.h>
29 static void log_item(int level, const void *b, const char *txt)
32 memcpy(&x, b, sizeof(int));
33 yaz_log(YLOG_DEBUG, "%s %d", txt, x);
36 static void log_pr(const char *txt)
38 yaz_log(YLOG_DEBUG, "%s", txt);
41 int compare_item(const void *a, const void *b)
45 memcpy(&ia, a, sizeof(int));
46 memcpy(&ib, b, sizeof(int));
59 void code_item(void *p, char **dst, const char **src)
61 memcpy (*dst, *src, sizeof(int));
62 (*dst) += sizeof(int);
63 (*src) += sizeof(int);
66 void code_reset(void *p)
69 void code_stop(void *p)
80 int code_read(void *vp, char **dst, int *insertMode)
82 struct read_info *ri = (struct read_info *)vp;
85 if (ri->no >= ri->max)
88 memcpy (*dst, &x, sizeof(int));
91 ri->no = ri->no + ri->step;
92 *insertMode = ri->insertMode;
96 void tst_insert(ISAMB isb, int n)
105 /* insert a number of entries */
111 isamc_i.clientData = &ri;
112 isamc_i.read_item = code_read;
114 isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
116 /* read the entries */
117 pp = isamb_pp_open (isb, isamc_p, 1);
120 while(isamb_pp_read (pp, key_buf))
123 memcpy (&x, key_buf, sizeof(int));
126 yaz_log(YLOG_WARN, "isamb_pp_read. n=%d Got %d (expected %d)",
131 yaz_log(YLOG_LOG, "isamb_pp_read. n=%d Got %d",
138 yaz_log(YLOG_WARN, "ri.max != ri.max (%d != %d)", ri.no, ri.max);
141 isamb_dump(isb, isamc_p, log_pr);
146 /* delete a number of entries (even ones) */
152 isamc_i.clientData = &ri;
153 isamc_i.read_item = code_read;
155 isamc_p = isamb_merge (isb, isamc_p , &isamc_i);
157 /* delete a number of entries (odd ones) */
163 isamc_i.clientData = &ri;
164 isamc_i.read_item = code_read;
166 isamc_p = isamb_merge (isb, isamc_p , &isamc_i);
170 yaz_log(YLOG_WARN, "isamb_merge did not return empty list");
175 void tst_forward(ISAMB isb, int n)
183 /* insert a number of entries */
189 isamc_i.clientData = &ri;
190 isamc_i.read_item = code_read;
192 isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
194 /* read the entries */
195 pp = isamb_pp_open (isb, isamc_p, 1);
197 for (i = 0; i<ri.max; i +=2 )
201 isamb_pp_forward(pp, &x, &xu);
202 if (x != xu && xu != x+1)
204 yaz_log(YLOG_WARN, "isamb_pp_forward (1). Got %d (expected %d)",
212 pp = isamb_pp_open (isb, isamc_p, 1);
213 for (i = 0; i<ri.max; i += 100)
217 isamb_pp_forward(pp, &x, &xu);
218 if (x != xu && xu != x+1)
220 yaz_log(YLOG_WARN, "isamb_pp_forward (2). Got %d (expected %d)",
228 isamb_unlink(isb, isamc_p);
231 int main(int argc, char **argv)
238 yaz_log_init_level(YLOG_ALL);
240 /* setup method (attributes) */
241 method.compare_item = compare_item;
242 method.log_item = log_item;
243 method.codec.start = code_start;
244 method.codec.encode = code_item;
245 method.codec.decode = code_item;
246 method.codec.reset = code_reset;
247 method.codec.stop = code_stop;
249 /* create block system */
250 bfs = bfs_create(0, 0);
253 yaz_log(YLOG_WARN, "bfs_create failed");
259 /* create isam handle */
260 isb = isamb_open (bfs, "isamb", 1, &method, 0);
263 yaz_log(YLOG_WARN, "isamb_open failed");
269 tst_insert(isb, 100);
270 tst_insert(isb, 500);
271 tst_insert(isb, 10000);
272 tst_forward(isb, 10000);
273 /* close isam handle */
276 /* exit block system */