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