Integrated ranking algorithm from Liverpool University
[idzebra-moved-to-github.git] / index / trunc.c
1 /* $Id: trunc.c,v 1.28 2003-03-26 16:41:48 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 <rsisamd.h>
36 #include <rsisamb.h>
37 #if NEW_TRUNC
38 #include <rsm_or.h>
39 #endif
40
41 struct trunc_info {
42     int  *ptr;
43     int  *indx;
44     char **heap;
45     int  heapnum;
46     int  (*cmp)(const void *p1, const void *p2);
47     int  keysize;
48     char *swapbuf;
49     char *tmpbuf;
50     char *buf;
51 };
52
53 static void heap_swap (struct trunc_info *ti, int i1, int i2)
54 {
55     int swap;
56
57     swap = ti->ptr[i1];
58     ti->ptr[i1] = ti->ptr[i2];
59     ti->ptr[i2] = swap;
60 }
61
62 static void heap_delete (struct trunc_info *ti)
63 {
64     int cur = 1, child = 2;
65
66     heap_swap (ti, 1, ti->heapnum--);
67     while (child <= ti->heapnum) {
68         if (child < ti->heapnum &&
69             (*ti->cmp)(ti->heap[ti->ptr[child]],
70                        ti->heap[ti->ptr[1+child]]) > 0)
71             child++;
72         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
73                        ti->heap[ti->ptr[child]]) > 0)
74         {
75             heap_swap (ti, cur, child);
76             cur = child;
77             child = 2*cur;
78         }
79         else
80             break;
81     }
82 }
83
84 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
85 {
86     int cur, parent;
87
88     cur = ++(ti->heapnum);
89     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
90     ti->indx[ti->ptr[cur]] = indx;
91     parent = cur/2;
92     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
93                                 ti->heap[ti->ptr[cur]]) > 0)
94     {
95         heap_swap (ti, cur, parent);
96         cur = parent;
97         parent = cur/2;
98     }
99 }
100
101 static struct trunc_info *heap_init (int size, int key_size,
102                                      int (*cmp)(const void *p1,
103                                                 const void *p2))
104 {
105     struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
106     int i;
107
108     ++size;
109     ti->heapnum = 0;
110     ti->keysize = key_size;
111     ti->cmp = cmp;
112     ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
113     ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
114     ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
115     ti->swapbuf = (char *) xmalloc (ti->keysize);
116     ti->tmpbuf = (char *) xmalloc (ti->keysize);
117     ti->buf = (char *) xmalloc (size * ti->keysize);
118     for (i = size; --i >= 0; )
119     {
120         ti->ptr[i] = i;
121         ti->heap[i] = ti->buf + ti->keysize * i;
122     }
123     return ti;
124 }
125
126 static void heap_close (struct trunc_info *ti)
127 {
128     xfree (ti->ptr);
129     xfree (ti->indx);
130     xfree (ti->heap);
131     xfree (ti->swapbuf);
132     xfree (ti->tmpbuf);
133     xfree (ti->buf);
134     xfree (ti);
135 }
136
137 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
138                           const char *flags, ISAMS_P *isam_p, int from, int to,
139                           int merge_chunk, int preserve_position,
140                           int term_type)
141 {
142     RSET result; 
143     RSFD result_rsfd;
144     rset_temp_parms parms;
145     int nn = 0;
146
147     parms.cmp = key_compare_it;
148     parms.key_size = sizeof(struct it_key);
149     parms.temp_path = res_get (zi->res, "setTmpDir");
150     parms.rset_term = rset_term_create (term, length, flags, term_type);
151     result = rset_create (rset_kind_temp, &parms);
152     result_rsfd = rset_open (result, RSETF_WRITE);
153
154     if (to - from > merge_chunk)
155     {
156         RSFD *rsfd;
157         RSET *rset;
158         int term_index;
159         int i, i_add = (to-from)/merge_chunk + 1;
160         struct trunc_info *ti;
161         int rscur = 0;
162         int rsmax = (to-from)/i_add + 1;
163         
164         rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
165         rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
166         
167         for (i = from; i < to; i += i_add)
168         {
169             if (i_add <= to - i)
170                 rset[rscur] = rset_trunc_r (zi, term, length, flags,
171                                             isam_p, i, i+i_add,
172                                             merge_chunk, preserve_position,
173                                             term_type);
174             else
175                 rset[rscur] = rset_trunc_r (zi, term, length, flags,
176                                             isam_p, i, to,
177                                             merge_chunk, preserve_position,
178                                             term_type);
179             rscur++;
180         }
181         ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
182         for (i = rscur; --i >= 0; )
183         {
184             rsfd[i] = rset_open (rset[i], RSETF_READ);
185             if (rset_read (rset[i], rsfd[i], ti->tmpbuf, &term_index))
186                 heap_insert (ti, ti->tmpbuf, i);
187             else
188             {
189                 rset_close (rset[i], rsfd[i]);
190                 rset_delete (rset[i]);
191             }
192         }
193         while (ti->heapnum)
194         {
195             int n = ti->indx[ti->ptr[1]];
196
197             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
198             nn++;
199
200             while (1)
201             {
202                 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf, &term_index))
203                 {
204                     heap_delete (ti);
205                     rset_close (rset[n], rsfd[n]);
206                     rset_delete (rset[n]);
207                     break;
208                 }
209                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
210                 {
211                     heap_delete (ti);
212                     heap_insert (ti, ti->tmpbuf, n);
213                     break;
214                 }
215             }
216         }
217         xfree (rset);
218         xfree (rsfd);
219         heap_close (ti);
220     }
221     else if (zi->reg->isam)
222     {
223         ISPT *ispt;
224         int i;
225         struct trunc_info *ti;
226
227         ispt = (ISPT *) xmalloc (sizeof(*ispt) * (to-from));
228
229         ti = heap_init (to-from, sizeof(struct it_key),
230                         key_compare_it);
231         for (i = to-from; --i >= 0; )
232         {
233             ispt[i] = is_position (zi->reg->isam, isam_p[from+i]);
234             if (is_readkey (ispt[i], ti->tmpbuf))
235                 heap_insert (ti, ti->tmpbuf, i);
236             else
237                 is_pt_free (ispt[i]);
238         }
239         while (ti->heapnum)
240         {
241             int n = ti->indx[ti->ptr[1]];
242
243             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
244             nn++;
245             if (preserve_position)
246             {
247 /* section that preserve all keys */
248                 heap_delete (ti);
249                 if (is_readkey (ispt[n], ti->tmpbuf))
250                     heap_insert (ti, ti->tmpbuf, n);
251                 else
252                     is_pt_free (ispt[n]);
253             }
254             else
255             {
256 /* section that preserve all keys with unique sysnos */
257                 while (1)
258                 {
259                     if (!is_readkey (ispt[n], ti->tmpbuf))
260                     {
261                         heap_delete (ti);
262                         is_pt_free (ispt[n]);
263                         break;
264                     }
265                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
266                     {
267                         heap_delete (ti);
268                         heap_insert (ti, ti->tmpbuf, n);
269                         break;
270                     }
271                 }
272             }
273         }
274         heap_close (ti);
275         xfree (ispt);
276     }
277     else if (zi->reg->isamc)
278     {
279         ISAMC_PP *ispt;
280         int i;
281         struct trunc_info *ti;
282
283         ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
284
285         ti = heap_init (to-from, sizeof(struct it_key),
286                         key_compare_it);
287         for (i = to-from; --i >= 0; )
288         {
289             ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
290             if (isc_pp_read (ispt[i], ti->tmpbuf))
291                 heap_insert (ti, ti->tmpbuf, i);
292             else
293                 isc_pp_close (ispt[i]);
294         }
295         while (ti->heapnum)
296         {
297             int n = ti->indx[ti->ptr[1]];
298
299             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
300             nn++;
301             if (preserve_position)
302             {
303                 heap_delete (ti);
304                 if (isc_pp_read (ispt[n], ti->tmpbuf))
305                     heap_insert (ti, ti->tmpbuf, n);
306                 else
307                     isc_pp_close (ispt[n]);
308             }
309             else
310             {
311                 while (1)
312                 {
313                     if (!isc_pp_read (ispt[n], ti->tmpbuf))
314                     {
315                         heap_delete (ti);
316                         isc_pp_close (ispt[n]);
317                         break;
318                     }
319                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
320                     {
321                         heap_delete (ti);
322                         heap_insert (ti, ti->tmpbuf, n);
323                         break;
324                     }
325                 }
326             }
327         }
328         heap_close (ti);
329         xfree (ispt);
330     }
331
332     else if (zi->reg->isamd)
333     {
334         ISAMD_PP *ispt;
335         int i;
336         struct trunc_info *ti;
337
338         ispt = (ISAMD_PP *) xmalloc (sizeof(*ispt) * (to-from));
339
340         ti = heap_init (to-from, sizeof(struct it_key),
341                         key_compare_it);
342         for (i = to-from; --i >= 0; )
343         {
344             logf(LOG_FATAL, "isam_d does not (currently) support truncs");
345             abort();
346             /*ispt[i] = isamd_pp_open (zi->reg->isamd, isam_p[from+i]); */
347             if (isamd_pp_read (ispt[i], ti->tmpbuf))
348                 heap_insert (ti, ti->tmpbuf, i);
349             else
350                 isamd_pp_close (ispt[i]);
351         }
352         while (ti->heapnum)
353         {
354             int n = ti->indx[ti->ptr[1]];
355
356             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
357             nn++;
358 #if 0
359 /* section that preserve all keys */
360             heap_delete (ti);
361             if (isamd_pp_read (ispt[n], ti->tmpbuf))
362                 heap_insert (ti, ti->tmpbuf, n);
363             else
364                 isamd_pp_close (ispt[n]);
365 #else
366 /* section that preserve all keys with unique sysnos */
367             while (1)
368             {
369                 if (!isamd_pp_read (ispt[n], ti->tmpbuf))
370                 {
371                     heap_delete (ti);
372                     isamd_pp_close (ispt[n]);
373                     break;
374                 }
375                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
376                 {
377                     heap_delete (ti);
378                     heap_insert (ti, ti->tmpbuf, n);
379                     break;
380                 }
381             }
382 #endif
383         }
384         heap_close (ti);
385         xfree (ispt);
386     }
387     else if (zi->reg->isams)
388     {
389         ISAMS_PP *ispt;
390         int i;
391         struct trunc_info *ti;
392         int nn = 0;
393
394         ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
395
396         ti = heap_init (to-from, sizeof(struct it_key),
397                         key_compare_it);
398         for (i = to-from; --i >= 0; )
399         {
400             ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
401             if (isams_pp_read (ispt[i], ti->tmpbuf))
402                 heap_insert (ti, ti->tmpbuf, i);
403             else
404                 isams_pp_close (ispt[i]);
405         }
406         while (ti->heapnum)
407         {
408             int n = ti->indx[ti->ptr[1]];
409
410             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
411             nn++;
412             while (1)
413             {
414                 if (!isams_pp_read (ispt[n], ti->tmpbuf))
415                 {
416                     heap_delete (ti);
417                     isams_pp_close (ispt[n]);
418                     break;
419                 }
420                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
421                 {
422                     heap_delete (ti);
423                     heap_insert (ti, ti->tmpbuf, n);
424                     break;
425                 }
426             }
427         }
428         heap_close (ti);
429         xfree (ispt);
430     }
431     else if (zi->reg->isamb)
432     {
433         ISAMB_PP *ispt;
434         int i;
435         struct trunc_info *ti;
436
437         ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
438
439         ti = heap_init (to-from, sizeof(struct it_key),
440                         key_compare_it);
441         for (i = to-from; --i >= 0; )
442         {
443             ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i]);
444             if (isamb_pp_read (ispt[i], ti->tmpbuf))
445                 heap_insert (ti, ti->tmpbuf, i);
446             else
447                 isamb_pp_close (ispt[i]);
448         }
449         while (ti->heapnum)
450         {
451             int n = ti->indx[ti->ptr[1]];
452
453             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
454             nn++;
455
456             if (preserve_position)
457             {
458                 heap_delete (ti);
459                 if (isamb_pp_read (ispt[n], ti->tmpbuf))
460                     heap_insert (ti, ti->tmpbuf, n);
461                 else
462                     isamb_pp_close (ispt[n]);
463             }
464             else
465             {
466                 while (1)
467                 {
468                     if (!isamb_pp_read (ispt[n], ti->tmpbuf))
469                     {
470                         heap_delete (ti);
471                         isamb_pp_close (ispt[n]);
472                         break;
473                     }
474                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
475                     {
476                         heap_delete (ti);
477                         heap_insert (ti, ti->tmpbuf, n);
478                         break;
479                     }
480                 }
481             }
482         }
483         heap_close (ti);
484         xfree (ispt);
485     }
486     else
487         logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
488
489     parms.rset_term->nn = nn;
490     rset_close (result, result_rsfd);
491     return result;
492 }
493
494 static int isams_trunc_cmp (const void *p1, const void *p2)
495 {
496     ISAMS_P i1 = *(ISAMS_P*) p1;
497     ISAMS_P i2 = *(ISAMS_P*) p2;
498
499     return i1 - i2;
500 }
501
502 static int isam_trunc_cmp (const void *p1, const void *p2)
503 {
504     ISAM_P i1 = *(ISAM_P*) p1;
505     ISAM_P i2 = *(ISAM_P*) p2;
506     int d;
507
508     d = is_type (i1) - is_type (i2);
509     if (d)
510         return d;
511     return is_block (i1) - is_block (i2);
512 }
513
514 static int isamc_trunc_cmp (const void *p1, const void *p2)
515 {
516     ISAMC_P i1 = *(ISAMC_P*) p1;
517     ISAMC_P i2 = *(ISAMC_P*) p2;
518     int d;
519
520     d = isc_type (i1) - isc_type (i2);
521     if (d)
522         return d;
523     return isc_block (i1) - isc_block (i2);
524 }
525 static int isamd_trunc_cmp (const void *p1, const void *p2)
526 {
527     ISAMD_P i1 = *(ISAMD_P*) p1;
528     ISAMD_P i2 = *(ISAMD_P*) p2;
529     int d;
530
531     d = isamd_type (i1) - isamd_type (i2);
532     if (d)
533         return d;
534     return isamd_block (i1) - isamd_block (i2);
535 }
536
537 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
538                  const char *term, int length, const char *flags,
539                  int preserve_position, int term_type)
540 {
541     logf (LOG_DEBUG, "rset_trunc no=%d", no);
542     if (no < 1)
543     {
544         rset_null_parms parms;
545         parms.rset_term = rset_term_create (term, length, flags, term_type);
546         return rset_create (rset_kind_null, &parms);
547     }
548     if (zi->reg->isams)
549     {
550         if (no == 1)
551         {
552             rset_isams_parms parms;
553
554             parms.pos = *isam_p;
555             parms.is = zi->reg->isams;
556             parms.rset_term = rset_term_create (term, length, flags,
557                                                 term_type);
558             return rset_create (rset_kind_isams, &parms);
559         }
560         qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
561     }
562     else if (zi->reg->isam)
563     {
564         if (no == 1)
565         {
566             rset_isam_parms parms;
567
568             parms.pos = *isam_p;
569             parms.is = zi->reg->isam;
570             parms.rset_term = rset_term_create (term, length, flags,
571                                                 term_type);
572             return rset_create (rset_kind_isam, &parms);
573         }
574         qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
575     }
576     else if (zi->reg->isamc)
577     {
578         if (no == 1)
579         {
580             rset_isamc_parms parms;
581
582             parms.key_size = sizeof(struct it_key);
583             parms.cmp = key_compare_it;
584             parms.pos = *isam_p;
585             parms.is = zi->reg->isamc;
586             parms.rset_term = rset_term_create (term, length, flags,
587                                                 term_type);
588             return rset_create (rset_kind_isamc, &parms);
589         }
590 #if NEW_TRUNC
591         else if (no < 10000)
592         {
593             rset_m_or_parms parms;
594
595             parms.key_size = sizeof(struct it_key);
596             parms.cmp = key_compare_it;
597             parms.isc = zi->reg->isamc;
598             parms.isam_positions = isam_p;
599             parms.no_isam_positions = no;
600             parms.no_save_positions = 100000;
601             parms.rset_term = rset_term_create (term, length, flags,
602                                                 term_type);
603             return rset_create (rset_kind_m_or, &parms);
604         }
605 #endif
606         qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
607     }
608     else if (zi->reg->isamd)
609     {
610         if (no == 1)
611         {
612             rset_isamd_parms parms;
613
614             logf(LOG_FATAL, "isam_d does not (currently) support truncs");
615             abort();
616             /* parms.pos = *isam_p; */
617             parms.is = zi->reg->isamd;
618             parms.rset_term = rset_term_create (term, length, flags,
619                                                 term_type);
620             return rset_create (rset_kind_isamd, &parms);
621         }
622 #if NEW_TRUNC_NOT_DONE_FOR_ISAM_D
623         else if (no < 10000)
624         {
625             rset_m_or_parms parms;
626
627             parms.key_size = sizeof(struct it_key);
628             parms.cmp = key_compare_it;
629             parms.isc = 0;
630             parms.isamd=zi->reg->isamd;
631             parms.isam_positions = isam_p;
632             parms.no_isam_positions = no;
633             parms.no_save_positions = 100000;
634             parms.rset_term = rset_term_create (term, length, flags);
635             return rset_create (rset_kind_m_or, &parms);
636         }
637 #endif
638         qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
639     }
640     else if (zi->reg->isamb)
641     {
642         if (no == 1)
643         {
644             rset_isamb_parms parms;
645
646             parms.key_size = sizeof(struct it_key);
647             parms.cmp = key_compare_it;
648             parms.pos = *isam_p;
649             parms.is = zi->reg->isamb;
650             parms.rset_term = rset_term_create (term, length, flags,
651                                                 term_type);
652             return rset_create (rset_kind_isamb, &parms);
653         }
654         qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
655     }
656     else
657     {
658         logf (LOG_WARN, "Unknown isam set in rset_trunc");
659         return rset_create (rset_kind_null, NULL);
660     }
661     return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
662                          preserve_position, term_type);
663 }
664