1 /* $Id: rsmultior.c,v 1.8 2004-08-31 10:43:39 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 RSFD r_open (RSET ct, int flag);
36 static void r_close (RSFD rfd);
37 static void r_delete (RSET ct);
38 static void r_rewind (RSFD rfd);
39 static int r_read (RSFD rfd, void *buf);
40 static int r_write (RSFD rfd, const void *buf);
41 static int r_forward(RSFD rfd, void *buf,
42 int (*cmpfunc)(const void *p1, const void *p2),
43 const void *untilbuf);
44 static void r_pos (RSFD rfd, double *current, double *total);
46 static const struct rset_control control =
59 const struct rset_control *rset_kind_multior = &control;
61 /* The heap structure:
62 * The rset contains a list or rsets we are ORing together
63 * The rfd contains a heap of heap-items, which contain
64 * a rfd opened to those rsets, and a buffer for one key.
65 * They also contain a ptr to the rset list in the rset
66 * itself, for practical reasons.
79 int (*cmp)(const void *p1, const void *p2);
80 struct heap_item **heap; /* ptrs to the rfd */
82 typedef struct heap *HEAP;
85 struct rset_multior_info {
88 int (*cmp)(const void *p1, const void *p2);
94 struct rset_multior_rfd {
96 struct heap_item *items; /* we alloc and free them here */
98 zint hits; /* returned so far */
99 char *prevvalue; /* to see if we are in another record */
100 /* FIXME - is this really needed? */
104 static void heap_dump_item( HEAP h, int i, int level) {
108 (void)rset_pos(h->heap[i]->rset,h->heap[i]->fd, &cur, &tot);
109 logf(LOG_LOG," %d %*s i=%p buf=%p %0.1f/%0.1f",i, level, "",
110 &(h->heap[i]), h->heap[i]->buf, cur,tot );
111 heap_dump_item(h, 2*i, level+1);
112 heap_dump_item(h, 2*i+1, level+1);
114 static void heap_dump( HEAP h,char *msg) {
115 logf(LOG_LOG, "heap dump: %s num=%d max=%d",msg, h->heapnum, h->heapmax);
116 heap_dump_item(h,1,1);
120 static void heap_swap (HEAP h, int x, int y)
122 struct heap_item *swap;
124 h->heap[x]=h->heap[y];
128 static int heap_cmp(HEAP h, int x, int y)
130 return (*h->cmp)(h->heap[x]->buf,h->heap[y]->buf);
133 static int heap_empty(HEAP h)
135 return ( 0==h->heapnum );
138 static void heap_delete (HEAP h)
139 { /* deletes the first item in the heap, and balances the rest */
140 int cur = 1, child = 2;
141 h->heap[1]=0; /* been deleted */
142 heap_swap (h, 1, h->heapnum--);
143 while (child <= h->heapnum) {
144 if (child < h->heapnum && heap_cmp(h,child,1+child)>0 )
146 if (heap_cmp(h,cur,child) > 0)
148 heap_swap (h, cur, child);
157 static void heap_balance (HEAP h)
158 { /* The heap root element has changed value (to bigger) */
159 /* swap downwards until the heap is ordered again */
160 int cur = 1, child = 2;
161 while (child <= h->heapnum) {
162 if (child < h->heapnum && heap_cmp(h,child,1+child)>0 )
164 if (heap_cmp(h,cur,child) > 0)
166 heap_swap (h, cur, child);
176 static void heap_insert (HEAP h, struct heap_item *hi)
180 cur = ++(h->heapnum);
181 assert(cur <= h->heapmax);
184 while (parent && (heap_cmp(h,parent,cur) > 0))
187 heap_swap (h, cur, parent);
195 HEAP heap_create (NMEM nmem, int size, int key_size,
196 int (*cmp)(const void *p1, const void *p2))
198 HEAP h = (HEAP) nmem_malloc (nmem, sizeof(*h));
200 ++size; /* heap array starts at 1 */
203 h->keysize = key_size;
205 h->heap = (struct heap_item**) nmem_malloc(nmem,(size)*sizeof(*h->heap));
206 h->heap[0]=0; /* not used */
210 static void heap_clear( HEAP h)
216 static void heap_destroy (HEAP h)
218 /* nothing to delete, all is nmem'd, and will go away in due time */
222 RSET rsmultior_create( NMEM nmem, int key_size,
223 int (*cmp)(const void *p1, const void *p2),
224 int no_rsets, RSET* rsets)
226 RSET rnew=rset_create_base(&control, nmem);
227 struct rset_multior_info *info;
228 info = (struct rset_multior_info *) nmem_malloc(rnew->nmem,sizeof(*info));
229 info->key_size = key_size;
231 info->no_rsets=no_rsets;
232 info->rsets=(RSET*)nmem_malloc(rnew->nmem, no_rsets*sizeof(*rsets));
233 memcpy(info->rsets,rsets,no_rsets*sizeof(*rsets));
238 static void r_delete (RSET ct)
240 struct rset_multior_info *info = (struct rset_multior_info *) ct->priv;
242 for(i=0;i<info->no_rsets;i++)
243 rset_delete(info->rsets[i]);
246 static RSFD r_open (RSET ct, int flag)
249 struct rset_multior_rfd *p;
250 struct rset_multior_info *info = (struct rset_multior_info *) ct->priv;
253 if (flag & RSETF_WRITE)
255 logf (LOG_FATAL, "multior set type is read-only");
258 rfd=rfd_create_base(ct);
260 p=(struct rset_multior_rfd *)rfd->priv;
263 /* all other pointers shouls already be allocated, in right sizes! */
266 p = (struct rset_multior_rfd *) nmem_malloc (ct->nmem,sizeof(*p));
268 p->h = heap_create( ct->nmem, info->no_rsets,
269 info->key_size, info->cmp);
270 p->items=(struct heap_item *) nmem_malloc(ct->nmem,
271 info->no_rsets*sizeof(*p->items));
272 for (i=0; i<info->no_rsets; i++){
273 p->items[i].rset=info->rsets[i];
274 p->items[i].buf=nmem_malloc(ct->nmem,info->key_size);
280 for (i=0; i<info->no_rsets; i++){
281 p->items[i].fd=rset_open(info->rsets[i],RSETF_READ);
282 if ( rset_read(p->items[i].fd, p->items[i].buf) )
283 heap_insert(p->h, &(p->items[i]));
288 static void r_close (RSFD rfd)
290 struct rset_multior_info *info=(struct rset_multior_info *)(rfd->rset->priv);
291 struct rset_multior_rfd *p=(struct rset_multior_rfd *)(rfd->priv);
295 for (i = 0; i<info->no_rsets; i++)
297 rset_close(p->items[i].fd);
298 rfd_delete_base(rfd);
302 static void r_rewind (RSFD rfd)
304 assert(!"rewind not implemented yet");
308 static int r_forward(RSFD rfd, void *buf,
309 int (*cmpfunc)(const void *p1, const void *p2),
310 const void *untilbuf)
312 struct rset_multior_info *info=(struct rset_multior_info *)(rfd->rset->priv);
313 struct rset_multior_rfd *mrfd=(struct rset_multior_rfd *)(rfd->priv);
316 if (heap_empty(mrfd->h))
319 assert(cmpfunc==info->cmp);
320 it = *(mrfd->h->heap[1]);
321 memcpy(buf,it.buf, info->key_size);
324 rdres=rset_forward(it.fd, it.buf, cmpfunc,untilbuf);
326 rdres=rset_read(it.fd, it.buf);
328 heap_balance(mrfd->h);
330 heap_delete(mrfd->h);
335 static int r_read (RSFD rfd, void *buf)
337 return r_forward(rfd, buf,0,0);
340 static void r_pos (RSFD rfd, double *current, double *total)
342 struct rset_multior_info *info=(struct rset_multior_info *)(rfd->rset->priv);
343 struct rset_multior_rfd *mrfd=(struct rset_multior_rfd *)(rfd->priv);
345 double scur=0.0, stot=0.0;
347 for (i=0; i<info->no_rsets; i++){
348 rset_pos(mrfd->items[i].fd, &cur, &tot);
349 logf(LOG_LOG, "r_pos: %d %0.1f %0.1f", i, cur,tot);
353 if (stot <1.0) { /* nothing there */
359 *total=*current*stot/scur;
362 static int r_write (RSFD rfd, const void *buf)
364 logf (LOG_FATAL, "multior set type is read-only");