2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1997-10-31 12:34:27 adam
8 * Bug fix: memory leak.
10 * Revision 1.7 1997/09/29 09:07:29 adam
13 * Revision 1.6 1997/09/22 12:39:06 adam
14 * Added get_pos method for the ranked result sets.
16 * Revision 1.5 1997/09/17 12:19:17 adam
17 * Zebra version corresponds to YAZ version 1.4.
18 * Changed Zebra server so that it doesn't depend on global common_resource.
20 * Revision 1.4 1996/12/23 15:30:44 adam
22 * Bug fix: result sets weren't deleted after server shut down.
24 * Revision 1.3 1996/12/20 11:07:14 adam
25 * Multi-or result set.
27 * Revision 1.2 1996/11/08 11:10:28 adam
28 * Buffers used during file match got bigger.
29 * Compressed ISAM support everywhere.
30 * Bug fixes regarding masking characters in queries.
31 * Redesigned Regexp-2 queries.
33 * Revision 1.1 1996/11/04 14:07:40 adam
34 * Moved truncation code to trunc.c.
57 int (*cmp)(const void *p1, const void *p2);
64 static void heap_swap (struct trunc_info *ti, int i1, int i2)
69 ti->ptr[i1] = ti->ptr[i2];
73 static void heap_delete (struct trunc_info *ti)
75 int cur = 1, child = 2;
77 heap_swap (ti, 1, ti->heapnum--);
78 while (child <= ti->heapnum) {
79 if (child < ti->heapnum &&
80 (*ti->cmp)(ti->heap[ti->ptr[child]],
81 ti->heap[ti->ptr[1+child]]) > 0)
83 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
84 ti->heap[ti->ptr[child]]) > 0)
86 heap_swap (ti, cur, child);
95 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
99 cur = ++(ti->heapnum);
100 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
101 ti->indx[ti->ptr[cur]] = indx;
103 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
104 ti->heap[ti->ptr[cur]]) > 0)
106 heap_swap (ti, cur, parent);
112 static struct trunc_info *heap_init (int size, int key_size,
113 int (*cmp)(const void *p1,
116 struct trunc_info *ti = xmalloc (sizeof(*ti));
121 ti->keysize = key_size;
123 ti->indx = xmalloc (size * sizeof(*ti->indx));
124 ti->heap = xmalloc (size * sizeof(*ti->heap));
125 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
126 ti->swapbuf = xmalloc (ti->keysize);
127 ti->tmpbuf = xmalloc (ti->keysize);
128 ti->buf = xmalloc (size * ti->keysize);
129 for (i = size; --i >= 0; )
132 ti->heap[i] = ti->buf + ti->keysize * i;
137 static void heap_close (struct trunc_info *ti)
148 static RSET rset_trunc_r (ZServerInfo *zi, ISAM_P *isam_p, int from, int to,
153 rset_temp_parms parms;
155 parms.key_size = sizeof(struct it_key);
156 parms.temp_path = res_get (zi->res, "setTmpDir");
157 result = rset_create (rset_kind_temp, &parms);
158 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
160 if (to - from > merge_chunk)
164 int i, i_add = (to-from)/merge_chunk + 1;
165 struct trunc_info *ti;
167 int rsmax = (to-from)/i_add + 1;
169 rset = xmalloc (sizeof(*rset) * rsmax);
170 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
172 for (i = from; i < to; i += i_add)
175 rset[rscur] = rset_trunc_r (zi, isam_p, i, i+i_add,
178 rset[rscur] = rset_trunc_r (zi, isam_p, i, to,
182 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
183 for (i = rscur; --i >= 0; )
185 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
186 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
187 heap_insert (ti, ti->tmpbuf, i);
190 rset_close (rset[i], rsfd[i]);
191 rset_delete (rset[i]);
196 int n = ti->indx[ti->ptr[1]];
198 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
202 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
205 rset_close (rset[n], rsfd[n]);
206 rset_delete (rset[n]);
209 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
212 heap_insert (ti, ti->tmpbuf, n);
225 struct trunc_info *ti;
227 ispt = xmalloc (sizeof(*ispt) * (to-from));
229 ti = heap_init (to-from, sizeof(struct it_key),
231 for (i = to-from; --i >= 0; )
233 ispt[i] = is_position (zi->isam, isam_p[from+i]);
234 if (is_readkey (ispt[i], ti->tmpbuf))
235 heap_insert (ti, ti->tmpbuf, i);
237 is_pt_free (ispt[i]);
241 int n = ti->indx[ti->ptr[1]];
243 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
245 /* section that preserve all keys */
247 if (is_readkey (ispt[n], ti->tmpbuf))
248 heap_insert (ti, ti->tmpbuf, n);
250 is_pt_free (ispt[n]);
252 /* section that preserve all keys with unique sysnos */
255 if (!is_readkey (ispt[n], ti->tmpbuf))
258 is_pt_free (ispt[n]);
261 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
264 heap_insert (ti, ti->tmpbuf, n);
277 struct trunc_info *ti;
279 ispt = xmalloc (sizeof(*ispt) * (to-from));
281 ti = heap_init (to-from, sizeof(struct it_key),
283 for (i = to-from; --i >= 0; )
285 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
286 if (isc_pp_read (ispt[i], ti->tmpbuf))
287 heap_insert (ti, ti->tmpbuf, i);
289 isc_pp_close (ispt[i]);
293 int n = ti->indx[ti->ptr[1]];
295 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
297 /* section that preserve all keys */
299 if (is_readkey (ispt[n], ti->tmpbuf))
300 heap_insert (ti, ti->tmpbuf, n);
302 isc_pp_close (ispt[n]);
304 /* section that preserve all keys with unique sysnos */
307 if (!isc_pp_read (ispt[n], ti->tmpbuf))
310 isc_pp_close (ispt[n]);
313 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
316 heap_insert (ti, ti->tmpbuf, n);
325 rset_close (result, result_rsfd);
329 static int isam_trunc_cmp (const void *p1, const void *p2)
331 ISAM_P i1 = *(ISAM_P*) p1;
332 ISAM_P i2 = *(ISAM_P*) p2;
335 d = is_type (i1) - is_type (i2);
338 return is_block (i1) - is_block (i2);
341 static int isamc_trunc_cmp (const void *p1, const void *p2)
343 ISAMC_P i1 = *(ISAMC_P*) p1;
344 ISAMC_P i2 = *(ISAMC_P*) p2;
347 d = isc_type (i1) - isc_type (i2);
350 return isc_block (i1) - isc_block (i2);
353 RSET rset_trunc (ZServerInfo *zi, ISAM_P *isam_p, int no)
355 logf (LOG_DEBUG, "rset_trunc no=%d", no);
359 return rset_create (rset_kind_null, NULL);
362 rset_isam_parms parms;
366 return rset_create (rset_kind_isam, &parms);
368 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
373 return rset_create (rset_kind_null, NULL);
376 rset_isamc_parms parms;
379 parms.is = zi->isamc;
380 return rset_create (rset_kind_isamc, &parms);
385 rset_m_or_parms parms;
387 logf (LOG_LOG, "new_trunc");
388 parms.key_size = sizeof(struct it_key);
389 parms.cmp = key_compare_it;
390 parms.isc = zi->isamc;
391 parms.isam_positions = isam_p;
392 parms.no_isam_positions = no;
393 parms.no_save_positions = 100;
394 return rset_create (rset_kind_m_or, &parms);
397 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
400 logf (LOG_FATAL, "Neither isam nor isamc set in rset_trunc");
401 return rset_trunc_r (zi, isam_p, 0, no, 100);