Fixed bug #281: heap truncation fails. Problem was thattermid parameter
[idzebra-moved-to-github.git] / index / trunc.c
1 /* $Id: trunc.c,v 1.54 2005-04-20 10:15:19 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <assert.h>
26
27 #include "index.h"
28 #include <rset.h>
29
30 struct trunc_info {
31     int  *ptr;
32     int  *indx;
33     char **heap;
34     int  heapnum;
35     int  (*cmp)(const void *p1, const void *p2);
36     int  keysize;
37     char *swapbuf;
38     char *tmpbuf;
39     char *buf;
40 };
41
42 static void heap_swap(struct trunc_info *ti, int i1, int i2)
43 {
44     int swap;
45
46     swap = ti->ptr[i1];
47     ti->ptr[i1] = ti->ptr[i2];
48     ti->ptr[i2] = swap;
49 }
50
51 static void heap_delete(struct trunc_info *ti)
52 {
53     int cur = 1, child = 2;
54
55     heap_swap(ti, 1, ti->heapnum--);
56     while (child <= ti->heapnum) {
57         if (child < ti->heapnum &&
58             (*ti->cmp)(ti->heap[ti->ptr[child]],
59                        ti->heap[ti->ptr[1+child]]) > 0)
60             child++;
61         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
62                        ti->heap[ti->ptr[child]]) > 0)
63         {
64             heap_swap(ti, cur, child);
65             cur = child;
66             child = 2*cur;
67         }
68         else
69             break;
70     }
71 }
72
73 static void heap_insert(struct trunc_info *ti, const char *buf, int indx)
74 {
75     int cur, parent;
76
77     cur = ++(ti->heapnum);
78     memcpy(ti->heap[ti->ptr[cur]], buf, ti->keysize);
79     ti->indx[ti->ptr[cur]] = indx;
80     parent = cur/2;
81     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
82                                 ti->heap[ti->ptr[cur]]) > 0)
83     {
84         heap_swap(ti, cur, parent);
85         cur = parent;
86         parent = cur/2;
87     }
88 }
89
90 static struct trunc_info *heap_init(int size, int key_size,
91                                     int (*cmp)(const void *p1,
92                                                const void *p2))
93 {
94     struct trunc_info *ti = (struct trunc_info *) xmalloc(sizeof(*ti));
95     int i;
96
97     ++size;
98     ti->heapnum = 0;
99     ti->keysize = key_size;
100     ti->cmp = cmp;
101     ti->indx = (int *) xmalloc(size * sizeof(*ti->indx));
102     ti->heap = (char **) xmalloc(size * sizeof(*ti->heap));
103     ti->ptr = (int *) xmalloc(size * sizeof(*ti->ptr));
104     ti->swapbuf = (char *) xmalloc(ti->keysize);
105     ti->tmpbuf = (char *) xmalloc(ti->keysize);
106     ti->buf = (char *) xmalloc(size * ti->keysize);
107     for (i = size; --i >= 0; )
108     {
109         ti->ptr[i] = i;
110         ti->heap[i] = ti->buf + ti->keysize * i;
111     }
112     return ti;
113 }
114
115 static void heap_close(struct trunc_info *ti)
116 {
117     xfree(ti->ptr);
118     xfree(ti->indx);
119     xfree(ti->heap);
120     xfree(ti->swapbuf);
121     xfree(ti->tmpbuf);
122     xfree(ti->buf);
123     xfree(ti);
124 }
125
126 static RSET rset_trunc_r(ZebraHandle zi, const char *term, int length,
127                          const char *flags, ISAM_P *isam_p, int from, int to,
128                          int merge_chunk, int preserve_position,
129                          int term_type, NMEM rset_nmem,
130                          const struct key_control *kctrl, int scope,
131                          TERMID termid)
132 {
133     RSET result;
134     RSFD result_rsfd;
135     int nn = 0;
136
137     result = rstemp_create(rset_nmem, kctrl, scope,
138                            res_get(zi->res, "setTmpDir"), termid);
139     result_rsfd = rset_open(result, RSETF_WRITE);
140     yaz_log(YLOG_LOG, "rset_trunc_r from=%d to=%d result=%p", from, to, result);
141
142     if (to - from > merge_chunk)
143     {
144         RSFD *rsfd;
145         RSET *rset;
146         int i, i_add = (to-from)/merge_chunk + 1;
147         struct trunc_info *ti;
148         int rscur = 0;
149         int rsmax = (to-from)/i_add + 1;
150         
151         rset = (RSET *) xmalloc(sizeof(*rset) * rsmax);
152         rsfd = (RSFD *) xmalloc(sizeof(*rsfd) * rsmax);
153         
154         for (i = from; i < to; i += i_add)
155         {
156             if (i_add <= to - i)
157                 rset[rscur] = rset_trunc_r(zi, term, length, flags,
158                                            isam_p, i, i+i_add,
159                                            merge_chunk, preserve_position,
160                                            term_type, rset_nmem, 
161                                            kctrl, scope, 0);
162             else
163                 rset[rscur] = rset_trunc_r(zi, term, length, flags,
164                                            isam_p, i, to,
165                                            merge_chunk, preserve_position,
166                                            term_type, rset_nmem, 
167                                            kctrl, scope, 0);
168             rscur++;
169         }
170         ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
171         for (i = rscur; --i >= 0; )
172         {
173             rsfd[i] = rset_open(rset[i], RSETF_READ);
174             if (rset_read(rsfd[i], ti->tmpbuf, 0))
175                 heap_insert(ti, ti->tmpbuf, i);
176             else
177             {
178                 rset_close(rsfd[i]);
179                 rset_delete(rset[i]);
180             }
181         }
182         while (ti->heapnum)
183         {
184             int n = ti->indx[ti->ptr[1]];
185
186             rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
187             nn++;
188
189             while (1)
190             {
191                 if(!rset_read (rsfd[n], ti->tmpbuf,0))
192                 {
193                     heap_delete(ti);
194                     rset_close(rsfd[n]);
195                     rset_delete(rset[n]);
196                     break;
197                 }
198                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
199                 {
200                     heap_delete(ti);
201                     heap_insert(ti, ti->tmpbuf, n);
202                     break;
203                 }
204             }
205         }
206         xfree(rset);
207         xfree(rsfd);
208         heap_close(ti);
209     }
210     else if (zi->reg->isamc)
211     {
212         ISAMC_PP *ispt;
213         int i;
214         struct trunc_info *ti;
215
216         ispt = (ISAMC_PP *) xmalloc(sizeof(*ispt) * (to-from));
217
218         ti = heap_init(to-from, sizeof(struct it_key),
219                        key_compare_it);
220         for (i = to-from; --i >= 0; )
221         {
222             ispt[i] = isamc_pp_open(zi->reg->isamc, isam_p[from+i]);
223             if (isamc_pp_read(ispt[i], ti->tmpbuf))
224                 heap_insert(ti, ti->tmpbuf, i);
225             else
226                 isamc_pp_close(ispt[i]);
227         }
228         while (ti->heapnum)
229         {
230             int n = ti->indx[ti->ptr[1]];
231
232             rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
233             nn++;
234             if (preserve_position)
235             {
236                 heap_delete(ti);
237                 if (isamc_pp_read(ispt[n], ti->tmpbuf))
238                     heap_insert(ti, ti->tmpbuf, n);
239                 else
240                     isamc_pp_close(ispt[n]);
241             }
242             else
243             {
244                 while (1)
245                 {
246                     if (!isamc_pp_read(ispt[n], ti->tmpbuf))
247                     {
248                         heap_delete(ti);
249                         isamc_pp_close(ispt[n]);
250                         break;
251                     }
252                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
253                     {
254                         heap_delete(ti);
255                         heap_insert(ti, ti->tmpbuf, n);
256                         break;
257                     }
258                 }
259             }
260         }
261         heap_close(ti);
262         xfree(ispt);
263     }
264     else if (zi->reg->isams)
265     {
266         ISAMS_PP *ispt;
267         int i;
268         struct trunc_info *ti;
269         int nn = 0;
270
271         ispt = (ISAMS_PP *) xmalloc(sizeof(*ispt) * (to-from));
272
273         ti = heap_init(to-from, sizeof(struct it_key),
274                        key_compare_it);
275         for (i = to-from; --i >= 0; )
276         {
277             ispt[i] = isams_pp_open(zi->reg->isams, isam_p[from+i]);
278             if (isams_pp_read(ispt[i], ti->tmpbuf))
279                 heap_insert(ti, ti->tmpbuf, i);
280             else
281                 isams_pp_close(ispt[i]);
282         }
283         while (ti->heapnum)
284         {
285             int n = ti->indx[ti->ptr[1]];
286
287             rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
288             nn++;
289             while (1)
290             {
291                 if (!isams_pp_read(ispt[n], ti->tmpbuf))
292                 {
293                     heap_delete(ti);
294                     isams_pp_close(ispt[n]);
295                     break;
296                 }
297                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
298                 {
299                     heap_delete(ti);
300                     heap_insert(ti, ti->tmpbuf, n);
301                     break;
302                 }
303             }
304         }
305         heap_close(ti);
306         xfree(ispt);
307     }
308     else if (zi->reg->isamb)
309     {
310         ISAMB_PP *ispt;
311         int i;
312         struct trunc_info *ti;
313
314         ispt = (ISAMB_PP *) xmalloc(sizeof(*ispt) * (to-from));
315
316         ti = heap_init(to-from, sizeof(struct it_key),
317                        key_compare_it);
318         for (i = to-from; --i >= 0; )
319         {
320             if (isam_p[from+i]) {
321                 ispt[i] = isamb_pp_open(zi->reg->isamb, isam_p[from+i], scope);
322                 if (isamb_pp_read(ispt[i], ti->tmpbuf))
323                     heap_insert(ti, ti->tmpbuf, i);
324                 else
325                     isamb_pp_close(ispt[i]);
326             }
327         }
328         while (ti->heapnum)
329         {
330             int n = ti->indx[ti->ptr[1]];
331
332             rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
333             nn++;
334
335             if (preserve_position)
336             {
337                 heap_delete(ti);
338                 if (isamb_pp_read(ispt[n], ti->tmpbuf))
339                     heap_insert(ti, ti->tmpbuf, n);
340                 else
341                     isamb_pp_close(ispt[n]);
342             }
343             else
344             {
345                 while (1)
346                 {
347                     if (!isamb_pp_read(ispt[n], ti->tmpbuf))
348                     {
349                         heap_delete(ti);
350                         isamb_pp_close(ispt[n]);
351                         break;
352                     }
353                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
354                     {
355                         heap_delete(ti);
356                         heap_insert(ti, ti->tmpbuf, n);
357                         break;
358                     }
359                 }
360             }
361         }
362         heap_close(ti);
363         xfree(ispt);
364     }
365     else
366         yaz_log(YLOG_WARN, "Unknown isam set in rset_trunc_r");
367
368     rset_close(result_rsfd);
369     yaz_log(YLOG_LOG, "rset_trunc_r returned result=%p", result);
370     return result;
371 }
372
373 static int isams_trunc_cmp(const void *p1, const void *p2)
374 {
375     ISAM_P i1 = *(ISAM_P*) p1;
376     ISAM_P i2 = *(ISAM_P*) p2;
377
378     if (i1 > i2)
379         return 1;
380     else if (i1 < i2)
381         return -1;
382     return 0;
383 }
384
385 static int isamc_trunc_cmp(const void *p1, const void *p2)
386 {
387     ISAM_P i1 = *(ISAM_P*) p1;
388     ISAM_P i2 = *(ISAM_P*) p2;
389     zint d;
390
391     d = (isamc_type(i1) - isamc_type(i2));
392     if (d == 0)
393         d = isamc_block(i1) - isamc_block(i2);
394     if (d > 0)
395         return 1;
396     else if (d < 0)
397         return -1;
398     return 0;
399 }
400
401 RSET rset_trunc(ZebraHandle zi, ISAM_P *isam_p, int no,
402                 const char *term, int length, const char *flags,
403                 int preserve_position, int term_type, NMEM rset_nmem,
404                 const struct key_control *kctrl, int scope)
405 {
406     TERMID termid;
407     RSET result;
408     int trunc_chunk;
409     yaz_log(YLOG_LOG, "rset_trunc no=%d", no);
410     if (no < 1)
411         return rsnull_create(rset_nmem,kctrl);
412     termid = rset_term_create(term, length, flags, term_type,rset_nmem);
413     if (zi->reg->isams)
414     {
415         if (no == 1)
416             return rsisams_create(rset_nmem, kctrl, scope,
417                                   zi->reg->isams, *isam_p, termid);
418         qsort(isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
419     }
420     else if (zi->reg->isamc)
421     {
422         if (no == 1)
423             return rsisamc_create(rset_nmem, kctrl, scope,
424                                   zi->reg->isamc, *isam_p, termid);
425         qsort(isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
426     }
427     else if (zi->reg->isamb)
428     {
429         int trunc_limit = atoi(res_get_def(zi->res, "trunclimit", "10000"));
430         if (no == 1)
431             return rsisamb_create(rset_nmem,kctrl, scope,
432                                   zi->reg->isamb, *isam_p, termid);
433         else if (no < trunc_limit) 
434         {
435             RSET r;
436             RSET *rsets = xmalloc(no*sizeof(RSET)); /* use nmem! */
437             int i;
438             for (i = 0; i<no; i++)
439                 rsets[i] = rsisamb_create(rset_nmem, kctrl, scope,
440                                           zi->reg->isamb, isam_p[i], termid);
441             r = rsmulti_or_create( rset_nmem, kctrl, scope, no, rsets);
442             xfree(rsets);
443             return r;
444         } 
445         fprintf(stderr, "Using rset_trunc_r limit=%d\n", trunc_limit);
446         qsort(isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
447     }
448     else
449     {
450         yaz_log(YLOG_WARN, "Unknown isam set in rset_trunc");
451         return rsnull_create(rset_nmem, kctrl);
452     }
453     trunc_chunk = atoi(res_get_def(zi->res, "truncchunk", "100"));
454     result = rset_trunc_r(zi, term, length, flags, isam_p, 0, no, trunc_chunk,
455                           preserve_position, term_type, rset_nmem, kctrl, scope,
456                           termid);
457     yaz_log(YLOG_LOG, "rset_trunc no=%d returned=%p", no, result);
458     return result;
459 }
460