2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1998-01-12 15:04:09 adam
8 * The test option (-s) only uses read-lock (and not write lock).
10 * Revision 1.8 1997/10/31 12:34:27 adam
11 * Bug fix: memory leak.
13 * Revision 1.7 1997/09/29 09:07:29 adam
16 * Revision 1.6 1997/09/22 12:39:06 adam
17 * Added get_pos method for the ranked result sets.
19 * Revision 1.5 1997/09/17 12:19:17 adam
20 * Zebra version corresponds to YAZ version 1.4.
21 * Changed Zebra server so that it doesn't depend on global common_resource.
23 * Revision 1.4 1996/12/23 15:30:44 adam
25 * Bug fix: result sets weren't deleted after server shut down.
27 * Revision 1.3 1996/12/20 11:07:14 adam
28 * Multi-or result set.
30 * Revision 1.2 1996/11/08 11:10:28 adam
31 * Buffers used during file match got bigger.
32 * Compressed ISAM support everywhere.
33 * Bug fixes regarding masking characters in queries.
34 * Redesigned Regexp-2 queries.
36 * Revision 1.1 1996/11/04 14:07:40 adam
37 * Moved truncation code to trunc.c.
60 int (*cmp)(const void *p1, const void *p2);
67 static void heap_swap (struct trunc_info *ti, int i1, int i2)
72 ti->ptr[i1] = ti->ptr[i2];
76 static void heap_delete (struct trunc_info *ti)
78 int cur = 1, child = 2;
80 heap_swap (ti, 1, ti->heapnum--);
81 while (child <= ti->heapnum) {
82 if (child < ti->heapnum &&
83 (*ti->cmp)(ti->heap[ti->ptr[child]],
84 ti->heap[ti->ptr[1+child]]) > 0)
86 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
87 ti->heap[ti->ptr[child]]) > 0)
89 heap_swap (ti, cur, child);
98 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
102 cur = ++(ti->heapnum);
103 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
104 ti->indx[ti->ptr[cur]] = indx;
106 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
107 ti->heap[ti->ptr[cur]]) > 0)
109 heap_swap (ti, cur, parent);
115 static struct trunc_info *heap_init (int size, int key_size,
116 int (*cmp)(const void *p1,
119 struct trunc_info *ti = xmalloc (sizeof(*ti));
124 ti->keysize = key_size;
126 ti->indx = xmalloc (size * sizeof(*ti->indx));
127 ti->heap = xmalloc (size * sizeof(*ti->heap));
128 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
129 ti->swapbuf = xmalloc (ti->keysize);
130 ti->tmpbuf = xmalloc (ti->keysize);
131 ti->buf = xmalloc (size * ti->keysize);
132 for (i = size; --i >= 0; )
135 ti->heap[i] = ti->buf + ti->keysize * i;
140 static void heap_close (struct trunc_info *ti)
151 static RSET rset_trunc_r (ZServerInfo *zi, ISAM_P *isam_p, int from, int to,
156 rset_temp_parms parms;
158 parms.key_size = sizeof(struct it_key);
159 parms.temp_path = res_get (zi->res, "setTmpDir");
160 result = rset_create (rset_kind_temp, &parms);
161 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
163 if (to - from > merge_chunk)
167 int i, i_add = (to-from)/merge_chunk + 1;
168 struct trunc_info *ti;
170 int rsmax = (to-from)/i_add + 1;
172 rset = xmalloc (sizeof(*rset) * rsmax);
173 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
175 for (i = from; i < to; i += i_add)
178 rset[rscur] = rset_trunc_r (zi, isam_p, i, i+i_add,
181 rset[rscur] = rset_trunc_r (zi, isam_p, i, to,
185 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
186 for (i = rscur; --i >= 0; )
188 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
189 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
190 heap_insert (ti, ti->tmpbuf, i);
193 rset_close (rset[i], rsfd[i]);
194 rset_delete (rset[i]);
199 int n = ti->indx[ti->ptr[1]];
201 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
205 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
208 rset_close (rset[n], rsfd[n]);
209 rset_delete (rset[n]);
212 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
215 heap_insert (ti, ti->tmpbuf, n);
228 struct trunc_info *ti;
230 ispt = xmalloc (sizeof(*ispt) * (to-from));
232 ti = heap_init (to-from, sizeof(struct it_key),
234 for (i = to-from; --i >= 0; )
236 ispt[i] = is_position (zi->isam, isam_p[from+i]);
237 if (is_readkey (ispt[i], ti->tmpbuf))
238 heap_insert (ti, ti->tmpbuf, i);
240 is_pt_free (ispt[i]);
244 int n = ti->indx[ti->ptr[1]];
246 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
248 /* section that preserve all keys */
250 if (is_readkey (ispt[n], ti->tmpbuf))
251 heap_insert (ti, ti->tmpbuf, n);
253 is_pt_free (ispt[n]);
255 /* section that preserve all keys with unique sysnos */
258 if (!is_readkey (ispt[n], ti->tmpbuf))
261 is_pt_free (ispt[n]);
264 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
267 heap_insert (ti, ti->tmpbuf, n);
280 struct trunc_info *ti;
282 ispt = xmalloc (sizeof(*ispt) * (to-from));
284 ti = heap_init (to-from, sizeof(struct it_key),
286 for (i = to-from; --i >= 0; )
288 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
289 if (isc_pp_read (ispt[i], ti->tmpbuf))
290 heap_insert (ti, ti->tmpbuf, i);
292 isc_pp_close (ispt[i]);
296 int n = ti->indx[ti->ptr[1]];
298 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
300 /* section that preserve all keys */
302 if (is_readkey (ispt[n], ti->tmpbuf))
303 heap_insert (ti, ti->tmpbuf, n);
305 isc_pp_close (ispt[n]);
307 /* section that preserve all keys with unique sysnos */
310 if (!isc_pp_read (ispt[n], ti->tmpbuf))
313 isc_pp_close (ispt[n]);
316 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
319 heap_insert (ti, ti->tmpbuf, n);
328 rset_close (result, result_rsfd);
332 static int isam_trunc_cmp (const void *p1, const void *p2)
334 ISAM_P i1 = *(ISAM_P*) p1;
335 ISAM_P i2 = *(ISAM_P*) p2;
338 d = is_type (i1) - is_type (i2);
341 return is_block (i1) - is_block (i2);
344 static int isamc_trunc_cmp (const void *p1, const void *p2)
346 ISAMC_P i1 = *(ISAMC_P*) p1;
347 ISAMC_P i2 = *(ISAMC_P*) p2;
350 d = isc_type (i1) - isc_type (i2);
353 return isc_block (i1) - isc_block (i2);
356 RSET rset_trunc (ZServerInfo *zi, ISAM_P *isam_p, int no)
358 logf (LOG_DEBUG, "rset_trunc no=%d", no);
362 return rset_create (rset_kind_null, NULL);
365 rset_isam_parms parms;
369 return rset_create (rset_kind_isam, &parms);
371 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
376 return rset_create (rset_kind_null, NULL);
379 rset_isamc_parms parms;
382 parms.is = zi->isamc;
383 return rset_create (rset_kind_isamc, &parms);
388 rset_m_or_parms parms;
390 logf (LOG_LOG, "new_trunc");
391 parms.key_size = sizeof(struct it_key);
392 parms.cmp = key_compare_it;
393 parms.isc = zi->isamc;
394 parms.isam_positions = isam_p;
395 parms.no_isam_positions = no;
396 parms.no_save_positions = 100;
397 return rset_create (rset_kind_m_or, &parms);
400 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
404 logf (LOG_WARN, "Neither isam nor isamc set in rset_trunc");
405 return rset_create (rset_kind_null, NULL);
407 return rset_trunc_r (zi, isam_p, 0, no, 100);