2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1997-09-29 09:07:29 adam
10 * Revision 1.6 1997/09/22 12:39:06 adam
11 * Added get_pos method for the ranked result sets.
13 * Revision 1.5 1997/09/17 12:19:17 adam
14 * Zebra version corresponds to YAZ version 1.4.
15 * Changed Zebra server so that it doesn't depend on global common_resource.
17 * Revision 1.4 1996/12/23 15:30:44 adam
19 * Bug fix: result sets weren't deleted after server shut down.
21 * Revision 1.3 1996/12/20 11:07:14 adam
22 * Multi-or result set.
24 * Revision 1.2 1996/11/08 11:10:28 adam
25 * Buffers used during file match got bigger.
26 * Compressed ISAM support everywhere.
27 * Bug fixes regarding masking characters in queries.
28 * Redesigned Regexp-2 queries.
30 * Revision 1.1 1996/11/04 14:07:40 adam
31 * Moved truncation code to trunc.c.
54 int (*cmp)(const void *p1, const void *p2);
61 static void heap_swap (struct trunc_info *ti, int i1, int i2)
66 ti->ptr[i1] = ti->ptr[i2];
70 static void heap_delete (struct trunc_info *ti)
72 int cur = 1, child = 2;
74 heap_swap (ti, 1, ti->heapnum--);
75 while (child <= ti->heapnum) {
76 if (child < ti->heapnum &&
77 (*ti->cmp)(ti->heap[ti->ptr[child]],
78 ti->heap[ti->ptr[1+child]]) > 0)
80 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
81 ti->heap[ti->ptr[child]]) > 0)
83 heap_swap (ti, cur, child);
92 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
96 cur = ++(ti->heapnum);
97 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
98 ti->indx[ti->ptr[cur]] = indx;
100 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
101 ti->heap[ti->ptr[cur]]) > 0)
103 heap_swap (ti, cur, parent);
109 static struct trunc_info *heap_init (int size, int key_size,
110 int (*cmp)(const void *p1,
113 struct trunc_info *ti = xmalloc (sizeof(*ti));
118 ti->keysize = key_size;
120 ti->indx = xmalloc (size * sizeof(*ti->indx));
121 ti->heap = xmalloc (size * sizeof(*ti->heap));
122 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
123 ti->swapbuf = xmalloc (ti->keysize);
124 ti->tmpbuf = xmalloc (ti->keysize);
125 ti->buf = xmalloc (size * ti->keysize);
126 for (i = size; --i >= 0; )
129 ti->heap[i] = ti->buf + ti->keysize * i;
134 static void heap_close (struct trunc_info *ti)
144 static RSET rset_trunc_r (ZServerInfo *zi, ISAM_P *isam_p, int from, int to,
149 rset_temp_parms parms;
151 parms.key_size = sizeof(struct it_key);
152 parms.temp_path = res_get (zi->res, "setTmpDir");
153 result = rset_create (rset_kind_temp, &parms);
154 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
156 if (to - from > merge_chunk)
160 int i, i_add = (to-from)/merge_chunk + 1;
161 struct trunc_info *ti;
163 int rsmax = (to-from)/i_add + 1;
165 rset = xmalloc (sizeof(*rset) * rsmax);
166 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
168 for (i = from; i < to; i += i_add)
171 rset[rscur] = rset_trunc_r (zi, isam_p, i, i+i_add,
174 rset[rscur] = rset_trunc_r (zi, isam_p, i, to,
178 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
179 for (i = rscur; --i >= 0; )
181 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
182 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
183 heap_insert (ti, ti->tmpbuf, i);
186 rset_close (rset[i], rsfd[i]);
187 rset_delete (rset[i]);
192 int n = ti->indx[ti->ptr[1]];
194 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
198 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
201 rset_close (rset[n], rsfd[n]);
202 rset_delete (rset[n]);
205 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
208 heap_insert (ti, ti->tmpbuf, n);
221 struct trunc_info *ti;
223 ispt = xmalloc (sizeof(*ispt) * (to-from));
225 ti = heap_init (to-from, sizeof(struct it_key),
227 for (i = to-from; --i >= 0; )
229 ispt[i] = is_position (zi->isam, isam_p[from+i]);
230 if (is_readkey (ispt[i], ti->tmpbuf))
231 heap_insert (ti, ti->tmpbuf, i);
233 is_pt_free (ispt[i]);
237 int n = ti->indx[ti->ptr[1]];
239 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
241 /* section that preserve all keys */
243 if (is_readkey (ispt[n], ti->tmpbuf))
244 heap_insert (ti, ti->tmpbuf, n);
246 is_pt_free (ispt[n]);
248 /* section that preserve all keys with unique sysnos */
251 if (!is_readkey (ispt[n], ti->tmpbuf))
254 is_pt_free (ispt[n]);
257 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
260 heap_insert (ti, ti->tmpbuf, n);
273 struct trunc_info *ti;
275 ispt = xmalloc (sizeof(*ispt) * (to-from));
277 ti = heap_init (to-from, sizeof(struct it_key),
279 for (i = to-from; --i >= 0; )
281 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
282 if (isc_pp_read (ispt[i], ti->tmpbuf))
283 heap_insert (ti, ti->tmpbuf, i);
285 isc_pp_close (ispt[i]);
289 int n = ti->indx[ti->ptr[1]];
291 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
293 /* section that preserve all keys */
295 if (is_readkey (ispt[n], ti->tmpbuf))
296 heap_insert (ti, ti->tmpbuf, n);
298 isc_pp_close (ispt[n]);
300 /* section that preserve all keys with unique sysnos */
303 if (!isc_pp_read (ispt[n], ti->tmpbuf))
306 isc_pp_close (ispt[n]);
309 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
312 heap_insert (ti, ti->tmpbuf, n);
321 rset_close (result, result_rsfd);
325 static int isam_trunc_cmp (const void *p1, const void *p2)
327 ISAM_P i1 = *(ISAM_P*) p1;
328 ISAM_P i2 = *(ISAM_P*) p2;
331 d = is_type (i1) - is_type (i2);
334 return is_block (i1) - is_block (i2);
337 static int isamc_trunc_cmp (const void *p1, const void *p2)
339 ISAMC_P i1 = *(ISAMC_P*) p1;
340 ISAMC_P i2 = *(ISAMC_P*) p2;
343 d = isc_type (i1) - isc_type (i2);
346 return isc_block (i1) - isc_block (i2);
349 RSET rset_trunc (ZServerInfo *zi, ISAM_P *isam_p, int no)
351 logf (LOG_DEBUG, "rset_trunc no=%d", no);
355 return rset_create (rset_kind_null, NULL);
358 rset_isam_parms parms;
362 return rset_create (rset_kind_isam, &parms);
364 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
369 return rset_create (rset_kind_null, NULL);
372 rset_isamc_parms parms;
375 parms.is = zi->isamc;
376 return rset_create (rset_kind_isamc, &parms);
381 rset_m_or_parms parms;
383 logf (LOG_LOG, "new_trunc");
384 parms.key_size = sizeof(struct it_key);
385 parms.cmp = key_compare_it;
386 parms.isc = zi->isamc;
387 parms.isam_positions = isam_p;
388 parms.no_isam_positions = no;
389 parms.no_save_positions = 100;
390 return rset_create (rset_kind_m_or, &parms);
393 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
396 logf (LOG_FATAL, "Neither isam nor isamc set in rset_trunc");
397 return rset_trunc_r (zi, isam_p, 0, no, 100);