2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1996-12-20 11:07:14 adam
10 * Revision 1.2 1996/11/08 11:10:28 adam
11 * Buffers used during file match got bigger.
12 * Compressed ISAM support everywhere.
13 * Bug fixes regarding masking characters in queries.
14 * Redesigned Regexp-2 queries.
16 * Revision 1.1 1996/11/04 14:07:40 adam
17 * Moved truncation code to trunc.c.
40 int (*cmp)(const void *p1, const void *p2);
47 static void heap_swap (struct trunc_info *ti, int i1, int i2)
52 ti->ptr[i1] = ti->ptr[i2];
56 static void heap_delete (struct trunc_info *ti)
58 int cur = 1, child = 2;
60 heap_swap (ti, 1, ti->heapnum--);
61 while (child <= ti->heapnum) {
62 if (child < ti->heapnum &&
63 (*ti->cmp)(ti->heap[ti->ptr[child]],
64 ti->heap[ti->ptr[1+child]]) > 0)
66 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
67 ti->heap[ti->ptr[child]]) > 0)
69 heap_swap (ti, cur, child);
78 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
82 cur = ++(ti->heapnum);
83 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
84 ti->indx[ti->ptr[cur]] = indx;
86 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
87 ti->heap[ti->ptr[cur]]) > 0)
89 heap_swap (ti, cur, parent);
96 struct trunc_info *heap_init (int size, int key_size,
97 int (*cmp)(const void *p1, const void *p2))
99 struct trunc_info *ti = xmalloc (sizeof(*ti));
104 ti->keysize = key_size;
106 ti->indx = xmalloc (size * sizeof(*ti->indx));
107 ti->heap = xmalloc (size * sizeof(*ti->heap));
108 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
109 ti->swapbuf = xmalloc (ti->keysize);
110 ti->tmpbuf = xmalloc (ti->keysize);
111 ti->buf = xmalloc (size * ti->keysize);
112 for (i = size; --i >= 0; )
115 ti->heap[i] = ti->buf + ti->keysize * i;
120 static void heap_close (struct trunc_info *ti)
130 static RSET rset_trunc_r (ZServerInfo *zi, ISAM_P *isam_p, int from, int to,
135 rset_temp_parms parms;
137 parms.key_size = sizeof(struct it_key);
138 result = rset_create (rset_kind_temp, &parms);
139 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
141 if (to - from > merge_chunk)
145 int i, i_add = (to-from)/merge_chunk + 1;
146 struct trunc_info *ti;
148 int rsmax = (to-from)/i_add + 1;
150 rset = xmalloc (sizeof(*rset) * rsmax);
151 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
153 for (i = from; i < to; i += i_add)
156 rset[rscur] = rset_trunc_r (zi, isam_p, i, i+i_add,
159 rset[rscur] = rset_trunc_r (zi, isam_p, i, to,
163 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
164 for (i = rscur; --i >= 0; )
166 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
167 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
168 heap_insert (ti, ti->tmpbuf, i);
171 rset_close (rset[i], rsfd[i]);
172 rset_delete (rset[i]);
177 int n = ti->indx[ti->ptr[1]];
179 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
183 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
186 rset_close (rset[n], rsfd[n]);
187 rset_delete (rset[n]);
190 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
193 heap_insert (ti, ti->tmpbuf, n);
206 struct trunc_info *ti;
208 ispt = xmalloc (sizeof(*ispt) * (to-from));
210 ti = heap_init (to-from, sizeof(struct it_key),
212 for (i = to-from; --i >= 0; )
214 ispt[i] = is_position (zi->isam, isam_p[from+i]);
215 if (is_readkey (ispt[i], ti->tmpbuf))
216 heap_insert (ti, ti->tmpbuf, i);
218 is_pt_free (ispt[i]);
222 int n = ti->indx[ti->ptr[1]];
224 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
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]);
233 /* section that preserve all keys with unique sysnos */
236 if (!is_readkey (ispt[n], ti->tmpbuf))
239 is_pt_free (ispt[n]);
242 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
245 heap_insert (ti, ti->tmpbuf, n);
258 struct trunc_info *ti;
260 ispt = xmalloc (sizeof(*ispt) * (to-from));
262 ti = heap_init (to-from, sizeof(struct it_key),
264 for (i = to-from; --i >= 0; )
266 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
267 if (isc_pp_read (ispt[i], ti->tmpbuf))
268 heap_insert (ti, ti->tmpbuf, i);
270 isc_pp_close (ispt[i]);
274 int n = ti->indx[ti->ptr[1]];
276 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
278 /* section that preserve all keys */
280 if (is_readkey (ispt[n], ti->tmpbuf))
281 heap_insert (ti, ti->tmpbuf, n);
283 isc_pp_close (ispt[n]);
285 /* section that preserve all keys with unique sysnos */
288 if (!isc_pp_read (ispt[n], ti->tmpbuf))
291 isc_pp_close (ispt[n]);
294 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
297 heap_insert (ti, ti->tmpbuf, n);
306 rset_close (result, result_rsfd);
310 static int isam_trunc_cmp (const void *p1, const void *p2)
312 ISAM_P i1 = *(ISAM_P*) p1;
313 ISAM_P i2 = *(ISAM_P*) p2;
316 d = is_type (i1) - is_type (i2);
319 return is_block (i1) - is_block (i2);
322 static int isamc_trunc_cmp (const void *p1, const void *p2)
324 ISAMC_P i1 = *(ISAMC_P*) p1;
325 ISAMC_P i2 = *(ISAMC_P*) p2;
328 d = isc_type (i1) - isc_type (i2);
331 return isc_block (i1) - isc_block (i2);
334 RSET rset_trunc (ZServerInfo *zi, ISAM_P *isam_p, int no)
336 logf (LOG_LOG, "rset_trunc no=%d", no);
340 return rset_create (rset_kind_null, NULL);
343 rset_isam_parms parms;
347 return rset_create (rset_kind_isam, &parms);
349 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
354 return rset_create (rset_kind_null, NULL);
357 rset_isamc_parms parms;
360 parms.is = zi->isamc;
361 return rset_create (rset_kind_isamc, &parms);
366 rset_m_or_parms parms;
368 logf (LOG_LOG, "new_trunc");
369 parms.key_size = sizeof(struct it_key);
370 parms.cmp = key_compare;
371 parms.isc = zi->isamc;
372 parms.isam_positions = isam_p;
373 parms.no_isam_positions = no;
374 return rset_create (rset_kind_m_or, &parms);
377 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
380 logf (LOG_FATAL, "Neither isam nor isamc set in rset_trunc");
381 return rset_trunc_r (zi, isam_p, 0, no, 100);