2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
6 * $Id: trunc.c,v 1.24 2002-04-16 22:31:42 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)
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 ispt[i] = isamd_pp_open (zi->reg->isamd, isam_p[from+i]);
323 if (isamd_pp_read (ispt[i], ti->tmpbuf))
324 heap_insert (ti, ti->tmpbuf, i);
326 isamd_pp_close (ispt[i]);
330 int n = ti->indx[ti->ptr[1]];
332 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
334 /* section that preserve all keys */
336 if (isamd_pp_read (ispt[n], ti->tmpbuf))
337 heap_insert (ti, ti->tmpbuf, n);
339 isamd_pp_close (ispt[n]);
341 /* section that preserve all keys with unique sysnos */
344 if (!isamd_pp_read (ispt[n], ti->tmpbuf))
347 isamd_pp_close (ispt[n]);
350 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
353 heap_insert (ti, ti->tmpbuf, n);
362 else if (zi->reg->isams)
366 struct trunc_info *ti;
368 ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
370 ti = heap_init (to-from, sizeof(struct it_key),
372 for (i = to-from; --i >= 0; )
374 ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
375 if (isams_pp_read (ispt[i], ti->tmpbuf))
376 heap_insert (ti, ti->tmpbuf, i);
378 isams_pp_close (ispt[i]);
382 int n = ti->indx[ti->ptr[1]];
384 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
387 if (!isams_pp_read (ispt[n], ti->tmpbuf))
390 isams_pp_close (ispt[n]);
393 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
396 heap_insert (ti, ti->tmpbuf, n);
404 else if (zi->reg->isamb)
408 struct trunc_info *ti;
410 ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
412 ti = heap_init (to-from, sizeof(struct it_key),
414 for (i = to-from; --i >= 0; )
416 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i]);
417 if (isamb_pp_read (ispt[i], ti->tmpbuf))
418 heap_insert (ti, ti->tmpbuf, i);
420 isamb_pp_close (ispt[i]);
424 int n = ti->indx[ti->ptr[1]];
426 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
428 if (preserve_position)
431 if (isamb_pp_read (ispt[n], ti->tmpbuf))
432 heap_insert (ti, ti->tmpbuf, n);
434 isamb_pp_close (ispt[n]);
440 if (!isamb_pp_read (ispt[n], ti->tmpbuf))
443 isamb_pp_close (ispt[n]);
446 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
449 heap_insert (ti, ti->tmpbuf, n);
459 logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
461 rset_close (result, result_rsfd);
465 static int isams_trunc_cmp (const void *p1, const void *p2)
467 ISAMS_P i1 = *(ISAMS_P*) p1;
468 ISAMS_P i2 = *(ISAMS_P*) p2;
473 static int isam_trunc_cmp (const void *p1, const void *p2)
475 ISAM_P i1 = *(ISAM_P*) p1;
476 ISAM_P i2 = *(ISAM_P*) p2;
479 d = is_type (i1) - is_type (i2);
482 return is_block (i1) - is_block (i2);
485 static int isamc_trunc_cmp (const void *p1, const void *p2)
487 ISAMC_P i1 = *(ISAMC_P*) p1;
488 ISAMC_P i2 = *(ISAMC_P*) p2;
491 d = isc_type (i1) - isc_type (i2);
494 return isc_block (i1) - isc_block (i2);
496 static int isamd_trunc_cmp (const void *p1, const void *p2)
498 ISAMD_P i1 = *(ISAMD_P*) p1;
499 ISAMD_P i2 = *(ISAMD_P*) p2;
502 d = isamd_type (i1) - isamd_type (i2);
505 return isamd_block (i1) - isamd_block (i2);
508 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
509 const char *term, int length, const char *flags,
510 int preserve_position)
512 logf (LOG_DEBUG, "rset_trunc no=%d", no);
515 rset_null_parms parms;
516 parms.rset_term = rset_term_create (term, length, flags);
517 return rset_create (rset_kind_null, &parms);
523 rset_isams_parms parms;
526 parms.is = zi->reg->isams;
527 parms.rset_term = rset_term_create (term, length, flags);
528 return rset_create (rset_kind_isams, &parms);
530 qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
532 else if (zi->reg->isam)
536 rset_isam_parms parms;
539 parms.is = zi->reg->isam;
540 parms.rset_term = rset_term_create (term, length, flags);
541 return rset_create (rset_kind_isam, &parms);
543 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
545 else if (zi->reg->isamc)
549 rset_isamc_parms parms;
551 parms.key_size = sizeof(struct it_key);
552 parms.cmp = key_compare_it;
554 parms.is = zi->reg->isamc;
555 parms.rset_term = rset_term_create (term, length, flags);
556 return rset_create (rset_kind_isamc, &parms);
561 rset_m_or_parms parms;
563 parms.key_size = sizeof(struct it_key);
564 parms.cmp = key_compare_it;
565 parms.isc = zi->reg->isamc;
566 parms.isam_positions = isam_p;
567 parms.no_isam_positions = no;
568 parms.no_save_positions = 100000;
569 parms.rset_term = rset_term_create (term, length, flags);
570 return rset_create (rset_kind_m_or, &parms);
573 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
575 else if (zi->reg->isamd)
579 rset_isamd_parms parms;
582 parms.is = zi->reg->isamd;
583 parms.rset_term = rset_term_create (term, length, flags);
584 return rset_create (rset_kind_isamd, &parms);
586 #if NEW_TRUNC_NOT_DONE_FOR_ISAM_D
589 rset_m_or_parms parms;
591 parms.key_size = sizeof(struct it_key);
592 parms.cmp = key_compare_it;
594 parms.isamd=zi->reg->isamd;
595 parms.isam_positions = isam_p;
596 parms.no_isam_positions = no;
597 parms.no_save_positions = 100000;
598 parms.rset_term = rset_term_create (term, length, flags);
599 return rset_create (rset_kind_m_or, &parms);
602 qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
604 else if (zi->reg->isamb)
608 rset_isamb_parms parms;
610 parms.key_size = sizeof(struct it_key);
611 parms.cmp = key_compare_it;
613 parms.is = zi->reg->isamb;
614 parms.rset_term = rset_term_create (term, length, flags);
615 return rset_create (rset_kind_isamb, &parms);
617 qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
621 logf (LOG_WARN, "Unknown isam set in rset_trunc");
622 return rset_create (rset_kind_null, NULL);
624 return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,