1 /* $Id: rsmultior.c,v 1.6 2004-08-24 14:25:16 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(RSET ct, 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);
91 struct rset_multior_rfd *rfd_list;
95 struct rset_multior_rfd {
97 struct heap_item *items; /* we alloc and free them here */
99 struct rset_multior_rfd *next;
100 struct rset_multior_info *info;
101 zint hits; /* returned so far */
102 char *prevvalue; /* to see if we are in another record */
103 /* FIXME - is this really needed? */
107 static void heap_dump_item( HEAP h, int i, int level) {
111 (void)rset_pos(h->heap[i]->rset,h->heap[i]->fd, &cur, &tot);
112 logf(LOG_LOG," %d %*s i=%p buf=%p %0.1f/%0.1f",i, level, "",
113 &(h->heap[i]), h->heap[i]->buf, cur,tot );
114 heap_dump_item(h, 2*i, level+1);
115 heap_dump_item(h, 2*i+1, level+1);
117 static void heap_dump( HEAP h,char *msg) {
118 logf(LOG_LOG, "heap dump: %s num=%d max=%d",msg, h->heapnum, h->heapmax);
119 heap_dump_item(h,1,1);
123 static void heap_swap (HEAP h, int x, int y)
125 struct heap_item *swap;
127 h->heap[x]=h->heap[y];
131 static int heap_cmp(HEAP h, int x, int y)
133 return (*h->cmp)(h->heap[x]->buf,h->heap[y]->buf);
136 static int heap_empty(HEAP h)
138 return ( 0==h->heapnum );
141 static void heap_delete (HEAP h)
142 { /* deletes the first item in the heap, and balances the rest */
143 int cur = 1, child = 2;
144 h->heap[1]=0; /* been deleted */
145 heap_swap (h, 1, h->heapnum--);
146 while (child <= h->heapnum) {
147 if (child < h->heapnum && heap_cmp(h,child,1+child)>0 )
149 if (heap_cmp(h,cur,child) > 0)
151 heap_swap (h, cur, child);
160 static void heap_balance (HEAP h)
161 { /* The heap root element has changed value (to bigger) */
162 /* swap downwards until the heap is ordered again */
163 int cur = 1, child = 2;
164 while (child <= h->heapnum) {
165 if (child < h->heapnum && heap_cmp(h,child,1+child)>0 )
167 if (heap_cmp(h,cur,child) > 0)
169 heap_swap (h, cur, child);
179 static void heap_insert (HEAP h, struct heap_item *hi)
183 cur = ++(h->heapnum);
184 assert(cur <= h->heapmax);
187 while (parent && (heap_cmp(h,parent,cur) > 0))
190 heap_swap (h, cur, parent);
198 HEAP heap_create (int size, int key_size,
199 int (*cmp)(const void *p1, const void *p2))
201 HEAP h = (HEAP) xmalloc (sizeof(*h));
203 ++size; /* heap array starts at 1 */
206 h->keysize = key_size;
208 h->heap = (struct heap_item**) xmalloc((size)*sizeof(*h->heap));
209 h->heap[0]=0; /* not used */
213 static void heap_destroy (HEAP h)
215 xfree (h->heap); /* safe, they all point to the rfd */
220 RSET rsmultior_create( NMEM nmem, int key_size,
221 int (*cmp)(const void *p1, const void *p2),
222 int no_rsets, RSET* rsets)
224 RSET rnew=rset_create_base(&control, nmem);
225 struct rset_multior_info *info;
226 info = (struct rset_multior_info *) nmem_malloc(rnew->nmem,sizeof(*info));
227 info->key_size = key_size;
229 info->no_rsets=no_rsets;
230 info->rsets=(RSET*)nmem_malloc(rnew->nmem, no_rsets*sizeof(*rsets));
231 memcpy(info->rsets,rsets,no_rsets*sizeof(*rsets));
232 info->rfd_list = NULL;
237 static void r_delete (RSET ct)
239 struct rset_multior_info *info = (struct rset_multior_info *) ct->priv;
242 assert (info->rfd_list == NULL);
243 for(i=0;i<info->no_rsets;i++)
244 rset_delete(info->rsets[i]);
245 /* xfree(info->rsets); */ /* nmem'd */
246 /* xfree(info); */ /* nmem'd */
249 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
251 rset_multior_parms *r_parms = (rset_multior_parms *) parms;
252 struct rset_multior_info *info;
253 info = (struct rset_multior_info *) xmalloc (sizeof(*info));
254 info->key_size = r_parms->key_size;
255 assert (info->key_size > 1);
256 info->cmp = r_parms->cmp;
257 info->no_rsets= r_parms->no_rsets;
258 info->rsets=r_parms->rsets; /* now we own it! */
264 static RSFD r_open (RSET ct, int flag)
266 struct rset_multior_rfd *rfd;
267 struct rset_multior_info *info = (struct rset_multior_info *) ct->priv;
270 if (flag & RSETF_WRITE)
272 logf (LOG_FATAL, "multior set type is read-only");
275 rfd = (struct rset_multior_rfd *) xmalloc (sizeof(*rfd));
277 rfd->next = info->rfd_list;
279 info->rfd_list = rfd;
280 rfd->h = heap_create( info->no_rsets, info->key_size, info->cmp);
283 rfd->items=(struct heap_item *) xmalloc(info->no_rsets*sizeof(*rfd->items));
284 for (i=0; i<info->no_rsets; i++){
285 rfd->items[i].rset=info->rsets[i];
286 rfd->items[i].buf=xmalloc(info->key_size);
287 rfd->items[i].fd=rset_open(info->rsets[i],RSETF_READ);
288 /* if (item_readbuf(&(rfd->items[i]))) */
289 if ( rset_read(rfd->items[i].rset, rfd->items[i].fd,
291 heap_insert(rfd->h, &(rfd->items[i]));
296 static void r_close (RSFD rfd)
298 struct rset_multior_rfd *mrfd = (struct rset_multior_rfd *) rfd;
299 struct rset_multior_info *info = mrfd->info;
300 struct rset_multior_rfd **rfdp;
303 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
306 *rfdp = (*rfdp)->next;
308 heap_destroy (mrfd->h);
309 for (i = 0; i<info->no_rsets; i++) {
310 if (mrfd->items[i].fd)
311 rset_close(info->rsets[i],mrfd->items[i].fd);
312 xfree(mrfd->items[i].buf);
316 xfree(mrfd->prevvalue);
320 logf (LOG_FATAL, "r_close but no rfd match!");
325 static void r_rewind (RSFD rfd)
327 assert(!"rewind not implemented yet");
331 static int r_forward(RSET ct, RSFD rfd, void *buf,
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;
339 if (heap_empty(mrfd->h))
342 assert(cmpfunc==mrfd->info->cmp);
343 it = *(mrfd->h->heap[1]);
344 memcpy(buf,it.buf, info->key_size);
347 rdres=rset_forward(it.rset, it.fd, it.buf, cmpfunc,untilbuf);
349 rdres=rset_read(it.rset, it.fd, it.buf);
351 heap_balance(mrfd->h);
353 heap_delete(mrfd->h);
358 static int r_read (RSFD rfd, void *buf)
360 return r_forward(0,rfd, buf,0,0);
363 static void r_pos (RSFD rfd, double *current, double *total)
365 struct rset_multior_rfd *mrfd = (struct rset_multior_rfd *) rfd;
366 struct rset_multior_info *info = mrfd->info;
368 double scur=0.0, stot=0.0;
370 for (i=0; i<info->no_rsets; i++){
371 rset_pos(mrfd->items[i].rset, mrfd->items[i].fd, &cur, &tot);
372 logf(LOG_LOG, "r_pos: %d %0.1f %0.1f", i, cur,tot);
376 if (stot <1.0) { /* nothing there */
382 *total=*current*stot/scur;
385 static int r_write (RSFD rfd, const void *buf)
387 logf (LOG_FATAL, "multior set type is read-only");