c56a98f3c8586eea6007a09975c69d95b4a29494
[idzebra-moved-to-github.git] / isamb / tstisamb.c
1 /* $Id: tstisamb.c,v 1.7.2.1 2005-01-17 08:46:25 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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 <stdlib.h>
24 #include <string.h>
25 #include <yaz/xmalloc.h>
26 #include <yaz/log.h>
27 #include <isamb.h>
28 #include <assert.h>
29
30 static void log_item(int level, const void *b, const char *txt)
31 {
32     int x;
33     memcpy(&x, b, sizeof(int));
34     yaz_log(LOG_DEBUG, "%s %d", txt, x);
35 }
36
37 static void log_pr(const char *txt)
38 {
39     yaz_log(LOG_DEBUG, "%s", txt);
40 }
41
42 int compare_item(const void *a, const void *b)
43 {
44     int ia, ib;
45
46     memcpy(&ia, a, sizeof(int));
47     memcpy(&ib, b, sizeof(int));
48     return ia - ib;
49 }
50
51 void *code_start(int mode)
52 {
53     return 0;
54 }
55
56 void code_item(int mode, void *p, char **dst, char **src)
57 {
58     memcpy (*dst, *src, sizeof(int));
59     (*dst) += sizeof(int);
60     (*src) += sizeof(int);
61 }
62
63 void code_reset(void *p)
64 {
65 }
66 void code_stop(int mode, void *p)
67 {
68 }
69
70 struct read_info {
71     int no;
72     int max;
73 };
74
75 int code_read(void *vp, char **dst, int *insertMode)
76 {
77     struct read_info *ri = (struct read_info *)vp;
78     int x;
79
80     if (ri->no > ri->max)
81         exit(3);
82     if (ri->no == ri->max)
83         return 0;
84     x = ri->no;
85     memcpy (*dst, &x, sizeof(int));
86     (*dst)+=sizeof(int);
87
88     (ri->no)++;
89     *insertMode = 1;
90     return 1;
91 }
92
93 void tst_forward(ISAMB isb, int n)
94 {
95     ISAMC_I isamc_i;
96     ISAMC_P isamc_p;
97     struct read_info ri;
98     int i;
99     ISAMB_PP pp;
100
101     /* insert a number of entries */
102     ri.no = 0;
103     ri.max = n;
104
105     isamc_i.clientData = &ri;
106     isamc_i.read_item = code_read;
107     
108     isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
109
110     /* read the entries */
111     pp = isamb_pp_open (isb, isamc_p);
112     
113     for (i = 0; i<ri.max; i +=2 )
114     {
115         int x = -1;
116         int xu = i;
117         isamb_pp_forward(pp, &x, &xu);
118         if (x != xu && xu != x+1)
119         {
120             yaz_log(LOG_WARN, "isamb_pp_forward (1). Got %d (expected %d)",
121                     x, xu);
122             exit(4);
123         }
124         ri.no++;
125     }
126     isamb_pp_close(pp);
127     
128     pp = isamb_pp_open (isb, isamc_p);
129     for (i = 0; i<ri.max; i += 100)
130     {
131         int x = -1;
132         int xu = i;
133         isamb_pp_forward(pp, &x, &xu);
134         if (x != xu && xu != x+1)
135         {
136             yaz_log(LOG_WARN, "isamb_pp_forward (2). Got %d (expected %d)",
137                     x, xu);
138             exit(4);
139         }
140         ri.no++;
141     }
142     isamb_pp_close(pp);
143
144     isamb_unlink(isb, isamc_p);
145 }
146
147 void tst_insert(ISAMB isb, int n)
148 {
149     ISAMC_I isamc_i;
150     ISAMC_P isamc_p;
151     struct read_info ri;
152     ISAMB_PP pp;
153     char key_buf[10];
154
155     /* insert a number of entries */
156     ri.no = 0;
157     ri.max = n;
158
159     isamc_i.clientData = &ri;
160     isamc_i.read_item = code_read;
161     
162     isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
163
164     /* read the entries */
165     pp = isamb_pp_open (isb, isamc_p);
166     
167     ri.no = 0;
168     while(isamb_pp_read (pp, key_buf))
169     {
170         int x;
171         memcpy (&x, key_buf, sizeof(int));
172         if (x != ri.no)
173         {
174             yaz_log(LOG_WARN, "isamb_pp_read. Got %d (expected %d)",
175                     x, ri.no);
176             exit(3);
177         }
178         ri.no++;
179     }
180     if (ri.no != ri.max)
181     {
182         yaz_log(LOG_WARN, "ri.max != ri.max (%d != %d)", ri.no, ri.max);
183         exit(3);
184     }
185     isamb_pp_close(pp);
186
187     isamb_dump(isb, isamc_p, log_pr);
188     isamb_unlink(isb, isamc_p);
189 }
190
191 int main(int argc, char **argv)
192 {
193     BFiles bfs;
194     ISAMB isb;
195     ISAMC_M method;
196     
197     if (argc == 2)
198         yaz_log_init_level(LOG_ALL);
199         
200     /* setup method (attributes) */
201     method.compare_item = compare_item;
202     method.log_item = log_item;
203     method.code_start = code_start;
204     method.code_item = code_item;
205     method.code_reset = code_reset;
206     method.code_stop = code_stop;
207
208     /* create block system */
209     bfs = bfs_create(0, 0);
210     if (!bfs)
211     {
212         yaz_log(LOG_WARN, "bfs_create failed");
213         exit(1);
214     }
215
216     bf_reset(bfs);
217
218     /* create isam handle */
219     isb = isamb_open (bfs, "isamb", 1, &method, 0);
220     if (!isb)
221     {
222         yaz_log(LOG_WARN, "isamb_open failed");
223         exit(2);
224     }
225     tst_insert(isb, 1);
226     tst_insert(isb, 2);
227     tst_insert(isb, 20);
228     tst_insert(isb, 100);
229     tst_insert(isb, 500);
230     tst_insert(isb, 10000);
231     tst_forward(isb, 10000);
232     /* close isam handle */
233     isamb_close(isb);
234
235     /* exit block system */
236     bfs_destroy(bfs);
237     exit(0);
238     return 0;
239 }