1 /* $Id: rsm_or.c,v 1.17 2004-08-06 12:55:03 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
36 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
37 static RSFD r_open (RSET ct, int flag);
38 static void r_close (RSFD rfd);
39 static void r_delete (RSET ct);
40 static void r_rewind (RSFD rfd);
41 static int r_read (RSFD rfd, void *buf, int *term_index);
42 static int r_write (RSFD rfd, const void *buf);
44 static const struct rset_control control =
52 rset_default_forward, /* FIXME */
53 rset_default_pos, /* FIXME */
58 const struct rset_control *rset_kind_m_or = &control;
60 struct rset_mor_info {
63 int (*cmp)(const void *p1, const void *p2);
65 ISAM_P *isam_positions;
67 int no_isam_positions;
68 int no_save_positions;
69 struct rset_mor_rfd *rfd_list;
77 int (*cmp)(const void *p1, const void *p2);
89 struct rset_mor_rfd *next;
90 struct rset_mor_info *info;
91 struct trunc_info *ti;
96 static void heap_swap (struct trunc_info *ti, int i1, int i2)
101 ti->ptr[i1] = ti->ptr[i2];
105 static void heap_delete (struct trunc_info *ti)
107 int cur = 1, child = 2;
109 heap_swap (ti, 1, ti->heapnum--);
110 while (child <= ti->heapnum) {
111 if (child < ti->heapnum &&
112 (*ti->cmp)(ti->heap[ti->ptr[child]],
113 ti->heap[ti->ptr[1+child]]) > 0)
115 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
116 ti->heap[ti->ptr[child]]) > 0)
118 heap_swap (ti, cur, child);
127 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
131 cur = ++(ti->heapnum);
132 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
133 ti->indx[ti->ptr[cur]] = indx;
135 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
136 ti->heap[ti->ptr[cur]]) > 0)
138 heap_swap (ti, cur, parent);
145 struct trunc_info *heap_init (int size, int key_size,
146 int (*cmp)(const void *p1, const void *p2))
148 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
153 ti->keysize = key_size;
155 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
156 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
157 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
158 ti->swapbuf = (char *) xmalloc (ti->keysize);
159 ti->tmpbuf = (char *) xmalloc (ti->keysize);
160 ti->buf = (char *) xmalloc (size * ti->keysize);
161 for (i = size; --i >= 0; )
164 ti->heap[i] = ti->buf + ti->keysize * i;
169 static void heap_close (struct trunc_info *ti)
180 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
182 rset_m_or_parms *r_parms = (rset_m_or_parms *) parms;
183 struct rset_mor_info *info;
185 ct->flags |= RSET_FLAG_VOLATILE;
186 info = (struct rset_mor_info *) xmalloc (sizeof(*info));
187 info->key_size = r_parms->key_size;
188 assert (info->key_size > 1);
189 info->cmp = r_parms->cmp;
191 info->no_save_positions = r_parms->no_save_positions;
193 info->isc = r_parms->isc;
194 info->no_isam_positions = r_parms->no_isam_positions;
195 info->isam_positions = (ISAM_P *)
196 xmalloc (sizeof(*info->isam_positions) * info->no_isam_positions);
197 memcpy (info->isam_positions, r_parms->isam_positions,
198 sizeof(*info->isam_positions) * info->no_isam_positions);
199 info->rfd_list = NULL;
201 ct->no_rset_terms = 1;
202 ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
203 ct->rset_terms[0] = r_parms->rset_term;
207 static RSFD r_open (RSET ct, int flag)
209 struct rset_mor_rfd *rfd;
210 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
213 if (flag & RSETF_WRITE)
215 logf (LOG_FATAL, "m_or set type is read-only");
218 rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
220 rfd->next = info->rfd_list;
222 info->rfd_list = rfd;
224 rfd->ispt = (ISAMC_PP *)
225 xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
227 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
229 ct->rset_terms[0]->nn = 0;
230 for (i = 0; i<info->no_isam_positions; i++)
232 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
234 ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
236 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
237 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
240 isc_pp_close (rfd->ispt[i]);
244 rfd->position = info->no_save_positions;
246 if (ct->no_rset_terms == 1)
247 rfd->countp = &ct->rset_terms[0]->count;
250 rfd->pbuf = xmalloc (info->key_size);
256 static void r_close (RSFD rfd)
258 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
259 struct rset_mor_rfd **rfdp;
262 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
265 *rfdp = (*rfdp)->next;
267 heap_close (((struct rset_mor_rfd *) rfd)->ti);
268 for (i = 0; i<info->no_isam_positions; i++)
269 if (((struct rset_mor_rfd *) rfd)->ispt[i])
270 isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
271 xfree (((struct rset_mor_rfd *)rfd)->ispt);
272 xfree (((struct rset_mor_rfd *)rfd)->pbuf);
276 logf (LOG_FATAL, "r_close but no rfd match!");
280 static void r_delete (RSET ct)
282 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
285 assert (info->rfd_list == NULL);
286 xfree (info->isam_positions);
288 for (i = 0; i<ct->no_rset_terms; i++)
289 rset_term_destroy (ct->rset_terms[i]);
290 xfree (ct->rset_terms);
295 static void r_rewind (RSFD rfd)
300 static int r_read (RSFD rfd, void *buf, int *term_index)
302 struct rset_mor_rfd *mrfd = (struct rset_mor_rfd *) rfd;
303 struct trunc_info *ti = mrfd->ti;
304 int n = ti->indx[ti->ptr[1]];
309 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
310 if (((struct rset_mor_rfd *) rfd)->position)
312 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
315 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
316 ((struct rset_mor_rfd *) rfd)->position--;
317 heap_insert (ti, ti->tmpbuf, n);
321 if (mrfd->countp && (
322 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
324 memcpy (mrfd->pbuf, buf, ti->keysize);
331 if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
336 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
339 heap_insert (ti, ti->tmpbuf, n);
343 if (mrfd->countp && (
344 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
346 memcpy (mrfd->pbuf, buf, ti->keysize);
352 static int r_write (RSFD rfd, const void *buf)
354 logf (LOG_FATAL, "mor set type is read-only");