Move headers zebraapi.h->idzebra/api.h, zebraver.h->idzebra/version.h,
[idzebra-moved-to-github.git] / rset / rsm_or.c
1 /* $Id: rsm_or.c,v 1.20 2004-08-20 14:44:46 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);
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     char *pbuf;
92 };
93
94 static void heap_swap (struct trunc_info *ti, int i1, int i2)
95 {
96     int swap;
97
98     swap = ti->ptr[i1];
99     ti->ptr[i1] = ti->ptr[i2];
100     ti->ptr[i2] = swap;
101 }
102
103 static void heap_delete (struct trunc_info *ti)
104 {
105     int cur = 1, child = 2;
106
107     heap_swap (ti, 1, ti->heapnum--);
108     while (child <= ti->heapnum) {
109         if (child < ti->heapnum &&
110             (*ti->cmp)(ti->heap[ti->ptr[child]],
111                        ti->heap[ti->ptr[1+child]]) > 0)
112             child++;
113         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
114                        ti->heap[ti->ptr[child]]) > 0)
115         {
116             heap_swap (ti, cur, child);
117             cur = child;
118             child = 2*cur;
119         }
120         else
121             break;
122     }
123 }
124
125 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
126 {
127     int cur, parent;
128
129     cur = ++(ti->heapnum);
130     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
131     ti->indx[ti->ptr[cur]] = indx;
132     parent = cur/2;
133     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
134                                 ti->heap[ti->ptr[cur]]) > 0)
135     {
136         heap_swap (ti, cur, parent);
137         cur = parent;
138         parent = cur/2;
139     }
140 }
141
142 static
143 struct trunc_info *heap_init (int size, int key_size,
144                               int (*cmp)(const void *p1, const void *p2))
145 {
146     struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
147     int i;
148
149     ++size;
150     ti->heapnum = 0;
151     ti->keysize = key_size;
152     ti->cmp = cmp;
153     ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
154     ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
155     ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
156     ti->swapbuf = (char *) xmalloc (ti->keysize);
157     ti->tmpbuf = (char *) xmalloc (ti->keysize);
158     ti->buf = (char *) xmalloc (size * ti->keysize);
159     for (i = size; --i >= 0; )
160     {
161         ti->ptr[i] = i;
162         ti->heap[i] = ti->buf + ti->keysize * i;
163     }
164     return ti;
165 }
166
167 static void heap_close (struct trunc_info *ti)
168 {
169     xfree (ti->buf);
170     xfree (ti->ptr);
171     xfree (ti->indx);
172     xfree (ti->heap);
173     xfree (ti->swapbuf);
174     xfree (ti->tmpbuf);
175     xfree (ti);
176 }
177
178 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
179 {
180     rset_m_or_parms *r_parms = (rset_m_or_parms *) parms;
181     struct rset_mor_info *info;
182
183     ct->flags |= RSET_FLAG_VOLATILE;
184     info = (struct rset_mor_info *) xmalloc (sizeof(*info));
185     info->key_size = r_parms->key_size;
186     assert (info->key_size > 1);
187     info->cmp = r_parms->cmp;
188     
189     info->no_save_positions = r_parms->no_save_positions;
190
191     info->isc = r_parms->isc;
192     info->no_isam_positions = r_parms->no_isam_positions;
193     info->isam_positions = (ISAMC_P *)
194         xmalloc (sizeof(*info->isam_positions) * info->no_isam_positions);
195     memcpy (info->isam_positions, r_parms->isam_positions,
196             sizeof(*info->isam_positions) * info->no_isam_positions);
197     info->rfd_list = NULL;
198
199     ct->no_rset_terms = 1;
200     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
201     ct->rset_terms[0] = r_parms->rset_term;
202     return info;
203 }
204
205 static RSFD r_open (RSET ct, int flag)
206 {
207     struct rset_mor_rfd *rfd;
208     struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
209     int i;
210
211     if (flag & RSETF_WRITE)
212     {
213         logf (LOG_FATAL, "m_or set type is read-only");
214         return NULL;
215     }
216     rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
217     rfd->flag = flag;
218     rfd->next = info->rfd_list;
219     rfd->info = info;
220     info->rfd_list = rfd;
221
222     rfd->ispt = (ISAMC_PP *)
223         xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
224         
225     rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
226
227     ct->rset_terms[0]->nn = 0;
228     for (i = 0; i<info->no_isam_positions; i++)
229     {
230         rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
231         
232         ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
233
234         if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
235             heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
236         else
237         {
238             isc_pp_close (rfd->ispt[i]);
239             rfd->ispt[i] = NULL;
240         }
241     }
242     rfd->position = info->no_save_positions;
243     rfd->pbuf = 0;
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 (((struct rset_mor_rfd *)rfd)->pbuf);
265             xfree (rfd);
266             return;
267         }
268     logf (LOG_FATAL, "r_close but no rfd match!");
269     assert (0);
270 }
271
272 static void r_delete (RSET ct)
273 {
274     struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
275     assert (info->rfd_list == NULL);
276     xfree (info->isam_positions);
277     xfree (info);
278 }
279
280 static void r_rewind (RSFD rfd)
281 {
282 }
283
284
285 static int r_read (RSFD rfd, void *buf)
286 {
287     struct rset_mor_rfd *mrfd = (struct rset_mor_rfd *) rfd;
288     struct trunc_info *ti = mrfd->ti;
289     int n = ti->indx[ti->ptr[1]];
290
291     if (!ti->heapnum)
292         return 0;
293     memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
294     if (((struct rset_mor_rfd *) rfd)->position)
295     {
296         if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
297         {
298             heap_delete (ti);
299             if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
300                  ((struct rset_mor_rfd *) rfd)->position--;
301             heap_insert (ti, ti->tmpbuf, n);
302         }
303         else
304             heap_delete (ti);
305     rfd->pbuf = xmalloc (info->key_size);
306         if (mrfd->countp && (
307                 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
308         {
309             memcpy (mrfd->pbuf, buf, ti->keysize);
310             (*mrfd->countp)++;
311         }
312         return 1;
313     }
314     while (1)
315     {
316         if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
317         {
318             heap_delete (ti);
319             break;
320         }
321         if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
322         {
323             heap_delete (ti);
324             heap_insert (ti, ti->tmpbuf, n);
325             break;
326         }
327     }
328     if (mrfd->countp && (
329             *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
330     {
331         memcpy (mrfd->pbuf, buf, ti->keysize);
332         (*mrfd->countp)++;
333     }
334     return 1;
335 }
336
337 static int r_write (RSFD rfd, const void *buf)
338 {
339     logf (LOG_FATAL, "mor set type is read-only");
340     return -1;
341 }