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