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