1 /* $Id: trunc.c,v 1.45 2004-09-29 11:00:56 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)
137 rset_temp_parms parms;
138 parms.cmp = key_compare_it;
139 parms.key_size = sizeof(struct it_key);
140 parms.temp_path = res_get (zi->res, "setTmpDir");
141 result = rset_create (rset_kind_temp, &parms);
143 result=rstemp_create( rset_nmem,kctrl, scope,
144 res_get (zi->res, "setTmpDir"));
145 result_rsfd = rset_open (result, RSETF_WRITE);
147 if (to - from > merge_chunk)
151 int i, i_add = (to-from)/merge_chunk + 1;
152 struct trunc_info *ti;
154 int rsmax = (to-from)/i_add + 1;
156 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
157 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
159 for (i = from; i < to; i += i_add)
162 rset[rscur] = rset_trunc_r (zi, term, length, flags,
164 merge_chunk, preserve_position,
165 term_type, rset_nmem,
168 rset[rscur] = rset_trunc_r (zi, term, length, flags,
170 merge_chunk, preserve_position,
171 term_type, rset_nmem,
175 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
176 for (i = rscur; --i >= 0; )
178 rsfd[i] = rset_open (rset[i], RSETF_READ);
179 if (rset_read(rsfd[i], ti->tmpbuf))
180 heap_insert (ti, ti->tmpbuf, i);
183 rset_close (rsfd[i]);
184 rset_delete (rset[i]);
189 int n = ti->indx[ti->ptr[1]];
191 rset_write (result_rsfd, ti->heap[ti->ptr[1]]);
196 if (!rset_read (rsfd[n], ti->tmpbuf))
199 rset_close (rsfd[n]);
200 rset_delete (rset[n]);
203 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
206 heap_insert (ti, ti->tmpbuf, n);
215 else if (zi->reg->isamc)
219 struct trunc_info *ti;
221 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
223 ti = heap_init (to-from, sizeof(struct it_key),
225 for (i = to-from; --i >= 0; )
227 ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
228 if (isc_pp_read (ispt[i], ti->tmpbuf))
229 heap_insert (ti, ti->tmpbuf, i);
231 isc_pp_close (ispt[i]);
235 int n = ti->indx[ti->ptr[1]];
237 rset_write (result_rsfd, ti->heap[ti->ptr[1]]);
239 if (preserve_position)
242 if (isc_pp_read (ispt[n], ti->tmpbuf))
243 heap_insert (ti, ti->tmpbuf, n);
245 isc_pp_close (ispt[n]);
251 if (!isc_pp_read (ispt[n], ti->tmpbuf))
254 isc_pp_close (ispt[n]);
257 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
260 heap_insert (ti, ti->tmpbuf, n);
269 else if (zi->reg->isams)
273 struct trunc_info *ti;
276 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
278 ti = heap_init (to-from, sizeof(struct it_key),
280 for (i = to-from; --i >= 0; )
282 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
283 if (isams_pp_read (ispt[i], ti->tmpbuf))
284 heap_insert (ti, ti->tmpbuf, i);
286 isams_pp_close (ispt[i]);
290 int n = ti->indx[ti->ptr[1]];
292 rset_write (result_rsfd, ti->heap[ti->ptr[1]]);
296 if (!isams_pp_read (ispt[n], ti->tmpbuf))
299 isams_pp_close (ispt[n]);
302 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
305 heap_insert (ti, ti->tmpbuf, n);
313 else if (zi->reg->isamb)
317 struct trunc_info *ti;
319 ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
321 ti = heap_init (to-from, sizeof(struct it_key),
323 for (i = to-from; --i >= 0; )
325 if (isam_p[from+i]) {
326 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i], scope);
327 if (isamb_pp_read (ispt[i], ti->tmpbuf))
328 heap_insert (ti, ti->tmpbuf, i);
330 isamb_pp_close (ispt[i]);
335 int n = ti->indx[ti->ptr[1]];
337 rset_write (result_rsfd, ti->heap[ti->ptr[1]]);
340 if (preserve_position)
343 if (isamb_pp_read (ispt[n], ti->tmpbuf))
344 heap_insert (ti, ti->tmpbuf, n);
346 isamb_pp_close (ispt[n]);
352 if (!isamb_pp_read (ispt[n], ti->tmpbuf))
355 isamb_pp_close (ispt[n]);
358 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
361 heap_insert (ti, ti->tmpbuf, n);
371 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
373 rset_close (result_rsfd);
377 static int isams_trunc_cmp (const void *p1, const void *p2)
379 ISAMS_P i1 = *(ISAMS_P*) p1;
380 ISAMS_P i2 = *(ISAMS_P*) p2;
389 static int isamc_trunc_cmp (const void *p1, const void *p2)
391 ISAMC_P i1 = *(ISAMC_P*) p1;
392 ISAMC_P i2 = *(ISAMC_P*) p2;
395 d = (isc_type (i1) - isc_type (i2));
397 d = isc_block (i1) - isc_block (i2);
405 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
406 const char *term, int length, const char *flags,
407 int preserve_position, int term_type, NMEM rset_nmem,
408 const struct key_control *kctrl, int scope)
410 logf (LOG_DEBUG, "rset_trunc no=%d", no);
412 return rsnull_create (rset_nmem,kctrl);
416 return rsisams_create(rset_nmem, kctrl, scope,
417 zi->reg->isams, *isam_p);
418 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
420 else if (zi->reg->isamc)
423 return rsisamc_create(rset_nmem, kctrl, scope,
424 zi->reg->isamc, *isam_p);
425 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
427 else if (zi->reg->isamb)
430 return rsisamb_create(rset_nmem,kctrl, scope,
431 zi->reg->isamb, *isam_p);
432 else if (no <10000 ) /* FIXME - hardcoded number */
435 RSET *rsets=xmalloc(no*sizeof(RSET)); /* use nmem! */
438 rsets[i]=rsisamb_create(rset_nmem, kctrl, scope,
439 zi->reg->isamb, isam_p[i] );
440 r=rsmultior_create( rset_nmem, kctrl, scope, no, rsets);
444 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
448 logf (LOG_WARN, "Unknown isam set in rset_trunc");
449 return rsnull_create (rset_nmem, kctrl);
451 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
452 preserve_position, term_type, rset_nmem,kctrl,scope);