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