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