Passing a TERMID to rsets when creating, and getting it back when reading.
[idzebra-moved-to-github.git] / rset / rsmultiandor.c
1 /* $Id: rsmultiandor.c,v 1.6 2004-10-15 10:07:34 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23
24 /*
25  * This module implements the rsmultior and rsmultiand result sets
26  *
27  * rsmultior is based on a heap, from which we find the next hit.
28  *
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.
34  */
35
36
37 #include <assert.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <zebrautl.h>
43 #include <isamc.h>
44 #include <rset.h>
45
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 int r_read_and (RSFD rfd, void *buf, TERMID *term);
51 static int r_read_or (RSFD rfd, void *buf, TERMID *term);
52 static int r_write (RSFD rfd, const void *buf);
53 static int r_forward_and(RSFD rfd, void *buf, TERMID *term,
54                      const void *untilbuf);
55 static int r_forward_or(RSFD rfd, void *buf, TERMID *term,
56                      const void *untilbuf);
57 static void r_pos (RSFD rfd, double *current, double *total);
58
59 static const struct rset_control control_or = 
60 {
61     "multi-or",
62     r_delete,
63     r_open_or,
64     r_close,
65     r_forward_or,
66     r_pos,
67     r_read_or,
68     r_write,
69 };
70 static const struct rset_control control_and = 
71 {
72     "multi-and",
73     r_delete,
74     r_open_and,
75     r_close,
76     r_forward_and,
77     r_pos,
78     r_read_and,
79     r_write,
80 };
81
82 const struct rset_control *rset_kind_multior = &control_or;
83 const struct rset_control *rset_kind_multiand = &control_and;
84
85 /* The heap structure: 
86  * The rset contains a list or rsets we are ORing together 
87  * The rfd contains a heap of heap-items, which contain
88  * a rfd opened to those rsets, and a buffer for one key.
89  * They also contain a ptr to the rset list in the rset 
90  * itself, for practical reasons. 
91  */
92
93 struct heap_item {
94     RSFD fd;
95     void *buf;
96     RSET rset;
97     TERMID term;
98 };
99
100 struct heap {
101     int heapnum;
102     int heapmax;
103     const struct key_control *kctrl;
104     struct heap_item **heap; /* ptrs to the rfd */
105 };
106 typedef struct heap *HEAP;
107
108
109 struct rset_multiandor_info {
110     int     no_rsets;
111     RSET    *rsets;
112 };
113
114
115 struct rset_multiandor_rfd {
116     int flag;
117     struct heap_item *items; /* we alloc and free them here */
118     HEAP h; /* and move around here */
119     zint hits; /* returned so far */
120     int eof; /* seen the end of it */
121     int tailcount; /* how many items are tailing */
122     char *tailbits;
123 };
124
125 /* Heap functions ***********************/
126
127 #if 0
128 static void heap_dump_item( HEAP h, int i, int level) {
129     double cur,tot;
130     if (i>h->heapnum)
131         return;
132     (void)rset_pos(h->heap[i]->rset,h->heap[i]->fd, &cur, &tot);
133     logf(LOG_LOG," %d %*s i=%p buf=%p %0.1f/%0.1f",i, level, "",  
134                     &(h->heap[i]), h->heap[i]->buf, cur,tot );
135     heap_dump_item(h, 2*i, level+1);
136     heap_dump_item(h, 2*i+1, level+1);
137 }
138 static void heap_dump( HEAP h,char *msg) {
139     logf(LOG_LOG, "heap dump: %s num=%d max=%d",msg, h->heapnum, h->heapmax);
140     heap_dump_item(h,1,1);
141 }
142 #endif
143
144 static void heap_swap (HEAP h, int x, int y)
145 {
146     struct heap_item *swap;
147     swap = h->heap[x];
148     h->heap[x]=h->heap[y];
149     h->heap[y]=swap;
150 }
151
152 static int heap_cmp(HEAP h, int x, int y)
153 {
154     return (*h->kctrl->cmp)(h->heap[x]->buf,h->heap[y]->buf);
155 }
156
157 static int heap_empty(HEAP h)
158 {
159     return ( 0==h->heapnum );
160 }
161
162 static void heap_delete (HEAP h)
163 { /* deletes the first item in the heap, and balances the rest */
164     int cur = 1, child = 2;
165     h->heap[1]=0; /* been deleted */
166     heap_swap (h, 1, h->heapnum--);
167     while (child <= h->heapnum) {
168         if (child < h->heapnum && heap_cmp(h,child,1+child)>0 )
169             child++;
170         if (heap_cmp(h,cur,child) > 0)
171         {
172             heap_swap (h, cur, child);
173             cur = child;
174             child = 2*cur;
175         }
176         else
177             break;
178     }
179 }
180
181 static void heap_balance (HEAP h)
182 { /* The heap root element has changed value (to bigger) */
183   /* swap downwards until the heap is ordered again */
184     int cur = 1, child = 2;
185     while (child <= h->heapnum) {
186         if (child < h->heapnum && heap_cmp(h,child,1+child)>0 )
187             child++;
188         if (heap_cmp(h,cur,child) > 0)
189         {
190             heap_swap (h, cur, child);
191             cur = child;
192             child = 2*cur;
193         }
194         else
195             break;
196     }
197 }
198
199
200 static void heap_insert (HEAP h, struct heap_item *hi)
201 {
202     int cur, parent;
203
204     cur = ++(h->heapnum);
205     assert(cur <= h->heapmax);
206     h->heap[cur]=hi;
207     parent = cur/2;
208     while (parent && (heap_cmp(h,parent,cur) > 0))
209     {
210         assert(parent>0);
211         heap_swap (h, cur, parent);
212         cur = parent;
213         parent = cur/2;
214     }
215 }
216
217
218 static
219 HEAP heap_create (NMEM nmem, int size, const struct key_control *kctrl)
220 {
221     HEAP h = (HEAP) nmem_malloc (nmem, sizeof(*h));
222
223     ++size; /* heap array starts at 1 */
224     h->heapnum = 0;
225     h->heapmax = size;
226     h->kctrl=kctrl;
227     h->heap = (struct heap_item**) nmem_malloc(nmem,size*sizeof(*h->heap));
228     h->heap[0]=0; /* not used */
229     return h;
230 }
231
232 static void heap_clear( HEAP h)
233 {
234     assert(h);
235     h->heapnum=0;
236 }
237
238 static void heap_destroy (HEAP h)
239 {
240     /* nothing to delete, all is nmem'd, and will go away in due time */
241 }
242
243 int compare_ands(const void *x, const void *y)
244 { /* used in qsort to get the multi-and args in optimal order */
245   /* that is, those with fewest occurrences first */
246     const struct heap_item *hx=x;
247     const struct heap_item *hy=y;
248     double cur, totx, toty;
249     rset_pos(hx->fd, &cur, &totx);
250     rset_pos(hy->fd, &cur, &toty);
251     if ( totx > toty +0.5 ) return 1;
252     if ( totx < toty -0.5 ) return -1;
253     return 0;  /* return totx - toty, except for overflows and rounding */
254 }
255
256 /* Creating and deleting rsets ***********************/
257
258 static RSET rsmulti_andor_create( NMEM nmem, const struct key_control *kcontrol, 
259                            int scope, int no_rsets, RSET* rsets, 
260                            const struct rset_control *ctrl)
261 {
262     RSET rnew=rset_create_base(ctrl, nmem,kcontrol, scope,0);
263     struct rset_multiandor_info *info;
264     info = (struct rset_multiandor_info *) nmem_malloc(rnew->nmem,sizeof(*info));
265     info->no_rsets=no_rsets;
266     info->rsets=(RSET*)nmem_malloc(rnew->nmem, no_rsets*sizeof(*rsets));
267     memcpy(info->rsets,rsets,no_rsets*sizeof(*rsets));
268     rnew->priv=info;
269     return rnew;
270 }
271
272 RSET rsmultior_create( NMEM nmem, const struct key_control *kcontrol, int scope,
273             int no_rsets, RSET* rsets)
274 {
275     return rsmulti_andor_create(nmem, kcontrol, scope, 
276                                 no_rsets, rsets, &control_or);
277 }
278
279 RSET rsmultiand_create( NMEM nmem, const struct key_control *kcontrol, int scope,
280             int no_rsets, RSET* rsets)
281 {
282     return rsmulti_andor_create(nmem, kcontrol, scope, 
283                                 no_rsets, rsets, &control_and);
284 }
285
286 static void r_delete (RSET ct)
287 {
288     struct rset_multiandor_info *info = (struct rset_multiandor_info *) ct->priv;
289     int i;
290     for(i=0;i<info->no_rsets;i++)
291         rset_delete(info->rsets[i]);
292 }
293
294
295 /* Opening and closing fd's on them *********************/
296
297 static RSFD r_open_andor (RSET ct, int flag, int is_and)
298 {
299     RSFD rfd;
300     struct rset_multiandor_rfd *p;
301     struct rset_multiandor_info *info = (struct rset_multiandor_info *) ct->priv;
302     const struct key_control *kctrl = ct->keycontrol;
303     int i;
304
305     if (flag & RSETF_WRITE)
306     {
307         logf (LOG_FATAL, "multiandor set type is read-only");
308         return NULL;
309     }
310     rfd=rfd_create_base(ct);
311     if (rfd->priv) {
312         p=(struct rset_multiandor_rfd *)rfd->priv;
313         if (!is_and)
314             heap_clear(p->h);
315         assert(p->items);
316         /* all other pointers shouls already be allocated, in right sizes! */
317     }
318     else {
319         p = (struct rset_multiandor_rfd *) nmem_malloc (ct->nmem,sizeof(*p));
320         rfd->priv=p;
321         p->h=0;
322         p->tailbits=0;
323         if (is_and)
324             p->tailbits=nmem_malloc(ct->nmem, info->no_rsets*sizeof(char) );
325         else 
326             p->h = heap_create( ct->nmem, info->no_rsets, kctrl);
327         p->items=(struct heap_item *) nmem_malloc(ct->nmem,
328                               info->no_rsets*sizeof(*p->items));
329         for (i=0; i<info->no_rsets; i++){
330             p->items[i].rset=info->rsets[i];
331             p->items[i].buf=nmem_malloc(ct->nmem,kctrl->key_size);
332         }
333     }
334     p->flag = flag;
335     p->hits=0;
336     p->eof=0;
337     p->tailcount=0;
338     if (is_and)
339     { /* read the array and sort it */
340         for (i=0; i<info->no_rsets; i++){
341             p->items[i].fd=rset_open(info->rsets[i],RSETF_READ);
342             if (!rset_read(p->items[i].fd, p->items[i].buf, &p->items[i].term))
343                 p->eof=1;
344             p->tailbits[i]=0;
345         }
346         qsort(p->items, info->no_rsets, sizeof(p->items[0]), compare_ands);
347     } else
348     { /* fill the heap for ORing */
349         for (i=0; i<info->no_rsets; i++){
350             p->items[i].fd=rset_open(info->rsets[i],RSETF_READ);
351             if ( rset_read(p->items[i].fd, p->items[i].buf, &p->items[i].term))
352                 heap_insert(p->h, &(p->items[i]));
353         }
354     }
355     return rfd;
356 }
357
358 static RSFD r_open_or (RSET ct, int flag)
359 {
360     return r_open_andor(ct, flag, 0);
361 }
362
363 static RSFD r_open_and (RSET ct, int flag)
364 {
365     return r_open_andor(ct, flag, 1);
366 }
367
368
369 static void r_close (RSFD rfd)
370 {
371     struct rset_multiandor_info *info=
372         (struct rset_multiandor_info *)(rfd->rset->priv);
373     struct rset_multiandor_rfd *p=(struct rset_multiandor_rfd *)(rfd->priv);
374     int i;
375
376     if (p->h)
377         heap_destroy (p->h);
378     for (i = 0; i<info->no_rsets; i++) 
379         if (p->items[i].fd)
380             rset_close(p->items[i].fd);
381     rfd_delete_base(rfd);
382 }
383
384
385
386 static int r_forward_or(RSFD rfd, void *buf, 
387                         TERMID *term,const void *untilbuf)
388 { /* while heap head behind untilbuf, forward it and rebalance heap */
389     struct rset_multiandor_rfd *p=rfd->priv;
390     const struct key_control *kctrl=rfd->rset->keycontrol;
391     if (heap_empty(p->h))
392         return 0;
393     while ( (*kctrl->cmp)(p->h->heap[1]->buf,untilbuf) < -rfd->rset->scope )
394     {
395         if (rset_forward(p->h->heap[1]->fd,p->h->heap[1]->buf,
396                          &p->h->heap[1]->term, untilbuf))
397             heap_balance(p->h);
398         else 
399         {
400             heap_delete(p->h);
401             if (heap_empty(p->h))
402                 return 0;
403         }
404
405     }
406     return r_read_or(rfd,buf,term);
407 }
408
409
410 static int r_read_or (RSFD rfd, void *buf, TERMID *term)
411 {
412     struct rset_multiandor_rfd *mrfd=rfd->priv;
413     const struct key_control *kctrl=rfd->rset->keycontrol;
414     struct heap_item *it;
415     int rdres;
416     if (heap_empty(mrfd->h))
417         return 0;
418     it = mrfd->h->heap[1];
419     memcpy(buf,it->buf, kctrl->key_size); 
420     if (term)
421         *term=it->term;
422     (mrfd->hits)++;
423     rdres=rset_read(it->fd, it->buf, &it->term);
424     if ( rdres )
425         heap_balance(mrfd->h);
426     else
427         heap_delete(mrfd->h);
428     return 1;
429
430 }
431
432 static int r_read_and (RSFD rfd, void *buf, TERMID *term)
433 { /* Has to return all hits where each item points to the */
434   /* same sysno (scope), in order. Keep an extra key (hitkey) */
435   /* as long as all records do not point to hitkey, forward */
436   /* them, and update hitkey to be the highest seen so far. */
437   /* (if any item eof's, mark eof, and return 0 thereafter) */
438   /* Once a hit has been found, scan all items for the smallest */
439   /* value. Mark all as being in the tail. Read next from that */
440   /* item, and if not in the same record, clear its tail bit */
441     struct rset_multiandor_rfd *p=rfd->priv;
442     const struct key_control *kctrl=rfd->rset->keycontrol;
443     struct rset_multiandor_info *info=rfd->rset->priv;
444     int i, mintail;
445     int cmp;
446
447     while (1) {
448         if (p->tailcount) 
449         { /* we are tailing, find lowest tail and return it */
450             mintail=0;
451             while ((mintail<info->no_rsets) && !p->tailbits[mintail])
452                 mintail++; /* first tail */
453             for (i=mintail+1;i<info->no_rsets;i++)
454             {
455                 if (p->tailbits[i])
456                 {
457                     cmp=(*kctrl->cmp)(p->items[i].buf,p->items[mintail].buf);
458                     if (cmp<0) 
459                         mintail=i;
460                 }
461             }
462             /* return the lowest tail */
463             memcpy(buf, p->items[mintail].buf, kctrl->key_size); 
464             if (term)
465                 *term=p->items[mintail].term;
466             if (!rset_read(p->items[mintail].fd, p->items[mintail].buf,
467                            &p->items[mintail].term))
468             {
469                 p->eof=1; /* game over, once tails have been returned */
470                 p->tailbits[mintail]=0; 
471                 (p->tailcount)--;
472                 return 1;
473             }
474             /* still a tail? */
475             cmp=(*kctrl->cmp)(p->items[mintail].buf,buf);
476             if (cmp >= rfd->rset->scope){
477                 p->tailbits[mintail]=0;
478                 (p->tailcount)--;
479             }
480             return 1;
481         } 
482         /* not tailing, forward until all reocrds match, and set up */
483         /* as tails. the earlier 'if' will then return the hits */
484         if (p->eof)
485             return 0; /* nothing more to see */
486         i=1; /* assume items[0] is highest up */
487         while (i<info->no_rsets) {
488             cmp=(*kctrl->cmp)(p->items[0].buf,p->items[i].buf);
489             if (cmp<=-rfd->rset->scope) { /* [0] was behind, forward it */
490                 if (!rset_forward(p->items[0].fd, p->items[0].buf, 
491                                   &p->items[0].term, p->items[i].buf))
492                 {
493                     p->eof=1; /* game over */
494                     return 0;
495                 }
496                 i=0; /* start frowarding from scratch */
497             } else if (cmp>=rfd->rset->scope)
498             { /* [0] was ahead, forward i */
499                 if (!rset_forward(p->items[i].fd, p->items[i].buf, 
500                                   &p->items[i].term, p->items[0].buf))
501                 {
502                     p->eof=1; /* game over */
503                     return 0;
504                 }
505             } else
506                 i++;
507         } /* while i */
508         /* if we get this far, all rsets are now within +- scope of [0] */
509         /* ergo, we have a hit. Mark them all as tailing, and let the */
510         /* upper 'if' return the hits in right order */
511         for (i=0; i<info->no_rsets;i++)
512             p->tailbits[i]=1;
513         p->tailcount=info->no_rsets;
514     } /* while 1 */
515 }
516
517
518 static int r_forward_and(RSFD rfd, void *buf, TERMID *term, 
519                          const void *untilbuf)
520
521     struct rset_multiandor_rfd *p=rfd->priv;
522     const struct key_control *kctrl=rfd->rset->keycontrol;
523     struct rset_multiandor_info *info=rfd->rset->priv;
524     int i;
525     int cmp;
526     int killtail=0;
527
528     for (i=0; i<info->no_rsets;i++)
529     {
530         cmp=(*kctrl->cmp)(p->items[i].buf,untilbuf);
531         if ( cmp <= -rfd->rset->scope )
532         {
533             killtail=1; /* we are moving to a different hit */
534             if (!rset_forward(p->items[i].fd, p->items[i].buf, 
535                               &p->items[i].term, untilbuf))
536             {
537                 p->eof=1; /* game over */
538                 p->tailcount=0;
539                 return 0;
540             }
541         }
542     }
543     if (killtail) 
544     {
545         for (i=0; i<info->no_rsets;i++)
546             p->tailbits[i]=0;
547         p->tailcount=0;
548     }
549     return r_read_and(rfd,buf,term);
550 }
551
552 static void r_pos (RSFD rfd, double *current, double *total)
553 {
554     struct rset_multiandor_info *info=
555              (struct rset_multiandor_info *)(rfd->rset->priv);
556     struct rset_multiandor_rfd *mrfd=(struct rset_multiandor_rfd *)(rfd->priv);
557     double cur, tot;
558     double scur=0.0, stot=0.0;
559     int i;
560     for (i=0; i<info->no_rsets; i++){
561         rset_pos(mrfd->items[i].fd, &cur, &tot);
562         logf(LOG_DEBUG, "r_pos: %d %0.1f %0.1f", i, cur,tot);
563         scur += cur;
564         stot += tot;
565     }
566     if (stot <1.0) { /* nothing there */
567         *current=0;
568         *total=0;
569         return;
570     }
571     *current=mrfd->hits;
572     *total=*current*stot/scur;
573 }
574
575
576
577 static int r_write (RSFD rfd, const void *buf)
578 {
579     logf (LOG_FATAL, "multior set type is read-only");
580     return -1;
581 }