Bug fix: memory leak.
[idzebra-moved-to-github.git] / index / trunc.c
1 /*
2  * Copyright (C) 1994-1997, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: trunc.c,v $
7  * Revision 1.8  1997-10-31 12:34:27  adam
8  * Bug fix: memory leak.
9  *
10  * Revision 1.7  1997/09/29 09:07:29  adam
11  * Minor change.
12  *
13  * Revision 1.6  1997/09/22 12:39:06  adam
14  * Added get_pos method for the ranked result sets.
15  *
16  * Revision 1.5  1997/09/17 12:19:17  adam
17  * Zebra version corresponds to YAZ version 1.4.
18  * Changed Zebra server so that it doesn't depend on global common_resource.
19  *
20  * Revision 1.4  1996/12/23 15:30:44  adam
21  * Work on truncation.
22  * Bug fix: result sets weren't deleted after server shut down.
23  *
24  * Revision 1.3  1996/12/20 11:07:14  adam
25  * Multi-or result set.
26  *
27  * Revision 1.2  1996/11/08 11:10:28  adam
28  * Buffers used during file match got bigger.
29  * Compressed ISAM support everywhere.
30  * Bug fixes regarding masking characters in queries.
31  * Redesigned Regexp-2 queries.
32  *
33  * Revision 1.1  1996/11/04 14:07:40  adam
34  * Moved truncation code to trunc.c.
35  *
36  */
37 #include <stdio.h>
38 #include <assert.h>
39
40 #include "zserver.h"
41 #include <rstemp.h>
42 #include <rsisam.h>
43 #include <rsisamc.h>
44 #include <rsnull.h>
45
46 #define NEW_TRUNC 1
47
48 #if NEW_TRUNC
49 #include <rsm_or.h>
50 #endif
51
52 struct trunc_info {
53     int  *ptr;
54     int  *indx;
55     char **heap;
56     int  heapnum;
57     int  (*cmp)(const void *p1, const void *p2);
58     int  keysize;
59     char *swapbuf;
60     char *tmpbuf;
61     char *buf;
62 };
63
64 static void heap_swap (struct trunc_info *ti, int i1, int i2)
65 {
66     int swap;
67
68     swap = ti->ptr[i1];
69     ti->ptr[i1] = ti->ptr[i2];
70     ti->ptr[i2] = swap;
71 }
72
73 static void heap_delete (struct trunc_info *ti)
74 {
75     int cur = 1, child = 2;
76
77     heap_swap (ti, 1, ti->heapnum--);
78     while (child <= ti->heapnum) {
79         if (child < ti->heapnum &&
80             (*ti->cmp)(ti->heap[ti->ptr[child]],
81                        ti->heap[ti->ptr[1+child]]) > 0)
82             child++;
83         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
84                        ti->heap[ti->ptr[child]]) > 0)
85         {
86             heap_swap (ti, cur, child);
87             cur = child;
88             child = 2*cur;
89         }
90         else
91             break;
92     }
93 }
94
95 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
96 {
97     int cur, parent;
98
99     cur = ++(ti->heapnum);
100     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
101     ti->indx[ti->ptr[cur]] = indx;
102     parent = cur/2;
103     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
104                                 ti->heap[ti->ptr[cur]]) > 0)
105     {
106         heap_swap (ti, cur, parent);
107         cur = parent;
108         parent = cur/2;
109     }
110 }
111
112 static struct trunc_info *heap_init (int size, int key_size,
113                                      int (*cmp)(const void *p1,
114                                                 const void *p2))
115 {
116     struct trunc_info *ti = xmalloc (sizeof(*ti));
117     int i;
118
119     ++size;
120     ti->heapnum = 0;
121     ti->keysize = key_size;
122     ti->cmp = cmp;
123     ti->indx = xmalloc (size * sizeof(*ti->indx));
124     ti->heap = xmalloc (size * sizeof(*ti->heap));
125     ti->ptr = xmalloc (size * sizeof(*ti->ptr));
126     ti->swapbuf = xmalloc (ti->keysize);
127     ti->tmpbuf = xmalloc (ti->keysize);
128     ti->buf = xmalloc (size * ti->keysize);
129     for (i = size; --i >= 0; )
130     {
131         ti->ptr[i] = i;
132         ti->heap[i] = ti->buf + ti->keysize * i;
133     }
134     return ti;
135 }
136
137 static void heap_close (struct trunc_info *ti)
138 {
139     xfree (ti->ptr);
140     xfree (ti->indx);
141     xfree (ti->heap);
142     xfree (ti->swapbuf);
143     xfree (ti->tmpbuf);
144     xfree (ti->buf);
145     xfree (ti);
146 }
147
148 static RSET rset_trunc_r (ZServerInfo *zi, ISAM_P *isam_p, int from, int to,
149                          int merge_chunk)
150 {
151     RSET result; 
152     RSFD result_rsfd;
153     rset_temp_parms parms;
154
155     parms.key_size = sizeof(struct it_key);
156     parms.temp_path = res_get (zi->res, "setTmpDir");
157     result = rset_create (rset_kind_temp, &parms);
158     result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
159
160     if (to - from > merge_chunk)
161     {
162         RSFD *rsfd;
163         RSET *rset;
164         int i, i_add = (to-from)/merge_chunk + 1;
165         struct trunc_info *ti;
166         int rscur = 0;
167         int rsmax = (to-from)/i_add + 1;
168         
169         rset = xmalloc (sizeof(*rset) * rsmax);
170         rsfd = xmalloc (sizeof(*rsfd) * rsmax);
171         
172         for (i = from; i < to; i += i_add)
173         {
174             if (i_add <= to - i)
175                 rset[rscur] = rset_trunc_r (zi, isam_p, i, i+i_add,
176                                             merge_chunk);
177             else
178                 rset[rscur] = rset_trunc_r (zi, isam_p, i, to,
179                                             merge_chunk);
180             rscur++;
181         }
182         ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
183         for (i = rscur; --i >= 0; )
184         {
185             rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
186             if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
187                 heap_insert (ti, ti->tmpbuf, i);
188             else
189             {
190                 rset_close (rset[i], rsfd[i]);
191                 rset_delete (rset[i]);
192             }
193         }
194         while (ti->heapnum)
195         {
196             int n = ti->indx[ti->ptr[1]];
197
198             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
199
200             while (1)
201             {
202                 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
203                 {
204                     heap_delete (ti);
205                     rset_close (rset[n], rsfd[n]);
206                     rset_delete (rset[n]);
207                     break;
208                 }
209                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
210                 {
211                     heap_delete (ti);
212                     heap_insert (ti, ti->tmpbuf, n);
213                     break;
214                 }
215             }
216         }
217         xfree (rset);
218         xfree (rsfd);
219         heap_close (ti);
220     }
221     else if (zi->isam)
222     {
223         ISPT *ispt;
224         int i;
225         struct trunc_info *ti;
226
227         ispt = xmalloc (sizeof(*ispt) * (to-from));
228
229         ti = heap_init (to-from, sizeof(struct it_key),
230                         key_compare_it);
231         for (i = to-from; --i >= 0; )
232         {
233             ispt[i] = is_position (zi->isam, isam_p[from+i]);
234             if (is_readkey (ispt[i], ti->tmpbuf))
235                 heap_insert (ti, ti->tmpbuf, i);
236             else
237                 is_pt_free (ispt[i]);
238         }
239         while (ti->heapnum)
240         {
241             int n = ti->indx[ti->ptr[1]];
242
243             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
244 #if 1
245 /* section that preserve all keys */
246             heap_delete (ti);
247             if (is_readkey (ispt[n], ti->tmpbuf))
248                 heap_insert (ti, ti->tmpbuf, n);
249             else
250                 is_pt_free (ispt[n]);
251 #else
252 /* section that preserve all keys with unique sysnos */
253             while (1)
254             {
255                 if (!is_readkey (ispt[n], ti->tmpbuf))
256                 {
257                     heap_delete (ti);
258                     is_pt_free (ispt[n]);
259                     break;
260                 }
261                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
262                 {
263                     heap_delete (ti);
264                     heap_insert (ti, ti->tmpbuf, n);
265                     break;
266                 }
267             }
268 #endif
269         }
270         heap_close (ti);
271         xfree (ispt);
272     }
273     else
274     {
275         ISAMC_PP *ispt;
276         int i;
277         struct trunc_info *ti;
278
279         ispt = xmalloc (sizeof(*ispt) * (to-from));
280
281         ti = heap_init (to-from, sizeof(struct it_key),
282                         key_compare_it);
283         for (i = to-from; --i >= 0; )
284         {
285             ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
286             if (isc_pp_read (ispt[i], ti->tmpbuf))
287                 heap_insert (ti, ti->tmpbuf, i);
288             else
289                 isc_pp_close (ispt[i]);
290         }
291         while (ti->heapnum)
292         {
293             int n = ti->indx[ti->ptr[1]];
294
295             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
296 #if 0
297 /* section that preserve all keys */
298             heap_delete (ti);
299             if (is_readkey (ispt[n], ti->tmpbuf))
300                 heap_insert (ti, ti->tmpbuf, n);
301             else
302                 isc_pp_close (ispt[n]);
303 #else
304 /* section that preserve all keys with unique sysnos */
305             while (1)
306             {
307                 if (!isc_pp_read (ispt[n], ti->tmpbuf))
308                 {
309                     heap_delete (ti);
310                     isc_pp_close (ispt[n]);
311                     break;
312                 }
313                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
314                 {
315                     heap_delete (ti);
316                     heap_insert (ti, ti->tmpbuf, n);
317                     break;
318                 }
319             }
320 #endif
321         }
322         heap_close (ti);
323         xfree (ispt);
324     }
325     rset_close (result, result_rsfd);
326     return result;
327 }
328
329 static int isam_trunc_cmp (const void *p1, const void *p2)
330 {
331     ISAM_P i1 = *(ISAM_P*) p1;
332     ISAM_P i2 = *(ISAM_P*) p2;
333     int d;
334
335     d = is_type (i1) - is_type (i2);
336     if (d)
337         return d;
338     return is_block (i1) - is_block (i2);
339 }
340
341 static int isamc_trunc_cmp (const void *p1, const void *p2)
342 {
343     ISAMC_P i1 = *(ISAMC_P*) p1;
344     ISAMC_P i2 = *(ISAMC_P*) p2;
345     int d;
346
347     d = isc_type (i1) - isc_type (i2);
348     if (d)
349         return d;
350     return isc_block (i1) - isc_block (i2);
351 }
352
353 RSET rset_trunc (ZServerInfo *zi, ISAM_P *isam_p, int no)
354 {
355     logf (LOG_DEBUG, "rset_trunc no=%d", no);
356     if (zi->isam)
357     {
358         if (no < 1)
359             return rset_create (rset_kind_null, NULL);
360         else if (no == 1)
361         {
362             rset_isam_parms parms;
363
364             parms.pos = *isam_p;
365             parms.is = zi->isam;
366             return rset_create (rset_kind_isam, &parms);
367         }
368         qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
369     }
370     else if (zi->isamc)
371     {
372         if (no < 1)
373             return rset_create (rset_kind_null, NULL);
374         else if (no == 1)
375         {
376             rset_isamc_parms parms;
377
378             parms.pos = *isam_p;
379             parms.is = zi->isamc;
380             return rset_create (rset_kind_isamc, &parms);
381         }
382 #if NEW_TRUNC
383         else if (no < 200)
384         {
385             rset_m_or_parms parms;
386
387             logf (LOG_LOG, "new_trunc");
388             parms.key_size = sizeof(struct it_key);
389             parms.cmp = key_compare_it;
390             parms.isc = zi->isamc;
391             parms.isam_positions = isam_p;
392             parms.no_isam_positions = no;
393             parms.no_save_positions = 100;
394             return rset_create (rset_kind_m_or, &parms);
395         }
396 #endif
397         qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
398     }
399     else
400         logf (LOG_FATAL, "Neither isam nor isamc set in rset_trunc");
401     return rset_trunc_r (zi, isam_p, 0, no, 100);
402 }
403