2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1999-11-30 13:48:04 adam
8 * Improved installation. Updated for inclusion of YAZ header files.
10 * Revision 1.9 1999/07/13 14:45:42 adam
13 * Revision 1.8 1999/05/26 07:49:14 adam
16 * Revision 1.7 1998/09/22 10:03:46 adam
17 * Changed result sets to be persistent in the sense that they can
18 * be re-searched if needed.
19 * Fixed memory leak in rsm_or.
21 * Revision 1.6 1998/03/05 08:36:28 adam
22 * New result set model.
24 * Revision 1.5 1997/12/18 10:54:25 adam
25 * New method result set method rs_hits that returns the number of
26 * hits in result-set (if known). The ranked result set returns real
27 * number of hits but only when not combined with other operands.
29 * Revision 1.4 1997/10/31 12:37:55 adam
30 * Code calls xfree() instead of free().
32 * Revision 1.3 1997/09/09 13:38:16 adam
33 * Partial port to WIN95/NT.
35 * Revision 1.2 1996/12/23 15:30:49 adam
38 * Revision 1.1 1996/12/20 11:07:21 adam
39 * Implemented Multi-or result set.
55 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
56 static RSFD r_open (RSET ct, int flag);
57 static void r_close (RSFD rfd);
58 static void r_delete (RSET ct);
59 static void r_rewind (RSFD rfd);
60 static int r_count (RSET ct);
61 static int r_read (RSFD rfd, void *buf, int *term_index);
62 static int r_write (RSFD rfd, const void *buf);
64 static const struct rset_control control =
77 const struct rset_control *rset_kind_m_or = &control;
79 struct rset_mor_info {
82 int (*cmp)(const void *p1, const void *p2);
84 ISAM_P *isam_positions;
86 int no_isam_positions;
87 int no_save_positions;
88 struct rset_mor_rfd *rfd_list;
96 int (*cmp)(const void *p1, const void *p2);
103 struct rset_mor_rfd {
108 struct rset_mor_rfd *next;
109 struct rset_mor_info *info;
110 struct trunc_info *ti;
113 static void heap_swap (struct trunc_info *ti, int i1, int i2)
118 ti->ptr[i1] = ti->ptr[i2];
122 static void heap_delete (struct trunc_info *ti)
124 int cur = 1, child = 2;
126 heap_swap (ti, 1, ti->heapnum--);
127 while (child <= ti->heapnum) {
128 if (child < ti->heapnum &&
129 (*ti->cmp)(ti->heap[ti->ptr[child]],
130 ti->heap[ti->ptr[1+child]]) > 0)
132 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
133 ti->heap[ti->ptr[child]]) > 0)
135 heap_swap (ti, cur, child);
144 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
148 cur = ++(ti->heapnum);
149 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
150 ti->indx[ti->ptr[cur]] = indx;
152 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
153 ti->heap[ti->ptr[cur]]) > 0)
155 heap_swap (ti, cur, parent);
162 struct trunc_info *heap_init (int size, int key_size,
163 int (*cmp)(const void *p1, const void *p2))
165 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
170 ti->keysize = key_size;
172 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
173 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
174 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
175 ti->swapbuf = (char *) xmalloc (ti->keysize);
176 ti->tmpbuf = (char *) xmalloc (ti->keysize);
177 ti->buf = (char *) xmalloc (size * ti->keysize);
178 for (i = size; --i >= 0; )
181 ti->heap[i] = ti->buf + ti->keysize * i;
186 static void heap_close (struct trunc_info *ti)
197 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
199 rset_m_or_parms *r_parms = (rset_m_or_parms *) parms;
200 struct rset_mor_info *info;
202 ct->flags |= RSET_FLAG_VOLATILE;
203 info = (struct rset_mor_info *) xmalloc (sizeof(*info));
204 info->key_size = r_parms->key_size;
205 assert (info->key_size > 1);
206 info->cmp = r_parms->cmp;
208 info->no_save_positions = r_parms->no_save_positions;
210 info->isc = r_parms->isc;
211 info->no_isam_positions = r_parms->no_isam_positions;
212 info->isam_positions = (ISAM_P *)
213 xmalloc (sizeof(*info->isam_positions) * info->no_isam_positions);
214 memcpy (info->isam_positions, r_parms->isam_positions,
215 sizeof(*info->isam_positions) * info->no_isam_positions);
216 info->rfd_list = NULL;
218 ct->no_rset_terms = 1;
219 ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
220 ct->rset_terms[0] = r_parms->rset_term;
224 static RSFD r_open (RSET ct, int flag)
226 struct rset_mor_rfd *rfd;
227 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
230 if (flag & RSETF_WRITE)
232 logf (LOG_FATAL, "m_or set type is read-only");
235 rfd = (struct rset_mor_rfd *) xmalloc (sizeof(*rfd));
237 rfd->next = info->rfd_list;
239 info->rfd_list = rfd;
241 rfd->ispt = (ISAMC_PP *)
242 xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
244 rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
246 ct->rset_terms[0]->nn = 0;
247 for (i = 0; i<info->no_isam_positions; i++)
249 rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
251 ct->rset_terms[0]->nn += isc_pp_num (rfd->ispt[i]);
253 if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
254 heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
257 isc_pp_close (rfd->ispt[i]);
261 rfd->position = info->no_save_positions;
266 static void r_close (RSFD rfd)
268 struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
269 struct rset_mor_rfd **rfdp;
272 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
275 *rfdp = (*rfdp)->next;
277 heap_close (((struct rset_mor_rfd *) rfd)->ti);
278 for (i = 0; i<info->no_isam_positions; i++)
279 if (((struct rset_mor_rfd *) rfd)->ispt[i])
280 isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
281 xfree (((struct rset_mor_rfd *)rfd)->ispt);
285 logf (LOG_FATAL, "r_close but no rfd match!");
289 static void r_delete (RSET ct)
291 struct rset_mor_info *info = (struct rset_mor_info *) ct->buf;
294 assert (info->rfd_list == NULL);
295 xfree (info->isam_positions);
297 for (i = 0; i<ct->no_rset_terms; i++)
298 rset_term_destroy (ct->rset_terms[i]);
299 xfree (ct->rset_terms);
304 static void r_rewind (RSFD rfd)
308 static int r_count (RSET ct)
313 static int r_read (RSFD rfd, void *buf, int *term_index)
315 struct trunc_info *ti = ((struct rset_mor_rfd *) rfd)->ti;
316 int n = ti->indx[ti->ptr[1]];
321 memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
322 if (((struct rset_mor_rfd *) rfd)->position)
324 if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
327 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
328 ((struct rset_mor_rfd *) rfd)->position--;
329 heap_insert (ti, ti->tmpbuf, n);
337 if (!isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
342 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
345 heap_insert (ti, ti->tmpbuf, n);
352 static int r_write (RSFD rfd, const void *buf)
354 logf (LOG_FATAL, "mor set type is read-only");