Bump year. Change Aps->ApS
[idzebra-moved-to-github.git] / isamb / tstisamb.c
1 /* $Id: tstisamb.c,v 1.16 2005-01-15 19:38: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     /* delete a number of entries (even ones) */
147     ri.no = 0;
148     ri.step = 2;
149     ri.max = n;
150     ri.insertMode = 0;
151
152     isamc_i.clientData = &ri;
153     isamc_i.read_item = code_read;
154     
155     isamc_p = isamb_merge (isb, isamc_p , &isamc_i);
156
157     /* delete a number of entries (odd ones) */
158     ri.no = 1;
159     ri.step = 2;
160     ri.max = n;
161     ri.insertMode = 0;
162
163     isamc_i.clientData = &ri;
164     isamc_i.read_item = code_read;
165     
166     isamc_p = isamb_merge (isb, isamc_p , &isamc_i);
167
168     if (isamc_p)
169     {
170         yaz_log(YLOG_WARN, "isamb_merge did not return empty list");
171         exit(3);
172     }
173 }
174
175 void tst_forward(ISAMB isb, int n)
176 {
177     ISAMC_I isamc_i;
178     ISAMC_P isamc_p;
179     struct read_info ri;
180     int i;
181     ISAMB_PP pp;
182
183     /* insert a number of entries */
184     ri.no = 0;
185     ri.step = 1;
186     ri.max = n;
187     ri.insertMode = 1;
188
189     isamc_i.clientData = &ri;
190     isamc_i.read_item = code_read;
191     
192     isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
193
194     /* read the entries */
195     pp = isamb_pp_open (isb, isamc_p, 1);
196     
197     for (i = 0; i<ri.max; i +=2 )
198     {
199         int x = -1;
200         int xu = i;
201         isamb_pp_forward(pp, &x, &xu);
202         if (x != xu && xu != x+1)
203         {
204             yaz_log(YLOG_WARN, "isamb_pp_forward (1). Got %d (expected %d)",
205                     x, xu);
206             exit(4);
207         }
208         ri.no++;
209     }
210     isamb_pp_close(pp);
211     
212     pp = isamb_pp_open (isb, isamc_p, 1);
213     for (i = 0; i<ri.max; i += 100)
214     {
215         int x = -1;
216         int xu = i;
217         isamb_pp_forward(pp, &x, &xu);
218         if (x != xu && xu != x+1)
219         {
220             yaz_log(YLOG_WARN, "isamb_pp_forward (2). Got %d (expected %d)",
221                     x, xu);
222             exit(4);
223         }
224         ri.no++;
225     }
226     isamb_pp_close(pp);
227
228     isamb_unlink(isb, isamc_p);
229 }
230
231 int main(int argc, char **argv)
232 {
233     BFiles bfs;
234     ISAMB isb;
235     ISAMC_M method;
236     
237     if (argc == 2)
238         yaz_log_init_level(YLOG_ALL);
239         
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;
248
249     /* create block system */
250     bfs = bfs_create(0, 0);
251     if (!bfs)
252     {
253         yaz_log(YLOG_WARN, "bfs_create failed");
254         exit(1);
255     }
256
257     bf_reset(bfs);
258
259     /* create isam handle */
260     isb = isamb_open (bfs, "isamb", 1, &method, 0);
261     if (!isb)
262     {
263         yaz_log(YLOG_WARN, "isamb_open failed");
264         exit(2);
265     }
266     tst_insert(isb, 1);
267     tst_insert(isb, 2);
268     tst_insert(isb, 20);
269     tst_insert(isb, 100);
270     tst_insert(isb, 500);
271     tst_insert(isb, 10000);
272     tst_forward(isb, 10000);
273     /* close isam handle */
274     isamb_close(isb);
275
276     /* exit block system */
277     bfs_destroy(bfs);
278     exit(0);
279     return 0;
280 }