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