1 /* $Id: rsmultiandor.c,v 1.2 2004-09-28 16:12:42 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
25 * This module implements the rsmultior and rsmultiand result sets
27 * rsmultior is based on a heap, from which we find the next hit.
29 * rsmultiand is based on a simple array of rsets, and a linear
30 * search to find the record that exists in all of those rsets.
31 * To speed things up, the array is sorted so that the smallest
32 * rsets come first, they are most likely to have the hits furthest
33 * away, and thus forwarding to them makes the most sense.
46 static RSFD r_open_and (RSET ct, int flag);
47 static RSFD r_open_or (RSET ct, int flag);
48 static void r_close (RSFD rfd);
49 static void r_delete (RSET ct);
50 static void r_rewind (RSFD rfd);
51 static int r_read_and (RSFD rfd, void *buf);
52 static int r_read_or (RSFD rfd, void *buf);
53 static int r_write (RSFD rfd, const void *buf);
54 static int r_forward_and(RSFD rfd, void *buf,
55 const void *untilbuf);
56 static int r_forward_or(RSFD rfd, void *buf,
57 const void *untilbuf);
58 static void r_pos (RSFD rfd, double *current, double *total);
60 static const struct rset_control control_or =
72 static const struct rset_control control_and =
85 const struct rset_control *rset_kind_multior = &control_or;
86 const struct rset_control *rset_kind_multiand = &control_and;
88 /* The heap structure:
89 * The rset contains a list or rsets we are ORing together
90 * The rfd contains a heap of heap-items, which contain
91 * a rfd opened to those rsets, and a buffer for one key.
92 * They also contain a ptr to the rset list in the rset
93 * itself, for practical reasons.
105 const struct key_control *kctrl;
106 struct heap_item **heap; /* ptrs to the rfd */
108 typedef struct heap *HEAP;
111 struct rset_multiandor_info {
117 struct rset_multiandor_rfd {
119 struct heap_item *items; /* we alloc and free them here */
120 HEAP h; /* and move around here */
121 zint hits; /* returned so far */
122 int eof; /* seen the end of it */
123 int tailcount; /* how many items are tailing */
127 /* Heap functions ***********************/
130 static void heap_dump_item( HEAP h, int i, int level) {
134 (void)rset_pos(h->heap[i]->rset,h->heap[i]->fd, &cur, &tot);
135 logf(LOG_LOG," %d %*s i=%p buf=%p %0.1f/%0.1f",i, level, "",
136 &(h->heap[i]), h->heap[i]->buf, cur,tot );
137 heap_dump_item(h, 2*i, level+1);
138 heap_dump_item(h, 2*i+1, level+1);
140 static void heap_dump( HEAP h,char *msg) {
141 logf(LOG_LOG, "heap dump: %s num=%d max=%d",msg, h->heapnum, h->heapmax);
142 heap_dump_item(h,1,1);
146 static void heap_swap (HEAP h, int x, int y)
148 struct heap_item *swap;
150 h->heap[x]=h->heap[y];
154 static int heap_cmp(HEAP h, int x, int y)
156 return (*h->kctrl->cmp)(h->heap[x]->buf,h->heap[y]->buf);
159 static int heap_empty(HEAP h)
161 return ( 0==h->heapnum );
164 static void heap_delete (HEAP h)
165 { /* deletes the first item in the heap, and balances the rest */
166 int cur = 1, child = 2;
167 h->heap[1]=0; /* been deleted */
168 heap_swap (h, 1, h->heapnum--);
169 while (child <= h->heapnum) {
170 if (child < h->heapnum && heap_cmp(h,child,1+child)>0 )
172 if (heap_cmp(h,cur,child) > 0)
174 heap_swap (h, cur, child);
183 static void heap_balance (HEAP h)
184 { /* The heap root element has changed value (to bigger) */
185 /* swap downwards until the heap is ordered again */
186 int cur = 1, child = 2;
187 while (child <= h->heapnum) {
188 if (child < h->heapnum && heap_cmp(h,child,1+child)>0 )
190 if (heap_cmp(h,cur,child) > 0)
192 heap_swap (h, cur, child);
202 static void heap_insert (HEAP h, struct heap_item *hi)
206 cur = ++(h->heapnum);
207 assert(cur <= h->heapmax);
210 while (parent && (heap_cmp(h,parent,cur) > 0))
213 heap_swap (h, cur, parent);
221 HEAP heap_create (NMEM nmem, int size, const struct key_control *kctrl)
223 HEAP h = (HEAP) nmem_malloc (nmem, sizeof(*h));
225 ++size; /* heap array starts at 1 */
229 h->heap = (struct heap_item**) nmem_malloc(nmem,size*sizeof(*h->heap));
230 h->heap[0]=0; /* not used */
234 static void heap_clear( HEAP h)
240 static void heap_destroy (HEAP h)
242 /* nothing to delete, all is nmem'd, and will go away in due time */
245 int compare_ands(const void *x, const void *y)
246 { /* used in qsort to get the multi-and args in optimal order */
247 /* that is, those with fewest occurrences first */
248 const struct heap_item *hx=x;
249 const struct heap_item *hy=y;
250 double cur, totx, toty;
251 rset_pos(hx->fd, &cur, &totx);
252 rset_pos(hy->fd, &cur, &toty);
253 if ( totx > toty +0.5 ) return 1;
254 if ( totx < toty -0.5 ) return -1;
255 return 0; /* return totx - toty, except for overflows and rounding */
258 /* Creating and deleting rsets ***********************/
260 static RSET rsmulti_andor_create( NMEM nmem, const struct key_control *kcontrol,
261 int scope, int no_rsets, RSET* rsets,
262 const struct rset_control *ctrl)
264 RSET rnew=rset_create_base(ctrl, nmem,kcontrol, scope);
265 struct rset_multiandor_info *info;
266 info = (struct rset_multiandor_info *) nmem_malloc(rnew->nmem,sizeof(*info));
267 info->no_rsets=no_rsets;
268 info->rsets=(RSET*)nmem_malloc(rnew->nmem, no_rsets*sizeof(*rsets));
269 memcpy(info->rsets,rsets,no_rsets*sizeof(*rsets));
274 RSET rsmultior_create( NMEM nmem, const struct key_control *kcontrol, int scope,
275 int no_rsets, RSET* rsets)
277 return rsmulti_andor_create(nmem, kcontrol, scope,
278 no_rsets, rsets, &control_or);
281 RSET rsmultiand_create( NMEM nmem, const struct key_control *kcontrol, int scope,
282 int no_rsets, RSET* rsets)
284 return rsmulti_andor_create(nmem, kcontrol, scope,
285 no_rsets, rsets, &control_and);
288 static void r_delete (RSET ct)
290 struct rset_multiandor_info *info = (struct rset_multiandor_info *) ct->priv;
292 for(i=0;i<info->no_rsets;i++)
293 rset_delete(info->rsets[i]);
297 /* Opening and closing fd's on them *********************/
299 static RSFD r_open_andor (RSET ct, int flag, int is_and)
302 struct rset_multiandor_rfd *p;
303 struct rset_multiandor_info *info = (struct rset_multiandor_info *) ct->priv;
304 const struct key_control *kctrl = ct->keycontrol;
307 if (flag & RSETF_WRITE)
309 logf (LOG_FATAL, "multior set type is read-only");
312 rfd=rfd_create_base(ct);
314 p=(struct rset_multiandor_rfd *)rfd->priv;
318 /* all other pointers shouls already be allocated, in right sizes! */
321 p = (struct rset_multiandor_rfd *) nmem_malloc (ct->nmem,sizeof(*p));
326 p->tailbits=nmem_malloc(ct->nmem, info->no_rsets*sizeof(char) );
328 p->h = heap_create( ct->nmem, info->no_rsets, kctrl);
329 p->items=(struct heap_item *) nmem_malloc(ct->nmem,
330 info->no_rsets*sizeof(*p->items));
331 for (i=0; i<info->no_rsets; i++){
332 p->items[i].rset=info->rsets[i];
333 p->items[i].buf=nmem_malloc(ct->nmem,kctrl->key_size);
341 { /* read the array and sort it */
342 for (i=0; i<info->no_rsets; i++){
343 p->items[i].fd=rset_open(info->rsets[i],RSETF_READ);
344 if ( !rset_read(p->items[i].fd, p->items[i].buf) )
348 qsort(p->items, info->no_rsets, sizeof(p->items[0]), compare_ands);
350 { /* fill the heap for ORing */
351 for (i=0; i<info->no_rsets; i++){
352 p->items[i].fd=rset_open(info->rsets[i],RSETF_READ);
353 if ( rset_read(p->items[i].fd, p->items[i].buf) )
354 heap_insert(p->h, &(p->items[i]));
360 static RSFD r_open_or (RSET ct, int flag)
362 return r_open_andor(ct, flag, 0);
365 static RSFD r_open_and (RSET ct, int flag)
367 return r_open_andor(ct, flag, 1);
371 static void r_close (RSFD rfd)
373 struct rset_multiandor_info *info=
374 (struct rset_multiandor_info *)(rfd->rset->priv);
375 struct rset_multiandor_rfd *p=(struct rset_multiandor_rfd *)(rfd->priv);
380 for (i = 0; i<info->no_rsets; i++)
382 rset_close(p->items[i].fd);
383 rfd_delete_base(rfd);
388 static int r_forward_or(RSFD rfd, void *buf, const void *untilbuf)
390 struct rset_multiandor_rfd *mrfd=rfd->priv;
391 const struct key_control *kctrl=rfd->rset->keycontrol;
394 if (heap_empty(mrfd->h))
396 it = *(mrfd->h->heap[1]);
397 memcpy(buf,it.buf, kctrl->key_size);
398 /* FIXME - This is not right ! */
399 /* If called with an untilbuf, we need to compare to that, and */
400 /* forward until we are somewhere! */
403 rdres=rset_forward(it.fd, it.buf, untilbuf);
405 rdres=rset_read(it.fd, it.buf);
407 heap_balance(mrfd->h);
409 heap_delete(mrfd->h);
414 static int r_read_or (RSFD rfd, void *buf)
416 return r_forward_or(rfd, buf,0);
419 static int r_read_and (RSFD rfd, void *buf)
420 { /* Has to return all hits where each item points to the */
421 /* same sysno (scope), in order. Keep an extra key (hitkey) */
422 /* as long as all records do not point to hitkey, forward */
423 /* them, and update hitkey to be the highest seen so far. */
424 /* (if any item eof's, mark eof, and return 0 thereafter) */
425 /* Once a hit has been found, scan all items for the smallest */
426 /* value. Mark all as being in the tail. Read next from that */
427 /* item, and if not in the same record, clear its tail bit */
428 struct rset_multiandor_rfd *p=rfd->priv;
429 const struct key_control *kctrl=rfd->rset->keycontrol;
430 struct rset_multiandor_info *info=rfd->rset->priv;
436 { /* we are tailing, find lowest tail and return it */
438 while ((mintail<info->no_rsets) && !p->tailbits[mintail])
439 mintail++; /* first tail */
440 for (i=mintail+1;i<info->no_rsets;i++)
444 cmp=(*kctrl->cmp)(p->items[i].buf,p->items[mintail].buf);
449 /* return the lowest tail */
450 memcpy(buf, p->items[mintail].buf, kctrl->key_size);
451 if (!rset_read(p->items[mintail].fd, p->items[mintail].buf))
453 p->eof=1; /* game over, once tails have been returned */
454 p->tailbits[mintail]=0;
459 cmp=(*kctrl->cmp)(p->items[mintail].buf,buf);
460 if (cmp >= rfd->rset->scope){
461 p->tailbits[mintail]=0;
466 /* not tailing, forward until all reocrds match, and set up */
467 /* as tails. the earlier 'if' will then return the hits */
469 return 0; /* nothing more to see */
470 i=1; /* assume items[0] is highest up */
471 while (i<info->no_rsets) {
472 cmp=(*kctrl->cmp)(p->items[0].buf,p->items[i].buf);
473 if (cmp<=-rfd->rset->scope) { /* [0] was behind, forward it */
474 if (!rset_forward(p->items[0].fd, p->items[0].buf,
477 p->eof=1; /* game over */
480 i=0; /* start frowarding from scratch */
481 } else if (cmp>=rfd->rset->scope)
482 { /* [0] was ahead, forward i */
483 if (!rset_forward(p->items[i].fd, p->items[i].buf,
486 p->eof=1; /* game over */
492 /* if we get this far, all rsets are now within +- scope of [0] */
493 /* ergo, we have a hit. Mark them all as tailing, and let the */
494 /* upper 'if' return the hits in right order */
495 for (i=0; i<info->no_rsets;i++)
497 p->tailcount=info->no_rsets;
502 static int r_forward_and(RSFD rfd, void *buf, const void *untilbuf)
507 static void r_pos (RSFD rfd, double *current, double *total)
509 struct rset_multiandor_info *info=
510 (struct rset_multiandor_info *)(rfd->rset->priv);
511 struct rset_multiandor_rfd *mrfd=(struct rset_multiandor_rfd *)(rfd->priv);
513 double scur=0.0, stot=0.0;
515 for (i=0; i<info->no_rsets; i++){
516 rset_pos(mrfd->items[i].fd, &cur, &tot);
517 logf(LOG_DEBUG, "r_pos: %d %0.1f %0.1f", i, cur,tot);
521 if (stot <1.0) { /* nothing there */
527 *total=*current*stot/scur;
531 static void r_rewind (RSFD rfd)
533 assert(!"rewind not implemented yet");
534 /* FIXME - rewind all parts, rebalance heap, clear hits */
537 static int r_write (RSFD rfd, const void *buf)
539 logf (LOG_FATAL, "multior set type is read-only");