1 /* $Id: rsm_or.c,v 1.15 2004-08-03 12:15:45 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
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_count (RSET ct); */
42 static int r_read (RSFD rfd, void *buf, int *term_index);
43 static int r_write (RSFD rfd, const void *buf);
45 static const struct rset_control control =
59 const struct rset_control *rset_kind_m_or = &control;
61 struct rset_mor_info {
64 int (*cmp)(const void *p1, const void *p2);
66 ISAM_P *isam_positions;
68 int no_isam_positions;
69 int no_save_positions;
70 struct rset_mor_rfd *rfd_list;
78 int (*cmp)(const void *p1, const void *p2);
90 struct rset_mor_rfd *next;
91 struct rset_mor_info *info;
92 struct trunc_info *ti;
97 static void heap_swap (struct trunc_info *ti, int i1, int i2)
102 ti->ptr[i1] = ti->ptr[i2];
106 static void heap_delete (struct trunc_info *ti)
108 int cur = 1, child = 2;
110 heap_swap (ti, 1, ti->heapnum--);
111 while (child <= ti->heapnum) {
112 if (child < ti->heapnum &&
113 (*ti->cmp)(ti->heap[ti->ptr[child]],
114 ti->heap[ti->ptr[1+child]]) > 0)
116 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
117 ti->heap[ti->ptr[child]]) > 0)
119 heap_swap (ti, cur, child);
128 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
132 cur = ++(ti->heapnum);
133 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
134 ti->indx[ti->ptr[cur]] = indx;
136 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
137 ti->heap[ti->ptr[cur]]) > 0)
139 heap_swap (ti, cur, parent);
146 struct trunc_info *heap_init (int size, int key_size,
147 int (*cmp)(const void *p1, const void *p2))
149 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
154 ti->keysize = key_size;
156 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
157 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
158 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
159 ti->swapbuf = (char *) xmalloc (ti->keysize);
160 ti->tmpbuf = (char *) xmalloc (ti->keysize);
161 ti->buf = (char *) xmalloc (size * ti->keysize);
162 for (i = size; --i >= 0; )
165 ti->heap[i] = ti->buf + ti->keysize * i;
170 static void heap_close (struct trunc_info *ti)
181 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
183 rset_m_or_parms *r_parms = (rset_m_or_parms *) parms;
184 struct rset_mor_info *info;
186 ct->flags |= RSET_FLAG_VOLATILE;
187 info = (struct rset_mor_info *) xmalloc (sizeof(*info));
188 info->key_size = r_parms->key_size;
189 assert (info->key_size > 1);
190 info->cmp = r_parms->cmp;
192 info->no_save_positions = r_parms->no_save_positions;
194 info->isc = r_parms->isc;
195 info->no_isam_positions = r_parms->no_isam_positions;
196 info->isam_positions = (ISAM_P *)
197 xmalloc (sizeof(*info->isam_positions) * info->no_isam_positions);
198 memcpy (info->isam_positions, r_parms->isam_positions,
199 sizeof(*info->isam_positions) * info->no_isam_positions);
200 info->rfd_list = NULL;
202 ct->no_rset_terms = 1;
203 ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
204 ct->rset_terms[0] = r_parms->rset_term;
208 static RSFD r_open (RSET ct, int flag)
210 struct rset_mor_rfd *rfd;
211 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
214 if (flag & RSETF_WRITE)
216 logf (LOG_FATAL, "m_or set type is read-only");
219 rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
221 rfd->next = info->rfd_list;
223 info->rfd_list = rfd;
225 rfd->ispt = (ISAMC_PP *)
226 xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
228 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
230 ct->rset_terms[0]->nn = 0;
231 for (i = 0; i<info->no_isam_positions; i++)
233 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
235 ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
237 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
238 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
241 isc_pp_close (rfd->ispt[i]);
245 rfd->position = info->no_save_positions;
247 if (ct->no_rset_terms == 1)
248 rfd->countp = &ct->rset_terms[0]->count;
251 rfd->pbuf = xmalloc (info->key_size);
257 static void r_close (RSFD rfd)
259 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
260 struct rset_mor_rfd **rfdp;
263 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
266 *rfdp = (*rfdp)->next;
268 heap_close (((struct rset_mor_rfd *) rfd)->ti);
269 for (i = 0; i<info->no_isam_positions; i++)
270 if (((struct rset_mor_rfd *) rfd)->ispt[i])
271 isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
272 xfree (((struct rset_mor_rfd *)rfd)->ispt);
273 xfree (((struct rset_mor_rfd *)rfd)->pbuf);
277 logf (LOG_FATAL, "r_close but no rfd match!");
281 static void r_delete (RSET ct)
283 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
286 assert (info->rfd_list == NULL);
287 xfree (info->isam_positions);
289 for (i = 0; i<ct->no_rset_terms; i++)
290 rset_term_destroy (ct->rset_terms[i]);
291 xfree (ct->rset_terms);
296 static void r_rewind (RSFD rfd)
301 static int r_count (RSET ct)
307 static int r_read (RSFD rfd, void *buf, int *term_index)
309 struct rset_mor_rfd *mrfd = (struct rset_mor_rfd *) rfd;
310 struct trunc_info *ti = mrfd->ti;
311 int n = ti->indx[ti->ptr[1]];
316 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
317 if (((struct rset_mor_rfd *) rfd)->position)
319 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
322 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
323 ((struct rset_mor_rfd *) rfd)->position--;
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);
338 if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
343 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
346 heap_insert (ti, ti->tmpbuf, n);
350 if (mrfd->countp && (
351 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
353 memcpy (mrfd->pbuf, buf, ti->keysize);
359 static int r_write (RSFD rfd, const void *buf)
361 logf (LOG_FATAL, "mor set type is read-only");