2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
6 * $Id: trunc.c,v 1.26 2002-07-25 13:06:43 adam Exp $
31 int (*cmp)(const void *p1, const void *p2);
38 static void heap_swap (struct trunc_info *ti, int i1, int i2)
43 ti->ptr[i1] = ti->ptr[i2];
47 static void heap_delete (struct trunc_info *ti)
49 int cur = 1, child = 2;
51 heap_swap (ti, 1, ti->heapnum--);
52 while (child <= ti->heapnum) {
53 if (child < ti->heapnum &&
54 (*ti->cmp)(ti->heap[ti->ptr[child]],
55 ti->heap[ti->ptr[1+child]]) > 0)
57 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
58 ti->heap[ti->ptr[child]]) > 0)
60 heap_swap (ti, cur, child);
69 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
73 cur = ++(ti->heapnum);
74 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
75 ti->indx[ti->ptr[cur]] = indx;
77 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
78 ti->heap[ti->ptr[cur]]) > 0)
80 heap_swap (ti, cur, parent);
86 static struct trunc_info *heap_init (int size, int key_size,
87 int (*cmp)(const void *p1,
90 struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
95 ti->keysize = key_size;
97 ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
98 ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
99 ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
100 ti->swapbuf = (char *) xmalloc (ti->keysize);
101 ti->tmpbuf = (char *) xmalloc (ti->keysize);
102 ti->buf = (char *) xmalloc (size * ti->keysize);
103 for (i = size; --i >= 0; )
106 ti->heap[i] = ti->buf + ti->keysize * i;
111 static void heap_close (struct trunc_info *ti)
122 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
123 const char *flags, ISAMS_P *isam_p, int from, int to,
124 int merge_chunk, int preserve_position,
129 rset_temp_parms parms;
131 parms.cmp = key_compare_it;
132 parms.key_size = sizeof(struct it_key);
133 parms.temp_path = res_get (zi->res, "setTmpDir");
134 parms.rset_term = rset_term_create (term, length, flags, term_type);
135 result = rset_create (rset_kind_temp, &parms);
136 result_rsfd = rset_open (result, RSETF_WRITE);
138 if (to - from > merge_chunk)
143 int i, i_add = (to-from)/merge_chunk + 1;
144 struct trunc_info *ti;
146 int rsmax = (to-from)/i_add + 1;
148 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
149 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
151 for (i = from; i < to; i += i_add)
154 rset[rscur] = rset_trunc_r (zi, term, length, flags,
156 merge_chunk, preserve_position,
159 rset[rscur] = rset_trunc_r (zi, term, length, flags,
161 merge_chunk, preserve_position,
165 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
166 for (i = rscur; --i >= 0; )
168 rsfd[i] = rset_open (rset[i], RSETF_READ);
169 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
170 heap_insert (ti, ti->tmpbuf, i);
173 rset_close (rset[i], rsfd[i]);
174 rset_delete (rset[i]);
179 int n = ti->indx[ti->ptr[1]];
181 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
185 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
188 rset_close (rset[n], rsfd[n]);
189 rset_delete (rset[n]);
192 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
195 heap_insert (ti, ti->tmpbuf, n);
204 else if (zi->reg->isam)
208 struct trunc_info *ti;
210 ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
212 ti = heap_init (to-from, sizeof(struct it_key),
214 for (i = to-from; --i >= 0; )
216 ispt[i] = is_position (zi->reg->isam, isam_p[from+i]);
217 if (is_readkey (ispt[i], ti->tmpbuf))
218 heap_insert (ti, ti->tmpbuf, i);
220 is_pt_free (ispt[i]);
224 int n = ti->indx[ti->ptr[1]];
226 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
227 if (preserve_position)
229 /* section that preserve all keys */
231 if (is_readkey (ispt[n], ti->tmpbuf))
232 heap_insert (ti, ti->tmpbuf, n);
234 is_pt_free (ispt[n]);
238 /* section that preserve all keys with unique sysnos */
241 if (!is_readkey (ispt[n], ti->tmpbuf))
244 is_pt_free (ispt[n]);
247 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
250 heap_insert (ti, ti->tmpbuf, n);
259 else if (zi->reg->isamc)
263 struct trunc_info *ti;
265 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
267 ti = heap_init (to-from, sizeof(struct it_key),
269 for (i = to-from; --i >= 0; )
271 ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
272 if (isc_pp_read (ispt[i], ti->tmpbuf))
273 heap_insert (ti, ti->tmpbuf, i);
275 isc_pp_close (ispt[i]);
279 int n = ti->indx[ti->ptr[1]];
281 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
282 if (preserve_position)
285 if (isc_pp_read (ispt[n], ti->tmpbuf))
286 heap_insert (ti, ti->tmpbuf, n);
288 isc_pp_close (ispt[n]);
294 if (!isc_pp_read (ispt[n], ti->tmpbuf))
297 isc_pp_close (ispt[n]);
300 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
303 heap_insert (ti, ti->tmpbuf, n);
313 else if (zi->reg->isamd)
317 struct trunc_info *ti;
319 ispt = (ISAMD_PP *) xmalloc (sizeof(*ispt) * (to-from));
321 ti = heap_init (to-from, sizeof(struct it_key),
323 for (i = to-from; --i >= 0; )
325 logf(LOG_FATAL, "isam_d does not (currently) support truncs");
327 /*ispt[i] = isamd_pp_open (zi->reg->isamd, isam_p[from+i]); */
328 if (isamd_pp_read (ispt[i], ti->tmpbuf))
329 heap_insert (ti, ti->tmpbuf, i);
331 isamd_pp_close (ispt[i]);
335 int n = ti->indx[ti->ptr[1]];
337 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
339 /* section that preserve all keys */
341 if (isamd_pp_read (ispt[n], ti->tmpbuf))
342 heap_insert (ti, ti->tmpbuf, n);
344 isamd_pp_close (ispt[n]);
346 /* section that preserve all keys with unique sysnos */
349 if (!isamd_pp_read (ispt[n], ti->tmpbuf))
352 isamd_pp_close (ispt[n]);
355 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
358 heap_insert (ti, ti->tmpbuf, n);
367 else if (zi->reg->isams)
371 struct trunc_info *ti;
373 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
375 ti = heap_init (to-from, sizeof(struct it_key),
377 for (i = to-from; --i >= 0; )
379 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
380 if (isams_pp_read (ispt[i], ti->tmpbuf))
381 heap_insert (ti, ti->tmpbuf, i);
383 isams_pp_close (ispt[i]);
387 int n = ti->indx[ti->ptr[1]];
389 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
392 if (!isams_pp_read (ispt[n], ti->tmpbuf))
395 isams_pp_close (ispt[n]);
398 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
401 heap_insert (ti, ti->tmpbuf, n);
409 else if (zi->reg->isamb)
413 struct trunc_info *ti;
415 ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
417 ti = heap_init (to-from, sizeof(struct it_key),
419 for (i = to-from; --i >= 0; )
421 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i]);
422 if (isamb_pp_read (ispt[i], ti->tmpbuf))
423 heap_insert (ti, ti->tmpbuf, i);
425 isamb_pp_close (ispt[i]);
429 int n = ti->indx[ti->ptr[1]];
431 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
433 if (preserve_position)
436 if (isamb_pp_read (ispt[n], ti->tmpbuf))
437 heap_insert (ti, ti->tmpbuf, n);
439 isamb_pp_close (ispt[n]);
445 if (!isamb_pp_read (ispt[n], ti->tmpbuf))
448 isamb_pp_close (ispt[n]);
451 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
454 heap_insert (ti, ti->tmpbuf, n);
464 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
466 rset_close (result, result_rsfd);
470 static int isams_trunc_cmp (const void *p1, const void *p2)
472 ISAMS_P i1 = *(ISAMS_P*) p1;
473 ISAMS_P i2 = *(ISAMS_P*) p2;
478 static int isam_trunc_cmp (const void *p1, const void *p2)
480 ISAM_P i1 = *(ISAM_P*) p1;
481 ISAM_P i2 = *(ISAM_P*) p2;
484 d = is_type (i1) - is_type (i2);
487 return is_block (i1) - is_block (i2);
490 static int isamc_trunc_cmp (const void *p1, const void *p2)
492 ISAMC_P i1 = *(ISAMC_P*) p1;
493 ISAMC_P i2 = *(ISAMC_P*) p2;
496 d = isc_type (i1) - isc_type (i2);
499 return isc_block (i1) - isc_block (i2);
501 static int isamd_trunc_cmp (const void *p1, const void *p2)
503 ISAMD_P i1 = *(ISAMD_P*) p1;
504 ISAMD_P i2 = *(ISAMD_P*) p2;
507 d = isamd_type (i1) - isamd_type (i2);
510 return isamd_block (i1) - isamd_block (i2);
513 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
514 const char *term, int length, const char *flags,
515 int preserve_position, int term_type)
517 logf (LOG_DEBUG, "rset_trunc no=%d", no);
520 rset_null_parms parms;
521 parms.rset_term = rset_term_create (term, length, flags, term_type);
522 return rset_create (rset_kind_null, &parms);
528 rset_isams_parms parms;
531 parms.is = zi->reg->isams;
532 parms.rset_term = rset_term_create (term, length, flags,
534 return rset_create (rset_kind_isams, &parms);
536 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
538 else if (zi->reg->isam)
542 rset_isam_parms parms;
545 parms.is = zi->reg->isam;
546 parms.rset_term = rset_term_create (term, length, flags,
548 return rset_create (rset_kind_isam, &parms);
550 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
552 else if (zi->reg->isamc)
556 rset_isamc_parms parms;
558 parms.key_size = sizeof(struct it_key);
559 parms.cmp = key_compare_it;
561 parms.is = zi->reg->isamc;
562 parms.rset_term = rset_term_create (term, length, flags,
564 return rset_create (rset_kind_isamc, &parms);
569 rset_m_or_parms parms;
571 parms.key_size = sizeof(struct it_key);
572 parms.cmp = key_compare_it;
573 parms.isc = zi->reg->isamc;
574 parms.isam_positions = isam_p;
575 parms.no_isam_positions = no;
576 parms.no_save_positions = 100000;
577 parms.rset_term = rset_term_create (term, length, flags,
579 return rset_create (rset_kind_m_or, &parms);
582 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
584 else if (zi->reg->isamd)
588 rset_isamd_parms parms;
590 logf(LOG_FATAL, "isam_d does not (currently) support truncs");
592 /* parms.pos = *isam_p; */
593 parms.is = zi->reg->isamd;
594 parms.rset_term = rset_term_create (term, length, flags,
596 return rset_create (rset_kind_isamd, &parms);
598 #if NEW_TRUNC_NOT_DONE_FOR_ISAM_D
601 rset_m_or_parms parms;
603 parms.key_size = sizeof(struct it_key);
604 parms.cmp = key_compare_it;
606 parms.isamd=zi->reg->isamd;
607 parms.isam_positions = isam_p;
608 parms.no_isam_positions = no;
609 parms.no_save_positions = 100000;
610 parms.rset_term = rset_term_create (term, length, flags);
611 return rset_create (rset_kind_m_or, &parms);
614 qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
616 else if (zi->reg->isamb)
620 rset_isamb_parms parms;
622 parms.key_size = sizeof(struct it_key);
623 parms.cmp = key_compare_it;
625 parms.is = zi->reg->isamb;
626 parms.rset_term = rset_term_create (term, length, flags,
628 return rset_create (rset_kind_isamb, &parms);
630 qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
634 logf (LOG_WARN, "Unknown isam set in rset_trunc");
635 return rset_create (rset_kind_null, NULL);
637 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
638 preserve_position, term_type);