Fix and operation which in some cases for equal keys
[idzebra-moved-to-github.git] / rset / rsbool.c
1 /* $Id: rsbool.c,v 1.21 2003-03-28 13:53:41 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     int tail;
107     struct rset_bool_rfd *next;
108     struct rset_bool_info *info;
109 };    
110
111 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
112 {
113     rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
114     struct rset_bool_info *info;
115
116     info = (struct rset_bool_info *) xmalloc (sizeof(*info));
117     info->key_size = bool_parms->key_size;
118     info->rset_l = bool_parms->rset_l;
119     info->rset_r = bool_parms->rset_r;
120     if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
121         ct->flags |= RSET_FLAG_VOLATILE;
122     info->cmp = bool_parms->cmp;
123     info->rfd_list = NULL;
124     
125     info->term_index_s = info->rset_l->no_rset_terms;
126     ct->no_rset_terms =
127         info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
128     ct->rset_terms = (RSET_TERM *)
129         xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
130
131     memcpy (ct->rset_terms, info->rset_l->rset_terms,
132             info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
133     memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
134             info->rset_r->rset_terms,
135             info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
136     return info;
137 }
138
139 static RSFD r_open (RSET ct, int flag)
140 {
141     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
142     struct rset_bool_rfd *rfd;
143
144     if (flag & RSETF_WRITE)
145     {
146         logf (LOG_FATAL, "bool set type is read-only");
147         return NULL;
148     }
149     rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
150     rfd->next = info->rfd_list;
151     info->rfd_list = rfd;
152     rfd->info = info;
153
154     rfd->buf_l = xmalloc (info->key_size);
155     rfd->buf_r = xmalloc (info->key_size);
156     rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
157     rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
158     rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
159                              &rfd->term_index_l);
160     rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
161                              &rfd->term_index_r);
162     rfd->tail = 0;
163     return rfd;
164 }
165
166 static void r_close (RSFD rfd)
167 {
168     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
169     struct rset_bool_rfd **rfdp;
170     
171     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
172         if (*rfdp == rfd)
173         {
174             xfree ((*rfdp)->buf_l);
175             xfree ((*rfdp)->buf_r);
176             rset_close (info->rset_l, (*rfdp)->rfd_l);
177             rset_close (info->rset_r, (*rfdp)->rfd_r);
178             *rfdp = (*rfdp)->next;
179             xfree (rfd);
180             return;
181         }
182     logf (LOG_FATAL, "r_close but no rfd match!");
183     assert (0);
184 }
185
186 static void r_delete (RSET ct)
187 {
188     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
189
190     assert (info->rfd_list == NULL);
191     xfree (ct->rset_terms);
192     rset_delete (info->rset_l);
193     rset_delete (info->rset_r);
194     xfree (info);
195 }
196
197 static void r_rewind (RSFD rfd)
198 {
199     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
200     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
201
202     logf (LOG_DEBUG, "rsbool_rewind");
203     rset_rewind (info->rset_l, p->rfd_l);
204     rset_rewind (info->rset_r, p->rfd_r);
205     p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
206     p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
207 }
208
209 static int r_count (RSET ct)
210 {
211     return 0;
212 }
213
214
215 /*
216     1,1         1,3
217     1,9         2,1
218     1,11        3,1
219     2,9
220
221   1,1     1,1
222   1,3     1,3
223           1,9
224           1,11
225   2,1     2,1
226           2,9
227           3,1
228 */
229
230 static int r_read_and (RSFD rfd, void *buf, int *term_index)
231 {
232     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
233     struct rset_bool_info *info = p->info;
234
235     while (p->more_l || p->more_r)
236     {
237         int cmp;
238
239         if (p->more_l && p->more_r)
240             cmp = (*info->cmp)(p->buf_l, p->buf_r);
241         else if (p->more_l)
242             cmp = -2;
243         else
244             cmp = 2;
245         if (!cmp)
246         {
247             memcpy (buf, p->buf_l, info->key_size);
248             *term_index = p->term_index_l;
249             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
250                                    &p->term_index_l);
251             p->tail = 1;
252         }
253         else if (cmp == 1)
254         {
255             memcpy (buf, p->buf_r, info->key_size);
256             *term_index = p->term_index_r + info->term_index_s;
257             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
258                                    &p->term_index_r);
259             p->tail = 1;
260             return 1;
261         }
262         else if (cmp == -1)
263         {
264             memcpy (buf, p->buf_l, info->key_size);
265             *term_index = p->term_index_l;
266             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
267                                    &p->term_index_l);
268             p->tail = 1;
269             return 1;
270         }
271         else if (cmp > 1)
272         {
273             memcpy (buf, p->buf_r, info->key_size);
274             *term_index = p->term_index_r + info->term_index_s;
275             
276             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
277                                    &p->term_index_r);
278             if (p->tail)
279             {
280                 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
281                     p->tail = 0;
282                 return 1;
283             }
284         }
285         else
286         {
287             memcpy (buf, p->buf_l, info->key_size);
288             *term_index = p->term_index_l;
289             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
290                                    &p->term_index_l);
291             if (p->tail)
292             {
293                 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
294                     p->tail = 0;
295                 return 1;
296             }
297         }
298     }
299     return 0;
300 }
301
302 static int r_read_or (RSFD rfd, void *buf, int *term_index)
303 {
304     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
305     struct rset_bool_info *info = p->info;
306
307     while (p->more_l || p->more_r)
308     {
309         int cmp;
310
311         if (p->more_l && p->more_r)
312             cmp = (*info->cmp)(p->buf_l, p->buf_r);
313         else if (p->more_r)
314             cmp = 2;
315         else
316             cmp = -2;
317         if (!cmp)
318         {
319             memcpy (buf, p->buf_l, info->key_size);
320             *term_index = p->term_index_l;
321             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
322                                    &p->term_index_l);
323             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
324                                    &p->term_index_r);
325             return 1;
326         }
327         else if (cmp > 0)
328         {
329             memcpy (buf, p->buf_r, info->key_size);
330             *term_index = p->term_index_r + info->term_index_s;
331             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
332                                    &p->term_index_r);
333             return 1;
334         }
335         else
336         {
337             memcpy (buf, p->buf_l, info->key_size);
338             *term_index = p->term_index_l;
339             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
340                                    &p->term_index_l);
341             return 1;
342         }
343     }
344     return 0;
345 }
346
347 static int r_read_not (RSFD rfd, void *buf, int *term_index)
348 {
349     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
350     struct rset_bool_info *info = p->info;
351
352     while (p->more_l || p->more_r)
353     {
354         int cmp;
355
356         if (p->more_l && p->more_r)
357             cmp = (*info->cmp)(p->buf_l, p->buf_r);
358         else if (p->more_r)
359             cmp = 2;
360         else
361             cmp = -2;
362         if (cmp < -1)
363         {
364             memcpy (buf, p->buf_l, info->key_size);
365             *term_index = p->term_index_l;
366             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
367                                    &p->term_index_l);
368             return 1;
369         }
370         else if (cmp > 1)
371             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
372                                    &p->term_index_r);
373         else
374         {
375             memcpy (buf, p->buf_l, info->key_size);
376             do
377             {
378                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
379                                        &p->term_index_l);
380                 if (!p->more_l)
381                     break;
382                 cmp = (*info->cmp)(p->buf_l, buf);
383             } while (cmp >= -1 && cmp <= 1);
384             do
385             {
386                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
387                                        &p->term_index_r);
388                 if (!p->more_r)
389                     break;
390                 cmp = (*info->cmp)(p->buf_r, buf);
391             } while (cmp >= -1 && cmp <= 1);
392         }
393     }
394     return 0;
395 }
396
397
398 static int r_write (RSFD rfd, const void *buf)
399 {
400     logf (LOG_FATAL, "bool set type is read-only");
401     return -1;
402 }
403