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