1 /* $Id: rsm_or.c,v 1.18 2004-08-06 13:14:47 adam 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
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_m_or = &control;
59 struct rset_mor_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;
76 int (*cmp)(const void *p1, const void *p2);
88 struct rset_mor_rfd *next;
89 struct rset_mor_info *info;
90 struct trunc_info *ti;
95 static void heap_swap (struct trunc_info *ti, int i1, int i2)
100 ti->ptr[i1] = ti->ptr[i2];
104 static void heap_delete (struct trunc_info *ti)
106 int cur = 1, child = 2;
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)
114 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
115 ti->heap[ti->ptr[child]]) > 0)
117 heap_swap (ti, cur, child);
126 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
130 cur = ++(ti->heapnum);
131 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
132 ti->indx[ti->ptr[cur]] = indx;
134 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
135 ti->heap[ti->ptr[cur]]) > 0)
137 heap_swap (ti, cur, parent);
144 struct trunc_info *heap_init (int size, int key_size,
145 int (*cmp)(const void *p1, const void *p2))
147 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
152 ti->keysize = key_size;
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; )
163 ti->heap[i] = ti->buf + ti->keysize * i;
168 static void heap_close (struct trunc_info *ti)
179 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
181 rset_m_or_parms *r_parms = (rset_m_or_parms *) parms;
182 struct rset_mor_info *info;
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;
190 info->no_save_positions = r_parms->no_save_positions;
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;
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;
206 static RSFD r_open (RSET ct, int flag)
208 struct rset_mor_rfd *rfd;
209 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
212 if (flag & RSETF_WRITE)
214 logf (LOG_FATAL, "m_or set type is read-only");
217 rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
219 rfd->next = info->rfd_list;
221 info->rfd_list = rfd;
223 rfd->ispt = (ISAMC_PP *)
224 xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
226 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
228 ct->rset_terms[0]->nn = 0;
229 for (i = 0; i<info->no_isam_positions; i++)
231 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
233 ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
235 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
236 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
239 isc_pp_close (rfd->ispt[i]);
243 rfd->position = info->no_save_positions;
245 if (ct->no_rset_terms == 1)
246 rfd->countp = &ct->rset_terms[0]->count;
249 rfd->pbuf = xmalloc (info->key_size);
255 static void r_close (RSFD rfd)
257 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
258 struct rset_mor_rfd **rfdp;
261 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
264 *rfdp = (*rfdp)->next;
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);
275 logf (LOG_FATAL, "r_close but no rfd match!");
279 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 xfree (ct->rset_terms);
294 static void r_rewind (RSFD rfd)
299 static int r_read (RSFD rfd, void *buf, int *term_index)
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]];
308 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
309 if (((struct rset_mor_rfd *) rfd)->position)
311 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
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);
320 if (mrfd->countp && (
321 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
323 memcpy (mrfd->pbuf, buf, ti->keysize);
330 if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
335 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
338 heap_insert (ti, ti->tmpbuf, n);
342 if (mrfd->countp && (
343 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
345 memcpy (mrfd->pbuf, buf, ti->keysize);
351 static int r_write (RSFD rfd, const void *buf)
353 logf (LOG_FATAL, "mor set type is read-only");