2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1996-12-23 15:30:44 adam
9 * Bug fix: result sets weren't deleted after server shut down.
11 * Revision 1.3 1996/12/20 11:07:14 adam
12 * Multi-or result set.
14 * Revision 1.2 1996/11/08 11:10:28 adam
15 * Buffers used during file match got bigger.
16 * Compressed ISAM support everywhere.
17 * Bug fixes regarding masking characters in queries.
18 * Redesigned Regexp-2 queries.
20 * Revision 1.1 1996/11/04 14:07:40 adam
21 * Moved truncation code to trunc.c.
44 int (*cmp)(const void *p1, const void *p2);
51 static void heap_swap (struct trunc_info *ti, int i1, int i2)
56 ti->ptr[i1] = ti->ptr[i2];
60 static void heap_delete (struct trunc_info *ti)
62 int cur = 1, child = 2;
64 heap_swap (ti, 1, ti->heapnum--);
65 while (child <= ti->heapnum) {
66 if (child < ti->heapnum &&
67 (*ti->cmp)(ti->heap[ti->ptr[child]],
68 ti->heap[ti->ptr[1+child]]) > 0)
70 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
71 ti->heap[ti->ptr[child]]) > 0)
73 heap_swap (ti, cur, child);
82 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
86 cur = ++(ti->heapnum);
87 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
88 ti->indx[ti->ptr[cur]] = indx;
90 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
91 ti->heap[ti->ptr[cur]]) > 0)
93 heap_swap (ti, cur, parent);
100 struct trunc_info *heap_init (int size, int key_size,
101 int (*cmp)(const void *p1, const void *p2))
103 struct trunc_info *ti = xmalloc (sizeof(*ti));
108 ti->keysize = key_size;
110 ti->indx = xmalloc (size * sizeof(*ti->indx));
111 ti->heap = xmalloc (size * sizeof(*ti->heap));
112 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
113 ti->swapbuf = xmalloc (ti->keysize);
114 ti->tmpbuf = xmalloc (ti->keysize);
115 ti->buf = xmalloc (size * ti->keysize);
116 for (i = size; --i >= 0; )
119 ti->heap[i] = ti->buf + ti->keysize * i;
124 static void heap_close (struct trunc_info *ti)
134 static RSET rset_trunc_r (ZServerInfo *zi, ISAM_P *isam_p, int from, int to,
139 rset_temp_parms parms;
141 parms.key_size = sizeof(struct it_key);
142 result = rset_create (rset_kind_temp, &parms);
143 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
145 if (to - from > merge_chunk)
149 int i, i_add = (to-from)/merge_chunk + 1;
150 struct trunc_info *ti;
152 int rsmax = (to-from)/i_add + 1;
154 rset = xmalloc (sizeof(*rset) * rsmax);
155 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
157 for (i = from; i < to; i += i_add)
160 rset[rscur] = rset_trunc_r (zi, isam_p, i, i+i_add,
163 rset[rscur] = rset_trunc_r (zi, isam_p, i, to,
167 ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
168 for (i = rscur; --i >= 0; )
170 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
171 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
172 heap_insert (ti, ti->tmpbuf, i);
175 rset_close (rset[i], rsfd[i]);
176 rset_delete (rset[i]);
181 int n = ti->indx[ti->ptr[1]];
183 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
187 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
190 rset_close (rset[n], rsfd[n]);
191 rset_delete (rset[n]);
194 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
197 heap_insert (ti, ti->tmpbuf, n);
210 struct trunc_info *ti;
212 ispt = xmalloc (sizeof(*ispt) * (to-from));
214 ti = heap_init (to-from, sizeof(struct it_key),
216 for (i = to-from; --i >= 0; )
218 ispt[i] = is_position (zi->isam, isam_p[from+i]);
219 if (is_readkey (ispt[i], ti->tmpbuf))
220 heap_insert (ti, ti->tmpbuf, i);
222 is_pt_free (ispt[i]);
226 int n = ti->indx[ti->ptr[1]];
228 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
230 /* section that preserve all keys */
232 if (is_readkey (ispt[n], ti->tmpbuf))
233 heap_insert (ti, ti->tmpbuf, n);
235 is_pt_free (ispt[n]);
237 /* section that preserve all keys with unique sysnos */
240 if (!is_readkey (ispt[n], ti->tmpbuf))
243 is_pt_free (ispt[n]);
246 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
249 heap_insert (ti, ti->tmpbuf, n);
262 struct trunc_info *ti;
264 ispt = xmalloc (sizeof(*ispt) * (to-from));
266 ti = heap_init (to-from, sizeof(struct it_key),
268 for (i = to-from; --i >= 0; )
270 ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
271 if (isc_pp_read (ispt[i], ti->tmpbuf))
272 heap_insert (ti, ti->tmpbuf, i);
274 isc_pp_close (ispt[i]);
278 int n = ti->indx[ti->ptr[1]];
280 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
282 /* section that preserve all keys */
284 if (is_readkey (ispt[n], ti->tmpbuf))
285 heap_insert (ti, ti->tmpbuf, n);
287 isc_pp_close (ispt[n]);
289 /* section that preserve all keys with unique sysnos */
292 if (!isc_pp_read (ispt[n], ti->tmpbuf))
295 isc_pp_close (ispt[n]);
298 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
301 heap_insert (ti, ti->tmpbuf, n);
310 rset_close (result, result_rsfd);
314 static int isam_trunc_cmp (const void *p1, const void *p2)
316 ISAM_P i1 = *(ISAM_P*) p1;
317 ISAM_P i2 = *(ISAM_P*) p2;
320 d = is_type (i1) - is_type (i2);
323 return is_block (i1) - is_block (i2);
326 static int isamc_trunc_cmp (const void *p1, const void *p2)
328 ISAMC_P i1 = *(ISAMC_P*) p1;
329 ISAMC_P i2 = *(ISAMC_P*) p2;
332 d = isc_type (i1) - isc_type (i2);
335 return isc_block (i1) - isc_block (i2);
338 RSET rset_trunc (ZServerInfo *zi, ISAM_P *isam_p, int no)
340 logf (LOG_LOG, "rset_trunc no=%d", no);
344 return rset_create (rset_kind_null, NULL);
347 rset_isam_parms parms;
351 return rset_create (rset_kind_isam, &parms);
353 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
358 return rset_create (rset_kind_null, NULL);
361 rset_isamc_parms parms;
364 parms.is = zi->isamc;
365 return rset_create (rset_kind_isamc, &parms);
370 rset_m_or_parms parms;
372 logf (LOG_LOG, "new_trunc");
373 parms.key_size = sizeof(struct it_key);
374 parms.cmp = key_compare_it;
375 parms.isc = zi->isamc;
376 parms.isam_positions = isam_p;
377 parms.no_isam_positions = no;
378 parms.no_save_positions = 100;
379 return rset_create (rset_kind_m_or, &parms);
382 qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
385 logf (LOG_FATAL, "Neither isam nor isamc set in rset_trunc");
386 return rset_trunc_r (zi, isam_p, 0, no, 100);