2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1999-05-26 07:49:14 adam
10 * Revision 1.15 1999/02/02 14:51:32 adam
11 * Updated WIN32 code specific sections. Changed header.
13 * Revision 1.14 1998/03/05 08:36:27 adam
14 * New result set model.
16 * Revision 1.13 1997/12/18 10:54:24 adam
17 * New method result set method rs_hits that returns the number of
18 * hits in result-set (if known). The ranked result set returns real
19 * number of hits but only when not combined with other operands.
21 * Revision 1.12 1997/10/31 12:37:01 adam
22 * Code calls xfree() instead of free().
24 * Revision 1.11 1997/09/09 13:38:15 adam
25 * Partial port to WIN95/NT.
27 * Revision 1.10 1996/10/29 13:55:20 adam
28 * Include of zebrautl.h instead of alexutil.h.
30 * Revision 1.9 1995/12/11 09:15:22 adam
31 * New set types: sand/sor/snot - ranked versions of and/or/not in
32 * ranked/semi-ranked result sets.
33 * Note: the snot not finished yet.
34 * New rset member: flag.
35 * Bug fix: r_delete in rsrel.c did free bad memory block.
37 * Revision 1.8 1995/10/12 12:41:55 adam
38 * Private info (buf) moved from struct rset_control to struct rset.
39 * Bug fixes in relevance.
41 * Revision 1.7 1995/10/10 14:00:03 adam
42 * Function rset_open changed its wflag parameter to general flags.
44 * Revision 1.6 1995/10/06 14:38:05 adam
45 * New result set method: r_score.
46 * Local no (sysno) and score is transferred to retrieveCtrl.
48 * Revision 1.5 1995/09/08 14:52:41 adam
49 * Work on relevance feedback.
51 * Revision 1.4 1995/09/08 08:54:04 adam
52 * More efficient and operation.
54 * Revision 1.3 1995/09/07 13:58:43 adam
55 * New parameter: result-set file descriptor (RSFD) to support multiple
56 * positions within the same result-set.
57 * Boolean operators: and, or, not implemented.
59 * Revision 1.2 1995/09/06 16:11:55 adam
60 * More work on boolean sets.
62 * Revision 1.1 1995/09/06 13:27:15 adam
63 * New set type: bool. Not finished yet.
75 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
76 static RSFD r_open (RSET ct, int flag);
77 static void r_close (RSFD rfd);
78 static void r_delete (RSET ct);
79 static void r_rewind (RSFD rfd);
80 static int r_count (RSET ct);
81 static int r_read_and (RSFD rfd, void *buf, int *term_index);
82 static int r_read_or (RSFD rfd, void *buf, int *term_index);
83 static int r_read_not (RSFD rfd, void *buf, int *term_index);
84 static int r_write (RSFD rfd, const void *buf);
86 static const struct rset_control control_and =
99 static const struct rset_control control_or =
112 static const struct rset_control control_not =
126 const struct rset_control *rset_kind_and = &control_and;
127 const struct rset_control *rset_kind_or = &control_or;
128 const struct rset_control *rset_kind_not = &control_not;
130 struct rset_bool_info {
135 int (*cmp)(const void *p1, const void *p2);
136 struct rset_bool_rfd *rfd_list;
139 struct rset_bool_rfd {
148 struct rset_bool_rfd *next;
149 struct rset_bool_info *info;
152 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
154 rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
155 struct rset_bool_info *info;
157 info = (struct rset_bool_info *) xmalloc (sizeof(*info));
158 info->key_size = bool_parms->key_size;
159 info->rset_l = bool_parms->rset_l;
160 info->rset_r = bool_parms->rset_r;
161 if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
162 ct->flags |= RSET_FLAG_VOLATILE;
163 info->cmp = bool_parms->cmp;
164 info->rfd_list = NULL;
166 info->term_index_s = info->rset_l->no_rset_terms;
168 info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
169 ct->rset_terms = (RSET_TERM *)
170 xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
172 memcpy (ct->rset_terms, info->rset_l->rset_terms,
173 info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
174 memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
175 info->rset_r->rset_terms,
176 info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
180 static RSFD r_open (RSET ct, int flag)
182 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
183 struct rset_bool_rfd *rfd;
185 if (flag & RSETF_WRITE)
187 logf (LOG_FATAL, "bool set type is read-only");
190 rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
191 rfd->next = info->rfd_list;
192 info->rfd_list = rfd;
195 rfd->buf_l = xmalloc (info->key_size);
196 rfd->buf_r = xmalloc (info->key_size);
197 rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
198 rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
199 rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
201 rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
206 static void r_close (RSFD rfd)
208 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
209 struct rset_bool_rfd **rfdp;
211 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
214 xfree ((*rfdp)->buf_l);
215 xfree ((*rfdp)->buf_r);
216 rset_close (info->rset_l, (*rfdp)->rfd_l);
217 rset_close (info->rset_r, (*rfdp)->rfd_r);
218 *rfdp = (*rfdp)->next;
222 logf (LOG_FATAL, "r_close but no rfd match!");
226 static void r_delete (RSET ct)
228 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
230 assert (info->rfd_list == NULL);
231 xfree (ct->rset_terms);
232 rset_delete (info->rset_l);
233 rset_delete (info->rset_r);
237 static void r_rewind (RSFD rfd)
239 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
240 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
242 logf (LOG_DEBUG, "rsbool_rewind");
243 rset_rewind (info->rset_l, p->rfd_l);
244 rset_rewind (info->rset_r, p->rfd_r);
245 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
246 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
249 static int r_count (RSET ct)
254 static int r_read_and (RSFD rfd, void *buf, int *term_index)
256 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
257 struct rset_bool_info *info = p->info;
259 while (p->more_l && p->more_r)
263 cmp = (*info->cmp)(p->buf_l, p->buf_r);
266 memcpy (buf, p->buf_l, info->key_size);
267 *term_index = p->term_index_l;
268 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
270 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
276 memcpy (buf, p->buf_r, info->key_size);
278 *term_index = p->term_index_r + info->term_index_s;
279 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
285 memcpy (buf, p->buf_l, info->key_size);
286 *term_index = p->term_index_l;
287 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
292 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
295 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
301 static int r_read_or (RSFD rfd, void *buf, int *term_index)
303 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
304 struct rset_bool_info *info = p->info;
306 while (p->more_l || p->more_r)
310 if (p->more_l && p->more_r)
311 cmp = (*info->cmp)(p->buf_l, p->buf_r);
318 memcpy (buf, p->buf_l, info->key_size);
319 *term_index = p->term_index_l;
320 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
322 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
328 memcpy (buf, p->buf_r, info->key_size);
329 *term_index = p->term_index_r + info->term_index_s;
330 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
336 memcpy (buf, p->buf_l, info->key_size);
337 *term_index = p->term_index_l;
338 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
346 static int r_read_not (RSFD rfd, void *buf, int *term_index)
348 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
349 struct rset_bool_info *info = p->info;
351 while (p->more_l || p->more_r)
355 if (p->more_l && p->more_r)
356 cmp = (*info->cmp)(p->buf_l, p->buf_r);
363 memcpy (buf, p->buf_l, info->key_size);
364 *term_index = p->term_index_l;
365 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
370 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
374 memcpy (buf, p->buf_l, info->key_size);
377 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
381 cmp = (*info->cmp)(p->buf_l, buf);
382 } while (cmp >= -1 && cmp <= 1);
385 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
389 cmp = (*info->cmp)(p->buf_r, buf);
390 } while (cmp >= -1 && cmp <= 1);
397 static int r_write (RSFD rfd, const void *buf)
399 logf (LOG_FATAL, "bool set type is read-only");