1 /* $Id: trunc.c,v 1.49 2004-11-19 10:27:03 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
35 int (*cmp)(const void *p1, const void *p2);
42 static void heap_swap (struct trunc_info *ti, int i1, int i2)
47 ti->ptr[i1] = ti->ptr[i2];
51 static void heap_delete (struct trunc_info *ti)
53 int cur = 1, child = 2;
55 heap_swap (ti, 1, ti->heapnum--);
56 while (child <= ti->heapnum) {
57 if (child < ti->heapnum &&
58 (*ti->cmp)(ti->heap[ti->ptr[child]],
59 ti->heap[ti->ptr[1+child]]) > 0)
61 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
62 ti->heap[ti->ptr[child]]) > 0)
64 heap_swap (ti, cur, child);
73 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
77 cur = ++(ti->heapnum);
78 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
79 ti->indx[ti->ptr[cur]] = indx;
81 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
82 ti->heap[ti->ptr[cur]]) > 0)
84 heap_swap (ti, cur, parent);
90 static struct trunc_info *heap_init (int size, int key_size,
91 int (*cmp)(const void *p1,
94 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
99 ti->keysize = key_size;
101 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
102 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
103 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
104 ti->swapbuf = (char *) xmalloc (ti->keysize);
105 ti->tmpbuf = (char *) xmalloc (ti->keysize);
106 ti->buf = (char *) xmalloc (size * ti->keysize);
107 for (i = size; --i >= 0; )
110 ti->heap[i] = ti->buf + ti->keysize * i;
115 static void heap_close (struct trunc_info *ti)
126 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
127 const char *flags, ISAMS_P *isam_p, int from, int to,
128 int merge_chunk, int preserve_position,
129 int term_type, NMEM rset_nmem,
130 const struct key_control *kctrl, int scope,
138 rset_temp_parms parms;
139 parms.cmp = key_compare_it;
140 parms.key_size = sizeof(struct it_key);
141 parms.temp_path = res_get (zi->res, "setTmpDir");
142 result = rset_create (rset_kind_temp, &parms);
144 result=rstemp_create( rset_nmem,kctrl, scope,
145 res_get (zi->res, "setTmpDir"), termid);
146 result_rsfd = rset_open (result, RSETF_WRITE);
148 if (to - from > merge_chunk)
152 int i, i_add = (to-from)/merge_chunk + 1;
153 struct trunc_info *ti;
155 int rsmax = (to-from)/i_add + 1;
157 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
158 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
160 for (i = from; i < to; i += i_add)
163 rset[rscur] = rset_trunc_r (zi, term, length, flags,
165 merge_chunk, preserve_position,
166 term_type, rset_nmem,
167 kctrl, scope,termid);
169 rset[rscur] = rset_trunc_r (zi, term, length, flags,
171 merge_chunk, preserve_position,
172 term_type, rset_nmem,
173 kctrl, scope,termid);
176 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
177 for (i = rscur; --i >= 0; )
179 rsfd[i] = rset_open (rset[i], RSETF_READ);
180 if (rset_read(rsfd[i], ti->tmpbuf,0))
181 heap_insert (ti, ti->tmpbuf, i);
184 rset_close (rsfd[i]);
185 rset_delete (rset[i]);
190 int n = ti->indx[ti->ptr[1]];
192 rset_write (result_rsfd, ti->heap[ti->ptr[1]]);
197 if (!rset_read (rsfd[n], ti->tmpbuf,0))
200 rset_close (rsfd[n]);
201 rset_delete (rset[n]);
204 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
207 heap_insert (ti, ti->tmpbuf, n);
216 else if (zi->reg->isamc)
220 struct trunc_info *ti;
222 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
224 ti = heap_init (to-from, sizeof(struct it_key),
226 for (i = to-from; --i >= 0; )
228 ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
229 if (isc_pp_read (ispt[i], ti->tmpbuf))
230 heap_insert (ti, ti->tmpbuf, i);
232 isc_pp_close (ispt[i]);
236 int n = ti->indx[ti->ptr[1]];
238 rset_write (result_rsfd, ti->heap[ti->ptr[1]]);
240 if (preserve_position)
243 if (isc_pp_read (ispt[n], ti->tmpbuf))
244 heap_insert (ti, ti->tmpbuf, n);
246 isc_pp_close (ispt[n]);
252 if (!isc_pp_read (ispt[n], ti->tmpbuf))
255 isc_pp_close (ispt[n]);
258 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
261 heap_insert (ti, ti->tmpbuf, n);
270 else if (zi->reg->isams)
274 struct trunc_info *ti;
277 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
279 ti = heap_init (to-from, sizeof(struct it_key),
281 for (i = to-from; --i >= 0; )
283 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
284 if (isams_pp_read (ispt[i], ti->tmpbuf))
285 heap_insert (ti, ti->tmpbuf, i);
287 isams_pp_close (ispt[i]);
291 int n = ti->indx[ti->ptr[1]];
293 rset_write (result_rsfd, ti->heap[ti->ptr[1]]);
297 if (!isams_pp_read (ispt[n], ti->tmpbuf))
300 isams_pp_close (ispt[n]);
303 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
306 heap_insert (ti, ti->tmpbuf, n);
314 else if (zi->reg->isamb)
318 struct trunc_info *ti;
320 ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
322 ti = heap_init (to-from, sizeof(struct it_key),
324 for (i = to-from; --i >= 0; )
326 if (isam_p[from+i]) {
327 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i], scope);
328 if (isamb_pp_read (ispt[i], ti->tmpbuf))
329 heap_insert (ti, ti->tmpbuf, i);
331 isamb_pp_close (ispt[i]);
336 int n = ti->indx[ti->ptr[1]];
338 rset_write (result_rsfd, ti->heap[ti->ptr[1]]);
341 if (preserve_position)
344 if (isamb_pp_read (ispt[n], ti->tmpbuf))
345 heap_insert (ti, ti->tmpbuf, n);
347 isamb_pp_close (ispt[n]);
353 if (!isamb_pp_read (ispt[n], ti->tmpbuf))
356 isamb_pp_close (ispt[n]);
359 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
362 heap_insert (ti, ti->tmpbuf, n);
372 yaz_log (YLOG_WARN, "Unknown isam set in rset_trunc_r");
374 rset_close (result_rsfd);
378 static int isams_trunc_cmp (const void *p1, const void *p2)
380 ISAMS_P i1 = *(ISAMS_P*) p1;
381 ISAMS_P i2 = *(ISAMS_P*) p2;
390 static int isamc_trunc_cmp (const void *p1, const void *p2)
392 ISAMC_P i1 = *(ISAMC_P*) p1;
393 ISAMC_P i2 = *(ISAMC_P*) p2;
396 d = (isc_type (i1) - isc_type (i2));
398 d = isc_block (i1) - isc_block (i2);
406 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
407 const char *term, int length, const char *flags,
408 int preserve_position, int term_type, NMEM rset_nmem,
409 const struct key_control *kctrl, int scope)
412 yaz_log (YLOG_DEBUG, "rset_trunc no=%d", no);
414 return rsnull_create (rset_nmem,kctrl);
415 termid=rset_term_create(term, length, flags, term_type,rset_nmem);
419 return rsisams_create(rset_nmem, kctrl, scope,
420 zi->reg->isams, *isam_p, termid);
421 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
423 else if (zi->reg->isamc)
426 return rsisamc_create(rset_nmem, kctrl, scope,
427 zi->reg->isamc, *isam_p, termid);
428 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
430 else if (zi->reg->isamb)
433 return rsisamb_create(rset_nmem,kctrl, scope,
434 zi->reg->isamb, *isam_p, termid);
435 else if (no <10000 ) /* FIXME - hardcoded number */
438 RSET *rsets=xmalloc(no*sizeof(RSET)); /* use nmem! */
441 rsets[i]=rsisamb_create(rset_nmem, kctrl, scope,
442 zi->reg->isamb, isam_p[i], termid);
443 r=rsmultior_create( rset_nmem, kctrl, scope, no, rsets);
447 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
451 yaz_log (YLOG_WARN, "Unknown isam set in rset_trunc");
452 return rsnull_create (rset_nmem, kctrl);
454 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
455 preserve_position, term_type, rset_nmem,kctrl,scope,