1 /* $Id: tstisamb.c,v 1.11 2004-11-19 10:27:09 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
24 #include <yaz/xmalloc.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));
55 void code_item(void *p, char **dst, const char **src)
57 memcpy (*dst, *src, sizeof(int));
58 (*dst) += sizeof(int);
59 (*src) += sizeof(int);
62 void code_reset(void *p)
65 void code_stop(void *p)
76 int code_read(void *vp, char **dst, int *insertMode)
78 struct read_info *ri = (struct read_info *)vp;
81 if (ri->no >= ri->max)
84 memcpy (*dst, &x, sizeof(int));
88 *insertMode = ri->insertMode;
92 void tst_forward(ISAMB isb, int n)
100 /* insert a number of entries */
106 isamc_i.clientData = &ri;
107 isamc_i.read_item = code_read;
109 isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
111 /* read the entries */
112 pp = isamb_pp_open (isb, isamc_p, 2);
114 for (i = 0; i<ri.max; i +=2 )
118 isamb_pp_forward(pp, &x, &xu);
119 if (x != xu && xu != x+1)
121 yaz_log(YLOG_WARN, "isamb_pp_forward (1). Got %d (expected %d)",
129 pp = isamb_pp_open (isb, isamc_p, 2);
130 for (i = 0; i<ri.max; i += 100)
134 isamb_pp_forward(pp, &x, &xu);
135 if (x != xu && xu != x+1)
137 yaz_log(YLOG_WARN, "isamb_pp_forward (2). Got %d (expected %d)",
145 isamb_unlink(isb, isamc_p);
148 void tst_insert(ISAMB isb, int n)
156 /* insert a number of entries */
162 isamc_i.clientData = &ri;
163 isamc_i.read_item = code_read;
165 isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
167 /* read the entries */
168 pp = isamb_pp_open (isb, isamc_p, 2);
171 while(isamb_pp_read (pp, key_buf))
174 memcpy (&x, key_buf, sizeof(int));
177 yaz_log(YLOG_WARN, "isamb_pp_read. Got %d (expected %d)",
185 yaz_log(YLOG_WARN, "ri.max != ri.max (%d != %d)", ri.no, ri.max);
190 isamb_dump(isb, isamc_p, log_pr);
192 /* delete a number of entries (even ones) */
198 isamc_i.clientData = &ri;
199 isamc_i.read_item = code_read;
201 isamc_p = isamb_merge (isb, isamc_p , &isamc_i);
203 /* delete a number of entries (odd ones) */
209 isamc_i.clientData = &ri;
210 isamc_i.read_item = code_read;
212 isamc_p = isamb_merge (isb, isamc_p , &isamc_i);
216 yaz_log(YLOG_WARN, "isamb_merge did not return empty list");
221 int main(int argc, char **argv)
228 yaz_log_init_level(YLOG_ALL);
230 /* setup method (attributes) */
231 method.compare_item = compare_item;
232 method.log_item = log_item;
233 method.codec.start = code_start;
234 method.codec.encode = code_item;
235 method.codec.decode = code_item;
236 method.codec.reset = code_reset;
237 method.codec.stop = code_stop;
239 /* create block system */
240 bfs = bfs_create(0, 0);
243 yaz_log(YLOG_WARN, "bfs_create failed");
249 /* create isam handle */
250 isb = isamb_open (bfs, "isamb", 1, &method, 0);
253 yaz_log(YLOG_WARN, "isamb_open failed");
259 tst_insert(isb, 100);
260 tst_insert(isb, 500);
261 tst_insert(isb, 10000);
262 tst_forward(isb, 10000);
263 /* close isam handle */
266 /* exit block system */