Added support for term references (queryIDs) for searchResult.
[idzebra-moved-to-github.git] / index / trunc.c
1 /* $Id: trunc.c,v 1.61 2005-06-22 19:42:38 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                          struct rset_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
141     if (to - from > merge_chunk)
142     {
143         RSFD *rsfd;
144         RSET *rset;
145         int i, i_add = (to-from)/merge_chunk + 1;
146         struct trunc_info *ti;
147         int rscur = 0;
148         int rsmax = (to-from)/i_add + 1;
149         
150         rset = (RSET *) xmalloc(sizeof(*rset) * rsmax);
151         rsfd = (RSFD *) xmalloc(sizeof(*rsfd) * rsmax);
152         
153         for (i = from; i < to; i += i_add)
154         {
155             if (i_add <= to - i)
156                 rset[rscur] = rset_trunc_r(zi, term, length, flags,
157                                            isam_p, i, i+i_add,
158                                            merge_chunk, preserve_position,
159                                            term_type, rset_nmem, 
160                                            kctrl, scope, 0);
161             else
162                 rset[rscur] = rset_trunc_r(zi, term, length, flags,
163                                            isam_p, i, to,
164                                            merge_chunk, preserve_position,
165                                            term_type, rset_nmem, 
166                                            kctrl, scope, 0);
167             rscur++;
168         }
169         ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
170         for (i = rscur; --i >= 0; )
171         {
172             rsfd[i] = rset_open(rset[i], RSETF_READ);
173             if (rset_read(rsfd[i], ti->tmpbuf, 0))
174                 heap_insert(ti, ti->tmpbuf, i);
175             else
176             {
177                 rset_close(rsfd[i]);
178                 rset_delete(rset[i]);
179             }
180         }
181         while (ti->heapnum)
182         {
183             int n = ti->indx[ti->ptr[1]];
184
185             rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
186             nn++;
187
188             while (1)
189             {
190                 if(!rset_read (rsfd[n], ti->tmpbuf,0))
191                 {
192                     heap_delete(ti);
193                     rset_close(rsfd[n]);
194                     rset_delete(rset[n]);
195                     break;
196                 }
197                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
198                 {
199                     heap_delete(ti);
200                     heap_insert(ti, ti->tmpbuf, n);
201                     break;
202                 }
203             }
204         }
205         xfree(rset);
206         xfree(rsfd);
207         heap_close(ti);
208     }
209     else if (zi->reg->isamc)
210     {
211         ISAMC_PP *ispt;
212         int i;
213         struct trunc_info *ti;
214
215         ispt = (ISAMC_PP *) xmalloc(sizeof(*ispt) * (to-from));
216
217         ti = heap_init(to-from, sizeof(struct it_key),
218                        key_compare_it);
219         for (i = to-from; --i >= 0; )
220         {
221             ispt[i] = isamc_pp_open(zi->reg->isamc, isam_p[from+i]);
222             if (isamc_pp_read(ispt[i], ti->tmpbuf))
223                 heap_insert(ti, ti->tmpbuf, i);
224             else
225                 isamc_pp_close(ispt[i]);
226         }
227         while (ti->heapnum)
228         {
229             int n = ti->indx[ti->ptr[1]];
230
231             rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
232             nn++;
233             if (preserve_position)
234             {
235                 heap_delete(ti);
236                 if (isamc_pp_read(ispt[n], ti->tmpbuf))
237                     heap_insert(ti, ti->tmpbuf, n);
238                 else
239                     isamc_pp_close(ispt[n]);
240             }
241             else
242             {
243                 while (1)
244                 {
245                     if (!isamc_pp_read(ispt[n], ti->tmpbuf))
246                     {
247                         heap_delete(ti);
248                         isamc_pp_close(ispt[n]);
249                         break;
250                     }
251                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
252                     {
253                         heap_delete(ti);
254                         heap_insert(ti, ti->tmpbuf, n);
255                         break;
256                     }
257                 }
258             }
259         }
260         heap_close(ti);
261         xfree(ispt);
262     }
263     else if (zi->reg->isams)
264     {
265         ISAMS_PP *ispt;
266         int i;
267         struct trunc_info *ti;
268         int nn = 0;
269
270         ispt = (ISAMS_PP *) xmalloc(sizeof(*ispt) * (to-from));
271
272         ti = heap_init(to-from, sizeof(struct it_key),
273                        key_compare_it);
274         for (i = to-from; --i >= 0; )
275         {
276             ispt[i] = isams_pp_open(zi->reg->isams, isam_p[from+i]);
277             if (isams_pp_read(ispt[i], ti->tmpbuf))
278                 heap_insert(ti, ti->tmpbuf, i);
279             else
280                 isams_pp_close(ispt[i]);
281         }
282         while (ti->heapnum)
283         {
284             int n = ti->indx[ti->ptr[1]];
285
286             rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
287             nn++;
288             while (1)
289             {
290                 if (!isams_pp_read(ispt[n], ti->tmpbuf))
291                 {
292                     heap_delete(ti);
293                     isams_pp_close(ispt[n]);
294                     break;
295                 }
296                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
297                 {
298                     heap_delete(ti);
299                     heap_insert(ti, ti->tmpbuf, n);
300                     break;
301                 }
302             }
303         }
304         heap_close(ti);
305         xfree(ispt);
306     }
307     else if (zi->reg->isamb)
308     {
309         ISAMB_PP *ispt;
310         int i;
311         struct trunc_info *ti;
312
313         ispt = (ISAMB_PP *) xmalloc(sizeof(*ispt) * (to-from));
314
315         ti = heap_init(to-from, sizeof(struct it_key),
316                        key_compare_it);
317         for (i = to-from; --i >= 0; )
318         {
319             if (isam_p[from+i]) {
320                 ispt[i] = isamb_pp_open(zi->reg->isamb, isam_p[from+i], scope);
321                 if (isamb_pp_read(ispt[i], ti->tmpbuf))
322                     heap_insert(ti, ti->tmpbuf, i);
323                 else
324                     isamb_pp_close(ispt[i]);
325             }
326         }
327         while (ti->heapnum)
328         {
329             int n = ti->indx[ti->ptr[1]];
330
331             rset_write(result_rsfd, ti->heap[ti->ptr[1]]);
332             nn++;
333
334             if (preserve_position)
335             {
336                 heap_delete(ti);
337                 if (isamb_pp_read(ispt[n], ti->tmpbuf))
338                     heap_insert(ti, ti->tmpbuf, n);
339                 else
340                     isamb_pp_close(ispt[n]);
341             }
342             else
343             {
344                 while (1)
345                 {
346                     if (!isamb_pp_read(ispt[n], ti->tmpbuf))
347                     {
348                         heap_delete(ti);
349                         isamb_pp_close(ispt[n]);
350                         break;
351                     }
352                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
353                     {
354                         heap_delete(ti);
355                         heap_insert(ti, ti->tmpbuf, n);
356                         break;
357                     }
358                 }
359             }
360         }
361         heap_close(ti);
362         xfree(ispt);
363     }
364     else
365         yaz_log(YLOG_WARN, "Unknown isam set in rset_trunc_r");
366
367     rset_close(result_rsfd);
368     return result;
369 }
370
371 static int isams_trunc_cmp(const void *p1, const void *p2)
372 {
373     ISAM_P i1 = *(ISAM_P*) p1;
374     ISAM_P i2 = *(ISAM_P*) p2;
375
376     if (i1 > i2)
377         return 1;
378     else if (i1 < i2)
379         return -1;
380     return 0;
381 }
382
383 static int isamc_trunc_cmp(const void *p1, const void *p2)
384 {
385     ISAM_P i1 = *(ISAM_P*) p1;
386     ISAM_P i2 = *(ISAM_P*) p2;
387     zint d;
388
389     d = (isamc_type(i1) - isamc_type(i2));
390     if (d == 0)
391         d = isamc_block(i1) - isamc_block(i2);
392     if (d > 0)
393         return 1;
394     else if (d < 0)
395         return -1;
396     return 0;
397 }
398
399 RSET rset_trunc(ZebraHandle zi, ISAM_P *isam_p, int no,
400                 const char *term, int length, const char *flags,
401                 int preserve_position, int term_type, NMEM rset_nmem,
402                 struct rset_key_control *kctrl, int scope,
403                 struct ord_list *ol, int reg_type,
404                 zint hits_limit, const char *term_ref_id)
405 {
406     TERMID termid;
407     RSET result;
408     int trunc_chunk;
409     
410     termid = rset_term_create(term, length, flags, term_type, rset_nmem, ol,
411                               reg_type, hits_limit, term_ref_id);
412     if (no < 1)
413         return rsnull_create(rset_nmem, kctrl, termid);
414     
415     if (zi->reg->isams)
416     {
417         if (no == 1)
418             return rsisams_create(rset_nmem, kctrl, scope,
419                                   zi->reg->isams, *isam_p, termid);
420         qsort(isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
421     }
422     else if (zi->reg->isamc)
423     {
424         if (no == 1)
425             return rsisamc_create(rset_nmem, kctrl, scope,
426                                   zi->reg->isamc, *isam_p, termid);
427         qsort(isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
428     }
429     else if (zi->reg->isamb)
430     {
431         int trunc_limit = atoi(res_get_def(zi->res, "trunclimit", "10000"));
432         if (no == 1)
433             return rsisamb_create(rset_nmem, kctrl, scope,
434                                   zi->reg->isamb, *isam_p, termid);
435         else if (no < trunc_limit) 
436         {
437             RSET r;
438             RSET *rsets = xmalloc(no*sizeof(RSET)); /* use nmem! */
439             int i;
440             for (i = 0; i<no; i++)
441                 rsets[i] = rsisamb_create(rset_nmem, kctrl, scope,
442                                           zi->reg->isamb, isam_p[i],
443                                           0 /* termid */);
444             r = rsmulti_or_create(rset_nmem, kctrl, scope,
445                                   termid /* termid */,
446                                   no, rsets);
447             xfree(rsets);
448             return r;
449         } 
450         qsort(isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
451     }
452     else
453     {
454         yaz_log(YLOG_WARN, "Unknown isam set in rset_trunc");
455         return rsnull_create(rset_nmem, kctrl, 0);
456     }
457     trunc_chunk = atoi(res_get_def(zi->res, "truncchunk", "100"));
458     result = rset_trunc_r(zi, term, length, flags, isam_p, 0, no, trunc_chunk,
459                           preserve_position, term_type, rset_nmem, kctrl,
460                           scope, termid);
461     return result;
462 }
463