Added a comment, cleaned whitespace
[idzebra-moved-to-github.git] / rset / rsbetween.c
1 /* $Id: rsbetween.c,v 1.11 2004-01-30 13:07:14 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_count_between (RSET ct);
47 static int r_read_between (RSFD rfd, void *buf, int *term_index);
48 static int r_write_between (RSFD rfd, const void *buf);
49
50 static const struct rset_control control_between = 
51 {
52     "between",
53     r_create_between,
54     r_open_between,
55     r_close_between,
56     r_delete_between,
57     r_rewind_between,
58     rset_default_forward,
59     r_count_between,
60     r_read_between,
61     r_write_between,
62 };
63
64
65 const struct rset_control *rset_kind_between = &control_between;
66
67 struct rset_between_info {
68     int key_size;
69     RSET rset_l;
70     RSET rset_m;
71     RSET rset_r;
72     RSET rset_attr;
73     int term_index_s;
74     int (*cmp)(const void *p1, const void *p2);
75     char *(*printer)(const void *p1, char *buf);
76     struct rset_between_rfd *rfd_list;
77 };
78
79 struct rset_between_rfd {
80     RSFD rfd_l;
81     RSFD rfd_m;
82     RSFD rfd_r;
83     RSFD rfd_attr;
84     int  more_l;
85     int  more_m;
86     int  more_r;
87     int  more_attr;
88     int term_index_l;
89     int term_index_m;
90     int term_index_r;
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 };    
99
100 static void *r_create_between (RSET ct, const struct rset_control *sel,
101                                void *parms)
102 {
103     rset_between_parms *between_parms = (rset_between_parms *) parms;
104     struct rset_between_info *info;
105
106     info = (struct rset_between_info *) xmalloc (sizeof(*info));
107     info->key_size = between_parms->key_size;
108     info->rset_l = between_parms->rset_l;
109     info->rset_m = between_parms->rset_m;
110     info->rset_r = between_parms->rset_r;
111     info->rset_attr = between_parms->rset_attr;
112     if (rset_is_volatile(info->rset_l) || 
113         rset_is_volatile(info->rset_m) ||
114         rset_is_volatile(info->rset_r))
115         ct->flags |= RSET_FLAG_VOLATILE;
116     info->cmp = between_parms->cmp;
117     info->printer = between_parms->printer;
118     info->rfd_list = NULL;
119     
120     info->term_index_s = info->rset_l->no_rset_terms;
121     if (info->rset_m)
122     {
123         ct->no_rset_terms =
124             info->rset_l->no_rset_terms + 
125             info->rset_m->no_rset_terms + 
126             info->rset_r->no_rset_terms;
127         ct->rset_terms = (RSET_TERM *)
128             xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
129         memcpy (ct->rset_terms, info->rset_l->rset_terms,
130                 info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
131         memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
132                 info->rset_m->rset_terms,
133                 info->rset_m->no_rset_terms * sizeof(*ct->rset_terms));
134         memcpy (ct->rset_terms + info->rset_l->no_rset_terms + 
135                 info->rset_m->no_rset_terms,
136                 info->rset_r->rset_terms,
137                 info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
138     }
139     else
140     {
141         ct->no_rset_terms =
142             info->rset_l->no_rset_terms + 
143             info->rset_r->no_rset_terms;
144         ct->rset_terms = (RSET_TERM *)
145             xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
146         memcpy (ct->rset_terms, info->rset_l->rset_terms,
147                 info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
148         memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
149                 info->rset_r->rset_terms,
150                 info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
151     }
152
153     return info;
154 }
155
156 static RSFD r_open_between (RSET ct, int flag)
157 {
158     struct rset_between_info *info = (struct rset_between_info *) ct->buf;
159     struct rset_between_rfd *rfd;
160
161     if (flag & RSETF_WRITE)
162     {
163         logf (LOG_FATAL, "between set type is read-only");
164         return NULL;
165     }
166     rfd = (struct rset_between_rfd *) xmalloc (sizeof(*rfd));
167     rfd->next = info->rfd_list;
168     info->rfd_list = rfd;
169     rfd->info = info;
170
171     rfd->buf_l = xmalloc (info->key_size);
172     rfd->buf_m = xmalloc (info->key_size);
173     rfd->buf_r = xmalloc (info->key_size);
174     rfd->buf_attr = xmalloc (info->key_size);
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->term_index_l);
182     rfd->more_m = rset_read (info->rset_m, rfd->rfd_m, rfd->buf_m,
183                              &rfd->term_index_m);
184     rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
185                              &rfd->term_index_r);
186     if (info->rset_attr)
187     {
188         int dummy;
189         rfd->rfd_attr = rset_open (info->rset_attr, RSETF_READ);
190         rfd->more_attr = rset_read (info->rset_attr, rfd->rfd_attr,
191                                     rfd->buf_attr, &dummy);
192     }
193     rfd->level=0;
194     return rfd;
195 }
196
197 static void r_close_between (RSFD rfd)
198 {
199     struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
200     struct rset_between_rfd **rfdp;
201     
202     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
203         if (*rfdp == rfd)
204         {
205             xfree ((*rfdp)->buf_l);
206             xfree ((*rfdp)->buf_m);
207             xfree ((*rfdp)->buf_r);
208             xfree ((*rfdp)->buf_attr);
209             rset_close (info->rset_l, (*rfdp)->rfd_l);
210             rset_close (info->rset_m, (*rfdp)->rfd_m);
211             rset_close (info->rset_r, (*rfdp)->rfd_r);
212             if (info->rset_attr)
213                 rset_close (info->rset_attr, (*rfdp)->rfd_attr);
214             
215             *rfdp = (*rfdp)->next;
216             xfree (rfd);
217             return;
218         }
219     logf (LOG_FATAL, "r_close_between but no rfd match!");
220     assert (0);
221 }
222
223 static void r_delete_between (RSET ct)
224 {
225     struct rset_between_info *info = (struct rset_between_info *) ct->buf;
226
227     assert (info->rfd_list == NULL);
228     xfree (ct->rset_terms);
229     rset_delete (info->rset_l);
230     rset_delete (info->rset_m);
231     rset_delete (info->rset_r);
232     if (info->rset_attr)
233         rset_delete (info->rset_attr);
234     xfree (info);
235 }
236
237 static void r_rewind_between (RSFD rfd)
238 {
239     struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
240     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
241
242     logf (LOG_DEBUG, "rsbetween_rewind");
243     rset_rewind (info->rset_l, p->rfd_l);
244     rset_rewind (info->rset_m, p->rfd_m);
245     rset_rewind (info->rset_r, p->rfd_r);
246     p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
247     p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m, &p->term_index_m);
248     p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
249     if (info->rset_attr)
250     {
251         int dummy;
252         rset_rewind (info->rset_attr, p->rfd_attr);
253         p->more_attr = rset_read (info->rset_attr, p->rfd_attr, p->buf_attr,
254                                   &dummy);
255     }
256     p->level=0;
257 }
258
259 static int r_count_between (RSET ct)
260 {
261     return 0;
262 }
263
264
265 static void log2 (struct rset_between_rfd *p, char *msg, int cmp_l, int cmp_r)
266 {
267     char buf_l[32];
268     char buf_m[32];
269     char buf_r[32];
270     logf(LOG_DEBUG,"btw: %s l=%s(%d/%d) m=%s(%d) r=%s(%d/%d), lev=%d",
271       msg, 
272       (*p->info->printer)(p->buf_l, buf_l), p->more_l, cmp_l,
273       (*p->info->printer)(p->buf_m, buf_m), p->more_m,
274       (*p->info->printer)(p->buf_r, buf_r), p->more_r, cmp_r,
275       p->level);
276 }
277
278 static int r_read_between (RSFD rfd, void *buf, int *term_index)
279 {
280     struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
281     struct rset_between_info *info = p->info;
282     int cmp_l=0;
283     int cmp_r=0;
284     int attr_match = 0;
285
286     while (p->more_m)
287     {
288         log2( p, "start of loop", cmp_l, cmp_r);
289
290         /* forward L until past m, count levels, note rec boundaries */
291         if (p->more_l)
292             cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
293         else
294         {
295             p->level = 0;
296             cmp_l=2; /* past this record */
297         }
298         log2( p, "after first L", cmp_l, cmp_r);
299
300         while (cmp_l < 0)   /* l before m */
301         {
302             if (cmp_l == -2)
303                 p->level=0; /* earlier record */
304             if (cmp_l == -1)
305             {
306                 p->level++; /* relevant start tag */
307
308                 if (!info->rset_attr)
309                     attr_match = 1;
310                 else
311                 {
312                     int cmp_attr;
313                     int dummy_term;
314                     attr_match = 0;
315                     while (p->more_attr)
316                     {
317                         cmp_attr = (*info->cmp)(p->buf_attr, p->buf_l);
318                         if (cmp_attr == 0)
319                         {
320                             attr_match = 1;
321                             break;
322                         }
323                         else if (cmp_attr > 0)
324                             break;
325                         p->more_attr = rset_read (info->rset_attr, p->rfd_attr,
326                                                   p->buf_attr, &dummy_term);
327                     }
328                 }
329             }
330             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
331                               &p->term_index_l);
332             if (p->more_l)
333             {
334                 cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
335             }
336             else
337                 cmp_l=2; 
338             log2( p, "end of L loop", cmp_l, cmp_r);
339         } /* forward L */
340
341             
342         /* forward R until past m, count levels */
343         log2( p, "Before moving R", cmp_l, cmp_r);
344         if (p->more_r)
345             cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
346         else
347             cmp_r=2; 
348         log2( p, "after first R", cmp_l, cmp_r);
349         while (cmp_r < 0)   /* r before m */
350         {
351              /* -2, earlier record, doesn't matter */
352             if (cmp_r == -1)
353                 p->level--; /* relevant end tag */
354             if (p->more_r)
355             {
356                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
357                                        &p->term_index_r);
358                 cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
359             }
360             else
361                 cmp_r=2; 
362         log2( p, "End of R loop", cmp_l, cmp_r);
363         } /* forward R */
364         
365         if ( ( p->level <= 0 ) && ! p->more_l)
366             return 0; /* no more start tags, nothing more to find */
367         
368         if ( attr_match && p->level > 0)  /* within a tag pair (or deeper) */
369         {
370             memcpy (buf, p->buf_m, info->key_size);
371             *term_index = p->term_index_m;
372             log2( p, "Returning a hit (and forwarding m)", cmp_l, cmp_r);
373             p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m,
374                                    &p->term_index_m);
375             if (cmp_l == 2)
376                 p->level = 0;
377             return 1;
378         }
379         else if ( ! p->more_l )  /* not in data, no more starts */
380         {
381             log2( p, "no more starts, exiting without a hit", cmp_l, cmp_r);
382             return 0;  /* ergo, nothing can be found. stop scanning */
383         }
384         if (cmp_l == 2)
385             p->level = 0;
386         p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m,
387                                &p->term_index_m);
388         log2( p, "End of M loop", cmp_l, cmp_r);
389     } /* while more_m */
390     
391     log2( p, "Exiting, nothing more in m", cmp_l, cmp_r);
392     return 0;  /* no more data possible */
393
394
395 }  /* r_read */
396
397
398 static int r_write_between (RSFD rfd, const void *buf)
399 {
400     logf (LOG_FATAL, "between set type is read-only");
401     return -1;
402 }
403