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