1 /* $Id: trunc.c,v 1.33 2004-08-11 13:35:04 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 if (isam_p[from+i]) {
330 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i]);
331 if (isamb_pp_read (ispt[i], ti->tmpbuf))
332 heap_insert (ti, ti->tmpbuf, i);
334 isamb_pp_close (ispt[i]);
339 int n = ti->indx[ti->ptr[1]];
341 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
344 if (preserve_position)
347 if (isamb_pp_read (ispt[n], ti->tmpbuf))
348 heap_insert (ti, ti->tmpbuf, n);
350 isamb_pp_close (ispt[n]);
356 if (!isamb_pp_read (ispt[n], ti->tmpbuf))
359 isamb_pp_close (ispt[n]);
362 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
365 heap_insert (ti, ti->tmpbuf, n);
375 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
377 parms.rset_term->nn = nn;
378 rset_close (result, result_rsfd);
382 static int isams_trunc_cmp (const void *p1, const void *p2)
384 ISAMS_P i1 = *(ISAMS_P*) p1;
385 ISAMS_P i2 = *(ISAMS_P*) p2;
394 static int isamc_trunc_cmp (const void *p1, const void *p2)
396 ISAMC_P i1 = *(ISAMC_P*) p1;
397 ISAMC_P i2 = *(ISAMC_P*) p2;
400 d = (isc_type (i1) - isc_type (i2));
402 d = isc_block (i1) - isc_block (i2);
410 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
411 const char *term, int length, const char *flags,
412 int preserve_position, int term_type)
414 logf (LOG_DEBUG, "rset_trunc no=%d", no);
417 rset_null_parms parms;
418 parms.rset_term = rset_term_create (term, length, flags, term_type);
419 return rset_create (rset_kind_null, &parms);
425 rset_isams_parms parms;
428 parms.is = zi->reg->isams;
429 parms.rset_term = rset_term_create (term, length, flags,
431 return rset_create (rset_kind_isams, &parms);
433 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
435 else if (zi->reg->isamc)
439 rset_isamc_parms parms;
441 parms.key_size = sizeof(struct it_key);
442 parms.cmp = key_compare_it;
444 parms.is = zi->reg->isamc;
445 parms.rset_term = rset_term_create (term, length, flags,
447 return rset_create (rset_kind_isamc, &parms);
452 rset_m_or_parms parms;
454 parms.key_size = sizeof(struct it_key);
455 parms.cmp = key_compare_it;
456 parms.isc = zi->reg->isamc;
457 parms.isam_positions = isam_p;
458 parms.no_isam_positions = no;
459 parms.no_save_positions = 100000;
460 parms.rset_term = rset_term_create (term, length, flags,
462 return rset_create (rset_kind_m_or, &parms);
465 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
467 else if (zi->reg->isamb)
471 rset_isamb_parms parms;
473 parms.key_size = sizeof(struct it_key);
474 parms.cmp = key_compare_it;
476 parms.is = zi->reg->isamb;
477 parms.rset_term = rset_term_create (term, length, flags,
479 return rset_create (rset_kind_isamb, &parms);
481 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
485 logf (LOG_WARN, "Unknown isam set in rset_trunc");
486 return rset_create (rset_kind_null, NULL);
488 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
489 preserve_position, term_type);