the new multior. Seems to work, forwards OK, does not yet estimate pos.
[idzebra-moved-to-github.git] / index / trunc.c
1 /* $Id: trunc.c,v 1.35 2004-08-19 12:49:14 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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 <rsisamc.h>
34 #include <rsisamb.h>
35 #if NEW_TRUNC
36 #include <rsm_or.h>
37 #endif
38 #include <rsmultior.h>
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->isamc)
221     {
222         ISAMC_PP *ispt;
223         int i;
224         struct trunc_info *ti;
225
226         ispt = (ISAMC_PP *) 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] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
233             if (isc_pp_read (ispt[i], ti->tmpbuf))
234                 heap_insert (ti, ti->tmpbuf, i);
235             else
236                 isc_pp_close (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                 heap_delete (ti);
247                 if (isc_pp_read (ispt[n], ti->tmpbuf))
248                     heap_insert (ti, ti->tmpbuf, n);
249                 else
250                     isc_pp_close (ispt[n]);
251             }
252             else
253             {
254                 while (1)
255                 {
256                     if (!isc_pp_read (ispt[n], ti->tmpbuf))
257                     {
258                         heap_delete (ti);
259                         isc_pp_close (ispt[n]);
260                         break;
261                     }
262                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
263                     {
264                         heap_delete (ti);
265                         heap_insert (ti, ti->tmpbuf, n);
266                         break;
267                     }
268                 }
269             }
270         }
271         heap_close (ti);
272         xfree (ispt);
273     }
274     else if (zi->reg->isams)
275     {
276         ISAMS_PP *ispt;
277         int i;
278         struct trunc_info *ti;
279         int nn = 0;
280
281         ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
282
283         ti = heap_init (to-from, sizeof(struct it_key),
284                         key_compare_it);
285         for (i = to-from; --i >= 0; )
286         {
287             ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
288             if (isams_pp_read (ispt[i], ti->tmpbuf))
289                 heap_insert (ti, ti->tmpbuf, i);
290             else
291                 isams_pp_close (ispt[i]);
292         }
293         while (ti->heapnum)
294         {
295             int n = ti->indx[ti->ptr[1]];
296
297             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
298             nn++;
299             while (1)
300             {
301                 if (!isams_pp_read (ispt[n], ti->tmpbuf))
302                 {
303                     heap_delete (ti);
304                     isams_pp_close (ispt[n]);
305                     break;
306                 }
307                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
308                 {
309                     heap_delete (ti);
310                     heap_insert (ti, ti->tmpbuf, n);
311                     break;
312                 }
313             }
314         }
315         heap_close (ti);
316         xfree (ispt);
317     }
318     else if (zi->reg->isamb)
319     {
320         ISAMB_PP *ispt;
321         int i;
322         struct trunc_info *ti;
323
324         ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
325
326         ti = heap_init (to-from, sizeof(struct it_key),
327                         key_compare_it);
328         for (i = to-from; --i >= 0; )
329         {
330             if (isam_p[from+i]) {
331                 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i]);
332                 if (isamb_pp_read (ispt[i], ti->tmpbuf))
333                     heap_insert (ti, ti->tmpbuf, i);
334                 else
335                     isamb_pp_close (ispt[i]);
336             }
337         }
338         while (ti->heapnum)
339         {
340             int n = ti->indx[ti->ptr[1]];
341
342             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
343             nn++;
344
345             if (preserve_position)
346             {
347                 heap_delete (ti);
348                 if (isamb_pp_read (ispt[n], ti->tmpbuf))
349                     heap_insert (ti, ti->tmpbuf, n);
350                 else
351                     isamb_pp_close (ispt[n]);
352             }
353             else
354             {
355                 while (1)
356                 {
357                     if (!isamb_pp_read (ispt[n], ti->tmpbuf))
358                     {
359                         heap_delete (ti);
360                         isamb_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         }
372         heap_close (ti);
373         xfree (ispt);
374     }
375     else
376         logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
377
378     parms.rset_term->nn = nn;
379     rset_close (result, result_rsfd);
380     return result;
381 }
382
383 static int isams_trunc_cmp (const void *p1, const void *p2)
384 {
385     ISAMS_P i1 = *(ISAMS_P*) p1;
386     ISAMS_P i2 = *(ISAMS_P*) p2;
387
388     if (i1 > i2)
389         return 1;
390     else if (i1 < i2)
391         return -1;
392     return 0;
393 }
394
395 static int isamc_trunc_cmp (const void *p1, const void *p2)
396 {
397     ISAMC_P i1 = *(ISAMC_P*) p1;
398     ISAMC_P i2 = *(ISAMC_P*) p2;
399     zint d;
400
401     d = (isc_type (i1) - isc_type (i2));
402     if (d == 0)
403         d = isc_block (i1) - isc_block (i2);
404     if (d > 0)
405         return 1;
406     else if (d < 0)
407         return -1;
408     return 0;
409 }
410
411 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
412                  const char *term, int length, const char *flags,
413                  int preserve_position, int term_type)
414 {
415     logf (LOG_DEBUG, "rset_trunc no=%d", no);
416     if (no < 1)
417     {
418         rset_null_parms parms;
419         parms.rset_term = rset_term_create (term, length, flags, term_type);
420         return rset_create (rset_kind_null, &parms);
421     }
422     if (zi->reg->isams)
423     {
424         if (no == 1)
425         {
426             rset_isams_parms parms;
427
428             parms.pos = *isam_p;
429             parms.is = zi->reg->isams;
430             parms.rset_term = rset_term_create (term, length, flags,
431                                                 term_type);
432             return rset_create (rset_kind_isams, &parms);
433         }
434         qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
435     }
436     else if (zi->reg->isamc)
437     {
438         if (no == 1)
439         {
440             rset_isamc_parms parms;
441
442             parms.key_size = sizeof(struct it_key);
443             parms.cmp = key_compare_it;
444             parms.pos = *isam_p;
445             parms.is = zi->reg->isamc;
446             parms.rset_term = rset_term_create (term, length, flags,
447                                                 term_type);
448             return rset_create (rset_kind_isamc, &parms);
449         }
450 #if NEW_TRUNC
451         else if (no < 10000)
452         {
453             rset_m_or_parms parms;
454
455             parms.key_size = sizeof(struct it_key);
456             parms.cmp = key_compare_it;
457             parms.isc = zi->reg->isamc;
458             parms.isam_positions = isam_p;
459             parms.no_isam_positions = no;
460             parms.no_save_positions = 100000;
461             parms.rset_term = rset_term_create (term, length, flags,
462                                                 term_type);
463             return rset_create (rset_kind_m_or, &parms);
464         }
465 #endif
466         qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
467     }
468     else if (zi->reg->isamb)
469     {
470         if (no == 1)
471         {
472             rset_isamb_parms parms;
473             parms.key_size = sizeof(struct it_key);
474             parms.cmp = key_compare_it;
475             parms.pos = *isam_p;
476             parms.is = zi->reg->isamb;
477             parms.rset_term = rset_term_create (term, length, flags,
478                                                 term_type);
479             return rset_create (rset_kind_isamb, &parms);
480         }
481 #if 1
482         else if (no <10000 ) /* FIXME - hardcoded number */
483         {
484             rset_multior_parms m_parms;
485             rset_isamb_parms b_parms;
486             int i;
487             m_parms.key_size = sizeof(struct it_key);
488             m_parms.cmp = key_compare_it;
489             m_parms.no_rsets=no;
490             m_parms.rsets=xmalloc(sizeof(*m_parms.rsets)*no);
491             m_parms.rset_term = rset_term_create (term, length, flags,
492                                                 term_type);
493             b_parms.key_size = sizeof(struct it_key);
494             b_parms.cmp = key_compare_it;
495             b_parms.is = zi->reg->isamb;
496             /* FIXME - make it so that we can pass a null ptr to term */
497             /* needs changes in all rsets, here and there! */
498             for (i=0;i<no;i++)
499             {
500                 b_parms.pos = isam_p[i];
501                 b_parms.rset_term = rset_term_create (term, length, flags,
502                                                 term_type);
503                 m_parms.rsets[i]=rset_create (rset_kind_isamb, &b_parms);
504             }
505             return rset_create (rset_kind_multior, &m_parms);
506         } /* <10000 - rs_multior */
507 #endif        
508         qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
509     }
510     else
511     {
512         logf (LOG_WARN, "Unknown isam set in rset_trunc");
513         return rset_create (rset_kind_null, NULL);
514     }
515     return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
516                          preserve_position, term_type);
517 }
518