2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 2001-01-16 16:56:15 heikki
8 * Searching in my isam-d
10 * Revision 1.18 2000/05/18 12:01:36 adam
11 * System call times(2) used again. More 64-bit fixes.
13 * Revision 1.17 2000/03/15 15:00:30 adam
14 * First work on threaded version.
16 * Revision 1.16 1999/11/30 13:48:03 adam
17 * Improved installation. Updated for inclusion of YAZ header files.
19 * Revision 1.15 1999/07/20 13:59:18 adam
20 * Fixed bug that occurred when phrases had 0 hits.
22 * Revision 1.14 1999/05/26 07:49:13 adam
25 * Revision 1.13 1999/05/12 13:08:06 adam
26 * First version of ISAMS.
28 * Revision 1.12 1999/02/02 14:51:10 adam
29 * Updated WIN32 code specific sections. Changed header.
31 * Revision 1.11 1998/03/25 13:48:02 adam
32 * Fixed bug in rset_trunc_r.
34 * Revision 1.10 1998/03/05 08:45:13 adam
35 * New result set model and modular ranking system. Moved towards
36 * descent server API. System information stored as "SGML" records.
38 * Revision 1.9 1998/01/12 15:04:09 adam
39 * The test option (-s) only uses read-lock (and not write lock).
41 * Revision 1.8 1997/10/31 12:34:27 adam
42 * Bug fix: memory leak.
44 * Revision 1.7 1997/09/29 09:07:29 adam
47 * Revision 1.6 1997/09/22 12:39:06 adam
48 * Added get_pos method for the ranked result sets.
50 * Revision 1.5 1997/09/17 12:19:17 adam
51 * Zebra version corresponds to YAZ version 1.4.
52 * Changed Zebra server so that it doesn't depend on global common_resource.
54 * Revision 1.4 1996/12/23 15:30:44 adam
56 * Bug fix: result sets weren't deleted after server shut down.
58 * Revision 1.3 1996/12/20 11:07:14 adam
59 * Multi-or result set.
61 * Revision 1.2 1996/11/08 11:10:28 adam
62 * Buffers used during file match got bigger.
63 * Compressed ISAM support everywhere.
64 * Bug fixes regarding masking characters in queries.
65 * Redesigned Regexp-2 queries.
67 * Revision 1.1 1996/11/04 14:07:40 adam
68 * Moved truncation code to trunc.c.
94 int (*cmp)(const void *p1, const void *p2);
101 static void heap_swap (struct trunc_info *ti, int i1, int i2)
106 ti->ptr[i1] = ti->ptr[i2];
110 static void heap_delete (struct trunc_info *ti)
112 int cur = 1, child = 2;
114 heap_swap (ti, 1, ti->heapnum--);
115 while (child <= ti->heapnum) {
116 if (child < ti->heapnum &&
117 (*ti->cmp)(ti->heap[ti->ptr[child]],
118 ti->heap[ti->ptr[1+child]]) > 0)
120 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
121 ti->heap[ti->ptr[child]]) > 0)
123 heap_swap (ti, cur, child);
132 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
136 cur = ++(ti->heapnum);
137 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
138 ti->indx[ti->ptr[cur]] = indx;
140 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
141 ti->heap[ti->ptr[cur]]) > 0)
143 heap_swap (ti, cur, parent);
149 static struct trunc_info *heap_init (int size, int key_size,
150 int (*cmp)(const void *p1,
153 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
158 ti->keysize = key_size;
160 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
161 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
162 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
163 ti->swapbuf = (char *) xmalloc (ti->keysize);
164 ti->tmpbuf = (char *) xmalloc (ti->keysize);
165 ti->buf = (char *) xmalloc (size * ti->keysize);
166 for (i = size; --i >= 0; )
169 ti->heap[i] = ti->buf + ti->keysize * i;
174 static void heap_close (struct trunc_info *ti)
185 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
186 const char *flags, ISAMS_P *isam_p, int from, int to,
191 rset_temp_parms parms;
193 parms.key_size = sizeof(struct it_key);
194 parms.temp_path = res_get (zi->service->res, "setTmpDir");
195 parms.rset_term = rset_term_create (term, length, flags);
196 result = rset_create (rset_kind_temp, &parms);
197 result_rsfd = rset_open (result, RSETF_WRITE);
199 if (to - from > merge_chunk)
204 int i, i_add = (to-from)/merge_chunk + 1;
205 struct trunc_info *ti;
207 int rsmax = (to-from)/i_add + 1;
209 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
210 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
212 for (i = from; i < to; i += i_add)
215 rset[rscur] = rset_trunc_r (zi, term, length, flags,
216 isam_p, i, i+i_add, merge_chunk);
218 rset[rscur] = rset_trunc_r (zi, term, length, flags,
219 isam_p, i, to, merge_chunk);
222 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
223 for (i = rscur; --i >= 0; )
225 rsfd[i] = rset_open (rset[i], RSETF_READ);
226 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
227 heap_insert (ti, ti->tmpbuf, i);
230 rset_close (rset[i], rsfd[i]);
231 rset_delete (rset[i]);
236 int n = ti->indx[ti->ptr[1]];
238 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
242 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
245 rset_close (rset[n], rsfd[n]);
246 rset_delete (rset[n]);
249 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
252 heap_insert (ti, ti->tmpbuf, n);
262 else if (zi->service->isam)
266 struct trunc_info *ti;
268 ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
270 ti = heap_init (to-from, sizeof(struct it_key),
272 for (i = to-from; --i >= 0; )
274 ispt[i] = is_position (zi->service->isam, isam_p[from+i]);
275 if (is_readkey (ispt[i], ti->tmpbuf))
276 heap_insert (ti, ti->tmpbuf, i);
278 is_pt_free (ispt[i]);
282 int n = ti->indx[ti->ptr[1]];
284 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
286 /* section that preserve all keys */
288 if (is_readkey (ispt[n], ti->tmpbuf))
289 heap_insert (ti, ti->tmpbuf, n);
291 is_pt_free (ispt[n]);
293 /* section that preserve all keys with unique sysnos */
296 if (!is_readkey (ispt[n], ti->tmpbuf))
299 is_pt_free (ispt[n]);
302 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
305 heap_insert (ti, ti->tmpbuf, n);
314 else if (zi->service->isamc)
318 struct trunc_info *ti;
320 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
322 ti = heap_init (to-from, sizeof(struct it_key),
324 for (i = to-from; --i >= 0; )
326 ispt[i] = isc_pp_open (zi->service->isamc, isam_p[from+i]);
327 if (isc_pp_read (ispt[i], ti->tmpbuf))
328 heap_insert (ti, ti->tmpbuf, i);
330 isc_pp_close (ispt[i]);
334 int n = ti->indx[ti->ptr[1]];
336 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
338 /* section that preserve all keys */
340 if (isc_pp_read (ispt[n], ti->tmpbuf))
341 heap_insert (ti, ti->tmpbuf, n);
343 isc_pp_close (ispt[n]);
345 /* section that preserve all keys with unique sysnos */
348 if (!isc_pp_read (ispt[n], ti->tmpbuf))
351 isc_pp_close (ispt[n]);
354 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
357 heap_insert (ti, ti->tmpbuf, n);
367 else if (zi->service->isamd)
371 struct trunc_info *ti;
373 ispt = (ISAMD_PP *) xmalloc (sizeof(*ispt) * (to-from));
375 ti = heap_init (to-from, sizeof(struct it_key),
377 for (i = to-from; --i >= 0; )
379 ispt[i] = isamd_pp_open (zi->service->isamd, isam_p[from+i]);
380 if (isamd_pp_read (ispt[i], ti->tmpbuf))
381 heap_insert (ti, ti->tmpbuf, i);
383 isamd_pp_close (ispt[i]);
387 int n = ti->indx[ti->ptr[1]];
389 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
391 /* section that preserve all keys */
393 if (isamd_pp_read (ispt[n], ti->tmpbuf))
394 heap_insert (ti, ti->tmpbuf, n);
396 isamd_pp_close (ispt[n]);
398 /* section that preserve all keys with unique sysnos */
401 if (!isamd_pp_read (ispt[n], ti->tmpbuf))
404 isamd_pp_close (ispt[n]);
407 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
410 heap_insert (ti, ti->tmpbuf, n);
421 else if (zi->service->isams)
425 struct trunc_info *ti;
427 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
429 ti = heap_init (to-from, sizeof(struct it_key),
431 for (i = to-from; --i >= 0; )
433 ispt[i] = isams_pp_open (zi->service->isams, isam_p[from+i]);
434 if (isams_pp_read (ispt[i], ti->tmpbuf))
435 heap_insert (ti, ti->tmpbuf, i);
437 isams_pp_close (ispt[i]);
441 int n = ti->indx[ti->ptr[1]];
443 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
446 if (!isams_pp_read (ispt[n], ti->tmpbuf))
449 isams_pp_close (ispt[n]);
452 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
455 heap_insert (ti, ti->tmpbuf, n);
464 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
466 rset_close (result, result_rsfd);
470 static int isams_trunc_cmp (const void *p1, const void *p2)
472 ISAMS_P i1 = *(ISAMS_P*) p1;
473 ISAMS_P i2 = *(ISAMS_P*) p2;
479 static int isam_trunc_cmp (const void *p1, const void *p2)
481 ISAM_P i1 = *(ISAM_P*) p1;
482 ISAM_P i2 = *(ISAM_P*) p2;
485 d = is_type (i1) - is_type (i2);
488 return is_block (i1) - is_block (i2);
491 static int isamc_trunc_cmp (const void *p1, const void *p2)
493 ISAMC_P i1 = *(ISAMC_P*) p1;
494 ISAMC_P i2 = *(ISAMC_P*) p2;
497 d = isc_type (i1) - isc_type (i2);
500 return isc_block (i1) - isc_block (i2);
502 static int isamd_trunc_cmp (const void *p1, const void *p2)
504 ISAMD_P i1 = *(ISAMD_P*) p1;
505 ISAMD_P i2 = *(ISAMD_P*) p2;
508 d = isamd_type (i1) - isamd_type (i2);
511 return isamd_block (i1) - isamd_block (i2);
515 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
516 const char *term, int length, const char *flags)
518 logf (LOG_DEBUG, "rset_trunc no=%d", no);
521 rset_null_parms parms;
522 parms.rset_term = rset_term_create (term, length, flags);
523 return rset_create (rset_kind_null, &parms);
525 if (zi->service->isams)
529 rset_isams_parms parms;
532 parms.is = zi->service->isams;
533 parms.rset_term = rset_term_create (term, length, flags);
534 return rset_create (rset_kind_isams, &parms);
536 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
539 else if (zi->service->isam)
543 rset_isam_parms parms;
546 parms.is = zi->service->isam;
547 parms.rset_term = rset_term_create (term, length, flags);
548 return rset_create (rset_kind_isam, &parms);
550 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
552 else if (zi->service->isamc)
556 rset_isamc_parms parms;
559 parms.is = zi->service->isamc;
560 parms.rset_term = rset_term_create (term, length, flags);
561 return rset_create (rset_kind_isamc, &parms);
566 rset_m_or_parms parms;
568 parms.key_size = sizeof(struct it_key);
569 parms.cmp = key_compare_it;
570 parms.isc = zi->service->isamc;
571 parms.isam_positions = isam_p;
572 parms.no_isam_positions = no;
573 parms.no_save_positions = 100000;
574 parms.rset_term = rset_term_create (term, length, flags);
575 return rset_create (rset_kind_m_or, &parms);
578 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
580 else if (zi->service->isamd)
584 rset_isamd_parms parms;
587 parms.is = zi->service->isamd;
588 parms.rset_term = rset_term_create (term, length, flags);
589 return rset_create (rset_kind_isamd, &parms);
591 #if NEW_TRUNC_NOT_DONE_FOR_ISAM_D
594 rset_m_or_parms parms;
596 parms.key_size = sizeof(struct it_key);
597 parms.cmp = key_compare_it;
599 parms.isamd=zi->service->isamd;
600 parms.isam_positions = isam_p;
601 parms.no_isam_positions = no;
602 parms.no_save_positions = 100000;
603 parms.rset_term = rset_term_create (term, length, flags);
604 return rset_create (rset_kind_m_or, &parms);
607 qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
612 logf (LOG_WARN, "Unknown isam set in rset_trunc");
613 return rset_create (rset_kind_null, NULL);
615 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100);