Fix and op. Mixed ranking/non-ranking did not work
[idzebra-moved-to-github.git] / rset / rsbool.c
1 /* $Id: rsbool.c,v 1.20 2003-01-13 22:16:24 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->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
252                                    &p->term_index_r);
253             p->tail = 1;
254             return 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