2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: rsbool.c,v 1.17 2002-03-20 20:24:30 adam Exp $
17 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
18 static RSFD r_open (RSET ct, int flag);
19 static void r_close (RSFD rfd);
20 static void r_delete (RSET ct);
21 static void r_rewind (RSFD rfd);
22 static int r_count (RSET ct);
23 static int r_read_and (RSFD rfd, void *buf, int *term_index);
24 static int r_read_or (RSFD rfd, void *buf, int *term_index);
25 static int r_read_not (RSFD rfd, void *buf, int *term_index);
26 static int r_write (RSFD rfd, const void *buf);
28 static const struct rset_control control_and =
41 static const struct rset_control control_or =
54 static const struct rset_control control_not =
68 const struct rset_control *rset_kind_and = &control_and;
69 const struct rset_control *rset_kind_or = &control_or;
70 const struct rset_control *rset_kind_not = &control_not;
72 struct rset_bool_info {
77 int (*cmp)(const void *p1, const void *p2);
78 struct rset_bool_rfd *rfd_list;
81 struct rset_bool_rfd {
90 struct rset_bool_rfd *next;
91 struct rset_bool_info *info;
94 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
96 rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
97 struct rset_bool_info *info;
99 info = (struct rset_bool_info *) xmalloc (sizeof(*info));
100 info->key_size = bool_parms->key_size;
101 info->rset_l = bool_parms->rset_l;
102 info->rset_r = bool_parms->rset_r;
103 if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
104 ct->flags |= RSET_FLAG_VOLATILE;
105 info->cmp = bool_parms->cmp;
106 info->rfd_list = NULL;
108 info->term_index_s = info->rset_l->no_rset_terms;
110 info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
111 ct->rset_terms = (RSET_TERM *)
112 xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
114 memcpy (ct->rset_terms, info->rset_l->rset_terms,
115 info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
116 memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
117 info->rset_r->rset_terms,
118 info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
122 static RSFD r_open (RSET ct, int flag)
124 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
125 struct rset_bool_rfd *rfd;
127 if (flag & RSETF_WRITE)
129 logf (LOG_FATAL, "bool set type is read-only");
132 rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
133 rfd->next = info->rfd_list;
134 info->rfd_list = rfd;
137 rfd->buf_l = xmalloc (info->key_size);
138 rfd->buf_r = xmalloc (info->key_size);
139 rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
140 rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
141 rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
143 rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
148 static void r_close (RSFD rfd)
150 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
151 struct rset_bool_rfd **rfdp;
153 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
156 xfree ((*rfdp)->buf_l);
157 xfree ((*rfdp)->buf_r);
158 rset_close (info->rset_l, (*rfdp)->rfd_l);
159 rset_close (info->rset_r, (*rfdp)->rfd_r);
160 *rfdp = (*rfdp)->next;
164 logf (LOG_FATAL, "r_close but no rfd match!");
168 static void r_delete (RSET ct)
170 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
172 assert (info->rfd_list == NULL);
173 xfree (ct->rset_terms);
174 rset_delete (info->rset_l);
175 rset_delete (info->rset_r);
179 static void r_rewind (RSFD rfd)
181 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
182 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
184 logf (LOG_DEBUG, "rsbool_rewind");
185 rset_rewind (info->rset_l, p->rfd_l);
186 rset_rewind (info->rset_r, p->rfd_r);
187 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
188 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
191 static int r_count (RSET ct)
196 static int r_read_and (RSFD rfd, void *buf, int *term_index)
198 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
199 struct rset_bool_info *info = p->info;
201 while (p->more_l && p->more_r)
205 cmp = (*info->cmp)(p->buf_l, p->buf_r);
208 memcpy (buf, p->buf_l, info->key_size);
209 *term_index = p->term_index_l;
210 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
212 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
218 memcpy (buf, p->buf_r, info->key_size);
220 *term_index = p->term_index_r + info->term_index_s;
221 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
227 memcpy (buf, p->buf_l, info->key_size);
228 *term_index = p->term_index_l;
229 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
234 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
237 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
243 static int r_read_or (RSFD rfd, void *buf, int *term_index)
245 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
246 struct rset_bool_info *info = p->info;
248 while (p->more_l || p->more_r)
252 if (p->more_l && p->more_r)
253 cmp = (*info->cmp)(p->buf_l, p->buf_r);
260 memcpy (buf, p->buf_l, info->key_size);
261 *term_index = p->term_index_l;
262 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
264 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
270 memcpy (buf, p->buf_r, info->key_size);
271 *term_index = p->term_index_r + info->term_index_s;
272 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
278 memcpy (buf, p->buf_l, info->key_size);
279 *term_index = p->term_index_l;
280 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
288 static int r_read_not (RSFD rfd, void *buf, int *term_index)
290 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
291 struct rset_bool_info *info = p->info;
293 while (p->more_l || p->more_r)
297 if (p->more_l && p->more_r)
298 cmp = (*info->cmp)(p->buf_l, p->buf_r);
305 memcpy (buf, p->buf_l, info->key_size);
306 *term_index = p->term_index_l;
307 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
312 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
316 memcpy (buf, p->buf_l, info->key_size);
319 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
323 cmp = (*info->cmp)(p->buf_l, buf);
324 } while (cmp >= -1 && cmp <= 1);
327 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
331 cmp = (*info->cmp)(p->buf_r, buf);
332 } while (cmp >= -1 && cmp <= 1);
339 static int r_write (RSFD rfd, const void *buf)
341 logf (LOG_FATAL, "bool set type is read-only");