Improved installation. Updated for inclusion of YAZ header files.
[idzebra-moved-to-github.git] / rset / rsm_or.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: rsm_or.c,v $
7  * Revision 1.10  1999-11-30 13:48:04  adam
8  * Improved installation. Updated for inclusion of YAZ header files.
9  *
10  * Revision 1.9  1999/07/13 14:45:42  adam
11  * Fixed memory leak.
12  *
13  * Revision 1.8  1999/05/26 07:49:14  adam
14  * C++ compilation.
15  *
16  * Revision 1.7  1998/09/22 10:03:46  adam
17  * Changed result sets to be persistent in the sense that they can
18  * be re-searched if needed.
19  * Fixed memory leak in rsm_or.
20  *
21  * Revision 1.6  1998/03/05 08:36:28  adam
22  * New result set model.
23  *
24  * Revision 1.5  1997/12/18 10:54:25  adam
25  * New method result set method rs_hits that returns the number of
26  * hits in result-set (if known). The ranked result set returns real
27  * number of hits but only when not combined with other operands.
28  *
29  * Revision 1.4  1997/10/31 12:37:55  adam
30  * Code calls xfree() instead of free().
31  *
32  * Revision 1.3  1997/09/09 13:38:16  adam
33  * Partial port to WIN95/NT.
34  *
35  * Revision 1.2  1996/12/23 15:30:49  adam
36  * Work on truncation.
37  *
38  * Revision 1.1  1996/12/20 11:07:21  adam
39  * Implemented Multi-or result set.
40  *
41  */
42
43
44 #include <assert.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include <zebrautl.h>
50 #if ZMBOL
51 #include <isam.h>
52 #include <isamc.h>
53 #include <rsm_or.h>
54
55 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
56 static RSFD r_open (RSET ct, int flag);
57 static void r_close (RSFD rfd);
58 static void r_delete (RSET ct);
59 static void r_rewind (RSFD rfd);
60 static int r_count (RSET ct);
61 static int r_read (RSFD rfd, void *buf, int *term_index);
62 static int r_write (RSFD rfd, const void *buf);
63
64 static const struct rset_control control = 
65 {
66     "multi-or",
67     r_create,
68     r_open,
69     r_close,
70     r_delete,
71     r_rewind,
72     r_count,
73     r_read,
74     r_write,
75 };
76
77 const struct rset_control *rset_kind_m_or = &control;
78
79 struct rset_mor_info {
80     int     key_size;
81     int     no_rec;
82     int     (*cmp)(const void *p1, const void *p2);
83     ISAMC   isc;
84     ISAM_P  *isam_positions;
85
86     int     no_isam_positions;
87     int     no_save_positions;
88     struct rset_mor_rfd *rfd_list;
89 };
90
91 struct trunc_info {
92     int  *ptr;
93     int  *indx;
94     char **heap;
95     int  heapnum;
96     int  (*cmp)(const void *p1, const void *p2);
97     int  keysize;
98     char *swapbuf;
99     char *tmpbuf;
100     char *buf;
101 };
102
103 struct rset_mor_rfd {
104     int flag;
105     int position;
106     int position_max;
107     ISAMC_PP *ispt;
108     struct rset_mor_rfd *next;
109     struct rset_mor_info *info;
110     struct trunc_info *ti;
111 };
112
113 static void heap_swap (struct trunc_info *ti, int i1, int i2)
114 {
115     int swap;
116
117     swap = ti->ptr[i1];
118     ti->ptr[i1] = ti->ptr[i2];
119     ti->ptr[i2] = swap;
120 }
121
122 static void heap_delete (struct trunc_info *ti)
123 {
124     int cur = 1, child = 2;
125
126     heap_swap (ti, 1, ti->heapnum--);
127     while (child <= ti->heapnum) {
128         if (child < ti->heapnum &&
129             (*ti->cmp)(ti->heap[ti->ptr[child]],
130                        ti->heap[ti->ptr[1+child]]) > 0)
131             child++;
132         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
133                        ti->heap[ti->ptr[child]]) > 0)
134         {
135             heap_swap (ti, cur, child);
136             cur = child;
137             child = 2*cur;
138         }
139         else
140             break;
141     }
142 }
143
144 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
145 {
146     int cur, parent;
147
148     cur = ++(ti->heapnum);
149     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
150     ti->indx[ti->ptr[cur]] = indx;
151     parent = cur/2;
152     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
153                                 ti->heap[ti->ptr[cur]]) > 0)
154     {
155         heap_swap (ti, cur, parent);
156         cur = parent;
157         parent = cur/2;
158     }
159 }
160
161 static
162 struct trunc_info *heap_init (int size, int key_size,
163                               int (*cmp)(const void *p1, const void *p2))
164 {
165     struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
166     int i;
167
168     ++size;
169     ti->heapnum = 0;
170     ti->keysize = key_size;
171     ti->cmp = cmp;
172     ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
173     ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
174     ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
175     ti->swapbuf = (char *) xmalloc (ti->keysize);
176     ti->tmpbuf = (char *) xmalloc (ti->keysize);
177     ti->buf = (char *) xmalloc (size * ti->keysize);
178     for (i = size; --i >= 0; )
179     {
180         ti->ptr[i] = i;
181         ti->heap[i] = ti->buf + ti->keysize * i;
182     }
183     return ti;
184 }
185
186 static void heap_close (struct trunc_info *ti)
187 {
188     xfree (ti->buf);
189     xfree (ti->ptr);
190     xfree (ti->indx);
191     xfree (ti->heap);
192     xfree (ti->swapbuf);
193     xfree (ti->tmpbuf);
194     xfree (ti);
195 }
196
197 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
198 {
199     rset_m_or_parms *r_parms = (rset_m_or_parms *) parms;
200     struct rset_mor_info *info;
201
202     ct->flags |= RSET_FLAG_VOLATILE;
203     info = (struct rset_mor_info *) xmalloc (sizeof(*info));
204     info->key_size = r_parms->key_size;
205     assert (info->key_size > 1);
206     info->cmp = r_parms->cmp;
207     
208     info->no_save_positions = r_parms->no_save_positions;
209
210     info->isc = r_parms->isc;
211     info->no_isam_positions = r_parms->no_isam_positions;
212     info->isam_positions = (ISAM_P *)
213         xmalloc (sizeof(*info->isam_positions) * info->no_isam_positions);
214     memcpy (info->isam_positions, r_parms->isam_positions,
215             sizeof(*info->isam_positions) * info->no_isam_positions);
216     info->rfd_list = NULL;
217
218     ct->no_rset_terms = 1;
219     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
220     ct->rset_terms[0] = r_parms->rset_term;
221     return info;
222 }
223
224 static RSFD r_open (RSET ct, int flag)
225 {
226     struct rset_mor_rfd *rfd;
227     struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
228     int i;
229
230     if (flag & RSETF_WRITE)
231     {
232         logf (LOG_FATAL, "m_or set type is read-only");
233         return NULL;
234     }
235     rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
236     rfd->flag = flag;
237     rfd->next = info->rfd_list;
238     rfd->info = info;
239     info->rfd_list = rfd;
240
241     rfd->ispt = (ISAMC_PP *)
242         xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
243         
244     rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
245
246     ct->rset_terms[0]->nn = 0;
247     for (i = 0; i<info->no_isam_positions; i++)
248     {
249         rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
250         
251         ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
252
253         if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
254             heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
255         else
256         {
257             isc_pp_close (rfd->ispt[i]);
258             rfd->ispt[i] = NULL;
259         }
260     }
261     rfd->position = info->no_save_positions;
262     r_rewind (rfd);
263     return rfd;
264 }
265
266 static void r_close (RSFD rfd)
267 {
268     struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
269     struct rset_mor_rfd **rfdp;
270     int i;
271     
272     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
273         if (*rfdp == rfd)
274         {
275             *rfdp = (*rfdp)->next;
276         
277             heap_close (((struct rset_mor_rfd *) rfd)->ti);
278             for (i = 0; i<info->no_isam_positions; i++)
279                 if (((struct rset_mor_rfd *) rfd)->ispt[i])
280                     isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
281             xfree (((struct rset_mor_rfd *)rfd)->ispt);
282             xfree (rfd);
283             return;
284         }
285     logf (LOG_FATAL, "r_close but no rfd match!");
286     assert (0);
287 }
288
289 static void r_delete (RSET ct)
290 {
291     struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
292     int i;
293
294     assert (info->rfd_list == NULL);
295     xfree (info->isam_positions);
296
297     for (i = 0; i<ct->no_rset_terms; i++)
298         rset_term_destroy (ct->rset_terms[i]);
299     xfree (ct->rset_terms);
300
301     xfree (info);
302 }
303
304 static void r_rewind (RSFD rfd)
305 {
306 }
307
308 static int r_count (RSET ct)
309 {
310     return 0;
311 }
312
313 static int r_read (RSFD rfd, void *buf, int *term_index)
314 {
315     struct trunc_info *ti = ((struct rset_mor_rfd *) rfd)->ti;
316     int n = ti->indx[ti->ptr[1]];
317
318     if (!ti->heapnum)
319         return 0;
320     *term_index = 0;
321     memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
322     if (((struct rset_mor_rfd *) rfd)->position)
323     {
324         if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
325         {
326             heap_delete (ti);
327             if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
328                  ((struct rset_mor_rfd *) rfd)->position--;
329             heap_insert (ti, ti->tmpbuf, n);
330         }
331         else
332             heap_delete (ti);
333         return 1;
334     }
335     while (1)
336     {
337         if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
338         {
339             heap_delete (ti);
340             break;
341         }
342         if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
343         {
344             heap_delete (ti);
345             heap_insert (ti, ti->tmpbuf, n);
346             break;
347         }
348     }
349     return 1;
350 }
351
352 static int r_write (RSFD rfd, const void *buf)
353 {
354     logf (LOG_FATAL, "mor set type is read-only");
355     return -1;
356 }
357 #endif