pos estimates for rsbetween and rsprox
[idzebra-moved-to-github.git] / rset / rsbetween.c
1 /* $Id: rsbetween.c,v 1.17 2004-08-06 14:09:02 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 /* rsbetween is (mostly) used for xml searches. It returns the hits of the
25  * "middle" rset, that are in between the "left" and "right" rsets. For
26  * example "Shakespeare" in between "<title>" and </title>. The thing is 
27  * complicated by the inclusion of attributes (from their own rset). If attrs
28  * specified, they must match the "left" rset (start tag). "Hamlet" between
29  * "<title lang=eng>" and "</title>". (This assumes that the attributes are
30  * indexed to the same seqno as the tags).
31 */ 
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <assert.h>
37
38 #include <zebrautl.h>
39 #include <rsbetween.h>
40
41 #define RSBETWEEN_DEBUG 0 
42
43 static void *r_create_between(RSET ct, const struct rset_control *sel, void *parms);
44 static RSFD r_open_between (RSET ct, int flag);
45 static void r_close_between (RSFD rfd);
46 static void r_delete_between (RSET ct);
47 static void r_rewind_between (RSFD rfd);
48 static int r_forward_between(RSET ct, RSFD rfd, void *buf, int *term_index,
49                      int (*cmpfunc)(const void *p1, const void *p2),
50                      const void *untilbuf);
51 static int r_read_between (RSFD rfd, void *buf, int *term_index);
52 static int r_write_between (RSFD rfd, const void *buf);
53 static void r_pos_between (RSFD rfd, double *current, double *total);
54
55 static const struct rset_control control_between = 
56 {
57     "between",
58     r_create_between,
59     r_open_between,
60     r_close_between,
61     r_delete_between,
62     r_rewind_between,
63     r_forward_between, /* rset_default_forward, */
64     r_pos_between,
65     r_read_between,
66     r_write_between,
67 };
68
69
70 const struct rset_control *rset_kind_between = &control_between;
71
72 struct rset_between_info {
73     int key_size;
74     RSET rset_l;
75     RSET rset_m;
76     RSET rset_r;
77     RSET rset_attr;
78     int term_index_s;
79     int (*cmp)(const void *p1, const void *p2);
80     char *(*printer)(const void *p1, char *buf);
81     struct rset_between_rfd *rfd_list;
82 };
83
84 struct rset_between_rfd {
85     RSFD rfd_l;
86     RSFD rfd_m;
87     RSFD rfd_r;
88     RSFD rfd_attr;
89     int  more_l;
90     int  more_m;
91     int  more_r;
92     int  more_attr;
93     int term_index_l;
94     int term_index_m;
95     int term_index_r;
96     void *buf_l;
97     void *buf_m;
98     void *buf_r;
99     void *buf_attr;
100     int level;
101     struct rset_between_rfd *next;
102     struct rset_between_info *info;
103     zint hits;
104 };    
105
106 #if RSBETWEEN_DEBUG
107 static void log2 (struct rset_between_rfd *p, char *msg, int cmp_l, int cmp_r)
108 {
109     char buf_l[32];
110     char buf_m[32];
111     char buf_r[32];
112     logf(LOG_DEBUG,"btw: %s l=%s(%d/%d) m=%s(%d) r=%s(%d/%d), lev=%d",
113       msg, 
114       (*p->info->printer)(p->buf_l, buf_l), p->more_l, cmp_l,
115       (*p->info->printer)(p->buf_m, buf_m), p->more_m,
116       (*p->info->printer)(p->buf_r, buf_r), p->more_r, cmp_r,
117       p->level);
118 }
119 #endif
120
121 static void *r_create_between (RSET ct, const struct rset_control *sel,
122                                void *parms)
123 {
124     rset_between_parms *between_parms = (rset_between_parms *) parms;
125     struct rset_between_info *info;
126
127     info = (struct rset_between_info *) xmalloc (sizeof(*info));
128     info->key_size = between_parms->key_size;
129     info->rset_l = between_parms->rset_l;
130     info->rset_m = between_parms->rset_m;
131     info->rset_r = between_parms->rset_r;
132     info->rset_attr = between_parms->rset_attr;
133     if (rset_is_volatile(info->rset_l) || 
134         rset_is_volatile(info->rset_m) ||
135         rset_is_volatile(info->rset_r))
136         ct->flags |= RSET_FLAG_VOLATILE;
137     info->cmp = between_parms->cmp;
138     info->printer = between_parms->printer;
139     info->rfd_list = NULL;
140     
141     info->term_index_s = info->rset_l->no_rset_terms;
142     if (info->rset_m)
143     {
144         ct->no_rset_terms =
145             info->rset_l->no_rset_terms + 
146             info->rset_m->no_rset_terms + 
147             info->rset_r->no_rset_terms;
148         ct->rset_terms = (RSET_TERM *)
149             xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
150         memcpy (ct->rset_terms, info->rset_l->rset_terms,
151                 info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
152         memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
153                 info->rset_m->rset_terms,
154                 info->rset_m->no_rset_terms * sizeof(*ct->rset_terms));
155         memcpy (ct->rset_terms + info->rset_l->no_rset_terms + 
156                 info->rset_m->no_rset_terms,
157                 info->rset_r->rset_terms,
158                 info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
159     }
160     else
161     {
162         ct->no_rset_terms =
163             info->rset_l->no_rset_terms + 
164             info->rset_r->no_rset_terms;
165         ct->rset_terms = (RSET_TERM *)
166             xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
167         memcpy (ct->rset_terms, info->rset_l->rset_terms,
168                 info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
169         memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
170                 info->rset_r->rset_terms,
171                 info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
172     }
173
174     return info;
175 }
176
177 static RSFD r_open_between (RSET ct, int flag)
178 {
179     struct rset_between_info *info = (struct rset_between_info *) ct->buf;
180     struct rset_between_rfd *rfd;
181
182     if (flag & RSETF_WRITE)
183     {
184         logf (LOG_FATAL, "between set type is read-only");
185         return NULL;
186     }
187     rfd = (struct rset_between_rfd *) xmalloc (sizeof(*rfd));
188     rfd->next = info->rfd_list;
189     info->rfd_list = rfd;
190     rfd->info = info;
191
192     rfd->buf_l = xmalloc (info->key_size);
193     rfd->buf_m = xmalloc (info->key_size);
194     rfd->buf_r = xmalloc (info->key_size);
195     rfd->buf_attr = xmalloc (info->key_size);
196
197     rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
198     rfd->rfd_m = rset_open (info->rset_m, RSETF_READ);
199     rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
200     
201     rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
202                              &rfd->term_index_l);
203     rfd->more_m = rset_read (info->rset_m, rfd->rfd_m, rfd->buf_m,
204                              &rfd->term_index_m);
205     rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
206                              &rfd->term_index_r);
207     if (info->rset_attr)
208     {
209         int dummy;
210         rfd->rfd_attr = rset_open (info->rset_attr, RSETF_READ);
211         rfd->more_attr = rset_read (info->rset_attr, rfd->rfd_attr,
212                                     rfd->buf_attr, &dummy);
213     }
214     rfd->level=0;
215     rfd->hits=0;
216     return rfd;
217 }
218
219 static void r_close_between (RSFD rfd)
220 {
221     struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
222     struct rset_between_rfd **rfdp;
223     
224     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
225         if (*rfdp == rfd)
226         {
227             xfree ((*rfdp)->buf_l);
228             xfree ((*rfdp)->buf_m);
229             xfree ((*rfdp)->buf_r);
230             xfree ((*rfdp)->buf_attr);
231             rset_close (info->rset_l, (*rfdp)->rfd_l);
232             rset_close (info->rset_m, (*rfdp)->rfd_m);
233             rset_close (info->rset_r, (*rfdp)->rfd_r);
234             if (info->rset_attr)
235                 rset_close (info->rset_attr, (*rfdp)->rfd_attr);
236             
237             *rfdp = (*rfdp)->next;
238             xfree (rfd);
239             return;
240         }
241     logf (LOG_FATAL, "r_close_between but no rfd match!");
242     assert (0);
243 }
244
245 static void r_delete_between (RSET ct)
246 {
247     struct rset_between_info *info = (struct rset_between_info *) ct->buf;
248
249     assert (info->rfd_list == NULL);
250     xfree (ct->rset_terms);
251     rset_delete (info->rset_l);
252     rset_delete (info->rset_m);
253     rset_delete (info->rset_r);
254     if (info->rset_attr)
255         rset_delete (info->rset_attr);
256     xfree (info);
257 }
258
259 static void r_rewind_between (RSFD rfd)
260 {
261     struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
262     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
263
264 #if RSBETWEEN_DEBUG
265     logf (LOG_DEBUG, "rsbetween_rewind");
266 #endif
267     rset_rewind (info->rset_l, p->rfd_l);
268     rset_rewind (info->rset_m, p->rfd_m);
269     rset_rewind (info->rset_r, p->rfd_r);
270     p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
271     p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m, &p->term_index_m);
272     p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
273     if (info->rset_attr)
274     {
275         int dummy;
276         rset_rewind (info->rset_attr, p->rfd_attr);
277         p->more_attr = rset_read (info->rset_attr, p->rfd_attr, p->buf_attr,
278                                   &dummy);
279     }
280     p->level=0;
281     p->hits=0;
282 }
283
284
285
286 static int r_forward_between(RSET ct, RSFD rfd, void *buf, int *term_index,
287                      int (*cmpfunc)(const void *p1, const void *p2),
288                      const void *untilbuf)
289 {
290     struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
291     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
292     int rc;
293 #if RSBETWEEN_DEBUG
294     log2( p, "fwd: before forward", 0,0);
295 #endif
296     /* It is enough to forward the m pointer here, the read will */
297     /* naturally forward the l, m, and attr pointers */
298     if (p->more_m)
299         p->more_m=rset_forward(info->rset_m,p->rfd_m, p->buf_m,
300                         &p->term_index_m, info->cmp,untilbuf);
301 #if RSBETWEEN_DEBUG
302     log2( p, "fwd: after forward M", 0,0);
303 #endif
304     rc = r_read_between(rfd, buf, term_index);
305 #if RSBETWEEN_DEBUG
306     log2( p, "fwd: after forward", 0,0);
307 #endif
308     return rc;
309 }
310
311
312
313
314 static int r_read_between (RSFD rfd, void *buf, int *term_index)
315 {
316     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
317     struct rset_between_info *info = p->info;
318     int cmp_l=0;
319     int cmp_r=0;
320     int attr_match = 0;
321
322     while (p->more_m)
323     {
324 #if RSBETWEEN_DEBUG
325         log2( p, "start of loop", cmp_l, cmp_r);
326 #endif
327
328         /* forward L until past m, count levels, note rec boundaries */
329         if (p->more_l)
330             cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
331         else
332         {
333             p->level = 0;
334             cmp_l=2; /* past this record */
335         }
336 #if RSBETWEEN_DEBUG
337         log2( p, "after first L", cmp_l, cmp_r);
338 #endif
339
340         while (cmp_l < 0)   /* l before m */
341         {
342             if (cmp_l == -2)
343                 p->level=0; /* earlier record */
344             if (cmp_l == -1)
345             {
346                 p->level++; /* relevant start tag */
347
348                 if (!info->rset_attr)
349                     attr_match = 1;
350                 else
351                 {
352                     int cmp_attr;
353                     int dummy_term;
354                     attr_match = 0;
355                     while (p->more_attr)
356                     {
357                         cmp_attr = (*info->cmp)(p->buf_attr, p->buf_l);
358                         if (cmp_attr == 0)
359                         {
360                             attr_match = 1;
361                             break;
362                         }
363                         else if (cmp_attr > 0)
364                             break;
365                         else if (cmp_attr==-1) 
366                             p->more_attr = rset_read (info->rset_attr, p->rfd_attr,
367                                                   p->buf_attr, &dummy_term);
368                             /* if we had a forward that went all the way to
369                              * the seqno, we could use that. But fwd only goes
370                              * to the sysno */
371                         else if (cmp_attr==-2) 
372                         {
373                             p->more_attr = rset_forward(
374                                       info->rset_attr, p->rfd_attr,
375                                       p->buf_attr, &dummy_term,
376                                       info->cmp, p->buf_l);
377 #if RSBETWEEN_DEBUG
378                             logf(LOG_DEBUG, "btw: after frowarding attr m=%d",p->more_attr);
379 #endif
380                         }
381                     } /* while more_attr */
382                 }
383             }
384 #define NEWCODE 1 
385 #if NEWCODE                
386             if (cmp_l==-2)
387             {
388                 if (p->more_l) 
389                 {
390                     p->more_l=rset_forward(
391                                       info->rset_l, p->rfd_l,
392                                       p->buf_l, &p->term_index_l,
393                                       info->cmp, p->buf_m);
394                     if (p->more_l)
395                         cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
396                     else
397                         cmp_l=2;
398 #if RSBETWEEN_DEBUG
399                     log2( p, "after forwarding L", cmp_l, cmp_r);
400 #endif
401                 }
402             } else
403             {
404                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
405                               &p->term_index_l);
406             }
407 #else
408             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
409                               &p->term_index_l);
410 #endif
411             if (p->more_l)
412             {
413                 cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
414             }
415             else
416                 cmp_l=2; 
417 #if RSBETWEEN_DEBUG
418             log2( p, "end of L loop", cmp_l, cmp_r);
419 #endif
420         } /* forward L */
421
422             
423         /* forward R until past m, count levels */
424 #if RSBETWEEN_DEBUG
425         log2( p, "Before moving R", cmp_l, cmp_r);
426 #endif
427         if (p->more_r)
428             cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
429         else
430             cmp_r=2; 
431 #if RSBETWEEN_DEBUG
432         log2( p, "after first R", cmp_l, cmp_r);
433 #endif
434         while (cmp_r < 0)   /* r before m */
435         {
436              /* -2, earlier record, don't count level */
437             if (cmp_r == -1)
438                 p->level--; /* relevant end tag */
439             if (p->more_r)
440             {
441 #if NEWCODE                
442                 if (cmp_r==-2)
443                 {
444                     p->more_r=rset_forward(
445                                       info->rset_r, p->rfd_r,
446                                       p->buf_r, &p->term_index_r,
447                                       info->cmp, p->buf_m);
448                 } else
449                 {
450                     p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
451                                        &p->term_index_r);
452                 }
453                 if (p->more_r)
454                     cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
455
456 #else
457                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
458                                        &p->term_index_r);
459                 cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
460 #endif
461             }
462             else
463                 cmp_r=2; 
464 #if RSBETWEEN_DEBUG
465         log2( p, "End of R loop", cmp_l, cmp_r);
466 #endif
467         } /* forward R */
468         
469         if ( ( p->level <= 0 ) && ! p->more_l)
470             return 0; /* no more start tags, nothing more to find */
471         
472         if ( attr_match && p->level > 0)  /* within a tag pair (or deeper) */
473         {
474             memcpy (buf, p->buf_m, info->key_size);
475             *term_index = p->term_index_m;
476 #if RSBETWEEN_DEBUG
477             log2( p, "Returning a hit (and forwarding m)", cmp_l, cmp_r);
478 #endif
479             p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m,
480                                    &p->term_index_m);
481             if (cmp_l == 2)
482                 p->level = 0;
483             p->hits++;
484             return 1;
485         }
486         else if ( ! p->more_l )  /* not in data, no more starts */
487         {
488 #if RSBETWEEN_DEBUG
489             log2( p, "no more starts, exiting without a hit", cmp_l, cmp_r);
490 #endif
491             return 0;  /* ergo, nothing can be found. stop scanning */
492         }
493 #if NEWCODE                
494         if (cmp_l == 2)
495         {
496             p->level = 0;
497             p->more_m=rset_forward(
498                               info->rset_m, p->rfd_m,
499                               p->buf_m, &p->term_index_m,
500                               info->cmp, p->buf_l);
501         } else
502         {
503             p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m,
504                                &p->term_index_m);
505         }
506 #else
507         if (cmp_l == 2)
508             p->level = 0;
509         p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m,
510                                &p->term_index_m);
511 #endif
512 #if RSBETWEEN_DEBUG
513         log2( p, "End of M loop", cmp_l, cmp_r);
514 #endif
515     } /* while more_m */
516     
517 #if RSBETWEEN_DEBUG
518     log2( p, "Exiting, nothing more in m", cmp_l, cmp_r);
519 #endif
520     return 0;  /* no more data possible */
521
522
523 }  /* r_read */
524
525
526 static int r_write_between (RSFD rfd, const void *buf)
527 {
528     logf (LOG_FATAL, "between set type is read-only");
529     return -1;
530 }
531
532
533 static void r_pos_between (RSFD rfd, double *current, double *total)
534 {
535     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
536     struct rset_between_info *info = p->info;
537     double lcur,ltot;
538     double mcur,mtot;
539     double rcur,rtot;
540     double r;
541     ltot=-1; rtot=-1;
542     rset_pos(info->rset_l, p->rfd_l,  &lcur, &ltot);
543     rset_pos(info->rset_m, p->rfd_m,  &mcur, &mtot);
544     rset_pos(info->rset_r, p->rfd_r,  &rcur, &rtot);
545     if ( (ltot<0) && (mtot<0) && (rtot<0) ) { /*no position */
546         *current=mcur;  /* return same as you got */
547         *total=mtot;    /* probably -1 for not available */
548     }
549     if ( ltot<0) { ltot=0; lcur=0;} /* if only one useful, use it */
550     if ( mtot<0) { mtot=0; mcur=0;}
551     if ( rtot<0) { rtot=0; rcur=0;}
552     if ( ltot+mtot+rtot < 1 ) { /* empty rset */
553         *current=0;
554         *total=0;
555         return;
556     }
557     r=1.0*(lcur+mcur+rcur)/(ltot+mtot+rtot); /* weighed average of l and r */
558     *current=p->hits;
559     *total=*current/r ; 
560 #if RSBETWEEN_DEBUG
561     yaz_log(LOG_DEBUG,"betw_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
562                     info->rset_l->control->desc, info->rset_r->control->desc,
563                     *current, *total, r);
564 #endif
565 }