2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1996-12-20 11:07:21 adam
8 * Implemented Multi-or result set.
21 static void *r_create(const struct rset_control *sel, void *parms,
23 static RSFD r_open (RSET ct, int flag);
24 static void r_close (RSFD rfd);
25 static void r_delete (RSET ct);
26 static void r_rewind (RSFD rfd);
27 static int r_count (RSET ct);
28 static int r_read (RSFD rfd, void *buf);
29 static int r_write (RSFD rfd, const void *buf);
30 static int r_score (RSFD rfd, int *score);
32 static const rset_control control =
46 const rset_control *rset_kind_m_or = &control;
48 struct rset_mor_info {
51 int (*cmp)(const void *p1, const void *p2);
53 ISAM_P *isam_positions;
55 int no_isam_positions;
56 struct rset_mor_rfd *rfd_list;
64 int (*cmp)(const void *p1, const void *p2);
74 struct rset_mor_rfd *next;
75 struct rset_mor_info *info;
76 struct trunc_info *ti;
79 static void heap_swap (struct trunc_info *ti, int i1, int i2)
84 ti->ptr[i1] = ti->ptr[i2];
88 static void heap_delete (struct trunc_info *ti)
90 int cur = 1, child = 2;
92 heap_swap (ti, 1, ti->heapnum--);
93 while (child <= ti->heapnum) {
94 if (child < ti->heapnum &&
95 (*ti->cmp)(ti->heap[ti->ptr[child]],
96 ti->heap[ti->ptr[1+child]]) > 0)
98 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
99 ti->heap[ti->ptr[child]]) > 0)
101 heap_swap (ti, cur, child);
110 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
114 cur = ++(ti->heapnum);
115 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
116 ti->indx[ti->ptr[cur]] = indx;
118 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
119 ti->heap[ti->ptr[cur]]) > 0)
121 heap_swap (ti, cur, parent);
128 struct trunc_info *heap_init (int size, int key_size,
129 int (*cmp)(const void *p1, const void *p2))
131 struct trunc_info *ti = xmalloc (sizeof(*ti));
136 ti->keysize = key_size;
138 ti->indx = xmalloc (size * sizeof(*ti->indx));
139 ti->heap = xmalloc (size * sizeof(*ti->heap));
140 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
141 ti->swapbuf = xmalloc (ti->keysize);
142 ti->tmpbuf = xmalloc (ti->keysize);
143 ti->buf = xmalloc (size * ti->keysize);
144 for (i = size; --i >= 0; )
147 ti->heap[i] = ti->buf + ti->keysize * i;
152 static void heap_close (struct trunc_info *ti)
163 static void *r_create (const struct rset_control *sel, void *parms,
166 rset_m_or_parms *r_parms = parms;
167 struct rset_mor_info *info;
169 *flags |= RSET_FLAG_VOLATILE;
170 info = xmalloc (sizeof(*info));
171 info->key_size = r_parms->key_size;
172 assert (info->key_size > 1);
173 info->cmp = r_parms->cmp;
175 info->isc = r_parms->isc;
176 info->no_isam_positions = r_parms->no_isam_positions;
177 info->isam_positions = xmalloc (sizeof(*info->isam_positions) *
178 info->no_isam_positions);
179 memcpy (info->isam_positions, r_parms->isam_positions,
180 sizeof(*info->isam_positions) * info->no_isam_positions);
181 info->rfd_list = NULL;
186 static RSFD r_open (RSET ct, int flag)
188 struct rset_mor_rfd *rfd;
189 struct rset_mor_info *info = ct->buf;
192 if (flag & RSETF_WRITE)
194 logf (LOG_FATAL, "m_or set type is read-only");
197 rfd = xmalloc (sizeof(*rfd));
199 rfd->next = info->rfd_list;
201 info->rfd_list = rfd;
203 rfd->ispt = xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
205 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
207 for (i = 0; i<info->no_isam_positions; i++)
209 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
210 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
211 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
214 isc_pp_close (rfd->ispt[i]);
222 static void r_close (RSFD rfd)
224 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
225 struct rset_mor_rfd **rfdp;
228 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
231 *rfdp = (*rfdp)->next;
233 heap_close (((struct rset_mor_rfd *) rfd)->ti);
234 for (i = 0; i<info->no_isam_positions; i++)
235 if (((struct rset_mor_rfd *) rfd)->ispt[i])
236 isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
237 free (((struct rset_mor_rfd *)rfd)->ispt);
241 logf (LOG_FATAL, "r_close but no rfd match!");
245 static void r_delete (RSET ct)
247 struct rset_mor_info *info = ct->buf;
249 assert (info->rfd_list == NULL);
250 xfree (info->isam_positions);
254 static void r_rewind (RSFD rfd)
258 static int r_count (RSET ct)
263 static int r_read (RSFD rfd, void *buf)
265 struct trunc_info *ti = ((struct rset_mor_rfd *) rfd)->ti;
266 int n = ti->indx[ti->ptr[1]];
270 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
272 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
273 heap_insert (ti, ti->tmpbuf, n);
276 isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[n]);
277 ((struct rset_mor_rfd*) rfd)->ispt[n] = NULL;
282 static int r_score (RSFD rfd, int *score)
288 static int r_write (RSFD rfd, const void *buf)
290 logf (LOG_FATAL, "mor set type is read-only");