2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1999-11-30 13:48:03 adam
8 * Improved installation. Updated for inclusion of YAZ header files.
10 * Revision 1.15 1999/07/20 13:59:18 adam
11 * Fixed bug that occurred when phrases had 0 hits.
13 * Revision 1.14 1999/05/26 07:49:13 adam
16 * Revision 1.13 1999/05/12 13:08:06 adam
17 * First version of ISAMS.
19 * Revision 1.12 1999/02/02 14:51:10 adam
20 * Updated WIN32 code specific sections. Changed header.
22 * Revision 1.11 1998/03/25 13:48:02 adam
23 * Fixed bug in rset_trunc_r.
25 * Revision 1.10 1998/03/05 08:45:13 adam
26 * New result set model and modular ranking system. Moved towards
27 * descent server API. System information stored as "SGML" records.
29 * Revision 1.9 1998/01/12 15:04:09 adam
30 * The test option (-s) only uses read-lock (and not write lock).
32 * Revision 1.8 1997/10/31 12:34:27 adam
33 * Bug fix: memory leak.
35 * Revision 1.7 1997/09/29 09:07:29 adam
38 * Revision 1.6 1997/09/22 12:39:06 adam
39 * Added get_pos method for the ranked result sets.
41 * Revision 1.5 1997/09/17 12:19:17 adam
42 * Zebra version corresponds to YAZ version 1.4.
43 * Changed Zebra server so that it doesn't depend on global common_resource.
45 * Revision 1.4 1996/12/23 15:30:44 adam
47 * Bug fix: result sets weren't deleted after server shut down.
49 * Revision 1.3 1996/12/20 11:07:14 adam
50 * Multi-or result set.
52 * Revision 1.2 1996/11/08 11:10:28 adam
53 * Buffers used during file match got bigger.
54 * Compressed ISAM support everywhere.
55 * Bug fixes regarding masking characters in queries.
56 * Redesigned Regexp-2 queries.
58 * Revision 1.1 1996/11/04 14:07:40 adam
59 * Moved truncation code to trunc.c.
84 int (*cmp)(const void *p1, const void *p2);
91 static void heap_swap (struct trunc_info *ti, int i1, int i2)
96 ti->ptr[i1] = ti->ptr[i2];
100 static void heap_delete (struct trunc_info *ti)
102 int cur = 1, child = 2;
104 heap_swap (ti, 1, ti->heapnum--);
105 while (child <= ti->heapnum) {
106 if (child < ti->heapnum &&
107 (*ti->cmp)(ti->heap[ti->ptr[child]],
108 ti->heap[ti->ptr[1+child]]) > 0)
110 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
111 ti->heap[ti->ptr[child]]) > 0)
113 heap_swap (ti, cur, child);
122 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
126 cur = ++(ti->heapnum);
127 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
128 ti->indx[ti->ptr[cur]] = indx;
130 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
131 ti->heap[ti->ptr[cur]]) > 0)
133 heap_swap (ti, cur, parent);
139 static struct trunc_info *heap_init (int size, int key_size,
140 int (*cmp)(const void *p1,
143 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
148 ti->keysize = key_size;
150 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
151 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
152 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
153 ti->swapbuf = (char *) xmalloc (ti->keysize);
154 ti->tmpbuf = (char *) xmalloc (ti->keysize);
155 ti->buf = (char *) xmalloc (size * ti->keysize);
156 for (i = size; --i >= 0; )
159 ti->heap[i] = ti->buf + ti->keysize * i;
164 static void heap_close (struct trunc_info *ti)
175 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
176 const char *flags, ISAMS_P *isam_p, int from, int to,
181 rset_temp_parms parms;
183 parms.key_size = sizeof(struct it_key);
184 parms.temp_path = res_get (zi->res, "setTmpDir");
185 parms.rset_term = rset_term_create (term, length, flags);
186 result = rset_create (rset_kind_temp, &parms);
187 result_rsfd = rset_open (result, RSETF_WRITE);
189 if (to - from > merge_chunk)
194 int i, i_add = (to-from)/merge_chunk + 1;
195 struct trunc_info *ti;
197 int rsmax = (to-from)/i_add + 1;
199 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
200 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
202 for (i = from; i < to; i += i_add)
205 rset[rscur] = rset_trunc_r (zi, term, length, flags,
206 isam_p, i, i+i_add, merge_chunk);
208 rset[rscur] = rset_trunc_r (zi, term, length, flags,
209 isam_p, i, to, merge_chunk);
212 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
213 for (i = rscur; --i >= 0; )
215 rsfd[i] = rset_open (rset[i], RSETF_READ);
216 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
217 heap_insert (ti, ti->tmpbuf, i);
220 rset_close (rset[i], rsfd[i]);
221 rset_delete (rset[i]);
226 int n = ti->indx[ti->ptr[1]];
228 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
232 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
235 rset_close (rset[n], rsfd[n]);
236 rset_delete (rset[n]);
239 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
242 heap_insert (ti, ti->tmpbuf, n);
256 struct trunc_info *ti;
258 ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
260 ti = heap_init (to-from, sizeof(struct it_key),
262 for (i = to-from; --i >= 0; )
264 ispt[i] = is_position (zi->isam, isam_p[from+i]);
265 if (is_readkey (ispt[i], ti->tmpbuf))
266 heap_insert (ti, ti->tmpbuf, i);
268 is_pt_free (ispt[i]);
272 int n = ti->indx[ti->ptr[1]];
274 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
276 /* section that preserve all keys */
278 if (is_readkey (ispt[n], ti->tmpbuf))
279 heap_insert (ti, ti->tmpbuf, n);
281 is_pt_free (ispt[n]);
283 /* section that preserve all keys with unique sysnos */
286 if (!is_readkey (ispt[n], ti->tmpbuf))
289 is_pt_free (ispt[n]);
292 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
295 heap_insert (ti, ti->tmpbuf, n);
308 struct trunc_info *ti;
310 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
312 ti = heap_init (to-from, sizeof(struct it_key),
314 for (i = to-from; --i >= 0; )
316 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
317 if (isc_pp_read (ispt[i], ti->tmpbuf))
318 heap_insert (ti, ti->tmpbuf, i);
320 isc_pp_close (ispt[i]);
324 int n = ti->indx[ti->ptr[1]];
326 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
328 /* section that preserve all keys */
330 if (isc_pp_read (ispt[n], ti->tmpbuf))
331 heap_insert (ti, ti->tmpbuf, n);
333 isc_pp_close (ispt[n]);
335 /* section that preserve all keys with unique sysnos */
338 if (!isc_pp_read (ispt[n], ti->tmpbuf))
341 isc_pp_close (ispt[n]);
344 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
347 heap_insert (ti, ti->tmpbuf, n);
361 struct trunc_info *ti;
363 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
365 ti = heap_init (to-from, sizeof(struct it_key),
367 for (i = to-from; --i >= 0; )
369 ispt[i] = isams_pp_open (zi->isams, isam_p[from+i]);
370 if (isams_pp_read (ispt[i], ti->tmpbuf))
371 heap_insert (ti, ti->tmpbuf, i);
373 isams_pp_close (ispt[i]);
377 int n = ti->indx[ti->ptr[1]];
379 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
382 if (!isams_pp_read (ispt[n], ti->tmpbuf))
385 isams_pp_close (ispt[n]);
388 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
391 heap_insert (ti, ti->tmpbuf, n);
399 rset_close (result, result_rsfd);
403 static int isams_trunc_cmp (const void *p1, const void *p2)
405 ISAMS_P i1 = *(ISAMS_P*) p1;
406 ISAMS_P i2 = *(ISAMS_P*) p2;
412 static int isam_trunc_cmp (const void *p1, const void *p2)
414 ISAM_P i1 = *(ISAM_P*) p1;
415 ISAM_P i2 = *(ISAM_P*) p2;
418 d = is_type (i1) - is_type (i2);
421 return is_block (i1) - is_block (i2);
424 static int isamc_trunc_cmp (const void *p1, const void *p2)
426 ISAMC_P i1 = *(ISAMC_P*) p1;
427 ISAMC_P i2 = *(ISAMC_P*) p2;
430 d = isc_type (i1) - isc_type (i2);
433 return isc_block (i1) - isc_block (i2);
437 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
438 const char *term, int length, const char *flags)
440 logf (LOG_DEBUG, "rset_trunc no=%d", no);
443 rset_null_parms parms;
444 parms.rset_term = rset_term_create (term, length, flags);
445 return rset_create (rset_kind_null, &parms);
451 rset_isams_parms parms;
454 parms.is = zi->isams;
455 parms.rset_term = rset_term_create (term, length, flags);
456 return rset_create (rset_kind_isams, &parms);
458 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
465 rset_isam_parms parms;
469 parms.rset_term = rset_term_create (term, length, flags);
470 return rset_create (rset_kind_isam, &parms);
472 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
478 rset_isamc_parms parms;
481 parms.is = zi->isamc;
482 parms.rset_term = rset_term_create (term, length, flags);
483 return rset_create (rset_kind_isamc, &parms);
488 rset_m_or_parms parms;
490 parms.key_size = sizeof(struct it_key);
491 parms.cmp = key_compare_it;
492 parms.isc = zi->isamc;
493 parms.isam_positions = isam_p;
494 parms.no_isam_positions = no;
495 parms.no_save_positions = 100000;
496 parms.rset_term = rset_term_create (term, length, flags);
497 return rset_create (rset_kind_m_or, &parms);
500 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
505 logf (LOG_WARN, "Neither isam / isamc / isams set in rset_trunc");
506 return rset_create (rset_kind_null, NULL);
508 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100);