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