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