2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1995-12-11 09:15:27 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.
22 static void *r_create_and(const struct rset_control *sel, void *parms,
24 static void *r_create_or(const struct rset_control *sel, void *parms,
26 static void *r_create_not(const struct rset_control *sel, void *parms,
28 static RSFD r_open (RSET ct, int flag);
29 static void r_close (RSFD rfd);
30 static void r_delete (RSET ct);
31 static void r_rewind (RSFD rfd);
32 static int r_count (RSET ct);
33 static int r_read (RSFD rfd, void *buf);
34 static int r_write (RSFD rfd, const void *buf);
35 static int r_score (RSFD rfd, int *score);
37 static const rset_control control_sand =
51 static const rset_control control_sor =
65 static const rset_control control_snot =
80 const rset_control *rset_kind_sand = &control_sand;
81 const rset_control *rset_kind_sor = &control_sor;
82 const rset_control *rset_kind_snot = &control_snot;
84 struct rset_bool_info {
92 int (*cmp)(const void *p1, const void *p2);
93 struct rset_bool_rfd *rfd_list;
96 struct rset_bool_rfd {
97 struct rset_bool_rfd *next;
98 struct rset_bool_info *info;
104 static void *r_create_common (const struct rset_control *sel,
105 rset_bool_parms *bool_parms, int *flags);
107 static void key_add (struct rset_bool_info *info,
108 char *buf, int score)
110 if (info->key_no == info->key_max)
112 memcpy (info->key_buf + info->key_size * info->key_no,
113 buf, info->key_size);
114 info->score_buf[info->key_no] = score;
118 static void *r_create_and (const struct rset_control *sel, void *parms,
125 struct rset_bool_info *info;
126 info = r_create_common (sel, parms, flags);
128 buf_l = xmalloc (info->key_size);
129 buf_r = xmalloc (info->key_size);
130 fd_l = rset_open (info->rset_l, RSETF_SORT_SYSNO|RSETF_READ);
131 fd_r = rset_open (info->rset_r, RSETF_SORT_SYSNO|RSETF_READ);
133 more_l = rset_read(info->rset_l, fd_l, buf_l);
134 more_r = rset_read(info->rset_r, fd_r, buf_r);
136 while (more_l || more_r)
139 int score, score_l, score_r;
141 if (more_l && more_r)
142 cmp = (*info->cmp)(buf_l, buf_r);
148 if (cmp >= -1 && cmp <= 1)
150 rset_score (info->rset_l, fd_l, &score_l);
151 rset_score (info->rset_r, fd_r, &score_r);
154 else if (score_r == -1)
157 score = score_l > score_r ? score_r : score_l;
158 key_add (info, buf_l, score);
160 more_l = rset_read (info->rset_l, fd_l, buf_l);
161 more_r = rset_read (info->rset_r, fd_r, buf_r);
165 rset_score (info->rset_r, fd_r, &score_r);
167 key_add (info, buf_r, 1);
168 more_r = rset_read (info->rset_r, fd_r, buf_r);
172 rset_score (info->rset_l, fd_l, &score_l);
174 key_add (info, buf_l, 1);
175 more_l = rset_read (info->rset_l, fd_l, buf_l);
178 rset_close (info->rset_l, fd_l);
179 rset_close (info->rset_r, fd_r);
180 rset_delete (info->rset_l);
181 rset_delete (info->rset_r);
187 static void *r_create_or (const struct rset_control *sel, void *parms,
194 struct rset_bool_info *info;
195 info = r_create_common (sel, parms, flags);
197 buf_l = xmalloc (info->key_size);
198 buf_r = xmalloc (info->key_size);
199 fd_l = rset_open (info->rset_l, RSETF_SORT_SYSNO|RSETF_READ);
200 fd_r = rset_open (info->rset_r, RSETF_SORT_SYSNO|RSETF_READ);
202 more_l = rset_read(info->rset_l, fd_l, buf_l);
203 more_r = rset_read(info->rset_r, fd_r, buf_r);
205 while (more_l || more_r)
208 int score, score_l, score_r;
210 if (more_l && more_r)
211 cmp = (*info->cmp)(buf_l, buf_r);
217 if (cmp >= -1 && cmp <= 1)
219 rset_score (info->rset_l, fd_l, &score_l);
220 rset_score (info->rset_r, fd_r, &score_r);
223 else if (score_r == -1)
226 score = score_r > score_l ? score_r : score_l;
227 key_add (info, buf_l, score);
229 more_l = rset_read (info->rset_l, fd_l, buf_l);
230 more_r = rset_read (info->rset_r, fd_r, buf_r);
234 rset_score (info->rset_r, fd_r, &score_r);
236 key_add (info, buf_r, score_r / 2);
237 more_r = rset_read (info->rset_r, fd_r, buf_r);
241 rset_score (info->rset_l, fd_l, &score_l);
243 key_add (info, buf_l, score_l / 2);
244 more_l = rset_read (info->rset_l, fd_l, buf_l);
247 rset_close (info->rset_l, fd_l);
248 rset_close (info->rset_r, fd_r);
249 rset_delete (info->rset_l);
250 rset_delete (info->rset_r);
256 static void *r_create_not (const struct rset_control *sel, void *parms,
261 struct rset_bool_info *info;
262 info = r_create_common (sel, parms, flags);
264 buf_l = xmalloc (info->key_size);
265 buf_r = xmalloc (info->key_size);
266 rset_delete (info->rset_l);
267 rset_delete (info->rset_r);
273 static void *r_create_common (const struct rset_control *sel,
274 rset_bool_parms *bool_parms, int *flags)
276 struct rset_bool_info *info;
278 info = xmalloc (sizeof(*info));
279 info->key_size = bool_parms->key_size;
280 info->rset_l = bool_parms->rset_l;
281 info->rset_r = bool_parms->rset_r;
282 info->cmp = bool_parms->cmp;
283 info->rfd_list = NULL;
285 if (rset_is_ranked(info->rset_l) || rset_is_ranked(info->rset_r))
286 *flags |= RSET_FLAG_RANKED;
288 info->key_max = rset_count(bool_parms->rset_l)
289 +rset_count(bool_parms->rset_r);
292 if (info->key_max > 1000)
293 info->key_max = 1000;
294 info->key_buf = xmalloc (info->key_size * info->key_max);
295 info->score_buf = xmalloc (info->key_max * sizeof(*info->score_buf));
301 static RSFD r_open (RSET ct, int flag)
303 struct rset_bool_info *info = ct->buf;
304 struct rset_bool_rfd *rfd;
306 if (flag & RSETF_WRITE)
308 logf (LOG_FATAL, "sbool set type is read-only");
311 rfd = xmalloc (sizeof(*rfd));
312 rfd->next = info->rfd_list;
313 info->rfd_list = rfd;
318 rfd->open_flag = flag;
323 static void r_close (RSFD rfd)
325 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
326 struct rset_bool_rfd **rfdp;
328 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
331 *rfdp = (*rfdp)->next;
335 logf (LOG_FATAL, "r_close but no rfd match!");
339 static void r_delete (RSET ct)
341 struct rset_bool_info *info = ct->buf;
343 assert (info->rfd_list == NULL);
344 xfree (info->score_buf);
345 xfree (info->key_buf);
349 static void r_rewind (RSFD rfd)
351 struct rset_bool_rfd *p = rfd;
353 logf (LOG_DEBUG, "rsbool_rewind");
354 p->position = p->last_pos = 0;
357 static int r_count (RSET ct)
359 struct rset_bool_info *info = ct->buf;
364 static int r_read (RSFD rfd, void *buf)
366 struct rset_bool_rfd *p = rfd;
367 struct rset_bool_info *info = p->info;
369 if (p->position >= info->key_no)
371 p->last_pos = (p->position)++;
372 memcpy (buf, info->key_buf + info->key_size * p->last_pos,
377 static int r_write (RSFD rfd, const void *buf)
379 logf (LOG_FATAL, "sbool set type is read-only");
383 static int r_score (RSFD rfd, int *score)
385 struct rset_bool_rfd *p = rfd;
386 struct rset_bool_info *info = p->info;
388 *score = info->score_buf[p->last_pos];