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