Added skeleton for multior - not at all functional yet. Call to it
[idzebra-moved-to-github.git] / rset / rsmultior.c
1 /* $Id: rsmultior.c,v 1.1 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 <rsmultior.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     "multi-or",
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_multior = &control;
58
59 struct rset_multior_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
72 struct rset_multior_rfd {
73     int flag;
74     int position;
75     int position_max;
76 /*    ISAMC_PP *ispt; */
77     struct rset_mor_rfd *next;
78     struct rset_mor_info *info;
79     struct trunc_info *ti;
80     zint *countp;
81     char *pbuf;
82 };
83
84 #if 0
85 static void heap_swap (struct trunc_info *ti, int i1, int i2)
86 {
87     int swap;
88
89     swap = ti->ptr[i1];
90     ti->ptr[i1] = ti->ptr[i2];
91     ti->ptr[i2] = swap;
92 }
93
94 static void heap_delete (struct trunc_info *ti)
95 {
96     int cur = 1, child = 2;
97
98     heap_swap (ti, 1, ti->heapnum--);
99     while (child <= ti->heapnum) {
100         if (child < ti->heapnum &&
101             (*ti->cmp)(ti->heap[ti->ptr[child]],
102                        ti->heap[ti->ptr[1+child]]) > 0)
103             child++;
104         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
105                        ti->heap[ti->ptr[child]]) > 0)
106         {
107             heap_swap (ti, cur, child);
108             cur = child;
109             child = 2*cur;
110         }
111         else
112             break;
113     }
114 }
115
116 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
117 {
118     int cur, parent;
119
120     cur = ++(ti->heapnum);
121     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
122     ti->indx[ti->ptr[cur]] = indx;
123     parent = cur/2;
124     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
125                                 ti->heap[ti->ptr[cur]]) > 0)
126     {
127         heap_swap (ti, cur, parent);
128         cur = parent;
129         parent = cur/2;
130     }
131 }
132
133 static
134 struct trunc_info *heap_init (int size, int key_size,
135                               int (*cmp)(const void *p1, const void *p2))
136 {
137     struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
138     int i;
139
140     ++size;
141     ti->heapnum = 0;
142     ti->keysize = key_size;
143     ti->cmp = cmp;
144     ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
145     ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
146     ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
147     ti->swapbuf = (char *) xmalloc (ti->keysize);
148     ti->tmpbuf = (char *) xmalloc (ti->keysize);
149     ti->buf = (char *) xmalloc (size * ti->keysize);
150     for (i = size; --i >= 0; )
151     {
152         ti->ptr[i] = i;
153         ti->heap[i] = ti->buf + ti->keysize * i;
154     }
155     return ti;
156 }
157
158 static void heap_close (struct trunc_info *ti)
159 {
160     xfree (ti->buf);
161     xfree (ti->ptr);
162     xfree (ti->indx);
163     xfree (ti->heap);
164     xfree (ti->swapbuf);
165     xfree (ti->tmpbuf);
166     xfree (ti);
167 }
168
169 #endif
170 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
171 {
172     return 0;
173 /*        
174     rset_multior_parms *r_parms = (rset_multior_parms *) parms;
175     struct rset_mor_info *info;
176
177     ct->flags |= RSET_FLAG_VOLATILE;
178     info = (struct rset_mor_info *) xmalloc (sizeof(*info));
179     info->key_size = r_parms->key_size;
180     assert (info->key_size > 1);
181     info->cmp = r_parms->cmp;
182     
183     info->no_save_positions = r_parms->no_save_positions;
184
185     info->isc = r_parms->isc;
186     info->no_isam_positions = r_parms->no_isam_positions;
187     info->isam_positions = (ISAMC_P *)
188         xmalloc (sizeof(*info->isam_positions) * info->no_isam_positions);
189     memcpy (info->isam_positions, r_parms->isam_positions,
190             sizeof(*info->isam_positions) * info->no_isam_positions);
191     info->rfd_list = NULL;
192
193     ct->no_rset_terms = 1;
194     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
195     ct->rset_terms[0] = r_parms->rset_term;
196     return info;
197    */
198 }
199
200 static RSFD r_open (RSET ct, int flag)
201 {
202         return 0;
203         /*
204     struct rset_mor_rfd *rfd;
205     struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
206     int i;
207
208     if (flag & RSETF_WRITE)
209     {
210         logf (LOG_FATAL, "m_or set type is read-only");
211         return NULL;
212     }
213     rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
214     rfd->flag = flag;
215     rfd->next = info->rfd_list;
216     rfd->info = info;
217     info->rfd_list = rfd;
218
219     rfd->ispt = (ISAMC_PP *)
220         xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
221         
222     rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
223
224     ct->rset_terms[0]->nn = 0;
225     for (i = 0; i<info->no_isam_positions; i++)
226     {
227         rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
228         
229         ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
230
231         if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
232             heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
233         else
234         {
235             isc_pp_close (rfd->ispt[i]);
236             rfd->ispt[i] = NULL;
237         }
238     }
239     rfd->position = info->no_save_positions;
240
241     if (ct->no_rset_terms == 1)
242         rfd->countp = &ct->rset_terms[0]->count;
243     else
244         rfd->countp = 0;
245     rfd->pbuf = xmalloc (info->key_size);
246
247     r_rewind (rfd);
248     return rfd;
249     */
250 }
251
252 static void r_close (RSFD rfd)
253 {
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 (((struct rset_mor_rfd *)rfd)->pbuf);
270             xfree (rfd);
271             return;
272         }
273     logf (LOG_FATAL, "r_close but no rfd match!");
274     assert (0);
275     */
276 }
277
278 static void r_delete (RSET ct)
279 {
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     */ /* FIXME - Watch out! */
290         /*
291     xfree (ct->rset_terms);
292
293     xfree (info);
294     */
295 }
296
297 static void r_rewind (RSFD rfd)
298 {
299 }
300
301
302 static int r_read (RSFD rfd, void *buf, int *term_index)
303 {
304         return 0;
305         /*
306     struct rset_mor_rfd *mrfd = (struct rset_mor_rfd *) rfd;
307     struct trunc_info *ti = mrfd->ti;
308     int n = ti->indx[ti->ptr[1]];
309
310     if (!ti->heapnum)
311         return 0;
312     *term_index = 0;
313     memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
314     if (((struct rset_mor_rfd *) rfd)->position)
315     {
316         if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
317         {
318             heap_delete (ti);
319             if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
320                  ((struct rset_mor_rfd *) rfd)->position--;
321             heap_insert (ti, ti->tmpbuf, n);
322         }
323         else
324             heap_delete (ti);
325         if (mrfd->countp && (
326                 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
327         {
328             memcpy (mrfd->pbuf, buf, ti->keysize);
329             (*mrfd->countp)++;
330         }
331         return 1;
332     }
333     while (1)
334     {
335         if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
336         {
337             heap_delete (ti);
338             break;
339         }
340         if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
341         {
342             heap_delete (ti);
343             heap_insert (ti, ti->tmpbuf, n);
344             break;
345         }
346     }
347     if (mrfd->countp && (
348             *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
349     {
350         memcpy (mrfd->pbuf, buf, ti->keysize);
351         (*mrfd->countp)++;
352     }
353     return 1;
354     */
355 }
356
357 static int r_write (RSFD rfd, const void *buf)
358 {
359     logf (LOG_FATAL, "mor set type is read-only");
360     return -1;
361 }