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