2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1999-05-26 07:49:13 adam
10 * Revision 1.13 1999/05/12 13:08:06 adam
11 * First version of ISAMS.
13 * Revision 1.12 1999/02/02 14:51:10 adam
14 * Updated WIN32 code specific sections. Changed header.
16 * Revision 1.11 1998/03/25 13:48:02 adam
17 * Fixed bug in rset_trunc_r.
19 * Revision 1.10 1998/03/05 08:45:13 adam
20 * New result set model and modular ranking system. Moved towards
21 * descent server API. System information stored as "SGML" records.
23 * Revision 1.9 1998/01/12 15:04:09 adam
24 * The test option (-s) only uses read-lock (and not write lock).
26 * Revision 1.8 1997/10/31 12:34:27 adam
27 * Bug fix: memory leak.
29 * Revision 1.7 1997/09/29 09:07:29 adam
32 * Revision 1.6 1997/09/22 12:39:06 adam
33 * Added get_pos method for the ranked result sets.
35 * Revision 1.5 1997/09/17 12:19:17 adam
36 * Zebra version corresponds to YAZ version 1.4.
37 * Changed Zebra server so that it doesn't depend on global common_resource.
39 * Revision 1.4 1996/12/23 15:30:44 adam
41 * Bug fix: result sets weren't deleted after server shut down.
43 * Revision 1.3 1996/12/20 11:07:14 adam
44 * Multi-or result set.
46 * Revision 1.2 1996/11/08 11:10:28 adam
47 * Buffers used during file match got bigger.
48 * Compressed ISAM support everywhere.
49 * Bug fixes regarding masking characters in queries.
50 * Redesigned Regexp-2 queries.
52 * Revision 1.1 1996/11/04 14:07:40 adam
53 * Moved truncation code to trunc.c.
77 int (*cmp)(const void *p1, const void *p2);
84 static void heap_swap (struct trunc_info *ti, int i1, int i2)
89 ti->ptr[i1] = ti->ptr[i2];
93 static void heap_delete (struct trunc_info *ti)
95 int cur = 1, child = 2;
97 heap_swap (ti, 1, ti->heapnum--);
98 while (child <= ti->heapnum) {
99 if (child < ti->heapnum &&
100 (*ti->cmp)(ti->heap[ti->ptr[child]],
101 ti->heap[ti->ptr[1+child]]) > 0)
103 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
104 ti->heap[ti->ptr[child]]) > 0)
106 heap_swap (ti, cur, child);
115 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
119 cur = ++(ti->heapnum);
120 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
121 ti->indx[ti->ptr[cur]] = indx;
123 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
124 ti->heap[ti->ptr[cur]]) > 0)
126 heap_swap (ti, cur, parent);
132 static struct trunc_info *heap_init (int size, int key_size,
133 int (*cmp)(const void *p1,
136 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
141 ti->keysize = key_size;
143 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
144 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
145 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
146 ti->swapbuf = (char *) xmalloc (ti->keysize);
147 ti->tmpbuf = (char *) xmalloc (ti->keysize);
148 ti->buf = (char *) xmalloc (size * ti->keysize);
149 for (i = size; --i >= 0; )
152 ti->heap[i] = ti->buf + ti->keysize * i;
157 static void heap_close (struct trunc_info *ti)
168 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
169 const char *flags, ISAM_P *isam_p, int from, int to,
174 rset_temp_parms parms;
176 parms.key_size = sizeof(struct it_key);
177 parms.temp_path = res_get (zi->res, "setTmpDir");
178 parms.rset_term = rset_term_create (term, length, flags);
179 result = rset_create (rset_kind_temp, &parms);
180 result_rsfd = rset_open (result, RSETF_WRITE);
182 if (to - from > merge_chunk)
187 int i, i_add = (to-from)/merge_chunk + 1;
188 struct trunc_info *ti;
190 int rsmax = (to-from)/i_add + 1;
192 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
193 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
195 for (i = from; i < to; i += i_add)
198 rset[rscur] = rset_trunc_r (zi, term, length, flags,
199 isam_p, i, i+i_add, merge_chunk);
201 rset[rscur] = rset_trunc_r (zi, term, length, flags,
202 isam_p, i, to, merge_chunk);
205 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
206 for (i = rscur; --i >= 0; )
208 rsfd[i] = rset_open (rset[i], RSETF_READ);
209 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
210 heap_insert (ti, ti->tmpbuf, i);
213 rset_close (rset[i], rsfd[i]);
214 rset_delete (rset[i]);
219 int n = ti->indx[ti->ptr[1]];
221 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
225 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
228 rset_close (rset[n], rsfd[n]);
229 rset_delete (rset[n]);
232 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
235 heap_insert (ti, ti->tmpbuf, n);
248 struct trunc_info *ti;
250 ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
252 ti = heap_init (to-from, sizeof(struct it_key),
254 for (i = to-from; --i >= 0; )
256 ispt[i] = is_position (zi->isam, isam_p[from+i]);
257 if (is_readkey (ispt[i], ti->tmpbuf))
258 heap_insert (ti, ti->tmpbuf, i);
260 is_pt_free (ispt[i]);
264 int n = ti->indx[ti->ptr[1]];
266 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
268 /* section that preserve all keys */
270 if (is_readkey (ispt[n], ti->tmpbuf))
271 heap_insert (ti, ti->tmpbuf, n);
273 is_pt_free (ispt[n]);
275 /* section that preserve all keys with unique sysnos */
278 if (!is_readkey (ispt[n], ti->tmpbuf))
281 is_pt_free (ispt[n]);
284 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
287 heap_insert (ti, ti->tmpbuf, n);
300 struct trunc_info *ti;
302 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
304 ti = heap_init (to-from, sizeof(struct it_key),
306 for (i = to-from; --i >= 0; )
308 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
309 if (isc_pp_read (ispt[i], ti->tmpbuf))
310 heap_insert (ti, ti->tmpbuf, i);
312 isc_pp_close (ispt[i]);
316 int n = ti->indx[ti->ptr[1]];
318 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
320 /* section that preserve all keys */
322 if (isc_pp_read (ispt[n], ti->tmpbuf))
323 heap_insert (ti, ti->tmpbuf, n);
325 isc_pp_close (ispt[n]);
327 /* section that preserve all keys with unique sysnos */
330 if (!isc_pp_read (ispt[n], ti->tmpbuf))
333 isc_pp_close (ispt[n]);
336 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
339 heap_insert (ti, ti->tmpbuf, n);
352 struct trunc_info *ti;
354 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
356 ti = heap_init (to-from, sizeof(struct it_key),
358 for (i = to-from; --i >= 0; )
360 ispt[i] = isams_pp_open (zi->isams, isam_p[from+i]);
361 if (isams_pp_read (ispt[i], ti->tmpbuf))
362 heap_insert (ti, ti->tmpbuf, i);
364 isams_pp_close (ispt[i]);
368 int n = ti->indx[ti->ptr[1]];
370 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
373 if (!isams_pp_read (ispt[n], ti->tmpbuf))
376 isams_pp_close (ispt[n]);
379 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
382 heap_insert (ti, ti->tmpbuf, n);
390 rset_close (result, result_rsfd);
394 static int isam_trunc_cmp (const void *p1, const void *p2)
396 ISAM_P i1 = *(ISAM_P*) p1;
397 ISAM_P i2 = *(ISAM_P*) p2;
400 d = is_type (i1) - is_type (i2);
403 return is_block (i1) - is_block (i2);
406 static int isamc_trunc_cmp (const void *p1, const void *p2)
408 ISAMC_P i1 = *(ISAMC_P*) p1;
409 ISAMC_P i2 = *(ISAMC_P*) p2;
412 d = isc_type (i1) - isc_type (i2);
415 return isc_block (i1) - isc_block (i2);
418 static int isams_trunc_cmp (const void *p1, const void *p2)
420 ISAMS_P i1 = *(ISAMS_P*) p1;
421 ISAMS_P i2 = *(ISAMS_P*) p2;
426 RSET rset_trunc (ZebraHandle zi, ISAM_P *isam_p, int no,
427 const char *term, int length, const char *flags)
429 logf (LOG_DEBUG, "rset_trunc no=%d", no);
433 return rset_create (rset_kind_null, NULL);
436 rset_isam_parms parms;
440 parms.rset_term = rset_term_create (term, length, flags);
441 return rset_create (rset_kind_isam, &parms);
443 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
448 return rset_create (rset_kind_null, NULL);
451 rset_isamc_parms parms;
454 parms.is = zi->isamc;
455 parms.rset_term = rset_term_create (term, length, flags);
456 return rset_create (rset_kind_isamc, &parms);
461 rset_m_or_parms parms;
463 parms.key_size = sizeof(struct it_key);
464 parms.cmp = key_compare_it;
465 parms.isc = zi->isamc;
466 parms.isam_positions = isam_p;
467 parms.no_isam_positions = no;
468 parms.no_save_positions = 100000;
469 parms.rset_term = rset_term_create (term, length, flags);
470 return rset_create (rset_kind_m_or, &parms);
473 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
478 return rset_create (rset_kind_null, NULL);
481 rset_isams_parms parms;
484 parms.is = zi->isams;
485 parms.rset_term = rset_term_create (term, length, flags);
486 return rset_create (rset_kind_isams, &parms);
488 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
492 logf (LOG_WARN, "Neither isam / isamc / isams set in rset_trunc");
493 return rset_create (rset_kind_null, NULL);
495 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100);