2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1997-10-31 12:37:55 adam
8 * Code calls xfree() instead of free().
10 * Revision 1.3 1997/09/09 13:38:16 adam
11 * Partial port to WIN95/NT.
13 * Revision 1.2 1996/12/23 15:30:49 adam
16 * Revision 1.1 1996/12/20 11:07:21 adam
17 * Implemented Multi-or result set.
31 static void *r_create(const struct rset_control *sel, void *parms,
33 static RSFD r_open (RSET ct, int flag);
34 static void r_close (RSFD rfd);
35 static void r_delete (RSET ct);
36 static void r_rewind (RSFD rfd);
37 static int r_count (RSET ct);
38 static int r_read (RSFD rfd, void *buf);
39 static int r_write (RSFD rfd, const void *buf);
40 static int r_score (RSFD rfd, int *score);
42 static const rset_control control =
56 const rset_control *rset_kind_m_or = &control;
58 struct rset_mor_info {
61 int (*cmp)(const void *p1, const void *p2);
63 ISAM_P *isam_positions;
65 int no_isam_positions;
66 int no_save_positions;
67 struct rset_mor_rfd *rfd_list;
75 int (*cmp)(const void *p1, const void *p2);
87 struct rset_mor_rfd *next;
88 struct rset_mor_info *info;
89 struct trunc_info *ti;
92 static void heap_swap (struct trunc_info *ti, int i1, int i2)
97 ti->ptr[i1] = ti->ptr[i2];
101 static void heap_delete (struct trunc_info *ti)
103 int cur = 1, child = 2;
105 heap_swap (ti, 1, ti->heapnum--);
106 while (child <= ti->heapnum) {
107 if (child < ti->heapnum &&
108 (*ti->cmp)(ti->heap[ti->ptr[child]],
109 ti->heap[ti->ptr[1+child]]) > 0)
111 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
112 ti->heap[ti->ptr[child]]) > 0)
114 heap_swap (ti, cur, child);
123 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
127 cur = ++(ti->heapnum);
128 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
129 ti->indx[ti->ptr[cur]] = indx;
131 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
132 ti->heap[ti->ptr[cur]]) > 0)
134 heap_swap (ti, cur, parent);
141 struct trunc_info *heap_init (int size, int key_size,
142 int (*cmp)(const void *p1, const void *p2))
144 struct trunc_info *ti = xmalloc (sizeof(*ti));
149 ti->keysize = key_size;
151 ti->indx = xmalloc (size * sizeof(*ti->indx));
152 ti->heap = xmalloc (size * sizeof(*ti->heap));
153 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
154 ti->swapbuf = xmalloc (ti->keysize);
155 ti->tmpbuf = xmalloc (ti->keysize);
156 ti->buf = xmalloc (size * ti->keysize);
157 for (i = size; --i >= 0; )
160 ti->heap[i] = ti->buf + ti->keysize * i;
165 static void heap_close (struct trunc_info *ti)
176 static void *r_create (const struct rset_control *sel, void *parms,
179 rset_m_or_parms *r_parms = parms;
180 struct rset_mor_info *info;
182 *flags |= RSET_FLAG_VOLATILE;
183 info = xmalloc (sizeof(*info));
184 info->key_size = r_parms->key_size;
185 assert (info->key_size > 1);
186 info->cmp = r_parms->cmp;
188 info->no_save_positions = r_parms->no_save_positions;
190 info->isc = r_parms->isc;
191 info->no_isam_positions = r_parms->no_isam_positions;
192 info->isam_positions = xmalloc (sizeof(*info->isam_positions) *
193 info->no_isam_positions);
194 memcpy (info->isam_positions, r_parms->isam_positions,
195 sizeof(*info->isam_positions) * info->no_isam_positions);
196 info->rfd_list = NULL;
201 static RSFD r_open (RSET ct, int flag)
203 struct rset_mor_rfd *rfd;
204 struct rset_mor_info *info = ct->buf;
207 if (flag & RSETF_WRITE)
209 logf (LOG_FATAL, "m_or set type is read-only");
212 rfd = xmalloc (sizeof(*rfd));
214 rfd->next = info->rfd_list;
216 info->rfd_list = rfd;
218 rfd->ispt = xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
220 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
222 for (i = 0; i<info->no_isam_positions; i++)
224 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
225 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
226 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
229 isc_pp_close (rfd->ispt[i]);
233 rfd->position = info->no_save_positions;
238 static void r_close (RSFD rfd)
240 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
241 struct rset_mor_rfd **rfdp;
244 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
247 *rfdp = (*rfdp)->next;
249 heap_close (((struct rset_mor_rfd *) rfd)->ti);
250 for (i = 0; i<info->no_isam_positions; i++)
251 if (((struct rset_mor_rfd *) rfd)->ispt[i])
252 isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
253 xfree (((struct rset_mor_rfd *)rfd)->ispt);
257 logf (LOG_FATAL, "r_close but no rfd match!");
261 static void r_delete (RSET ct)
263 struct rset_mor_info *info = ct->buf;
265 assert (info->rfd_list == NULL);
266 xfree (info->isam_positions);
270 static void r_rewind (RSFD rfd)
274 static int r_count (RSET ct)
279 static int r_read (RSFD rfd, void *buf)
281 struct trunc_info *ti = ((struct rset_mor_rfd *) rfd)->ti;
282 int n = ti->indx[ti->ptr[1]];
286 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
287 if (((struct rset_mor_rfd *) rfd)->position)
289 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
292 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
293 ((struct rset_mor_rfd *) rfd)->position--;
294 heap_insert (ti, ti->tmpbuf, n);
302 if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
307 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
310 heap_insert (ti, ti->tmpbuf, n);
317 static int r_score (RSFD rfd, int *score)
323 static int r_write (RSFD rfd, const void *buf)
325 logf (LOG_FATAL, "mor set type is read-only");