40c0e501bbd47ea97a105de0f058bcd1dc162adb
[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.23  2002-04-12 14:40:42  adam
8  * Work on XPATH
9  *
10  * Revision 1.22  2002/04/05 08:46:26  adam
11  * Zebra with full functionality
12  *
13  * Revision 1.21  2002/04/04 14:14:13  adam
14  * Multiple registers (alpha early)
15  *
16  * Revision 1.20  2002/03/20 20:24:29  adam
17  * Hits per term. Returned in SearchResult-1
18  *
19  * Revision 1.19  2001/01/16 16:56:15  heikki
20  * Searching in my isam-d
21  *
22  * Revision 1.18  2000/05/18 12:01:36  adam
23  * System call times(2) used again. More 64-bit fixes.
24  *
25  * Revision 1.17  2000/03/15 15:00:30  adam
26  * First work on threaded version.
27  *
28  * Revision 1.16  1999/11/30 13:48:03  adam
29  * Improved installation. Updated for inclusion of YAZ header files.
30  *
31  * Revision 1.15  1999/07/20 13:59:18  adam
32  * Fixed bug that occurred when phrases had 0 hits.
33  *
34  * Revision 1.14  1999/05/26 07:49:13  adam
35  * C++ compilation.
36  *
37  * Revision 1.13  1999/05/12 13:08:06  adam
38  * First version of ISAMS.
39  *
40  * Revision 1.12  1999/02/02 14:51:10  adam
41  * Updated WIN32 code specific sections. Changed header.
42  *
43  * Revision 1.11  1998/03/25 13:48:02  adam
44  * Fixed bug in rset_trunc_r.
45  *
46  * Revision 1.10  1998/03/05 08:45:13  adam
47  * New result set model and modular ranking system. Moved towards
48  * descent server API. System information stored as "SGML" records.
49  *
50  * Revision 1.9  1998/01/12 15:04:09  adam
51  * The test option (-s) only uses read-lock (and not write lock).
52  *
53  * Revision 1.8  1997/10/31 12:34:27  adam
54  * Bug fix: memory leak.
55  *
56  * Revision 1.7  1997/09/29 09:07:29  adam
57  * Minor change.
58  *
59  * Revision 1.6  1997/09/22 12:39:06  adam
60  * Added get_pos method for the ranked result sets.
61  *
62  * Revision 1.5  1997/09/17 12:19:17  adam
63  * Zebra version corresponds to YAZ version 1.4.
64  * Changed Zebra server so that it doesn't depend on global common_resource.
65  *
66  * Revision 1.4  1996/12/23 15:30:44  adam
67  * Work on truncation.
68  * Bug fix: result sets weren't deleted after server shut down.
69  *
70  * Revision 1.3  1996/12/20 11:07:14  adam
71  * Multi-or result set.
72  *
73  * Revision 1.2  1996/11/08 11:10:28  adam
74  * Buffers used during file match got bigger.
75  * Compressed ISAM support everywhere.
76  * Bug fixes regarding masking characters in queries.
77  * Redesigned Regexp-2 queries.
78  *
79  * Revision 1.1  1996/11/04 14:07:40  adam
80  * Moved truncation code to trunc.c.
81  *
82  */
83 #include <stdio.h>
84 #include <assert.h>
85
86 #define NEW_TRUNC 1
87
88 #include "index.h"
89 #include <rstemp.h>
90 #include <rsnull.h>
91 #include <rsisams.h>
92 #include <rsisam.h>
93 #include <rsisamc.h>
94 #include <rsisamd.h>
95 #if NEW_TRUNC
96 #include <rsm_or.h>
97 #endif
98
99 struct trunc_info {
100     int  *ptr;
101     int  *indx;
102     char **heap;
103     int  heapnum;
104     int  (*cmp)(const void *p1, const void *p2);
105     int  keysize;
106     char *swapbuf;
107     char *tmpbuf;
108     char *buf;
109 };
110
111 static void heap_swap (struct trunc_info *ti, int i1, int i2)
112 {
113     int swap;
114
115     swap = ti->ptr[i1];
116     ti->ptr[i1] = ti->ptr[i2];
117     ti->ptr[i2] = swap;
118 }
119
120 static void heap_delete (struct trunc_info *ti)
121 {
122     int cur = 1, child = 2;
123
124     heap_swap (ti, 1, ti->heapnum--);
125     while (child <= ti->heapnum) {
126         if (child < ti->heapnum &&
127             (*ti->cmp)(ti->heap[ti->ptr[child]],
128                        ti->heap[ti->ptr[1+child]]) > 0)
129             child++;
130         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
131                        ti->heap[ti->ptr[child]]) > 0)
132         {
133             heap_swap (ti, cur, child);
134             cur = child;
135             child = 2*cur;
136         }
137         else
138             break;
139     }
140 }
141
142 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
143 {
144     int cur, parent;
145
146     cur = ++(ti->heapnum);
147     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
148     ti->indx[ti->ptr[cur]] = indx;
149     parent = cur/2;
150     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
151                                 ti->heap[ti->ptr[cur]]) > 0)
152     {
153         heap_swap (ti, cur, parent);
154         cur = parent;
155         parent = cur/2;
156     }
157 }
158
159 static struct trunc_info *heap_init (int size, int key_size,
160                                      int (*cmp)(const void *p1,
161                                                 const void *p2))
162 {
163     struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
164     int i;
165
166     ++size;
167     ti->heapnum = 0;
168     ti->keysize = key_size;
169     ti->cmp = cmp;
170     ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
171     ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
172     ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
173     ti->swapbuf = (char *) xmalloc (ti->keysize);
174     ti->tmpbuf = (char *) xmalloc (ti->keysize);
175     ti->buf = (char *) xmalloc (size * ti->keysize);
176     for (i = size; --i >= 0; )
177     {
178         ti->ptr[i] = i;
179         ti->heap[i] = ti->buf + ti->keysize * i;
180     }
181     return ti;
182 }
183
184 static void heap_close (struct trunc_info *ti)
185 {
186     xfree (ti->ptr);
187     xfree (ti->indx);
188     xfree (ti->heap);
189     xfree (ti->swapbuf);
190     xfree (ti->tmpbuf);
191     xfree (ti->buf);
192     xfree (ti);
193 }
194
195 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
196                           const char *flags, ISAMS_P *isam_p, int from, int to,
197                           int merge_chunk, int preserve_position)
198 {
199     RSET result; 
200     RSFD result_rsfd;
201     rset_temp_parms parms;
202
203     parms.cmp = key_compare_it;
204     parms.key_size = sizeof(struct it_key);
205     parms.temp_path = res_get (zi->res, "setTmpDir");
206     parms.rset_term = rset_term_create (term, length, flags);
207     result = rset_create (rset_kind_temp, &parms);
208     result_rsfd = rset_open (result, RSETF_WRITE);
209
210     if (to - from > merge_chunk)
211     {
212         RSFD *rsfd;
213         RSET *rset;
214         int term_index;
215         int i, i_add = (to-from)/merge_chunk + 1;
216         struct trunc_info *ti;
217         int rscur = 0;
218         int rsmax = (to-from)/i_add + 1;
219         
220         rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
221         rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
222         
223         for (i = from; i < to; i += i_add)
224         {
225             if (i_add <= to - i)
226                 rset[rscur] = rset_trunc_r (zi, term, length, flags,
227                                             isam_p, i, i+i_add,
228                                             merge_chunk, preserve_position);
229             else
230                 rset[rscur] = rset_trunc_r (zi, term, length, flags,
231                                             isam_p, i, to,
232                                             merge_chunk, preserve_position);
233             rscur++;
234         }
235         ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
236         for (i = rscur; --i >= 0; )
237         {
238             rsfd[i] = rset_open (rset[i], RSETF_READ);
239             if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
240                 heap_insert (ti, ti->tmpbuf, i);
241             else
242             {
243                 rset_close (rset[i], rsfd[i]);
244                 rset_delete (rset[i]);
245             }
246         }
247         while (ti->heapnum)
248         {
249             int n = ti->indx[ti->ptr[1]];
250
251             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
252
253             while (1)
254             {
255                 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
256                 {
257                     heap_delete (ti);
258                     rset_close (rset[n], rsfd[n]);
259                     rset_delete (rset[n]);
260                     break;
261                 }
262                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
263                 {
264                     heap_delete (ti);
265                     heap_insert (ti, ti->tmpbuf, n);
266                     break;
267                 }
268             }
269         }
270         xfree (rset);
271         xfree (rsfd);
272         heap_close (ti);
273     }
274     else if (zi->reg->isam)
275     {
276         ISPT *ispt;
277         int i;
278         struct trunc_info *ti;
279
280         ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
281
282         ti = heap_init (to-from, sizeof(struct it_key),
283                         key_compare_it);
284         for (i = to-from; --i >= 0; )
285         {
286             ispt[i] = is_position (zi->reg->isam, isam_p[from+i]);
287             if (is_readkey (ispt[i], ti->tmpbuf))
288                 heap_insert (ti, ti->tmpbuf, i);
289             else
290                 is_pt_free (ispt[i]);
291         }
292         while (ti->heapnum)
293         {
294             int n = ti->indx[ti->ptr[1]];
295
296             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
297 #if 1
298 /* section that preserve all keys */
299             heap_delete (ti);
300             if (is_readkey (ispt[n], ti->tmpbuf))
301                 heap_insert (ti, ti->tmpbuf, n);
302             else
303                 is_pt_free (ispt[n]);
304 #else
305 /* section that preserve all keys with unique sysnos */
306             while (1)
307             {
308                 if (!is_readkey (ispt[n], ti->tmpbuf))
309                 {
310                     heap_delete (ti);
311                     is_pt_free (ispt[n]);
312                     break;
313                 }
314                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
315                 {
316                     heap_delete (ti);
317                     heap_insert (ti, ti->tmpbuf, n);
318                     break;
319                 }
320             }
321 #endif
322         }
323         heap_close (ti);
324         xfree (ispt);
325     }
326     else if (zi->reg->isamc)
327     {
328         ISAMC_PP *ispt;
329         int i;
330         struct trunc_info *ti;
331
332         ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
333
334         ti = heap_init (to-from, sizeof(struct it_key),
335                         key_compare_it);
336         for (i = to-from; --i >= 0; )
337         {
338             ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
339             if (isc_pp_read (ispt[i], ti->tmpbuf))
340                 heap_insert (ti, ti->tmpbuf, i);
341             else
342                 isc_pp_close (ispt[i]);
343         }
344         while (ti->heapnum)
345         {
346             int n = ti->indx[ti->ptr[1]];
347
348             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
349             if (preserve_position)
350             {
351                 heap_delete (ti);
352                 if (isc_pp_read (ispt[n], ti->tmpbuf))
353                     heap_insert (ti, ti->tmpbuf, n);
354                 else
355                     isc_pp_close (ispt[n]);
356             }
357             else
358             {
359                 while (1)
360                 {
361                     if (!isc_pp_read (ispt[n], ti->tmpbuf))
362                     {
363                         heap_delete (ti);
364                         isc_pp_close (ispt[n]);
365                         break;
366                     }
367                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
368                     {
369                         heap_delete (ti);
370                         heap_insert (ti, ti->tmpbuf, n);
371                         break;
372                     }
373                 }
374             }
375         }
376         heap_close (ti);
377         xfree (ispt);
378     }
379
380     else if (zi->reg->isamd)
381     {
382         ISAMD_PP *ispt;
383         int i;
384         struct trunc_info *ti;
385
386         ispt = (ISAMD_PP *) xmalloc (sizeof(*ispt) * (to-from));
387
388         ti = heap_init (to-from, sizeof(struct it_key),
389                         key_compare_it);
390         for (i = to-from; --i >= 0; )
391         {
392             ispt[i] = isamd_pp_open (zi->reg->isamd, isam_p[from+i]);
393             if (isamd_pp_read (ispt[i], ti->tmpbuf))
394                 heap_insert (ti, ti->tmpbuf, i);
395             else
396                 isamd_pp_close (ispt[i]);
397         }
398         while (ti->heapnum)
399         {
400             int n = ti->indx[ti->ptr[1]];
401
402             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
403 #if 0
404 /* section that preserve all keys */
405             heap_delete (ti);
406             if (isamd_pp_read (ispt[n], ti->tmpbuf))
407                 heap_insert (ti, ti->tmpbuf, n);
408             else
409                 isamd_pp_close (ispt[n]);
410 #else
411 /* section that preserve all keys with unique sysnos */
412             while (1)
413             {
414                 if (!isamd_pp_read (ispt[n], ti->tmpbuf))
415                 {
416                     heap_delete (ti);
417                     isamd_pp_close (ispt[n]);
418                     break;
419                 }
420                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
421                 {
422                     heap_delete (ti);
423                     heap_insert (ti, ti->tmpbuf, n);
424                     break;
425                 }
426             }
427 #endif
428         }
429         heap_close (ti);
430         xfree (ispt);
431     }
432
433     else if (zi->reg->isams)
434     {
435         ISAMS_PP *ispt;
436         int i;
437         struct trunc_info *ti;
438
439         ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
440
441         ti = heap_init (to-from, sizeof(struct it_key),
442                         key_compare_it);
443         for (i = to-from; --i >= 0; )
444         {
445             ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
446             if (isams_pp_read (ispt[i], ti->tmpbuf))
447                 heap_insert (ti, ti->tmpbuf, i);
448             else
449                 isams_pp_close (ispt[i]);
450         }
451         while (ti->heapnum)
452         {
453             int n = ti->indx[ti->ptr[1]];
454
455             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
456             while (1)
457             {
458                 if (!isams_pp_read (ispt[n], ti->tmpbuf))
459                 {
460                     heap_delete (ti);
461                     isams_pp_close (ispt[n]);
462                     break;
463                 }
464                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
465                 {
466                     heap_delete (ti);
467                     heap_insert (ti, ti->tmpbuf, n);
468                     break;
469                 }
470             }
471         }
472         heap_close (ti);
473         xfree (ispt);
474     }
475     else
476         logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
477
478     rset_close (result, result_rsfd);
479     return result;
480 }
481
482 static int isams_trunc_cmp (const void *p1, const void *p2)
483 {
484     ISAMS_P i1 = *(ISAMS_P*) p1;
485     ISAMS_P i2 = *(ISAMS_P*) p2;
486
487     return i1 - i2;
488 }
489
490 static int isam_trunc_cmp (const void *p1, const void *p2)
491 {
492     ISAM_P i1 = *(ISAM_P*) p1;
493     ISAM_P i2 = *(ISAM_P*) p2;
494     int d;
495
496     d = is_type (i1) - is_type (i2);
497     if (d)
498         return d;
499     return is_block (i1) - is_block (i2);
500 }
501
502 static int isamc_trunc_cmp (const void *p1, const void *p2)
503 {
504     ISAMC_P i1 = *(ISAMC_P*) p1;
505     ISAMC_P i2 = *(ISAMC_P*) p2;
506     int d;
507
508     d = isc_type (i1) - isc_type (i2);
509     if (d)
510         return d;
511     return isc_block (i1) - isc_block (i2);
512 }
513 static int isamd_trunc_cmp (const void *p1, const void *p2)
514 {
515     ISAMD_P i1 = *(ISAMD_P*) p1;
516     ISAMD_P i2 = *(ISAMD_P*) p2;
517     int d;
518
519     d = isamd_type (i1) - isamd_type (i2);
520     if (d)
521         return d;
522     return isamd_block (i1) - isamd_block (i2);
523 }
524
525 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
526                  const char *term, int length, const char *flags,
527                  int preserve_position)
528 {
529     logf (LOG_DEBUG, "rset_trunc no=%d", no);
530     if (no < 1)
531     {
532         rset_null_parms parms;
533         parms.rset_term = rset_term_create (term, length, flags);
534         return rset_create (rset_kind_null, &parms);
535     }
536     if (zi->reg->isams)
537     {
538         if (no == 1)
539         {
540             rset_isams_parms parms;
541
542             parms.pos = *isam_p;
543             parms.is = zi->reg->isams;
544             parms.rset_term = rset_term_create (term, length, flags);
545             return rset_create (rset_kind_isams, &parms);
546         }
547         qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
548     }
549     else if (zi->reg->isam)
550     {
551         if (no == 1)
552         {
553             rset_isam_parms parms;
554
555             parms.pos = *isam_p;
556             parms.is = zi->reg->isam;
557             parms.rset_term = rset_term_create (term, length, flags);
558             return rset_create (rset_kind_isam, &parms);
559         }
560         qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
561     }
562     else if (zi->reg->isamc)
563     {
564         if (no == 1)
565         {
566             rset_isamc_parms parms;
567
568             parms.key_size = sizeof(struct it_key);
569             parms.cmp = key_compare_it;
570             parms.pos = *isam_p;
571             parms.is = zi->reg->isamc;
572             parms.rset_term = rset_term_create (term, length, flags);
573             return rset_create (rset_kind_isamc, &parms);
574         }
575 #if NEW_TRUNC
576         else if (no < 10000)
577         {
578             rset_m_or_parms parms;
579
580             parms.key_size = sizeof(struct it_key);
581             parms.cmp = key_compare_it;
582             parms.isc = zi->reg->isamc;
583             parms.isam_positions = isam_p;
584             parms.no_isam_positions = no;
585             parms.no_save_positions = 100000;
586             parms.rset_term = rset_term_create (term, length, flags);
587             return rset_create (rset_kind_m_or, &parms);
588         }
589 #endif
590         qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
591     }
592     else if (zi->reg->isamd)
593     {
594         if (no == 1)
595         {
596             rset_isamd_parms parms;
597
598             parms.pos = *isam_p;
599             parms.is = zi->reg->isamd;
600             parms.rset_term = rset_term_create (term, length, flags);
601             return rset_create (rset_kind_isamd, &parms);
602         }
603 #if NEW_TRUNC_NOT_DONE_FOR_ISAM_D
604         else if (no < 10000)
605         {
606             rset_m_or_parms parms;
607
608             parms.key_size = sizeof(struct it_key);
609             parms.cmp = key_compare_it;
610             parms.isc = 0;
611             parms.isamd=zi->reg->isamd;
612             parms.isam_positions = isam_p;
613             parms.no_isam_positions = no;
614             parms.no_save_positions = 100000;
615             parms.rset_term = rset_term_create (term, length, flags);
616             return rset_create (rset_kind_m_or, &parms);
617         }
618 #endif
619         qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
620     }
621     else
622     {
623         logf (LOG_WARN, "Unknown isam set in rset_trunc");
624         return rset_create (rset_kind_null, NULL);
625     }
626     return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
627                          preserve_position);
628 }
629