2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1998-03-05 08:36:27 adam
8 * New result set model.
10 * Revision 1.13 1997/12/18 10:54:24 adam
11 * New method result set method rs_hits that returns the number of
12 * hits in result-set (if known). The ranked result set returns real
13 * number of hits but only when not combined with other operands.
15 * Revision 1.12 1997/10/31 12:37:01 adam
16 * Code calls xfree() instead of free().
18 * Revision 1.11 1997/09/09 13:38:15 adam
19 * Partial port to WIN95/NT.
21 * Revision 1.10 1996/10/29 13:55:20 adam
22 * Include of zebrautl.h instead of alexutil.h.
24 * Revision 1.9 1995/12/11 09:15:22 adam
25 * New set types: sand/sor/snot - ranked versions of and/or/not in
26 * ranked/semi-ranked result sets.
27 * Note: the snot not finished yet.
28 * New rset member: flag.
29 * Bug fix: r_delete in rsrel.c did free bad memory block.
31 * Revision 1.8 1995/10/12 12:41:55 adam
32 * Private info (buf) moved from struct rset_control to struct rset.
33 * Bug fixes in relevance.
35 * Revision 1.7 1995/10/10 14:00:03 adam
36 * Function rset_open changed its wflag parameter to general flags.
38 * Revision 1.6 1995/10/06 14:38:05 adam
39 * New result set method: r_score.
40 * Local no (sysno) and score is transferred to retrieveCtrl.
42 * Revision 1.5 1995/09/08 14:52:41 adam
43 * Work on relevance feedback.
45 * Revision 1.4 1995/09/08 08:54:04 adam
46 * More efficient and operation.
48 * Revision 1.3 1995/09/07 13:58:43 adam
49 * New parameter: result-set file descriptor (RSFD) to support multiple
50 * positions within the same result-set.
51 * Boolean operators: and, or, not implemented.
53 * Revision 1.2 1995/09/06 16:11:55 adam
54 * More work on boolean sets.
56 * Revision 1.1 1995/09/06 13:27:15 adam
57 * New set type: bool. Not finished yet.
69 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
70 static RSFD r_open (RSET ct, int flag);
71 static void r_close (RSFD rfd);
72 static void r_delete (RSET ct);
73 static void r_rewind (RSFD rfd);
74 static int r_count (RSET ct);
75 static int r_read_and (RSFD rfd, void *buf, int *term_index);
76 static int r_read_or (RSFD rfd, void *buf, int *term_index);
77 static int r_read_not (RSFD rfd, void *buf, int *term_index);
78 static int r_write (RSFD rfd, const void *buf);
80 static const struct rset_control control_and =
93 static const struct rset_control control_or =
106 static const struct rset_control control_not =
120 const struct rset_control *rset_kind_and = &control_and;
121 const struct rset_control *rset_kind_or = &control_or;
122 const struct rset_control *rset_kind_not = &control_not;
124 struct rset_bool_info {
129 int (*cmp)(const void *p1, const void *p2);
130 struct rset_bool_rfd *rfd_list;
133 struct rset_bool_rfd {
142 struct rset_bool_rfd *next;
143 struct rset_bool_info *info;
146 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
148 rset_bool_parms *bool_parms = parms;
149 struct rset_bool_info *info;
151 info = xmalloc (sizeof(*info));
152 info->key_size = bool_parms->key_size;
153 info->rset_l = bool_parms->rset_l;
154 info->rset_r = bool_parms->rset_r;
155 if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
156 ct->flags |= RSET_FLAG_VOLATILE;
157 info->cmp = bool_parms->cmp;
158 info->rfd_list = NULL;
160 info->term_index_s = info->rset_l->no_rset_terms;
162 info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
163 ct->rset_terms = xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
165 memcpy (ct->rset_terms, info->rset_l->rset_terms,
166 info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
167 memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
168 info->rset_r->rset_terms,
169 info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
173 static RSFD r_open (RSET ct, int flag)
175 struct rset_bool_info *info = ct->buf;
176 struct rset_bool_rfd *rfd;
178 if (flag & RSETF_WRITE)
180 logf (LOG_FATAL, "bool set type is read-only");
183 rfd = xmalloc (sizeof(*rfd));
184 rfd->next = info->rfd_list;
185 info->rfd_list = rfd;
188 rfd->buf_l = xmalloc (info->key_size);
189 rfd->buf_r = xmalloc (info->key_size);
190 rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
191 rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
192 rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
194 rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
199 static void r_close (RSFD rfd)
201 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
202 struct rset_bool_rfd **rfdp;
204 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
207 xfree ((*rfdp)->buf_l);
208 xfree ((*rfdp)->buf_r);
209 rset_close (info->rset_l, (*rfdp)->rfd_l);
210 rset_close (info->rset_r, (*rfdp)->rfd_r);
211 *rfdp = (*rfdp)->next;
215 logf (LOG_FATAL, "r_close but no rfd match!");
219 static void r_delete (RSET ct)
221 struct rset_bool_info *info = ct->buf;
223 assert (info->rfd_list == NULL);
224 xfree (ct->rset_terms);
225 rset_delete (info->rset_l);
226 rset_delete (info->rset_r);
230 static void r_rewind (RSFD rfd)
232 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
233 struct rset_bool_rfd *p = rfd;
235 logf (LOG_DEBUG, "rsbool_rewind");
236 rset_rewind (info->rset_l, p->rfd_l);
237 rset_rewind (info->rset_r, p->rfd_r);
238 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
239 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
242 static int r_count (RSET ct)
247 static int r_read_and (RSFD rfd, void *buf, int *term_index)
249 struct rset_bool_rfd *p = rfd;
250 struct rset_bool_info *info = p->info;
252 while (p->more_l && p->more_r)
256 cmp = (*info->cmp)(p->buf_l, p->buf_r);
259 memcpy (buf, p->buf_l, info->key_size);
260 *term_index = p->term_index_l;
261 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
263 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
269 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,
285 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
288 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
294 static int r_read_or (RSFD rfd, void *buf, int *term_index)
296 struct rset_bool_rfd *p = rfd;
297 struct rset_bool_info *info = p->info;
299 while (p->more_l || p->more_r)
303 if (p->more_l && p->more_r)
304 cmp = (*info->cmp)(p->buf_l, p->buf_r);
311 memcpy (buf, p->buf_l, info->key_size);
312 *term_index = p->term_index_l;
313 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
315 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
321 memcpy (buf, p->buf_r, info->key_size);
322 *term_index = p->term_index_r + info->term_index_s;
323 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
329 memcpy (buf, p->buf_l, info->key_size);
330 *term_index = p->term_index_l;
331 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
339 static int r_read_not (RSFD rfd, void *buf, int *term_index)
341 struct rset_bool_rfd *p = rfd;
342 struct rset_bool_info *info = p->info;
344 while (p->more_l || p->more_r)
348 if (p->more_l && p->more_r)
349 cmp = (*info->cmp)(p->buf_l, p->buf_r);
356 memcpy (buf, p->buf_l, info->key_size);
357 *term_index = p->term_index_l;
358 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
363 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
367 memcpy (buf, p->buf_l, info->key_size);
370 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
374 cmp = (*info->cmp)(p->buf_l, buf);
375 } while (cmp >= -1 && cmp <= 1);
378 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
382 cmp = (*info->cmp)(p->buf_r, buf);
383 } while (cmp >= -1 && cmp <= 1);
390 static int r_write (RSFD rfd, const void *buf)
392 logf (LOG_FATAL, "bool set type is read-only");