0569d8399d0a4b3e67f3ab1d39abaeed8973ca2b
[idzebra-moved-to-github.git] / rset / rsm_or.c
1 /* $Id: rsm_or.c,v 1.16 2004-08-03 14:54:41 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
24
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <zebrautl.h>
32 #include <isam.h>
33 #include <isamc.h>
34 #include <rsm_or.h>
35
36 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
37 static RSFD r_open (RSET ct, int flag);
38 static void r_close (RSFD rfd);
39 static void r_delete (RSET ct);
40 static void r_rewind (RSFD rfd);
41 static int r_read (RSFD rfd, void *buf, int *term_index);
42 static int r_write (RSFD rfd, const void *buf);
43
44 static const struct rset_control control = 
45 {
46     "multi-or",
47     r_create,
48     r_open,
49     r_close,
50     r_delete,
51     r_rewind,
52     rset_default_forward, /* FIXME */
53     rset_default_pos, /* FIXME */
54     r_read,
55     r_write,
56 };
57
58 const struct rset_control *rset_kind_m_or = &control;
59
60 struct rset_mor_info {
61     int     key_size;
62     int     no_rec;
63     int     (*cmp)(const void *p1, const void *p2);
64     ISAMC   isc;
65     ISAM_P  *isam_positions;
66
67     int     no_isam_positions;
68     int     no_save_positions;
69     struct rset_mor_rfd *rfd_list;
70 };
71
72 struct trunc_info {
73     int  *ptr;
74     int  *indx;
75     char **heap;
76     int  heapnum;
77     int  (*cmp)(const void *p1, const void *p2);
78     int  keysize;
79     char *swapbuf;
80     char *tmpbuf;
81     char *buf;
82 };
83
84 struct rset_mor_rfd {
85     int flag;
86     int position;
87     int position_max;
88     ISAMC_PP *ispt;
89     struct rset_mor_rfd *next;
90     struct rset_mor_info *info;
91     struct trunc_info *ti;
92     int  *countp;
93     char *pbuf;
94 };
95
96 static void heap_swap (struct trunc_info *ti, int i1, int i2)
97 {
98     int swap;
99
100     swap = ti->ptr[i1];
101     ti->ptr[i1] = ti->ptr[i2];
102     ti->ptr[i2] = swap;
103 }
104
105 static void heap_delete (struct trunc_info *ti)
106 {
107     int cur = 1, child = 2;
108
109     heap_swap (ti, 1, ti->heapnum--);
110     while (child <= ti->heapnum) {
111         if (child < ti->heapnum &&
112             (*ti->cmp)(ti->heap[ti->ptr[child]],
113                        ti->heap[ti->ptr[1+child]]) > 0)
114             child++;
115         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
116                        ti->heap[ti->ptr[child]]) > 0)
117         {
118             heap_swap (ti, cur, child);
119             cur = child;
120             child = 2*cur;
121         }
122         else
123             break;
124     }
125 }
126
127 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
128 {
129     int cur, parent;
130
131     cur = ++(ti->heapnum);
132     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
133     ti->indx[ti->ptr[cur]] = indx;
134     parent = cur/2;
135     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
136                                 ti->heap[ti->ptr[cur]]) > 0)
137     {
138         heap_swap (ti, cur, parent);
139         cur = parent;
140         parent = cur/2;
141     }
142 }
143
144 static
145 struct trunc_info *heap_init (int size, int key_size,
146                               int (*cmp)(const void *p1, const void *p2))
147 {
148     struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
149     int i;
150
151     ++size;
152     ti->heapnum = 0;
153     ti->keysize = key_size;
154     ti->cmp = cmp;
155     ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
156     ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
157     ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
158     ti->swapbuf = (char *) xmalloc (ti->keysize);
159     ti->tmpbuf = (char *) xmalloc (ti->keysize);
160     ti->buf = (char *) xmalloc (size * ti->keysize);
161     for (i = size; --i >= 0; )
162     {
163         ti->ptr[i] = i;
164         ti->heap[i] = ti->buf + ti->keysize * i;
165     }
166     return ti;
167 }
168
169 static void heap_close (struct trunc_info *ti)
170 {
171     xfree (ti->buf);
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 = (rset_m_or_parms *) parms;
183     struct rset_mor_info *info;
184
185     ct->flags |= RSET_FLAG_VOLATILE;
186     info = (struct rset_mor_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 = (ISAM_P *)
196         xmalloc (sizeof(*info->isam_positions) * 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 = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
203     ct->rset_terms[0] = 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 = (struct rset_mor_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 = (struct rset_mor_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 = (ISAMC_PP *)
225         xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
226         
227     rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
228
229     ct->rset_terms[0]->nn = 0;
230     for (i = 0; i<info->no_isam_positions; i++)
231     {
232         rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
233         
234         ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
235
236         if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
237             heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
238         else
239         {
240             isc_pp_close (rfd->ispt[i]);
241             rfd->ispt[i] = NULL;
242         }
243     }
244     rfd->position = info->no_save_positions;
245
246     if (ct->no_rset_terms == 1)
247         rfd->countp = &ct->rset_terms[0]->count;
248     else
249         rfd->countp = 0;
250     rfd->pbuf = xmalloc (info->key_size);
251
252     r_rewind (rfd);
253     return rfd;
254 }
255
256 static void r_close (RSFD rfd)
257 {
258     struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
259     struct rset_mor_rfd **rfdp;
260     int i;
261     
262     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
263         if (*rfdp == rfd)
264         {
265             *rfdp = (*rfdp)->next;
266         
267             heap_close (((struct rset_mor_rfd *) rfd)->ti);
268             for (i = 0; i<info->no_isam_positions; i++)
269                 if (((struct rset_mor_rfd *) rfd)->ispt[i])
270                     isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
271             xfree (((struct rset_mor_rfd *)rfd)->ispt);
272             xfree (((struct rset_mor_rfd *)rfd)->pbuf);
273             xfree (rfd);
274             return;
275         }
276     logf (LOG_FATAL, "r_close but no rfd match!");
277     assert (0);
278 }
279
280 static void r_delete (RSET ct)
281 {
282     struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
283     int i;
284
285     assert (info->rfd_list == NULL);
286     xfree (info->isam_positions);
287
288     for (i = 0; i<ct->no_rset_terms; i++)
289         rset_term_destroy (ct->rset_terms[i]);
290     xfree (ct->rset_terms);
291
292     xfree (info);
293 }
294
295 static void r_rewind (RSFD rfd)
296 {
297 }
298
299
300 static int r_read (RSFD rfd, void *buf, int *term_index)
301 {
302     struct rset_mor_rfd *mrfd = (struct rset_mor_rfd *) rfd;
303     struct trunc_info *ti = mrfd->ti;
304     int n = ti->indx[ti->ptr[1]];
305
306     if (!ti->heapnum)
307         return 0;
308     *term_index = 0;
309     memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
310     if (((struct rset_mor_rfd *) rfd)->position)
311     {
312         if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
313         {
314             heap_delete (ti);
315             if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
316                  ((struct rset_mor_rfd *) rfd)->position--;
317             heap_insert (ti, ti->tmpbuf, n);
318         }
319         else
320             heap_delete (ti);
321         if (mrfd->countp && (
322                 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
323         {
324             memcpy (mrfd->pbuf, buf, ti->keysize);
325             (*mrfd->countp)++;
326         }
327         return 1;
328     }
329     while (1)
330     {
331         if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
332         {
333             heap_delete (ti);
334             break;
335         }
336         if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
337         {
338             heap_delete (ti);
339             heap_insert (ti, ti->tmpbuf, n);
340             break;
341         }
342     }
343     if (mrfd->countp && (
344             *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
345     {
346         memcpy (mrfd->pbuf, buf, ti->keysize);
347         (*mrfd->countp)++;
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 }