Added get_pos method for the ranked result sets.
[idzebra-moved-to-github.git] / index / trunc.c
1 /*
2  * Copyright (C) 1994-1996, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: trunc.c,v $
7  * Revision 1.6  1997-09-22 12:39:06  adam
8  * Added get_pos method for the ranked result sets.
9  *
10  * Revision 1.5  1997/09/17 12:19:17  adam
11  * Zebra version corresponds to YAZ version 1.4.
12  * Changed Zebra server so that it doesn't depend on global common_resource.
13  *
14  * Revision 1.4  1996/12/23 15:30:44  adam
15  * Work on truncation.
16  * Bug fix: result sets weren't deleted after server shut down.
17  *
18  * Revision 1.3  1996/12/20 11:07:14  adam
19  * Multi-or result set.
20  *
21  * Revision 1.2  1996/11/08 11:10:28  adam
22  * Buffers used during file match got bigger.
23  * Compressed ISAM support everywhere.
24  * Bug fixes regarding masking characters in queries.
25  * Redesigned Regexp-2 queries.
26  *
27  * Revision 1.1  1996/11/04 14:07:40  adam
28  * Moved truncation code to trunc.c.
29  *
30  */
31 #include <stdio.h>
32 #include <assert.h>
33
34 #include "zserver.h"
35 #include <rstemp.h>
36 #include <rsisam.h>
37 #include <rsisamc.h>
38 #include <rsnull.h>
39
40 #define NEW_TRUNC 1
41
42 #if NEW_TRUNC
43 #include <rsm_or.h>
44 #endif
45
46 struct trunc_info {
47     int  *ptr;
48     int  *indx;
49     char **heap;
50     int  heapnum;
51     int  (*cmp)(const void *p1, const void *p2);
52     int  keysize;
53     char *swapbuf;
54     char *tmpbuf;
55     char *buf;
56 };
57
58 static void heap_swap (struct trunc_info *ti, int i1, int i2)
59 {
60     int swap;
61
62     swap = ti->ptr[i1];
63     ti->ptr[i1] = ti->ptr[i2];
64     ti->ptr[i2] = swap;
65 }
66
67 static void heap_delete (struct trunc_info *ti)
68 {
69     int cur = 1, child = 2;
70
71     heap_swap (ti, 1, ti->heapnum--);
72     while (child <= ti->heapnum) {
73         if (child < ti->heapnum &&
74             (*ti->cmp)(ti->heap[ti->ptr[child]],
75                        ti->heap[ti->ptr[1+child]]) > 0)
76             child++;
77         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
78                        ti->heap[ti->ptr[child]]) > 0)
79         {
80             heap_swap (ti, cur, child);
81             cur = child;
82             child = 2*cur;
83         }
84         else
85             break;
86     }
87 }
88
89 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
90 {
91     int cur, parent;
92
93     cur = ++(ti->heapnum);
94     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
95     ti->indx[ti->ptr[cur]] = indx;
96     parent = cur/2;
97     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
98                                 ti->heap[ti->ptr[cur]]) > 0)
99     {
100         heap_swap (ti, cur, parent);
101         cur = parent;
102         parent = cur/2;
103     }
104 }
105
106 static
107 struct trunc_info *heap_init (int size, int key_size,
108                               int (*cmp)(const void *p1, const void *p2))
109 {
110     struct trunc_info *ti = xmalloc (sizeof(*ti));
111     int i;
112
113     ++size;
114     ti->heapnum = 0;
115     ti->keysize = key_size;
116     ti->cmp = cmp;
117     ti->indx = xmalloc (size * sizeof(*ti->indx));
118     ti->heap = xmalloc (size * sizeof(*ti->heap));
119     ti->ptr = xmalloc (size * sizeof(*ti->ptr));
120     ti->swapbuf = xmalloc (ti->keysize);
121     ti->tmpbuf = xmalloc (ti->keysize);
122     ti->buf = xmalloc (size * ti->keysize);
123     for (i = size; --i >= 0; )
124     {
125         ti->ptr[i] = i;
126         ti->heap[i] = ti->buf + ti->keysize * i;
127     }
128     return ti;
129 }
130
131 static void heap_close (struct trunc_info *ti)
132 {
133     xfree (ti->ptr);
134     xfree (ti->indx);
135     xfree (ti->heap);
136     xfree (ti->swapbuf);
137     xfree (ti->tmpbuf);
138     xfree (ti);
139 }
140
141 static RSET rset_trunc_r (ZServerInfo *zi, ISAM_P *isam_p, int from, int to,
142                          int merge_chunk)
143 {
144     RSET result; 
145     RSFD result_rsfd;
146     rset_temp_parms parms;
147
148     parms.key_size = sizeof(struct it_key);
149     parms.temp_path = res_get (zi->res, "setTmpDir");
150     result = rset_create (rset_kind_temp, &parms);
151     result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
152
153     if (to - from > merge_chunk)
154     {
155         RSFD *rsfd;
156         RSET *rset;
157         int i, i_add = (to-from)/merge_chunk + 1;
158         struct trunc_info *ti;
159         int rscur = 0;
160         int rsmax = (to-from)/i_add + 1;
161         
162         rset = xmalloc (sizeof(*rset) * rsmax);
163         rsfd = xmalloc (sizeof(*rsfd) * rsmax);
164         
165         for (i = from; i < to; i += i_add)
166         {
167             if (i_add <= to - i)
168                 rset[rscur] = rset_trunc_r (zi, isam_p, i, i+i_add,
169                                             merge_chunk);
170             else
171                 rset[rscur] = rset_trunc_r (zi, isam_p, i, to,
172                                             merge_chunk);
173             rscur++;
174         }
175         ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
176         for (i = rscur; --i >= 0; )
177         {
178             rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
179             if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
180                 heap_insert (ti, ti->tmpbuf, i);
181             else
182             {
183                 rset_close (rset[i], rsfd[i]);
184                 rset_delete (rset[i]);
185             }
186         }
187         while (ti->heapnum)
188         {
189             int n = ti->indx[ti->ptr[1]];
190
191             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
192
193             while (1)
194             {
195                 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
196                 {
197                     heap_delete (ti);
198                     rset_close (rset[n], rsfd[n]);
199                     rset_delete (rset[n]);
200                     break;
201                 }
202                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
203                 {
204                     heap_delete (ti);
205                     heap_insert (ti, ti->tmpbuf, n);
206                     break;
207                 }
208             }
209         }
210         xfree (rset);
211         xfree (rsfd);
212         heap_close (ti);
213     }
214     else if (zi->isam)
215     {
216         ISPT *ispt;
217         int i;
218         struct trunc_info *ti;
219
220         ispt = xmalloc (sizeof(*ispt) * (to-from));
221
222         ti = heap_init (to-from, sizeof(struct it_key),
223                         key_compare_it);
224         for (i = to-from; --i >= 0; )
225         {
226             ispt[i] = is_position (zi->isam, isam_p[from+i]);
227             if (is_readkey (ispt[i], ti->tmpbuf))
228                 heap_insert (ti, ti->tmpbuf, i);
229             else
230                 is_pt_free (ispt[i]);
231         }
232         while (ti->heapnum)
233         {
234             int n = ti->indx[ti->ptr[1]];
235
236             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
237 #if 1
238 /* section that preserve all keys */
239             heap_delete (ti);
240             if (is_readkey (ispt[n], ti->tmpbuf))
241                 heap_insert (ti, ti->tmpbuf, n);
242             else
243                 is_pt_free (ispt[n]);
244 #else
245 /* section that preserve all keys with unique sysnos */
246             while (1)
247             {
248                 if (!is_readkey (ispt[n], ti->tmpbuf))
249                 {
250                     heap_delete (ti);
251                     is_pt_free (ispt[n]);
252                     break;
253                 }
254                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
255                 {
256                     heap_delete (ti);
257                     heap_insert (ti, ti->tmpbuf, n);
258                     break;
259                 }
260             }
261 #endif
262         }
263         heap_close (ti);
264         xfree (ispt);
265     }
266     else
267     {
268         ISAMC_PP *ispt;
269         int i;
270         struct trunc_info *ti;
271
272         ispt = xmalloc (sizeof(*ispt) * (to-from));
273
274         ti = heap_init (to-from, sizeof(struct it_key),
275                         key_compare_it);
276         for (i = to-from; --i >= 0; )
277         {
278             ispt[i] = isc_pp_open (zi->isamc, isam_p[from+i]);
279             if (isc_pp_read (ispt[i], ti->tmpbuf))
280                 heap_insert (ti, ti->tmpbuf, i);
281             else
282                 isc_pp_close (ispt[i]);
283         }
284         while (ti->heapnum)
285         {
286             int n = ti->indx[ti->ptr[1]];
287
288             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
289 #if 0
290 /* section that preserve all keys */
291             heap_delete (ti);
292             if (is_readkey (ispt[n], ti->tmpbuf))
293                 heap_insert (ti, ti->tmpbuf, n);
294             else
295                 isc_pp_close (ispt[n]);
296 #else
297 /* section that preserve all keys with unique sysnos */
298             while (1)
299             {
300                 if (!isc_pp_read (ispt[n], ti->tmpbuf))
301                 {
302                     heap_delete (ti);
303                     isc_pp_close (ispt[n]);
304                     break;
305                 }
306                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
307                 {
308                     heap_delete (ti);
309                     heap_insert (ti, ti->tmpbuf, n);
310                     break;
311                 }
312             }
313 #endif
314         }
315         heap_close (ti);
316         xfree (ispt);
317     }
318     rset_close (result, result_rsfd);
319     return result;
320 }
321
322 static int isam_trunc_cmp (const void *p1, const void *p2)
323 {
324     ISAM_P i1 = *(ISAM_P*) p1;
325     ISAM_P i2 = *(ISAM_P*) p2;
326     int d;
327
328     d = is_type (i1) - is_type (i2);
329     if (d)
330         return d;
331     return is_block (i1) - is_block (i2);
332 }
333
334 static int isamc_trunc_cmp (const void *p1, const void *p2)
335 {
336     ISAMC_P i1 = *(ISAMC_P*) p1;
337     ISAMC_P i2 = *(ISAMC_P*) p2;
338     int d;
339
340     d = isc_type (i1) - isc_type (i2);
341     if (d)
342         return d;
343     return isc_block (i1) - isc_block (i2);
344 }
345
346 RSET rset_trunc (ZServerInfo *zi, ISAM_P *isam_p, int no)
347 {
348     logf (LOG_DEBUG, "rset_trunc no=%d", no);
349     if (zi->isam)
350     {
351         if (no < 1)
352             return rset_create (rset_kind_null, NULL);
353         else if (no == 1)
354         {
355             rset_isam_parms parms;
356
357             parms.pos = *isam_p;
358             parms.is = zi->isam;
359             return rset_create (rset_kind_isam, &parms);
360         }
361         qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
362     }
363     else if (zi->isamc)
364     {
365         if (no < 1)
366             return rset_create (rset_kind_null, NULL);
367         else if (no == 1)
368         {
369             rset_isamc_parms parms;
370
371             parms.pos = *isam_p;
372             parms.is = zi->isamc;
373             return rset_create (rset_kind_isamc, &parms);
374         }
375 #if NEW_TRUNC
376         else if (no < 200)
377         {
378             rset_m_or_parms parms;
379
380             logf (LOG_LOG, "new_trunc");
381             parms.key_size = sizeof(struct it_key);
382             parms.cmp = key_compare_it;
383             parms.isc = zi->isamc;
384             parms.isam_positions = isam_p;
385             parms.no_isam_positions = no;
386             parms.no_save_positions = 100;
387             return rset_create (rset_kind_m_or, &parms);
388         }
389 #endif
390         qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
391     }
392     else
393         logf (LOG_FATAL, "Neither isam nor isamc set in rset_trunc");
394     return rset_trunc_r (zi, isam_p, 0, no, 100);
395 }
396