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
5 This file is part of the Zebra server.
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
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
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
33 #include <rsmultior.h>
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);
43 static const struct rset_control control =
51 rset_default_forward, /* FIXME */
52 rset_default_pos, /* FIXME */
57 const struct rset_control *rset_kind_multior = &control;
59 struct rset_multior_info {
62 int (*cmp)(const void *p1, const void *p2);
64 ISAMC_P *isam_positions;
66 int no_isam_positions;
67 int no_save_positions;
68 struct rset_mor_rfd *rfd_list;
72 struct rset_multior_rfd {
77 struct rset_mor_rfd *next;
78 struct rset_mor_info *info;
79 struct trunc_info *ti;
85 static void heap_swap (struct trunc_info *ti, int i1, int i2)
90 ti->ptr[i1] = ti->ptr[i2];
94 static void heap_delete (struct trunc_info *ti)
96 int cur = 1, child = 2;
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)
104 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
105 ti->heap[ti->ptr[child]]) > 0)
107 heap_swap (ti, cur, child);
116 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
120 cur = ++(ti->heapnum);
121 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
122 ti->indx[ti->ptr[cur]] = indx;
124 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
125 ti->heap[ti->ptr[cur]]) > 0)
127 heap_swap (ti, cur, parent);
134 struct trunc_info *heap_init (int size, int key_size,
135 int (*cmp)(const void *p1, const void *p2))
137 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
142 ti->keysize = key_size;
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; )
153 ti->heap[i] = ti->buf + ti->keysize * i;
158 static void heap_close (struct trunc_info *ti)
170 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
174 rset_multior_parms *r_parms = (rset_multior_parms *) parms;
175 struct rset_mor_info *info;
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;
183 info->no_save_positions = r_parms->no_save_positions;
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;
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;
200 static RSFD r_open (RSET ct, int flag)
204 struct rset_mor_rfd *rfd;
205 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
208 if (flag & RSETF_WRITE)
210 logf (LOG_FATAL, "m_or set type is read-only");
213 rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
215 rfd->next = info->rfd_list;
217 info->rfd_list = rfd;
219 rfd->ispt = (ISAMC_PP *)
220 xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
222 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
224 ct->rset_terms[0]->nn = 0;
225 for (i = 0; i<info->no_isam_positions; i++)
227 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
229 ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
231 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
232 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
235 isc_pp_close (rfd->ispt[i]);
239 rfd->position = info->no_save_positions;
241 if (ct->no_rset_terms == 1)
242 rfd->countp = &ct->rset_terms[0]->count;
245 rfd->pbuf = xmalloc (info->key_size);
252 static void r_close (RSFD rfd)
255 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
256 struct rset_mor_rfd **rfdp;
259 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
262 *rfdp = (*rfdp)->next;
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);
273 logf (LOG_FATAL, "r_close but no rfd match!");
278 static void r_delete (RSET ct)
281 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
284 assert (info->rfd_list == NULL);
285 xfree (info->isam_positions);
287 for (i = 0; i<ct->no_rset_terms; i++)
288 rset_term_destroy (ct->rset_terms[i]);
289 */ /* FIXME - Watch out! */
291 xfree (ct->rset_terms);
297 static void r_rewind (RSFD rfd)
302 static int r_read (RSFD rfd, void *buf, int *term_index)
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]];
313 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
314 if (((struct rset_mor_rfd *) rfd)->position)
316 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
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);
325 if (mrfd->countp && (
326 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
328 memcpy (mrfd->pbuf, buf, ti->keysize);
335 if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
340 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
343 heap_insert (ti, ti->tmpbuf, n);
347 if (mrfd->countp && (
348 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
350 memcpy (mrfd->pbuf, buf, ti->keysize);
357 static int r_write (RSFD rfd, const void *buf)
359 logf (LOG_FATAL, "mor set type is read-only");