2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1997-09-17 12:19:17 adam
8 * Zebra version corresponds to YAZ version 1.4.
9 * Changed Zebra server so that it doesn't depend on global common_resource.
11 * Revision 1.4 1996/12/23 15:30:44 adam
13 * Bug fix: result sets weren't deleted after server shut down.
15 * Revision 1.3 1996/12/20 11:07:14 adam
16 * Multi-or result set.
18 * Revision 1.2 1996/11/08 11:10:28 adam
19 * Buffers used during file match got bigger.
20 * Compressed ISAM support everywhere.
21 * Bug fixes regarding masking characters in queries.
22 * Redesigned Regexp-2 queries.
24 * Revision 1.1 1996/11/04 14:07:40 adam
25 * Moved truncation code to trunc.c.
48 int (*cmp)(const void *p1, const void *p2);
55 static void heap_swap (struct trunc_info *ti, int i1, int i2)
60 ti->ptr[i1] = ti->ptr[i2];
64 static void heap_delete (struct trunc_info *ti)
66 int cur = 1, child = 2;
68 heap_swap (ti, 1, ti->heapnum--);
69 while (child <= ti->heapnum) {
70 if (child < ti->heapnum &&
71 (*ti->cmp)(ti->heap[ti->ptr[child]],
72 ti->heap[ti->ptr[1+child]]) > 0)
74 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
75 ti->heap[ti->ptr[child]]) > 0)
77 heap_swap (ti, cur, child);
86 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
90 cur = ++(ti->heapnum);
91 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
92 ti->indx[ti->ptr[cur]] = indx;
94 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
95 ti->heap[ti->ptr[cur]]) > 0)
97 heap_swap (ti, cur, parent);
104 struct trunc_info *heap_init (int size, int key_size,
105 int (*cmp)(const void *p1, const void *p2))
107 struct trunc_info *ti = xmalloc (sizeof(*ti));
112 ti->keysize = key_size;
114 ti->indx = xmalloc (size * sizeof(*ti->indx));
115 ti->heap = xmalloc (size * sizeof(*ti->heap));
116 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
117 ti->swapbuf = xmalloc (ti->keysize);
118 ti->tmpbuf = xmalloc (ti->keysize);
119 ti->buf = xmalloc (size * ti->keysize);
120 for (i = size; --i >= 0; )
123 ti->heap[i] = ti->buf + ti->keysize * i;
128 static void heap_close (struct trunc_info *ti)
138 static RSET rset_trunc_r (ZServerInfo *zi, ISAM_P *isam_p, int from, int to,
143 rset_temp_parms parms;
145 parms.key_size = sizeof(struct it_key);
146 parms.temp_path = res_get (zi->res, "setTmpDir");
147 result = rset_create (rset_kind_temp, &parms);
148 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
150 if (to - from > merge_chunk)
154 int i, i_add = (to-from)/merge_chunk + 1;
155 struct trunc_info *ti;
157 int rsmax = (to-from)/i_add + 1;
159 rset = xmalloc (sizeof(*rset) * rsmax);
160 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
162 for (i = from; i < to; i += i_add)
165 rset[rscur] = rset_trunc_r (zi, isam_p, i, i+i_add,
168 rset[rscur] = rset_trunc_r (zi, isam_p, i, to,
172 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
173 for (i = rscur; --i >= 0; )
175 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
176 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
177 heap_insert (ti, ti->tmpbuf, i);
180 rset_close (rset[i], rsfd[i]);
181 rset_delete (rset[i]);
186 int n = ti->indx[ti->ptr[1]];
188 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
192 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
195 rset_close (rset[n], rsfd[n]);
196 rset_delete (rset[n]);
199 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
202 heap_insert (ti, ti->tmpbuf, n);
215 struct trunc_info *ti;
217 ispt = xmalloc (sizeof(*ispt) * (to-from));
219 ti = heap_init (to-from, sizeof(struct it_key),
221 for (i = to-from; --i >= 0; )
223 ispt[i] = is_position (zi->isam, isam_p[from+i]);
224 if (is_readkey (ispt[i], ti->tmpbuf))
225 heap_insert (ti, ti->tmpbuf, i);
227 is_pt_free (ispt[i]);
231 int n = ti->indx[ti->ptr[1]];
233 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
235 /* section that preserve all keys */
237 if (is_readkey (ispt[n], ti->tmpbuf))
238 heap_insert (ti, ti->tmpbuf, n);
240 is_pt_free (ispt[n]);
242 /* section that preserve all keys with unique sysnos */
245 if (!is_readkey (ispt[n], ti->tmpbuf))
248 is_pt_free (ispt[n]);
251 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
254 heap_insert (ti, ti->tmpbuf, n);
267 struct trunc_info *ti;
269 ispt = xmalloc (sizeof(*ispt) * (to-from));
271 ti = heap_init (to-from, sizeof(struct it_key),
273 for (i = to-from; --i >= 0; )
275 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
276 if (isc_pp_read (ispt[i], ti->tmpbuf))
277 heap_insert (ti, ti->tmpbuf, i);
279 isc_pp_close (ispt[i]);
283 int n = ti->indx[ti->ptr[1]];
285 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
287 /* section that preserve all keys */
289 if (is_readkey (ispt[n], ti->tmpbuf))
290 heap_insert (ti, ti->tmpbuf, n);
292 isc_pp_close (ispt[n]);
294 /* section that preserve all keys with unique sysnos */
297 if (!isc_pp_read (ispt[n], ti->tmpbuf))
300 isc_pp_close (ispt[n]);
303 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
306 heap_insert (ti, ti->tmpbuf, n);
315 rset_close (result, result_rsfd);
319 static int isam_trunc_cmp (const void *p1, const void *p2)
321 ISAM_P i1 = *(ISAM_P*) p1;
322 ISAM_P i2 = *(ISAM_P*) p2;
325 d = is_type (i1) - is_type (i2);
328 return is_block (i1) - is_block (i2);
331 static int isamc_trunc_cmp (const void *p1, const void *p2)
333 ISAMC_P i1 = *(ISAMC_P*) p1;
334 ISAMC_P i2 = *(ISAMC_P*) p2;
337 d = isc_type (i1) - isc_type (i2);
340 return isc_block (i1) - isc_block (i2);
343 RSET rset_trunc (ZServerInfo *zi, ISAM_P *isam_p, int no)
345 logf (LOG_LOG, "rset_trunc no=%d", no);
349 return rset_create (rset_kind_null, NULL);
352 rset_isam_parms parms;
356 return rset_create (rset_kind_isam, &parms);
358 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
363 return rset_create (rset_kind_null, NULL);
366 rset_isamc_parms parms;
369 parms.is = zi->isamc;
370 return rset_create (rset_kind_isamc, &parms);
375 rset_m_or_parms parms;
377 logf (LOG_LOG, "new_trunc");
378 parms.key_size = sizeof(struct it_key);
379 parms.cmp = key_compare_it;
380 parms.isc = zi->isamc;
381 parms.isam_positions = isam_p;
382 parms.no_isam_positions = no;
383 parms.no_save_positions = 100;
384 return rset_create (rset_kind_m_or, &parms);
387 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
390 logf (LOG_FATAL, "Neither isam nor isamc set in rset_trunc");
391 return rset_trunc_r (zi, isam_p, 0, no, 100);