2 * Copyright (C) 1994-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1998-09-22 10:03:46 adam
8 * Changed result sets to be persistent in the sense that they can
9 * be re-searched if needed.
10 * Fixed memory leak in rsm_or.
12 * Revision 1.6 1998/03/05 08:36:28 adam
13 * New result set model.
15 * Revision 1.5 1997/12/18 10:54:25 adam
16 * New method result set method rs_hits that returns the number of
17 * hits in result-set (if known). The ranked result set returns real
18 * number of hits but only when not combined with other operands.
20 * Revision 1.4 1997/10/31 12:37:55 adam
21 * Code calls xfree() instead of free().
23 * Revision 1.3 1997/09/09 13:38:16 adam
24 * Partial port to WIN95/NT.
26 * Revision 1.2 1996/12/23 15:30:49 adam
29 * Revision 1.1 1996/12/20 11:07:21 adam
30 * Implemented Multi-or result set.
44 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
45 static RSFD r_open (RSET ct, int flag);
46 static void r_close (RSFD rfd);
47 static void r_delete (RSET ct);
48 static void r_rewind (RSFD rfd);
49 static int r_count (RSET ct);
50 static int r_read (RSFD rfd, void *buf, int *term_index);
51 static int r_write (RSFD rfd, const void *buf);
53 static const struct rset_control control =
66 const struct rset_control *rset_kind_m_or = &control;
68 struct rset_mor_info {
71 int (*cmp)(const void *p1, const void *p2);
73 ISAM_P *isam_positions;
75 int no_isam_positions;
76 int no_save_positions;
77 struct rset_mor_rfd *rfd_list;
85 int (*cmp)(const void *p1, const void *p2);
97 struct rset_mor_rfd *next;
98 struct rset_mor_info *info;
99 struct trunc_info *ti;
102 static void heap_swap (struct trunc_info *ti, int i1, int i2)
107 ti->ptr[i1] = ti->ptr[i2];
111 static void heap_delete (struct trunc_info *ti)
113 int cur = 1, child = 2;
115 heap_swap (ti, 1, ti->heapnum--);
116 while (child <= ti->heapnum) {
117 if (child < ti->heapnum &&
118 (*ti->cmp)(ti->heap[ti->ptr[child]],
119 ti->heap[ti->ptr[1+child]]) > 0)
121 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
122 ti->heap[ti->ptr[child]]) > 0)
124 heap_swap (ti, cur, child);
133 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
137 cur = ++(ti->heapnum);
138 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
139 ti->indx[ti->ptr[cur]] = indx;
141 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
142 ti->heap[ti->ptr[cur]]) > 0)
144 heap_swap (ti, cur, parent);
151 struct trunc_info *heap_init (int size, int key_size,
152 int (*cmp)(const void *p1, const void *p2))
154 struct trunc_info *ti = xmalloc (sizeof(*ti));
159 ti->keysize = key_size;
161 ti->indx = xmalloc (size * sizeof(*ti->indx));
162 ti->heap = xmalloc (size * sizeof(*ti->heap));
163 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
164 ti->swapbuf = xmalloc (ti->keysize);
165 ti->tmpbuf = xmalloc (ti->keysize);
166 ti->buf = xmalloc (size * ti->keysize);
167 for (i = size; --i >= 0; )
170 ti->heap[i] = ti->buf + ti->keysize * i;
175 static void heap_close (struct trunc_info *ti)
185 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
187 rset_m_or_parms *r_parms = parms;
188 struct rset_mor_info *info;
190 ct->flags |= RSET_FLAG_VOLATILE;
191 info = xmalloc (sizeof(*info));
192 info->key_size = r_parms->key_size;
193 assert (info->key_size > 1);
194 info->cmp = r_parms->cmp;
196 info->no_save_positions = r_parms->no_save_positions;
198 info->isc = r_parms->isc;
199 info->no_isam_positions = r_parms->no_isam_positions;
200 info->isam_positions = xmalloc (sizeof(*info->isam_positions) *
201 info->no_isam_positions);
202 memcpy (info->isam_positions, r_parms->isam_positions,
203 sizeof(*info->isam_positions) * info->no_isam_positions);
204 info->rfd_list = NULL;
206 ct->no_rset_terms = 1;
207 ct->rset_terms = xmalloc (sizeof(*ct->rset_terms));
208 ct->rset_terms[0] = r_parms->rset_term;
212 static RSFD r_open (RSET ct, int flag)
214 struct rset_mor_rfd *rfd;
215 struct rset_mor_info *info = ct->buf;
218 if (flag & RSETF_WRITE)
220 logf (LOG_FATAL, "m_or set type is read-only");
223 rfd = xmalloc (sizeof(*rfd));
225 rfd->next = info->rfd_list;
227 info->rfd_list = rfd;
229 rfd->ispt = xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
231 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
233 ct->rset_terms[0]->nn = 0;
234 for (i = 0; i<info->no_isam_positions; i++)
236 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
238 ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
240 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
241 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
244 isc_pp_close (rfd->ispt[i]);
248 rfd->position = info->no_save_positions;
253 static void r_close (RSFD rfd)
255 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
256 struct rset_mor_rfd **rfdp;
259 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
262 *rfdp = (*rfdp)->next;
264 heap_close (((struct rset_mor_rfd *) rfd)->ti);
265 for (i = 0; i<info->no_isam_positions; i++)
266 if (((struct rset_mor_rfd *) rfd)->ispt[i])
267 isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
268 xfree (((struct rset_mor_rfd *)rfd)->ispt);
272 logf (LOG_FATAL, "r_close but no rfd match!");
276 static void r_delete (RSET ct)
278 struct rset_mor_info *info = ct->buf;
281 assert (info->rfd_list == NULL);
282 xfree (info->isam_positions);
284 for (i = 0; i<ct->no_rset_terms; i++)
285 rset_term_destroy (ct->rset_terms[i]);
286 xfree (ct->rset_terms);
291 static void r_rewind (RSFD rfd)
295 static int r_count (RSET ct)
300 static int r_read (RSFD rfd, void *buf, int *term_index)
302 struct trunc_info *ti = ((struct rset_mor_rfd *) rfd)->ti;
303 int n = ti->indx[ti->ptr[1]];
308 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
309 if (((struct rset_mor_rfd *) rfd)->position)
311 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
314 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
315 ((struct rset_mor_rfd *) rfd)->position--;
316 heap_insert (ti, ti->tmpbuf, n);
324 if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
329 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
332 heap_insert (ti, ti->tmpbuf, n);
339 static int r_write (RSFD rfd, const void *buf)
341 logf (LOG_FATAL, "mor set type is read-only");