Added skeleton for multior - not at all functional yet. Call to it
[idzebra-moved-to-github.git] / rset / rsm_or.c
1 /* $Id: rsm_or.c,v 1.19 2004-08-16 16:17:49 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 <isamc.h>
33 #include <rsm_or.h>
34
35 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
36 static RSFD r_open (RSET ct, int flag);
37 static void r_close (RSFD rfd);
38 static void r_delete (RSET ct);
39 static void r_rewind (RSFD rfd);
40 static int r_read (RSFD rfd, void *buf, int *term_index);
41 static int r_write (RSFD rfd, const void *buf);
42
43 static const struct rset_control control = 
44 {
45     "m-or (old)",
46     r_create,
47     r_open,
48     r_close,
49     r_delete,
50     r_rewind,
51     rset_default_forward, /* FIXME */
52     rset_default_pos, /* FIXME */
53     r_read,
54     r_write,
55 };
56
57 const struct rset_control *rset_kind_m_or = &control;
58
59 struct rset_mor_info {
60     int     key_size;
61     int     no_rec;
62     int     (*cmp)(const void *p1, const void *p2);
63     ISAMC   isc;
64     ISAMC_P *isam_positions;
65
66     int     no_isam_positions;
67     int     no_save_positions;
68     struct rset_mor_rfd *rfd_list;
69 };
70
71 struct trunc_info {
72     int  *ptr;
73     int  *indx;
74     char **heap;
75     int  heapnum;
76     int  (*cmp)(const void *p1, const void *p2);
77     int  keysize;
78     char *swapbuf;
79     char *tmpbuf;
80     char *buf;
81 };
82
83 struct rset_mor_rfd {
84     int flag;
85     int position;
86     int position_max;
87     ISAMC_PP *ispt;
88     struct rset_mor_rfd *next;
89     struct rset_mor_info *info;
90     struct trunc_info *ti;
91     zint *countp;
92     char *pbuf;
93 };
94
95 static void heap_swap (struct trunc_info *ti, int i1, int i2)
96 {
97     int swap;
98
99     swap = ti->ptr[i1];
100     ti->ptr[i1] = ti->ptr[i2];
101     ti->ptr[i2] = swap;
102 }
103
104 static void heap_delete (struct trunc_info *ti)
105 {
106     int cur = 1, child = 2;
107
108     heap_swap (ti, 1, ti->heapnum--);
109     while (child <= ti->heapnum) {
110         if (child < ti->heapnum &&
111             (*ti->cmp)(ti->heap[ti->ptr[child]],
112                        ti->heap[ti->ptr[1+child]]) > 0)
113             child++;
114         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
115                        ti->heap[ti->ptr[child]]) > 0)
116         {
117             heap_swap (ti, cur, child);
118             cur = child;
119             child = 2*cur;
120         }
121         else
122             break;
123     }
124 }
125
126 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
127 {
128     int cur, parent;
129
130     cur = ++(ti->heapnum);
131     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
132     ti->indx[ti->ptr[cur]] = indx;
133     parent = cur/2;
134     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
135                                 ti->heap[ti->ptr[cur]]) > 0)
136     {
137         heap_swap (ti, cur, parent);
138         cur = parent;
139         parent = cur/2;
140     }
141 }
142
143 static
144 struct trunc_info *heap_init (int size, int key_size,
145                               int (*cmp)(const void *p1, const void *p2))
146 {
147     struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
148     int i;
149
150     ++size;
151     ti->heapnum = 0;
152     ti->keysize = key_size;
153     ti->cmp = cmp;
154     ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
155     ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
156     ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
157     ti->swapbuf = (char *) xmalloc (ti->keysize);
158     ti->tmpbuf = (char *) xmalloc (ti->keysize);
159     ti->buf = (char *) xmalloc (size * ti->keysize);
160     for (i = size; --i >= 0; )
161     {
162         ti->ptr[i] = i;
163         ti->heap[i] = ti->buf + ti->keysize * i;
164     }
165     return ti;
166 }
167
168 static void heap_close (struct trunc_info *ti)
169 {
170     xfree (ti->buf);
171     xfree (ti->ptr);
172     xfree (ti->indx);
173     xfree (ti->heap);
174     xfree (ti->swapbuf);
175     xfree (ti->tmpbuf);
176     xfree (ti);
177 }
178
179 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
180 {
181     rset_m_or_parms *r_parms = (rset_m_or_parms *) parms;
182     struct rset_mor_info *info;
183
184     ct->flags |= RSET_FLAG_VOLATILE;
185     info = (struct rset_mor_info *) xmalloc (sizeof(*info));
186     info->key_size = r_parms->key_size;
187     assert (info->key_size > 1);
188     info->cmp = r_parms->cmp;
189     
190     info->no_save_positions = r_parms->no_save_positions;
191
192     info->isc = r_parms->isc;
193     info->no_isam_positions = r_parms->no_isam_positions;
194     info->isam_positions = (ISAMC_P *)
195         xmalloc (sizeof(*info->isam_positions) * info->no_isam_positions);
196     memcpy (info->isam_positions, r_parms->isam_positions,
197             sizeof(*info->isam_positions) * info->no_isam_positions);
198     info->rfd_list = NULL;
199
200     ct->no_rset_terms = 1;
201     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
202     ct->rset_terms[0] = r_parms->rset_term;
203     return info;
204 }
205
206 static RSFD r_open (RSET ct, int flag)
207 {
208     struct rset_mor_rfd *rfd;
209     struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
210     int i;
211
212     if (flag & RSETF_WRITE)
213     {
214         logf (LOG_FATAL, "m_or set type is read-only");
215         return NULL;
216     }
217     rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
218     rfd->flag = flag;
219     rfd->next = info->rfd_list;
220     rfd->info = info;
221     info->rfd_list = rfd;
222
223     rfd->ispt = (ISAMC_PP *)
224         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
245     if (ct->no_rset_terms == 1)
246         rfd->countp = &ct->rset_terms[0]->count;
247     else
248         rfd->countp = 0;
249     rfd->pbuf = xmalloc (info->key_size);
250
251     r_rewind (rfd);
252     return rfd;
253 }
254
255 static void r_close (RSFD rfd)
256 {
257     struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
258     struct rset_mor_rfd **rfdp;
259     int i;
260     
261     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
262         if (*rfdp == rfd)
263         {
264             *rfdp = (*rfdp)->next;
265         
266             heap_close (((struct rset_mor_rfd *) rfd)->ti);
267             for (i = 0; i<info->no_isam_positions; i++)
268                 if (((struct rset_mor_rfd *) rfd)->ispt[i])
269                     isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
270             xfree (((struct rset_mor_rfd *)rfd)->ispt);
271             xfree (((struct rset_mor_rfd *)rfd)->pbuf);
272             xfree (rfd);
273             return;
274         }
275     logf (LOG_FATAL, "r_close but no rfd match!");
276     assert (0);
277 }
278
279 static void r_delete (RSET ct)
280 {
281     struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
282     int i;
283
284     assert (info->rfd_list == NULL);
285     xfree (info->isam_positions);
286
287     for (i = 0; i<ct->no_rset_terms; i++)
288         rset_term_destroy (ct->rset_terms[i]);
289     xfree (ct->rset_terms);
290
291     xfree (info);
292 }
293
294 static void r_rewind (RSFD rfd)
295 {
296 }
297
298
299 static int r_read (RSFD rfd, void *buf, int *term_index)
300 {
301     struct rset_mor_rfd *mrfd = (struct rset_mor_rfd *) rfd;
302     struct trunc_info *ti = mrfd->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         if (mrfd->countp && (
321                 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
322         {
323             memcpy (mrfd->pbuf, buf, ti->keysize);
324             (*mrfd->countp)++;
325         }
326         return 1;
327     }
328     while (1)
329     {
330         if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
331         {
332             heap_delete (ti);
333             break;
334         }
335         if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
336         {
337             heap_delete (ti);
338             heap_insert (ti, ti->tmpbuf, n);
339             break;
340         }
341     }
342     if (mrfd->countp && (
343             *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
344     {
345         memcpy (mrfd->pbuf, buf, ti->keysize);
346         (*mrfd->countp)++;
347     }
348     return 1;
349 }
350
351 static int r_write (RSFD rfd, const void *buf)
352 {
353     logf (LOG_FATAL, "mor set type is read-only");
354     return -1;
355 }