Swap functions - minor changes to compare tst function
[idzebra-moved-to-github.git] / isamb / tstisamb.c
1 /* $Id: tstisamb.c,v 1.14 2005-01-02 18:51:31 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23 #include <string.h>
24 #include <yaz/log.h>
25 #include <yaz/xmalloc.h>
26 #include <idzebra/isamb.h>
27 #include <assert.h>
28
29 static void log_item(int level, const void *b, const char *txt)
30 {
31     int x;
32     memcpy(&x, b, sizeof(int));
33     yaz_log(YLOG_DEBUG, "%s %d", txt, x);
34 }
35
36 static void log_pr(const char *txt)
37 {
38     yaz_log(YLOG_DEBUG, "%s", txt);
39 }
40
41 int compare_item(const void *a, const void *b)
42 {
43     int ia, ib;
44
45     memcpy(&ia, a, sizeof(int));
46     memcpy(&ib, b, sizeof(int));
47     if (ia > ib)
48         return 1;
49     if (ia < ib)
50         return -1;
51    return 0;
52 }
53
54 void *code_start()
55 {
56     return 0;
57 }
58
59 void code_item(void *p, char **dst, const char **src)
60 {
61     memcpy (*dst, *src, sizeof(int));
62     (*dst) += sizeof(int);
63     (*src) += sizeof(int);
64 }
65
66 void code_reset(void *p)
67 {
68 }
69 void code_stop(void *p)
70 {
71 }
72
73 struct read_info {
74     int no;
75     int step;
76     int max;
77     int insertMode;
78 };
79
80 int code_read(void *vp, char **dst, int *insertMode)
81 {
82     struct read_info *ri = (struct read_info *)vp;
83     int x;
84
85     if (ri->no >= ri->max)
86         return 0;
87     x = ri->no;
88     memcpy (*dst, &x, sizeof(int));
89     (*dst)+=sizeof(int);
90
91     ri->no = ri->no + ri->step;
92     *insertMode = ri->insertMode;
93     return 1;
94 }
95
96 void tst_insert(ISAMB isb, int n)
97 {
98     ISAMC_I isamc_i;
99     ISAMC_P isamc_p;
100     struct read_info ri;
101     ISAMB_PP pp;
102     char key_buf[10];
103     int nerrs = 0;
104
105     /* insert a number of entries */
106     ri.no = 0;
107     ri.step = 1;
108     ri.max = n;
109     ri.insertMode = 1;
110
111     isamc_i.clientData = &ri;
112     isamc_i.read_item = code_read;
113     
114     isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
115
116     /* read the entries */
117     pp = isamb_pp_open (isb, isamc_p, 1);
118
119     ri.no = 0;
120     while(isamb_pp_read (pp, key_buf))
121     {
122         int x;
123         memcpy (&x, key_buf, sizeof(int));
124         if (x != ri.no)
125         {
126             yaz_log(YLOG_WARN, "isamb_pp_read. n=%d Got %d (expected %d)",
127                     n, x, ri.no);
128             nerrs++;
129         }
130         else if (nerrs)
131             yaz_log(YLOG_LOG, "isamb_pp_read. n=%d Got %d",
132                     n, x);
133
134         ri.no++;
135     }
136     if (ri.no != ri.max)
137     {
138         yaz_log(YLOG_WARN, "ri.max != ri.max (%d != %d)", ri.no, ri.max);
139         nerrs++;
140     }
141     isamb_dump(isb, isamc_p, log_pr);
142     isamb_pp_close(pp);
143
144     if (nerrs)
145         exit(3);
146     return;
147     /* delete a number of entries (even ones) */
148     ri.no = 0;
149     ri.step = 2;
150     ri.max = n;
151     ri.insertMode = 0;
152
153     isamc_i.clientData = &ri;
154     isamc_i.read_item = code_read;
155     
156     isamc_p = isamb_merge (isb, isamc_p , &isamc_i);
157
158     /* delete a number of entries (odd ones) */
159     ri.no = 0;
160     ri.step = 2;
161     ri.max = n;
162     ri.insertMode = 0;
163
164     isamc_i.clientData = &ri;
165     isamc_i.read_item = code_read;
166     
167     isamc_p = isamb_merge (isb, isamc_p , &isamc_i);
168
169     if (isamc_p)
170     {
171         yaz_log(YLOG_WARN, "isamb_merge did not return empty list");
172         exit(3);
173     }
174 }
175
176 void tst_forward(ISAMB isb, int n)
177 {
178     ISAMC_I isamc_i;
179     ISAMC_P isamc_p;
180     struct read_info ri;
181     int i;
182     ISAMB_PP pp;
183
184     /* insert a number of entries */
185     ri.no = 0;
186     ri.step = 1;
187     ri.max = n;
188     ri.insertMode = 1;
189
190     isamc_i.clientData = &ri;
191     isamc_i.read_item = code_read;
192     
193     isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
194
195     /* read the entries */
196     pp = isamb_pp_open (isb, isamc_p, 1);
197     
198     for (i = 0; i<ri.max; i +=2 )
199     {
200         int x = -1;
201         int xu = i;
202         isamb_pp_forward(pp, &x, &xu);
203         if (x != xu && xu != x+1)
204         {
205             yaz_log(YLOG_WARN, "isamb_pp_forward (1). Got %d (expected %d)",
206                     x, xu);
207             exit(4);
208         }
209         ri.no++;
210     }
211     isamb_pp_close(pp);
212     
213     pp = isamb_pp_open (isb, isamc_p, 1);
214     for (i = 0; i<ri.max; i += 100)
215     {
216         int x = -1;
217         int xu = i;
218         isamb_pp_forward(pp, &x, &xu);
219         if (x != xu && xu != x+1)
220         {
221             yaz_log(YLOG_WARN, "isamb_pp_forward (2). Got %d (expected %d)",
222                     x, xu);
223             exit(4);
224         }
225         ri.no++;
226     }
227     isamb_pp_close(pp);
228
229     isamb_unlink(isb, isamc_p);
230 }
231
232 int main(int argc, char **argv)
233 {
234     BFiles bfs;
235     ISAMB isb;
236     ISAMC_M method;
237     
238     if (argc == 2)
239         yaz_log_init_level(YLOG_ALL);
240         
241     /* setup method (attributes) */
242     method.compare_item = compare_item;
243     method.log_item = log_item;
244     method.codec.start = code_start;
245     method.codec.encode = code_item;
246     method.codec.decode = code_item;
247     method.codec.reset = code_reset;
248     method.codec.stop = code_stop;
249
250     /* create block system */
251     bfs = bfs_create(0, 0);
252     if (!bfs)
253     {
254         yaz_log(YLOG_WARN, "bfs_create failed");
255         exit(1);
256     }
257
258     bf_reset(bfs);
259
260     /* create isam handle */
261     isb = isamb_open (bfs, "isamb", 1, &method, 0);
262     if (!isb)
263     {
264         yaz_log(YLOG_WARN, "isamb_open failed");
265         exit(2);
266     }
267     tst_insert(isb, 1);
268     tst_insert(isb, 2);
269     tst_insert(isb, 20);
270     tst_insert(isb, 100);
271     tst_insert(isb, 500);
272     tst_insert(isb, 10000);
273     tst_forward(isb, 10000);
274     /* close isam handle */
275     isamb_close(isb);
276
277     /* exit block system */
278     bfs_destroy(bfs);
279     exit(0);
280     return 0;
281 }