2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 2002-04-05 08:46:26 adam
8 * Zebra with full functionality
10 * Revision 1.21 2002/04/04 14:14:13 adam
11 * Multiple registers (alpha early)
13 * Revision 1.20 2002/03/20 20:24:29 adam
14 * Hits per term. Returned in SearchResult-1
16 * Revision 1.19 2001/01/16 16:56:15 heikki
17 * Searching in my isam-d
19 * Revision 1.18 2000/05/18 12:01:36 adam
20 * System call times(2) used again. More 64-bit fixes.
22 * Revision 1.17 2000/03/15 15:00:30 adam
23 * First work on threaded version.
25 * Revision 1.16 1999/11/30 13:48:03 adam
26 * Improved installation. Updated for inclusion of YAZ header files.
28 * Revision 1.15 1999/07/20 13:59:18 adam
29 * Fixed bug that occurred when phrases had 0 hits.
31 * Revision 1.14 1999/05/26 07:49:13 adam
34 * Revision 1.13 1999/05/12 13:08:06 adam
35 * First version of ISAMS.
37 * Revision 1.12 1999/02/02 14:51:10 adam
38 * Updated WIN32 code specific sections. Changed header.
40 * Revision 1.11 1998/03/25 13:48:02 adam
41 * Fixed bug in rset_trunc_r.
43 * Revision 1.10 1998/03/05 08:45:13 adam
44 * New result set model and modular ranking system. Moved towards
45 * descent server API. System information stored as "SGML" records.
47 * Revision 1.9 1998/01/12 15:04:09 adam
48 * The test option (-s) only uses read-lock (and not write lock).
50 * Revision 1.8 1997/10/31 12:34:27 adam
51 * Bug fix: memory leak.
53 * Revision 1.7 1997/09/29 09:07:29 adam
56 * Revision 1.6 1997/09/22 12:39:06 adam
57 * Added get_pos method for the ranked result sets.
59 * Revision 1.5 1997/09/17 12:19:17 adam
60 * Zebra version corresponds to YAZ version 1.4.
61 * Changed Zebra server so that it doesn't depend on global common_resource.
63 * Revision 1.4 1996/12/23 15:30:44 adam
65 * Bug fix: result sets weren't deleted after server shut down.
67 * Revision 1.3 1996/12/20 11:07:14 adam
68 * Multi-or result set.
70 * Revision 1.2 1996/11/08 11:10:28 adam
71 * Buffers used during file match got bigger.
72 * Compressed ISAM support everywhere.
73 * Bug fixes regarding masking characters in queries.
74 * Redesigned Regexp-2 queries.
76 * Revision 1.1 1996/11/04 14:07:40 adam
77 * Moved truncation code to trunc.c.
101 int (*cmp)(const void *p1, const void *p2);
108 static void heap_swap (struct trunc_info *ti, int i1, int i2)
113 ti->ptr[i1] = ti->ptr[i2];
117 static void heap_delete (struct trunc_info *ti)
119 int cur = 1, child = 2;
121 heap_swap (ti, 1, ti->heapnum--);
122 while (child <= ti->heapnum) {
123 if (child < ti->heapnum &&
124 (*ti->cmp)(ti->heap[ti->ptr[child]],
125 ti->heap[ti->ptr[1+child]]) > 0)
127 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
128 ti->heap[ti->ptr[child]]) > 0)
130 heap_swap (ti, cur, child);
139 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
143 cur = ++(ti->heapnum);
144 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
145 ti->indx[ti->ptr[cur]] = indx;
147 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
148 ti->heap[ti->ptr[cur]]) > 0)
150 heap_swap (ti, cur, parent);
156 static struct trunc_info *heap_init (int size, int key_size,
157 int (*cmp)(const void *p1,
160 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
165 ti->keysize = key_size;
167 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
168 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
169 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
170 ti->swapbuf = (char *) xmalloc (ti->keysize);
171 ti->tmpbuf = (char *) xmalloc (ti->keysize);
172 ti->buf = (char *) xmalloc (size * ti->keysize);
173 for (i = size; --i >= 0; )
176 ti->heap[i] = ti->buf + ti->keysize * i;
181 static void heap_close (struct trunc_info *ti)
192 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
193 const char *flags, ISAMS_P *isam_p, int from, int to,
198 rset_temp_parms parms;
200 parms.cmp = key_compare_it;
201 parms.key_size = sizeof(struct it_key);
202 parms.temp_path = res_get (zi->res, "setTmpDir");
203 parms.rset_term = rset_term_create (term, length, flags);
204 result = rset_create (rset_kind_temp, &parms);
205 result_rsfd = rset_open (result, RSETF_WRITE);
207 if (to - from > merge_chunk)
212 int i, i_add = (to-from)/merge_chunk + 1;
213 struct trunc_info *ti;
215 int rsmax = (to-from)/i_add + 1;
217 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
218 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
220 for (i = from; i < to; i += i_add)
223 rset[rscur] = rset_trunc_r (zi, term, length, flags,
224 isam_p, i, i+i_add, merge_chunk);
226 rset[rscur] = rset_trunc_r (zi, term, length, flags,
227 isam_p, i, to, merge_chunk);
230 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
231 for (i = rscur; --i >= 0; )
233 rsfd[i] = rset_open (rset[i], RSETF_READ);
234 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
235 heap_insert (ti, ti->tmpbuf, i);
238 rset_close (rset[i], rsfd[i]);
239 rset_delete (rset[i]);
244 int n = ti->indx[ti->ptr[1]];
246 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
250 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
253 rset_close (rset[n], rsfd[n]);
254 rset_delete (rset[n]);
257 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
260 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);
427 else if (zi->reg->isams)
431 struct trunc_info *ti;
433 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
435 ti = heap_init (to-from, sizeof(struct it_key),
437 for (i = to-from; --i >= 0; )
439 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
440 if (isams_pp_read (ispt[i], ti->tmpbuf))
441 heap_insert (ti, ti->tmpbuf, i);
443 isams_pp_close (ispt[i]);
447 int n = ti->indx[ti->ptr[1]];
449 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
452 if (!isams_pp_read (ispt[n], ti->tmpbuf))
455 isams_pp_close (ispt[n]);
458 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
461 heap_insert (ti, ti->tmpbuf, n);
470 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
472 rset_close (result, result_rsfd);
476 static int isams_trunc_cmp (const void *p1, const void *p2)
478 ISAMS_P i1 = *(ISAMS_P*) p1;
479 ISAMS_P i2 = *(ISAMS_P*) p2;
484 static int isam_trunc_cmp (const void *p1, const void *p2)
486 ISAM_P i1 = *(ISAM_P*) p1;
487 ISAM_P i2 = *(ISAM_P*) p2;
490 d = is_type (i1) - is_type (i2);
493 return is_block (i1) - is_block (i2);
496 static int isamc_trunc_cmp (const void *p1, const void *p2)
498 ISAMC_P i1 = *(ISAMC_P*) p1;
499 ISAMC_P i2 = *(ISAMC_P*) p2;
502 d = isc_type (i1) - isc_type (i2);
505 return isc_block (i1) - isc_block (i2);
507 static int isamd_trunc_cmp (const void *p1, const void *p2)
509 ISAMD_P i1 = *(ISAMD_P*) p1;
510 ISAMD_P i2 = *(ISAMD_P*) p2;
513 d = isamd_type (i1) - isamd_type (i2);
516 return isamd_block (i1) - isamd_block (i2);
519 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
520 const char *term, int length, const char *flags)
522 logf (LOG_DEBUG, "rset_trunc no=%d", no);
525 rset_null_parms parms;
526 parms.rset_term = rset_term_create (term, length, flags);
527 return rset_create (rset_kind_null, &parms);
533 rset_isams_parms parms;
536 parms.is = zi->reg->isams;
537 parms.rset_term = rset_term_create (term, length, flags);
538 return rset_create (rset_kind_isams, &parms);
540 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
542 else if (zi->reg->isam)
546 rset_isam_parms parms;
549 parms.is = zi->reg->isam;
550 parms.rset_term = rset_term_create (term, length, flags);
551 return rset_create (rset_kind_isam, &parms);
553 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
555 else if (zi->reg->isamc)
559 rset_isamc_parms parms;
561 parms.key_size = sizeof(struct it_key);
562 parms.cmp = key_compare_it;
564 parms.is = zi->reg->isamc;
565 parms.rset_term = rset_term_create (term, length, flags);
566 return rset_create (rset_kind_isamc, &parms);
571 rset_m_or_parms parms;
573 parms.key_size = sizeof(struct it_key);
574 parms.cmp = key_compare_it;
575 parms.isc = zi->reg->isamc;
576 parms.isam_positions = isam_p;
577 parms.no_isam_positions = no;
578 parms.no_save_positions = 100000;
579 parms.rset_term = rset_term_create (term, length, flags);
580 return rset_create (rset_kind_m_or, &parms);
583 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
585 else if (zi->reg->isamd)
589 rset_isamd_parms parms;
592 parms.is = zi->reg->isamd;
593 parms.rset_term = rset_term_create (term, length, flags);
594 return rset_create (rset_kind_isamd, &parms);
596 #if NEW_TRUNC_NOT_DONE_FOR_ISAM_D
599 rset_m_or_parms parms;
601 parms.key_size = sizeof(struct it_key);
602 parms.cmp = key_compare_it;
604 parms.isamd=zi->reg->isamd;
605 parms.isam_positions = isam_p;
606 parms.no_isam_positions = no;
607 parms.no_save_positions = 100000;
608 parms.rset_term = rset_term_create (term, length, flags);
609 return rset_create (rset_kind_m_or, &parms);
612 qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
616 logf (LOG_WARN, "Unknown isam set in rset_trunc");
617 return rset_create (rset_kind_null, NULL);
619 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100);