1 /* $Id: trunc.c,v 1.38 2004-08-24 15:00:16 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,
146 rset_temp_parms parms;
147 parms.cmp = key_compare_it;
148 parms.key_size = sizeof(struct it_key);
149 parms.temp_path = res_get (zi->res, "setTmpDir");
150 result = rset_create (rset_kind_temp, &parms);
152 result=rstemp_create(NULL, /* FIXME - use a proper nmem */
153 sizeof(struct it_key), key_compare_it,
154 res_get (zi->res, "setTmpDir"));
155 result_rsfd = rset_open (result, RSETF_WRITE);
157 if (to - from > merge_chunk)
161 int i, i_add = (to-from)/merge_chunk + 1;
162 struct trunc_info *ti;
164 int rsmax = (to-from)/i_add + 1;
166 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
167 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
169 for (i = from; i < to; i += i_add)
172 rset[rscur] = rset_trunc_r (zi, term, length, flags,
174 merge_chunk, preserve_position,
177 rset[rscur] = rset_trunc_r (zi, term, length, flags,
179 merge_chunk, preserve_position,
183 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
184 for (i = rscur; --i >= 0; )
186 rsfd[i] = rset_open (rset[i], RSETF_READ);
187 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
188 heap_insert (ti, ti->tmpbuf, i);
191 rset_close (rset[i], rsfd[i]);
192 rset_delete (rset[i]);
197 int n = ti->indx[ti->ptr[1]];
199 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
204 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
207 rset_close (rset[n], rsfd[n]);
208 rset_delete (rset[n]);
211 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
214 heap_insert (ti, ti->tmpbuf, n);
223 else if (zi->reg->isamc)
227 struct trunc_info *ti;
229 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
231 ti = heap_init (to-from, sizeof(struct it_key),
233 for (i = to-from; --i >= 0; )
235 ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
236 if (isc_pp_read (ispt[i], ti->tmpbuf))
237 heap_insert (ti, ti->tmpbuf, i);
239 isc_pp_close (ispt[i]);
243 int n = ti->indx[ti->ptr[1]];
245 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
247 if (preserve_position)
250 if (isc_pp_read (ispt[n], ti->tmpbuf))
251 heap_insert (ti, ti->tmpbuf, n);
253 isc_pp_close (ispt[n]);
259 if (!isc_pp_read (ispt[n], ti->tmpbuf))
262 isc_pp_close (ispt[n]);
265 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
268 heap_insert (ti, ti->tmpbuf, n);
277 else if (zi->reg->isams)
281 struct trunc_info *ti;
284 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
286 ti = heap_init (to-from, sizeof(struct it_key),
288 for (i = to-from; --i >= 0; )
290 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
291 if (isams_pp_read (ispt[i], ti->tmpbuf))
292 heap_insert (ti, ti->tmpbuf, i);
294 isams_pp_close (ispt[i]);
298 int n = ti->indx[ti->ptr[1]];
300 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
304 if (!isams_pp_read (ispt[n], ti->tmpbuf))
307 isams_pp_close (ispt[n]);
310 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
313 heap_insert (ti, ti->tmpbuf, n);
321 else if (zi->reg->isamb)
325 struct trunc_info *ti;
327 ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
329 ti = heap_init (to-from, sizeof(struct it_key),
331 for (i = to-from; --i >= 0; )
333 if (isam_p[from+i]) {
334 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i]);
335 if (isamb_pp_read (ispt[i], ti->tmpbuf))
336 heap_insert (ti, ti->tmpbuf, i);
338 isamb_pp_close (ispt[i]);
343 int n = ti->indx[ti->ptr[1]];
345 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
348 if (preserve_position)
351 if (isamb_pp_read (ispt[n], ti->tmpbuf))
352 heap_insert (ti, ti->tmpbuf, n);
354 isamb_pp_close (ispt[n]);
360 if (!isamb_pp_read (ispt[n], ti->tmpbuf))
363 isamb_pp_close (ispt[n]);
366 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
369 heap_insert (ti, ti->tmpbuf, n);
379 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
381 rset_close (result, result_rsfd);
385 static int isams_trunc_cmp (const void *p1, const void *p2)
387 ISAMS_P i1 = *(ISAMS_P*) p1;
388 ISAMS_P i2 = *(ISAMS_P*) p2;
397 static int isamc_trunc_cmp (const void *p1, const void *p2)
399 ISAMC_P i1 = *(ISAMC_P*) p1;
400 ISAMC_P i2 = *(ISAMC_P*) p2;
403 d = (isc_type (i1) - isc_type (i2));
405 d = isc_block (i1) - isc_block (i2);
413 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
414 const char *term, int length, const char *flags,
415 int preserve_position, int term_type)
417 logf (LOG_DEBUG, "rset_trunc no=%d", no);
419 return rsnull_create (NULL); /* FIXME - use a proper nmem */
423 return rsisams_create(NULL, /* FIXME - use some nmem */
424 sizeof(struct it_key), key_compare_it,
425 zi->reg->isams, *isam_p);
428 rset_isams_parms parms;
431 parms.is = zi->reg->isams;
432 return rset_create (rset_kind_isams, &parms);
435 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
437 else if (zi->reg->isamc)
440 return rsisamc_create(NULL, /* FIXME - use some nmem */
441 sizeof(struct it_key), key_compare_it,
442 zi->reg->isamc, *isam_p);
445 rset_isamc_parms parms;
447 parms.key_size = sizeof(struct it_key);
448 parms.cmp = key_compare_it;
450 parms.is = zi->reg->isamc;
451 return rset_create (rset_kind_isamc, &parms);
455 #if 0 /* NEW_TRUNC */ /* FIXME - Use the new multi_or instead !! */
458 rset_m_or_parms parms;
460 parms.key_size = sizeof(struct it_key);
461 parms.cmp = key_compare_it;
462 parms.isc = zi->reg->isamc;
463 parms.isam_positions = isam_p;
464 parms.no_isam_positions = no;
465 parms.no_save_positions = 100000;
466 return rset_create (rset_kind_m_or, &parms);
469 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
471 else if (zi->reg->isamb)
474 return rsisamb_create(NULL, /* FIXME - use some nmem */
475 sizeof(struct it_key), key_compare_it,
476 zi->reg->isamb, *isam_p);
479 rset_isamb_parms parms;
480 parms.key_size = sizeof(struct it_key);
481 parms.cmp = key_compare_it;
483 parms.is = zi->reg->isamb;
484 return rset_create (rset_kind_isamb, &parms);
488 else if (no <10000 ) /* FIXME - hardcoded number */
491 RSET *rsets=xmalloc(no*sizeof(RSET)); /* use nmem! */
494 rsets[i]=rsisamb_create(NULL, /* */
495 sizeof(struct it_key), key_compare_it,
496 zi->reg->isamb, isam_p[i] );
497 r=rsmultior_create( NULL, /* FIXME - use some nmem */
498 sizeof(struct it_key), key_compare_it,
503 rset_multior_parms m_parms;
504 rset_isamb_parms b_parms;
506 m_parms.key_size = sizeof(struct it_key);
507 m_parms.cmp = key_compare_it;
509 m_parms.rsets=xmalloc(sizeof(*m_parms.rsets)*no);
510 b_parms.key_size = sizeof(struct it_key);
511 b_parms.cmp = key_compare_it;
512 b_parms.is = zi->reg->isamb;
515 b_parms.pos = isam_p[i];
516 m_parms.rsets[i]=rset_create (rset_kind_isamb, &b_parms);
518 return rset_create (rset_kind_multior, &m_parms);
520 } /* <10000 - rs_multior */
522 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
526 logf (LOG_WARN, "Unknown isam set in rset_trunc");
527 return rsnull_create (NULL); /* FIXME - nmem */
529 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
530 preserve_position, term_type);