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