2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1999-07-20 13:59:18 adam
8 * Fixed bug that occurred when phrases had 0 hits.
10 * Revision 1.14 1999/05/26 07:49:13 adam
13 * Revision 1.13 1999/05/12 13:08:06 adam
14 * First version of ISAMS.
16 * Revision 1.12 1999/02/02 14:51:10 adam
17 * Updated WIN32 code specific sections. Changed header.
19 * Revision 1.11 1998/03/25 13:48:02 adam
20 * Fixed bug in rset_trunc_r.
22 * Revision 1.10 1998/03/05 08:45:13 adam
23 * New result set model and modular ranking system. Moved towards
24 * descent server API. System information stored as "SGML" records.
26 * Revision 1.9 1998/01/12 15:04:09 adam
27 * The test option (-s) only uses read-lock (and not write lock).
29 * Revision 1.8 1997/10/31 12:34:27 adam
30 * Bug fix: memory leak.
32 * Revision 1.7 1997/09/29 09:07:29 adam
35 * Revision 1.6 1997/09/22 12:39:06 adam
36 * Added get_pos method for the ranked result sets.
38 * Revision 1.5 1997/09/17 12:19:17 adam
39 * Zebra version corresponds to YAZ version 1.4.
40 * Changed Zebra server so that it doesn't depend on global common_resource.
42 * Revision 1.4 1996/12/23 15:30:44 adam
44 * Bug fix: result sets weren't deleted after server shut down.
46 * Revision 1.3 1996/12/20 11:07:14 adam
47 * Multi-or result set.
49 * Revision 1.2 1996/11/08 11:10:28 adam
50 * Buffers used during file match got bigger.
51 * Compressed ISAM support everywhere.
52 * Bug fixes regarding masking characters in queries.
53 * Redesigned Regexp-2 queries.
55 * Revision 1.1 1996/11/04 14:07:40 adam
56 * Moved truncation code to trunc.c.
80 int (*cmp)(const void *p1, const void *p2);
87 static void heap_swap (struct trunc_info *ti, int i1, int i2)
92 ti->ptr[i1] = ti->ptr[i2];
96 static void heap_delete (struct trunc_info *ti)
98 int cur = 1, child = 2;
100 heap_swap (ti, 1, ti->heapnum--);
101 while (child <= ti->heapnum) {
102 if (child < ti->heapnum &&
103 (*ti->cmp)(ti->heap[ti->ptr[child]],
104 ti->heap[ti->ptr[1+child]]) > 0)
106 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
107 ti->heap[ti->ptr[child]]) > 0)
109 heap_swap (ti, cur, child);
118 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
122 cur = ++(ti->heapnum);
123 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
124 ti->indx[ti->ptr[cur]] = indx;
126 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
127 ti->heap[ti->ptr[cur]]) > 0)
129 heap_swap (ti, cur, parent);
135 static struct trunc_info *heap_init (int size, int key_size,
136 int (*cmp)(const void *p1,
139 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
144 ti->keysize = key_size;
146 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
147 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
148 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
149 ti->swapbuf = (char *) xmalloc (ti->keysize);
150 ti->tmpbuf = (char *) xmalloc (ti->keysize);
151 ti->buf = (char *) xmalloc (size * ti->keysize);
152 for (i = size; --i >= 0; )
155 ti->heap[i] = ti->buf + ti->keysize * i;
160 static void heap_close (struct trunc_info *ti)
171 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
172 const char *flags, ISAM_P *isam_p, int from, int to,
177 rset_temp_parms parms;
179 parms.key_size = sizeof(struct it_key);
180 parms.temp_path = res_get (zi->res, "setTmpDir");
181 parms.rset_term = rset_term_create (term, length, flags);
182 result = rset_create (rset_kind_temp, &parms);
183 result_rsfd = rset_open (result, RSETF_WRITE);
185 if (to - from > merge_chunk)
190 int i, i_add = (to-from)/merge_chunk + 1;
191 struct trunc_info *ti;
193 int rsmax = (to-from)/i_add + 1;
195 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
196 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
198 for (i = from; i < to; i += i_add)
201 rset[rscur] = rset_trunc_r (zi, term, length, flags,
202 isam_p, i, i+i_add, merge_chunk);
204 rset[rscur] = rset_trunc_r (zi, term, length, flags,
205 isam_p, i, to, merge_chunk);
208 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
209 for (i = rscur; --i >= 0; )
211 rsfd[i] = rset_open (rset[i], RSETF_READ);
212 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
213 heap_insert (ti, ti->tmpbuf, i);
216 rset_close (rset[i], rsfd[i]);
217 rset_delete (rset[i]);
222 int n = ti->indx[ti->ptr[1]];
224 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
228 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
231 rset_close (rset[n], rsfd[n]);
232 rset_delete (rset[n]);
235 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
238 heap_insert (ti, ti->tmpbuf, n);
251 struct trunc_info *ti;
253 ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
255 ti = heap_init (to-from, sizeof(struct it_key),
257 for (i = to-from; --i >= 0; )
259 ispt[i] = is_position (zi->isam, isam_p[from+i]);
260 if (is_readkey (ispt[i], ti->tmpbuf))
261 heap_insert (ti, ti->tmpbuf, i);
263 is_pt_free (ispt[i]);
267 int n = ti->indx[ti->ptr[1]];
269 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
271 /* section that preserve all keys */
273 if (is_readkey (ispt[n], ti->tmpbuf))
274 heap_insert (ti, ti->tmpbuf, n);
276 is_pt_free (ispt[n]);
278 /* section that preserve all keys with unique sysnos */
281 if (!is_readkey (ispt[n], ti->tmpbuf))
284 is_pt_free (ispt[n]);
287 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
290 heap_insert (ti, ti->tmpbuf, n);
303 struct trunc_info *ti;
305 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
307 ti = heap_init (to-from, sizeof(struct it_key),
309 for (i = to-from; --i >= 0; )
311 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
312 if (isc_pp_read (ispt[i], ti->tmpbuf))
313 heap_insert (ti, ti->tmpbuf, i);
315 isc_pp_close (ispt[i]);
319 int n = ti->indx[ti->ptr[1]];
321 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
323 /* section that preserve all keys */
325 if (isc_pp_read (ispt[n], ti->tmpbuf))
326 heap_insert (ti, ti->tmpbuf, n);
328 isc_pp_close (ispt[n]);
330 /* section that preserve all keys with unique sysnos */
333 if (!isc_pp_read (ispt[n], ti->tmpbuf))
336 isc_pp_close (ispt[n]);
339 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
342 heap_insert (ti, ti->tmpbuf, n);
355 struct trunc_info *ti;
357 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
359 ti = heap_init (to-from, sizeof(struct it_key),
361 for (i = to-from; --i >= 0; )
363 ispt[i] = isams_pp_open (zi->isams, isam_p[from+i]);
364 if (isams_pp_read (ispt[i], ti->tmpbuf))
365 heap_insert (ti, ti->tmpbuf, i);
367 isams_pp_close (ispt[i]);
371 int n = ti->indx[ti->ptr[1]];
373 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
376 if (!isams_pp_read (ispt[n], ti->tmpbuf))
379 isams_pp_close (ispt[n]);
382 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
385 heap_insert (ti, ti->tmpbuf, n);
393 rset_close (result, result_rsfd);
397 static int isam_trunc_cmp (const void *p1, const void *p2)
399 ISAM_P i1 = *(ISAM_P*) p1;
400 ISAM_P i2 = *(ISAM_P*) p2;
403 d = is_type (i1) - is_type (i2);
406 return is_block (i1) - is_block (i2);
409 static int isamc_trunc_cmp (const void *p1, const void *p2)
411 ISAMC_P i1 = *(ISAMC_P*) p1;
412 ISAMC_P i2 = *(ISAMC_P*) p2;
415 d = isc_type (i1) - isc_type (i2);
418 return isc_block (i1) - isc_block (i2);
421 static int isams_trunc_cmp (const void *p1, const void *p2)
423 ISAMS_P i1 = *(ISAMS_P*) p1;
424 ISAMS_P i2 = *(ISAMS_P*) p2;
429 RSET rset_trunc (ZebraHandle zi, ISAM_P *isam_p, int no,
430 const char *term, int length, const char *flags)
432 logf (LOG_DEBUG, "rset_trunc no=%d", no);
435 rset_null_parms parms;
436 parms.rset_term = rset_term_create (term, length, flags);
437 return rset_create (rset_kind_null, &parms);
443 rset_isam_parms parms;
447 parms.rset_term = rset_term_create (term, length, flags);
448 return rset_create (rset_kind_isam, &parms);
450 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
456 rset_isamc_parms parms;
459 parms.is = zi->isamc;
460 parms.rset_term = rset_term_create (term, length, flags);
461 return rset_create (rset_kind_isamc, &parms);
466 rset_m_or_parms parms;
468 parms.key_size = sizeof(struct it_key);
469 parms.cmp = key_compare_it;
470 parms.isc = zi->isamc;
471 parms.isam_positions = isam_p;
472 parms.no_isam_positions = no;
473 parms.no_save_positions = 100000;
474 parms.rset_term = rset_term_create (term, length, flags);
475 return rset_create (rset_kind_m_or, &parms);
478 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
484 rset_isams_parms parms;
487 parms.is = zi->isams;
488 parms.rset_term = rset_term_create (term, length, flags);
489 return rset_create (rset_kind_isams, &parms);
491 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
495 logf (LOG_WARN, "Neither isam / isamc / isams set in rset_trunc");
496 return rset_create (rset_kind_null, NULL);
498 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100);