2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
6 * $Id: trunc.c,v 1.25 2002-07-12 18:12:22 heikki 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)
128 rset_temp_parms parms;
130 parms.cmp = key_compare_it;
131 parms.key_size = sizeof(struct it_key);
132 parms.temp_path = res_get (zi->res, "setTmpDir");
133 parms.rset_term = rset_term_create (term, length, flags);
134 result = rset_create (rset_kind_temp, &parms);
135 result_rsfd = rset_open (result, RSETF_WRITE);
137 if (to - from > merge_chunk)
142 int i, i_add = (to-from)/merge_chunk + 1;
143 struct trunc_info *ti;
145 int rsmax = (to-from)/i_add + 1;
147 rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
148 rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
150 for (i = from; i < to; i += i_add)
153 rset[rscur] = rset_trunc_r (zi, term, length, flags,
155 merge_chunk, preserve_position);
157 rset[rscur] = rset_trunc_r (zi, term, length, flags,
159 merge_chunk, preserve_position);
162 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
163 for (i = rscur; --i >= 0; )
165 rsfd[i] = rset_open (rset[i], RSETF_READ);
166 if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
167 heap_insert (ti, ti->tmpbuf, i);
170 rset_close (rset[i], rsfd[i]);
171 rset_delete (rset[i]);
176 int n = ti->indx[ti->ptr[1]];
178 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
182 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
185 rset_close (rset[n], rsfd[n]);
186 rset_delete (rset[n]);
189 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
192 heap_insert (ti, ti->tmpbuf, n);
201 else if (zi->reg->isam)
205 struct trunc_info *ti;
207 ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
209 ti = heap_init (to-from, sizeof(struct it_key),
211 for (i = to-from; --i >= 0; )
213 ispt[i] = is_position (zi->reg->isam, isam_p[from+i]);
214 if (is_readkey (ispt[i], ti->tmpbuf))
215 heap_insert (ti, ti->tmpbuf, i);
217 is_pt_free (ispt[i]);
221 int n = ti->indx[ti->ptr[1]];
223 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
224 if (preserve_position)
226 /* section that preserve all keys */
228 if (is_readkey (ispt[n], ti->tmpbuf))
229 heap_insert (ti, ti->tmpbuf, n);
231 is_pt_free (ispt[n]);
235 /* section that preserve all keys with unique sysnos */
238 if (!is_readkey (ispt[n], ti->tmpbuf))
241 is_pt_free (ispt[n]);
244 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
247 heap_insert (ti, ti->tmpbuf, n);
256 else if (zi->reg->isamc)
260 struct trunc_info *ti;
262 ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
264 ti = heap_init (to-from, sizeof(struct it_key),
266 for (i = to-from; --i >= 0; )
268 ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
269 if (isc_pp_read (ispt[i], ti->tmpbuf))
270 heap_insert (ti, ti->tmpbuf, i);
272 isc_pp_close (ispt[i]);
276 int n = ti->indx[ti->ptr[1]];
278 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
279 if (preserve_position)
282 if (isc_pp_read (ispt[n], ti->tmpbuf))
283 heap_insert (ti, ti->tmpbuf, n);
285 isc_pp_close (ispt[n]);
291 if (!isc_pp_read (ispt[n], ti->tmpbuf))
294 isc_pp_close (ispt[n]);
297 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
300 heap_insert (ti, ti->tmpbuf, n);
310 else if (zi->reg->isamd)
314 struct trunc_info *ti;
316 ispt = (ISAMD_PP *) xmalloc (sizeof(*ispt) * (to-from));
318 ti = heap_init (to-from, sizeof(struct it_key),
320 for (i = to-from; --i >= 0; )
322 logf(LOG_FATAL, "isam_d does not (currently) support truncs");
324 /*ispt[i] = isamd_pp_open (zi->reg->isamd, isam_p[from+i]); */
325 if (isamd_pp_read (ispt[i], ti->tmpbuf))
326 heap_insert (ti, ti->tmpbuf, i);
328 isamd_pp_close (ispt[i]);
332 int n = ti->indx[ti->ptr[1]];
334 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
336 /* section that preserve all keys */
338 if (isamd_pp_read (ispt[n], ti->tmpbuf))
339 heap_insert (ti, ti->tmpbuf, n);
341 isamd_pp_close (ispt[n]);
343 /* section that preserve all keys with unique sysnos */
346 if (!isamd_pp_read (ispt[n], ti->tmpbuf))
349 isamd_pp_close (ispt[n]);
352 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
355 heap_insert (ti, ti->tmpbuf, n);
364 else if (zi->reg->isams)
368 struct trunc_info *ti;
370 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
372 ti = heap_init (to-from, sizeof(struct it_key),
374 for (i = to-from; --i >= 0; )
376 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
377 if (isams_pp_read (ispt[i], ti->tmpbuf))
378 heap_insert (ti, ti->tmpbuf, i);
380 isams_pp_close (ispt[i]);
384 int n = ti->indx[ti->ptr[1]];
386 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
389 if (!isams_pp_read (ispt[n], ti->tmpbuf))
392 isams_pp_close (ispt[n]);
395 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
398 heap_insert (ti, ti->tmpbuf, n);
406 else if (zi->reg->isamb)
410 struct trunc_info *ti;
412 ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
414 ti = heap_init (to-from, sizeof(struct it_key),
416 for (i = to-from; --i >= 0; )
418 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i]);
419 if (isamb_pp_read (ispt[i], ti->tmpbuf))
420 heap_insert (ti, ti->tmpbuf, i);
422 isamb_pp_close (ispt[i]);
426 int n = ti->indx[ti->ptr[1]];
428 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
430 if (preserve_position)
433 if (isamb_pp_read (ispt[n], ti->tmpbuf))
434 heap_insert (ti, ti->tmpbuf, n);
436 isamb_pp_close (ispt[n]);
442 if (!isamb_pp_read (ispt[n], ti->tmpbuf))
445 isamb_pp_close (ispt[n]);
448 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
451 heap_insert (ti, ti->tmpbuf, n);
461 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
463 rset_close (result, result_rsfd);
467 static int isams_trunc_cmp (const void *p1, const void *p2)
469 ISAMS_P i1 = *(ISAMS_P*) p1;
470 ISAMS_P i2 = *(ISAMS_P*) p2;
475 static int isam_trunc_cmp (const void *p1, const void *p2)
477 ISAM_P i1 = *(ISAM_P*) p1;
478 ISAM_P i2 = *(ISAM_P*) p2;
481 d = is_type (i1) - is_type (i2);
484 return is_block (i1) - is_block (i2);
487 static int isamc_trunc_cmp (const void *p1, const void *p2)
489 ISAMC_P i1 = *(ISAMC_P*) p1;
490 ISAMC_P i2 = *(ISAMC_P*) p2;
493 d = isc_type (i1) - isc_type (i2);
496 return isc_block (i1) - isc_block (i2);
498 static int isamd_trunc_cmp (const void *p1, const void *p2)
500 ISAMD_P i1 = *(ISAMD_P*) p1;
501 ISAMD_P i2 = *(ISAMD_P*) p2;
504 d = isamd_type (i1) - isamd_type (i2);
507 return isamd_block (i1) - isamd_block (i2);
510 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
511 const char *term, int length, const char *flags,
512 int preserve_position)
514 logf (LOG_DEBUG, "rset_trunc no=%d", no);
517 rset_null_parms parms;
518 parms.rset_term = rset_term_create (term, length, flags);
519 return rset_create (rset_kind_null, &parms);
525 rset_isams_parms parms;
528 parms.is = zi->reg->isams;
529 parms.rset_term = rset_term_create (term, length, flags);
530 return rset_create (rset_kind_isams, &parms);
532 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
534 else if (zi->reg->isam)
538 rset_isam_parms parms;
541 parms.is = zi->reg->isam;
542 parms.rset_term = rset_term_create (term, length, flags);
543 return rset_create (rset_kind_isam, &parms);
545 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
547 else if (zi->reg->isamc)
551 rset_isamc_parms parms;
553 parms.key_size = sizeof(struct it_key);
554 parms.cmp = key_compare_it;
556 parms.is = zi->reg->isamc;
557 parms.rset_term = rset_term_create (term, length, flags);
558 return rset_create (rset_kind_isamc, &parms);
563 rset_m_or_parms parms;
565 parms.key_size = sizeof(struct it_key);
566 parms.cmp = key_compare_it;
567 parms.isc = zi->reg->isamc;
568 parms.isam_positions = isam_p;
569 parms.no_isam_positions = no;
570 parms.no_save_positions = 100000;
571 parms.rset_term = rset_term_create (term, length, flags);
572 return rset_create (rset_kind_m_or, &parms);
575 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
577 else if (zi->reg->isamd)
581 rset_isamd_parms parms;
583 logf(LOG_FATAL, "isam_d does not (currently) support truncs");
585 /* parms.pos = *isam_p; */
586 parms.is = zi->reg->isamd;
587 parms.rset_term = rset_term_create (term, length, flags);
588 return rset_create (rset_kind_isamd, &parms);
590 #if NEW_TRUNC_NOT_DONE_FOR_ISAM_D
593 rset_m_or_parms parms;
595 parms.key_size = sizeof(struct it_key);
596 parms.cmp = key_compare_it;
598 parms.isamd=zi->reg->isamd;
599 parms.isam_positions = isam_p;
600 parms.no_isam_positions = no;
601 parms.no_save_positions = 100000;
602 parms.rset_term = rset_term_create (term, length, flags);
603 return rset_create (rset_kind_m_or, &parms);
606 qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
608 else if (zi->reg->isamb)
612 rset_isamb_parms parms;
614 parms.key_size = sizeof(struct it_key);
615 parms.cmp = key_compare_it;
617 parms.is = zi->reg->isamb;
618 parms.rset_term = rset_term_create (term, length, flags);
619 return rset_create (rset_kind_isamb, &parms);
621 qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
625 logf (LOG_WARN, "Unknown isam set in rset_trunc");
626 return rset_create (rset_kind_null, NULL);
628 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,