1 /* $Id: rsmultior.c,v 1.3 2004-08-19 14:11:54 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);
42 static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index,
43 int (*cmpfunc)(const void *p1, const void *p2),
44 const void *untilbuf);
45 static void r_pos (RSFD rfd, double *current, double *total);
47 static const struct rset_control control =
61 const struct rset_control *rset_kind_multior = &control;
63 /* The heap structure:
64 * The rset contains a list or rsets we are ORing together
65 * The rfd contains a heap of heap-items, which contain
66 * a rfd opened to those rsets, and a buffer for one key.
67 * They also contain a ptr to the rset list in the rset
68 * itself, for practical reasons.
81 int (*cmp)(const void *p1, const void *p2);
82 struct heap_item **heap; /* ptrs to the rfd */
84 typedef struct heap *HEAP;
87 struct rset_multior_info {
90 int (*cmp)(const void *p1, const void *p2);
94 struct rset_multior_rfd *rfd_list;
98 struct rset_multior_rfd {
100 struct heap_item *items; /* we alloc and free them here */
102 struct rset_multior_rfd *next;
103 struct rset_multior_info *info;
104 zint *countp; /* inc at every new record we see */
105 zint hits; /* returned so far */
106 char *prevvalue; /* to see if we are in another record */
110 static void heap_dump_item( HEAP h, int i, int level) {
114 (void)rset_pos(h->heap[i]->rset,h->heap[i]->fd, &cur, &tot);
115 logf(LOG_LOG," %d %*s i=%p buf=%p %0.1f/%0.1f",i, level, "",
116 &(h->heap[i]), h->heap[i]->buf, cur,tot );
117 heap_dump_item(h, 2*i, level+1);
118 heap_dump_item(h, 2*i+1, level+1);
120 static void heap_dump( HEAP h,char *msg) {
121 logf(LOG_LOG, "heap dump: %s num=%d max=%d",msg, h->heapnum, h->heapmax);
122 heap_dump_item(h,1,1);
126 static void heap_swap (HEAP h, int x, int y)
128 struct heap_item *swap;
130 h->heap[x]=h->heap[y];
134 static int heap_cmp(HEAP h, int x, int y)
136 return (*h->cmp)(h->heap[x]->buf,h->heap[y]->buf);
139 static int heap_empty(HEAP h)
141 return ( 0==h->heapnum );
144 static void heap_delete (HEAP h)
145 { /* deletes the first item in the heap, and balances the rest */
146 int cur = 1, child = 2;
147 h->heap[1]=0; /* been deleted */
148 heap_swap (h, 1, h->heapnum--);
149 while (child <= h->heapnum) {
150 if (child < h->heapnum && heap_cmp(h,child,1+child)>0 )
152 if (heap_cmp(h,cur,child) > 0)
154 heap_swap (h, cur, child);
163 static void heap_balance (HEAP h)
164 { /* The heap root element has changed value (to bigger) */
165 /* swap downwards until the heap is ordered again */
166 int cur = 1, child = 2;
167 while (child <= h->heapnum) {
168 if (child < h->heapnum && heap_cmp(h,child,1+child)>0 )
170 if (heap_cmp(h,cur,child) > 0)
172 heap_swap (h, cur, child);
182 static void heap_insert (HEAP h, struct heap_item *hi)
186 cur = ++(h->heapnum);
187 assert(cur <= h->heapmax);
190 while (parent && (heap_cmp(h,parent,cur) > 0))
193 heap_swap (h, cur, parent);
201 HEAP heap_create (int size, int key_size,
202 int (*cmp)(const void *p1, const void *p2))
204 HEAP h = (HEAP) xmalloc (sizeof(*h));
206 ++size; /* heap array starts at 1 */
209 h->keysize = key_size;
211 h->heap = (struct heap_item**) xmalloc((size)*sizeof(*h->heap));
212 h->heap[0]=0; /* not used */
216 static void heap_destroy (HEAP h)
218 xfree (h->heap); /* safe, they all point to the rfd */
223 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
225 rset_multior_parms *r_parms = (rset_multior_parms *) parms;
226 struct rset_multior_info *info;
228 ct->flags |= RSET_FLAG_VOLATILE;
229 /* FIXME - Remove the whole flags thing, from all rsets */
230 info = (struct rset_multior_info *) xmalloc (sizeof(*info));
231 info->key_size = r_parms->key_size;
232 assert (info->key_size > 1);
233 info->cmp = r_parms->cmp;
234 info->no_rsets= r_parms->no_rsets;
235 info->rsets=r_parms->rsets; /* now we own it! */
237 info->term_index=0 ; /* r_parms->rset_term; */ /*??*/ /*FIXME */
238 ct->no_rset_terms = 1;
239 ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
240 ct->rset_terms[0] = r_parms->rset_term;
244 static RSFD r_open (RSET ct, int flag)
246 struct rset_multior_rfd *rfd;
247 struct rset_multior_info *info = (struct rset_multior_info *) ct->buf;
251 if (flag & RSETF_WRITE)
253 logf (LOG_FATAL, "multior set type is read-only");
256 rfd = (struct rset_multior_rfd *) xmalloc (sizeof(*rfd));
258 rfd->next = info->rfd_list;
260 info->rfd_list = rfd;
261 if (ct->no_rset_terms==1)
262 rfd->countp=&ct->rset_terms[0]->count;
265 rfd->h = heap_create( info->no_rsets, info->key_size, info->cmp);
268 rfd->items=(struct heap_item *) xmalloc(info->no_rsets*sizeof(*rfd->items));
269 for (i=0; i<info->no_rsets; i++){
270 rfd->items[i].rset=info->rsets[i];
271 rfd->items[i].buf=xmalloc(info->key_size);
272 rfd->items[i].fd=rset_open(info->rsets[i],RSETF_READ);
273 /* if (item_readbuf(&(rfd->items[i]))) */
274 if ( rset_read(rfd->items[i].rset, rfd->items[i].fd,
275 rfd->items[i].buf, &dummy_termindex) )
276 heap_insert(rfd->h, &(rfd->items[i]));
281 static void r_close (RSFD rfd)
283 struct rset_multior_rfd *mrfd = (struct rset_multior_rfd *) rfd;
284 struct rset_multior_info *info = mrfd->info;
285 struct rset_multior_rfd **rfdp;
288 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
291 *rfdp = (*rfdp)->next;
293 heap_destroy (mrfd->h);
294 for (i = 0; i<info->no_rsets; i++) {
295 if (mrfd->items[i].fd)
296 rset_close(info->rsets[i],mrfd->items[i].fd);
297 xfree(mrfd->items[i].buf);
301 xfree(mrfd->prevvalue);
305 logf (LOG_FATAL, "r_close but no rfd match!");
309 static void r_delete (RSET ct)
311 struct rset_multior_info *info = (struct rset_multior_info *) ct->buf;
314 assert (info->rfd_list == NULL);
315 for(i=0;i<info->no_rsets;i++)
316 rset_delete(info->rsets[i]);
320 for (i = 0; i<ct->no_rset_terms; i++) /* usually only 1 */
321 rset_term_destroy (ct->rset_terms[i]);
322 xfree (ct->rset_terms);
325 static void r_rewind (RSFD rfd)
327 assert(!"rewind not implemented yet");
331 static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index,
332 int (*cmpfunc)(const void *p1, const void *p2),
333 const void *untilbuf)
335 struct rset_multior_rfd *mrfd = (struct rset_multior_rfd *) rfd;
336 struct rset_multior_info *info = mrfd->info;
340 if (heap_empty(mrfd->h))
343 assert(cmpfunc==mrfd->info->cmp);
345 it = *(mrfd->h->heap[1]);
346 memcpy(buf,it.buf, info->key_size);
349 if (mrfd->prevvalue) { /* in another record */
350 if ( (*mrfd->info->cmp)(mrfd->prevvalue,it.buf) < -1)
353 mrfd->prevvalue=xmalloc(info->key_size);
356 memcpy(mrfd->prevvalue,it.buf, info->key_size);
359 rdres=rset_forward(it.rset, it.fd, it.buf, &dummycount,
362 rdres=rset_read(it.rset, it.fd, it.buf, &dummycount);
364 heap_balance(mrfd->h);
366 heap_delete(mrfd->h);
371 static int r_read (RSFD rfd, void *buf, int *term_index)
373 return r_forward(0,rfd, buf, term_index,0,0);
376 static void r_pos (RSFD rfd, double *current, double *total)
378 struct rset_multior_rfd *mrfd = (struct rset_multior_rfd *) rfd;
379 struct rset_multior_info *info = mrfd->info;
381 double scur=0.0, stot=0.0;
383 for (i=0; i<info->no_rsets; i++){
384 rset_pos(mrfd->items[i].rset, mrfd->items[i].fd, &cur, &tot);
385 logf(LOG_LOG, "r_pos: %d %0.1f %0.1f", i, cur,tot);
389 if (stot <1.0) { /* nothing there */
395 *total=*current*stot/scur;
398 static int r_write (RSFD rfd, const void *buf)
400 logf (LOG_FATAL, "multior set type is read-only");