2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1997-12-18 10:54:25 adam
8 * New method result set method rs_hits that returns the number of
9 * hits in result-set (if known). The ranked result set returns real
10 * number of hits but only when not combined with other operands.
12 * Revision 1.4 1997/10/31 12:37:55 adam
13 * Code calls xfree() instead of free().
15 * Revision 1.3 1997/09/09 13:38:16 adam
16 * Partial port to WIN95/NT.
18 * Revision 1.2 1996/12/23 15:30:49 adam
21 * Revision 1.1 1996/12/20 11:07:21 adam
22 * Implemented Multi-or result set.
36 static void *r_create(const struct rset_control *sel, void *parms,
38 static RSFD r_open (RSET ct, int flag);
39 static void r_close (RSFD rfd);
40 static void r_delete (RSET ct);
41 static void r_rewind (RSFD rfd);
42 static int r_count (RSET ct);
43 static int r_hits (RSET ct, void *oi);
44 static int r_read (RSFD rfd, void *buf);
45 static int r_write (RSFD rfd, const void *buf);
46 static int r_score (RSFD rfd, int *score);
48 static const rset_control control =
63 const rset_control *rset_kind_m_or = &control;
65 struct rset_mor_info {
68 int (*cmp)(const void *p1, const void *p2);
70 ISAM_P *isam_positions;
72 int no_isam_positions;
73 int no_save_positions;
74 struct rset_mor_rfd *rfd_list;
82 int (*cmp)(const void *p1, const void *p2);
94 struct rset_mor_rfd *next;
95 struct rset_mor_info *info;
96 struct trunc_info *ti;
99 static void heap_swap (struct trunc_info *ti, int i1, int i2)
104 ti->ptr[i1] = ti->ptr[i2];
108 static void heap_delete (struct trunc_info *ti)
110 int cur = 1, child = 2;
112 heap_swap (ti, 1, ti->heapnum--);
113 while (child <= ti->heapnum) {
114 if (child < ti->heapnum &&
115 (*ti->cmp)(ti->heap[ti->ptr[child]],
116 ti->heap[ti->ptr[1+child]]) > 0)
118 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
119 ti->heap[ti->ptr[child]]) > 0)
121 heap_swap (ti, cur, child);
130 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
134 cur = ++(ti->heapnum);
135 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
136 ti->indx[ti->ptr[cur]] = indx;
138 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
139 ti->heap[ti->ptr[cur]]) > 0)
141 heap_swap (ti, cur, parent);
148 struct trunc_info *heap_init (int size, int key_size,
149 int (*cmp)(const void *p1, const void *p2))
151 struct trunc_info *ti = xmalloc (sizeof(*ti));
156 ti->keysize = key_size;
158 ti->indx = xmalloc (size * sizeof(*ti->indx));
159 ti->heap = xmalloc (size * sizeof(*ti->heap));
160 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
161 ti->swapbuf = xmalloc (ti->keysize);
162 ti->tmpbuf = xmalloc (ti->keysize);
163 ti->buf = xmalloc (size * ti->keysize);
164 for (i = size; --i >= 0; )
167 ti->heap[i] = ti->buf + ti->keysize * i;
172 static void heap_close (struct trunc_info *ti)
183 static void *r_create (const struct rset_control *sel, void *parms,
186 rset_m_or_parms *r_parms = parms;
187 struct rset_mor_info *info;
189 *flags |= RSET_FLAG_VOLATILE;
190 info = xmalloc (sizeof(*info));
191 info->key_size = r_parms->key_size;
192 assert (info->key_size > 1);
193 info->cmp = r_parms->cmp;
195 info->no_save_positions = r_parms->no_save_positions;
197 info->isc = r_parms->isc;
198 info->no_isam_positions = r_parms->no_isam_positions;
199 info->isam_positions = xmalloc (sizeof(*info->isam_positions) *
200 info->no_isam_positions);
201 memcpy (info->isam_positions, r_parms->isam_positions,
202 sizeof(*info->isam_positions) * info->no_isam_positions);
203 info->rfd_list = NULL;
208 static RSFD r_open (RSET ct, int flag)
210 struct rset_mor_rfd *rfd;
211 struct rset_mor_info *info = ct->buf;
214 if (flag & RSETF_WRITE)
216 logf (LOG_FATAL, "m_or set type is read-only");
219 rfd = xmalloc (sizeof(*rfd));
221 rfd->next = info->rfd_list;
223 info->rfd_list = rfd;
225 rfd->ispt = xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
227 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
229 for (i = 0; i<info->no_isam_positions; i++)
231 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
232 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
233 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
236 isc_pp_close (rfd->ispt[i]);
240 rfd->position = info->no_save_positions;
245 static void r_close (RSFD rfd)
247 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
248 struct rset_mor_rfd **rfdp;
251 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
254 *rfdp = (*rfdp)->next;
256 heap_close (((struct rset_mor_rfd *) rfd)->ti);
257 for (i = 0; i<info->no_isam_positions; i++)
258 if (((struct rset_mor_rfd *) rfd)->ispt[i])
259 isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
260 xfree (((struct rset_mor_rfd *)rfd)->ispt);
264 logf (LOG_FATAL, "r_close but no rfd match!");
268 static void r_delete (RSET ct)
270 struct rset_mor_info *info = ct->buf;
272 assert (info->rfd_list == NULL);
273 xfree (info->isam_positions);
277 static void r_rewind (RSFD rfd)
281 static int r_count (RSET ct)
286 static int r_hits (RSET ct, void *oi)
291 static int r_read (RSFD rfd, void *buf)
293 struct trunc_info *ti = ((struct rset_mor_rfd *) rfd)->ti;
294 int n = ti->indx[ti->ptr[1]];
298 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
299 if (((struct rset_mor_rfd *) rfd)->position)
301 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
304 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
305 ((struct rset_mor_rfd *) rfd)->position--;
306 heap_insert (ti, ti->tmpbuf, n);
314 if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
319 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
322 heap_insert (ti, ti->tmpbuf, n);
329 static int r_score (RSFD rfd, int *score)
335 static int r_write (RSFD rfd, const void *buf)
337 logf (LOG_FATAL, "mor set type is read-only");