2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 2000-03-15 15:00:30 adam
8 * First work on threaded version.
10 * Revision 1.16 1999/11/30 13:48:03 adam
11 * Improved installation. Updated for inclusion of YAZ header files.
13 * Revision 1.15 1999/07/20 13:59:18 adam
14 * Fixed bug that occurred when phrases had 0 hits.
16 * Revision 1.14 1999/05/26 07:49:13 adam
19 * Revision 1.13 1999/05/12 13:08:06 adam
20 * First version of ISAMS.
22 * Revision 1.12 1999/02/02 14:51:10 adam
23 * Updated WIN32 code specific sections. Changed header.
25 * Revision 1.11 1998/03/25 13:48:02 adam
26 * Fixed bug in rset_trunc_r.
28 * Revision 1.10 1998/03/05 08:45:13 adam
29 * New result set model and modular ranking system. Moved towards
30 * descent server API. System information stored as "SGML" records.
32 * Revision 1.9 1998/01/12 15:04:09 adam
33 * The test option (-s) only uses read-lock (and not write lock).
35 * Revision 1.8 1997/10/31 12:34:27 adam
36 * Bug fix: memory leak.
38 * Revision 1.7 1997/09/29 09:07:29 adam
41 * Revision 1.6 1997/09/22 12:39:06 adam
42 * Added get_pos method for the ranked result sets.
44 * Revision 1.5 1997/09/17 12:19:17 adam
45 * Zebra version corresponds to YAZ version 1.4.
46 * Changed Zebra server so that it doesn't depend on global common_resource.
48 * Revision 1.4 1996/12/23 15:30:44 adam
50 * Bug fix: result sets weren't deleted after server shut down.
52 * Revision 1.3 1996/12/20 11:07:14 adam
53 * Multi-or result set.
55 * Revision 1.2 1996/11/08 11:10:28 adam
56 * Buffers used during file match got bigger.
57 * Compressed ISAM support everywhere.
58 * Bug fixes regarding masking characters in queries.
59 * Redesigned Regexp-2 queries.
61 * Revision 1.1 1996/11/04 14:07:40 adam
62 * Moved truncation code to trunc.c.
87 int (*cmp)(const void *p1, const void *p2);
94 static void heap_swap (struct trunc_info *ti, int i1, int i2)
99 ti->ptr[i1] = ti->ptr[i2];
103 static void heap_delete (struct trunc_info *ti)
105 int cur = 1, child = 2;
107 heap_swap (ti, 1, ti->heapnum--);
108 while (child <= ti->heapnum) {
109 if (child < ti->heapnum &&
110 (*ti->cmp)(ti->heap[ti->ptr[child]],
111 ti->heap[ti->ptr[1+child]]) > 0)
113 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
114 ti->heap[ti->ptr[child]]) > 0)
116 heap_swap (ti, cur, child);
125 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
129 cur = ++(ti->heapnum);
130 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
131 ti->indx[ti->ptr[cur]] = indx;
133 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
134 ti->heap[ti->ptr[cur]]) > 0)
136 heap_swap (ti, cur, parent);
142 static struct trunc_info *heap_init (int size, int key_size,
143 int (*cmp)(const void *p1,
146 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
151 ti->keysize = key_size;
153 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
154 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
155 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
156 ti->swapbuf = (char *) xmalloc (ti->keysize);
157 ti->tmpbuf = (char *) xmalloc (ti->keysize);
158 ti->buf = (char *) xmalloc (size * ti->keysize);
159 for (i = size; --i >= 0; )
162 ti->heap[i] = ti->buf + ti->keysize * i;
167 static void heap_close (struct trunc_info *ti)
178 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
179 const char *flags, ISAMS_P *isam_p, int from, int to,
184 rset_temp_parms parms;
186 parms.key_size = sizeof(struct it_key);
187 parms.temp_path = res_get (zi->service->res, "setTmpDir");
188 parms.rset_term = rset_term_create (term, length, flags);
189 result = rset_create (rset_kind_temp, &parms);
190 result_rsfd = rset_open (result, RSETF_WRITE);
192 if (to - from > merge_chunk)
197 int i, i_add = (to-from)/merge_chunk + 1;
198 struct trunc_info *ti;
200 int rsmax = (to-from)/i_add + 1;
202 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
203 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
205 for (i = from; i < to; i += i_add)
208 rset[rscur] = rset_trunc_r (zi, term, length, flags,
209 isam_p, i, i+i_add, merge_chunk);
211 rset[rscur] = rset_trunc_r (zi, term, length, flags,
212 isam_p, i, to, merge_chunk);
215 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
216 for (i = rscur; --i >= 0; )
218 rsfd[i] = rset_open (rset[i], RSETF_READ);
219 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
220 heap_insert (ti, ti->tmpbuf, i);
223 rset_close (rset[i], rsfd[i]);
224 rset_delete (rset[i]);
229 int n = ti->indx[ti->ptr[1]];
231 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
235 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
238 rset_close (rset[n], rsfd[n]);
239 rset_delete (rset[n]);
242 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
245 heap_insert (ti, ti->tmpbuf, n);
255 else if (zi->service->isam)
259 struct trunc_info *ti;
261 ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
263 ti = heap_init (to-from, sizeof(struct it_key),
265 for (i = to-from; --i >= 0; )
267 ispt[i] = is_position (zi->service->isam, isam_p[from+i]);
268 if (is_readkey (ispt[i], ti->tmpbuf))
269 heap_insert (ti, ti->tmpbuf, i);
271 is_pt_free (ispt[i]);
275 int n = ti->indx[ti->ptr[1]];
277 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
279 /* section that preserve all keys */
281 if (is_readkey (ispt[n], ti->tmpbuf))
282 heap_insert (ti, ti->tmpbuf, n);
284 is_pt_free (ispt[n]);
286 /* section that preserve all keys with unique sysnos */
289 if (!is_readkey (ispt[n], ti->tmpbuf))
292 is_pt_free (ispt[n]);
295 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
298 heap_insert (ti, ti->tmpbuf, n);
307 else if (zi->service->isamc)
311 struct trunc_info *ti;
313 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
315 ti = heap_init (to-from, sizeof(struct it_key),
317 for (i = to-from; --i >= 0; )
319 ispt[i] = isc_pp_open (zi->service->isamc, isam_p[from+i]);
320 if (isc_pp_read (ispt[i], ti->tmpbuf))
321 heap_insert (ti, ti->tmpbuf, i);
323 isc_pp_close (ispt[i]);
327 int n = ti->indx[ti->ptr[1]];
329 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
331 /* section that preserve all keys */
333 if (isc_pp_read (ispt[n], ti->tmpbuf))
334 heap_insert (ti, ti->tmpbuf, n);
336 isc_pp_close (ispt[n]);
338 /* section that preserve all keys with unique sysnos */
341 if (!isc_pp_read (ispt[n], ti->tmpbuf))
344 isc_pp_close (ispt[n]);
347 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
350 heap_insert (ti, ti->tmpbuf, n);
360 else if (zi->service->isams)
364 struct trunc_info *ti;
366 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
368 ti = heap_init (to-from, sizeof(struct it_key),
370 for (i = to-from; --i >= 0; )
372 ispt[i] = isams_pp_open (zi->service->isams, isam_p[from+i]);
373 if (isams_pp_read (ispt[i], ti->tmpbuf))
374 heap_insert (ti, ti->tmpbuf, i);
376 isams_pp_close (ispt[i]);
380 int n = ti->indx[ti->ptr[1]];
382 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
385 if (!isams_pp_read (ispt[n], ti->tmpbuf))
388 isams_pp_close (ispt[n]);
391 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
394 heap_insert (ti, ti->tmpbuf, n);
402 rset_close (result, result_rsfd);
406 static int isams_trunc_cmp (const void *p1, const void *p2)
408 ISAMS_P i1 = *(ISAMS_P*) p1;
409 ISAMS_P i2 = *(ISAMS_P*) p2;
415 static int isam_trunc_cmp (const void *p1, const void *p2)
417 ISAM_P i1 = *(ISAM_P*) p1;
418 ISAM_P i2 = *(ISAM_P*) p2;
421 d = is_type (i1) - is_type (i2);
424 return is_block (i1) - is_block (i2);
427 static int isamc_trunc_cmp (const void *p1, const void *p2)
429 ISAMC_P i1 = *(ISAMC_P*) p1;
430 ISAMC_P i2 = *(ISAMC_P*) p2;
433 d = isc_type (i1) - isc_type (i2);
436 return isc_block (i1) - isc_block (i2);
440 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
441 const char *term, int length, const char *flags)
443 logf (LOG_DEBUG, "rset_trunc no=%d", no);
446 rset_null_parms parms;
447 parms.rset_term = rset_term_create (term, length, flags);
448 return rset_create (rset_kind_null, &parms);
450 if (zi->service->isams)
454 rset_isams_parms parms;
457 parms.is = zi->service->isams;
458 parms.rset_term = rset_term_create (term, length, flags);
459 return rset_create (rset_kind_isams, &parms);
461 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
464 else if (zi->service->isam)
468 rset_isam_parms parms;
471 parms.is = zi->service->isam;
472 parms.rset_term = rset_term_create (term, length, flags);
473 return rset_create (rset_kind_isam, &parms);
475 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
477 else if (zi->service->isamc)
481 rset_isamc_parms parms;
484 parms.is = zi->service->isamc;
485 parms.rset_term = rset_term_create (term, length, flags);
486 return rset_create (rset_kind_isamc, &parms);
491 rset_m_or_parms parms;
493 parms.key_size = sizeof(struct it_key);
494 parms.cmp = key_compare_it;
495 parms.isc = zi->service->isamc;
496 parms.isam_positions = isam_p;
497 parms.no_isam_positions = no;
498 parms.no_save_positions = 100000;
499 parms.rset_term = rset_term_create (term, length, flags);
500 return rset_create (rset_kind_m_or, &parms);
503 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
508 logf (LOG_WARN, "Neither isam / isamc / isams set in rset_trunc");
509 return rset_create (rset_kind_null, NULL);
511 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100);