1 /* $Id: trunc.c,v 1.36 2004-08-20 14:44:46 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
38 #include <rsmultior.h>
45 int (*cmp)(const void *p1, const void *p2);
52 static void heap_swap (struct trunc_info *ti, int i1, int i2)
57 ti->ptr[i1] = ti->ptr[i2];
61 static void heap_delete (struct trunc_info *ti)
63 int cur = 1, child = 2;
65 heap_swap (ti, 1, ti->heapnum--);
66 while (child <= ti->heapnum) {
67 if (child < ti->heapnum &&
68 (*ti->cmp)(ti->heap[ti->ptr[child]],
69 ti->heap[ti->ptr[1+child]]) > 0)
71 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
72 ti->heap[ti->ptr[child]]) > 0)
74 heap_swap (ti, cur, child);
83 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
87 cur = ++(ti->heapnum);
88 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
89 ti->indx[ti->ptr[cur]] = indx;
91 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
92 ti->heap[ti->ptr[cur]]) > 0)
94 heap_swap (ti, cur, parent);
100 static struct trunc_info *heap_init (int size, int key_size,
101 int (*cmp)(const void *p1,
104 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
109 ti->keysize = key_size;
111 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
112 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
113 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
114 ti->swapbuf = (char *) xmalloc (ti->keysize);
115 ti->tmpbuf = (char *) xmalloc (ti->keysize);
116 ti->buf = (char *) xmalloc (size * ti->keysize);
117 for (i = size; --i >= 0; )
120 ti->heap[i] = ti->buf + ti->keysize * i;
125 static void heap_close (struct trunc_info *ti)
136 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
137 const char *flags, ISAMS_P *isam_p, int from, int to,
138 int merge_chunk, int preserve_position,
143 rset_temp_parms parms;
146 parms.cmp = key_compare_it;
147 parms.key_size = sizeof(struct it_key);
148 parms.temp_path = res_get (zi->res, "setTmpDir");
149 result = rset_create (rset_kind_temp, &parms);
150 result_rsfd = rset_open (result, RSETF_WRITE);
152 if (to - from > merge_chunk)
156 int i, i_add = (to-from)/merge_chunk + 1;
157 struct trunc_info *ti;
159 int rsmax = (to-from)/i_add + 1;
161 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
162 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
164 for (i = from; i < to; i += i_add)
167 rset[rscur] = rset_trunc_r (zi, term, length, flags,
169 merge_chunk, preserve_position,
172 rset[rscur] = rset_trunc_r (zi, term, length, flags,
174 merge_chunk, preserve_position,
178 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
179 for (i = rscur; --i >= 0; )
181 rsfd[i] = rset_open (rset[i], RSETF_READ);
182 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
183 heap_insert (ti, ti->tmpbuf, i);
186 rset_close (rset[i], rsfd[i]);
187 rset_delete (rset[i]);
192 int n = ti->indx[ti->ptr[1]];
194 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
199 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
202 rset_close (rset[n], rsfd[n]);
203 rset_delete (rset[n]);
206 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
209 heap_insert (ti, ti->tmpbuf, n);
218 else if (zi->reg->isamc)
222 struct trunc_info *ti;
224 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
226 ti = heap_init (to-from, sizeof(struct it_key),
228 for (i = to-from; --i >= 0; )
230 ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
231 if (isc_pp_read (ispt[i], ti->tmpbuf))
232 heap_insert (ti, ti->tmpbuf, i);
234 isc_pp_close (ispt[i]);
238 int n = ti->indx[ti->ptr[1]];
240 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
242 if (preserve_position)
245 if (isc_pp_read (ispt[n], ti->tmpbuf))
246 heap_insert (ti, ti->tmpbuf, n);
248 isc_pp_close (ispt[n]);
254 if (!isc_pp_read (ispt[n], ti->tmpbuf))
257 isc_pp_close (ispt[n]);
260 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
263 heap_insert (ti, ti->tmpbuf, n);
272 else if (zi->reg->isams)
276 struct trunc_info *ti;
279 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
281 ti = heap_init (to-from, sizeof(struct it_key),
283 for (i = to-from; --i >= 0; )
285 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
286 if (isams_pp_read (ispt[i], ti->tmpbuf))
287 heap_insert (ti, ti->tmpbuf, i);
289 isams_pp_close (ispt[i]);
293 int n = ti->indx[ti->ptr[1]];
295 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
299 if (!isams_pp_read (ispt[n], ti->tmpbuf))
302 isams_pp_close (ispt[n]);
305 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
308 heap_insert (ti, ti->tmpbuf, n);
316 else if (zi->reg->isamb)
320 struct trunc_info *ti;
322 ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
324 ti = heap_init (to-from, sizeof(struct it_key),
326 for (i = to-from; --i >= 0; )
328 if (isam_p[from+i]) {
329 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i]);
330 if (isamb_pp_read (ispt[i], ti->tmpbuf))
331 heap_insert (ti, ti->tmpbuf, i);
333 isamb_pp_close (ispt[i]);
338 int n = ti->indx[ti->ptr[1]];
340 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
343 if (preserve_position)
346 if (isamb_pp_read (ispt[n], ti->tmpbuf))
347 heap_insert (ti, ti->tmpbuf, n);
349 isamb_pp_close (ispt[n]);
355 if (!isamb_pp_read (ispt[n], ti->tmpbuf))
358 isamb_pp_close (ispt[n]);
361 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
364 heap_insert (ti, ti->tmpbuf, n);
374 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
376 rset_close (result, result_rsfd);
380 static int isams_trunc_cmp (const void *p1, const void *p2)
382 ISAMS_P i1 = *(ISAMS_P*) p1;
383 ISAMS_P i2 = *(ISAMS_P*) p2;
392 static int isamc_trunc_cmp (const void *p1, const void *p2)
394 ISAMC_P i1 = *(ISAMC_P*) p1;
395 ISAMC_P i2 = *(ISAMC_P*) p2;
398 d = (isc_type (i1) - isc_type (i2));
400 d = isc_block (i1) - isc_block (i2);
408 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
409 const char *term, int length, const char *flags,
410 int preserve_position, int term_type)
412 logf (LOG_DEBUG, "rset_trunc no=%d", no);
415 rset_null_parms parms;
416 return rset_create (rset_kind_null, &parms);
422 rset_isams_parms parms;
425 parms.is = zi->reg->isams;
426 return rset_create (rset_kind_isams, &parms);
428 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
430 else if (zi->reg->isamc)
434 rset_isamc_parms parms;
436 parms.key_size = sizeof(struct it_key);
437 parms.cmp = key_compare_it;
439 parms.is = zi->reg->isamc;
440 return rset_create (rset_kind_isamc, &parms);
442 #if 0 /* NEW_TRUNC */ /* FIXME - Use the new multi_or instead !! */
445 rset_m_or_parms parms;
447 parms.key_size = sizeof(struct it_key);
448 parms.cmp = key_compare_it;
449 parms.isc = zi->reg->isamc;
450 parms.isam_positions = isam_p;
451 parms.no_isam_positions = no;
452 parms.no_save_positions = 100000;
453 return rset_create (rset_kind_m_or, &parms);
456 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
458 else if (zi->reg->isamb)
462 rset_isamb_parms parms;
463 parms.key_size = sizeof(struct it_key);
464 parms.cmp = key_compare_it;
466 parms.is = zi->reg->isamb;
467 return rset_create (rset_kind_isamb, &parms);
470 else if (no <10000 ) /* FIXME - hardcoded number */
472 rset_multior_parms m_parms;
473 rset_isamb_parms b_parms;
475 m_parms.key_size = sizeof(struct it_key);
476 m_parms.cmp = key_compare_it;
478 m_parms.rsets=xmalloc(sizeof(*m_parms.rsets)*no);
479 b_parms.key_size = sizeof(struct it_key);
480 b_parms.cmp = key_compare_it;
481 b_parms.is = zi->reg->isamb;
482 /* FIXME - make it so that we can pass a null ptr to term */
483 /* needs changes in all rsets, here and there! */
486 b_parms.pos = isam_p[i];
487 m_parms.rsets[i]=rset_create (rset_kind_isamb, &b_parms);
489 return rset_create (rset_kind_multior, &m_parms);
490 } /* <10000 - rs_multior */
492 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
496 logf (LOG_WARN, "Unknown isam set in rset_trunc");
497 return rset_create (rset_kind_null, NULL);
499 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
500 preserve_position, term_type);