Added several type casts and changed many types - mostly from int to zint.
[idzebra-moved-to-github.git] / index / trunc.c
1 /* $Id: trunc.c,v 1.30 2004-08-06 12:28:22 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <assert.h>
26
27 #define NEW_TRUNC 1
28
29 #include "index.h"
30 #include <rstemp.h>
31 #include <rsnull.h>
32 #include <rsisams.h>
33 #include <rsisam.h>
34 #include <rsisamc.h>
35 #include <rsisamb.h>
36 #if NEW_TRUNC
37 #include <rsm_or.h>
38 #endif
39
40 struct trunc_info {
41     int  *ptr;
42     int  *indx;
43     char **heap;
44     int  heapnum;
45     int  (*cmp)(const void *p1, const void *p2);
46     int  keysize;
47     char *swapbuf;
48     char *tmpbuf;
49     char *buf;
50 };
51
52 static void heap_swap (struct trunc_info *ti, int i1, int i2)
53 {
54     int swap;
55
56     swap = ti->ptr[i1];
57     ti->ptr[i1] = ti->ptr[i2];
58     ti->ptr[i2] = swap;
59 }
60
61 static void heap_delete (struct trunc_info *ti)
62 {
63     int cur = 1, child = 2;
64
65     heap_swap (ti, 1, ti->heapnum--);
66     while (child <= ti->heapnum) {
67         if (child < ti->heapnum &&
68             (*ti->cmp)(ti->heap[ti->ptr[child]],
69                        ti->heap[ti->ptr[1+child]]) > 0)
70             child++;
71         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
72                        ti->heap[ti->ptr[child]]) > 0)
73         {
74             heap_swap (ti, cur, child);
75             cur = child;
76             child = 2*cur;
77         }
78         else
79             break;
80     }
81 }
82
83 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
84 {
85     int cur, parent;
86
87     cur = ++(ti->heapnum);
88     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
89     ti->indx[ti->ptr[cur]] = indx;
90     parent = cur/2;
91     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
92                                 ti->heap[ti->ptr[cur]]) > 0)
93     {
94         heap_swap (ti, cur, parent);
95         cur = parent;
96         parent = cur/2;
97     }
98 }
99
100 static struct trunc_info *heap_init (int size, int key_size,
101                                      int (*cmp)(const void *p1,
102                                                 const void *p2))
103 {
104     struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
105     int i;
106
107     ++size;
108     ti->heapnum = 0;
109     ti->keysize = key_size;
110     ti->cmp = cmp;
111     ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
112     ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
113     ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
114     ti->swapbuf = (char *) xmalloc (ti->keysize);
115     ti->tmpbuf = (char *) xmalloc (ti->keysize);
116     ti->buf = (char *) xmalloc (size * ti->keysize);
117     for (i = size; --i >= 0; )
118     {
119         ti->ptr[i] = i;
120         ti->heap[i] = ti->buf + ti->keysize * i;
121     }
122     return ti;
123 }
124
125 static void heap_close (struct trunc_info *ti)
126 {
127     xfree (ti->ptr);
128     xfree (ti->indx);
129     xfree (ti->heap);
130     xfree (ti->swapbuf);
131     xfree (ti->tmpbuf);
132     xfree (ti->buf);
133     xfree (ti);
134 }
135
136 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
137                           const char *flags, ISAMS_P *isam_p, int from, int to,
138                           int merge_chunk, int preserve_position,
139                           int term_type)
140 {
141     RSET result; 
142     RSFD result_rsfd;
143     rset_temp_parms parms;
144     int nn = 0;
145
146     parms.cmp = key_compare_it;
147     parms.key_size = sizeof(struct it_key);
148     parms.temp_path = res_get (zi->res, "setTmpDir");
149     parms.rset_term = rset_term_create (term, length, flags, term_type);
150     result = rset_create (rset_kind_temp, &parms);
151     result_rsfd = rset_open (result, RSETF_WRITE);
152
153     if (to - from > merge_chunk)
154     {
155         RSFD *rsfd;
156         RSET *rset;
157         int term_index;
158         int i, i_add = (to-from)/merge_chunk + 1;
159         struct trunc_info *ti;
160         int rscur = 0;
161         int rsmax = (to-from)/i_add + 1;
162         
163         rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
164         rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
165         
166         for (i = from; i < to; i += i_add)
167         {
168             if (i_add <= to - i)
169                 rset[rscur] = rset_trunc_r (zi, term, length, flags,
170                                             isam_p, i, i+i_add,
171                                             merge_chunk, preserve_position,
172                                             term_type);
173             else
174                 rset[rscur] = rset_trunc_r (zi, term, length, flags,
175                                             isam_p, i, to,
176                                             merge_chunk, preserve_position,
177                                             term_type);
178             rscur++;
179         }
180         ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
181         for (i = rscur; --i >= 0; )
182         {
183             rsfd[i] = rset_open (rset[i], RSETF_READ);
184             if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
185                 heap_insert (ti, ti->tmpbuf, i);
186             else
187             {
188                 rset_close (rset[i], rsfd[i]);
189                 rset_delete (rset[i]);
190             }
191         }
192         while (ti->heapnum)
193         {
194             int n = ti->indx[ti->ptr[1]];
195
196             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
197             nn++;
198
199             while (1)
200             {
201                 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
202                 {
203                     heap_delete (ti);
204                     rset_close (rset[n], rsfd[n]);
205                     rset_delete (rset[n]);
206                     break;
207                 }
208                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
209                 {
210                     heap_delete (ti);
211                     heap_insert (ti, ti->tmpbuf, n);
212                     break;
213                 }
214             }
215         }
216         xfree (rset);
217         xfree (rsfd);
218         heap_close (ti);
219     }
220     else if (zi->reg->isam)
221     {
222         ISPT *ispt;
223         int i;
224         struct trunc_info *ti;
225
226         ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
227
228         ti = heap_init (to-from, sizeof(struct it_key),
229                         key_compare_it);
230         for (i = to-from; --i >= 0; )
231         {
232             ispt[i] = is_position (zi->reg->isam, isam_p[from+i]);
233             if (is_readkey (ispt[i], ti->tmpbuf))
234                 heap_insert (ti, ti->tmpbuf, i);
235             else
236                 is_pt_free (ispt[i]);
237         }
238         while (ti->heapnum)
239         {
240             int n = ti->indx[ti->ptr[1]];
241
242             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
243             nn++;
244             if (preserve_position)
245             {
246 /* section that preserve all keys */
247                 heap_delete (ti);
248                 if (is_readkey (ispt[n], ti->tmpbuf))
249                     heap_insert (ti, ti->tmpbuf, n);
250                 else
251                     is_pt_free (ispt[n]);
252             }
253             else
254             {
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             }
272         }
273         heap_close (ti);
274         xfree (ispt);
275     }
276     else if (zi->reg->isamc)
277     {
278         ISAMC_PP *ispt;
279         int i;
280         struct trunc_info *ti;
281
282         ispt = (ISAMC_PP *) 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->reg->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             nn++;
300             if (preserve_position)
301             {
302                 heap_delete (ti);
303                 if (isc_pp_read (ispt[n], ti->tmpbuf))
304                     heap_insert (ti, ti->tmpbuf, n);
305                 else
306                     isc_pp_close (ispt[n]);
307             }
308             else
309             {
310                 while (1)
311                 {
312                     if (!isc_pp_read (ispt[n], ti->tmpbuf))
313                     {
314                         heap_delete (ti);
315                         isc_pp_close (ispt[n]);
316                         break;
317                     }
318                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
319                     {
320                         heap_delete (ti);
321                         heap_insert (ti, ti->tmpbuf, n);
322                         break;
323                     }
324                 }
325             }
326         }
327         heap_close (ti);
328         xfree (ispt);
329     }
330     else if (zi->reg->isams)
331     {
332         ISAMS_PP *ispt;
333         int i;
334         struct trunc_info *ti;
335         int nn = 0;
336
337         ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
338
339         ti = heap_init (to-from, sizeof(struct it_key),
340                         key_compare_it);
341         for (i = to-from; --i >= 0; )
342         {
343             ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
344             if (isams_pp_read (ispt[i], ti->tmpbuf))
345                 heap_insert (ti, ti->tmpbuf, i);
346             else
347                 isams_pp_close (ispt[i]);
348         }
349         while (ti->heapnum)
350         {
351             int n = ti->indx[ti->ptr[1]];
352
353             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
354             nn++;
355             while (1)
356             {
357                 if (!isams_pp_read (ispt[n], ti->tmpbuf))
358                 {
359                     heap_delete (ti);
360                     isams_pp_close (ispt[n]);
361                     break;
362                 }
363                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
364                 {
365                     heap_delete (ti);
366                     heap_insert (ti, ti->tmpbuf, n);
367                     break;
368                 }
369             }
370         }
371         heap_close (ti);
372         xfree (ispt);
373     }
374     else if (zi->reg->isamb)
375     {
376         ISAMB_PP *ispt;
377         int i;
378         struct trunc_info *ti;
379
380         ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
381
382         ti = heap_init (to-from, sizeof(struct it_key),
383                         key_compare_it);
384         for (i = to-from; --i >= 0; )
385         {
386             ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i]);
387             if (isamb_pp_read (ispt[i], ti->tmpbuf))
388                 heap_insert (ti, ti->tmpbuf, i);
389             else
390                 isamb_pp_close (ispt[i]);
391         }
392         while (ti->heapnum)
393         {
394             int n = ti->indx[ti->ptr[1]];
395
396             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
397             nn++;
398
399             if (preserve_position)
400             {
401                 heap_delete (ti);
402                 if (isamb_pp_read (ispt[n], ti->tmpbuf))
403                     heap_insert (ti, ti->tmpbuf, n);
404                 else
405                     isamb_pp_close (ispt[n]);
406             }
407             else
408             {
409                 while (1)
410                 {
411                     if (!isamb_pp_read (ispt[n], ti->tmpbuf))
412                     {
413                         heap_delete (ti);
414                         isamb_pp_close (ispt[n]);
415                         break;
416                     }
417                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
418                     {
419                         heap_delete (ti);
420                         heap_insert (ti, ti->tmpbuf, n);
421                         break;
422                     }
423                 }
424             }
425         }
426         heap_close (ti);
427         xfree (ispt);
428     }
429     else
430         logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
431
432     parms.rset_term->nn = nn;
433     rset_close (result, result_rsfd);
434     return result;
435 }
436
437 static int isams_trunc_cmp (const void *p1, const void *p2)
438 {
439     ISAMS_P i1 = *(ISAMS_P*) p1;
440     ISAMS_P i2 = *(ISAMS_P*) p2;
441
442     if (i1 > i2)
443         return 1;
444     if (i1 < i2)
445         return -1;
446     return 0;
447 }
448
449 static int isam_trunc_cmp (const void *p1, const void *p2)
450 {
451     ISAM_P i1 = *(ISAM_P*) p1;
452     ISAM_P i2 = *(ISAM_P*) p2;
453     int d;
454
455     d = is_type (i1) - is_type (i2);
456     if (d)
457         return d;
458     return is_block (i1) - is_block (i2);
459 }
460
461 static int isamc_trunc_cmp (const void *p1, const void *p2)
462 {
463     ISAMC_P i1 = *(ISAMC_P*) p1;
464     ISAMC_P i2 = *(ISAMC_P*) p2;
465     int d;
466
467     d = (int) (isc_type (i1) - isc_type (i2));
468     if (d)
469         return d;
470     d = isc_block (i1) - isc_block (i2);
471     if (d > 0)
472         return 1;
473     else if (d < 0)
474         return -1;
475     return 0;
476 }
477
478 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
479                  const char *term, int length, const char *flags,
480                  int preserve_position, int term_type)
481 {
482     logf (LOG_DEBUG, "rset_trunc no=%d", no);
483     if (no < 1)
484     {
485         rset_null_parms parms;
486         parms.rset_term = rset_term_create (term, length, flags, term_type);
487         return rset_create (rset_kind_null, &parms);
488     }
489     if (zi->reg->isams)
490     {
491         if (no == 1)
492         {
493             rset_isams_parms parms;
494
495             parms.pos = *isam_p;
496             parms.is = zi->reg->isams;
497             parms.rset_term = rset_term_create (term, length, flags,
498                                                 term_type);
499             return rset_create (rset_kind_isams, &parms);
500         }
501         qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
502     }
503     else if (zi->reg->isam)
504     {
505         if (no == 1)
506         {
507             rset_isam_parms parms;
508
509             parms.pos = *isam_p;
510             parms.is = zi->reg->isam;
511             parms.rset_term = rset_term_create (term, length, flags,
512                                                 term_type);
513             return rset_create (rset_kind_isam, &parms);
514         }
515         qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
516     }
517     else if (zi->reg->isamc)
518     {
519         if (no == 1)
520         {
521             rset_isamc_parms parms;
522
523             parms.key_size = sizeof(struct it_key);
524             parms.cmp = key_compare_it;
525             parms.pos = *isam_p;
526             parms.is = zi->reg->isamc;
527             parms.rset_term = rset_term_create (term, length, flags,
528                                                 term_type);
529             return rset_create (rset_kind_isamc, &parms);
530         }
531 #if NEW_TRUNC
532         else if (no < 10000)
533         {
534             rset_m_or_parms parms;
535
536             parms.key_size = sizeof(struct it_key);
537             parms.cmp = key_compare_it;
538             parms.isc = zi->reg->isamc;
539             parms.isam_positions = isam_p;
540             parms.no_isam_positions = no;
541             parms.no_save_positions = 100000;
542             parms.rset_term = rset_term_create (term, length, flags,
543                                                 term_type);
544             return rset_create (rset_kind_m_or, &parms);
545         }
546 #endif
547         qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
548     }
549     else if (zi->reg->isamb)
550     {
551         if (no == 1)
552         {
553             rset_isamb_parms parms;
554
555             parms.key_size = sizeof(struct it_key);
556             parms.cmp = key_compare_it;
557             parms.pos = *isam_p;
558             parms.is = zi->reg->isamb;
559             parms.rset_term = rset_term_create (term, length, flags,
560                                                 term_type);
561             return rset_create (rset_kind_isamb, &parms);
562         }
563         qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
564     }
565     else
566     {
567         logf (LOG_WARN, "Unknown isam set in rset_trunc");
568         return rset_create (rset_kind_null, NULL);
569     }
570     return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
571                          preserve_position, term_type);
572 }
573