estimatehits config option,
[idzebra-moved-to-github.git] / rset / rsbool.c
1 /* $Id: rsbool.c,v 1.34 2004-08-06 09:43:03 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27
28 #include <zebrautl.h>
29 #include <rset.h>
30 #include <rsbool.h>
31
32 #ifndef RSET_DEBUG
33 #define RSET_DEBUG 0
34 #endif
35
36 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
37 static RSFD r_open (RSET ct, int flag);
38 static void r_close (RSFD rfd);
39 static void r_delete (RSET ct);
40 static void r_rewind (RSFD rfd);
41 static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index,
42                      int (*cmpfunc)(const void *p1, const void *p2),
43                      const void *untilbuf);
44 static void r_pos (RSFD rfd, zint *current, zint *total); 
45 static int r_read_and (RSFD rfd, void *buf, int *term_index);
46 static int r_read_or (RSFD rfd, void *buf, int *term_index);
47 static int r_read_not (RSFD rfd, void *buf, int *term_index);
48 static int r_write (RSFD rfd, const void *buf);
49
50 static const struct rset_control control_and = 
51 {
52     "and",
53     r_create,
54     r_open,
55     r_close,
56     r_delete,
57     r_rewind,
58     r_forward, /* rset_default_forward, */
59     r_pos,     /* rset_default_pos */
60     r_read_and,
61     r_write,
62 };
63
64 static const struct rset_control control_or = 
65 {
66     "or",
67     r_create,
68     r_open,
69     r_close,
70     r_delete,
71     r_rewind,
72     r_forward, 
73     r_pos,
74     r_read_or,
75     r_write,
76 };
77
78 static const struct rset_control control_not = 
79 {
80     "not",
81     r_create,
82     r_open,
83     r_close,
84     r_delete,
85     r_rewind,
86     r_forward, 
87     r_pos,
88     r_read_not,
89     r_write,
90 };
91
92
93 const struct rset_control *rset_kind_and = &control_and;
94 const struct rset_control *rset_kind_or = &control_or;
95 const struct rset_control *rset_kind_not = &control_not;
96
97 struct rset_bool_info {
98     int key_size;
99     RSET rset_l;
100     RSET rset_r;
101     int term_index_s;
102     int (*cmp)(const void *p1, const void *p2);
103     void (*log_item)(int logmask, const void *p, const char *txt);
104     struct rset_bool_rfd *rfd_list;
105 };
106
107 struct rset_bool_rfd {
108     zint hits;
109     RSFD rfd_l;
110     RSFD rfd_r;
111     int  more_l;
112     int  more_r;
113     int term_index_l;
114     int term_index_r;
115     void *buf_l;
116     void *buf_r;
117     int tail;
118     struct rset_bool_rfd *next;
119     struct rset_bool_info *info;
120 };    
121
122 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
123 {
124     rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
125     struct rset_bool_info *info;
126
127     info = (struct rset_bool_info *) xmalloc (sizeof(*info));
128     info->key_size = bool_parms->key_size;
129     info->rset_l = bool_parms->rset_l;
130     info->rset_r = bool_parms->rset_r;
131     if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
132         ct->flags |= RSET_FLAG_VOLATILE;
133     info->cmp = bool_parms->cmp;
134     info->log_item = bool_parms->log_item;
135     info->rfd_list = NULL;
136     
137     info->term_index_s = info->rset_l->no_rset_terms;
138     ct->no_rset_terms =
139         info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
140     ct->rset_terms = (RSET_TERM *)
141         xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
142
143     memcpy (ct->rset_terms, info->rset_l->rset_terms,
144             info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
145     memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
146             info->rset_r->rset_terms,
147             info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
148     return info;
149 }
150
151 static RSFD r_open (RSET ct, int flag)
152 {
153     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
154     struct rset_bool_rfd *rfd;
155
156     if (flag & RSETF_WRITE)
157     {
158         logf (LOG_FATAL, "bool set type is read-only");
159         return NULL;
160     }
161     rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
162     logf(LOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
163     rfd->next = info->rfd_list;
164     info->rfd_list = rfd;
165     rfd->info = info;
166     rfd->hits=0;
167
168     rfd->buf_l = xmalloc (info->key_size);
169     rfd->buf_r = xmalloc (info->key_size);
170     rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
171     rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
172     rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
173                              &rfd->term_index_l);
174     rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
175                              &rfd->term_index_r);
176     rfd->tail = 0;
177     return rfd;
178 }
179
180 static void r_close (RSFD rfd)
181 {
182     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
183     struct rset_bool_rfd **rfdp;
184     
185     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
186         if (*rfdp == rfd)
187         {
188             xfree ((*rfdp)->buf_l);
189             xfree ((*rfdp)->buf_r);
190             rset_close (info->rset_l, (*rfdp)->rfd_l);
191             rset_close (info->rset_r, (*rfdp)->rfd_r);
192             *rfdp = (*rfdp)->next;
193             xfree (rfd);
194             return;
195         }
196     logf (LOG_FATAL, "r_close but no rfd match!");
197     assert (0);
198 }
199
200 static void r_delete (RSET ct)
201 {
202     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
203
204     assert (info->rfd_list == NULL);
205     xfree (ct->rset_terms);
206     rset_delete (info->rset_l);
207     rset_delete (info->rset_r);
208     xfree (info);
209 }
210
211 static void r_rewind (RSFD rfd)
212 {
213     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
214     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
215
216     logf (LOG_DEBUG, "rsbool_rewind");
217     rset_rewind (info->rset_l, p->rfd_l);
218     rset_rewind (info->rset_r, p->rfd_r);
219     p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
220     p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
221     p->hits=0;
222 }
223
224 static int r_forward (RSET ct, RSFD rfd, void *buf, int *term_index,
225                      int (*cmpfunc)(const void *p1, const void *p2),
226                      const void *untilbuf)
227 {
228     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
229     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
230     int rc;
231
232 #if RSET_DEBUG
233     logf (LOG_DEBUG, "rsbool_forward (L) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
234                       rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
235 #endif
236     if ( p->more_l && ((cmpfunc)(untilbuf,p->buf_l)==2) )
237         p->more_l = rset_forward(info->rset_l, p->rfd_l, p->buf_l,
238                         &p->term_index_l, info->cmp, untilbuf);
239 #if RSET_DEBUG
240     logf (LOG_DEBUG, "rsbool_forward (R) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
241                       rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
242 #endif
243     if ( p->more_r && ((cmpfunc)(untilbuf,p->buf_r)==2))
244         p->more_r = rset_forward(info->rset_r, p->rfd_r, p->buf_r,
245                         &p->term_index_r, info->cmp, untilbuf);
246 #if RSET_DEBUG
247     logf (LOG_DEBUG, "rsbool_forward [%p] calling read, m=%d,%d t=%d", 
248                        rfd, p->more_l, p->more_r, p->tail);
249 #endif
250     
251     p->tail=0; 
252     rc = rset_read(ct,rfd,buf,term_index); 
253 #if RSET_DEBUG
254     logf (LOG_DEBUG, "rsbool_forward returning [%p] %d m=%d,%d", 
255                        rfd, rc, p->more_l, p->more_r);
256 #endif
257     return rc;
258 }
259
260
261 /*
262     1,1         1,3
263     1,9         2,1
264     1,11        3,1
265     2,9
266
267   1,1     1,1
268   1,3     1,3
269           1,9
270           1,11
271   2,1     2,1
272           2,9
273           3,1
274 */
275
276 static int r_read_and (RSFD rfd, void *buf, int *term_index)
277 {
278     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
279     struct rset_bool_info *info = p->info;
280
281     { zint cur,tot; r_pos(rfd, &cur, &tot); } 
282
283     while (p->more_l || p->more_r)
284     {
285         int cmp;
286
287         if (p->more_l && p->more_r)
288             cmp = (*info->cmp)(p->buf_l, p->buf_r);
289         else if (p->more_l)
290             cmp = -2;
291         else
292             cmp = 2;
293 #if RSET_DEBUG
294         logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
295                         rfd, p->more_l, p->more_r, cmp, p->tail);
296         (*info->log_item)(LOG_DEBUG, p->buf_l, "left ");
297         (*info->log_item)(LOG_DEBUG, p->buf_r, "right ");
298 #endif
299         if (!cmp)
300         {
301             memcpy (buf, p->buf_l, info->key_size);
302                 *term_index = p->term_index_l;
303             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
304                                    &p->term_index_l);
305             p->tail = 1;
306         }
307         else if (cmp == 1)
308         {
309             memcpy (buf, p->buf_r, info->key_size);
310                 *term_index = p->term_index_r + info->term_index_s;
311             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
312                                    &p->term_index_r);
313             p->tail = 1;
314 #if RSET_DEBUG
315             logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
316                     rfd, p->more_l, p->more_r, cmp);
317             key_logdump(LOG_DEBUG,buf);
318             (*info->log_item)(LOG_DEBUG, buf, "");
319 #endif
320             p->hits++;
321             return 1;
322         }
323         else if (cmp == -1)
324         {
325             memcpy (buf, p->buf_l, info->key_size);
326                 *term_index = p->term_index_l;
327             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
328                                    &p->term_index_l);
329             p->tail = 1;
330 #if RSET_DEBUG
331             logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
332                     rfd, p->more_l, p->more_r, cmp);
333             (*info->log_item)(LOG_DEBUG, buf, "");
334 #endif
335             p->hits++;
336             return 1;
337         }
338         else if (cmp > 1)  /* cmp == 2 */
339         {
340 #define OLDCODE 0
341 #if OLDCODE
342             memcpy (buf, p->buf_r, info->key_size);
343             *term_index = p->term_index_r + info->term_index_s;
344             
345             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
346                                    &p->term_index_r);
347             if (p->tail)
348             {
349                 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
350                     p->tail = 0;
351 #if RSET_DEBUG
352                 logf (LOG_DEBUG, "r_read_and returning C m=%d/%d c=%d",
353                         p->more_l, p->more_r, cmp);
354                 (*info->log_item)(LOG_DEBUG, buf, "");
355 #endif
356                 p->hits++;
357                 return 1;
358             }
359 #else
360             
361             if (p->tail)
362             {
363                 memcpy (buf, p->buf_r, info->key_size);
364                 *term_index = p->term_index_r + info->term_index_s;
365                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
366                                    &p->term_index_r);
367                 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
368                     p->tail = 0;
369 #if RSET_DEBUG
370                 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
371                         rfd, p->more_l, p->more_r, cmp);
372                 (*info->log_item)(LOG_DEBUG, buf, "");
373 #endif
374                 p->hits++;
375                 return 1;
376             }
377             else
378             {
379 #if RSET_DEBUG
380                 logf (LOG_DEBUG, "r_read_and [%p] about to forward R m=%d/%d c=%d",
381                         rfd, p->more_l, p->more_r, cmp);
382 #endif
383                 if (p->more_r && p->more_l)
384                     p->more_r = rset_forward( 
385                                     info->rset_r, p->rfd_r, 
386                                     p->buf_r, &p->term_index_r, 
387                                     (info->cmp), p->buf_l);
388                 else 
389                     return 0; /* no point in reading further */
390             }
391 #endif
392         }
393         else  /* cmp == -2 */
394         {
395 #if OLDCODE
396              memcpy (buf, p->buf_l, info->key_size);
397              *term_index = p->term_index_l;
398              p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
399                                     &p->term_index_l);
400              if (p->tail)
401              {
402                  if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
403                      p->tail = 0;
404 #if RSET_DEBUG
405                  logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
406                         rfd, p->more_l, p->more_r, cmp);
407                  (*info->log_item)(LOG_DEBUG, buf, "");
408 #endif
409                  p->hits++;
410                  return 1;
411              }
412 #else
413             if (p->tail)
414             {
415                 memcpy (buf, p->buf_l, info->key_size);
416                     *term_index = p->term_index_l;
417                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
418                                    &p->term_index_l);
419                 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
420                     p->tail = 0;
421 #if RSET_DEBUG
422                 logf (LOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
423                         rfd, p->more_l, p->more_r, cmp);
424                 (*info->log_item)(LOG_DEBUG, buf, "");
425 #endif
426                 p->hits++;
427                 return 1;
428             }
429             else
430             {
431 #if RSET_DEBUG
432                 logf (LOG_DEBUG, "r_read_and [%p] about to forward L m=%d/%d c=%d",
433                         rfd, p->more_l, p->more_r, cmp);
434 #endif
435                 if (p->more_r && p->more_l)
436                     p->more_l = rset_forward( 
437                     /* p->more_l = rset_default_forward( */
438                                     info->rset_l, p->rfd_l, 
439                                     p->buf_l, &p->term_index_l, 
440                                     (info->cmp), p->buf_r);
441                 else 
442                     return 0; /* no point in reading further */
443             }
444 #endif
445         }
446     }
447 #if RSET_DEBUG
448     logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
449 #endif
450     return 0;
451 }
452
453 static int r_read_or (RSFD rfd, void *buf, int *term_index)
454 {
455     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
456     struct rset_bool_info *info = p->info;
457
458     { zint cur,tot; r_pos(rfd, &cur, &tot); }
459     while (p->more_l || p->more_r)
460     {
461         int cmp;
462
463         if (p->more_l && p->more_r)
464             cmp = (*info->cmp)(p->buf_l, p->buf_r);
465         else if (p->more_r)
466             cmp = 2;
467         else
468             cmp = -2;
469         if (!cmp)
470         {
471             memcpy (buf, p->buf_l, info->key_size);
472                 *term_index = p->term_index_l;
473             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
474                                    &p->term_index_l);
475             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
476                                    &p->term_index_r);
477 #if RSET_DEBUG
478             logf (LOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
479                     p->more_l, p->more_r, cmp);
480             (*info->log_item)(LOG_DEBUG, buf, "");
481 #endif
482             p->hits++;
483             return 1;
484         }
485         else if (cmp > 0)
486         {
487             memcpy (buf, p->buf_r, info->key_size);
488                 *term_index = p->term_index_r + info->term_index_s;
489             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
490                                    &p->term_index_r);
491 #if RSET_DEBUG
492             logf (LOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
493                     p->more_l, p->more_r, cmp);
494             (*info->log_item)(LOG_DEBUG, buf, "");
495 #endif
496             p->hits++;
497             return 1;
498         }
499         else
500         {
501             memcpy (buf, p->buf_l, info->key_size);
502                 *term_index = p->term_index_l;
503             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
504                                    &p->term_index_l);
505 #if RSET_DEBUG
506             logf (LOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
507                     p->more_l, p->more_r, cmp);
508             (*info->log_item)(LOG_DEBUG, buf, "");
509 #endif
510             p->hits++;
511             return 1;
512         }
513     }
514     return 0;
515 }
516
517 static int r_read_not (RSFD rfd, void *buf, int *term_index)
518 {
519     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
520     struct rset_bool_info *info = p->info;
521
522     { zint cur,tot; r_pos(rfd, &cur, &tot); }
523     while (p->more_l || p->more_r)
524     {
525         int cmp;
526
527         if (p->more_l && p->more_r)
528             cmp = (*info->cmp)(p->buf_l, p->buf_r);
529         else if (p->more_r)
530             cmp = 2;
531         else
532             cmp = -2;
533         if (cmp < -1)
534         {
535             memcpy (buf, p->buf_l, info->key_size);
536                 *term_index = p->term_index_l;
537             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
538                                    &p->term_index_l);
539             p->hits++;
540             return 1;
541         }
542         else if (cmp > 1)
543         {
544                 p->more_r = rset_forward( 
545                     info->rset_r, p->rfd_r, 
546                     p->buf_r, &p->term_index_r, 
547                     (info->cmp), p->buf_l);
548         }
549         else
550         {
551             memcpy (buf, p->buf_l, info->key_size);
552             do
553             { 
554                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
555                                        &p->term_index_l);
556                 if (!p->more_l)
557                     break;
558                 cmp = (*info->cmp)(p->buf_l, buf);
559             } while (cmp >= -1 && cmp <= 1);
560             do
561             {
562                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
563                                        &p->term_index_r);
564                 if (!p->more_r)
565                     break;
566                 cmp = (*info->cmp)(p->buf_r, buf);
567             } while (cmp >= -1 && cmp <= 1);
568         }
569     }
570     return 0;
571 }
572
573
574 static int r_write (RSFD rfd, const void *buf)
575 {
576     logf (LOG_FATAL, "bool set type is read-only");
577     return -1;
578 }
579
580 static void r_pos (RSFD rfd, zint *current, zint *total)
581 {
582     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
583     struct rset_bool_info *info = p->info;
584     zint lcur,ltot;
585     zint rcur,rtot;
586     float r;
587     ltot=-1; rtot=-1;
588     rset_pos(info->rset_l, p->rfd_l,  &lcur, &ltot);
589     rset_pos(info->rset_r, p->rfd_r,  &rcur, &rtot);
590     if ( (rtot<0) && (ltot<0)) { /*no position */
591         *current=rcur;  /* return same as you got */
592         *total=rtot;    /* probably -1 for not available */
593     }
594     if ( rtot<0) { rtot=0; rcur=0;} /* if only one useful, use it */
595     if ( ltot<0) { ltot=0; lcur=0;}
596     if ( rtot+ltot == 0 ) { /* empty rset */
597         *current=0;
598         *total=0;
599         return;
600     }
601     r=1.0*(lcur+rcur)/(ltot+rtot); /* weighed average of l and r */
602     *current=p->hits;
603     *total=(zint)(0.5+*current/r); 
604 #if RSET_DEBUG
605     yaz_log(LOG_DEBUG,"bool_pos: (%s/%s) "ZINT_FORMAT"/"ZINT_FORMAT"= %0.4f ",
606                     info->rset_l->control->desc, info->rset_r->control->desc,
607                     *current, *total, r);
608 #endif
609 }