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