2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1996-11-08 11:10:28 adam
8 * Buffers used during file match got bigger.
9 * Compressed ISAM support everywhere.
10 * Bug fixes regarding masking characters in queries.
11 * Redesigned Regexp-2 queries.
13 * Revision 1.1 1996/11/04 14:07:40 adam
14 * Moved truncation code to trunc.c.
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);
87 struct trunc_info *heap_init (int size, int key_size,
88 int (*cmp)(const void *p1, const void *p2))
90 struct trunc_info *ti = xmalloc (sizeof(*ti));
95 ti->keysize = key_size;
97 ti->indx = xmalloc (size * sizeof(*ti->indx));
98 ti->heap = xmalloc (size * sizeof(*ti->heap));
99 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
100 ti->swapbuf = xmalloc (ti->keysize);
101 ti->tmpbuf = xmalloc (ti->keysize);
102 ti->buf = 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)
121 static RSET rset_trunc_r (ZServerInfo *zi, ISAM_P *isam_p, int from, int to,
126 rset_temp_parms parms;
128 parms.key_size = sizeof(struct it_key);
129 result = rset_create (rset_kind_temp, &parms);
130 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
132 if (to - from > merge_chunk)
136 int i, i_add = (to-from)/merge_chunk + 1;
137 struct trunc_info *ti;
139 int rsmax = (to-from)/i_add + 1;
141 rset = xmalloc (sizeof(*rset) * rsmax);
142 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
144 for (i = from; i < to; i += i_add)
147 rset[rscur] = rset_trunc_r (zi, isam_p, i, i+i_add,
150 rset[rscur] = rset_trunc_r (zi, isam_p, i, to,
154 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
155 for (i = rscur; --i >= 0; )
157 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
158 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
159 heap_insert (ti, ti->tmpbuf, i);
162 rset_close (rset[i], rsfd[i]);
163 rset_delete (rset[i]);
168 int n = ti->indx[ti->ptr[1]];
170 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
174 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
177 rset_close (rset[n], rsfd[n]);
178 rset_delete (rset[n]);
181 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
184 heap_insert (ti, ti->tmpbuf, n);
197 struct trunc_info *ti;
199 ispt = xmalloc (sizeof(*ispt) * (to-from));
201 ti = heap_init (to-from, sizeof(struct it_key),
203 for (i = to-from; --i >= 0; )
205 ispt[i] = is_position (zi->isam, isam_p[from+i]);
206 if (is_readkey (ispt[i], ti->tmpbuf))
207 heap_insert (ti, ti->tmpbuf, i);
209 is_pt_free (ispt[i]);
213 int n = ti->indx[ti->ptr[1]];
215 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
217 /* section that preserve all keys */
219 if (is_readkey (ispt[n], ti->tmpbuf))
220 heap_insert (ti, ti->tmpbuf, n);
222 is_pt_free (ispt[n]);
224 /* section that preserve all keys with unique sysnos */
227 if (!is_readkey (ispt[n], ti->tmpbuf))
230 is_pt_free (ispt[n]);
233 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
236 heap_insert (ti, ti->tmpbuf, n);
249 struct trunc_info *ti;
251 ispt = xmalloc (sizeof(*ispt) * (to-from));
253 ti = heap_init (to-from, sizeof(struct it_key),
255 for (i = to-from; --i >= 0; )
257 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
258 if (isc_pp_read (ispt[i], ti->tmpbuf))
259 heap_insert (ti, ti->tmpbuf, i);
261 isc_pp_close (ispt[i]);
265 int n = ti->indx[ti->ptr[1]];
267 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
269 /* section that preserve all keys */
271 if (is_readkey (ispt[n], ti->tmpbuf))
272 heap_insert (ti, ti->tmpbuf, n);
274 isc_pp_close (ispt[n]);
276 /* section that preserve all keys with unique sysnos */
279 if (!isc_pp_read (ispt[n], ti->tmpbuf))
282 isc_pp_close (ispt[n]);
285 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
288 heap_insert (ti, ti->tmpbuf, n);
297 rset_close (result, result_rsfd);
301 static int isam_trunc_cmp (const void *p1, const void *p2)
303 ISAM_P i1 = *(ISAM_P*) p1;
304 ISAM_P i2 = *(ISAM_P*) p2;
307 d = is_type (i1) - is_type (i2);
310 return is_block (i1) - is_block (i2);
313 static int isamc_trunc_cmp (const void *p1, const void *p2)
315 ISAMC_P i1 = *(ISAMC_P*) p1;
316 ISAMC_P i2 = *(ISAMC_P*) p2;
319 d = isc_type (i1) - isc_type (i2);
322 return isc_block (i1) - isc_block (i2);
325 RSET rset_trunc (ZServerInfo *zi, ISAM_P *isam_p, int no)
330 return rset_create (rset_kind_null, NULL);
333 rset_isam_parms parms;
337 return rset_create (rset_kind_isam, &parms);
339 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
344 return rset_create (rset_kind_null, NULL);
347 rset_isamc_parms parms;
350 parms.is = zi->isamc;
351 return rset_create (rset_kind_isamc, &parms);
353 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
356 logf (LOG_FATAL, "Neither isam nor isamc set in rset_trunc");
357 return rset_trunc_r (zi, isam_p, 0, no, 100);