1 /* $Id: trunc.c,v 1.63 2005-08-25 10:37:05 adam Exp $
2 Copyright (C) 1995-2005
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, ISAM_P *isam_p, int from, int to,
128 int merge_chunk, int preserve_position,
129 int term_type, NMEM rset_nmem,
130 struct rset_key_control *kctrl, int scope,
137 result = rstemp_create(rset_nmem, kctrl, scope,
138 res_get(zi->res, "setTmpDir"), termid);
139 result_rsfd = rset_open(result, RSETF_WRITE);
141 if (to - from > merge_chunk)
145 int i, i_add = (to-from)/merge_chunk + 1;
146 struct trunc_info *ti;
148 int rsmax = (to-from)/i_add + 1;
149 NMEM rset_nmem_sub = nmem_create(); /* all sub rsets not needed
152 rset = (RSET *) xmalloc(sizeof(*rset) * rsmax);
153 rsfd = (RSFD *) xmalloc(sizeof(*rsfd) * rsmax);
155 for (i = from; i < to; i += i_add)
158 rset[rscur] = rset_trunc_r(zi, term, length, flags,
160 merge_chunk, preserve_position,
161 term_type, rset_nmem_sub,
164 rset[rscur] = rset_trunc_r(zi, term, length, flags,
166 merge_chunk, preserve_position,
167 term_type, rset_nmem_sub,
171 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
172 for (i = rscur; --i >= 0; )
174 rsfd[i] = rset_open(rset[i], RSETF_READ);
175 if (rset_read(rsfd[i], ti->tmpbuf, 0))
176 heap_insert(ti, ti->tmpbuf, i);
180 rset_delete(rset[i]);
185 int n = ti->indx[ti->ptr[1]];
187 rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
192 if(!rset_read (rsfd[n], ti->tmpbuf,0))
196 rset_delete(rset[n]);
199 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
202 heap_insert(ti, ti->tmpbuf, n);
210 nmem_destroy(rset_nmem_sub);
212 else if (zi->reg->isamc)
216 struct trunc_info *ti;
218 ispt = (ISAMC_PP *) xmalloc(sizeof(*ispt) * (to-from));
220 ti = heap_init(to-from, sizeof(struct it_key),
222 for (i = to-from; --i >= 0; )
224 ispt[i] = isamc_pp_open(zi->reg->isamc, isam_p[from+i]);
225 if (isamc_pp_read(ispt[i], ti->tmpbuf))
226 heap_insert(ti, ti->tmpbuf, i);
228 isamc_pp_close(ispt[i]);
232 int n = ti->indx[ti->ptr[1]];
234 rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
236 if (preserve_position)
239 if (isamc_pp_read(ispt[n], ti->tmpbuf))
240 heap_insert(ti, ti->tmpbuf, n);
242 isamc_pp_close(ispt[n]);
248 if (!isamc_pp_read(ispt[n], ti->tmpbuf))
251 isamc_pp_close(ispt[n]);
254 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
257 heap_insert(ti, ti->tmpbuf, n);
266 else if (zi->reg->isams)
270 struct trunc_info *ti;
273 ispt = (ISAMS_PP *) xmalloc(sizeof(*ispt) * (to-from));
275 ti = heap_init(to-from, sizeof(struct it_key),
277 for (i = to-from; --i >= 0; )
279 ispt[i] = isams_pp_open(zi->reg->isams, isam_p[from+i]);
280 if (isams_pp_read(ispt[i], ti->tmpbuf))
281 heap_insert(ti, ti->tmpbuf, i);
283 isams_pp_close(ispt[i]);
287 int n = ti->indx[ti->ptr[1]];
289 rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
293 if (!isams_pp_read(ispt[n], ti->tmpbuf))
296 isams_pp_close(ispt[n]);
299 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
302 heap_insert(ti, ti->tmpbuf, n);
310 else if (zi->reg->isamb)
314 struct trunc_info *ti;
316 ispt = (ISAMB_PP *) xmalloc(sizeof(*ispt) * (to-from));
318 ti = heap_init(to-from, sizeof(struct it_key),
320 for (i = to-from; --i >= 0; )
322 if (isam_p[from+i]) {
323 ispt[i] = isamb_pp_open(zi->reg->isamb, isam_p[from+i], scope);
324 if (isamb_pp_read(ispt[i], ti->tmpbuf))
325 heap_insert(ti, ti->tmpbuf, i);
327 isamb_pp_close(ispt[i]);
332 int n = ti->indx[ti->ptr[1]];
334 rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
337 if (preserve_position)
340 if (isamb_pp_read(ispt[n], ti->tmpbuf))
341 heap_insert(ti, ti->tmpbuf, n);
343 isamb_pp_close(ispt[n]);
349 if (!isamb_pp_read(ispt[n], ti->tmpbuf))
352 isamb_pp_close(ispt[n]);
355 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
358 heap_insert(ti, ti->tmpbuf, n);
368 yaz_log(YLOG_WARN, "Unknown isam set in rset_trunc_r");
370 rset_close(result_rsfd);
374 static int isams_trunc_cmp(const void *p1, const void *p2)
376 ISAM_P i1 = *(ISAM_P*) p1;
377 ISAM_P i2 = *(ISAM_P*) p2;
386 static int isamc_trunc_cmp(const void *p1, const void *p2)
388 ISAM_P i1 = *(ISAM_P*) p1;
389 ISAM_P i2 = *(ISAM_P*) p2;
392 d = (isamc_type(i1) - isamc_type(i2));
394 d = isamc_block(i1) - isamc_block(i2);
402 RSET rset_trunc(ZebraHandle zi, ISAM_P *isam_p, int no,
403 const char *term, int length, const char *flags,
404 int preserve_position, int term_type, NMEM rset_nmem,
405 struct rset_key_control *kctrl, int scope,
406 struct ord_list *ol, int reg_type,
407 zint hits_limit, const char *term_ref_id)
413 termid = rset_term_create(term, length, flags, term_type, rset_nmem, ol,
414 reg_type, hits_limit, term_ref_id);
416 return rsnull_create(rset_nmem, kctrl, termid);
421 return rsisams_create(rset_nmem, kctrl, scope,
422 zi->reg->isams, *isam_p, termid);
423 qsort(isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
425 else if (zi->reg->isamc)
428 return rsisamc_create(rset_nmem, kctrl, scope,
429 zi->reg->isamc, *isam_p, termid);
430 qsort(isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
432 else if (zi->reg->isamb)
434 int trunc_limit = atoi(res_get_def(zi->res, "trunclimit", "10000"));
436 return rsisamb_create(rset_nmem, kctrl, scope,
437 zi->reg->isamb, *isam_p, termid);
438 else if (no < trunc_limit)
441 RSET *rsets = xmalloc(no*sizeof(RSET)); /* use nmem! */
443 for (i = 0; i<no; i++)
444 rsets[i] = rsisamb_create(rset_nmem, kctrl, scope,
445 zi->reg->isamb, isam_p[i],
447 r = rsmulti_or_create(rset_nmem, kctrl, scope,
453 qsort(isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
457 yaz_log(YLOG_WARN, "Unknown isam set in rset_trunc");
458 return rsnull_create(rset_nmem, kctrl, 0);
460 trunc_chunk = atoi(res_get_def(zi->res, "truncchunk", "20"));
461 result = rset_trunc_r(zi, term, length, flags, isam_p, 0, no, trunc_chunk,
462 preserve_position, term_type, rset_nmem, kctrl,