2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.23 2002-04-12 14:40:42 adam
10 * Revision 1.22 2002/04/05 08:46:26 adam
11 * Zebra with full functionality
13 * Revision 1.21 2002/04/04 14:14:13 adam
14 * Multiple registers (alpha early)
16 * Revision 1.20 2002/03/20 20:24:29 adam
17 * Hits per term. Returned in SearchResult-1
19 * Revision 1.19 2001/01/16 16:56:15 heikki
20 * Searching in my isam-d
22 * Revision 1.18 2000/05/18 12:01:36 adam
23 * System call times(2) used again. More 64-bit fixes.
25 * Revision 1.17 2000/03/15 15:00:30 adam
26 * First work on threaded version.
28 * Revision 1.16 1999/11/30 13:48:03 adam
29 * Improved installation. Updated for inclusion of YAZ header files.
31 * Revision 1.15 1999/07/20 13:59:18 adam
32 * Fixed bug that occurred when phrases had 0 hits.
34 * Revision 1.14 1999/05/26 07:49:13 adam
37 * Revision 1.13 1999/05/12 13:08:06 adam
38 * First version of ISAMS.
40 * Revision 1.12 1999/02/02 14:51:10 adam
41 * Updated WIN32 code specific sections. Changed header.
43 * Revision 1.11 1998/03/25 13:48:02 adam
44 * Fixed bug in rset_trunc_r.
46 * Revision 1.10 1998/03/05 08:45:13 adam
47 * New result set model and modular ranking system. Moved towards
48 * descent server API. System information stored as "SGML" records.
50 * Revision 1.9 1998/01/12 15:04:09 adam
51 * The test option (-s) only uses read-lock (and not write lock).
53 * Revision 1.8 1997/10/31 12:34:27 adam
54 * Bug fix: memory leak.
56 * Revision 1.7 1997/09/29 09:07:29 adam
59 * Revision 1.6 1997/09/22 12:39:06 adam
60 * Added get_pos method for the ranked result sets.
62 * Revision 1.5 1997/09/17 12:19:17 adam
63 * Zebra version corresponds to YAZ version 1.4.
64 * Changed Zebra server so that it doesn't depend on global common_resource.
66 * Revision 1.4 1996/12/23 15:30:44 adam
68 * Bug fix: result sets weren't deleted after server shut down.
70 * Revision 1.3 1996/12/20 11:07:14 adam
71 * Multi-or result set.
73 * Revision 1.2 1996/11/08 11:10:28 adam
74 * Buffers used during file match got bigger.
75 * Compressed ISAM support everywhere.
76 * Bug fixes regarding masking characters in queries.
77 * Redesigned Regexp-2 queries.
79 * Revision 1.1 1996/11/04 14:07:40 adam
80 * Moved truncation code to trunc.c.
104 int (*cmp)(const void *p1, const void *p2);
111 static void heap_swap (struct trunc_info *ti, int i1, int i2)
116 ti->ptr[i1] = ti->ptr[i2];
120 static void heap_delete (struct trunc_info *ti)
122 int cur = 1, child = 2;
124 heap_swap (ti, 1, ti->heapnum--);
125 while (child <= ti->heapnum) {
126 if (child < ti->heapnum &&
127 (*ti->cmp)(ti->heap[ti->ptr[child]],
128 ti->heap[ti->ptr[1+child]]) > 0)
130 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
131 ti->heap[ti->ptr[child]]) > 0)
133 heap_swap (ti, cur, child);
142 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
146 cur = ++(ti->heapnum);
147 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
148 ti->indx[ti->ptr[cur]] = indx;
150 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
151 ti->heap[ti->ptr[cur]]) > 0)
153 heap_swap (ti, cur, parent);
159 static struct trunc_info *heap_init (int size, int key_size,
160 int (*cmp)(const void *p1,
163 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
168 ti->keysize = key_size;
170 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
171 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
172 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
173 ti->swapbuf = (char *) xmalloc (ti->keysize);
174 ti->tmpbuf = (char *) xmalloc (ti->keysize);
175 ti->buf = (char *) xmalloc (size * ti->keysize);
176 for (i = size; --i >= 0; )
179 ti->heap[i] = ti->buf + ti->keysize * i;
184 static void heap_close (struct trunc_info *ti)
195 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
196 const char *flags, ISAMS_P *isam_p, int from, int to,
197 int merge_chunk, int preserve_position)
201 rset_temp_parms parms;
203 parms.cmp = key_compare_it;
204 parms.key_size = sizeof(struct it_key);
205 parms.temp_path = res_get (zi->res, "setTmpDir");
206 parms.rset_term = rset_term_create (term, length, flags);
207 result = rset_create (rset_kind_temp, &parms);
208 result_rsfd = rset_open (result, RSETF_WRITE);
210 if (to - from > merge_chunk)
215 int i, i_add = (to-from)/merge_chunk + 1;
216 struct trunc_info *ti;
218 int rsmax = (to-from)/i_add + 1;
220 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
221 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
223 for (i = from; i < to; i += i_add)
226 rset[rscur] = rset_trunc_r (zi, term, length, flags,
228 merge_chunk, preserve_position);
230 rset[rscur] = rset_trunc_r (zi, term, length, flags,
232 merge_chunk, preserve_position);
235 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
236 for (i = rscur; --i >= 0; )
238 rsfd[i] = rset_open (rset[i], RSETF_READ);
239 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
240 heap_insert (ti, ti->tmpbuf, i);
243 rset_close (rset[i], rsfd[i]);
244 rset_delete (rset[i]);
249 int n = ti->indx[ti->ptr[1]];
251 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
255 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
258 rset_close (rset[n], rsfd[n]);
259 rset_delete (rset[n]);
262 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
265 heap_insert (ti, ti->tmpbuf, n);
274 else if (zi->reg->isam)
278 struct trunc_info *ti;
280 ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
282 ti = heap_init (to-from, sizeof(struct it_key),
284 for (i = to-from; --i >= 0; )
286 ispt[i] = is_position (zi->reg->isam, isam_p[from+i]);
287 if (is_readkey (ispt[i], ti->tmpbuf))
288 heap_insert (ti, ti->tmpbuf, i);
290 is_pt_free (ispt[i]);
294 int n = ti->indx[ti->ptr[1]];
296 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
298 /* section that preserve all keys */
300 if (is_readkey (ispt[n], ti->tmpbuf))
301 heap_insert (ti, ti->tmpbuf, n);
303 is_pt_free (ispt[n]);
305 /* section that preserve all keys with unique sysnos */
308 if (!is_readkey (ispt[n], ti->tmpbuf))
311 is_pt_free (ispt[n]);
314 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
317 heap_insert (ti, ti->tmpbuf, n);
326 else if (zi->reg->isamc)
330 struct trunc_info *ti;
332 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
334 ti = heap_init (to-from, sizeof(struct it_key),
336 for (i = to-from; --i >= 0; )
338 ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
339 if (isc_pp_read (ispt[i], ti->tmpbuf))
340 heap_insert (ti, ti->tmpbuf, i);
342 isc_pp_close (ispt[i]);
346 int n = ti->indx[ti->ptr[1]];
348 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
349 if (preserve_position)
352 if (isc_pp_read (ispt[n], ti->tmpbuf))
353 heap_insert (ti, ti->tmpbuf, n);
355 isc_pp_close (ispt[n]);
361 if (!isc_pp_read (ispt[n], ti->tmpbuf))
364 isc_pp_close (ispt[n]);
367 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
370 heap_insert (ti, ti->tmpbuf, n);
380 else if (zi->reg->isamd)
384 struct trunc_info *ti;
386 ispt = (ISAMD_PP *) xmalloc (sizeof(*ispt) * (to-from));
388 ti = heap_init (to-from, sizeof(struct it_key),
390 for (i = to-from; --i >= 0; )
392 ispt[i] = isamd_pp_open (zi->reg->isamd, isam_p[from+i]);
393 if (isamd_pp_read (ispt[i], ti->tmpbuf))
394 heap_insert (ti, ti->tmpbuf, i);
396 isamd_pp_close (ispt[i]);
400 int n = ti->indx[ti->ptr[1]];
402 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
404 /* section that preserve all keys */
406 if (isamd_pp_read (ispt[n], ti->tmpbuf))
407 heap_insert (ti, ti->tmpbuf, n);
409 isamd_pp_close (ispt[n]);
411 /* section that preserve all keys with unique sysnos */
414 if (!isamd_pp_read (ispt[n], ti->tmpbuf))
417 isamd_pp_close (ispt[n]);
420 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
423 heap_insert (ti, ti->tmpbuf, n);
433 else if (zi->reg->isams)
437 struct trunc_info *ti;
439 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
441 ti = heap_init (to-from, sizeof(struct it_key),
443 for (i = to-from; --i >= 0; )
445 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
446 if (isams_pp_read (ispt[i], ti->tmpbuf))
447 heap_insert (ti, ti->tmpbuf, i);
449 isams_pp_close (ispt[i]);
453 int n = ti->indx[ti->ptr[1]];
455 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
458 if (!isams_pp_read (ispt[n], ti->tmpbuf))
461 isams_pp_close (ispt[n]);
464 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
467 heap_insert (ti, ti->tmpbuf, n);
476 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
478 rset_close (result, result_rsfd);
482 static int isams_trunc_cmp (const void *p1, const void *p2)
484 ISAMS_P i1 = *(ISAMS_P*) p1;
485 ISAMS_P i2 = *(ISAMS_P*) p2;
490 static int isam_trunc_cmp (const void *p1, const void *p2)
492 ISAM_P i1 = *(ISAM_P*) p1;
493 ISAM_P i2 = *(ISAM_P*) p2;
496 d = is_type (i1) - is_type (i2);
499 return is_block (i1) - is_block (i2);
502 static int isamc_trunc_cmp (const void *p1, const void *p2)
504 ISAMC_P i1 = *(ISAMC_P*) p1;
505 ISAMC_P i2 = *(ISAMC_P*) p2;
508 d = isc_type (i1) - isc_type (i2);
511 return isc_block (i1) - isc_block (i2);
513 static int isamd_trunc_cmp (const void *p1, const void *p2)
515 ISAMD_P i1 = *(ISAMD_P*) p1;
516 ISAMD_P i2 = *(ISAMD_P*) p2;
519 d = isamd_type (i1) - isamd_type (i2);
522 return isamd_block (i1) - isamd_block (i2);
525 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
526 const char *term, int length, const char *flags,
527 int preserve_position)
529 logf (LOG_DEBUG, "rset_trunc no=%d", no);
532 rset_null_parms parms;
533 parms.rset_term = rset_term_create (term, length, flags);
534 return rset_create (rset_kind_null, &parms);
540 rset_isams_parms parms;
543 parms.is = zi->reg->isams;
544 parms.rset_term = rset_term_create (term, length, flags);
545 return rset_create (rset_kind_isams, &parms);
547 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
549 else if (zi->reg->isam)
553 rset_isam_parms parms;
556 parms.is = zi->reg->isam;
557 parms.rset_term = rset_term_create (term, length, flags);
558 return rset_create (rset_kind_isam, &parms);
560 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
562 else if (zi->reg->isamc)
566 rset_isamc_parms parms;
568 parms.key_size = sizeof(struct it_key);
569 parms.cmp = key_compare_it;
571 parms.is = zi->reg->isamc;
572 parms.rset_term = rset_term_create (term, length, flags);
573 return rset_create (rset_kind_isamc, &parms);
578 rset_m_or_parms parms;
580 parms.key_size = sizeof(struct it_key);
581 parms.cmp = key_compare_it;
582 parms.isc = zi->reg->isamc;
583 parms.isam_positions = isam_p;
584 parms.no_isam_positions = no;
585 parms.no_save_positions = 100000;
586 parms.rset_term = rset_term_create (term, length, flags);
587 return rset_create (rset_kind_m_or, &parms);
590 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
592 else if (zi->reg->isamd)
596 rset_isamd_parms parms;
599 parms.is = zi->reg->isamd;
600 parms.rset_term = rset_term_create (term, length, flags);
601 return rset_create (rset_kind_isamd, &parms);
603 #if NEW_TRUNC_NOT_DONE_FOR_ISAM_D
606 rset_m_or_parms parms;
608 parms.key_size = sizeof(struct it_key);
609 parms.cmp = key_compare_it;
611 parms.isamd=zi->reg->isamd;
612 parms.isam_positions = isam_p;
613 parms.no_isam_positions = no;
614 parms.no_save_positions = 100000;
615 parms.rset_term = rset_term_create (term, length, flags);
616 return rset_create (rset_kind_m_or, &parms);
619 qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
623 logf (LOG_WARN, "Unknown isam set in rset_trunc");
624 return rset_create (rset_kind_null, NULL);
626 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,