2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1999-02-02 14:51:10 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.11 1998/03/25 13:48:02 adam
11 * Fixed bug in rset_trunc_r.
13 * Revision 1.10 1998/03/05 08:45:13 adam
14 * New result set model and modular ranking system. Moved towards
15 * descent server API. System information stored as "SGML" records.
17 * Revision 1.9 1998/01/12 15:04:09 adam
18 * The test option (-s) only uses read-lock (and not write lock).
20 * Revision 1.8 1997/10/31 12:34:27 adam
21 * Bug fix: memory leak.
23 * Revision 1.7 1997/09/29 09:07:29 adam
26 * Revision 1.6 1997/09/22 12:39:06 adam
27 * Added get_pos method for the ranked result sets.
29 * Revision 1.5 1997/09/17 12:19:17 adam
30 * Zebra version corresponds to YAZ version 1.4.
31 * Changed Zebra server so that it doesn't depend on global common_resource.
33 * Revision 1.4 1996/12/23 15:30:44 adam
35 * Bug fix: result sets weren't deleted after server shut down.
37 * Revision 1.3 1996/12/20 11:07:14 adam
38 * Multi-or result set.
40 * Revision 1.2 1996/11/08 11:10:28 adam
41 * Buffers used during file match got bigger.
42 * Compressed ISAM support everywhere.
43 * Bug fixes regarding masking characters in queries.
44 * Redesigned Regexp-2 queries.
46 * Revision 1.1 1996/11/04 14:07:40 adam
47 * Moved truncation code to trunc.c.
70 int (*cmp)(const void *p1, const void *p2);
77 static void heap_swap (struct trunc_info *ti, int i1, int i2)
82 ti->ptr[i1] = ti->ptr[i2];
86 static void heap_delete (struct trunc_info *ti)
88 int cur = 1, child = 2;
90 heap_swap (ti, 1, ti->heapnum--);
91 while (child <= ti->heapnum) {
92 if (child < ti->heapnum &&
93 (*ti->cmp)(ti->heap[ti->ptr[child]],
94 ti->heap[ti->ptr[1+child]]) > 0)
96 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
97 ti->heap[ti->ptr[child]]) > 0)
99 heap_swap (ti, cur, child);
108 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
112 cur = ++(ti->heapnum);
113 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
114 ti->indx[ti->ptr[cur]] = indx;
116 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
117 ti->heap[ti->ptr[cur]]) > 0)
119 heap_swap (ti, cur, parent);
125 static struct trunc_info *heap_init (int size, int key_size,
126 int (*cmp)(const void *p1,
129 struct trunc_info *ti = xmalloc (sizeof(*ti));
134 ti->keysize = key_size;
136 ti->indx = xmalloc (size * sizeof(*ti->indx));
137 ti->heap = xmalloc (size * sizeof(*ti->heap));
138 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
139 ti->swapbuf = xmalloc (ti->keysize);
140 ti->tmpbuf = xmalloc (ti->keysize);
141 ti->buf = xmalloc (size * ti->keysize);
142 for (i = size; --i >= 0; )
145 ti->heap[i] = ti->buf + ti->keysize * i;
150 static void heap_close (struct trunc_info *ti)
161 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
162 const char *flags, ISAM_P *isam_p, int from, int to,
167 rset_temp_parms parms;
169 parms.key_size = sizeof(struct it_key);
170 parms.temp_path = res_get (zi->res, "setTmpDir");
171 parms.rset_term = rset_term_create (term, length, flags);
172 result = rset_create (rset_kind_temp, &parms);
173 result_rsfd = rset_open (result, RSETF_WRITE);
175 if (to - from > merge_chunk)
180 int i, i_add = (to-from)/merge_chunk + 1;
181 struct trunc_info *ti;
183 int rsmax = (to-from)/i_add + 1;
185 rset = xmalloc (sizeof(*rset) * rsmax);
186 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
188 for (i = from; i < to; i += i_add)
191 rset[rscur] = rset_trunc_r (zi, term, length, flags,
192 isam_p, i, i+i_add, merge_chunk);
194 rset[rscur] = rset_trunc_r (zi, term, length, flags,
195 isam_p, i, to, merge_chunk);
198 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
199 for (i = rscur; --i >= 0; )
201 rsfd[i] = rset_open (rset[i], RSETF_READ);
202 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
203 heap_insert (ti, ti->tmpbuf, i);
206 rset_close (rset[i], rsfd[i]);
207 rset_delete (rset[i]);
212 int n = ti->indx[ti->ptr[1]];
214 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
218 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
221 rset_close (rset[n], rsfd[n]);
222 rset_delete (rset[n]);
225 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
228 heap_insert (ti, ti->tmpbuf, n);
241 struct trunc_info *ti;
243 ispt = xmalloc (sizeof(*ispt) * (to-from));
245 ti = heap_init (to-from, sizeof(struct it_key),
247 for (i = to-from; --i >= 0; )
249 ispt[i] = is_position (zi->isam, isam_p[from+i]);
250 if (is_readkey (ispt[i], ti->tmpbuf))
251 heap_insert (ti, ti->tmpbuf, i);
253 is_pt_free (ispt[i]);
257 int n = ti->indx[ti->ptr[1]];
259 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
261 /* section that preserve all keys */
263 if (is_readkey (ispt[n], ti->tmpbuf))
264 heap_insert (ti, ti->tmpbuf, n);
266 is_pt_free (ispt[n]);
268 /* section that preserve all keys with unique sysnos */
271 if (!is_readkey (ispt[n], ti->tmpbuf))
274 is_pt_free (ispt[n]);
277 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
280 heap_insert (ti, ti->tmpbuf, n);
293 struct trunc_info *ti;
295 ispt = xmalloc (sizeof(*ispt) * (to-from));
297 ti = heap_init (to-from, sizeof(struct it_key),
299 for (i = to-from; --i >= 0; )
301 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
302 if (isc_pp_read (ispt[i], ti->tmpbuf))
303 heap_insert (ti, ti->tmpbuf, i);
305 isc_pp_close (ispt[i]);
309 int n = ti->indx[ti->ptr[1]];
311 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
313 /* section that preserve all keys */
315 if (is_readkey (ispt[n], ti->tmpbuf))
316 heap_insert (ti, ti->tmpbuf, n);
318 isc_pp_close (ispt[n]);
320 /* section that preserve all keys with unique sysnos */
323 if (!isc_pp_read (ispt[n], ti->tmpbuf))
326 isc_pp_close (ispt[n]);
329 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
332 heap_insert (ti, ti->tmpbuf, n);
341 rset_close (result, result_rsfd);
345 static int isam_trunc_cmp (const void *p1, const void *p2)
347 ISAM_P i1 = *(ISAM_P*) p1;
348 ISAM_P i2 = *(ISAM_P*) p2;
351 d = is_type (i1) - is_type (i2);
354 return is_block (i1) - is_block (i2);
357 static int isamc_trunc_cmp (const void *p1, const void *p2)
359 ISAMC_P i1 = *(ISAMC_P*) p1;
360 ISAMC_P i2 = *(ISAMC_P*) p2;
363 d = isc_type (i1) - isc_type (i2);
366 return isc_block (i1) - isc_block (i2);
369 RSET rset_trunc (ZebraHandle zi, ISAM_P *isam_p, int no,
370 const char *term, int length, const char *flags)
372 logf (LOG_DEBUG, "rset_trunc no=%d", no);
376 return rset_create (rset_kind_null, NULL);
379 rset_isam_parms parms;
383 parms.rset_term = rset_term_create (term, length, flags);
384 return rset_create (rset_kind_isam, &parms);
386 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
391 return rset_create (rset_kind_null, NULL);
394 rset_isamc_parms parms;
397 parms.is = zi->isamc;
398 parms.rset_term = rset_term_create (term, length, flags);
399 return rset_create (rset_kind_isamc, &parms);
404 rset_m_or_parms parms;
406 parms.key_size = sizeof(struct it_key);
407 parms.cmp = key_compare_it;
408 parms.isc = zi->isamc;
409 parms.isam_positions = isam_p;
410 parms.no_isam_positions = no;
411 parms.no_save_positions = 100000;
412 parms.rset_term = rset_term_create (term, length, flags);
413 return rset_create (rset_kind_m_or, &parms);
416 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
420 logf (LOG_WARN, "Neither isam nor isamc set in rset_trunc");
421 return rset_create (rset_kind_null, NULL);
423 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100);