2 * Copyright (C) 1994-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1999-05-26 07:49:14 adam
10 * Revision 1.7 1998/09/22 10:03:46 adam
11 * Changed result sets to be persistent in the sense that they can
12 * be re-searched if needed.
13 * Fixed memory leak in rsm_or.
15 * Revision 1.6 1998/03/05 08:36:28 adam
16 * New result set model.
18 * Revision 1.5 1997/12/18 10:54:25 adam
19 * New method result set method rs_hits that returns the number of
20 * hits in result-set (if known). The ranked result set returns real
21 * number of hits but only when not combined with other operands.
23 * Revision 1.4 1997/10/31 12:37:55 adam
24 * Code calls xfree() instead of free().
26 * Revision 1.3 1997/09/09 13:38:16 adam
27 * Partial port to WIN95/NT.
29 * Revision 1.2 1996/12/23 15:30:49 adam
32 * Revision 1.1 1996/12/20 11:07:21 adam
33 * Implemented Multi-or result set.
47 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
48 static RSFD r_open (RSET ct, int flag);
49 static void r_close (RSFD rfd);
50 static void r_delete (RSET ct);
51 static void r_rewind (RSFD rfd);
52 static int r_count (RSET ct);
53 static int r_read (RSFD rfd, void *buf, int *term_index);
54 static int r_write (RSFD rfd, const void *buf);
56 static const struct rset_control control =
69 const struct rset_control *rset_kind_m_or = &control;
71 struct rset_mor_info {
74 int (*cmp)(const void *p1, const void *p2);
76 ISAM_P *isam_positions;
78 int no_isam_positions;
79 int no_save_positions;
80 struct rset_mor_rfd *rfd_list;
88 int (*cmp)(const void *p1, const void *p2);
100 struct rset_mor_rfd *next;
101 struct rset_mor_info *info;
102 struct trunc_info *ti;
105 static void heap_swap (struct trunc_info *ti, int i1, int i2)
110 ti->ptr[i1] = ti->ptr[i2];
114 static void heap_delete (struct trunc_info *ti)
116 int cur = 1, child = 2;
118 heap_swap (ti, 1, ti->heapnum--);
119 while (child <= ti->heapnum) {
120 if (child < ti->heapnum &&
121 (*ti->cmp)(ti->heap[ti->ptr[child]],
122 ti->heap[ti->ptr[1+child]]) > 0)
124 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
125 ti->heap[ti->ptr[child]]) > 0)
127 heap_swap (ti, cur, child);
136 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
140 cur = ++(ti->heapnum);
141 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
142 ti->indx[ti->ptr[cur]] = indx;
144 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
145 ti->heap[ti->ptr[cur]]) > 0)
147 heap_swap (ti, cur, parent);
154 struct trunc_info *heap_init (int size, int key_size,
155 int (*cmp)(const void *p1, const void *p2))
157 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
162 ti->keysize = key_size;
164 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
165 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
166 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
167 ti->swapbuf = (char *) xmalloc (ti->keysize);
168 ti->tmpbuf = (char *) xmalloc (ti->keysize);
169 ti->buf = (char *) xmalloc (size * ti->keysize);
170 for (i = size; --i >= 0; )
173 ti->heap[i] = ti->buf + ti->keysize * i;
178 static void heap_close (struct trunc_info *ti)
188 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
190 rset_m_or_parms *r_parms = (rset_m_or_parms *) parms;
191 struct rset_mor_info *info;
193 ct->flags |= RSET_FLAG_VOLATILE;
194 info = (struct rset_mor_info *) xmalloc (sizeof(*info));
195 info->key_size = r_parms->key_size;
196 assert (info->key_size > 1);
197 info->cmp = r_parms->cmp;
199 info->no_save_positions = r_parms->no_save_positions;
201 info->isc = r_parms->isc;
202 info->no_isam_positions = r_parms->no_isam_positions;
203 info->isam_positions = (ISAM_P *)
204 xmalloc (sizeof(*info->isam_positions) * info->no_isam_positions);
205 memcpy (info->isam_positions, r_parms->isam_positions,
206 sizeof(*info->isam_positions) * info->no_isam_positions);
207 info->rfd_list = NULL;
209 ct->no_rset_terms = 1;
210 ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
211 ct->rset_terms[0] = r_parms->rset_term;
215 static RSFD r_open (RSET ct, int flag)
217 struct rset_mor_rfd *rfd;
218 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
221 if (flag & RSETF_WRITE)
223 logf (LOG_FATAL, "m_or set type is read-only");
226 rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
228 rfd->next = info->rfd_list;
230 info->rfd_list = rfd;
232 rfd->ispt = (ISAMC_PP *)
233 xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
235 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
237 ct->rset_terms[0]->nn = 0;
238 for (i = 0; i<info->no_isam_positions; i++)
240 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
242 ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
244 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
245 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
248 isc_pp_close (rfd->ispt[i]);
252 rfd->position = info->no_save_positions;
257 static void r_close (RSFD rfd)
259 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
260 struct rset_mor_rfd **rfdp;
263 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
266 *rfdp = (*rfdp)->next;
268 heap_close (((struct rset_mor_rfd *) rfd)->ti);
269 for (i = 0; i<info->no_isam_positions; i++)
270 if (((struct rset_mor_rfd *) rfd)->ispt[i])
271 isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
272 xfree (((struct rset_mor_rfd *)rfd)->ispt);
276 logf (LOG_FATAL, "r_close but no rfd match!");
280 static void r_delete (RSET ct)
282 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
285 assert (info->rfd_list == NULL);
286 xfree (info->isam_positions);
288 for (i = 0; i<ct->no_rset_terms; i++)
289 rset_term_destroy (ct->rset_terms[i]);
290 xfree (ct->rset_terms);
295 static void r_rewind (RSFD rfd)
299 static int r_count (RSET ct)
304 static int r_read (RSFD rfd, void *buf, int *term_index)
306 struct trunc_info *ti = ((struct rset_mor_rfd *) rfd)->ti;
307 int n = ti->indx[ti->ptr[1]];
312 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
313 if (((struct rset_mor_rfd *) rfd)->position)
315 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
318 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
319 ((struct rset_mor_rfd *) rfd)->position--;
320 heap_insert (ti, ti->tmpbuf, n);
328 if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
333 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
336 heap_insert (ti, ti->tmpbuf, n);
343 static int r_write (RSFD rfd, const void *buf)
345 logf (LOG_FATAL, "mor set type is read-only");