Fixed bug that occurred when phrases had 0 hits.
[idzebra-moved-to-github.git] / index / trunc.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: trunc.c,v $
7  * Revision 1.15  1999-07-20 13:59:18  adam
8  * Fixed bug that occurred when phrases had 0 hits.
9  *
10  * Revision 1.14  1999/05/26 07:49:13  adam
11  * C++ compilation.
12  *
13  * Revision 1.13  1999/05/12 13:08:06  adam
14  * First version of ISAMS.
15  *
16  * Revision 1.12  1999/02/02 14:51:10  adam
17  * Updated WIN32 code specific sections. Changed header.
18  *
19  * Revision 1.11  1998/03/25 13:48:02  adam
20  * Fixed bug in rset_trunc_r.
21  *
22  * Revision 1.10  1998/03/05 08:45:13  adam
23  * New result set model and modular ranking system. Moved towards
24  * descent server API. System information stored as "SGML" records.
25  *
26  * Revision 1.9  1998/01/12 15:04:09  adam
27  * The test option (-s) only uses read-lock (and not write lock).
28  *
29  * Revision 1.8  1997/10/31 12:34:27  adam
30  * Bug fix: memory leak.
31  *
32  * Revision 1.7  1997/09/29 09:07:29  adam
33  * Minor change.
34  *
35  * Revision 1.6  1997/09/22 12:39:06  adam
36  * Added get_pos method for the ranked result sets.
37  *
38  * Revision 1.5  1997/09/17 12:19:17  adam
39  * Zebra version corresponds to YAZ version 1.4.
40  * Changed Zebra server so that it doesn't depend on global common_resource.
41  *
42  * Revision 1.4  1996/12/23 15:30:44  adam
43  * Work on truncation.
44  * Bug fix: result sets weren't deleted after server shut down.
45  *
46  * Revision 1.3  1996/12/20 11:07:14  adam
47  * Multi-or result set.
48  *
49  * Revision 1.2  1996/11/08 11:10:28  adam
50  * Buffers used during file match got bigger.
51  * Compressed ISAM support everywhere.
52  * Bug fixes regarding masking characters in queries.
53  * Redesigned Regexp-2 queries.
54  *
55  * Revision 1.1  1996/11/04 14:07:40  adam
56  * Moved truncation code to trunc.c.
57  *
58  */
59 #include <stdio.h>
60 #include <assert.h>
61
62 #include "zserver.h"
63 #include <rstemp.h>
64 #include <rsisam.h>
65 #include <rsisamc.h>
66 #include <rsisams.h>
67 #include <rsnull.h>
68
69 #define NEW_TRUNC 1
70
71 #if NEW_TRUNC
72 #include <rsm_or.h>
73 #endif
74
75 struct trunc_info {
76     int  *ptr;
77     int  *indx;
78     char **heap;
79     int  heapnum;
80     int  (*cmp)(const void *p1, const void *p2);
81     int  keysize;
82     char *swapbuf;
83     char *tmpbuf;
84     char *buf;
85 };
86
87 static void heap_swap (struct trunc_info *ti, int i1, int i2)
88 {
89     int swap;
90
91     swap = ti->ptr[i1];
92     ti->ptr[i1] = ti->ptr[i2];
93     ti->ptr[i2] = swap;
94 }
95
96 static void heap_delete (struct trunc_info *ti)
97 {
98     int cur = 1, child = 2;
99
100     heap_swap (ti, 1, ti->heapnum--);
101     while (child <= ti->heapnum) {
102         if (child < ti->heapnum &&
103             (*ti->cmp)(ti->heap[ti->ptr[child]],
104                        ti->heap[ti->ptr[1+child]]) > 0)
105             child++;
106         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
107                        ti->heap[ti->ptr[child]]) > 0)
108         {
109             heap_swap (ti, cur, child);
110             cur = child;
111             child = 2*cur;
112         }
113         else
114             break;
115     }
116 }
117
118 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
119 {
120     int cur, parent;
121
122     cur = ++(ti->heapnum);
123     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
124     ti->indx[ti->ptr[cur]] = indx;
125     parent = cur/2;
126     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
127                                 ti->heap[ti->ptr[cur]]) > 0)
128     {
129         heap_swap (ti, cur, parent);
130         cur = parent;
131         parent = cur/2;
132     }
133 }
134
135 static struct trunc_info *heap_init (int size, int key_size,
136                                      int (*cmp)(const void *p1,
137                                                 const void *p2))
138 {
139     struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
140     int i;
141
142     ++size;
143     ti->heapnum = 0;
144     ti->keysize = key_size;
145     ti->cmp = cmp;
146     ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
147     ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
148     ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
149     ti->swapbuf = (char *) xmalloc (ti->keysize);
150     ti->tmpbuf = (char *) xmalloc (ti->keysize);
151     ti->buf = (char *) xmalloc (size * ti->keysize);
152     for (i = size; --i >= 0; )
153     {
154         ti->ptr[i] = i;
155         ti->heap[i] = ti->buf + ti->keysize * i;
156     }
157     return ti;
158 }
159
160 static void heap_close (struct trunc_info *ti)
161 {
162     xfree (ti->ptr);
163     xfree (ti->indx);
164     xfree (ti->heap);
165     xfree (ti->swapbuf);
166     xfree (ti->tmpbuf);
167     xfree (ti->buf);
168     xfree (ti);
169 }
170
171 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
172                          const char *flags, ISAM_P *isam_p, int from, int to,
173                          int merge_chunk)
174 {
175     RSET result; 
176     RSFD result_rsfd;
177     rset_temp_parms parms;
178
179     parms.key_size = sizeof(struct it_key);
180     parms.temp_path = res_get (zi->res, "setTmpDir");
181     parms.rset_term = rset_term_create (term, length, flags);
182     result = rset_create (rset_kind_temp, &parms);
183     result_rsfd = rset_open (result, RSETF_WRITE);
184
185     if (to - from > merge_chunk)
186     {
187         RSFD *rsfd;
188         RSET *rset;
189         int term_index;
190         int i, i_add = (to-from)/merge_chunk + 1;
191         struct trunc_info *ti;
192         int rscur = 0;
193         int rsmax = (to-from)/i_add + 1;
194         
195         rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
196         rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
197         
198         for (i = from; i < to; i += i_add)
199         {
200             if (i_add <= to - i)
201                 rset[rscur] = rset_trunc_r (zi, term, length, flags,
202                                             isam_p, i, i+i_add, merge_chunk);
203             else
204                 rset[rscur] = rset_trunc_r (zi, term, length, flags,
205                                             isam_p, i, to, merge_chunk);
206             rscur++;
207         }
208         ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
209         for (i = rscur; --i >= 0; )
210         {
211             rsfd[i] = rset_open (rset[i], RSETF_READ);
212             if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
213                 heap_insert (ti, ti->tmpbuf, i);
214             else
215             {
216                 rset_close (rset[i], rsfd[i]);
217                 rset_delete (rset[i]);
218             }
219         }
220         while (ti->heapnum)
221         {
222             int n = ti->indx[ti->ptr[1]];
223
224             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
225
226             while (1)
227             {
228                 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
229                 {
230                     heap_delete (ti);
231                     rset_close (rset[n], rsfd[n]);
232                     rset_delete (rset[n]);
233                     break;
234                 }
235                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
236                 {
237                     heap_delete (ti);
238                     heap_insert (ti, ti->tmpbuf, n);
239                     break;
240                 }
241             }
242         }
243         xfree (rset);
244         xfree (rsfd);
245         heap_close (ti);
246     }
247     else if (zi->isam)
248     {
249         ISPT *ispt;
250         int i;
251         struct trunc_info *ti;
252
253         ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
254
255         ti = heap_init (to-from, sizeof(struct it_key),
256                         key_compare_it);
257         for (i = to-from; --i >= 0; )
258         {
259             ispt[i] = is_position (zi->isam, isam_p[from+i]);
260             if (is_readkey (ispt[i], ti->tmpbuf))
261                 heap_insert (ti, ti->tmpbuf, i);
262             else
263                 is_pt_free (ispt[i]);
264         }
265         while (ti->heapnum)
266         {
267             int n = ti->indx[ti->ptr[1]];
268
269             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
270 #if 1
271 /* section that preserve all keys */
272             heap_delete (ti);
273             if (is_readkey (ispt[n], ti->tmpbuf))
274                 heap_insert (ti, ti->tmpbuf, n);
275             else
276                 is_pt_free (ispt[n]);
277 #else
278 /* section that preserve all keys with unique sysnos */
279             while (1)
280             {
281                 if (!is_readkey (ispt[n], ti->tmpbuf))
282                 {
283                     heap_delete (ti);
284                     is_pt_free (ispt[n]);
285                     break;
286                 }
287                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
288                 {
289                     heap_delete (ti);
290                     heap_insert (ti, ti->tmpbuf, n);
291                     break;
292                 }
293             }
294 #endif
295         }
296         heap_close (ti);
297         xfree (ispt);
298     }
299     else if (zi->isamc)
300     {
301         ISAMC_PP *ispt;
302         int i;
303         struct trunc_info *ti;
304
305         ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
306
307         ti = heap_init (to-from, sizeof(struct it_key),
308                         key_compare_it);
309         for (i = to-from; --i >= 0; )
310         {
311             ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
312             if (isc_pp_read (ispt[i], ti->tmpbuf))
313                 heap_insert (ti, ti->tmpbuf, i);
314             else
315                 isc_pp_close (ispt[i]);
316         }
317         while (ti->heapnum)
318         {
319             int n = ti->indx[ti->ptr[1]];
320
321             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
322 #if 0
323 /* section that preserve all keys */
324             heap_delete (ti);
325             if (isc_pp_read (ispt[n], ti->tmpbuf))
326                 heap_insert (ti, ti->tmpbuf, n);
327             else
328                 isc_pp_close (ispt[n]);
329 #else
330 /* section that preserve all keys with unique sysnos */
331             while (1)
332             {
333                 if (!isc_pp_read (ispt[n], ti->tmpbuf))
334                 {
335                     heap_delete (ti);
336                     isc_pp_close (ispt[n]);
337                     break;
338                 }
339                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
340                 {
341                     heap_delete (ti);
342                     heap_insert (ti, ti->tmpbuf, n);
343                     break;
344                 }
345             }
346 #endif
347         }
348         heap_close (ti);
349         xfree (ispt);
350     }
351     else if (zi->isams)
352     {
353         ISAMS_PP *ispt;
354         int i;
355         struct trunc_info *ti;
356
357         ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
358
359         ti = heap_init (to-from, sizeof(struct it_key),
360                         key_compare_it);
361         for (i = to-from; --i >= 0; )
362         {
363             ispt[i] = isams_pp_open (zi->isams, isam_p[from+i]);
364             if (isams_pp_read (ispt[i], ti->tmpbuf))
365                 heap_insert (ti, ti->tmpbuf, i);
366             else
367                 isams_pp_close (ispt[i]);
368         }
369         while (ti->heapnum)
370         {
371             int n = ti->indx[ti->ptr[1]];
372
373             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
374             while (1)
375             {
376                 if (!isams_pp_read (ispt[n], ti->tmpbuf))
377                 {
378                     heap_delete (ti);
379                     isams_pp_close (ispt[n]);
380                     break;
381                 }
382                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
383                 {
384                     heap_delete (ti);
385                     heap_insert (ti, ti->tmpbuf, n);
386                     break;
387                 }
388             }
389         }
390         heap_close (ti);
391         xfree (ispt);
392     }
393     rset_close (result, result_rsfd);
394     return result;
395 }
396
397 static int isam_trunc_cmp (const void *p1, const void *p2)
398 {
399     ISAM_P i1 = *(ISAM_P*) p1;
400     ISAM_P i2 = *(ISAM_P*) p2;
401     int d;
402
403     d = is_type (i1) - is_type (i2);
404     if (d)
405         return d;
406     return is_block (i1) - is_block (i2);
407 }
408
409 static int isamc_trunc_cmp (const void *p1, const void *p2)
410 {
411     ISAMC_P i1 = *(ISAMC_P*) p1;
412     ISAMC_P i2 = *(ISAMC_P*) p2;
413     int d;
414
415     d = isc_type (i1) - isc_type (i2);
416     if (d)
417         return d;
418     return isc_block (i1) - isc_block (i2);
419 }
420
421 static int isams_trunc_cmp (const void *p1, const void *p2)
422 {
423     ISAMS_P i1 = *(ISAMS_P*) p1;
424     ISAMS_P i2 = *(ISAMS_P*) p2;
425
426     return i1 - i2;
427 }
428
429 RSET rset_trunc (ZebraHandle zi, ISAM_P *isam_p, int no,
430                  const char *term, int length, const char *flags)
431 {
432     logf (LOG_DEBUG, "rset_trunc no=%d", no);
433     if (no < 1)
434     {
435         rset_null_parms parms;
436         parms.rset_term = rset_term_create (term, length, flags);
437         return rset_create (rset_kind_null, &parms);
438     }
439     if (zi->isam)
440     {
441         if (no == 1)
442         {
443             rset_isam_parms parms;
444
445             parms.pos = *isam_p;
446             parms.is = zi->isam;
447             parms.rset_term = rset_term_create (term, length, flags);
448             return rset_create (rset_kind_isam, &parms);
449         }
450         qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
451     }
452     else if (zi->isamc)
453     {
454         if (no == 1)
455         {
456             rset_isamc_parms parms;
457
458             parms.pos = *isam_p;
459             parms.is = zi->isamc;
460             parms.rset_term = rset_term_create (term, length, flags);
461             return rset_create (rset_kind_isamc, &parms);
462         }
463 #if NEW_TRUNC
464         else if (no < 200)
465         {
466             rset_m_or_parms parms;
467
468             parms.key_size = sizeof(struct it_key);
469             parms.cmp = key_compare_it;
470             parms.isc = zi->isamc;
471             parms.isam_positions = isam_p;
472             parms.no_isam_positions = no;
473             parms.no_save_positions = 100000;
474             parms.rset_term = rset_term_create (term, length, flags);
475             return rset_create (rset_kind_m_or, &parms);
476         }
477 #endif
478         qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
479     }
480     else if (zi->isams)
481     {
482         if (no == 1)
483         {
484             rset_isams_parms parms;
485
486             parms.pos = *isam_p;
487             parms.is = zi->isams;
488             parms.rset_term = rset_term_create (term, length, flags);
489             return rset_create (rset_kind_isams, &parms);
490         }
491         qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
492     }
493     else
494     {
495         logf (LOG_WARN, "Neither isam / isamc / isams set in rset_trunc");
496         return rset_create (rset_kind_null, NULL);
497     }
498     return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100);
499 }
500