2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1996-11-04 14:07:40 adam
8 * Moved truncation code to trunc.c.
25 int (*cmp)(const void *p1, const void *p2);
32 static void heap_swap (struct trunc_info *ti, int i1, int i2)
37 ti->ptr[i1] = ti->ptr[i2];
41 static void heap_delete (struct trunc_info *ti)
43 int cur = 1, child = 2;
45 heap_swap (ti, 1, ti->heapnum--);
46 while (child <= ti->heapnum) {
47 if (child < ti->heapnum &&
48 (*ti->cmp)(ti->heap[ti->ptr[child]],
49 ti->heap[ti->ptr[1+child]]) > 0)
51 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
52 ti->heap[ti->ptr[child]]) > 0)
54 heap_swap (ti, cur, child);
63 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
67 cur = ++(ti->heapnum);
68 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
69 ti->indx[ti->ptr[cur]] = indx;
71 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
72 ti->heap[ti->ptr[cur]]) > 0)
74 heap_swap (ti, cur, parent);
81 struct trunc_info *heap_init (int size, int key_size,
82 int (*cmp)(const void *p1, const void *p2))
84 struct trunc_info *ti = xmalloc (sizeof(*ti));
89 ti->keysize = key_size;
91 ti->indx = xmalloc (size * sizeof(*ti->indx));
92 ti->heap = xmalloc (size * sizeof(*ti->heap));
93 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
94 ti->swapbuf = xmalloc (ti->keysize);
95 ti->tmpbuf = xmalloc (ti->keysize);
96 ti->buf = xmalloc (size * ti->keysize);
97 for (i = size; --i >= 0; )
100 ti->heap[i] = ti->buf + ti->keysize * i;
105 static void heap_close (struct trunc_info *ti)
115 static RSET rset_trunc_r (ZServerInfo *zi, ISAM_P *isam_p, int from, int to,
120 rset_temp_parms parms;
122 parms.key_size = sizeof(struct it_key);
123 result = rset_create (rset_kind_temp, &parms);
124 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
126 if (to - from > merge_chunk)
130 int i, i_add = (to-from)/merge_chunk + 1;
131 struct trunc_info *ti;
133 int rsmax = (to-from)/i_add + 1;
135 rset = xmalloc (sizeof(*rset) * rsmax);
136 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
138 for (i = from; i < to; i += i_add)
141 rset[rscur] = rset_trunc_r (zi, isam_p, i, i+i_add,
144 rset[rscur] = rset_trunc_r (zi, isam_p, i, to,
148 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
149 for (i = rscur; --i >= 0; )
151 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
152 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
153 heap_insert (ti, ti->tmpbuf, i);
156 rset_close (rset[i], rsfd[i]);
157 rset_delete (rset[i]);
162 int n = ti->indx[ti->ptr[1]];
164 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
168 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
171 rset_close (rset[n], rsfd[n]);
172 rset_delete (rset[n]);
175 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
178 heap_insert (ti, ti->tmpbuf, n);
191 struct trunc_info *ti;
193 ispt = xmalloc (sizeof(*ispt) * (to-from));
195 ti = heap_init (to-from, sizeof(struct it_key),
197 for (i = to-from; --i >= 0; )
199 ispt[i] = is_position (zi->isam, isam_p[from+i]);
200 if (is_readkey (ispt[i], ti->tmpbuf))
201 heap_insert (ti, ti->tmpbuf, i);
203 is_pt_free (ispt[i]);
207 int n = ti->indx[ti->ptr[1]];
209 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
211 /* section that preserve all keys */
213 if (is_readkey (ispt[n], ti->tmpbuf))
214 heap_insert (ti, ti->tmpbuf, n);
216 is_pt_free (ispt[n]);
218 /* section that preserve all keys with unique sysnos */
221 if (!is_readkey (ispt[n], ti->tmpbuf))
224 is_pt_free (ispt[n]);
227 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
230 heap_insert (ti, ti->tmpbuf, n);
243 struct trunc_info *ti;
245 ispt = xmalloc (sizeof(*ispt) * (to-from));
247 ti = heap_init (to-from, sizeof(struct it_key),
249 for (i = to-from; --i >= 0; )
251 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
252 if (isc_read_key (ispt[i], ti->tmpbuf))
253 heap_insert (ti, ti->tmpbuf, i);
255 isc_pp_close (ispt[i]);
259 int n = ti->indx[ti->ptr[1]];
261 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
263 /* section that preserve all keys */
265 if (is_readkey (ispt[n], ti->tmpbuf))
266 heap_insert (ti, ti->tmpbuf, n);
268 isc_pp_close (ispt[n]);
270 /* section that preserve all keys with unique sysnos */
273 if (!isc_read_key (ispt[n], ti->tmpbuf))
276 isc_pp_close (ispt[n]);
279 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
282 heap_insert (ti, ti->tmpbuf, n);
291 rset_close (result, result_rsfd);
295 static int isam_trunc_cmp (const void *p1, const void *p2)
297 ISAM_P i1 = *(ISAM_P*) p1;
298 ISAM_P i2 = *(ISAM_P*) p2;
301 d = is_type (i1) - is_type (i2);
304 return is_block (i1) - is_block (i2);
307 static int isamc_trunc_cmp (const void *p1, const void *p2)
309 ISAMC_P i1 = *(ISAMC_P*) p1;
310 ISAMC_P i2 = *(ISAMC_P*) p2;
313 d = isc_type (i1) - isc_type (i2);
316 return isc_block (i1) - isc_block (i2);
319 RSET rset_trunc (ZServerInfo *zi, ISAM_P *isam_p, int no)
324 return rset_create (rset_kind_null, NULL);
327 rset_isam_parms parms;
331 return rset_create (rset_kind_isam, &parms);
333 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
338 return rset_create (rset_kind_null, NULL);
341 rset_isamc_parms parms;
344 parms.is = zi->isamc;
345 return rset_create (rset_kind_isamc, &parms);
347 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
350 logf (LOG_FATAL, "Neither isam nor isamc set in rset_trunc");
351 return rset_trunc_r (zi, isam_p, 0, no, 100);