Declarations for the rset_forward function. No actual code yet
[idzebra-moved-to-github.git] / rset / rsbool.c
1 /* $Id: rsbool.c,v 1.23 2004-01-16 15:27:35 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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 <rsbool.h>
31 #include <zebrautl.h>
32
33 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
34 static RSFD r_open (RSET ct, int flag);
35 static void r_close (RSFD rfd);
36 static void r_delete (RSET ct);
37 static void r_rewind (RSFD rfd);
38 static int r_count (RSET ct);
39 static int r_read_and (RSFD rfd, void *buf, int *term_index);
40 static int r_read_or (RSFD rfd, void *buf, int *term_index);
41 static int r_read_not (RSFD rfd, void *buf, int *term_index);
42 static int r_write (RSFD rfd, const void *buf);
43
44 static const struct rset_control control_and = 
45 {
46     "and",
47     r_create,
48     r_open,
49     r_close,
50     r_delete,
51     r_rewind,
52     rset_default_forward,
53     r_count,
54     r_read_and,
55     r_write,
56 };
57
58 static const struct rset_control control_or = 
59 {
60     "or",
61     r_create,
62     r_open,
63     r_close,
64     r_delete,
65     r_rewind,
66     rset_default_forward,
67     r_count,
68     r_read_or,
69     r_write,
70 };
71
72 static const struct rset_control control_not = 
73 {
74     "not",
75     r_create,
76     r_open,
77     r_close,
78     r_delete,
79     r_rewind,
80     rset_default_forward,
81     r_count,
82     r_read_not,
83     r_write,
84 };
85
86
87 const struct rset_control *rset_kind_and = &control_and;
88 const struct rset_control *rset_kind_or = &control_or;
89 const struct rset_control *rset_kind_not = &control_not;
90
91 struct rset_bool_info {
92     int key_size;
93     RSET rset_l;
94     RSET rset_r;
95     int term_index_s;
96     int (*cmp)(const void *p1, const void *p2);
97     struct rset_bool_rfd *rfd_list;
98 };
99
100 struct rset_bool_rfd {
101     RSFD rfd_l;
102     RSFD rfd_r;
103     int  more_l;
104     int  more_r;
105     int term_index_l;
106     int term_index_r;
107     void *buf_l;
108     void *buf_r;
109     int tail;
110     struct rset_bool_rfd *next;
111     struct rset_bool_info *info;
112 };    
113
114 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
115 {
116     rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
117     struct rset_bool_info *info;
118
119     info = (struct rset_bool_info *) xmalloc (sizeof(*info));
120     info->key_size = bool_parms->key_size;
121     info->rset_l = bool_parms->rset_l;
122     info->rset_r = bool_parms->rset_r;
123     if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
124         ct->flags |= RSET_FLAG_VOLATILE;
125     info->cmp = bool_parms->cmp;
126     info->rfd_list = NULL;
127     
128     info->term_index_s = info->rset_l->no_rset_terms;
129     ct->no_rset_terms =
130         info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
131     ct->rset_terms = (RSET_TERM *)
132         xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
133
134     memcpy (ct->rset_terms, info->rset_l->rset_terms,
135             info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
136     memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
137             info->rset_r->rset_terms,
138             info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
139     return info;
140 }
141
142 static RSFD r_open (RSET ct, int flag)
143 {
144     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
145     struct rset_bool_rfd *rfd;
146
147     if (flag & RSETF_WRITE)
148     {
149         logf (LOG_FATAL, "bool set type is read-only");
150         return NULL;
151     }
152     rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
153     rfd->next = info->rfd_list;
154     info->rfd_list = rfd;
155     rfd->info = info;
156
157     rfd->buf_l = xmalloc (info->key_size);
158     rfd->buf_r = xmalloc (info->key_size);
159     rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
160     rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
161     rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
162                              &rfd->term_index_l);
163     rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
164                              &rfd->term_index_r);
165     rfd->tail = 0;
166     return rfd;
167 }
168
169 static void r_close (RSFD rfd)
170 {
171     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
172     struct rset_bool_rfd **rfdp;
173     
174     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
175         if (*rfdp == rfd)
176         {
177             xfree ((*rfdp)->buf_l);
178             xfree ((*rfdp)->buf_r);
179             rset_close (info->rset_l, (*rfdp)->rfd_l);
180             rset_close (info->rset_r, (*rfdp)->rfd_r);
181             *rfdp = (*rfdp)->next;
182             xfree (rfd);
183             return;
184         }
185     logf (LOG_FATAL, "r_close but no rfd match!");
186     assert (0);
187 }
188
189 static void r_delete (RSET ct)
190 {
191     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
192
193     assert (info->rfd_list == NULL);
194     xfree (ct->rset_terms);
195     rset_delete (info->rset_l);
196     rset_delete (info->rset_r);
197     xfree (info);
198 }
199
200 static void r_rewind (RSFD rfd)
201 {
202     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
203     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
204
205     logf (LOG_DEBUG, "rsbool_rewind");
206     rset_rewind (info->rset_l, p->rfd_l);
207     rset_rewind (info->rset_r, p->rfd_r);
208     p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
209     p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
210 }
211
212 static int r_count (RSET ct)
213 {
214     return 0;
215 }
216
217
218 /*
219     1,1         1,3
220     1,9         2,1
221     1,11        3,1
222     2,9
223
224   1,1     1,1
225   1,3     1,3
226           1,9
227           1,11
228   2,1     2,1
229           2,9
230           3,1
231 */
232
233 static int r_read_and (RSFD rfd, void *buf, int *term_index)
234 {
235     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
236     struct rset_bool_info *info = p->info;
237
238     while (p->more_l || p->more_r)
239     {
240         int cmp;
241
242         if (p->more_l && p->more_r)
243             cmp = (*info->cmp)(p->buf_l, p->buf_r);
244         else if (p->more_l)
245             cmp = -2;
246         else
247             cmp = 2;
248         if (!cmp)
249         {
250             memcpy (buf, p->buf_l, info->key_size);
251             *term_index = p->term_index_l;
252             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
253                                    &p->term_index_l);
254             p->tail = 1;
255         }
256         else if (cmp == 1)
257         {
258             memcpy (buf, p->buf_r, info->key_size);
259             *term_index = p->term_index_r + info->term_index_s;
260             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
261                                    &p->term_index_r);
262             p->tail = 1;
263             return 1;
264         }
265         else if (cmp == -1)
266         {
267             memcpy (buf, p->buf_l, info->key_size);
268             *term_index = p->term_index_l;
269             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
270                                    &p->term_index_l);
271             p->tail = 1;
272             return 1;
273         }
274         else if (cmp > 1)
275         {
276             memcpy (buf, p->buf_r, info->key_size);
277             *term_index = p->term_index_r + info->term_index_s;
278             
279             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
280                                    &p->term_index_r);
281             if (p->tail)
282             {
283                 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
284                     p->tail = 0;
285                 return 1;
286             }
287         }
288         else
289         {
290             memcpy (buf, p->buf_l, info->key_size);
291             *term_index = p->term_index_l;
292             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
293                                    &p->term_index_l);
294             if (p->tail)
295             {
296                 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
297                     p->tail = 0;
298                 return 1;
299             }
300         }
301     }
302     return 0;
303 }
304
305 static int r_read_or (RSFD rfd, void *buf, int *term_index)
306 {
307     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
308     struct rset_bool_info *info = p->info;
309
310     while (p->more_l || p->more_r)
311     {
312         int cmp;
313
314         if (p->more_l && p->more_r)
315             cmp = (*info->cmp)(p->buf_l, p->buf_r);
316         else if (p->more_r)
317             cmp = 2;
318         else
319             cmp = -2;
320         if (!cmp)
321         {
322             memcpy (buf, p->buf_l, info->key_size);
323             *term_index = p->term_index_l;
324             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
325                                    &p->term_index_l);
326             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
327                                    &p->term_index_r);
328             return 1;
329         }
330         else if (cmp > 0)
331         {
332             memcpy (buf, p->buf_r, info->key_size);
333             *term_index = p->term_index_r + info->term_index_s;
334             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
335                                    &p->term_index_r);
336             return 1;
337         }
338         else
339         {
340             memcpy (buf, p->buf_l, info->key_size);
341             *term_index = p->term_index_l;
342             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
343                                    &p->term_index_l);
344             return 1;
345         }
346     }
347     return 0;
348 }
349
350 static int r_read_not (RSFD rfd, void *buf, int *term_index)
351 {
352     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
353     struct rset_bool_info *info = p->info;
354
355     while (p->more_l || p->more_r)
356     {
357         int cmp;
358
359         if (p->more_l && p->more_r)
360             cmp = (*info->cmp)(p->buf_l, p->buf_r);
361         else if (p->more_r)
362             cmp = 2;
363         else
364             cmp = -2;
365         if (cmp < -1)
366         {
367             memcpy (buf, p->buf_l, info->key_size);
368             *term_index = p->term_index_l;
369             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
370                                    &p->term_index_l);
371             return 1;
372         }
373         else if (cmp > 1)
374             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
375                                    &p->term_index_r);
376         else
377         {
378             memcpy (buf, p->buf_l, info->key_size);
379             do
380             {
381                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
382                                        &p->term_index_l);
383                 if (!p->more_l)
384                     break;
385                 cmp = (*info->cmp)(p->buf_l, buf);
386             } while (cmp >= -1 && cmp <= 1);
387             do
388             {
389                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
390                                        &p->term_index_r);
391                 if (!p->more_r)
392                     break;
393                 cmp = (*info->cmp)(p->buf_r, buf);
394             } while (cmp >= -1 && cmp <= 1);
395         }
396     }
397     return 0;
398 }
399
400
401 static int r_write (RSFD rfd, const void *buf)
402 {
403     logf (LOG_FATAL, "bool set type is read-only");
404     return -1;
405 }
406