2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1995-12-11 09:15:22 adam
8 * New set types: sand/sor/snot - ranked versions of and/or/not in
9 * ranked/semi-ranked result sets.
10 * Note: the snot not finished yet.
11 * New rset member: flag.
12 * Bug fix: r_delete in rsrel.c did free bad memory block.
14 * Revision 1.8 1995/10/12 12:41:55 adam
15 * Private info (buf) moved from struct rset_control to struct rset.
16 * Bug fixes in relevance.
18 * Revision 1.7 1995/10/10 14:00:03 adam
19 * Function rset_open changed its wflag parameter to general flags.
21 * Revision 1.6 1995/10/06 14:38:05 adam
22 * New result set method: r_score.
23 * Local no (sysno) and score is transferred to retrieveCtrl.
25 * Revision 1.5 1995/09/08 14:52:41 adam
26 * Work on relevance feedback.
28 * Revision 1.4 1995/09/08 08:54:04 adam
29 * More efficient and operation.
31 * Revision 1.3 1995/09/07 13:58:43 adam
32 * New parameter: result-set file descriptor (RSFD) to support multiple
33 * positions within the same result-set.
34 * Boolean operators: and, or, not implemented.
36 * Revision 1.2 1995/09/06 16:11:55 adam
37 * More work on boolean sets.
39 * Revision 1.1 1995/09/06 13:27:15 adam
40 * New set type: bool. Not finished yet.
50 static void *r_create(const struct rset_control *sel, void *parms,
52 static RSFD r_open (RSET ct, int flag);
53 static void r_close (RSFD rfd);
54 static void r_delete (RSET ct);
55 static void r_rewind (RSFD rfd);
56 static int r_count (RSET ct);
57 static int r_read_and (RSFD rfd, void *buf);
58 static int r_read_or (RSFD rfd, void *buf);
59 static int r_read_not (RSFD rfd, void *buf);
60 static int r_write (RSFD rfd, const void *buf);
61 static int r_score (RSFD rfd, int *score);
63 static const rset_control control_and =
77 static const rset_control control_or =
91 static const rset_control control_not =
106 const rset_control *rset_kind_and = &control_and;
107 const rset_control *rset_kind_or = &control_or;
108 const rset_control *rset_kind_not = &control_not;
110 struct rset_bool_info {
114 int (*cmp)(const void *p1, const void *p2);
115 struct rset_bool_rfd *rfd_list;
118 struct rset_bool_rfd {
125 struct rset_bool_rfd *next;
126 struct rset_bool_info *info;
129 static void *r_create (const struct rset_control *sel, void *parms,
132 rset_bool_parms *bool_parms = parms;
133 struct rset_bool_info *info;
135 info = xmalloc (sizeof(*info));
136 info->key_size = bool_parms->key_size;
137 info->rset_l = bool_parms->rset_l;
138 info->rset_r = bool_parms->rset_r;
139 if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
140 *flags |= RSET_FLAG_VOLATILE;
141 info->cmp = bool_parms->cmp;
142 info->rfd_list = NULL;
146 static RSFD r_open (RSET ct, int flag)
148 struct rset_bool_info *info = ct->buf;
149 struct rset_bool_rfd *rfd;
151 if (flag & RSETF_WRITE)
153 logf (LOG_FATAL, "bool set type is read-only");
156 rfd = xmalloc (sizeof(*rfd));
157 rfd->next = info->rfd_list;
158 info->rfd_list = rfd;
161 rfd->buf_l = xmalloc (info->key_size);
162 rfd->buf_r = xmalloc (info->key_size);
163 rfd->rfd_l = rset_open (info->rset_l, RSETF_READ|RSETF_SORT_SYSNO);
164 rfd->rfd_r = rset_open (info->rset_r, RSETF_READ|RSETF_SORT_SYSNO);
165 rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l);
166 rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r);
170 static void r_close (RSFD rfd)
172 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
173 struct rset_bool_rfd **rfdp;
175 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
178 xfree ((*rfdp)->buf_l);
179 xfree ((*rfdp)->buf_r);
180 rset_close (info->rset_l, (*rfdp)->rfd_l);
181 rset_close (info->rset_r, (*rfdp)->rfd_r);
182 *rfdp = (*rfdp)->next;
186 logf (LOG_FATAL, "r_close but no rfd match!");
190 static void r_delete (RSET ct)
192 struct rset_bool_info *info = ct->buf;
194 assert (info->rfd_list == NULL);
195 rset_delete (info->rset_l);
196 rset_delete (info->rset_r);
200 static void r_rewind (RSFD rfd)
202 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
203 struct rset_bool_rfd *p = rfd;
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);
209 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
212 static int r_count (RSET ct)
217 static int r_read_and (RSFD rfd, void *buf)
219 struct rset_bool_rfd *p = rfd;
220 struct rset_bool_info *info = p->info;
222 while (p->more_l && p->more_r)
226 cmp = (*info->cmp)(p->buf_l, p->buf_r);
229 memcpy (buf, p->buf_l, info->key_size);
230 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
231 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
236 memcpy (buf, p->buf_r, info->key_size);
237 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
242 memcpy (buf, p->buf_l, info->key_size);
243 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
247 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
249 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
254 static int r_read_or (RSFD rfd, void *buf)
256 struct rset_bool_rfd *p = rfd;
257 struct rset_bool_info *info = p->info;
259 while (p->more_l || p->more_r)
263 if (p->more_l && p->more_r)
264 cmp = (*info->cmp)(p->buf_l, p->buf_r);
271 memcpy (buf, p->buf_l, info->key_size);
272 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
273 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
278 memcpy (buf, p->buf_r, info->key_size);
279 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
284 memcpy (buf, p->buf_l, info->key_size);
285 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
292 static int r_read_not (RSFD rfd, void *buf)
294 struct rset_bool_rfd *p = rfd;
295 struct rset_bool_info *info = p->info;
297 while (p->more_l || p->more_r)
301 if (p->more_l && p->more_r)
302 cmp = (*info->cmp)(p->buf_l, p->buf_r);
309 memcpy (buf, p->buf_l, info->key_size);
310 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
314 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
317 memcpy (buf, p->buf_l, info->key_size);
320 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);
330 cmp = (*info->cmp)(p->buf_r, buf);
331 } while (cmp >= -1 && cmp <= 1);
338 static int r_write (RSFD rfd, const void *buf)
340 logf (LOG_FATAL, "bool set type is read-only");
344 static int r_score (RSFD rfd, int *score)