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