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