Passing a TERMID to rsets when creating, and getting it back when reading.
[idzebra-moved-to-github.git] / index / trunc.c
1 /* $Id: trunc.c,v 1.46 2004-10-15 10:07:32 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 #include "index.h"
28 #include <rset.h>
29
30 struct trunc_info {
31     int  *ptr;
32     int  *indx;
33     char **heap;
34     int  heapnum;
35     int  (*cmp)(const void *p1, const void *p2);
36     int  keysize;
37     char *swapbuf;
38     char *tmpbuf;
39     char *buf;
40 };
41
42 static void heap_swap (struct trunc_info *ti, int i1, int i2)
43 {
44     int swap;
45
46     swap = ti->ptr[i1];
47     ti->ptr[i1] = ti->ptr[i2];
48     ti->ptr[i2] = swap;
49 }
50
51 static void heap_delete (struct trunc_info *ti)
52 {
53     int cur = 1, child = 2;
54
55     heap_swap (ti, 1, ti->heapnum--);
56     while (child <= ti->heapnum) {
57         if (child < ti->heapnum &&
58             (*ti->cmp)(ti->heap[ti->ptr[child]],
59                        ti->heap[ti->ptr[1+child]]) > 0)
60             child++;
61         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
62                        ti->heap[ti->ptr[child]]) > 0)
63         {
64             heap_swap (ti, cur, child);
65             cur = child;
66             child = 2*cur;
67         }
68         else
69             break;
70     }
71 }
72
73 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
74 {
75     int cur, parent;
76
77     cur = ++(ti->heapnum);
78     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
79     ti->indx[ti->ptr[cur]] = indx;
80     parent = cur/2;
81     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
82                                 ti->heap[ti->ptr[cur]]) > 0)
83     {
84         heap_swap (ti, cur, parent);
85         cur = parent;
86         parent = cur/2;
87     }
88 }
89
90 static struct trunc_info *heap_init (int size, int key_size,
91                                      int (*cmp)(const void *p1,
92                                                 const void *p2))
93 {
94     struct trunc_info *ti = (struct trunc_info *) xmalloc (sizeof(*ti));
95     int i;
96
97     ++size;
98     ti->heapnum = 0;
99     ti->keysize = key_size;
100     ti->cmp = cmp;
101     ti->indx = (int *) xmalloc (size * sizeof(*ti->indx));
102     ti->heap = (char **) xmalloc (size * sizeof(*ti->heap));
103     ti->ptr = (int *) xmalloc (size * sizeof(*ti->ptr));
104     ti->swapbuf = (char *) xmalloc (ti->keysize);
105     ti->tmpbuf = (char *) xmalloc (ti->keysize);
106     ti->buf = (char *) xmalloc (size * ti->keysize);
107     for (i = size; --i >= 0; )
108     {
109         ti->ptr[i] = i;
110         ti->heap[i] = ti->buf + ti->keysize * i;
111     }
112     return ti;
113 }
114
115 static void heap_close (struct trunc_info *ti)
116 {
117     xfree (ti->ptr);
118     xfree (ti->indx);
119     xfree (ti->heap);
120     xfree (ti->swapbuf);
121     xfree (ti->tmpbuf);
122     xfree (ti->buf);
123     xfree (ti);
124 }
125
126 static RSET rset_trunc_r (ZebraHandle zi, const char *term, int length,
127                           const char *flags, ISAMS_P *isam_p, int from, int to,
128                           int merge_chunk, int preserve_position,
129                           int term_type, NMEM rset_nmem,
130                           const struct key_control *kctrl, int scope)
131 {
132     RSET result; 
133     RSFD result_rsfd;
134     int nn = 0;
135
136     /*
137     rset_temp_parms parms;
138     parms.cmp = key_compare_it;
139     parms.key_size = sizeof(struct it_key);
140     parms.temp_path = res_get (zi->res, "setTmpDir");
141     result = rset_create (rset_kind_temp, &parms);
142     */
143     result=rstemp_create( rset_nmem,kctrl, scope,
144             res_get (zi->res, "setTmpDir"));
145     result_rsfd = rset_open (result, RSETF_WRITE);
146
147     if (to - from > merge_chunk)
148     {
149         RSFD *rsfd;
150         RSET *rset;
151         int i, i_add = (to-from)/merge_chunk + 1;
152         struct trunc_info *ti;
153         int rscur = 0;
154         int rsmax = (to-from)/i_add + 1;
155         
156         rset = (RSET *) xmalloc (sizeof(*rset) * rsmax);
157         rsfd = (RSFD *) xmalloc (sizeof(*rsfd) * rsmax);
158         
159         for (i = from; i < to; i += i_add)
160         {
161             if (i_add <= to - i)
162                 rset[rscur] = rset_trunc_r (zi, term, length, flags,
163                                             isam_p, i, i+i_add,
164                                             merge_chunk, preserve_position,
165                                             term_type, rset_nmem, 
166                                             kctrl, scope);
167             else
168                 rset[rscur] = rset_trunc_r (zi, term, length, flags,
169                                             isam_p, i, to,
170                                             merge_chunk, preserve_position,
171                                             term_type, rset_nmem, 
172                                             kctrl, scope);
173             rscur++;
174         }
175         ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
176         for (i = rscur; --i >= 0; )
177         {
178             rsfd[i] = rset_open (rset[i], RSETF_READ);
179             if (rset_read(rsfd[i], ti->tmpbuf,0))
180                 heap_insert (ti, ti->tmpbuf, i);
181             else
182             {
183                 rset_close (rsfd[i]);
184                 rset_delete (rset[i]);
185             }
186         }
187         while (ti->heapnum)
188         {
189             int n = ti->indx[ti->ptr[1]];
190
191             rset_write (result_rsfd, ti->heap[ti->ptr[1]]);
192             nn++;
193
194             while (1)
195             {
196                 if (!rset_read (rsfd[n], ti->tmpbuf,0))
197                 {
198                     heap_delete (ti);
199                     rset_close (rsfd[n]);
200                     rset_delete (rset[n]);
201                     break;
202                 }
203                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
204                 {
205                     heap_delete (ti);
206                     heap_insert (ti, ti->tmpbuf, n);
207                     break;
208                 }
209             }
210         }
211         xfree (rset);
212         xfree (rsfd);
213         heap_close (ti);
214     }
215     else if (zi->reg->isamc)
216     {
217         ISAMC_PP *ispt;
218         int i;
219         struct trunc_info *ti;
220
221         ispt = (ISAMC_PP *) xmalloc (sizeof(*ispt) * (to-from));
222
223         ti = heap_init (to-from, sizeof(struct it_key),
224                         key_compare_it);
225         for (i = to-from; --i >= 0; )
226         {
227             ispt[i] = isc_pp_open (zi->reg->isamc, isam_p[from+i]);
228             if (isc_pp_read (ispt[i], ti->tmpbuf))
229                 heap_insert (ti, ti->tmpbuf, i);
230             else
231                 isc_pp_close (ispt[i]);
232         }
233         while (ti->heapnum)
234         {
235             int n = ti->indx[ti->ptr[1]];
236
237             rset_write (result_rsfd, ti->heap[ti->ptr[1]]);
238             nn++;
239             if (preserve_position)
240             {
241                 heap_delete (ti);
242                 if (isc_pp_read (ispt[n], ti->tmpbuf))
243                     heap_insert (ti, ti->tmpbuf, n);
244                 else
245                     isc_pp_close (ispt[n]);
246             }
247             else
248             {
249                 while (1)
250                 {
251                     if (!isc_pp_read (ispt[n], ti->tmpbuf))
252                     {
253                         heap_delete (ti);
254                         isc_pp_close (ispt[n]);
255                         break;
256                     }
257                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
258                     {
259                         heap_delete (ti);
260                         heap_insert (ti, ti->tmpbuf, n);
261                         break;
262                     }
263                 }
264             }
265         }
266         heap_close (ti);
267         xfree (ispt);
268     }
269     else if (zi->reg->isams)
270     {
271         ISAMS_PP *ispt;
272         int i;
273         struct trunc_info *ti;
274         int nn = 0;
275
276         ispt = (ISAMS_PP *) xmalloc (sizeof(*ispt) * (to-from));
277
278         ti = heap_init (to-from, sizeof(struct it_key),
279                         key_compare_it);
280         for (i = to-from; --i >= 0; )
281         {
282             ispt[i] = isams_pp_open (zi->reg->isams, isam_p[from+i]);
283             if (isams_pp_read (ispt[i], ti->tmpbuf))
284                 heap_insert (ti, ti->tmpbuf, i);
285             else
286                 isams_pp_close (ispt[i]);
287         }
288         while (ti->heapnum)
289         {
290             int n = ti->indx[ti->ptr[1]];
291
292             rset_write (result_rsfd, ti->heap[ti->ptr[1]]);
293             nn++;
294             while (1)
295             {
296                 if (!isams_pp_read (ispt[n], ti->tmpbuf))
297                 {
298                     heap_delete (ti);
299                     isams_pp_close (ispt[n]);
300                     break;
301                 }
302                 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
303                 {
304                     heap_delete (ti);
305                     heap_insert (ti, ti->tmpbuf, n);
306                     break;
307                 }
308             }
309         }
310         heap_close (ti);
311         xfree (ispt);
312     }
313     else if (zi->reg->isamb)
314     {
315         ISAMB_PP *ispt;
316         int i;
317         struct trunc_info *ti;
318
319         ispt = (ISAMB_PP *) xmalloc (sizeof(*ispt) * (to-from));
320
321         ti = heap_init (to-from, sizeof(struct it_key),
322                         key_compare_it);
323         for (i = to-from; --i >= 0; )
324         {
325             if (isam_p[from+i]) {
326                 ispt[i] = isamb_pp_open (zi->reg->isamb, isam_p[from+i], scope);
327                 if (isamb_pp_read (ispt[i], ti->tmpbuf))
328                     heap_insert (ti, ti->tmpbuf, i);
329                 else
330                     isamb_pp_close (ispt[i]);
331             }
332         }
333         while (ti->heapnum)
334         {
335             int n = ti->indx[ti->ptr[1]];
336
337             rset_write (result_rsfd, ti->heap[ti->ptr[1]]);
338             nn++;
339
340             if (preserve_position)
341             {
342                 heap_delete (ti);
343                 if (isamb_pp_read (ispt[n], ti->tmpbuf))
344                     heap_insert (ti, ti->tmpbuf, n);
345                 else
346                     isamb_pp_close (ispt[n]);
347             }
348             else
349             {
350                 while (1)
351                 {
352                     if (!isamb_pp_read (ispt[n], ti->tmpbuf))
353                     {
354                         heap_delete (ti);
355                         isamb_pp_close (ispt[n]);
356                         break;
357                     }
358                     if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
359                     {
360                         heap_delete (ti);
361                         heap_insert (ti, ti->tmpbuf, n);
362                         break;
363                     }
364                 }
365             }
366         }
367         heap_close (ti);
368         xfree (ispt);
369     }
370     else
371         logf (LOG_WARN, "Unknown isam set in rset_trunc_r");
372
373     rset_close (result_rsfd);
374     return result;
375 }
376
377 static int isams_trunc_cmp (const void *p1, const void *p2)
378 {
379     ISAMS_P i1 = *(ISAMS_P*) p1;
380     ISAMS_P i2 = *(ISAMS_P*) p2;
381
382     if (i1 > i2)
383         return 1;
384     else if (i1 < i2)
385         return -1;
386     return 0;
387 }
388
389 static int isamc_trunc_cmp (const void *p1, const void *p2)
390 {
391     ISAMC_P i1 = *(ISAMC_P*) p1;
392     ISAMC_P i2 = *(ISAMC_P*) p2;
393     zint d;
394
395     d = (isc_type (i1) - isc_type (i2));
396     if (d == 0)
397         d = isc_block (i1) - isc_block (i2);
398     if (d > 0)
399         return 1;
400     else if (d < 0)
401         return -1;
402     return 0;
403 }
404
405 RSET rset_trunc (ZebraHandle zi, ISAMS_P *isam_p, int no,
406                  const char *term, int length, const char *flags,
407                  int preserve_position, int term_type, NMEM rset_nmem,
408                  const struct key_control *kctrl, int scope)
409 {
410     logf (LOG_DEBUG, "rset_trunc no=%d", no);
411     if (no < 1)
412         return rsnull_create (rset_nmem,kctrl);
413     if (zi->reg->isams)
414     {
415         if (no == 1)
416             return rsisams_create(rset_nmem, kctrl, scope,
417                     zi->reg->isams, *isam_p, 
418                     0 /*FIXME - use proper TERMID*/); 
419         qsort (isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
420     }
421     else if (zi->reg->isamc)
422     {
423         if (no == 1)
424             return rsisamc_create(rset_nmem, kctrl, scope,
425                     zi->reg->isamc, *isam_p,
426                     0 /*FIXME - use proper TERMID*/); 
427         qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
428     }
429     else if (zi->reg->isamb)
430     {
431         if (no == 1)
432             return rsisamb_create(rset_nmem,kctrl, scope,
433                     zi->reg->isamb, *isam_p, 0 /* FIXME - TERMID */ );
434         else if (no <10000 ) /* FIXME - hardcoded number */
435         {
436             RSET r;
437             RSET *rsets=xmalloc(no*sizeof(RSET)); /* use nmem! */
438             int i;
439             for (i=0;i<no;i++)
440                 rsets[i]=rsisamb_create(rset_nmem, kctrl, scope,
441                     zi->reg->isamb, isam_p[i],
442                     0 /* FIXME - use a proper TERMID */ );
443             r=rsmultior_create( rset_nmem, kctrl, scope, no, rsets);
444             xfree(rsets);
445             return r;
446         } 
447         qsort (isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
448     }
449     else
450     {
451         logf (LOG_WARN, "Unknown isam set in rset_trunc");
452         return rsnull_create (rset_nmem, kctrl);
453     }
454     return rset_trunc_r (zi, term, length, flags, isam_p, 0, no, 100,
455                          preserve_position, term_type, rset_nmem,kctrl,scope);
456 }
457