2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1997-09-22 12:39:06 adam
8 * Added get_pos method for the ranked result sets.
10 * Revision 1.5 1997/09/17 12:19:17 adam
11 * Zebra version corresponds to YAZ version 1.4.
12 * Changed Zebra server so that it doesn't depend on global common_resource.
14 * Revision 1.4 1996/12/23 15:30:44 adam
16 * Bug fix: result sets weren't deleted after server shut down.
18 * Revision 1.3 1996/12/20 11:07:14 adam
19 * Multi-or result set.
21 * Revision 1.2 1996/11/08 11:10:28 adam
22 * Buffers used during file match got bigger.
23 * Compressed ISAM support everywhere.
24 * Bug fixes regarding masking characters in queries.
25 * Redesigned Regexp-2 queries.
27 * Revision 1.1 1996/11/04 14:07:40 adam
28 * Moved truncation code to trunc.c.
51 int (*cmp)(const void *p1, const void *p2);
58 static void heap_swap (struct trunc_info *ti, int i1, int i2)
63 ti->ptr[i1] = ti->ptr[i2];
67 static void heap_delete (struct trunc_info *ti)
69 int cur = 1, child = 2;
71 heap_swap (ti, 1, ti->heapnum--);
72 while (child <= ti->heapnum) {
73 if (child < ti->heapnum &&
74 (*ti->cmp)(ti->heap[ti->ptr[child]],
75 ti->heap[ti->ptr[1+child]]) > 0)
77 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
78 ti->heap[ti->ptr[child]]) > 0)
80 heap_swap (ti, cur, child);
89 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
93 cur = ++(ti->heapnum);
94 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
95 ti->indx[ti->ptr[cur]] = indx;
97 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
98 ti->heap[ti->ptr[cur]]) > 0)
100 heap_swap (ti, cur, parent);
107 struct trunc_info *heap_init (int size, int key_size,
108 int (*cmp)(const void *p1, const void *p2))
110 struct trunc_info *ti = xmalloc (sizeof(*ti));
115 ti->keysize = key_size;
117 ti->indx = xmalloc (size * sizeof(*ti->indx));
118 ti->heap = xmalloc (size * sizeof(*ti->heap));
119 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
120 ti->swapbuf = xmalloc (ti->keysize);
121 ti->tmpbuf = xmalloc (ti->keysize);
122 ti->buf = xmalloc (size * ti->keysize);
123 for (i = size; --i >= 0; )
126 ti->heap[i] = ti->buf + ti->keysize * i;
131 static void heap_close (struct trunc_info *ti)
141 static RSET rset_trunc_r (ZServerInfo *zi, ISAM_P *isam_p, int from, int to,
146 rset_temp_parms parms;
148 parms.key_size = sizeof(struct it_key);
149 parms.temp_path = res_get (zi->res, "setTmpDir");
150 result = rset_create (rset_kind_temp, &parms);
151 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
153 if (to - from > merge_chunk)
157 int i, i_add = (to-from)/merge_chunk + 1;
158 struct trunc_info *ti;
160 int rsmax = (to-from)/i_add + 1;
162 rset = xmalloc (sizeof(*rset) * rsmax);
163 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
165 for (i = from; i < to; i += i_add)
168 rset[rscur] = rset_trunc_r (zi, isam_p, i, i+i_add,
171 rset[rscur] = rset_trunc_r (zi, isam_p, i, to,
175 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
176 for (i = rscur; --i >= 0; )
178 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
179 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
180 heap_insert (ti, ti->tmpbuf, i);
183 rset_close (rset[i], rsfd[i]);
184 rset_delete (rset[i]);
189 int n = ti->indx[ti->ptr[1]];
191 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
195 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
198 rset_close (rset[n], rsfd[n]);
199 rset_delete (rset[n]);
202 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
205 heap_insert (ti, ti->tmpbuf, n);
218 struct trunc_info *ti;
220 ispt = xmalloc (sizeof(*ispt) * (to-from));
222 ti = heap_init (to-from, sizeof(struct it_key),
224 for (i = to-from; --i >= 0; )
226 ispt[i] = is_position (zi->isam, isam_p[from+i]);
227 if (is_readkey (ispt[i], ti->tmpbuf))
228 heap_insert (ti, ti->tmpbuf, i);
230 is_pt_free (ispt[i]);
234 int n = ti->indx[ti->ptr[1]];
236 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
238 /* section that preserve all keys */
240 if (is_readkey (ispt[n], ti->tmpbuf))
241 heap_insert (ti, ti->tmpbuf, n);
243 is_pt_free (ispt[n]);
245 /* section that preserve all keys with unique sysnos */
248 if (!is_readkey (ispt[n], ti->tmpbuf))
251 is_pt_free (ispt[n]);
254 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
257 heap_insert (ti, ti->tmpbuf, n);
270 struct trunc_info *ti;
272 ispt = xmalloc (sizeof(*ispt) * (to-from));
274 ti = heap_init (to-from, sizeof(struct it_key),
276 for (i = to-from; --i >= 0; )
278 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
279 if (isc_pp_read (ispt[i], ti->tmpbuf))
280 heap_insert (ti, ti->tmpbuf, i);
282 isc_pp_close (ispt[i]);
286 int n = ti->indx[ti->ptr[1]];
288 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
290 /* section that preserve all keys */
292 if (is_readkey (ispt[n], ti->tmpbuf))
293 heap_insert (ti, ti->tmpbuf, n);
295 isc_pp_close (ispt[n]);
297 /* section that preserve all keys with unique sysnos */
300 if (!isc_pp_read (ispt[n], ti->tmpbuf))
303 isc_pp_close (ispt[n]);
306 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
309 heap_insert (ti, ti->tmpbuf, n);
318 rset_close (result, result_rsfd);
322 static int isam_trunc_cmp (const void *p1, const void *p2)
324 ISAM_P i1 = *(ISAM_P*) p1;
325 ISAM_P i2 = *(ISAM_P*) p2;
328 d = is_type (i1) - is_type (i2);
331 return is_block (i1) - is_block (i2);
334 static int isamc_trunc_cmp (const void *p1, const void *p2)
336 ISAMC_P i1 = *(ISAMC_P*) p1;
337 ISAMC_P i2 = *(ISAMC_P*) p2;
340 d = isc_type (i1) - isc_type (i2);
343 return isc_block (i1) - isc_block (i2);
346 RSET rset_trunc (ZServerInfo *zi, ISAM_P *isam_p, int no)
348 logf (LOG_DEBUG, "rset_trunc no=%d", no);
352 return rset_create (rset_kind_null, NULL);
355 rset_isam_parms parms;
359 return rset_create (rset_kind_isam, &parms);
361 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
366 return rset_create (rset_kind_null, NULL);
369 rset_isamc_parms parms;
372 parms.is = zi->isamc;
373 return rset_create (rset_kind_isamc, &parms);
378 rset_m_or_parms parms;
380 logf (LOG_LOG, "new_trunc");
381 parms.key_size = sizeof(struct it_key);
382 parms.cmp = key_compare_it;
383 parms.isc = zi->isamc;
384 parms.isam_positions = isam_p;
385 parms.no_isam_positions = no;
386 parms.no_save_positions = 100;
387 return rset_create (rset_kind_m_or, &parms);
390 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
393 logf (LOG_FATAL, "Neither isam nor isamc set in rset_trunc");
394 return rset_trunc_r (zi, isam_p, 0, no, 100);