2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: rsm_or.c,v 1.12 2002-04-05 08:46:26 adam Exp $
21 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
22 static RSFD r_open (RSET ct, int flag);
23 static void r_close (RSFD rfd);
24 static void r_delete (RSET ct);
25 static void r_rewind (RSFD rfd);
26 static int r_count (RSET ct);
27 static int r_read (RSFD rfd, void *buf, int *term_index);
28 static int r_write (RSFD rfd, const void *buf);
30 static const struct rset_control control =
43 const struct rset_control *rset_kind_m_or = &control;
45 struct rset_mor_info {
48 int (*cmp)(const void *p1, const void *p2);
50 ISAM_P *isam_positions;
52 int no_isam_positions;
53 int no_save_positions;
54 struct rset_mor_rfd *rfd_list;
62 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;
81 static void heap_swap (struct trunc_info *ti, int i1, int i2)
86 ti->ptr[i1] = ti->ptr[i2];
90 static void heap_delete (struct trunc_info *ti)
92 int cur = 1, child = 2;
94 heap_swap (ti, 1, ti->heapnum--);
95 while (child <= ti->heapnum) {
96 if (child < ti->heapnum &&
97 (*ti->cmp)(ti->heap[ti->ptr[child]],
98 ti->heap[ti->ptr[1+child]]) > 0)
100 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
101 ti->heap[ti->ptr[child]]) > 0)
103 heap_swap (ti, cur, child);
112 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
116 cur = ++(ti->heapnum);
117 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
118 ti->indx[ti->ptr[cur]] = indx;
120 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
121 ti->heap[ti->ptr[cur]]) > 0)
123 heap_swap (ti, cur, parent);
130 struct trunc_info *heap_init (int size, int key_size,
131 int (*cmp)(const void *p1, const void *p2))
133 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
138 ti->keysize = key_size;
140 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
141 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
142 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
143 ti->swapbuf = (char *) xmalloc (ti->keysize);
144 ti->tmpbuf = (char *) xmalloc (ti->keysize);
145 ti->buf = (char *) xmalloc (size * ti->keysize);
146 for (i = size; --i >= 0; )
149 ti->heap[i] = ti->buf + ti->keysize * i;
154 static void heap_close (struct trunc_info *ti)
165 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
167 rset_m_or_parms *r_parms = (rset_m_or_parms *) parms;
168 struct rset_mor_info *info;
170 ct->flags |= RSET_FLAG_VOLATILE;
171 info = (struct rset_mor_info *) xmalloc (sizeof(*info));
172 info->key_size = r_parms->key_size;
173 assert (info->key_size > 1);
174 info->cmp = r_parms->cmp;
176 info->no_save_positions = r_parms->no_save_positions;
178 info->isc = r_parms->isc;
179 info->no_isam_positions = r_parms->no_isam_positions;
180 info->isam_positions = (ISAM_P *)
181 xmalloc (sizeof(*info->isam_positions) * info->no_isam_positions);
182 memcpy (info->isam_positions, r_parms->isam_positions,
183 sizeof(*info->isam_positions) * info->no_isam_positions);
184 info->rfd_list = NULL;
186 ct->no_rset_terms = 1;
187 ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
188 ct->rset_terms[0] = r_parms->rset_term;
192 static RSFD r_open (RSET ct, int flag)
194 struct rset_mor_rfd *rfd;
195 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
198 if (flag & RSETF_WRITE)
200 logf (LOG_FATAL, "m_or set type is read-only");
203 rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
205 rfd->next = info->rfd_list;
207 info->rfd_list = rfd;
209 rfd->ispt = (ISAMC_PP *)
210 xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
212 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
214 ct->rset_terms[0]->nn = 0;
215 for (i = 0; i<info->no_isam_positions; i++)
217 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
219 ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
221 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
222 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
225 isc_pp_close (rfd->ispt[i]);
229 rfd->position = info->no_save_positions;
231 if (ct->no_rset_terms == 1)
232 rfd->countp = &ct->rset_terms[0]->count;
235 rfd->pbuf = xmalloc (info->key_size);
241 static void r_close (RSFD rfd)
243 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
244 struct rset_mor_rfd **rfdp;
247 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
250 *rfdp = (*rfdp)->next;
252 heap_close (((struct rset_mor_rfd *) rfd)->ti);
253 for (i = 0; i<info->no_isam_positions; i++)
254 if (((struct rset_mor_rfd *) rfd)->ispt[i])
255 isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
256 xfree (((struct rset_mor_rfd *)rfd)->ispt);
257 xfree (((struct rset_mor_rfd *)rfd)->pbuf);
261 logf (LOG_FATAL, "r_close but no rfd match!");
265 static void r_delete (RSET ct)
267 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
270 assert (info->rfd_list == NULL);
271 xfree (info->isam_positions);
273 for (i = 0; i<ct->no_rset_terms; i++)
274 rset_term_destroy (ct->rset_terms[i]);
275 xfree (ct->rset_terms);
280 static void r_rewind (RSFD rfd)
284 static int r_count (RSET ct)
289 static int r_read (RSFD rfd, void *buf, int *term_index)
291 struct rset_mor_rfd *mrfd = (struct rset_mor_rfd *) rfd;
292 struct trunc_info *ti = mrfd->ti;
293 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);
310 if (mrfd->countp && (
311 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
313 memcpy (mrfd->pbuf, buf, ti->keysize);
320 if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
325 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
328 heap_insert (ti, ti->tmpbuf, n);
332 if (mrfd->countp && (
333 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
335 memcpy (mrfd->pbuf, buf, ti->keysize);
341 static int r_write (RSFD rfd, const void *buf)
343 logf (LOG_FATAL, "mor set type is read-only");