+ RSFD *rsfd;
+ RSET *rset;
+ int i, i_add = (to-from)/merge_chunk + 1;
+ struct trunc_info *ti;
+ int rscur = 0;
+ int rsmax = (to-from)/i_add + 1;
+
+ rset = xmalloc (sizeof(*rset) * rsmax);
+ rsfd = xmalloc (sizeof(*rsfd) * rsmax);
+
+ for (i = from; i < to; i += i_add)
+ {
+ if (i_add <= to - i)
+ rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
+ merge_chunk);
+ else
+ rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
+ merge_chunk);
+ rscur++;
+ }
+ ti = heap_init (rscur, sizeof(struct it_key), key_compare);
+ for (i = rscur; --i >= 0; )
+ {
+ rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
+ if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
+ heap_insert (ti, ti->tmpbuf, i);
+ else
+ {
+ rset_close (rset[i], rsfd[i]);
+ rset_delete (rset[i]);
+ }
+ }
+ while (ti->heapnum)
+ {
+ int n = ti->indx[ti->ptr[1]];
+
+ rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
+
+ while (1)
+ {
+ if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
+ {
+ heap_delete (ti);
+ rset_close (rset[n], rsfd[n]);
+ rset_delete (rset[n]);
+ break;
+ }
+ if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
+ {
+ heap_delete (ti);
+ heap_insert (ti, ti->tmpbuf, n);
+ break;
+ }
+ }
+ }
+ xfree (rset);
+ xfree (rsfd);
+ heap_close (ti);