1 /* $Id: trunc.c,v 1.32 2004-08-06 13:36:23 adam 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
44 int (*cmp)(const void *p1, const void *p2);
51 static void heap_swap (struct trunc_info *ti, int i1, int i2)
56 ti->ptr[i1] = ti->ptr[i2];
60 static void heap_delete (struct trunc_info *ti)
62 int cur = 1, child = 2;
64 heap_swap (ti, 1, ti->heapnum--);
65 while (child <= ti->heapnum) {
66 if (child < ti->heapnum &&
67 (*ti->cmp)(ti->heap[ti->ptr[child]],
68 ti->heap[ti->ptr[1+child]]) > 0)
70 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
71 ti->heap[ti->ptr[child]]) > 0)
73 heap_swap (ti, cur, child);
82 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
86 cur = ++(ti->heapnum);
87 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
88 ti->indx[ti->ptr[cur]] = indx;
90 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
91 ti->heap[ti->ptr[cur]]) > 0)
93 heap_swap (ti, cur, parent);
99 static struct trunc_info *heap_init (int size, int key_size,
100 int (*cmp)(const void *p1,
103 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
108 ti->keysize = key_size;
110 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
111 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
112 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
113 ti->swapbuf = (char *) xmalloc (ti->keysize);
114 ti->tmpbuf = (char *) xmalloc (ti->keysize);
115 ti->buf = (char *) xmalloc (size * ti->keysize);
116 for (i = size; --i >= 0; )
119 ti->heap[i] = ti->buf + ti->keysize * i;
124 static void heap_close (struct trunc_info *ti)
135 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
136 const char *flags, ISAMS_P *isam_p, int from, int to,
137 int merge_chunk, int preserve_position,
142 rset_temp_parms parms;
145 parms.cmp = key_compare_it;
146 parms.key_size = sizeof(struct it_key);
147 parms.temp_path = res_get (zi->res, "setTmpDir");
148 parms.rset_term = rset_term_create (term, length, flags, term_type);
149 result = rset_create (rset_kind_temp, &parms);
150 result_rsfd = rset_open (result, RSETF_WRITE);
152 if (to - from > merge_chunk)
157 int i, i_add = (to-from)/merge_chunk + 1;
158 struct trunc_info *ti;
160 int rsmax = (to-from)/i_add + 1;
162 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
163 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
165 for (i = from; i < to; i += i_add)
168 rset[rscur] = rset_trunc_r (zi, term, length, flags,
170 merge_chunk, preserve_position,
173 rset[rscur] = rset_trunc_r (zi, term, length, flags,
175 merge_chunk, preserve_position,
179 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
180 for (i = rscur; --i >= 0; )
182 rsfd[i] = rset_open (rset[i], RSETF_READ);
183 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
184 heap_insert (ti, ti->tmpbuf, i);
187 rset_close (rset[i], rsfd[i]);
188 rset_delete (rset[i]);
193 int n = ti->indx[ti->ptr[1]];
195 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
200 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
203 rset_close (rset[n], rsfd[n]);
204 rset_delete (rset[n]);
207 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
210 heap_insert (ti, ti->tmpbuf, n);
219 else if (zi->reg->isamc)
223 struct trunc_info *ti;
225 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
227 ti = heap_init (to-from, sizeof(struct it_key),
229 for (i = to-from; --i >= 0; )
231 ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
232 if (isc_pp_read (ispt[i], ti->tmpbuf))
233 heap_insert (ti, ti->tmpbuf, i);
235 isc_pp_close (ispt[i]);
239 int n = ti->indx[ti->ptr[1]];
241 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
243 if (preserve_position)
246 if (isc_pp_read (ispt[n], ti->tmpbuf))
247 heap_insert (ti, ti->tmpbuf, n);
249 isc_pp_close (ispt[n]);
255 if (!isc_pp_read (ispt[n], ti->tmpbuf))
258 isc_pp_close (ispt[n]);
261 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
264 heap_insert (ti, ti->tmpbuf, n);
273 else if (zi->reg->isams)
277 struct trunc_info *ti;
280 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
282 ti = heap_init (to-from, sizeof(struct it_key),
284 for (i = to-from; --i >= 0; )
286 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
287 if (isams_pp_read (ispt[i], ti->tmpbuf))
288 heap_insert (ti, ti->tmpbuf, i);
290 isams_pp_close (ispt[i]);
294 int n = ti->indx[ti->ptr[1]];
296 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
300 if (!isams_pp_read (ispt[n], ti->tmpbuf))
303 isams_pp_close (ispt[n]);
306 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
309 heap_insert (ti, ti->tmpbuf, n);
317 else if (zi->reg->isamb)
321 struct trunc_info *ti;
323 ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
325 ti = heap_init (to-from, sizeof(struct it_key),
327 for (i = to-from; --i >= 0; )
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]);
337 int n = ti->indx[ti->ptr[1]];
339 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
342 if (preserve_position)
345 if (isamb_pp_read (ispt[n], ti->tmpbuf))
346 heap_insert (ti, ti->tmpbuf, n);
348 isamb_pp_close (ispt[n]);
354 if (!isamb_pp_read (ispt[n], ti->tmpbuf))
357 isamb_pp_close (ispt[n]);
360 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
363 heap_insert (ti, ti->tmpbuf, n);
373 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
375 parms.rset_term->nn = nn;
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 parms.rset_term = rset_term_create (term, length, flags, term_type);
417 return rset_create (rset_kind_null, &parms);
423 rset_isams_parms parms;
426 parms.is = zi->reg->isams;
427 parms.rset_term = rset_term_create (term, length, flags,
429 return rset_create (rset_kind_isams, &parms);
431 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
433 else if (zi->reg->isamc)
437 rset_isamc_parms parms;
439 parms.key_size = sizeof(struct it_key);
440 parms.cmp = key_compare_it;
442 parms.is = zi->reg->isamc;
443 parms.rset_term = rset_term_create (term, length, flags,
445 return rset_create (rset_kind_isamc, &parms);
450 rset_m_or_parms parms;
452 parms.key_size = sizeof(struct it_key);
453 parms.cmp = key_compare_it;
454 parms.isc = zi->reg->isamc;
455 parms.isam_positions = isam_p;
456 parms.no_isam_positions = no;
457 parms.no_save_positions = 100000;
458 parms.rset_term = rset_term_create (term, length, flags,
460 return rset_create (rset_kind_m_or, &parms);
463 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
465 else if (zi->reg->isamb)
469 rset_isamb_parms parms;
471 parms.key_size = sizeof(struct it_key);
472 parms.cmp = key_compare_it;
474 parms.is = zi->reg->isamb;
475 parms.rset_term = rset_term_create (term, length, flags,
477 return rset_create (rset_kind_isamb, &parms);
479 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
483 logf (LOG_WARN, "Unknown isam set in rset_trunc");
484 return rset_create (rset_kind_null, NULL);
486 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
487 preserve_position, term_type);