Removed the rewind function from rsets, it was unused
[idzebra-moved-to-github.git] / rset / rsbetween.c
1 /* $Id: rsbetween.c,v 1.26 2004-09-30 09:53:04 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 <rset.h>
40
41 #define RSBETWEEN_DEBUG 0 
42
43 static RSFD r_open_between (RSET ct, int flag);
44 static void r_close_between (RSFD rfd);
45 static void r_delete_between (RSET ct);
46 static int r_forward_between(RSFD rfd, void *buf, const void *untilbuf);
47 static int r_read_between (RSFD rfd, void *buf);
48 static int r_write_between (RSFD rfd, const void *buf);
49 static void r_pos_between (RSFD rfd, double *current, double *total);
50
51 static const struct rset_control control = 
52 {
53     "between",
54     r_delete_between,
55     r_open_between,
56     r_close_between,
57     r_forward_between, 
58     r_pos_between,
59     r_read_between,
60     r_write_between,
61 };
62
63
64 const struct rset_control *rset_kind_between = &control;
65
66 struct rset_between_info {
67     RSET rset_l;  /* left arg, start tag */
68     RSET rset_m;  /* the thing itself */
69     RSET rset_r;  /* right arg, end tag */
70     RSET rset_attr; /* attributes , optional */
71 };
72
73 struct rset_between_rfd {
74     RSFD rfd_l;
75     RSFD rfd_m;
76     RSFD rfd_r;
77     RSFD rfd_attr;
78     int  more_l;
79     int  more_m;
80     int  more_r;
81     int  more_attr;
82     void *buf_l;
83     void *buf_m;
84     void *buf_r;
85     void *buf_attr;
86     int level;  /* counting start/end tags */
87     zint hits;
88 };    
89
90 #if RSBETWEEN_DEBUG
91 static void log2 (struct rset_between_rfd *p, char *msg, int cmp_l, int cmp_r)
92 {
93     char buf_l[32];
94     char buf_m[32];
95     char buf_r[32];
96     logf(LOG_DEBUG,"btw: %s l=%s(%d/%d) m=%s(%d) r=%s(%d/%d), lev=%d",
97       msg, 
98       (*p->info->printer)(p->buf_l, buf_l), p->more_l, cmp_l,
99       (*p->info->printer)(p->buf_m, buf_m), p->more_m,
100       (*p->info->printer)(p->buf_r, buf_r), p->more_r, cmp_r,
101       p->level);
102 }
103 #endif
104
105 RSET rsbetween_create( NMEM nmem, const struct key_control *kcontrol,
106             int scope,
107             RSET rset_l, RSET rset_m, RSET rset_r, RSET rset_attr)
108 {
109     RSET rnew=rset_create_base(&control, nmem, kcontrol, scope);
110     struct rset_between_info *info=
111         (struct rset_between_info *) nmem_malloc(rnew->nmem,sizeof(*info));
112     info->rset_l = rset_l;
113     info->rset_m = rset_m;
114     info->rset_r = rset_r;
115     info->rset_attr = rset_attr;
116     rnew->priv=info;
117     return rnew;
118 }
119
120
121 static void r_delete_between (RSET ct)
122 {
123     struct rset_between_info *info = (struct rset_between_info *) ct->priv;
124
125     rset_delete (info->rset_l);
126     rset_delete (info->rset_m);
127     rset_delete (info->rset_r);
128     if (info->rset_attr)
129         rset_delete (info->rset_attr);
130 }
131
132
133 static RSFD r_open_between (RSET ct, int flag)
134 {
135     struct rset_between_info *info = (struct rset_between_info *) ct->priv;
136     RSFD rfd;
137     struct rset_between_rfd *p;
138
139     if (flag & RSETF_WRITE)
140     {
141         logf (LOG_FATAL, "between set type is read-only");
142         return NULL;
143     }
144     rfd=rfd_create_base(ct);
145     if (rfd->priv)  
146         p=(struct rset_between_rfd *)rfd->priv;
147     else {
148         p = (struct rset_between_rfd *) nmem_malloc(ct->nmem, (sizeof(*p)));
149         rfd->priv=p;
150         p->buf_l = nmem_malloc(ct->nmem, (ct->keycontrol->key_size));
151         p->buf_m = nmem_malloc(ct->nmem, (ct->keycontrol->key_size));
152         p->buf_r = nmem_malloc(ct->nmem, (ct->keycontrol->key_size));
153         p->buf_attr = nmem_malloc(ct->nmem, (ct->keycontrol->key_size));
154     }
155
156     p->rfd_l = rset_open (info->rset_l, RSETF_READ);
157     p->rfd_m = rset_open (info->rset_m, RSETF_READ);
158     p->rfd_r = rset_open (info->rset_r, RSETF_READ);
159     
160     p->more_l = rset_read (p->rfd_l, p->buf_l);
161     p->more_m = rset_read (p->rfd_m, p->buf_m);
162     p->more_r = rset_read (p->rfd_r, p->buf_r);
163     if (info->rset_attr)
164     {
165         p->rfd_attr = rset_open (info->rset_attr, RSETF_READ);
166         p->more_attr = rset_read (p->rfd_attr, p->buf_attr);
167     }
168     p->level=0;
169     p->hits=0;
170     return rfd;
171 }
172
173 static void r_close_between (RSFD rfd)
174 {
175     struct rset_between_info *info =(struct rset_between_info *)rfd->rset->priv;
176     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
177
178     rset_close (p->rfd_l);
179     rset_close (p->rfd_m);
180     rset_close (p->rfd_r);
181     if (info->rset_attr)
182         rset_close (p->rfd_attr);
183     rfd_delete_base(rfd);
184 }
185
186
187
188 static int r_forward_between(RSFD rfd, void *buf, const void *untilbuf)
189 {
190     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
191     int rc;
192 #if RSBETWEEN_DEBUG
193     log2( p, "fwd: before forward", 0,0);
194 #endif
195     /* It is enough to forward the m pointer here, the read will */
196     /* naturally forward the l, m, and attr pointers */
197     if (p->more_m)
198         p->more_m=rset_forward(p->rfd_m, p->buf_m,untilbuf);
199 #if RSBETWEEN_DEBUG
200     log2( p, "fwd: after forward M", 0,0);
201 #endif
202     rc = r_read_between(rfd, buf);
203 #if RSBETWEEN_DEBUG
204     log2( p, "fwd: after forward", 0,0);
205 #endif
206     return rc;
207 }
208
209
210
211
212 static int r_read_between (RSFD rfd, void *buf)
213 {
214     struct rset_between_info *info =(struct rset_between_info *)rfd->rset->priv;
215     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
216     const struct key_control *kctrl=rfd->rset->keycontrol;
217     int cmp_l=0;
218     int cmp_r=0;
219     int attr_match = 0;
220
221     while (p->more_m)
222     {
223 #if RSBETWEEN_DEBUG
224         log2( p, "start of loop", cmp_l, cmp_r);
225 #endif
226
227         /* forward L until past m, count levels, note rec boundaries */
228         if (p->more_l)
229             cmp_l= (*kctrl->cmp)(p->buf_l, p->buf_m);
230         else
231         {
232             p->level = 0;
233             cmp_l=rfd->rset->scope; /* past this record */
234         }
235 #if RSBETWEEN_DEBUG
236         log2( p, "after first L", cmp_l, cmp_r);
237 #endif
238
239         while (cmp_l < 0)   /* l before m */
240         {
241             if (cmp_l <= - rfd->rset->scope)  /* ==-2 */
242                 p->level=0; /* earlier record */
243             if (cmp_l > - rfd->rset->scope)  /* == -1 */
244             {
245                 p->level++; /* relevant start tag */
246
247                 if (!info->rset_attr)
248                     attr_match = 1;
249                 else
250                 {
251                     int cmp_attr;
252                     attr_match = 0;
253                     while (p->more_attr)
254                     {
255                         cmp_attr = (*kctrl->cmp)(p->buf_attr, p->buf_l);
256                         if (cmp_attr == 0)
257                         {
258                             attr_match = 1;
259                             break;
260                         }
261                         else if (cmp_attr > 0)
262                             break;
263                         else if (cmp_attr > - rfd->rset->scope)  /* == -1 */
264                             p->more_attr = rset_read (p->rfd_attr, p->buf_attr);
265                             /* if we had a forward that went all the way to
266                              * the seqno, we could use that. But fwd only goes
267                              * to the sysno */
268                         else if (cmp_attr <= - rfd->rset->scope) /* ==-2 */
269                         {
270                             p->more_attr = rset_forward( p->rfd_attr,
271                                              p->buf_attr, p->buf_l);
272 #if RSBETWEEN_DEBUG
273                             logf(LOG_DEBUG, "btw: after frowarding attr m=%d",
274                                       p->more_attr);
275 #endif
276                         }
277                     } /* while more_attr */
278                 }
279             }
280 #define NEWCODE 1 
281 #if NEWCODE                
282             if (cmp_l <= - rfd->rset->scope )/* ==-2 */
283             {
284                 if (p->more_l) 
285                 {
286                     p->more_l=rset_forward(p->rfd_l, p->buf_l, p->buf_m);
287                     if (p->more_l)
288                         cmp_l= (*kctrl->cmp)(p->buf_l, p->buf_m);
289                     else
290                         cmp_l=rfd->rset->scope;  /*2*/
291 #if RSBETWEEN_DEBUG
292                     log2( p, "after forwarding L", cmp_l, cmp_r);
293 #endif
294                 }
295             } else
296             {
297                 p->more_l = rset_read (p->rfd_l, p->buf_l);
298             }
299 #else
300             p->more_l = rset_read (p->rfd_l, p->buf_l);
301 #endif
302             if (p->more_l)
303             {
304                 cmp_l= (*kctrl->cmp)(p->buf_l, p->buf_m);
305             }
306             else
307                 cmp_l=rfd->rset->scope;   /*2*/
308 #if RSBETWEEN_DEBUG
309             log2( p, "end of L loop", cmp_l, cmp_r);
310 #endif
311         } /* forward L */
312
313             
314         /* forward R until past m, count levels */
315 #if RSBETWEEN_DEBUG
316         log2( p, "Before moving R", cmp_l, cmp_r);
317 #endif
318         if (p->more_r)
319             cmp_r= (*kctrl->cmp)(p->buf_r, p->buf_m);
320         else
321             cmp_r=rfd->rset->scope;  /*2*/
322 #if RSBETWEEN_DEBUG
323         log2( p, "after first R", cmp_l, cmp_r);
324 #endif
325         while (cmp_r < 0)   /* r before m */
326         {
327              /* -2, earlier record, don't count level */
328             if (cmp_r > -rfd->rset->scope) /* == -1 */
329                 p->level--; /* relevant end tag */
330             if (p->more_r)
331             {
332 #if NEWCODE                
333                 if (cmp_r <= - rfd->rset->scope) /* == -2 */
334                 {
335                     p->more_r=rset_forward(p->rfd_r, p->buf_r, p->buf_m);
336                 } else
337                 {
338                     p->more_r = rset_read (p->rfd_r, p->buf_r);
339                 }
340                 if (p->more_r)
341                     cmp_r= (*kctrl->cmp)(p->buf_r, p->buf_m);
342
343 #else
344                 p->more_r = rset_read (p->rfd_r, p->buf_r);
345                 cmp_r= (*kctrl->cmp)(p->buf_r, p->buf_m);
346 #endif
347             }
348             else
349                 cmp_r=rfd->rset->scope;  /*2*/
350 #if RSBETWEEN_DEBUG
351         log2( p, "End of R loop", cmp_l, cmp_r);
352 #endif
353         } /* forward R */
354         
355         if ( ( p->level <= 0 ) && ! p->more_l)
356             return 0; /* no more start tags, nothing more to find */
357         
358         if ( attr_match && p->level > 0)  /* within a tag pair (or deeper) */
359         {
360             memcpy (buf, p->buf_m, kctrl->key_size);
361 #if RSBETWEEN_DEBUG
362             log2( p, "Returning a hit (and forwarding m)", cmp_l, cmp_r);
363 #endif
364             p->more_m = rset_read (p->rfd_m, p->buf_m);
365             if (cmp_l >= rfd->rset->scope)  /* == 2 */
366                 p->level = 0;
367             p->hits++;
368             return 1;
369         }
370         else if ( ! p->more_l )  /* not in data, no more starts */
371         {
372 #if RSBETWEEN_DEBUG
373             log2( p, "no more starts, exiting without a hit", cmp_l, cmp_r);
374 #endif
375             return 0;  /* ergo, nothing can be found. stop scanning */
376         }
377 #if NEWCODE                
378         if (cmp_l >= rfd->rset->scope) /* == 2 */
379         {
380             p->level = 0;
381             p->more_m=rset_forward(p->rfd_m, p->buf_m,  p->buf_l);
382         } else
383         {
384             p->more_m = rset_read (p->rfd_m, p->buf_m);
385         }
386 #else
387         if (cmp_l >= rfd->rset->scope )  /* == 2 */
388             p->level = 0;
389         p->more_m = rset_read (p->rfd_m, p->buf_m);
390 #endif
391 #if RSBETWEEN_DEBUG
392         log2( p, "End of M loop", cmp_l, cmp_r);
393 #endif
394     } /* while more_m */
395     
396 #if RSBETWEEN_DEBUG
397     log2( p, "Exiting, nothing more in m", cmp_l, cmp_r);
398 #endif
399     return 0;  /* no more data possible */
400
401
402 }  /* r_read */
403
404
405 static int r_write_between (RSFD rfd, const void *buf)
406 {
407     logf (LOG_FATAL, "between set type is read-only");
408     return -1;
409 }
410
411
412 static void r_pos_between (RSFD rfd, double *current, double *total)
413 {
414     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
415     double lcur,ltot;
416     double mcur,mtot;
417     double rcur,rtot;
418     double r;
419     ltot=-1; rtot=-1;
420     rset_pos(p->rfd_l,  &lcur, &ltot);
421     rset_pos(p->rfd_m,  &mcur, &mtot);
422     rset_pos(p->rfd_r,  &rcur, &rtot);
423     if ( (ltot<0) && (mtot<0) && (rtot<0) ) { /*no position */
424         *current=mcur;  /* return same as you got */
425         *total=mtot;    /* probably -1 for not available */
426     }
427     if ( ltot<0) { ltot=0; lcur=0;} /* if only one useful, use it */
428     if ( mtot<0) { mtot=0; mcur=0;}
429     if ( rtot<0) { rtot=0; rcur=0;}
430     if ( ltot+mtot+rtot < 1 ) { /* empty rset */
431         *current=0;
432         *total=0;
433         return;
434     }
435     r=1.0*(lcur+mcur+rcur)/(ltot+mtot+rtot); /* weighed average of l and r */
436     *current=p->hits;
437     *total=*current/r ; 
438 #if RSBETWEEN_DEBUG
439     yaz_log(LOG_DEBUG,"betw_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
440                     info->rset_l->control->desc, info->rset_r->control->desc,
441                     *current, *total, r);
442 #endif
443 }