1 /* $Id: trunc.c,v 1.29 2004-08-04 08:35:23 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
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 parms.rset_term = rset_term_create (term, length, flags, term_type);
150 result = rset_create (rset_kind_temp, &parms);
151 result_rsfd = rset_open (result, RSETF_WRITE);
153 if (to - from > merge_chunk)
158 int i, i_add = (to-from)/merge_chunk + 1;
159 struct trunc_info *ti;
161 int rsmax = (to-from)/i_add + 1;
163 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
164 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
166 for (i = from; i < to; i += i_add)
169 rset[rscur] = rset_trunc_r (zi, term, length, flags,
171 merge_chunk, preserve_position,
174 rset[rscur] = rset_trunc_r (zi, term, length, flags,
176 merge_chunk, preserve_position,
180 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
181 for (i = rscur; --i >= 0; )
183 rsfd[i] = rset_open (rset[i], RSETF_READ);
184 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
185 heap_insert (ti, ti->tmpbuf, i);
188 rset_close (rset[i], rsfd[i]);
189 rset_delete (rset[i]);
194 int n = ti->indx[ti->ptr[1]];
196 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
201 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
204 rset_close (rset[n], rsfd[n]);
205 rset_delete (rset[n]);
208 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
211 heap_insert (ti, ti->tmpbuf, n);
220 else if (zi->reg->isam)
224 struct trunc_info *ti;
226 ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
228 ti = heap_init (to-from, sizeof(struct it_key),
230 for (i = to-from; --i >= 0; )
232 ispt[i] = is_position (zi->reg->isam, isam_p[from+i]);
233 if (is_readkey (ispt[i], ti->tmpbuf))
234 heap_insert (ti, ti->tmpbuf, i);
236 is_pt_free (ispt[i]);
240 int n = ti->indx[ti->ptr[1]];
242 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
244 if (preserve_position)
246 /* section that preserve all keys */
248 if (is_readkey (ispt[n], ti->tmpbuf))
249 heap_insert (ti, ti->tmpbuf, n);
251 is_pt_free (ispt[n]);
255 /* section that preserve all keys with unique sysnos */
258 if (!is_readkey (ispt[n], ti->tmpbuf))
261 is_pt_free (ispt[n]);
264 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
267 heap_insert (ti, ti->tmpbuf, n);
276 else if (zi->reg->isamc)
280 struct trunc_info *ti;
282 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
284 ti = heap_init (to-from, sizeof(struct it_key),
286 for (i = to-from; --i >= 0; )
288 ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
289 if (isc_pp_read (ispt[i], ti->tmpbuf))
290 heap_insert (ti, ti->tmpbuf, i);
292 isc_pp_close (ispt[i]);
296 int n = ti->indx[ti->ptr[1]];
298 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
300 if (preserve_position)
303 if (isc_pp_read (ispt[n], ti->tmpbuf))
304 heap_insert (ti, ti->tmpbuf, n);
306 isc_pp_close (ispt[n]);
312 if (!isc_pp_read (ispt[n], ti->tmpbuf))
315 isc_pp_close (ispt[n]);
318 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
321 heap_insert (ti, ti->tmpbuf, n);
330 else if (zi->reg->isams)
334 struct trunc_info *ti;
337 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
339 ti = heap_init (to-from, sizeof(struct it_key),
341 for (i = to-from; --i >= 0; )
343 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
344 if (isams_pp_read (ispt[i], ti->tmpbuf))
345 heap_insert (ti, ti->tmpbuf, i);
347 isams_pp_close (ispt[i]);
351 int n = ti->indx[ti->ptr[1]];
353 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
357 if (!isams_pp_read (ispt[n], ti->tmpbuf))
360 isams_pp_close (ispt[n]);
363 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
366 heap_insert (ti, ti->tmpbuf, n);
374 else if (zi->reg->isamb)
378 struct trunc_info *ti;
380 ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
382 ti = heap_init (to-from, sizeof(struct it_key),
384 for (i = to-from; --i >= 0; )
386 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i]);
387 if (isamb_pp_read (ispt[i], ti->tmpbuf))
388 heap_insert (ti, ti->tmpbuf, i);
390 isamb_pp_close (ispt[i]);
394 int n = ti->indx[ti->ptr[1]];
396 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
399 if (preserve_position)
402 if (isamb_pp_read (ispt[n], ti->tmpbuf))
403 heap_insert (ti, ti->tmpbuf, n);
405 isamb_pp_close (ispt[n]);
411 if (!isamb_pp_read (ispt[n], ti->tmpbuf))
414 isamb_pp_close (ispt[n]);
417 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
420 heap_insert (ti, ti->tmpbuf, n);
430 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
432 parms.rset_term->nn = nn;
433 rset_close (result, result_rsfd);
437 static int isams_trunc_cmp (const void *p1, const void *p2)
439 ISAMS_P i1 = *(ISAMS_P*) p1;
440 ISAMS_P i2 = *(ISAMS_P*) p2;
445 static int isam_trunc_cmp (const void *p1, const void *p2)
447 ISAM_P i1 = *(ISAM_P*) p1;
448 ISAM_P i2 = *(ISAM_P*) p2;
451 d = is_type (i1) - is_type (i2);
454 return is_block (i1) - is_block (i2);
457 static int isamc_trunc_cmp (const void *p1, const void *p2)
459 ISAMC_P i1 = *(ISAMC_P*) p1;
460 ISAMC_P i2 = *(ISAMC_P*) p2;
463 d = isc_type (i1) - isc_type (i2);
466 return isc_block (i1) - isc_block (i2);
469 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
470 const char *term, int length, const char *flags,
471 int preserve_position, int term_type)
473 logf (LOG_DEBUG, "rset_trunc no=%d", no);
476 rset_null_parms parms;
477 parms.rset_term = rset_term_create (term, length, flags, term_type);
478 return rset_create (rset_kind_null, &parms);
484 rset_isams_parms parms;
487 parms.is = zi->reg->isams;
488 parms.rset_term = rset_term_create (term, length, flags,
490 return rset_create (rset_kind_isams, &parms);
492 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
494 else if (zi->reg->isam)
498 rset_isam_parms parms;
501 parms.is = zi->reg->isam;
502 parms.rset_term = rset_term_create (term, length, flags,
504 return rset_create (rset_kind_isam, &parms);
506 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
508 else if (zi->reg->isamc)
512 rset_isamc_parms parms;
514 parms.key_size = sizeof(struct it_key);
515 parms.cmp = key_compare_it;
517 parms.is = zi->reg->isamc;
518 parms.rset_term = rset_term_create (term, length, flags,
520 return rset_create (rset_kind_isamc, &parms);
525 rset_m_or_parms parms;
527 parms.key_size = sizeof(struct it_key);
528 parms.cmp = key_compare_it;
529 parms.isc = zi->reg->isamc;
530 parms.isam_positions = isam_p;
531 parms.no_isam_positions = no;
532 parms.no_save_positions = 100000;
533 parms.rset_term = rset_term_create (term, length, flags,
535 return rset_create (rset_kind_m_or, &parms);
538 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
540 else if (zi->reg->isamb)
544 rset_isamb_parms parms;
546 parms.key_size = sizeof(struct it_key);
547 parms.cmp = key_compare_it;
549 parms.is = zi->reg->isamb;
550 parms.rset_term = rset_term_create (term, length, flags,
552 return rset_create (rset_kind_isamb, &parms);
554 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
558 logf (LOG_WARN, "Unknown isam set in rset_trunc");
559 return rset_create (rset_kind_null, NULL);
561 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
562 preserve_position, term_type);