1 /* $Id: trunc.c,v 1.28.2.4 2005-10-22 17:27:28 adam Exp $
2 Copyright (C) 1995-2005
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
46 int (*cmp)(const void *p1, const void *p2);
53 static void heap_swap (struct trunc_info *ti, int i1, int i2)
58 ti->ptr[i1] = ti->ptr[i2];
62 static void heap_delete (struct trunc_info *ti)
64 int cur = 1, child = 2;
66 heap_swap (ti, 1, ti->heapnum--);
67 while (child <= ti->heapnum) {
68 if (child < ti->heapnum &&
69 (*ti->cmp)(ti->heap[ti->ptr[child]],
70 ti->heap[ti->ptr[1+child]]) > 0)
72 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
73 ti->heap[ti->ptr[child]]) > 0)
75 heap_swap (ti, cur, child);
84 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
88 cur = ++(ti->heapnum);
89 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
90 ti->indx[ti->ptr[cur]] = indx;
92 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
93 ti->heap[ti->ptr[cur]]) > 0)
95 heap_swap (ti, cur, parent);
101 static struct trunc_info *heap_init (int size, int key_size,
102 int (*cmp)(const void *p1,
105 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
110 ti->keysize = key_size;
112 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
113 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
114 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
115 ti->swapbuf = (char *) xmalloc (ti->keysize);
116 ti->tmpbuf = (char *) xmalloc (ti->keysize);
117 ti->buf = (char *) xmalloc (size * ti->keysize);
118 for (i = size; --i >= 0; )
121 ti->heap[i] = ti->buf + ti->keysize * i;
126 static void heap_close (struct trunc_info *ti)
137 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
138 const char *flags, ISAMS_P *isam_p, int from, int to,
139 int merge_chunk, int preserve_position,
144 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 parms.rset_term = rset_term_create (term, length, flags, term_type);
151 result = rset_create (rset_kind_temp, &parms);
152 result_rsfd = rset_open (result, RSETF_WRITE);
154 if (to - from > merge_chunk)
159 int i, i_add = (to-from)/merge_chunk + 1;
160 struct trunc_info *ti;
162 int rsmax = (to-from)/i_add + 1;
163 int cmp_border = preserve_position ? 0 : 1;
165 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
166 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
168 for (i = from; i < to; i += i_add)
171 rset[rscur] = rset_trunc_r (zi, term, length, flags,
173 merge_chunk, preserve_position,
176 rset[rscur] = rset_trunc_r (zi, term, length, flags,
178 merge_chunk, preserve_position,
182 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
183 for (i = rscur; --i >= 0; )
185 rsfd[i] = rset_open (rset[i], RSETF_READ);
186 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
187 heap_insert (ti, ti->tmpbuf, i);
190 rset_close (rset[i], rsfd[i]);
191 rset_delete (rset[i]);
196 int n = ti->indx[ti->ptr[1]];
198 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
203 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
206 rset_close (rset[n], rsfd[n]);
207 rset_delete (rset[n]);
210 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > cmp_border)
213 heap_insert (ti, ti->tmpbuf, n);
222 else if (zi->reg->isam)
226 struct trunc_info *ti;
228 ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
230 ti = heap_init (to-from, sizeof(struct it_key),
232 for (i = to-from; --i >= 0; )
234 ispt[i] = is_position (zi->reg->isam, isam_p[from+i]);
235 if (is_readkey (ispt[i], ti->tmpbuf))
236 heap_insert (ti, ti->tmpbuf, i);
238 is_pt_free (ispt[i]);
242 int n = ti->indx[ti->ptr[1]];
244 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
246 if (preserve_position)
248 /* section that preserve all keys */
250 if (is_readkey (ispt[n], ti->tmpbuf))
251 heap_insert (ti, ti->tmpbuf, n);
253 is_pt_free (ispt[n]);
257 /* section that preserve all keys with unique sysnos */
260 if (!is_readkey (ispt[n], ti->tmpbuf))
263 is_pt_free (ispt[n]);
266 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
269 heap_insert (ti, ti->tmpbuf, n);
278 else if (zi->reg->isamc)
282 struct trunc_info *ti;
284 ispt = (ISAMC_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] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
291 if (isc_pp_read (ispt[i], ti->tmpbuf))
292 heap_insert (ti, ti->tmpbuf, i);
294 isc_pp_close (ispt[i]);
298 int n = ti->indx[ti->ptr[1]];
300 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
302 if (preserve_position)
305 if (isc_pp_read (ispt[n], ti->tmpbuf))
306 heap_insert (ti, ti->tmpbuf, n);
308 isc_pp_close (ispt[n]);
314 if (!isc_pp_read (ispt[n], ti->tmpbuf))
317 isc_pp_close (ispt[n]);
320 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
323 heap_insert (ti, ti->tmpbuf, n);
333 else if (zi->reg->isamd)
337 struct trunc_info *ti;
339 ispt = (ISAMD_PP *) xmalloc (sizeof(*ispt) * (to-from));
341 ti = heap_init (to-from, sizeof(struct it_key),
343 for (i = to-from; --i >= 0; )
345 logf(LOG_FATAL, "isam_d does not (currently) support truncs");
347 /*ispt[i] = isamd_pp_open (zi->reg->isamd, isam_p[from+i]); */
348 if (isamd_pp_read (ispt[i], ti->tmpbuf))
349 heap_insert (ti, ti->tmpbuf, i);
351 isamd_pp_close (ispt[i]);
355 int n = ti->indx[ti->ptr[1]];
357 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
360 /* section that preserve all keys */
362 if (isamd_pp_read (ispt[n], ti->tmpbuf))
363 heap_insert (ti, ti->tmpbuf, n);
365 isamd_pp_close (ispt[n]);
367 /* section that preserve all keys with unique sysnos */
370 if (!isamd_pp_read (ispt[n], ti->tmpbuf))
373 isamd_pp_close (ispt[n]);
376 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
379 heap_insert (ti, ti->tmpbuf, n);
388 else if (zi->reg->isams)
392 struct trunc_info *ti;
395 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
397 ti = heap_init (to-from, sizeof(struct it_key),
399 for (i = to-from; --i >= 0; )
401 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
402 if (isams_pp_read (ispt[i], ti->tmpbuf))
403 heap_insert (ti, ti->tmpbuf, i);
405 isams_pp_close (ispt[i]);
409 int n = ti->indx[ti->ptr[1]];
411 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
415 if (!isams_pp_read (ispt[n], ti->tmpbuf))
418 isams_pp_close (ispt[n]);
421 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
424 heap_insert (ti, ti->tmpbuf, n);
432 else if (zi->reg->isamb)
436 struct trunc_info *ti;
438 ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
440 ti = heap_init (to-from, sizeof(struct it_key),
442 for (i = to-from; --i >= 0; )
444 if (isam_p[from+i]) {
445 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i]);
446 if (isamb_pp_read (ispt[i], ti->tmpbuf))
447 heap_insert (ti, ti->tmpbuf, i);
449 isamb_pp_close (ispt[i]);
454 int n = ti->indx[ti->ptr[1]];
456 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
459 if (preserve_position)
462 if (isamb_pp_read (ispt[n], ti->tmpbuf))
463 heap_insert (ti, ti->tmpbuf, n);
465 isamb_pp_close (ispt[n]);
471 if (!isamb_pp_read (ispt[n], ti->tmpbuf))
474 isamb_pp_close (ispt[n]);
477 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
480 heap_insert (ti, ti->tmpbuf, n);
490 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
492 parms.rset_term->nn = nn;
493 rset_close (result, result_rsfd);
497 static int isams_trunc_cmp (const void *p1, const void *p2)
499 ISAMS_P i1 = *(ISAMS_P*) p1;
500 ISAMS_P i2 = *(ISAMS_P*) p2;
505 static int isam_trunc_cmp (const void *p1, const void *p2)
507 ISAM_P i1 = *(ISAM_P*) p1;
508 ISAM_P i2 = *(ISAM_P*) p2;
511 d = is_type (i1) - is_type (i2);
514 return is_block (i1) - is_block (i2);
517 static int isamc_trunc_cmp (const void *p1, const void *p2)
519 ISAMC_P i1 = *(ISAMC_P*) p1;
520 ISAMC_P i2 = *(ISAMC_P*) p2;
523 d = isc_type (i1) - isc_type (i2);
526 return isc_block (i1) - isc_block (i2);
528 static int isamd_trunc_cmp (const void *p1, const void *p2)
530 ISAMD_P i1 = *(ISAMD_P*) p1;
531 ISAMD_P i2 = *(ISAMD_P*) p2;
534 d = isamd_type (i1) - isamd_type (i2);
537 return isamd_block (i1) - isamd_block (i2);
540 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
541 const char *term, int length, const char *flags,
542 int preserve_position, int term_type)
544 logf (LOG_DEBUG, "rset_trunc no=%d", no);
547 rset_null_parms parms;
548 parms.rset_term = rset_term_create (term, length, flags, term_type);
549 return rset_create (rset_kind_null, &parms);
555 rset_isams_parms parms;
558 parms.is = zi->reg->isams;
559 parms.rset_term = rset_term_create (term, length, flags,
561 return rset_create (rset_kind_isams, &parms);
563 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
565 else if (zi->reg->isam)
569 rset_isam_parms parms;
572 parms.is = zi->reg->isam;
573 parms.rset_term = rset_term_create (term, length, flags,
575 return rset_create (rset_kind_isam, &parms);
577 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
579 else if (zi->reg->isamc)
583 rset_isamc_parms parms;
585 parms.key_size = sizeof(struct it_key);
586 parms.cmp = key_compare_it;
588 parms.is = zi->reg->isamc;
589 parms.rset_term = rset_term_create (term, length, flags,
591 return rset_create (rset_kind_isamc, &parms);
596 rset_m_or_parms parms;
598 parms.key_size = sizeof(struct it_key);
599 parms.cmp = key_compare_it;
600 parms.isc = zi->reg->isamc;
601 parms.isam_positions = isam_p;
602 parms.no_isam_positions = no;
603 parms.no_save_positions = 100000;
604 parms.rset_term = rset_term_create (term, length, flags,
606 return rset_create (rset_kind_m_or, &parms);
609 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
611 else if (zi->reg->isamd)
615 rset_isamd_parms parms;
617 logf(LOG_FATAL, "isam_d does not (currently) support truncs");
619 /* parms.pos = *isam_p; */
620 parms.is = zi->reg->isamd;
621 parms.rset_term = rset_term_create (term, length, flags,
623 return rset_create (rset_kind_isamd, &parms);
625 #if NEW_TRUNC_NOT_DONE_FOR_ISAM_D
628 rset_m_or_parms parms;
630 parms.key_size = sizeof(struct it_key);
631 parms.cmp = key_compare_it;
633 parms.isamd=zi->reg->isamd;
634 parms.isam_positions = isam_p;
635 parms.no_isam_positions = no;
636 parms.no_save_positions = 100000;
637 parms.rset_term = rset_term_create (term, length, flags);
638 return rset_create (rset_kind_m_or, &parms);
641 qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
643 else if (zi->reg->isamb)
647 rset_isamb_parms parms;
649 parms.key_size = sizeof(struct it_key);
650 parms.cmp = key_compare_it;
652 parms.is = zi->reg->isamb;
653 parms.rset_term = rset_term_create (term, length, flags,
655 if (res_get(zi->res, "rsetforward"))
656 return rset_create (rset_kind_isamb_forward, &parms);
658 return rset_create (rset_kind_isamb, &parms);
660 qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
664 logf (LOG_WARN, "Unknown isam set in rset_trunc");
665 return rset_create (rset_kind_null, NULL);
667 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
668 preserve_position, term_type);