2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1998-03-05 08:45:13 adam
8 * New result set model and modular ranking system. Moved towards
9 * descent server API. System information stored as "SGML" records.
11 * Revision 1.9 1998/01/12 15:04:09 adam
12 * The test option (-s) only uses read-lock (and not write lock).
14 * Revision 1.8 1997/10/31 12:34:27 adam
15 * Bug fix: memory leak.
17 * Revision 1.7 1997/09/29 09:07:29 adam
20 * Revision 1.6 1997/09/22 12:39:06 adam
21 * Added get_pos method for the ranked result sets.
23 * Revision 1.5 1997/09/17 12:19:17 adam
24 * Zebra version corresponds to YAZ version 1.4.
25 * Changed Zebra server so that it doesn't depend on global common_resource.
27 * Revision 1.4 1996/12/23 15:30:44 adam
29 * Bug fix: result sets weren't deleted after server shut down.
31 * Revision 1.3 1996/12/20 11:07:14 adam
32 * Multi-or result set.
34 * Revision 1.2 1996/11/08 11:10:28 adam
35 * Buffers used during file match got bigger.
36 * Compressed ISAM support everywhere.
37 * Bug fixes regarding masking characters in queries.
38 * Redesigned Regexp-2 queries.
40 * Revision 1.1 1996/11/04 14:07:40 adam
41 * Moved truncation code to trunc.c.
64 int (*cmp)(const void *p1, const void *p2);
71 static void heap_swap (struct trunc_info *ti, int i1, int i2)
76 ti->ptr[i1] = ti->ptr[i2];
80 static void heap_delete (struct trunc_info *ti)
82 int cur = 1, child = 2;
84 heap_swap (ti, 1, ti->heapnum--);
85 while (child <= ti->heapnum) {
86 if (child < ti->heapnum &&
87 (*ti->cmp)(ti->heap[ti->ptr[child]],
88 ti->heap[ti->ptr[1+child]]) > 0)
90 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
91 ti->heap[ti->ptr[child]]) > 0)
93 heap_swap (ti, cur, child);
102 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
106 cur = ++(ti->heapnum);
107 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
108 ti->indx[ti->ptr[cur]] = indx;
110 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
111 ti->heap[ti->ptr[cur]]) > 0)
113 heap_swap (ti, cur, parent);
119 static struct trunc_info *heap_init (int size, int key_size,
120 int (*cmp)(const void *p1,
123 struct trunc_info *ti = xmalloc (sizeof(*ti));
128 ti->keysize = key_size;
130 ti->indx = xmalloc (size * sizeof(*ti->indx));
131 ti->heap = xmalloc (size * sizeof(*ti->heap));
132 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
133 ti->swapbuf = xmalloc (ti->keysize);
134 ti->tmpbuf = xmalloc (ti->keysize);
135 ti->buf = xmalloc (size * ti->keysize);
136 for (i = size; --i >= 0; )
139 ti->heap[i] = ti->buf + ti->keysize * i;
144 static void heap_close (struct trunc_info *ti)
155 static RSET rset_trunc_r (ZebraHandle zi, ISAM_P *isam_p, int from, int to,
160 rset_temp_parms parms;
162 parms.key_size = sizeof(struct it_key);
163 parms.temp_path = res_get (zi->res, "setTmpDir");
164 result = rset_create (rset_kind_temp, &parms);
165 result_rsfd = rset_open (result, RSETF_WRITE);
167 if (to - from > merge_chunk)
172 int i, i_add = (to-from)/merge_chunk + 1;
173 struct trunc_info *ti;
175 int rsmax = (to-from)/i_add + 1;
177 rset = xmalloc (sizeof(*rset) * rsmax);
178 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
180 for (i = from; i < to; i += i_add)
183 rset[rscur] = rset_trunc_r (zi, isam_p, i, i+i_add,
186 rset[rscur] = rset_trunc_r (zi, isam_p, i, to,
190 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
191 for (i = rscur; --i >= 0; )
193 rsfd[i] = rset_open (rset[i], RSETF_READ);
194 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
195 heap_insert (ti, ti->tmpbuf, i);
198 rset_close (rset[i], rsfd[i]);
199 rset_delete (rset[i]);
204 int n = ti->indx[ti->ptr[1]];
206 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
210 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
213 rset_close (rset[n], rsfd[n]);
214 rset_delete (rset[n]);
217 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
220 heap_insert (ti, ti->tmpbuf, n);
233 struct trunc_info *ti;
235 ispt = xmalloc (sizeof(*ispt) * (to-from));
237 ti = heap_init (to-from, sizeof(struct it_key),
239 for (i = to-from; --i >= 0; )
241 ispt[i] = is_position (zi->isam, isam_p[from+i]);
242 if (is_readkey (ispt[i], ti->tmpbuf))
243 heap_insert (ti, ti->tmpbuf, i);
245 is_pt_free (ispt[i]);
249 int n = ti->indx[ti->ptr[1]];
251 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
253 /* section that preserve all keys */
255 if (is_readkey (ispt[n], ti->tmpbuf))
256 heap_insert (ti, ti->tmpbuf, n);
258 is_pt_free (ispt[n]);
260 /* section that preserve all keys with unique sysnos */
263 if (!is_readkey (ispt[n], ti->tmpbuf))
266 is_pt_free (ispt[n]);
269 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
272 heap_insert (ti, ti->tmpbuf, n);
285 struct trunc_info *ti;
287 ispt = xmalloc (sizeof(*ispt) * (to-from));
289 ti = heap_init (to-from, sizeof(struct it_key),
291 for (i = to-from; --i >= 0; )
293 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
294 if (isc_pp_read (ispt[i], ti->tmpbuf))
295 heap_insert (ti, ti->tmpbuf, i);
297 isc_pp_close (ispt[i]);
301 int n = ti->indx[ti->ptr[1]];
303 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
305 /* section that preserve all keys */
307 if (is_readkey (ispt[n], ti->tmpbuf))
308 heap_insert (ti, ti->tmpbuf, n);
310 isc_pp_close (ispt[n]);
312 /* section that preserve all keys with unique sysnos */
315 if (!isc_pp_read (ispt[n], ti->tmpbuf))
318 isc_pp_close (ispt[n]);
321 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
324 heap_insert (ti, ti->tmpbuf, n);
333 rset_close (result, result_rsfd);
337 static int isam_trunc_cmp (const void *p1, const void *p2)
339 ISAM_P i1 = *(ISAM_P*) p1;
340 ISAM_P i2 = *(ISAM_P*) p2;
343 d = is_type (i1) - is_type (i2);
346 return is_block (i1) - is_block (i2);
349 static int isamc_trunc_cmp (const void *p1, const void *p2)
351 ISAMC_P i1 = *(ISAMC_P*) p1;
352 ISAMC_P i2 = *(ISAMC_P*) p2;
355 d = isc_type (i1) - isc_type (i2);
358 return isc_block (i1) - isc_block (i2);
361 RSET rset_trunc (ZebraHandle zi, ISAM_P *isam_p, int no,
362 const char *term, int length, const char *flags)
364 logf (LOG_DEBUG, "rset_trunc no=%d", no);
368 return rset_create (rset_kind_null, NULL);
371 rset_isam_parms parms;
375 parms.rset_term = rset_term_create (term, length, flags);
376 return rset_create (rset_kind_isam, &parms);
378 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
383 return rset_create (rset_kind_null, NULL);
386 rset_isamc_parms parms;
389 parms.is = zi->isamc;
390 parms.rset_term = rset_term_create (term, length, flags);
391 return rset_create (rset_kind_isamc, &parms);
396 rset_m_or_parms parms;
398 parms.key_size = sizeof(struct it_key);
399 parms.cmp = key_compare_it;
400 parms.isc = zi->isamc;
401 parms.isam_positions = isam_p;
402 parms.no_isam_positions = no;
403 parms.no_save_positions = 100000;
404 parms.rset_term = rset_term_create (term, length, flags);
405 return rset_create (rset_kind_m_or, &parms);
408 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
412 logf (LOG_WARN, "Neither isam nor isamc set in rset_trunc");
413 return rset_create (rset_kind_null, NULL);
415 return rset_trunc_r (zi, isam_p, 0, no, 100);