2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1999-05-12 13:08:06 adam
8 * First version of ISAMS.
10 * Revision 1.12 1999/02/02 14:51:10 adam
11 * Updated WIN32 code specific sections. Changed header.
13 * Revision 1.11 1998/03/25 13:48:02 adam
14 * Fixed bug in rset_trunc_r.
16 * Revision 1.10 1998/03/05 08:45:13 adam
17 * New result set model and modular ranking system. Moved towards
18 * descent server API. System information stored as "SGML" records.
20 * Revision 1.9 1998/01/12 15:04:09 adam
21 * The test option (-s) only uses read-lock (and not write lock).
23 * Revision 1.8 1997/10/31 12:34:27 adam
24 * Bug fix: memory leak.
26 * Revision 1.7 1997/09/29 09:07:29 adam
29 * Revision 1.6 1997/09/22 12:39:06 adam
30 * Added get_pos method for the ranked result sets.
32 * Revision 1.5 1997/09/17 12:19:17 adam
33 * Zebra version corresponds to YAZ version 1.4.
34 * Changed Zebra server so that it doesn't depend on global common_resource.
36 * Revision 1.4 1996/12/23 15:30:44 adam
38 * Bug fix: result sets weren't deleted after server shut down.
40 * Revision 1.3 1996/12/20 11:07:14 adam
41 * Multi-or result set.
43 * Revision 1.2 1996/11/08 11:10:28 adam
44 * Buffers used during file match got bigger.
45 * Compressed ISAM support everywhere.
46 * Bug fixes regarding masking characters in queries.
47 * Redesigned Regexp-2 queries.
49 * Revision 1.1 1996/11/04 14:07:40 adam
50 * Moved truncation code to trunc.c.
74 int (*cmp)(const void *p1, const void *p2);
81 static void heap_swap (struct trunc_info *ti, int i1, int i2)
86 ti->ptr[i1] = ti->ptr[i2];
90 static void heap_delete (struct trunc_info *ti)
92 int cur = 1, child = 2;
94 heap_swap (ti, 1, ti->heapnum--);
95 while (child <= ti->heapnum) {
96 if (child < ti->heapnum &&
97 (*ti->cmp)(ti->heap[ti->ptr[child]],
98 ti->heap[ti->ptr[1+child]]) > 0)
100 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
101 ti->heap[ti->ptr[child]]) > 0)
103 heap_swap (ti, cur, child);
112 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
116 cur = ++(ti->heapnum);
117 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
118 ti->indx[ti->ptr[cur]] = indx;
120 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
121 ti->heap[ti->ptr[cur]]) > 0)
123 heap_swap (ti, cur, parent);
129 static struct trunc_info *heap_init (int size, int key_size,
130 int (*cmp)(const void *p1,
133 struct trunc_info *ti = xmalloc (sizeof(*ti));
138 ti->keysize = key_size;
140 ti->indx = xmalloc (size * sizeof(*ti->indx));
141 ti->heap = xmalloc (size * sizeof(*ti->heap));
142 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
143 ti->swapbuf = xmalloc (ti->keysize);
144 ti->tmpbuf = xmalloc (ti->keysize);
145 ti->buf = xmalloc (size * ti->keysize);
146 for (i = size; --i >= 0; )
149 ti->heap[i] = ti->buf + ti->keysize * i;
154 static void heap_close (struct trunc_info *ti)
165 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
166 const char *flags, ISAM_P *isam_p, int from, int to,
171 rset_temp_parms parms;
173 parms.key_size = sizeof(struct it_key);
174 parms.temp_path = res_get (zi->res, "setTmpDir");
175 parms.rset_term = rset_term_create (term, length, flags);
176 result = rset_create (rset_kind_temp, &parms);
177 result_rsfd = rset_open (result, RSETF_WRITE);
179 if (to - from > merge_chunk)
184 int i, i_add = (to-from)/merge_chunk + 1;
185 struct trunc_info *ti;
187 int rsmax = (to-from)/i_add + 1;
189 rset = xmalloc (sizeof(*rset) * rsmax);
190 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
192 for (i = from; i < to; i += i_add)
195 rset[rscur] = rset_trunc_r (zi, term, length, flags,
196 isam_p, i, i+i_add, merge_chunk);
198 rset[rscur] = rset_trunc_r (zi, term, length, flags,
199 isam_p, i, to, merge_chunk);
202 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
203 for (i = rscur; --i >= 0; )
205 rsfd[i] = rset_open (rset[i], RSETF_READ);
206 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
207 heap_insert (ti, ti->tmpbuf, i);
210 rset_close (rset[i], rsfd[i]);
211 rset_delete (rset[i]);
216 int n = ti->indx[ti->ptr[1]];
218 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
222 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
225 rset_close (rset[n], rsfd[n]);
226 rset_delete (rset[n]);
229 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
232 heap_insert (ti, ti->tmpbuf, n);
245 struct trunc_info *ti;
247 ispt = xmalloc (sizeof(*ispt) * (to-from));
249 ti = heap_init (to-from, sizeof(struct it_key),
251 for (i = to-from; --i >= 0; )
253 ispt[i] = is_position (zi->isam, isam_p[from+i]);
254 if (is_readkey (ispt[i], ti->tmpbuf))
255 heap_insert (ti, ti->tmpbuf, i);
257 is_pt_free (ispt[i]);
261 int n = ti->indx[ti->ptr[1]];
263 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
265 /* section that preserve all keys */
267 if (is_readkey (ispt[n], ti->tmpbuf))
268 heap_insert (ti, ti->tmpbuf, n);
270 is_pt_free (ispt[n]);
272 /* section that preserve all keys with unique sysnos */
275 if (!is_readkey (ispt[n], ti->tmpbuf))
278 is_pt_free (ispt[n]);
281 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
284 heap_insert (ti, ti->tmpbuf, n);
297 struct trunc_info *ti;
299 ispt = xmalloc (sizeof(*ispt) * (to-from));
301 ti = heap_init (to-from, sizeof(struct it_key),
303 for (i = to-from; --i >= 0; )
305 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
306 if (isc_pp_read (ispt[i], ti->tmpbuf))
307 heap_insert (ti, ti->tmpbuf, i);
309 isc_pp_close (ispt[i]);
313 int n = ti->indx[ti->ptr[1]];
315 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
317 /* section that preserve all keys */
319 if (isc_pp_read (ispt[n], ti->tmpbuf))
320 heap_insert (ti, ti->tmpbuf, n);
322 isc_pp_close (ispt[n]);
324 /* section that preserve all keys with unique sysnos */
327 if (!isc_pp_read (ispt[n], ti->tmpbuf))
330 isc_pp_close (ispt[n]);
333 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
336 heap_insert (ti, ti->tmpbuf, n);
349 struct trunc_info *ti;
351 ispt = xmalloc (sizeof(*ispt) * (to-from));
353 ti = heap_init (to-from, sizeof(struct it_key),
355 for (i = to-from; --i >= 0; )
357 ispt[i] = isams_pp_open (zi->isams, isam_p[from+i]);
358 if (isams_pp_read (ispt[i], ti->tmpbuf))
359 heap_insert (ti, ti->tmpbuf, i);
361 isams_pp_close (ispt[i]);
365 int n = ti->indx[ti->ptr[1]];
367 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
370 if (!isams_pp_read (ispt[n], ti->tmpbuf))
373 isams_pp_close (ispt[n]);
376 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
379 heap_insert (ti, ti->tmpbuf, n);
387 rset_close (result, result_rsfd);
391 static int isam_trunc_cmp (const void *p1, const void *p2)
393 ISAM_P i1 = *(ISAM_P*) p1;
394 ISAM_P i2 = *(ISAM_P*) p2;
397 d = is_type (i1) - is_type (i2);
400 return is_block (i1) - is_block (i2);
403 static int isamc_trunc_cmp (const void *p1, const void *p2)
405 ISAMC_P i1 = *(ISAMC_P*) p1;
406 ISAMC_P i2 = *(ISAMC_P*) p2;
409 d = isc_type (i1) - isc_type (i2);
412 return isc_block (i1) - isc_block (i2);
415 static int isams_trunc_cmp (const void *p1, const void *p2)
417 ISAMS_P i1 = *(ISAMS_P*) p1;
418 ISAMS_P i2 = *(ISAMS_P*) p2;
423 RSET rset_trunc (ZebraHandle zi, ISAM_P *isam_p, int no,
424 const char *term, int length, const char *flags)
426 logf (LOG_DEBUG, "rset_trunc no=%d", no);
430 return rset_create (rset_kind_null, NULL);
433 rset_isam_parms parms;
437 parms.rset_term = rset_term_create (term, length, flags);
438 return rset_create (rset_kind_isam, &parms);
440 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
445 return rset_create (rset_kind_null, NULL);
448 rset_isamc_parms parms;
451 parms.is = zi->isamc;
452 parms.rset_term = rset_term_create (term, length, flags);
453 return rset_create (rset_kind_isamc, &parms);
458 rset_m_or_parms parms;
460 parms.key_size = sizeof(struct it_key);
461 parms.cmp = key_compare_it;
462 parms.isc = zi->isamc;
463 parms.isam_positions = isam_p;
464 parms.no_isam_positions = no;
465 parms.no_save_positions = 100000;
466 parms.rset_term = rset_term_create (term, length, flags);
467 return rset_create (rset_kind_m_or, &parms);
470 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
475 return rset_create (rset_kind_null, NULL);
478 rset_isams_parms parms;
481 parms.is = zi->isams;
482 parms.rset_term = rset_term_create (term, length, flags);
483 return rset_create (rset_kind_isams, &parms);
485 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
489 logf (LOG_WARN, "Neither isam / isamc / isams set in rset_trunc");
490 return rset_create (rset_kind_null, NULL);
492 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100);