2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1997-09-09 13:38:16 adam
8 * Partial port to WIN95/NT.
10 * Revision 1.2 1996/12/23 15:30:49 adam
13 * Revision 1.1 1996/12/20 11:07:21 adam
14 * Implemented Multi-or result set.
28 static void *r_create(const struct rset_control *sel, void *parms,
30 static RSFD r_open (RSET ct, int flag);
31 static void r_close (RSFD rfd);
32 static void r_delete (RSET ct);
33 static void r_rewind (RSFD rfd);
34 static int r_count (RSET ct);
35 static int r_read (RSFD rfd, void *buf);
36 static int r_write (RSFD rfd, const void *buf);
37 static int r_score (RSFD rfd, int *score);
39 static const rset_control control =
53 const rset_control *rset_kind_m_or = &control;
55 struct rset_mor_info {
58 int (*cmp)(const void *p1, const void *p2);
60 ISAM_P *isam_positions;
62 int no_isam_positions;
63 int no_save_positions;
64 struct rset_mor_rfd *rfd_list;
72 int (*cmp)(const void *p1, const void *p2);
84 struct rset_mor_rfd *next;
85 struct rset_mor_info *info;
86 struct trunc_info *ti;
89 static void heap_swap (struct trunc_info *ti, int i1, int i2)
94 ti->ptr[i1] = ti->ptr[i2];
98 static void heap_delete (struct trunc_info *ti)
100 int cur = 1, child = 2;
102 heap_swap (ti, 1, ti->heapnum--);
103 while (child <= ti->heapnum) {
104 if (child < ti->heapnum &&
105 (*ti->cmp)(ti->heap[ti->ptr[child]],
106 ti->heap[ti->ptr[1+child]]) > 0)
108 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
109 ti->heap[ti->ptr[child]]) > 0)
111 heap_swap (ti, cur, child);
120 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
124 cur = ++(ti->heapnum);
125 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
126 ti->indx[ti->ptr[cur]] = indx;
128 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
129 ti->heap[ti->ptr[cur]]) > 0)
131 heap_swap (ti, cur, parent);
138 struct trunc_info *heap_init (int size, int key_size,
139 int (*cmp)(const void *p1, const void *p2))
141 struct trunc_info *ti = xmalloc (sizeof(*ti));
146 ti->keysize = key_size;
148 ti->indx = xmalloc (size * sizeof(*ti->indx));
149 ti->heap = xmalloc (size * sizeof(*ti->heap));
150 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
151 ti->swapbuf = xmalloc (ti->keysize);
152 ti->tmpbuf = xmalloc (ti->keysize);
153 ti->buf = xmalloc (size * ti->keysize);
154 for (i = size; --i >= 0; )
157 ti->heap[i] = ti->buf + ti->keysize * i;
162 static void heap_close (struct trunc_info *ti)
173 static void *r_create (const struct rset_control *sel, void *parms,
176 rset_m_or_parms *r_parms = parms;
177 struct rset_mor_info *info;
179 *flags |= RSET_FLAG_VOLATILE;
180 info = xmalloc (sizeof(*info));
181 info->key_size = r_parms->key_size;
182 assert (info->key_size > 1);
183 info->cmp = r_parms->cmp;
185 info->no_save_positions = r_parms->no_save_positions;
187 info->isc = r_parms->isc;
188 info->no_isam_positions = r_parms->no_isam_positions;
189 info->isam_positions = xmalloc (sizeof(*info->isam_positions) *
190 info->no_isam_positions);
191 memcpy (info->isam_positions, r_parms->isam_positions,
192 sizeof(*info->isam_positions) * info->no_isam_positions);
193 info->rfd_list = NULL;
198 static RSFD r_open (RSET ct, int flag)
200 struct rset_mor_rfd *rfd;
201 struct rset_mor_info *info = ct->buf;
204 if (flag & RSETF_WRITE)
206 logf (LOG_FATAL, "m_or set type is read-only");
209 rfd = xmalloc (sizeof(*rfd));
211 rfd->next = info->rfd_list;
213 info->rfd_list = rfd;
215 rfd->ispt = xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
217 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
219 for (i = 0; i<info->no_isam_positions; i++)
221 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
222 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
223 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
226 isc_pp_close (rfd->ispt[i]);
230 rfd->position = info->no_save_positions;
235 static void r_close (RSFD rfd)
237 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
238 struct rset_mor_rfd **rfdp;
241 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
244 *rfdp = (*rfdp)->next;
246 heap_close (((struct rset_mor_rfd *) rfd)->ti);
247 for (i = 0; i<info->no_isam_positions; i++)
248 if (((struct rset_mor_rfd *) rfd)->ispt[i])
249 isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
250 free (((struct rset_mor_rfd *)rfd)->ispt);
254 logf (LOG_FATAL, "r_close but no rfd match!");
258 static void r_delete (RSET ct)
260 struct rset_mor_info *info = ct->buf;
262 assert (info->rfd_list == NULL);
263 xfree (info->isam_positions);
267 static void r_rewind (RSFD rfd)
271 static int r_count (RSET ct)
276 static int r_read (RSFD rfd, void *buf)
278 struct trunc_info *ti = ((struct rset_mor_rfd *) rfd)->ti;
279 int n = ti->indx[ti->ptr[1]];
283 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
284 if (((struct rset_mor_rfd *) rfd)->position)
286 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
289 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
290 ((struct rset_mor_rfd *) rfd)->position--;
291 heap_insert (ti, ti->tmpbuf, n);
299 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)
307 heap_insert (ti, ti->tmpbuf, n);
314 static int r_score (RSFD rfd, int *score)
320 static int r_write (RSFD rfd, const void *buf)
322 logf (LOG_FATAL, "mor set type is read-only");