2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: rsm_or.c,v 1.11 2002-03-20 20:24:30 adam Exp $
22 static void *r_create(RSET ct, 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, int *term_index);
29 static int r_write (RSFD rfd, const void *buf);
31 static const struct rset_control control =
44 const struct rset_control *rset_kind_m_or = &control;
46 struct rset_mor_info {
49 int (*cmp)(const void *p1, const void *p2);
51 ISAM_P *isam_positions;
53 int no_isam_positions;
54 int no_save_positions;
55 struct rset_mor_rfd *rfd_list;
63 int (*cmp)(const void *p1, const void *p2);
75 struct rset_mor_rfd *next;
76 struct rset_mor_info *info;
77 struct trunc_info *ti;
82 static void heap_swap (struct trunc_info *ti, int i1, int i2)
87 ti->ptr[i1] = ti->ptr[i2];
91 static void heap_delete (struct trunc_info *ti)
93 int cur = 1, child = 2;
95 heap_swap (ti, 1, ti->heapnum--);
96 while (child <= ti->heapnum) {
97 if (child < ti->heapnum &&
98 (*ti->cmp)(ti->heap[ti->ptr[child]],
99 ti->heap[ti->ptr[1+child]]) > 0)
101 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
102 ti->heap[ti->ptr[child]]) > 0)
104 heap_swap (ti, cur, child);
113 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
117 cur = ++(ti->heapnum);
118 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
119 ti->indx[ti->ptr[cur]] = indx;
121 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
122 ti->heap[ti->ptr[cur]]) > 0)
124 heap_swap (ti, cur, parent);
131 struct trunc_info *heap_init (int size, int key_size,
132 int (*cmp)(const void *p1, const void *p2))
134 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
139 ti->keysize = key_size;
141 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
142 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
143 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
144 ti->swapbuf = (char *) xmalloc (ti->keysize);
145 ti->tmpbuf = (char *) xmalloc (ti->keysize);
146 ti->buf = (char *) xmalloc (size * ti->keysize);
147 for (i = size; --i >= 0; )
150 ti->heap[i] = ti->buf + ti->keysize * i;
155 static void heap_close (struct trunc_info *ti)
166 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
168 rset_m_or_parms *r_parms = (rset_m_or_parms *) parms;
169 struct rset_mor_info *info;
171 ct->flags |= RSET_FLAG_VOLATILE;
172 info = (struct rset_mor_info *) xmalloc (sizeof(*info));
173 info->key_size = r_parms->key_size;
174 assert (info->key_size > 1);
175 info->cmp = r_parms->cmp;
177 info->no_save_positions = r_parms->no_save_positions;
179 info->isc = r_parms->isc;
180 info->no_isam_positions = r_parms->no_isam_positions;
181 info->isam_positions = (ISAM_P *)
182 xmalloc (sizeof(*info->isam_positions) * info->no_isam_positions);
183 memcpy (info->isam_positions, r_parms->isam_positions,
184 sizeof(*info->isam_positions) * info->no_isam_positions);
185 info->rfd_list = NULL;
187 ct->no_rset_terms = 1;
188 ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
189 ct->rset_terms[0] = r_parms->rset_term;
193 static RSFD r_open (RSET ct, int flag)
195 struct rset_mor_rfd *rfd;
196 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
199 if (flag & RSETF_WRITE)
201 logf (LOG_FATAL, "m_or set type is read-only");
204 rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
206 rfd->next = info->rfd_list;
208 info->rfd_list = rfd;
210 rfd->ispt = (ISAMC_PP *)
211 xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
213 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
215 ct->rset_terms[0]->nn = 0;
216 for (i = 0; i<info->no_isam_positions; i++)
218 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
220 ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[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;
232 if (ct->no_rset_terms == 1)
233 rfd->countp = &ct->rset_terms[0]->count;
236 rfd->pbuf = xmalloc (info->key_size);
242 static void r_close (RSFD rfd)
244 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
245 struct rset_mor_rfd **rfdp;
248 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
251 *rfdp = (*rfdp)->next;
253 heap_close (((struct rset_mor_rfd *) rfd)->ti);
254 for (i = 0; i<info->no_isam_positions; i++)
255 if (((struct rset_mor_rfd *) rfd)->ispt[i])
256 isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
257 xfree (((struct rset_mor_rfd *)rfd)->ispt);
258 xfree (((struct rset_mor_rfd *)rfd)->pbuf);
262 logf (LOG_FATAL, "r_close but no rfd match!");
266 static void r_delete (RSET ct)
268 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
271 assert (info->rfd_list == NULL);
272 xfree (info->isam_positions);
274 for (i = 0; i<ct->no_rset_terms; i++)
275 rset_term_destroy (ct->rset_terms[i]);
276 xfree (ct->rset_terms);
281 static void r_rewind (RSFD rfd)
285 static int r_count (RSET ct)
290 static int r_read (RSFD rfd, void *buf, int *term_index)
292 struct rset_mor_rfd *mrfd = (struct rset_mor_rfd *) rfd;
293 struct trunc_info *ti = mrfd->ti;
294 int n = ti->indx[ti->ptr[1]];
299 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
300 if (((struct rset_mor_rfd *) rfd)->position)
302 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
305 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
306 ((struct rset_mor_rfd *) rfd)->position--;
307 heap_insert (ti, ti->tmpbuf, n);
311 if (mrfd->countp && (
312 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
314 memcpy (mrfd->pbuf, buf, ti->keysize);
321 if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
326 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
329 heap_insert (ti, ti->tmpbuf, n);
333 if (mrfd->countp && (
334 *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1))
336 memcpy (mrfd->pbuf, buf, ti->keysize);
342 static int r_write (RSFD rfd, const void *buf)
344 logf (LOG_FATAL, "mor set type is read-only");