Fixed bug #245: Time for getting records changes a lot based on record
[idzebra-moved-to-github.git] / index / trunc.c
1 /* $Id: trunc.c,v 1.28.2.1 2004-08-11 13:31:10 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             if (isam_p[from+i]) {
444                 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i]);
445                 if (isamb_pp_read (ispt[i], ti->tmpbuf))
446                     heap_insert (ti, ti->tmpbuf, i);
447                 else
448                     isamb_pp_close (ispt[i]);
449             }
450         }
451         while (ti->heapnum)
452         {
453             int n = ti->indx[ti->ptr[1]];
454
455             rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
456             nn++;
457
458             if (preserve_position)
459             {
460                 heap_delete (ti);
461                 if (isamb_pp_read (ispt[n], ti->tmpbuf))
462                     heap_insert (ti, ti->tmpbuf, n);
463                 else
464                     isamb_pp_close (ispt[n]);
465             }
466             else
467             {
468                 while (1)
469                 {
470                     if (!isamb_pp_read (ispt[n], ti->tmpbuf))
471                     {
472                         heap_delete (ti);
473                         isamb_pp_close (ispt[n]);
474                         break;
475                     }
476                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
477                     {
478                         heap_delete (ti);
479                         heap_insert (ti, ti->tmpbuf, n);
480                         break;
481                     }
482                 }
483             }
484         }
485         heap_close (ti);
486         xfree (ispt);
487     }
488     else
489         logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
490
491     parms.rset_term->nn = nn;
492     rset_close (result, result_rsfd);
493     return result;
494 }
495
496 static int isams_trunc_cmp (const void *p1, const void *p2)
497 {
498     ISAMS_P i1 = *(ISAMS_P*) p1;
499     ISAMS_P i2 = *(ISAMS_P*) p2;
500
501     return i1 - i2;
502 }
503
504 static int isam_trunc_cmp (const void *p1, const void *p2)
505 {
506     ISAM_P i1 = *(ISAM_P*) p1;
507     ISAM_P i2 = *(ISAM_P*) p2;
508     int d;
509
510     d = is_type (i1) - is_type (i2);
511     if (d)
512         return d;
513     return is_block (i1) - is_block (i2);
514 }
515
516 static int isamc_trunc_cmp (const void *p1, const void *p2)
517 {
518     ISAMC_P i1 = *(ISAMC_P*) p1;
519     ISAMC_P i2 = *(ISAMC_P*) p2;
520     int d;
521
522     d = isc_type (i1) - isc_type (i2);
523     if (d)
524         return d;
525     return isc_block (i1) - isc_block (i2);
526 }
527 static int isamd_trunc_cmp (const void *p1, const void *p2)
528 {
529     ISAMD_P i1 = *(ISAMD_P*) p1;
530     ISAMD_P i2 = *(ISAMD_P*) p2;
531     int d;
532
533     d = isamd_type (i1) - isamd_type (i2);
534     if (d)
535         return d;
536     return isamd_block (i1) - isamd_block (i2);
537 }
538
539 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
540                  const char *term, int length, const char *flags,
541                  int preserve_position, int term_type)
542 {
543     logf (LOG_DEBUG, "rset_trunc no=%d", no);
544     if (no < 1)
545     {
546         rset_null_parms parms;
547         parms.rset_term = rset_term_create (term, length, flags, term_type);
548         return rset_create (rset_kind_null, &parms);
549     }
550     if (zi->reg->isams)
551     {
552         if (no == 1)
553         {
554             rset_isams_parms parms;
555
556             parms.pos = *isam_p;
557             parms.is = zi->reg->isams;
558             parms.rset_term = rset_term_create (term, length, flags,
559                                                 term_type);
560             return rset_create (rset_kind_isams, &parms);
561         }
562         qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
563     }
564     else if (zi->reg->isam)
565     {
566         if (no == 1)
567         {
568             rset_isam_parms parms;
569
570             parms.pos = *isam_p;
571             parms.is = zi->reg->isam;
572             parms.rset_term = rset_term_create (term, length, flags,
573                                                 term_type);
574             return rset_create (rset_kind_isam, &parms);
575         }
576         qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
577     }
578     else if (zi->reg->isamc)
579     {
580         if (no == 1)
581         {
582             rset_isamc_parms parms;
583
584             parms.key_size = sizeof(struct it_key);
585             parms.cmp = key_compare_it;
586             parms.pos = *isam_p;
587             parms.is = zi->reg->isamc;
588             parms.rset_term = rset_term_create (term, length, flags,
589                                                 term_type);
590             return rset_create (rset_kind_isamc, &parms);
591         }
592 #if NEW_TRUNC
593         else if (no < 10000)
594         {
595             rset_m_or_parms parms;
596
597             parms.key_size = sizeof(struct it_key);
598             parms.cmp = key_compare_it;
599             parms.isc = zi->reg->isamc;
600             parms.isam_positions = isam_p;
601             parms.no_isam_positions = no;
602             parms.no_save_positions = 100000;
603             parms.rset_term = rset_term_create (term, length, flags,
604                                                 term_type);
605             return rset_create (rset_kind_m_or, &parms);
606         }
607 #endif
608         qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
609     }
610     else if (zi->reg->isamd)
611     {
612         if (no == 1)
613         {
614             rset_isamd_parms parms;
615
616             logf(LOG_FATAL, "isam_d does not (currently) support truncs");
617             abort();
618             /* parms.pos = *isam_p; */
619             parms.is = zi->reg->isamd;
620             parms.rset_term = rset_term_create (term, length, flags,
621                                                 term_type);
622             return rset_create (rset_kind_isamd, &parms);
623         }
624 #if NEW_TRUNC_NOT_DONE_FOR_ISAM_D
625         else if (no < 10000)
626         {
627             rset_m_or_parms parms;
628
629             parms.key_size = sizeof(struct it_key);
630             parms.cmp = key_compare_it;
631             parms.isc = 0;
632             parms.isamd=zi->reg->isamd;
633             parms.isam_positions = isam_p;
634             parms.no_isam_positions = no;
635             parms.no_save_positions = 100000;
636             parms.rset_term = rset_term_create (term, length, flags);
637             return rset_create (rset_kind_m_or, &parms);
638         }
639 #endif
640         qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
641     }
642     else if (zi->reg->isamb)
643     {
644         if (no == 1)
645         {
646             rset_isamb_parms parms;
647
648             parms.key_size = sizeof(struct it_key);
649             parms.cmp = key_compare_it;
650             parms.pos = *isam_p;
651             parms.is = zi->reg->isamb;
652             parms.rset_term = rset_term_create (term, length, flags,
653                                                 term_type);
654             return rset_create (rset_kind_isamb, &parms);
655         }
656         qsort (isam_p, no, sizeof(*isam_p), isamd_trunc_cmp);
657     }
658     else
659     {
660         logf (LOG_WARN, "Unknown isam set in rset_trunc");
661         return rset_create (rset_kind_null, NULL);
662     }
663     return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
664                          preserve_position, term_type);
665 }
666