1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 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
32 int (*cmp)(const void *p1, const void *p2);
39 static void heap_swap(struct trunc_info *ti, int i1, int i2)
44 ti->ptr[i1] = ti->ptr[i2];
48 static void heap_delete(struct trunc_info *ti)
50 int cur = 1, child = 2;
52 heap_swap(ti, 1, ti->heapnum--);
53 while (child <= ti->heapnum) {
54 if (child < ti->heapnum &&
55 (*ti->cmp)(ti->heap[ti->ptr[child]],
56 ti->heap[ti->ptr[1+child]]) > 0)
58 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
59 ti->heap[ti->ptr[child]]) > 0)
61 heap_swap(ti, cur, child);
70 static void heap_insert(struct trunc_info *ti, const char *buf, int indx)
74 cur = ++(ti->heapnum);
75 memcpy(ti->heap[ti->ptr[cur]], buf, ti->keysize);
76 ti->indx[ti->ptr[cur]] = indx;
78 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
79 ti->heap[ti->ptr[cur]]) > 0)
81 heap_swap(ti, cur, parent);
87 static struct trunc_info *heap_init(int size, int key_size,
88 int (*cmp)(const void *p1,
91 struct trunc_info *ti = (struct trunc_info *) xmalloc(sizeof(*ti));
96 ti->keysize = key_size;
98 ti->indx = (int *) xmalloc(size * sizeof(*ti->indx));
99 ti->heap = (char **) xmalloc(size * sizeof(*ti->heap));
100 ti->ptr = (int *) xmalloc(size * sizeof(*ti->ptr));
101 ti->swapbuf = (char *) xmalloc(ti->keysize);
102 ti->tmpbuf = (char *) xmalloc(ti->keysize);
103 ti->buf = (char *) xmalloc(size * ti->keysize);
104 for (i = size; --i >= 0; )
107 ti->heap[i] = ti->buf + ti->keysize * i;
112 static void heap_close(struct trunc_info *ti)
123 static RSET rset_trunc_r(ZebraHandle zi, const char *term, int length,
124 const char *flags, ISAM_P *isam_p, int from, int to,
125 int merge_chunk, int preserve_position,
126 int term_type, NMEM rset_nmem,
127 struct rset_key_control *kctrl, int scope,
134 result = rset_create_temp(rset_nmem, kctrl, scope,
135 res_get(zi->res, "setTmpDir"), termid);
136 result_rsfd = rset_open(result, RSETF_WRITE);
138 if (to - from > merge_chunk)
142 int i, i_add = (to-from)/merge_chunk + 1;
143 struct trunc_info *ti;
145 int rsmax = (to-from)/i_add + 1;
146 int cmp_border = preserve_position ? 0 : 1;
147 NMEM rset_nmem_sub = nmem_create(); /* all sub rsets not needed
150 rset = (RSET *) xmalloc(sizeof(*rset) * rsmax);
151 rsfd = (RSFD *) xmalloc(sizeof(*rsfd) * rsmax);
153 for (i = from; i < to; i += i_add)
156 rset[rscur] = rset_trunc_r(zi, term, length, flags,
158 merge_chunk, preserve_position,
159 term_type, rset_nmem_sub,
162 rset[rscur] = rset_trunc_r(zi, term, length, flags,
164 merge_chunk, preserve_position,
165 term_type, rset_nmem_sub,
169 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
170 for (i = rscur; --i >= 0; )
172 rsfd[i] = rset_open(rset[i], RSETF_READ);
173 if (rset_read(rsfd[i], ti->tmpbuf, 0))
174 heap_insert(ti, ti->tmpbuf, i);
178 rset_delete(rset[i]);
183 int n = ti->indx[ti->ptr[1]];
185 rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
190 if(!rset_read (rsfd[n], ti->tmpbuf,0))
194 rset_delete(rset[n]);
197 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > cmp_border)
200 heap_insert(ti, ti->tmpbuf, n);
208 nmem_destroy(rset_nmem_sub);
210 else if (zi->reg->isamc)
214 struct trunc_info *ti;
216 ispt = (ISAMC_PP *) xmalloc(sizeof(*ispt) * (to-from));
218 ti = heap_init(to-from, sizeof(struct it_key),
220 for (i = to-from; --i >= 0; )
222 ispt[i] = isamc_pp_open(zi->reg->isamc, isam_p[from+i]);
223 if (isamc_pp_read(ispt[i], ti->tmpbuf))
224 heap_insert(ti, ti->tmpbuf, i);
226 isamc_pp_close(ispt[i]);
230 int n = ti->indx[ti->ptr[1]];
232 rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
234 if (preserve_position)
237 if (isamc_pp_read(ispt[n], ti->tmpbuf))
238 heap_insert(ti, ti->tmpbuf, n);
240 isamc_pp_close(ispt[n]);
246 if (!isamc_pp_read(ispt[n], ti->tmpbuf))
249 isamc_pp_close(ispt[n]);
252 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
255 heap_insert(ti, ti->tmpbuf, n);
264 else if (zi->reg->isams)
268 struct trunc_info *ti;
271 ispt = (ISAMS_PP *) xmalloc(sizeof(*ispt) * (to-from));
273 ti = heap_init(to-from, sizeof(struct it_key),
275 for (i = to-from; --i >= 0; )
277 ispt[i] = isams_pp_open(zi->reg->isams, isam_p[from+i]);
278 if (isams_pp_read(ispt[i], ti->tmpbuf))
279 heap_insert(ti, ti->tmpbuf, i);
281 isams_pp_close(ispt[i]);
285 int n = ti->indx[ti->ptr[1]];
287 rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
291 if (!isams_pp_read(ispt[n], ti->tmpbuf))
294 isams_pp_close(ispt[n]);
297 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
300 heap_insert(ti, ti->tmpbuf, n);
308 else if (zi->reg->isamb)
312 struct trunc_info *ti;
314 ispt = (ISAMB_PP *) xmalloc(sizeof(*ispt) * (to-from));
316 ti = heap_init(to-from, sizeof(struct it_key),
318 for (i = to-from; --i >= 0; )
320 if (isam_p[from+i]) {
321 ispt[i] = isamb_pp_open(zi->reg->isamb, isam_p[from+i], scope);
322 if (isamb_pp_read(ispt[i], ti->tmpbuf))
323 heap_insert(ti, ti->tmpbuf, i);
325 isamb_pp_close(ispt[i]);
330 int n = ti->indx[ti->ptr[1]];
332 rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
335 if (preserve_position)
338 if (isamb_pp_read(ispt[n], ti->tmpbuf))
339 heap_insert(ti, ti->tmpbuf, n);
341 isamb_pp_close(ispt[n]);
347 if (!isamb_pp_read(ispt[n], ti->tmpbuf))
350 isamb_pp_close(ispt[n]);
353 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
356 heap_insert(ti, ti->tmpbuf, n);
366 yaz_log(YLOG_WARN, "Unknown isam set in rset_trunc_r");
368 rset_close(result_rsfd);
372 static int isams_trunc_cmp(const void *p1, const void *p2)
374 ISAM_P i1 = *(ISAM_P*) p1;
375 ISAM_P i2 = *(ISAM_P*) p2;
384 static int isamc_trunc_cmp(const void *p1, const void *p2)
386 ISAM_P i1 = *(ISAM_P*) p1;
387 ISAM_P i2 = *(ISAM_P*) p2;
390 d = (isamc_type(i1) - isamc_type(i2));
392 d = isamc_block(i1) - isamc_block(i2);
400 RSET rset_trunc(ZebraHandle zh, ISAM_P *isam_p, int no,
401 const char *term, int length, const char *flags,
402 int preserve_position, int term_type, NMEM rset_nmem,
403 struct rset_key_control *kctrl, int scope,
404 struct ord_list *ol, const char *index_type,
405 zint hits_limit, const char *term_ref_id)
410 int trunc_limit = atoi(res_get_def(zh->res, "trunclimit", "10000"));
412 termid = rset_term_create(term, length, flags, term_type, rset_nmem, ol,
413 *index_type, hits_limit, term_ref_id);
416 return rset_create_null(rset_nmem, kctrl, termid);
418 return zebra_create_rset_isam(zh, rset_nmem, kctrl,
419 scope, *isam_p, termid);
420 else if (zh->reg->isamb && no > 1 && no < trunc_limit)
423 RSET *rsets = xmalloc(no*sizeof(RSET)); /* use nmem! */
425 for (i = 0; i<no; i++)
426 rsets[i] = rsisamb_create(rset_nmem, kctrl, scope,
427 zh->reg->isamb, isam_p[i],
429 r = rset_create_or(rset_nmem, kctrl, scope,
435 qsort(isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
437 qsort(isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
438 trunc_chunk = atoi(res_get_def(zh->res, "truncchunk", "20"));
439 result = rset_trunc_r(zh, term, length, flags, isam_p, 0, no, trunc_chunk,
440 preserve_position, term_type, rset_nmem, kctrl,
448 * c-file-style: "Stroustrup"
449 * indent-tabs-mode: nil
451 * vim: shiftwidth=4 tabstop=8 expandtab