1 /* This file is part of the Zebra server.
2 Copyright (C) Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 = rset_create_temp(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 int cmp_border = preserve_position ? 0 : 1;
150 NMEM rset_nmem_sub = nmem_create(); /* all sub rsets not needed
153 rset = (RSET *) xmalloc(sizeof(*rset) * rsmax);
154 rsfd = (RSFD *) xmalloc(sizeof(*rsfd) * rsmax);
156 for (i = from; i < to; i += i_add)
159 rset[rscur] = rset_trunc_r(zi, term, length, flags,
161 merge_chunk, preserve_position,
162 term_type, rset_nmem_sub,
165 rset[rscur] = rset_trunc_r(zi, term, length, flags,
167 merge_chunk, preserve_position,
168 term_type, rset_nmem_sub,
172 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
173 for (i = rscur; --i >= 0; )
175 rsfd[i] = rset_open(rset[i], RSETF_READ);
176 if (rset_read(rsfd[i], ti->tmpbuf, 0))
177 heap_insert(ti, ti->tmpbuf, i);
181 rset_delete(rset[i]);
186 int n = ti->indx[ti->ptr[1]];
188 rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
193 if(!rset_read (rsfd[n], ti->tmpbuf,0))
197 rset_delete(rset[n]);
200 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > cmp_border)
203 heap_insert(ti, ti->tmpbuf, n);
211 nmem_destroy(rset_nmem_sub);
213 else if (zi->reg->isamc)
217 struct trunc_info *ti;
219 ispt = (ISAMC_PP *) xmalloc(sizeof(*ispt) * (to-from));
221 ti = heap_init(to-from, sizeof(struct it_key),
223 for (i = to-from; --i >= 0; )
225 ispt[i] = isamc_pp_open(zi->reg->isamc, isam_p[from+i]);
226 if (isamc_pp_read(ispt[i], ti->tmpbuf))
227 heap_insert(ti, ti->tmpbuf, i);
229 isamc_pp_close(ispt[i]);
233 int n = ti->indx[ti->ptr[1]];
235 rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
237 if (preserve_position)
240 if (isamc_pp_read(ispt[n], ti->tmpbuf))
241 heap_insert(ti, ti->tmpbuf, n);
243 isamc_pp_close(ispt[n]);
249 if (!isamc_pp_read(ispt[n], ti->tmpbuf))
252 isamc_pp_close(ispt[n]);
255 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
258 heap_insert(ti, ti->tmpbuf, n);
267 else if (zi->reg->isams)
271 struct trunc_info *ti;
274 ispt = (ISAMS_PP *) xmalloc(sizeof(*ispt) * (to-from));
276 ti = heap_init(to-from, sizeof(struct it_key),
278 for (i = to-from; --i >= 0; )
280 ispt[i] = isams_pp_open(zi->reg->isams, isam_p[from+i]);
281 if (isams_pp_read(ispt[i], ti->tmpbuf))
282 heap_insert(ti, ti->tmpbuf, i);
284 isams_pp_close(ispt[i]);
288 int n = ti->indx[ti->ptr[1]];
290 rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
294 if (!isams_pp_read(ispt[n], ti->tmpbuf))
297 isams_pp_close(ispt[n]);
300 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
303 heap_insert(ti, ti->tmpbuf, n);
311 else if (zi->reg->isamb)
315 struct trunc_info *ti;
317 ispt = (ISAMB_PP *) xmalloc(sizeof(*ispt) * (to-from));
319 ti = heap_init(to-from, sizeof(struct it_key),
321 for (i = to-from; --i >= 0; )
323 if (isam_p[from+i]) {
324 ispt[i] = isamb_pp_open(zi->reg->isamb, isam_p[from+i], scope);
325 if (isamb_pp_read(ispt[i], ti->tmpbuf))
326 heap_insert(ti, ti->tmpbuf, i);
328 isamb_pp_close(ispt[i]);
333 int n = ti->indx[ti->ptr[1]];
335 rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
338 if (preserve_position)
341 if (isamb_pp_read(ispt[n], ti->tmpbuf))
342 heap_insert(ti, ti->tmpbuf, n);
344 isamb_pp_close(ispt[n]);
350 if (!isamb_pp_read(ispt[n], ti->tmpbuf))
353 isamb_pp_close(ispt[n]);
356 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
359 heap_insert(ti, ti->tmpbuf, n);
369 yaz_log(YLOG_WARN, "Unknown isam set in rset_trunc_r");
371 rset_close(result_rsfd);
375 static int isams_trunc_cmp(const void *p1, const void *p2)
377 ISAM_P i1 = *(ISAM_P*) p1;
378 ISAM_P i2 = *(ISAM_P*) p2;
387 static int isamc_trunc_cmp(const void *p1, const void *p2)
389 ISAM_P i1 = *(ISAM_P*) p1;
390 ISAM_P i2 = *(ISAM_P*) p2;
393 d = (isamc_type(i1) - isamc_type(i2));
395 d = isamc_block(i1) - isamc_block(i2);
403 RSET rset_trunc(ZebraHandle zh, ISAM_P *isam_p, int no,
404 const char *term, int length, const char *flags,
405 int preserve_position, int term_type, NMEM rset_nmem,
406 struct rset_key_control *kctrl, int scope,
407 struct ord_list *ol, const char *index_type,
408 zint hits_limit, const char *term_ref_id)
413 int trunc_limit = atoi(res_get_def(zh->res, "trunclimit", "10000"));
415 termid = rset_term_create(term, length, flags, term_type, rset_nmem, ol,
416 *index_type, hits_limit, term_ref_id);
419 return rset_create_null(rset_nmem, kctrl, termid);
421 return zebra_create_rset_isam(zh, rset_nmem, kctrl,
422 scope, *isam_p, termid);
423 else if (zh->reg->isamb && no > 1 && no < trunc_limit)
426 RSET *rsets = xmalloc(no*sizeof(RSET)); /* use nmem! */
428 for (i = 0; i<no; i++)
429 rsets[i] = rsisamb_create(rset_nmem, kctrl, scope,
430 zh->reg->isamb, isam_p[i],
432 r = rset_create_or(rset_nmem, kctrl, scope,
438 qsort(isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
440 qsort(isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
441 trunc_chunk = atoi(res_get_def(zh->res, "truncchunk", "20"));
442 result = rset_trunc_r(zh, term, length, flags, isam_p, 0, no, trunc_chunk,
443 preserve_position, term_type, rset_nmem, kctrl,
451 * c-file-style: "Stroustrup"
452 * indent-tabs-mode: nil
454 * vim: shiftwidth=4 tabstop=8 expandtab