2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1998-03-25 13:48:02 adam
8 * Fixed bug in rset_trunc_r.
10 * Revision 1.10 1998/03/05 08:45:13 adam
11 * New result set model and modular ranking system. Moved towards
12 * descent server API. System information stored as "SGML" records.
14 * Revision 1.9 1998/01/12 15:04:09 adam
15 * The test option (-s) only uses read-lock (and not write lock).
17 * Revision 1.8 1997/10/31 12:34:27 adam
18 * Bug fix: memory leak.
20 * Revision 1.7 1997/09/29 09:07:29 adam
23 * Revision 1.6 1997/09/22 12:39:06 adam
24 * Added get_pos method for the ranked result sets.
26 * Revision 1.5 1997/09/17 12:19:17 adam
27 * Zebra version corresponds to YAZ version 1.4.
28 * Changed Zebra server so that it doesn't depend on global common_resource.
30 * Revision 1.4 1996/12/23 15:30:44 adam
32 * Bug fix: result sets weren't deleted after server shut down.
34 * Revision 1.3 1996/12/20 11:07:14 adam
35 * Multi-or result set.
37 * Revision 1.2 1996/11/08 11:10:28 adam
38 * Buffers used during file match got bigger.
39 * Compressed ISAM support everywhere.
40 * Bug fixes regarding masking characters in queries.
41 * Redesigned Regexp-2 queries.
43 * Revision 1.1 1996/11/04 14:07:40 adam
44 * Moved truncation code to trunc.c.
67 int (*cmp)(const void *p1, const void *p2);
74 static void heap_swap (struct trunc_info *ti, int i1, int i2)
79 ti->ptr[i1] = ti->ptr[i2];
83 static void heap_delete (struct trunc_info *ti)
85 int cur = 1, child = 2;
87 heap_swap (ti, 1, ti->heapnum--);
88 while (child <= ti->heapnum) {
89 if (child < ti->heapnum &&
90 (*ti->cmp)(ti->heap[ti->ptr[child]],
91 ti->heap[ti->ptr[1+child]]) > 0)
93 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
94 ti->heap[ti->ptr[child]]) > 0)
96 heap_swap (ti, cur, child);
105 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
109 cur = ++(ti->heapnum);
110 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
111 ti->indx[ti->ptr[cur]] = indx;
113 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
114 ti->heap[ti->ptr[cur]]) > 0)
116 heap_swap (ti, cur, parent);
122 static struct trunc_info *heap_init (int size, int key_size,
123 int (*cmp)(const void *p1,
126 struct trunc_info *ti = xmalloc (sizeof(*ti));
131 ti->keysize = key_size;
133 ti->indx = xmalloc (size * sizeof(*ti->indx));
134 ti->heap = xmalloc (size * sizeof(*ti->heap));
135 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
136 ti->swapbuf = xmalloc (ti->keysize);
137 ti->tmpbuf = xmalloc (ti->keysize);
138 ti->buf = xmalloc (size * ti->keysize);
139 for (i = size; --i >= 0; )
142 ti->heap[i] = ti->buf + ti->keysize * i;
147 static void heap_close (struct trunc_info *ti)
158 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
159 const char *flags, ISAM_P *isam_p, int from, int to,
164 rset_temp_parms parms;
166 parms.key_size = sizeof(struct it_key);
167 parms.temp_path = res_get (zi->res, "setTmpDir");
168 parms.rset_term = rset_term_create (term, length, flags);
169 result = rset_create (rset_kind_temp, &parms);
170 result_rsfd = rset_open (result, RSETF_WRITE);
172 if (to - from > merge_chunk)
177 int i, i_add = (to-from)/merge_chunk + 1;
178 struct trunc_info *ti;
180 int rsmax = (to-from)/i_add + 1;
182 rset = xmalloc (sizeof(*rset) * rsmax);
183 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
185 for (i = from; i < to; i += i_add)
188 rset[rscur] = rset_trunc_r (zi, term, length, flags,
189 isam_p, i, i+i_add, merge_chunk);
191 rset[rscur] = rset_trunc_r (zi, term, length, flags,
192 isam_p, i, to, merge_chunk);
195 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
196 for (i = rscur; --i >= 0; )
198 rsfd[i] = rset_open (rset[i], RSETF_READ);
199 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
200 heap_insert (ti, ti->tmpbuf, i);
203 rset_close (rset[i], rsfd[i]);
204 rset_delete (rset[i]);
209 int n = ti->indx[ti->ptr[1]];
211 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
215 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
218 rset_close (rset[n], rsfd[n]);
219 rset_delete (rset[n]);
222 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
225 heap_insert (ti, ti->tmpbuf, n);
238 struct trunc_info *ti;
240 ispt = xmalloc (sizeof(*ispt) * (to-from));
242 ti = heap_init (to-from, sizeof(struct it_key),
244 for (i = to-from; --i >= 0; )
246 ispt[i] = is_position (zi->isam, isam_p[from+i]);
247 if (is_readkey (ispt[i], ti->tmpbuf))
248 heap_insert (ti, ti->tmpbuf, i);
250 is_pt_free (ispt[i]);
254 int n = ti->indx[ti->ptr[1]];
256 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
258 /* section that preserve all keys */
260 if (is_readkey (ispt[n], ti->tmpbuf))
261 heap_insert (ti, ti->tmpbuf, n);
263 is_pt_free (ispt[n]);
265 /* section that preserve all keys with unique sysnos */
268 if (!is_readkey (ispt[n], ti->tmpbuf))
271 is_pt_free (ispt[n]);
274 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
277 heap_insert (ti, ti->tmpbuf, n);
290 struct trunc_info *ti;
292 ispt = xmalloc (sizeof(*ispt) * (to-from));
294 ti = heap_init (to-from, sizeof(struct it_key),
296 for (i = to-from; --i >= 0; )
298 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
299 if (isc_pp_read (ispt[i], ti->tmpbuf))
300 heap_insert (ti, ti->tmpbuf, i);
302 isc_pp_close (ispt[i]);
306 int n = ti->indx[ti->ptr[1]];
308 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
310 /* section that preserve all keys */
312 if (is_readkey (ispt[n], ti->tmpbuf))
313 heap_insert (ti, ti->tmpbuf, n);
315 isc_pp_close (ispt[n]);
317 /* section that preserve all keys with unique sysnos */
320 if (!isc_pp_read (ispt[n], ti->tmpbuf))
323 isc_pp_close (ispt[n]);
326 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
329 heap_insert (ti, ti->tmpbuf, n);
338 rset_close (result, result_rsfd);
342 static int isam_trunc_cmp (const void *p1, const void *p2)
344 ISAM_P i1 = *(ISAM_P*) p1;
345 ISAM_P i2 = *(ISAM_P*) p2;
348 d = is_type (i1) - is_type (i2);
351 return is_block (i1) - is_block (i2);
354 static int isamc_trunc_cmp (const void *p1, const void *p2)
356 ISAMC_P i1 = *(ISAMC_P*) p1;
357 ISAMC_P i2 = *(ISAMC_P*) p2;
360 d = isc_type (i1) - isc_type (i2);
363 return isc_block (i1) - isc_block (i2);
366 RSET rset_trunc (ZebraHandle zi, ISAM_P *isam_p, int no,
367 const char *term, int length, const char *flags)
369 logf (LOG_DEBUG, "rset_trunc no=%d", no);
373 return rset_create (rset_kind_null, NULL);
376 rset_isam_parms parms;
380 parms.rset_term = rset_term_create (term, length, flags);
381 return rset_create (rset_kind_isam, &parms);
383 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
388 return rset_create (rset_kind_null, NULL);
391 rset_isamc_parms parms;
394 parms.is = zi->isamc;
395 parms.rset_term = rset_term_create (term, length, flags);
396 return rset_create (rset_kind_isamc, &parms);
401 rset_m_or_parms parms;
403 parms.key_size = sizeof(struct it_key);
404 parms.cmp = key_compare_it;
405 parms.isc = zi->isamc;
406 parms.isam_positions = isam_p;
407 parms.no_isam_positions = no;
408 parms.no_save_positions = 100000;
409 parms.rset_term = rset_term_create (term, length, flags);
410 return rset_create (rset_kind_m_or, &parms);
413 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
417 logf (LOG_WARN, "Neither isam nor isamc set in rset_trunc");
418 return rset_create (rset_kind_null, NULL);
420 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100);