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