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