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
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);
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;
94 static void heap_swap (struct trunc_info *ti, int i1, int i2)
99 ti->ptr[i1] = ti->ptr[i2];
103 static void heap_delete (struct trunc_info *ti)
105 int cur = 1, child = 2;
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)
113 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
114 ti->heap[ti->ptr[child]]) > 0)
116 heap_swap (ti, cur, child);
125 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
129 cur = ++(ti->heapnum);
130 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
131 ti->indx[ti->ptr[cur]] = indx;
133 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
134 ti->heap[ti->ptr[cur]]) > 0)
136 heap_swap (ti, cur, parent);
143 struct trunc_info *heap_init (int size, int key_size,
144 int (*cmp)(const void *p1, const void *p2))
146 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
151 ti->keysize = key_size;
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; )
162 ti->heap[i] = ti->buf + ti->keysize * i;
167 static void heap_close (struct trunc_info *ti)
178 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
180 rset_m_or_parms *r_parms = (rset_m_or_parms *) parms;
181 struct rset_mor_info *info;
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;
189 info->no_save_positions = r_parms->no_save_positions;
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;
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;
205 static RSFD r_open (RSET ct, int flag)
207 struct rset_mor_rfd *rfd;
208 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
211 if (flag & RSETF_WRITE)
213 logf (LOG_FATAL, "m_or set type is read-only");
216 rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
218 rfd->next = info->rfd_list;
220 info->rfd_list = rfd;
222 rfd->ispt = (ISAMC_PP *)
223 xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
225 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
227 ct->rset_terms[0]->nn = 0;
228 for (i = 0; i<info->no_isam_positions; i++)
230 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
232 ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
234 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
235 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
238 isc_pp_close (rfd->ispt[i]);
242 rfd->position = info->no_save_positions;
248 static void r_close (RSFD rfd)
250 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
251 struct rset_mor_rfd **rfdp;
254 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
257 *rfdp = (*rfdp)->next;
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);
268 logf (LOG_FATAL, "r_close but no rfd match!");
272 static void r_delete (RSET ct)
274 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
275 assert (info->rfd_list == NULL);
276 xfree (info->isam_positions);
280 static void r_rewind (RSFD rfd)
285 static int r_read (RSFD rfd, void *buf)
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]];
293 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
294 if (((struct rset_mor_rfd *) rfd)->position)
296 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
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);
305 rfd->pbuf = xmalloc (info->key_size);
306 if (mrfd->countp && (
307 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
309 memcpy (mrfd->pbuf, buf, ti->keysize);
316 if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
321 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
324 heap_insert (ti, ti->tmpbuf, n);
328 if (mrfd->countp && (
329 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
331 memcpy (mrfd->pbuf, buf, ti->keysize);
337 static int r_write (RSFD rfd, const void *buf)
339 logf (LOG_FATAL, "mor set type is read-only");