Removed the term count stuff from all rsets, and fixed what ever that broke.
[idzebra-moved-to-github.git] / rset / rsbool.c
1 /* $Id: rsbool.c,v 1.38 2004-08-20 14:44:46 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,
42                      int (*cmpfunc)(const void *p1, const void *p2),
43                      const void *untilbuf);
44 static void r_pos (RSFD rfd, double *current, double *total); 
45 static int r_read_and (RSFD rfd, void *buf);
46 static int r_read_or (RSFD rfd, void *buf);
47 static int r_read_not (RSFD rfd, void *buf);
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, 
59     r_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 (*cmp)(const void *p1, const void *p2);
102     void (*log_item)(int logmask, const void *p, const char *txt);
103     struct rset_bool_rfd *rfd_list;
104 };
105
106 struct rset_bool_rfd {
107     zint hits;
108     RSFD rfd_l;
109     RSFD rfd_r;
110     int  more_l;
111     int  more_r;
112     void *buf_l;
113     void *buf_r;
114     int tail;
115     struct rset_bool_rfd *next;
116     struct rset_bool_info *info;
117 };    
118
119 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
120 {
121     rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
122     struct rset_bool_info *info;
123
124     info = (struct rset_bool_info *) xmalloc (sizeof(*info));
125     info->key_size = bool_parms->key_size;
126     info->rset_l = bool_parms->rset_l;
127     info->rset_r = bool_parms->rset_r;
128     if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
129         ct->flags |= RSET_FLAG_VOLATILE;
130     info->cmp = bool_parms->cmp;
131     info->log_item = bool_parms->log_item;
132     info->rfd_list = NULL;
133     
134     return info;
135 }
136
137 static RSFD r_open (RSET ct, int flag)
138 {
139     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
140     struct rset_bool_rfd *rfd;
141
142     if (flag & RSETF_WRITE)
143     {
144         logf (LOG_FATAL, "bool set type is read-only");
145         return NULL;
146     }
147     rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
148     logf(LOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
149     rfd->next = info->rfd_list;
150     info->rfd_list = rfd;
151     rfd->info = info;
152     rfd->hits=0;
153
154     rfd->buf_l = xmalloc (info->key_size);
155     rfd->buf_r = xmalloc (info->key_size);
156     rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
157     rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
158     rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l);
159     rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r);
160     rfd->tail = 0;
161     return rfd;
162 }
163
164 static void r_close (RSFD rfd)
165 {
166     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
167     struct rset_bool_rfd **rfdp;
168     
169     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
170         if (*rfdp == rfd)
171         {
172             xfree ((*rfdp)->buf_l);
173             xfree ((*rfdp)->buf_r);
174             rset_close (info->rset_l, (*rfdp)->rfd_l);
175             rset_close (info->rset_r, (*rfdp)->rfd_r);
176             *rfdp = (*rfdp)->next;
177             xfree (rfd);
178             return;
179         }
180     logf (LOG_FATAL, "r_close but no rfd match!");
181     assert (0);
182 }
183
184 static void r_delete (RSET ct)
185 {
186     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
187
188     assert (info->rfd_list == NULL);
189     rset_delete (info->rset_l);
190     rset_delete (info->rset_r);
191     xfree (info);
192 }
193
194 static void r_rewind (RSFD rfd)
195 {
196     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
197     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
198
199     logf (LOG_DEBUG, "rsbool_rewind");
200     rset_rewind (info->rset_l, p->rfd_l);
201     rset_rewind (info->rset_r, p->rfd_r);
202     p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
203     p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
204     p->hits=0;
205 }
206
207 static int r_forward (RSET ct, RSFD rfd, void *buf,
208                      int (*cmpfunc)(const void *p1, const void *p2),
209                      const void *untilbuf)
210 {
211     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
212     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
213     int rc;
214
215 #if RSET_DEBUG
216     logf (LOG_DEBUG, "rsbool_forward (L) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
217                       rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
218 #endif
219     if ( p->more_l && ((cmpfunc)(untilbuf,p->buf_l)==2) )
220         p->more_l = rset_forward(info->rset_l, p->rfd_l, p->buf_l,
221                         info->cmp, untilbuf);
222 #if RSET_DEBUG
223     logf (LOG_DEBUG, "rsbool_forward (R) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
224                       rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
225 #endif
226     if ( p->more_r && ((cmpfunc)(untilbuf,p->buf_r)==2))
227         p->more_r = rset_forward(info->rset_r, p->rfd_r, p->buf_r,
228                         info->cmp, untilbuf);
229 #if RSET_DEBUG
230     logf (LOG_DEBUG, "rsbool_forward [%p] calling read, m=%d,%d t=%d", 
231                        rfd, p->more_l, p->more_r, p->tail);
232 #endif
233     
234     p->tail=0; 
235     rc = rset_read(ct,rfd,buf); 
236 #if RSET_DEBUG
237     logf (LOG_DEBUG, "rsbool_forward returning [%p] %d m=%d,%d", 
238                        rfd, rc, p->more_l, p->more_r);
239 #endif
240     return rc;
241 }
242
243
244 /*
245     1,1         1,3
246     1,9         2,1
247     1,11        3,1
248     2,9
249
250   1,1     1,1
251   1,3     1,3
252           1,9
253           1,11
254   2,1     2,1
255           2,9
256           3,1
257 */
258
259 static int r_read_and (RSFD rfd, void *buf)
260 {
261     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
262     struct rset_bool_info *info = p->info;
263
264     while (p->more_l || p->more_r)
265     {
266         int cmp;
267
268         if (p->more_l && p->more_r)
269             cmp = (*info->cmp)(p->buf_l, p->buf_r);
270         else if (p->more_l)
271             cmp = -2;
272         else
273             cmp = 2;
274 #if RSET_DEBUG
275         logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
276                         rfd, p->more_l, p->more_r, cmp, p->tail);
277         (*info->log_item)(LOG_DEBUG, p->buf_l, "left ");
278         (*info->log_item)(LOG_DEBUG, p->buf_r, "right ");
279 #endif
280         if (!cmp)
281         {
282             memcpy (buf, p->buf_l, info->key_size);
283             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
284             p->tail = 1;
285         }
286         else if (cmp == 1)
287         {
288             memcpy (buf, p->buf_r, info->key_size);
289             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
290             p->tail = 1;
291 #if RSET_DEBUG
292             logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
293                     rfd, p->more_l, p->more_r, cmp);
294             key_logdump(LOG_DEBUG,buf);
295             (*info->log_item)(LOG_DEBUG, buf, "");
296 #endif
297             p->hits++;
298             return 1;
299         }
300         else if (cmp == -1)
301         {
302             memcpy (buf, p->buf_l, info->key_size);
303             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
304             p->tail = 1;
305 #if RSET_DEBUG
306             logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
307                     rfd, p->more_l, p->more_r, cmp);
308             (*info->log_item)(LOG_DEBUG, buf, "");
309 #endif
310             p->hits++;
311             return 1;
312         }
313         else if (cmp > 1)  /* cmp == 2 */
314         {
315 #define OLDCODE 0
316 #if OLDCODE
317             memcpy (buf, p->buf_r, info->key_size);
318             
319             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
320             if (p->tail)
321             {
322                 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
323                     p->tail = 0;
324 #if RSET_DEBUG
325                 logf (LOG_DEBUG, "r_read_and returning C m=%d/%d c=%d",
326                         p->more_l, p->more_r, cmp);
327                 (*info->log_item)(LOG_DEBUG, buf, "");
328 #endif
329                 p->hits++;
330                 return 1;
331             }
332 #else
333             
334             if (p->tail)
335             {
336                 memcpy (buf, p->buf_r, info->key_size);
337                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
338                 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
339                     p->tail = 0;
340 #if RSET_DEBUG
341                 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
342                         rfd, p->more_l, p->more_r, cmp);
343                 (*info->log_item)(LOG_DEBUG, buf, "");
344 #endif
345                 p->hits++;
346                 return 1;
347             }
348             else
349             {
350 #if RSET_DEBUG
351                 logf (LOG_DEBUG, "r_read_and [%p] about to forward R m=%d/%d c=%d",
352                         rfd, p->more_l, p->more_r, cmp);
353 #endif
354                 if (p->more_r && p->more_l)
355                     p->more_r = rset_forward( info->rset_r, p->rfd_r, 
356                                     p->buf_r, (info->cmp), p->buf_l);
357                 else 
358                     return 0; /* no point in reading further */
359             }
360 #endif
361         }
362         else  /* cmp == -2 */
363         {
364 #if OLDCODE
365              memcpy (buf, p->buf_l, info->key_size);
366              p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
367              if (p->tail)
368              {
369                  if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
370                      p->tail = 0;
371 #if RSET_DEBUG
372                  logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
373                         rfd, p->more_l, p->more_r, cmp);
374                  (*info->log_item)(LOG_DEBUG, buf, "");
375 #endif
376                  p->hits++;
377                  return 1;
378              }
379 #else
380             if (p->tail)
381             {
382                 memcpy (buf, p->buf_l, info->key_size);
383                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
384                 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
385                     p->tail = 0;
386 #if RSET_DEBUG
387                 logf (LOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
388                         rfd, p->more_l, p->more_r, cmp);
389                 (*info->log_item)(LOG_DEBUG, buf, "");
390 #endif
391                 p->hits++;
392                 return 1;
393             }
394             else
395             {
396 #if RSET_DEBUG
397                 logf (LOG_DEBUG, "r_read_and [%p] about to forward L m=%d/%d c=%d",
398                         rfd, p->more_l, p->more_r, cmp);
399 #endif
400                 if (p->more_r && p->more_l)
401                     p->more_l = rset_forward( 
402                                     info->rset_l, p->rfd_l, 
403                                     p->buf_l, (info->cmp), p->buf_r);
404                 else 
405                     return 0; /* no point in reading further */
406             }
407 #endif
408         }
409     }
410 #if RSET_DEBUG
411     logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
412 #endif
413     return 0;
414 }
415
416 static int r_read_or (RSFD rfd, void *buf)
417 {
418     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
419     struct rset_bool_info *info = p->info;
420
421     while (p->more_l || p->more_r)
422     {
423         int cmp;
424
425         if (p->more_l && p->more_r)
426             cmp = (*info->cmp)(p->buf_l, p->buf_r);
427         else if (p->more_r)
428             cmp = 2;
429         else
430             cmp = -2;
431         if (!cmp)
432         {
433             memcpy (buf, p->buf_l, info->key_size);
434             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
435             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
436 #if RSET_DEBUG
437             logf (LOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
438                     p->more_l, p->more_r, cmp);
439             (*info->log_item)(LOG_DEBUG, buf, "");
440 #endif
441             p->hits++;
442             return 1;
443         }
444         else if (cmp > 0)
445         {
446             memcpy (buf, p->buf_r, info->key_size);
447             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
448 #if RSET_DEBUG
449             logf (LOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
450                     p->more_l, p->more_r, cmp);
451             (*info->log_item)(LOG_DEBUG, buf, "");
452 #endif
453             p->hits++;
454             return 1;
455         }
456         else
457         {
458             memcpy (buf, p->buf_l, info->key_size);
459             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
460 #if RSET_DEBUG
461             logf (LOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
462                     p->more_l, p->more_r, cmp);
463             (*info->log_item)(LOG_DEBUG, buf, "");
464 #endif
465             p->hits++;
466             return 1;
467         }
468     }
469     return 0;
470 }
471
472 static int r_read_not (RSFD rfd, void *buf)
473 {
474     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
475     struct rset_bool_info *info = p->info;
476
477     while (p->more_l || p->more_r)
478     {
479         int cmp;
480
481         if (p->more_l && p->more_r)
482             cmp = (*info->cmp)(p->buf_l, p->buf_r);
483         else if (p->more_r)
484             cmp = 2;
485         else
486             cmp = -2;
487         if (cmp < -1)
488         {
489             memcpy (buf, p->buf_l, info->key_size);
490             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
491             p->hits++;
492             return 1;
493         }
494         else if (cmp > 1)
495         {
496                 p->more_r = rset_forward( 
497                     info->rset_r, p->rfd_r, 
498                     p->buf_r, (info->cmp), p->buf_l);
499         }
500         else
501         {
502             memcpy (buf, p->buf_l, info->key_size);
503             do
504             { 
505                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
506                 if (!p->more_l)
507                     break;
508                 cmp = (*info->cmp)(p->buf_l, buf);
509             } while (cmp >= -1 && cmp <= 1);
510             do
511             {
512                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
513                 if (!p->more_r)
514                     break;
515                 cmp = (*info->cmp)(p->buf_r, buf);
516             } while (cmp >= -1 && cmp <= 1);
517         }
518     }
519     return 0;
520 }
521
522
523 static int r_write (RSFD rfd, const void *buf)
524 {
525     logf (LOG_FATAL, "bool set type is read-only");
526     return -1;
527 }
528
529 static void r_pos (RSFD rfd, double *current, double *total)
530 {
531     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
532     struct rset_bool_info *info = p->info;
533     double lcur,ltot;
534     double rcur,rtot;
535     double r;
536     ltot=-1; rtot=-1;
537     rset_pos(info->rset_l, p->rfd_l,  &lcur, &ltot);
538     rset_pos(info->rset_r, p->rfd_r,  &rcur, &rtot);
539     if ( (rtot<0) && (ltot<0)) { /*no position */
540         *current=rcur;  /* return same as you got */
541         *total=rtot;    /* probably -1 for not available */
542     }
543     if ( rtot<0) { rtot=0; rcur=0;} /* if only one useful, use it */
544     if ( ltot<0) { ltot=0; lcur=0;}
545     if ( rtot+ltot < 1 ) { /* empty rset */
546         *current=0;
547         *total=0;
548         return;
549     }
550     r=1.0*(lcur+rcur)/(ltot+rtot); /* weighed average of l and r */
551     *current=(double) (p->hits);
552     *total=*current/r ; 
553 #if RSET_DEBUG
554     yaz_log(LOG_DEBUG,"bool_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
555                     info->rset_l->control->desc, info->rset_r->control->desc,
556                     *current, *total, r);
557 #endif
558 }