Removed the term count stuff from all rsets, and fixed what ever that broke.
[idzebra-moved-to-github.git] / rset / rsbetween.c
1 /* $Id: rsbetween.c,v 1.18 2004-08-20 14:44: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 /* 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, 
49                      int (*cmpfunc)(const void *p1, const void *p2),
50                      const void *untilbuf);
51 static int r_read_between (RSFD rfd, void *buf);
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 (*cmp)(const void *p1, const void *p2);
79     char *(*printer)(const void *p1, char *buf);
80     struct rset_between_rfd *rfd_list;
81 };
82
83 struct rset_between_rfd {
84     RSFD rfd_l;
85     RSFD rfd_m;
86     RSFD rfd_r;
87     RSFD rfd_attr;
88     int  more_l;
89     int  more_m;
90     int  more_r;
91     int  more_attr;
92     void *buf_l;
93     void *buf_m;
94     void *buf_r;
95     void *buf_attr;
96     int level;
97     struct rset_between_rfd *next;
98     struct rset_between_info *info;
99     zint hits;
100 };    
101
102 #if RSBETWEEN_DEBUG
103 static void log2 (struct rset_between_rfd *p, char *msg, int cmp_l, int cmp_r)
104 {
105     char buf_l[32];
106     char buf_m[32];
107     char buf_r[32];
108     logf(LOG_DEBUG,"btw: %s l=%s(%d/%d) m=%s(%d) r=%s(%d/%d), lev=%d",
109       msg, 
110       (*p->info->printer)(p->buf_l, buf_l), p->more_l, cmp_l,
111       (*p->info->printer)(p->buf_m, buf_m), p->more_m,
112       (*p->info->printer)(p->buf_r, buf_r), p->more_r, cmp_r,
113       p->level);
114 }
115 #endif
116
117 static void *r_create_between (RSET ct, const struct rset_control *sel,
118                                void *parms)
119 {
120     rset_between_parms *between_parms = (rset_between_parms *) parms;
121     struct rset_between_info *info;
122
123     info = (struct rset_between_info *) xmalloc (sizeof(*info));
124     info->key_size = between_parms->key_size;
125     info->rset_l = between_parms->rset_l;
126     info->rset_m = between_parms->rset_m;
127     info->rset_r = between_parms->rset_r;
128     info->rset_attr = between_parms->rset_attr;
129     if (rset_is_volatile(info->rset_l) || 
130         rset_is_volatile(info->rset_m) ||
131         rset_is_volatile(info->rset_r))
132         ct->flags |= RSET_FLAG_VOLATILE;
133     info->cmp = between_parms->cmp;
134     info->printer = between_parms->printer;
135     info->rfd_list = NULL;
136     return info;
137 }
138
139 static RSFD r_open_between (RSET ct, int flag)
140 {
141     struct rset_between_info *info = (struct rset_between_info *) ct->buf;
142     struct rset_between_rfd *rfd;
143
144     if (flag & RSETF_WRITE)
145     {
146         logf (LOG_FATAL, "between set type is read-only");
147         return NULL;
148     }
149     rfd = (struct rset_between_rfd *) xmalloc (sizeof(*rfd));
150     rfd->next = info->rfd_list;
151     info->rfd_list = rfd;
152     rfd->info = info;
153
154     rfd->buf_l = xmalloc (info->key_size);
155     rfd->buf_m = xmalloc (info->key_size);
156     rfd->buf_r = xmalloc (info->key_size);
157     rfd->buf_attr = xmalloc (info->key_size);
158
159     rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
160     rfd->rfd_m = rset_open (info->rset_m, RSETF_READ);
161     rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
162     
163     rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l);
164     rfd->more_m = rset_read (info->rset_m, rfd->rfd_m, rfd->buf_m);
165     rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r);
166     if (info->rset_attr)
167     {
168         rfd->rfd_attr = rset_open (info->rset_attr, RSETF_READ);
169         rfd->more_attr = rset_read (info->rset_attr, rfd->rfd_attr,
170                                     rfd->buf_attr);
171     }
172     rfd->level=0;
173     rfd->hits=0;
174     return rfd;
175 }
176
177 static void r_close_between (RSFD rfd)
178 {
179     struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
180     struct rset_between_rfd **rfdp;
181     
182     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
183         if (*rfdp == rfd)
184         {
185             xfree ((*rfdp)->buf_l);
186             xfree ((*rfdp)->buf_m);
187             xfree ((*rfdp)->buf_r);
188             xfree ((*rfdp)->buf_attr);
189             rset_close (info->rset_l, (*rfdp)->rfd_l);
190             rset_close (info->rset_m, (*rfdp)->rfd_m);
191             rset_close (info->rset_r, (*rfdp)->rfd_r);
192             if (info->rset_attr)
193                 rset_close (info->rset_attr, (*rfdp)->rfd_attr);
194             
195             *rfdp = (*rfdp)->next;
196             xfree (rfd);
197             return;
198         }
199     logf (LOG_FATAL, "r_close_between but no rfd match!");
200     assert (0);
201 }
202
203 static void r_delete_between (RSET ct)
204 {
205     struct rset_between_info *info = (struct rset_between_info *) ct->buf;
206
207     assert (info->rfd_list == NULL);
208     rset_delete (info->rset_l);
209     rset_delete (info->rset_m);
210     rset_delete (info->rset_r);
211     if (info->rset_attr)
212         rset_delete (info->rset_attr);
213     xfree (info);
214 }
215
216 static void r_rewind_between (RSFD rfd)
217 {
218     struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
219     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
220
221 #if RSBETWEEN_DEBUG
222     logf (LOG_DEBUG, "rsbetween_rewind");
223 #endif
224     rset_rewind (info->rset_l, p->rfd_l);
225     rset_rewind (info->rset_m, p->rfd_m);
226     rset_rewind (info->rset_r, p->rfd_r);
227     p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
228     p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m);
229     p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
230     if (info->rset_attr)
231     {
232         rset_rewind (info->rset_attr, p->rfd_attr);
233         p->more_attr = rset_read (info->rset_attr, p->rfd_attr, p->buf_attr);
234     }
235     p->level=0;
236     p->hits=0;
237 }
238
239
240
241 static int r_forward_between(RSET ct, RSFD rfd, void *buf,
242                      int (*cmpfunc)(const void *p1, const void *p2),
243                      const void *untilbuf)
244 {
245     struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
246     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
247     int rc;
248 #if RSBETWEEN_DEBUG
249     log2( p, "fwd: before forward", 0,0);
250 #endif
251     /* It is enough to forward the m pointer here, the read will */
252     /* naturally forward the l, m, and attr pointers */
253     if (p->more_m)
254         p->more_m=rset_forward(info->rset_m,p->rfd_m, p->buf_m,
255                         info->cmp,untilbuf);
256 #if RSBETWEEN_DEBUG
257     log2( p, "fwd: after forward M", 0,0);
258 #endif
259     rc = r_read_between(rfd, buf);
260 #if RSBETWEEN_DEBUG
261     log2( p, "fwd: after forward", 0,0);
262 #endif
263     return rc;
264 }
265
266
267
268
269 static int r_read_between (RSFD rfd, void *buf)
270 {
271     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
272     struct rset_between_info *info = p->info;
273     int cmp_l=0;
274     int cmp_r=0;
275     int attr_match = 0;
276
277     while (p->more_m)
278     {
279 #if RSBETWEEN_DEBUG
280         log2( p, "start of loop", cmp_l, cmp_r);
281 #endif
282
283         /* forward L until past m, count levels, note rec boundaries */
284         if (p->more_l)
285             cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
286         else
287         {
288             p->level = 0;
289             cmp_l=2; /* past this record */
290         }
291 #if RSBETWEEN_DEBUG
292         log2( p, "after first L", cmp_l, cmp_r);
293 #endif
294
295         while (cmp_l < 0)   /* l before m */
296         {
297             if (cmp_l == -2)
298                 p->level=0; /* earlier record */
299             if (cmp_l == -1)
300             {
301                 p->level++; /* relevant start tag */
302
303                 if (!info->rset_attr)
304                     attr_match = 1;
305                 else
306                 {
307                     int cmp_attr;
308                     attr_match = 0;
309                     while (p->more_attr)
310                     {
311                         cmp_attr = (*info->cmp)(p->buf_attr, p->buf_l);
312                         if (cmp_attr == 0)
313                         {
314                             attr_match = 1;
315                             break;
316                         }
317                         else if (cmp_attr > 0)
318                             break;
319                         else if (cmp_attr==-1) 
320                             p->more_attr = rset_read (info->rset_attr, 
321                                                   p->rfd_attr, p->buf_attr);
322                             /* if we had a forward that went all the way to
323                              * the seqno, we could use that. But fwd only goes
324                              * to the sysno */
325                         else if (cmp_attr==-2) 
326                         {
327                             p->more_attr = rset_forward(
328                                       info->rset_attr, p->rfd_attr,
329                                       p->buf_attr, info->cmp, p->buf_l);
330 #if RSBETWEEN_DEBUG
331                             logf(LOG_DEBUG, "btw: after frowarding attr m=%d",p->more_attr);
332 #endif
333                         }
334                     } /* while more_attr */
335                 }
336             }
337 #define NEWCODE 1 
338 #if NEWCODE                
339             if (cmp_l==-2)
340             {
341                 if (p->more_l) 
342                 {
343                     p->more_l=rset_forward( info->rset_l, p->rfd_l,
344                                       p->buf_l, info->cmp, p->buf_m);
345                     if (p->more_l)
346                         cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
347                     else
348                         cmp_l=2;
349 #if RSBETWEEN_DEBUG
350                     log2( p, "after forwarding L", cmp_l, cmp_r);
351 #endif
352                 }
353             } else
354             {
355                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
356             }
357 #else
358             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
359 #endif
360             if (p->more_l)
361             {
362                 cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
363             }
364             else
365                 cmp_l=2; 
366 #if RSBETWEEN_DEBUG
367             log2( p, "end of L loop", cmp_l, cmp_r);
368 #endif
369         } /* forward L */
370
371             
372         /* forward R until past m, count levels */
373 #if RSBETWEEN_DEBUG
374         log2( p, "Before moving R", cmp_l, cmp_r);
375 #endif
376         if (p->more_r)
377             cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
378         else
379             cmp_r=2; 
380 #if RSBETWEEN_DEBUG
381         log2( p, "after first R", cmp_l, cmp_r);
382 #endif
383         while (cmp_r < 0)   /* r before m */
384         {
385              /* -2, earlier record, don't count level */
386             if (cmp_r == -1)
387                 p->level--; /* relevant end tag */
388             if (p->more_r)
389             {
390 #if NEWCODE                
391                 if (cmp_r==-2)
392                 {
393                     p->more_r=rset_forward( info->rset_r, p->rfd_r,
394                                       p->buf_r, info->cmp, p->buf_m);
395                 } else
396                 {
397                     p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
398                 }
399                 if (p->more_r)
400                     cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
401
402 #else
403                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
404                 cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
405 #endif
406             }
407             else
408                 cmp_r=2; 
409 #if RSBETWEEN_DEBUG
410         log2( p, "End of R loop", cmp_l, cmp_r);
411 #endif
412         } /* forward R */
413         
414         if ( ( p->level <= 0 ) && ! p->more_l)
415             return 0; /* no more start tags, nothing more to find */
416         
417         if ( attr_match && p->level > 0)  /* within a tag pair (or deeper) */
418         {
419             memcpy (buf, p->buf_m, info->key_size);
420 #if RSBETWEEN_DEBUG
421             log2( p, "Returning a hit (and forwarding m)", cmp_l, cmp_r);
422 #endif
423             p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m);
424             if (cmp_l == 2)
425                 p->level = 0;
426             p->hits++;
427             return 1;
428         }
429         else if ( ! p->more_l )  /* not in data, no more starts */
430         {
431 #if RSBETWEEN_DEBUG
432             log2( p, "no more starts, exiting without a hit", cmp_l, cmp_r);
433 #endif
434             return 0;  /* ergo, nothing can be found. stop scanning */
435         }
436 #if NEWCODE                
437         if (cmp_l == 2)
438         {
439             p->level = 0;
440             p->more_m=rset_forward( info->rset_m, p->rfd_m,
441                               p->buf_m, info->cmp, p->buf_l);
442         } else
443         {
444             p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m);
445         }
446 #else
447         if (cmp_l == 2)
448             p->level = 0;
449         p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m);
450 #endif
451 #if RSBETWEEN_DEBUG
452         log2( p, "End of M loop", cmp_l, cmp_r);
453 #endif
454     } /* while more_m */
455     
456 #if RSBETWEEN_DEBUG
457     log2( p, "Exiting, nothing more in m", cmp_l, cmp_r);
458 #endif
459     return 0;  /* no more data possible */
460
461
462 }  /* r_read */
463
464
465 static int r_write_between (RSFD rfd, const void *buf)
466 {
467     logf (LOG_FATAL, "between set type is read-only");
468     return -1;
469 }
470
471
472 static void r_pos_between (RSFD rfd, double *current, double *total)
473 {
474     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
475     struct rset_between_info *info = p->info;
476     double lcur,ltot;
477     double mcur,mtot;
478     double rcur,rtot;
479     double r;
480     ltot=-1; rtot=-1;
481     rset_pos(info->rset_l, p->rfd_l,  &lcur, &ltot);
482     rset_pos(info->rset_m, p->rfd_m,  &mcur, &mtot);
483     rset_pos(info->rset_r, p->rfd_r,  &rcur, &rtot);
484     if ( (ltot<0) && (mtot<0) && (rtot<0) ) { /*no position */
485         *current=mcur;  /* return same as you got */
486         *total=mtot;    /* probably -1 for not available */
487     }
488     if ( ltot<0) { ltot=0; lcur=0;} /* if only one useful, use it */
489     if ( mtot<0) { mtot=0; mcur=0;}
490     if ( rtot<0) { rtot=0; rcur=0;}
491     if ( ltot+mtot+rtot < 1 ) { /* empty rset */
492         *current=0;
493         *total=0;
494         return;
495     }
496     r=1.0*(lcur+mcur+rcur)/(ltot+mtot+rtot); /* weighed average of l and r */
497     *current=p->hits;
498     *total=*current/r ; 
499 #if RSBETWEEN_DEBUG
500     yaz_log(LOG_DEBUG,"betw_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
501                     info->rset_l->control->desc, info->rset_r->control->desc,
502                     *current, *total, r);
503 #endif
504 }