multiand seesm to work. Is used for and-lists (@attr 4=6)
[idzebra-moved-to-github.git] / rset / rsmultiandor.c
1 /* $Id: rsmultiandor.c,v 1.3 2004-09-28 16:39:46 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 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);
59
60 static const struct rset_control control_or = 
61 {
62     "multi-or",
63     r_delete,
64     r_open_or,
65     r_close,
66     r_rewind,
67     r_forward_or,
68     r_pos,
69     r_read_or,
70     r_write,
71 };
72 static const struct rset_control control_and = 
73 {
74     "multi-and",
75     r_delete,
76     r_open_and,
77     r_close,
78     r_rewind,
79     r_forward_and,
80     r_pos,
81     r_read_and,
82     r_write,
83 };
84
85 const struct rset_control *rset_kind_multior = &control_or;
86 const struct rset_control *rset_kind_multiand = &control_and;
87
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. 
94  */
95
96 struct heap_item {
97     RSFD fd;
98     void *buf;
99     RSET rset;
100 };
101
102 struct heap {
103     int heapnum;
104     int heapmax;
105     const struct key_control *kctrl;
106     struct heap_item **heap; /* ptrs to the rfd */
107 };
108 typedef struct heap *HEAP;
109
110
111 struct rset_multiandor_info {
112     int     no_rsets;
113     RSET    *rsets;
114 };
115
116
117 struct rset_multiandor_rfd {
118     int flag;
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 */
124     char *tailbits;
125 };
126
127 /* Heap functions ***********************/
128
129 #if 0
130 static void heap_dump_item( HEAP h, int i, int level) {
131     double cur,tot;
132     if (i>h->heapnum)
133         return;
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);
139 }
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);
143 }
144 #endif
145
146 static void heap_swap (HEAP h, int x, int y)
147 {
148     struct heap_item *swap;
149     swap = h->heap[x];
150     h->heap[x]=h->heap[y];
151     h->heap[y]=swap;
152 }
153
154 static int heap_cmp(HEAP h, int x, int y)
155 {
156     return (*h->kctrl->cmp)(h->heap[x]->buf,h->heap[y]->buf);
157 }
158
159 static int heap_empty(HEAP h)
160 {
161     return ( 0==h->heapnum );
162 }
163
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 )
171             child++;
172         if (heap_cmp(h,cur,child) > 0)
173         {
174             heap_swap (h, cur, child);
175             cur = child;
176             child = 2*cur;
177         }
178         else
179             break;
180     }
181 }
182
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 )
189             child++;
190         if (heap_cmp(h,cur,child) > 0)
191         {
192             heap_swap (h, cur, child);
193             cur = child;
194             child = 2*cur;
195         }
196         else
197             break;
198     }
199 }
200
201
202 static void heap_insert (HEAP h, struct heap_item *hi)
203 {
204     int cur, parent;
205
206     cur = ++(h->heapnum);
207     assert(cur <= h->heapmax);
208     h->heap[cur]=hi;
209     parent = cur/2;
210     while (parent && (heap_cmp(h,parent,cur) > 0))
211     {
212         assert(parent>0);
213         heap_swap (h, cur, parent);
214         cur = parent;
215         parent = cur/2;
216     }
217 }
218
219
220 static
221 HEAP heap_create (NMEM nmem, int size, const struct key_control *kctrl)
222 {
223     HEAP h = (HEAP) nmem_malloc (nmem, sizeof(*h));
224
225     ++size; /* heap array starts at 1 */
226     h->heapnum = 0;
227     h->heapmax = size;
228     h->kctrl=kctrl;
229     h->heap = (struct heap_item**) nmem_malloc(nmem,size*sizeof(*h->heap));
230     h->heap[0]=0; /* not used */
231     return h;
232 }
233
234 static void heap_clear( HEAP h)
235 {
236     assert(h);
237     h->heapnum=0;
238 }
239
240 static void heap_destroy (HEAP h)
241 {
242     /* nothing to delete, all is nmem'd, and will go away in due time */
243 }
244
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 */
256 }
257
258 /* Creating and deleting rsets ***********************/
259
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)
263 {
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));
270     rnew->priv=info;
271     return rnew;
272 }
273
274 RSET rsmultior_create( NMEM nmem, const struct key_control *kcontrol, int scope,
275             int no_rsets, RSET* rsets)
276 {
277     return rsmulti_andor_create(nmem, kcontrol, scope, 
278                                 no_rsets, rsets, &control_or);
279 }
280
281 RSET rsmultiand_create( NMEM nmem, const struct key_control *kcontrol, int scope,
282             int no_rsets, RSET* rsets)
283 {
284     return rsmulti_andor_create(nmem, kcontrol, scope, 
285                                 no_rsets, rsets, &control_and);
286 }
287
288 static void r_delete (RSET ct)
289 {
290     struct rset_multiandor_info *info = (struct rset_multiandor_info *) ct->priv;
291     int i;
292     for(i=0;i<info->no_rsets;i++)
293         rset_delete(info->rsets[i]);
294 }
295
296
297 /* Opening and closing fd's on them *********************/
298
299 static RSFD r_open_andor (RSET ct, int flag, int is_and)
300 {
301     RSFD rfd;
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;
305     int i;
306
307     if (flag & RSETF_WRITE)
308     {
309         logf (LOG_FATAL, "multior set type is read-only");
310         return NULL;
311     }
312     rfd=rfd_create_base(ct);
313     if (rfd->priv) {
314         p=(struct rset_multiandor_rfd *)rfd->priv;
315         if (!is_and)
316             heap_clear(p->h);
317         assert(p->items);
318         /* all other pointers shouls already be allocated, in right sizes! */
319     }
320     else {
321         p = (struct rset_multiandor_rfd *) nmem_malloc (ct->nmem,sizeof(*p));
322         rfd->priv=p;
323         p->h=0;
324         p->tailbits=0;
325         if (is_and)
326             p->tailbits=nmem_malloc(ct->nmem, info->no_rsets*sizeof(char) );
327         else 
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);
334         }
335     }
336     p->flag = flag;
337     p->hits=0;
338     p->eof=0;
339     p->tailcount=0;
340     if (is_and)
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) )
345                 p->eof=1;
346             p->tailbits[i]=0;
347         }
348         qsort(p->items, info->no_rsets, sizeof(p->items[0]), compare_ands);
349     } else
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]));
355         }
356     }
357     return rfd;
358 }
359
360 static RSFD r_open_or (RSET ct, int flag)
361 {
362     return r_open_andor(ct, flag, 0);
363 }
364
365 static RSFD r_open_and (RSET ct, int flag)
366 {
367     return r_open_andor(ct, flag, 1);
368 }
369
370
371 static void r_close (RSFD rfd)
372 {
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);
376     int i;
377
378     if (p->h)
379         heap_destroy (p->h);
380     for (i = 0; i<info->no_rsets; i++) 
381         if (p->items[i].fd)
382             rset_close(p->items[i].fd);
383     rfd_delete_base(rfd);
384 }
385
386
387
388 static int r_forward_or(RSFD rfd, void *buf, const void *untilbuf)
389 {
390     struct rset_multiandor_rfd *mrfd=rfd->priv;
391     const struct key_control *kctrl=rfd->rset->keycontrol;
392     struct heap_item it;
393     int rdres;
394     if (heap_empty(mrfd->h))
395         return 0;
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! */
401     (mrfd->hits)++;
402     if (untilbuf)
403         rdres=rset_forward(it.fd, it.buf, untilbuf);
404     else
405         rdres=rset_read(it.fd, it.buf);
406     if ( rdres )
407         heap_balance(mrfd->h);
408     else
409         heap_delete(mrfd->h);
410     return 1;
411
412 }
413
414 static int r_read_or (RSFD rfd, void *buf)
415 {
416     return r_forward_or(rfd, buf,0);
417 }
418
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;
431     int i, mintail;
432     int cmp;
433
434     while (1) {
435         if (p->tailcount) 
436         { /* we are tailing, find lowest tail and return it */
437             mintail=0;
438             while ((mintail<info->no_rsets) && !p->tailbits[mintail])
439                 mintail++; /* first tail */
440             for (i=mintail+1;i<info->no_rsets;i++)
441             {
442                 if (p->tailbits[i])
443                 {
444                     cmp=(*kctrl->cmp)(p->items[i].buf,p->items[mintail].buf);
445                     if (cmp<0) 
446                         mintail=i;
447                 }
448             }
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))
452             {
453                 p->eof=1; /* game over, once tails have been returned */
454                 p->tailbits[mintail]=0; 
455                 (p->tailcount)--;
456                 return 1;
457             }
458             /* still a tail? */
459             cmp=(*kctrl->cmp)(p->items[mintail].buf,buf);
460             if (cmp >= rfd->rset->scope){
461                 p->tailbits[mintail]=0;
462                 (p->tailcount)--;
463             }
464             return 1;
465         } 
466         /* not tailing, forward until all reocrds match, and set up */
467         /* as tails. the earlier 'if' will then return the hits */
468         if (p->eof)
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, 
475                                   p->items[i].buf))
476                 {
477                     p->eof=1; /* game over */
478                     return 0;
479                 }
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, 
484                                   p->items[0].buf))
485                 {
486                     p->eof=1; /* game over */
487                     return 0;
488                 }
489             } else
490                 i++;
491         } /* while i */
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++)
496             p->tailbits[i]=1;
497         p->tailcount=info->no_rsets;
498     } /* while 1 */
499 }
500
501
502 static int r_forward_and(RSFD rfd, void *buf, const void *untilbuf)
503
504     struct rset_multiandor_rfd *p=rfd->priv;
505     const struct key_control *kctrl=rfd->rset->keycontrol;
506     struct rset_multiandor_info *info=rfd->rset->priv;
507     int i;
508     int cmp;
509     int killtail=0;
510
511     for (i=0; i<info->no_rsets;i++)
512     {
513         cmp=(*kctrl->cmp)(p->items[i].buf,untilbuf);
514         if ( cmp <= -rfd->rset->scope )
515         {
516             killtail=1; /* we are moving to a different hit */
517             if (!rset_forward(p->items[i].fd, p->items[i].buf, 
518                               untilbuf))
519             {
520                 p->eof=1; /* game over */
521                 p->tailcount=0;
522                 return 0;
523             }
524         }
525     }
526     if (killtail) 
527     {
528         for (i=0; i<info->no_rsets;i++)
529             p->tailbits[i]=0;
530         p->tailcount=0;
531     }
532     return r_read_and(rfd,buf);
533 }
534
535 static void r_pos (RSFD rfd, double *current, double *total)
536 {
537     struct rset_multiandor_info *info=
538              (struct rset_multiandor_info *)(rfd->rset->priv);
539     struct rset_multiandor_rfd *mrfd=(struct rset_multiandor_rfd *)(rfd->priv);
540     double cur, tot;
541     double scur=0.0, stot=0.0;
542     int i;
543     for (i=0; i<info->no_rsets; i++){
544         rset_pos(mrfd->items[i].fd, &cur, &tot);
545         logf(LOG_DEBUG, "r_pos: %d %0.1f %0.1f", i, cur,tot);
546         scur += cur;
547         stot += tot;
548     }
549     if (stot <1.0) { /* nothing there */
550         *current=0;
551         *total=0;
552         return;
553     }
554     *current=mrfd->hits;
555     *total=*current*stot/scur;
556 }
557
558
559 static void r_rewind (RSFD rfd)
560 {
561     assert(!"rewind not implemented yet");
562     /* FIXME - rewind all parts, rebalance heap, clear hits */
563 }
564
565 static int r_write (RSFD rfd, const void *buf)
566 {
567     logf (LOG_FATAL, "multior set type is read-only");
568     return -1;
569 }