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