2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 2002-04-04 14:14:13 adam
8 * Multiple registers (alpha early)
10 * Revision 1.20 2002/03/20 20:24:29 adam
11 * Hits per term. Returned in SearchResult-1
13 * Revision 1.19 2001/01/16 16:56:15 heikki
14 * Searching in my isam-d
16 * Revision 1.18 2000/05/18 12:01:36 adam
17 * System call times(2) used again. More 64-bit fixes.
19 * Revision 1.17 2000/03/15 15:00:30 adam
20 * First work on threaded version.
22 * Revision 1.16 1999/11/30 13:48:03 adam
23 * Improved installation. Updated for inclusion of YAZ header files.
25 * Revision 1.15 1999/07/20 13:59:18 adam
26 * Fixed bug that occurred when phrases had 0 hits.
28 * Revision 1.14 1999/05/26 07:49:13 adam
31 * Revision 1.13 1999/05/12 13:08:06 adam
32 * First version of ISAMS.
34 * Revision 1.12 1999/02/02 14:51:10 adam
35 * Updated WIN32 code specific sections. Changed header.
37 * Revision 1.11 1998/03/25 13:48:02 adam
38 * Fixed bug in rset_trunc_r.
40 * Revision 1.10 1998/03/05 08:45:13 adam
41 * New result set model and modular ranking system. Moved towards
42 * descent server API. System information stored as "SGML" records.
44 * Revision 1.9 1998/01/12 15:04:09 adam
45 * The test option (-s) only uses read-lock (and not write lock).
47 * Revision 1.8 1997/10/31 12:34:27 adam
48 * Bug fix: memory leak.
50 * Revision 1.7 1997/09/29 09:07:29 adam
53 * Revision 1.6 1997/09/22 12:39:06 adam
54 * Added get_pos method for the ranked result sets.
56 * Revision 1.5 1997/09/17 12:19:17 adam
57 * Zebra version corresponds to YAZ version 1.4.
58 * Changed Zebra server so that it doesn't depend on global common_resource.
60 * Revision 1.4 1996/12/23 15:30:44 adam
62 * Bug fix: result sets weren't deleted after server shut down.
64 * Revision 1.3 1996/12/20 11:07:14 adam
65 * Multi-or result set.
67 * Revision 1.2 1996/11/08 11:10:28 adam
68 * Buffers used during file match got bigger.
69 * Compressed ISAM support everywhere.
70 * Bug fixes regarding masking characters in queries.
71 * Redesigned Regexp-2 queries.
73 * Revision 1.1 1996/11/04 14:07:40 adam
74 * Moved truncation code to trunc.c.
100 int (*cmp)(const void *p1, const void *p2);
107 static void heap_swap (struct trunc_info *ti, int i1, int i2)
112 ti->ptr[i1] = ti->ptr[i2];
116 static void heap_delete (struct trunc_info *ti)
118 int cur = 1, child = 2;
120 heap_swap (ti, 1, ti->heapnum--);
121 while (child <= ti->heapnum) {
122 if (child < ti->heapnum &&
123 (*ti->cmp)(ti->heap[ti->ptr[child]],
124 ti->heap[ti->ptr[1+child]]) > 0)
126 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
127 ti->heap[ti->ptr[child]]) > 0)
129 heap_swap (ti, cur, child);
138 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
142 cur = ++(ti->heapnum);
143 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
144 ti->indx[ti->ptr[cur]] = indx;
146 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
147 ti->heap[ti->ptr[cur]]) > 0)
149 heap_swap (ti, cur, parent);
155 static struct trunc_info *heap_init (int size, int key_size,
156 int (*cmp)(const void *p1,
159 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
164 ti->keysize = key_size;
166 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
167 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
168 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
169 ti->swapbuf = (char *) xmalloc (ti->keysize);
170 ti->tmpbuf = (char *) xmalloc (ti->keysize);
171 ti->buf = (char *) xmalloc (size * ti->keysize);
172 for (i = size; --i >= 0; )
175 ti->heap[i] = ti->buf + ti->keysize * i;
180 static void heap_close (struct trunc_info *ti)
191 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
192 const char *flags, ISAMS_P *isam_p, int from, int to,
197 rset_temp_parms parms;
199 parms.cmp = key_compare_it;
200 parms.key_size = sizeof(struct it_key);
201 parms.temp_path = res_get (zi->res, "setTmpDir");
202 parms.rset_term = rset_term_create (term, length, flags);
203 result = rset_create (rset_kind_temp, &parms);
204 result_rsfd = rset_open (result, RSETF_WRITE);
206 if (to - from > merge_chunk)
211 int i, i_add = (to-from)/merge_chunk + 1;
212 struct trunc_info *ti;
214 int rsmax = (to-from)/i_add + 1;
216 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
217 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
219 for (i = from; i < to; i += i_add)
222 rset[rscur] = rset_trunc_r (zi, term, length, flags,
223 isam_p, i, i+i_add, merge_chunk);
225 rset[rscur] = rset_trunc_r (zi, term, length, flags,
226 isam_p, i, to, merge_chunk);
229 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
230 for (i = rscur; --i >= 0; )
232 rsfd[i] = rset_open (rset[i], RSETF_READ);
233 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
234 heap_insert (ti, ti->tmpbuf, i);
237 rset_close (rset[i], rsfd[i]);
238 rset_delete (rset[i]);
243 int n = ti->indx[ti->ptr[1]];
245 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
249 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
252 rset_close (rset[n], rsfd[n]);
253 rset_delete (rset[n]);
256 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
259 heap_insert (ti, ti->tmpbuf, n);
269 else if (zi->reg->isam)
273 struct trunc_info *ti;
275 ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
277 ti = heap_init (to-from, sizeof(struct it_key),
279 for (i = to-from; --i >= 0; )
281 ispt[i] = is_position (zi->reg->isam, isam_p[from+i]);
282 if (is_readkey (ispt[i], ti->tmpbuf))
283 heap_insert (ti, ti->tmpbuf, i);
285 is_pt_free (ispt[i]);
289 int n = ti->indx[ti->ptr[1]];
291 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
293 /* section that preserve all keys */
295 if (is_readkey (ispt[n], ti->tmpbuf))
296 heap_insert (ti, ti->tmpbuf, n);
298 is_pt_free (ispt[n]);
300 /* section that preserve all keys with unique sysnos */
303 if (!is_readkey (ispt[n], ti->tmpbuf))
306 is_pt_free (ispt[n]);
309 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
312 heap_insert (ti, ti->tmpbuf, n);
321 else if (zi->reg->isamc)
325 struct trunc_info *ti;
327 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
329 ti = heap_init (to-from, sizeof(struct it_key),
331 for (i = to-from; --i >= 0; )
333 ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
334 if (isc_pp_read (ispt[i], ti->tmpbuf))
335 heap_insert (ti, ti->tmpbuf, i);
337 isc_pp_close (ispt[i]);
341 int n = ti->indx[ti->ptr[1]];
343 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
345 /* section that preserve all keys */
347 if (isc_pp_read (ispt[n], ti->tmpbuf))
348 heap_insert (ti, ti->tmpbuf, n);
350 isc_pp_close (ispt[n]);
352 /* section that preserve all keys with unique sysnos */
355 if (!isc_pp_read (ispt[n], ti->tmpbuf))
358 isc_pp_close (ispt[n]);
361 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
364 heap_insert (ti, ti->tmpbuf, n);
374 else if (zi->reg->isamd)
378 struct trunc_info *ti;
380 ispt = (ISAMD_PP *) xmalloc (sizeof(*ispt) * (to-from));
382 ti = heap_init (to-from, sizeof(struct it_key),
384 for (i = to-from; --i >= 0; )
386 ispt[i] = isamd_pp_open (zi->reg->isamd, isam_p[from+i]);
387 if (isamd_pp_read (ispt[i], ti->tmpbuf))
388 heap_insert (ti, ti->tmpbuf, i);
390 isamd_pp_close (ispt[i]);
394 int n = ti->indx[ti->ptr[1]];
396 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
398 /* section that preserve all keys */
400 if (isamd_pp_read (ispt[n], ti->tmpbuf))
401 heap_insert (ti, ti->tmpbuf, n);
403 isamd_pp_close (ispt[n]);
405 /* section that preserve all keys with unique sysnos */
408 if (!isamd_pp_read (ispt[n], ti->tmpbuf))
411 isamd_pp_close (ispt[n]);
414 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
417 heap_insert (ti, ti->tmpbuf, n);
428 else if (zi->reg->isams)
432 struct trunc_info *ti;
434 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
436 ti = heap_init (to-from, sizeof(struct it_key),
438 for (i = to-from; --i >= 0; )
440 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
441 if (isams_pp_read (ispt[i], ti->tmpbuf))
442 heap_insert (ti, ti->tmpbuf, i);
444 isams_pp_close (ispt[i]);
448 int n = ti->indx[ti->ptr[1]];
450 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
453 if (!isams_pp_read (ispt[n], ti->tmpbuf))
456 isams_pp_close (ispt[n]);
459 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
462 heap_insert (ti, ti->tmpbuf, n);
471 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
473 rset_close (result, result_rsfd);
477 static int isams_trunc_cmp (const void *p1, const void *p2)
479 ISAMS_P i1 = *(ISAMS_P*) p1;
480 ISAMS_P i2 = *(ISAMS_P*) p2;
486 static int isam_trunc_cmp (const void *p1, const void *p2)
488 ISAM_P i1 = *(ISAM_P*) p1;
489 ISAM_P i2 = *(ISAM_P*) p2;
492 d = is_type (i1) - is_type (i2);
495 return is_block (i1) - is_block (i2);
498 static int isamc_trunc_cmp (const void *p1, const void *p2)
500 ISAMC_P i1 = *(ISAMC_P*) p1;
501 ISAMC_P i2 = *(ISAMC_P*) p2;
504 d = isc_type (i1) - isc_type (i2);
507 return isc_block (i1) - isc_block (i2);
509 static int isamd_trunc_cmp (const void *p1, const void *p2)
511 ISAMD_P i1 = *(ISAMD_P*) p1;
512 ISAMD_P i2 = *(ISAMD_P*) p2;
515 d = isamd_type (i1) - isamd_type (i2);
518 return isamd_block (i1) - isamd_block (i2);
522 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
523 const char *term, int length, const char *flags)
525 logf (LOG_DEBUG, "rset_trunc no=%d", no);
528 rset_null_parms parms;
529 parms.rset_term = rset_term_create (term, length, flags);
530 return rset_create (rset_kind_null, &parms);
536 rset_isams_parms parms;
539 parms.is = zi->reg->isams;
540 parms.rset_term = rset_term_create (term, length, flags);
541 return rset_create (rset_kind_isams, &parms);
543 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
546 else if (zi->reg->isam)
550 rset_isam_parms parms;
553 parms.is = zi->reg->isam;
554 parms.rset_term = rset_term_create (term, length, flags);
555 return rset_create (rset_kind_isam, &parms);
557 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
559 else if (zi->reg->isamc)
563 rset_isamc_parms parms;
565 parms.key_size = sizeof(struct it_key);
566 parms.cmp = key_compare_it;
568 parms.is = zi->reg->isamc;
569 parms.rset_term = rset_term_create (term, length, flags);
570 return rset_create (rset_kind_isamc, &parms);
575 rset_m_or_parms parms;
577 parms.key_size = sizeof(struct it_key);
578 parms.cmp = key_compare_it;
579 parms.isc = zi->reg->isamc;
580 parms.isam_positions = isam_p;
581 parms.no_isam_positions = no;
582 parms.no_save_positions = 100000;
583 parms.rset_term = rset_term_create (term, length, flags);
584 return rset_create (rset_kind_m_or, &parms);
587 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
589 else if (zi->reg->isamd)
593 rset_isamd_parms parms;
596 parms.is = zi->reg->isamd;
597 parms.rset_term = rset_term_create (term, length, flags);
598 return rset_create (rset_kind_isamd, &parms);
600 #if NEW_TRUNC_NOT_DONE_FOR_ISAM_D
603 rset_m_or_parms parms;
605 parms.key_size = sizeof(struct it_key);
606 parms.cmp = key_compare_it;
608 parms.isamd=zi->reg->isamd;
609 parms.isam_positions = isam_p;
610 parms.no_isam_positions = no;
611 parms.no_save_positions = 100000;
612 parms.rset_term = rset_term_create (term, length, flags);
613 return rset_create (rset_kind_m_or, &parms);
616 qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
621 logf (LOG_WARN, "Unknown isam set in rset_trunc");
622 return rset_create (rset_kind_null, NULL);
624 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100);